Implementing a multi-stage bootloader

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Implementing a multi-stage bootloader

Post 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 .
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Implementing a multi-stage bootloader

Post 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:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Re: Implementing a multi-stage bootloader

Post 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 .
Attachments
Boot1.asm
This is the file for stage1 bootloader
(11.67 KiB) Downloaded 72 times
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Implementing a multi-stage bootloader

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
richi18007
Member
Member
Posts: 35
Joined: Mon Mar 07, 2011 1:41 pm

Re: Implementing a multi-stage bootloader

Post 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
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Re: Implementing a multi-stage bootloader

Post by neon »

Hello,

The boot sector is a reserved sector as noted in my previous post.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
Post Reply