Page 1 of 2
Can't load kernel from floppy
Posted: Sat May 18, 2013 11:15 am
by matt8110
I am new to OS development and i am having trouble loading my kernel from a floppy, here is my code.
Bootloader
Code: Select all
bits 16
org 0x7C00
;Jumping to main
jmp main
;Main
main:
call ResetFloppy
call LoadKernel
jmp hang
;Reset floppy drive
ResetFloppy:
mov ah, 0x00 ;Reset function
mov dl, 0x00 ;Floppy
int 0x13 ;Calling interupt
;If there is no error then return
jnc Return
;If there is a error display it
mov si, FloppyError
call PrintString
ret
;Loading kernel from floppy
LoadKernel:
mov ah, 0x02 ;Read from drive
mov al, 0x01 ;Sectors(512 bytes) to read
mov ch, 0x02 ;Track
mov cl, 0x0f ;Sector
mov dh, 0x01 ;Head AKA side of floppy
mov dl, 0x00 ;Drive, 0 is floppy A:
mov bx, 0x1000 ;Moving adress 0x1000 into bx
mov es, bx ;Moving bx into es
mov bx, 0 ;Resetting bx
int 0x13 ;Calling interrupt
jnc JumpToKernel
mov si, ErrorLoadingKernel
jc PrintString
jc Return
;Jumps to kernel
JumpToKernel:
jmp 0x1000:0x0000
;Print string in si
PrintString:
mov al, [si] ;Loading si into al
cmp al, 0 ;If the current character = 0 AKA end of string, then return
je Return
mov ah, 0x0e ;Print character function
int 0x10 ;Calling the interrupt
inc si ;Going to the next letter
jmp PrintString ;Looping back
;Return
Return:
ret
hang:
mov si, Hanging
call PrintString
jmp hang
FloppyError db "Error resetting drive.", 0
Hanging db "Hanging...", 0
ErrorLoadingKernel db "Error loading kernel from floppy.", 0
;Filling the rest of the file with zeros to make 512 bytes
times 512 - ($-$$) db 0
kernel.
Code: Select all
bits 16
org 0
jmp main
main:
mov ax, 0
mov ah, 0x0e
mov al, 'A'
int 0x10
Thanks in advance!
EDIT, I should also mention that my kernel is at sector 33 of the floppy, i found that using WinHex, Also i should mention i am loading the bootloader from an ISO and i am using VirtualBox for testing.
Re: Can't load kernel from floppy
Posted: Sat May 18, 2013 6:40 pm
by Prochamber
You should probably mention what actually happens when you test your kernel. It help if we know what to look for rather than just dumping a bunch of code.
If your kernel is at logical sector 33 (remembering sectors start at zero), you are using the wrong CHS values.
Code: Select all
mov ch, 0x02 ;Track
mov cl, 0x0f ;Sector
mov dh, 0x01 ;Head AKA side of floppy
From look at
Converting LBA to CHS on the Wiki, you can find the following values:
Temp = LBA / (Sectors per Track) = 33 / 18 = 1.833 = 1 = 0x01
Sector = (LBA % (Sectors per Track)) + 1 = (33 % 18) + 1 = 16 = 0x10
Head = Temp % (Number of Heads) = 0x01 = 1 % 2 = 1 = 0x01
Track = Temp / (Number of Heads) = 1 / 2 = 0.5 = 0 = 0x00
Re: Can't load kernel from floppy
Posted: Sat May 18, 2013 6:52 pm
by matt8110
I am not getting any errors, in fact i am not getting anything, and i am hearing logical sector everywhere i look, what is that exactly? Also what is LBA and CHS? And i am also fuzzy on something, when i reach sector 18(Maximum) do i go to the next track, and i did not think about the zeros, i have trying to load it at track 2, sector 15, but i guess i should be at 13, right?
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 12:13 am
by Antti
You can add the boot signature if that has anything to do with this.
Code: Select all
times 510 - ($-$$) db 0
db 0x55, 0xAA
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 12:50 am
by dozniak
matt8110 wrote:, but i guess i should be at 13, right?
When you stop guessing and start to know, things get a little bit easier.
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 1:26 am
by Prochamber
matt8110 wrote:I am not getting any errors, in fact i am not getting anything, and i am hearing logical sector everywhere i look, what is that exactly? Also what is LBA and CHS? And i am also fuzzy on something, when i reach sector 18(Maximum) do i go to the next track, and i did not think about the zeros, i have trying to load it at track 2, sector 15, but i guess i should be at 13, right?
Not quite, the head increases before the track.
LBA stands for
Linear Block Addressing, the absolute (logical) sector number (starting to zero)
CHS stands for
Cylinder Head Sector, the three values that give the physical position on a floppy disk.
The first logical sector is, Sector 1, Head 0, Track 0 --- Logical Sector 0
Where you get to: Sector 18, Head 0, Track 0 --- Logical Sector 17
The next logical sector is: Sector 1, Head 1, Track 0 --- Logical Sector 18
When you get to: Sector 18, Head 1, Track 0 --- Logical Sector 35
The next logical sector is: Sector 1, Head 0, Track 1 --- Logical Sector 36
Just use the formulas on the page I linked you to on my last post to find this.
This means if your kernel is placed in logical sector 33 then the CHS value is: Sector 16, Head 1, Track 0
Also, the last two bytes of your bootloader must be a "boot signature" for some machines.
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 7:01 am
by AbstractYouShudNow
You can't just come here, dump your code, and ask us to fix it. You wouldn't learn anything from that and would make the same errors again later.
First, I would object that loading from a fixed location is no good practice. Instead, you should consider booting from some filesystem (such as FAT12/16/32) or store that fixed location, and process the CHS coordinates dynamically (use code to compute them). Then, one sector won't be sufficient to boot your OS once it will great and you will need the bootsector to setup video modes, provide a memory map and other such things. What I suggest is that you support some filesystem, which will allow you to load files of any length. I would also recommend that you don't load your kernel directly, but instead load a second stage that does evrything. You'll see that support for a filesystem leaves very little room in the bootsector. That's what GRUB and Windows NT does (NTLDR).
In your case, you are using the wrong CHS values to access sector 33... By the way, why 33 ? Is it a constrainst of your kernel or something ? And your kernel would better get loaded at the 1MB mark, which will help you in later development. In fact, you'll find that there's a special mode to execute real-mode programs (BIOS included) from protected mdoe, and your drivers (such as VESA) will probably need it. In that mode, you'll need the first MB almost untouched; or you'll corrupts things such as the BIOS.
You could also just be using a well-known bootloader such as GRUB, or the result of the last 512 byte context on this forum which can load ELF files from a FAT filesystem and skip to 32 bit (
http://forum.osdev.org/viewtopic.php?f= ... 3&start=45 ).
Finally, I have a bootsector that loads such a second stage in real mode (the 2nd stage must switch to protected mode by itself) from which you could learn, but it's in AT&T syntax
I would also mention :
http://www.brokenthorn.com/Resources/OSDevIndex.html for a tutorial on system development, including bootloader, with code and all that might help.
http://wiki.osdev.org/Rolling_Your_Own_Bootloader For advice on creating your own bootloader
Also, if you are new to OSDev, first real several tutorials, to ensure that you understand everything.
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 7:03 am
by AbstractYouShudNow
Oh, and also, consider writing an UEFI bootloader instead, UEFI seems to be going to replace the BIOS in the future...
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 11:46 am
by matt8110
@AbstractYouShudNow
I didn't ask someone to fix it, i simply do not understand exactly how the floppy works, also no it is not a constraint on my kernel, it is where the data starts on the floppy, and the kernel is just a test, i am planning on loading a second stage bootloader which puts the computer in 32 bit protected mode.
@Prochamber
Okay, i thought the head was the side of the floppy, well in the floppy's case, not in HDDs
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 1:14 pm
by sortie
matt8110 wrote:I didn't ask someone to fix it, i simply do not understand exactly how the floppy works.
Hi Matt8110, welcome to osdev.org. Be wary of just posting code with some comment "Here is my code. Thanks in advance!" as it sounds like you want us to fix the code, rather than answer any specific questions or problems you might have.
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 6:22 pm
by matt8110
I didn't even think about that, but now looking at my post it does seem like that.
Re: Can't load kernel from floppy
Posted: Sun May 19, 2013 8:34 pm
by Prochamber
matt8110 wrote:Okay, i thought the head was the side of the floppy, well in the floppy's case, not in HDDs
Yes, and it is.
The floppy disk drive has two mechanical "heads" that move at the same time. Each head is responsible for reading one side of the floppy disk.
The logic between alternating sides for each track is that you don't have to move the heads to another track as often, you just have to read with the opposite head.
Re: Can't load kernel from floppy
Posted: Mon May 20, 2013 7:00 am
by embryo
What I would do in your situation.
1. Try to use Int 42 instead of 13
2. Print some bytes from the data loaded by your boot sector
3. Think
4. If absolutely nothing goes to mind - ask here
What have you did to solve your problem ?
Re: Can't load kernel from floppy
Posted: Mon May 20, 2013 4:12 pm
by Prochamber
embryo wrote:What I would do in your situation.
1. Try to use Int 42 instead of 13
2. Print some bytes from the data loaded by your boot sector
3. Think
4. If absolutely nothing goes to mind - ask here
What have you did to solve your problem ?
What?
"Address of relocated Int 10h Handler"? That has nothing to do with floppy disks. It doesn't even contain an interrupt handler.
I'll assume you meant Int 21h,
Function 42h, which is for moving files around. Actually that wouldn't be useful.
Also, it wouldn't work because Int 21h is a
DOS interrupt not a BIOS interrupt. Unless the OP has written some kind of DOS emulator that I'm not aware of they can only use BIOS interrupts.
Re: Can't load kernel from floppy
Posted: Mon May 20, 2013 4:40 pm
by Kazinsal
Prochamber wrote:I'll assume you meant Int 21h, Function 42h, which is for moving files around. Actually that wouldn't be useful.
Fifty invisible internet dollars on INT 13h, AH=42h - IBM/MS INT 13h Extensions; Extended (LBA) Read. Which wouldn't work, anyways, because it only works for hard disks, and even then, may not be supported by all BIOSes.