Writing a boot menu

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.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Writing a boot menu

Post by mangaluve »

Im pretty new to OS dev (as can be seen in my other posts). I think it would be a fun idea to write a own boot loader, such as GRUB, which basically should be a menu where I chose a partition to start from. Now I know how to write a simple boot sector, how to load some code from the disc and so on. Now in my head it looks pretty simple, just the following steps:
Create a small boot sector which should be used as master boot sector. Use it to read the actual boot-menu code and launch it.
List all the partitions on the partition-table
When I choose one, jump to the boot sector on that partition.

But I guess I've missed some things, or is it really that easy? Secondly, how would I go about the technical details, such as putting my boot sector on the hard drive (I've only tried with floppy disks before). Should I create a entirely new partition where I store the code / data for my boot menu aswell?
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: Writing a boot menu

Post by jal »

mangaluve wrote:I think it would be a fun idea to write a own boot loader
Please check the Wiki, most of your questions have been answered there. Also, search the forum, this topic has come by many, many times.


JAL
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Writing a boot menu

Post by mangaluve »

Thanks. Right now I have a boot loader, that reads a small kernel and some other minor stuff. I know how to read from a disk in real mode, using BIOS interrupts. But now I'm in protected mode. I wasn't able to find how to do that on the wiki, so a little help (for instance a link) would be appreciated.

Another basic question I have is: Suppose I have Windows on a partition. Then the first 512 bytes of this partition will be the win-bootloader (my boot menu is ofcourse MBR). Should I then load the win-bootloader into 0x7C00 and run it from there? What I mean is, does this win-bootloader expect to be loaded into 0x7C00 and then executed from real mode or something like that?
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Writing a boot menu

Post by JohnnyTheDon »

Yes. This is called chainloading.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Writing a boot menu

Post by mangaluve »

Thanks! I assume this bootsector (on the partition) is exactly the same as the one that would be used as MBR (if I only have one OS and I hadn't been there messing around). For instance if I got a computer with only Windows, then the MBR and the partition-boot-loader would be exactly the same code?

Is there anything special that needs to be done, except loading the partition boot loader into 0x7C00, make sure the computer is in real mode, and make a jump to 0x7C00? Secondly, how do I load stuff from a HD in protected mode (any links?). I though about doing a bootable USB-stick from which I could boot windows/linux.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Writing a boot menu

Post by Dex »

mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Writing a boot menu

Post by mangaluve »

I got a little confused by that link. It uses ports, but it also uses interrupts? Also, do I need something different if it's a USB stick I want to read from?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Writing a boot menu

Post by Troy Martin »

mangaluve wrote:I got a little confused by that link. It uses ports, but it also uses interrupts? Also, do I need something different if it's a USB stick I want to read from?
The interrupts are DOS ones just to quit the program. And I'm not sure about the USB stick thing, but probably.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
JohnnyTheDon
Member
Member
Posts: 524
Joined: Sun Nov 09, 2008 2:55 am
Location: Pennsylvania, USA

Re: Writing a boot menu

Post by JohnnyTheDon »

AFAIK you need a USB driver, unless you drop down to real mode and use the BIOS.
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Writing a boot menu

Post by mangaluve »

I didn't really understand the code for reading from the hard drive. I saw that interrupt 13h and so on was used? What I want to do is to read some data from the harddrive and put it on a certain memory location.
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Writing a boot menu

Post by JAAman »

that is a bios disk access function, which you can use to access the disk as long as your in RMode

check with RBIL for more information about them
Is there anything special that needs to be done, except loading the partition boot loader into 0x7C00 (the specific segment:offset combination doesnt matter, but 0:7C00 is quite popular on modern systems), make sure the computer is in real mode, and make a jump to 0x7C00?
basically, the only thing that is necessary for the MBR to do for the boot loader is to load the code to absolute address 0x7C00 and make sure DL still contains the BIOS boot drive number (should be easy to do, since it must be in DL to load the sector in the first place)

technically you should also check the last 2 bytes of the boot sector to make sure they contain the appropriate boot signature, but some code these days skips that...
Thanks! I assume this bootsector (on the partition) is exactly the same as the one that would be used as MBR (if I only have one OS and I hadn't been there messing around). For instance if I got a computer with only Windows, then the MBR and the partition-boot-loader would be exactly the same code?
no, the boot partition and MBR are different programs with different functions

in actuality, the active partition isnt necessarily a boot loader either -- it could be an extended partition, in which case it is another MBR that you are loading there, however, the actual boot partition will contain a boot sector which is very different, written with a different purpose in mind
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Writing a boot menu

Post by mangaluve »

Thanks!
In which ways is it different? I mean suppose I have Windows, then I have a Windows MBR and a partition boot sector on the Win partition. When I then install for instance GRUB, the MBR is removed and instead (in the end when I choose to start Windows) the windows-partition boot sector is loaded into 0x7C00 and executed. Does this partition boot sector "know" that it's a partition bootsector and not a MBR, and does it assume that some things has been done already?
User avatar
JAAman
Member
Member
Posts: 879
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Re: Writing a boot menu

Post by JAAman »

the boot sector does know its not an MBR, because it has a different purpose, and is written specifically to be a boot sector (and cannot function as an MBR) -- however, the boot sector does not know that it was loaded by an MBR, and can exist as the first sector on the disk loaded directly by BIOS without any problems (which is common for some smaller disk types, such as FDDs, but can cause problems with OSs which assume the presence of a partition table on larger HDDs...)

the only things a properly written bootsector should assume is that the physical address it is loaded at is 0x7C00, and that DL contains the boot drive number (which it will save for use with the BIOS disk load functions)

other than that, the bootsector cannot assume anything about its boot state, including the contents of any GPR (except DL) or segment register (including CS/DS)

the difference between a MBR and a bootsector are simple:
a MBR will simply load the first sector from the appropriate partition, where the bootsector is responsible for loading the next part of the OS... very different jobs, with different purposes...
mangaluve
Member
Member
Posts: 110
Joined: Mon Feb 23, 2009 6:53 am

Re: Writing a boot menu

Post by mangaluve »

Okey thanks! But I could basically take any partition boot sector and use it as MBR aswell then? Only that I don't have to? :)

Secondly, shouldnt the MBR contain a partition table? Should the partition boot sectors contain the partition table aswell?
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Re: Writing a boot menu

Post by Dex »

mangaluve wrote:I didn't really understand the code for reading from the hard drive. I saw that interrupt 13h and so on was used? What I want to do is to read some data from the harddrive and put it on a certain memory location.
The code is a demo, that read/write from hdd using both ports and BIOS to test the port bit eg:
This bit users ports

Code: Select all

  mov     dx,1f6h         ;Drive and head port
   mov     al,0a0h         ;Drive 0, head 0
   out     dx,al

   mov     dx,1f2h         ;Sector count port
   mov     al,1            ;Read one sector
   out     dx,al

   mov     dx,1f3h         ;Sector number port
   mov     al,1            ;Read sector one
   out     dx,al

   mov     dx,1f4h         ;Cylinder low port
   mov     al,0            ;Cylinder 0
   out     dx,al

   mov     dx,1f5h         ;Cylinder high port
   mov     al,0            ;The rest of the cylinder 0
   out     dx,al

   mov     dx,1f7h         ;Command port
   mov     al,20h          ;Read with retry.
   out     dx,al
still_going:
   in      al,dx
   test    al,8            ;This means the sector buffer requires
            ;servicing.
   jz      still_going     ;Don't continue until the sector buffer
            ;is ready.

   mov     cx,512/2        ;One sector /2
   mov     di,offset buffer
   mov     dx,1f0h         ;Data port - data comes in and out of here.
   rep     insw
and loads it to offset buffer
This bit load the same sector, to offset buffer2
using BIOS

Code: Select all

   mov     ax,201h         ;Read using int13h then compare buffers.
   mov     dx,80h
   mov     cx,1
   mov     bx,offset buffer2
   int     13h
This bit compare the two, to see if they are the same:

Code: Select all

   mov     cx,512
   mov     si,offset buffer
   mov     di,offset buffer2
   repe    cmpsb
   jne     failure
   mov     ah,9
   mov     dx,offset readmsg
   int     21h
   jmp     good_exit
failure:
   mov     ah,9
   mov     dx,offset failmsg
   int     21h
good_exit:
   mov     ax,4c00h        ;Exit the program
   int     21h

   readmsg db      'The buffers match.  Hard disk read using ports.$'
   failmsg db      'The buffers do not match.$'

buffer  db      512 dup ('V')
buffer2 db      512 dup ('L')
You would only use the port bit from pmode.

To read/write to usb fob you need to boot from it than go to and from realmode to read/write from fob or write a usb stack.
Post Reply