Page 1 of 1

Implementing a multi-stage bootloader

Posted: Wed Mar 09, 2011 11:09 am
by richi18007
Can anyone give me a tutorial or some info at least which i need to do differently in order to work up with the tutorials on brokenthorn regarding the multistage bootloader for linux . The tutorials are all done in Windows very difficult to put together

I copied the multistage bootloader http://www.brokenthorn.com/Resources/Demos/Demo1.zip and tried it on Ubuntu 10.04 as follows

Code: Select all

dd bs=512 count=2880 if=/dev/zero of=floppy.img
mkdosfs -f 2 floppy.img
cp stage_2.bin floppy.img
dd if=boot_stage1.bin of=floppy.img
Now running floppy in bochs gave the following error in log
floppy 1 read returned 0
(or may be 1 , I don't remember )
Rebooting again and again geve the same line in floppy 1 read returned some erroneous status
Can anybody help me with this . I'm really going insomniac because of this , and the worst part is , I don't know how to do it . Just shooting in the dark with hit and trial .

Re: Implementing a multi-stage bootloader

Posted: Wed Mar 09, 2011 12:22 pm
by Combuster
The error message says that bochs ran into the end of a file while reading it.

I suggest you check the filesize of floppy.img after each build step (so you can see the problem - you are making the same mistake twice), and after that you should download and compile/install mtools and read the manual page for dd :wink:

Re: Implementing a multi-stage bootloader

Posted: Wed Mar 09, 2011 3:21 pm
by richi18007
Okay figured out the problem , silly mistake . The hex dump made things clear .

the correct chain of dd s should be

Code: Select all

dd bs=512 if=boot_stage1.bin of=floppy.img //no count here , 1st mistake
dd bs=512 seek=1 count=2779 if=/dev/zero of=floppy.omg // one more step ,second mistake :P
rest everything was the same , set up a loopback device , mount it . blah blah .

But now , I have a new problem . I copied the code , it works fine , but I do not understand these series of steps

Code: Select all

 ; compute size of root directory and store in "cx"

     

          xor     cx, cx

          xor     dx, dx

          mov     ax, 0x0020                           ; 32 byte directory entry

          mul     WORD [bpbRootEntries]                ; total size of directory

          div     WORD [bpbBytesPerSector]             ; sectors used by directory

          xchg    ax, cx

          
This is fine , computes the size , stores it in cx , osum . But ,

Code: Select all

   mov     al, BYTE [bpbNumberOfFATs]            ; number of FATs

          mul     WORD [bpbSectorsPerFAT]               ; sectors used by FATs

          add     ax, WORD [bpbReservedSectors]         ; adjust for bootsector(means the reserved sector , But why don't we add the bootsector as well , the fist 512 bytes :O)

          mov     WORD [datasector], ax                 ; base of root directory

          add     WORD [datasector], cx
We add the reserved sectors for FAT12 which come after the bootsector to calculate the offset for the root directory , but we don't add for the bootsector(or do we ? #-o ) , e.g . let's say we have the bootsector , reserved sector , FAT table , and extended FAT .. all in their one sector , which makes the root partition to be located at 2056 offset , but this code will only calculate the offset as 2056-512 ("poor at maths don't know :D) because it doesn't add the bootsector's size (I suppose )

So , what's the problem . Code is still working fine .

Re: Implementing a multi-stage bootloader

Posted: Wed Mar 09, 2011 3:59 pm
by neon
Hello,

Your math is incorrect. As described in the series, the root directory is located after the reserved sectors and both FATs. The boot sector is reserved, and the size of a FAT is 9 sectors. So the root directory is at 9*2+1=sector 19.

Re: Implementing a multi-stage bootloader

Posted: Wed Mar 09, 2011 5:16 pm
by richi18007
Okay 9 sectors (two fats ) so 18 here
And then , 1 bootsector , as descibed by you , this is the bootsector , does that mean no additional reserved sectors ?
Does the reserved sector include the bootsector ?

PS: Sorry for the typos and silly questions , haven't slept for 50 hours now . Systems programming is far far tougher that Software engineering and OOP

Re: Implementing a multi-stage bootloader

Posted: Wed Mar 09, 2011 6:37 pm
by neon
Hello,

The boot sector is a reserved sector as noted in my previous post.