Boot loader code

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.
manchev

Boot loader code

Post by manchev »

Hi fellaz! I have basic knowledge of assembler. I need to make a boot sector or eventually boot sector and boot loader. I just want to put the sector on a floppy and when my computer starts just to load it in the memory and then load the mbr of my hdd or the boot sector of my active partition and start windows. Is it possible and if yes how? Thanks in advance!



:manchev
gtsphere

Re:Boot loader code

Post by gtsphere »

starting windows from your own bootloader, that replaces the current bootloader for windows, i think (but don't quote me) would be very hard for the fact that you will need to know how to handle the filesystem (fat32, ntfs, etc).

Also, i would imagine that the memory setup and placement for certain programs would have to be very detailed.

A good idea would be to check out either Grub or LILO, to see how they boot up windows, linux, etc. might help a bit
-GT
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Boot loader code

Post by Pype.Clicker »

all you have to do is move your floppy bootsector to another location and load the MBR of your HDD to 0:0x7C00. Then, set the value of DL appropriately to 0x80 (just as the BIOS would have done if it had ran the partition itself) and jump to 0:0x7c00 :)

As an extension, you could read the partition table, check for active partitions and let the user decide which partition should be executed ...
Perica
Member
Member
Posts: 454
Joined: Sat Nov 25, 2006 12:50 am

Re:Boot loader code

Post by Perica »

..
Last edited by Perica on Sun Dec 03, 2006 9:13 pm, edited 1 time in total.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Boot loader code

Post by Pype.Clicker »

afaik, there are bios extension for large drives (using LBA, mostly), but i can't remember what interrupt it uses (i just remember it's not the usual INT13 callz).
Btw, int13 should be enough to read the MBR of any hard disk. Reading the boot sector of partition X may be more complicated, and it explains why LILO had a restriction like "your /boot partition must start at max at offset xGB on your hard drive"...
manchev

Re:Boot loader code

Post by manchev »

You are fantastic boyz, but can you tell me just how exactly to "load" the MBR and then execute it like BIOS normally does? Thanks in advance!


:manchev
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Boot loader code

Post by Pype.Clicker »

get the INT13 read disk description in Ralf Brown's interrupt list and feed the correct parameters
(C=0, H=0, S=1 -- drive = 0x80 , 1 sector target = 0x0000: 0x7C00)

then jmp 0x0000:0x7C00

Couldn't you just deduce it out of the previous informations ?
manchev

Re:Boot loader code

Post by manchev »

Ok, Pype.Clicker! That's the source. I think it should work, but if you try it you'll see that it does nothing else than just rebooting your computer after some seconds. If you see any errors and can help me, just drop a line!


[bits 16]
[org 0]

begin:

mov ah,02h
mov al,01h
mov ch,00h
mov cl,01h
mov dh,00h
mov dl,80h
mov bx,0x0000
mov es,bx
mov bx,0x7C00
int 13h

jmp 0x0000:0x7C00

times 512-($-$$)-2 db 0
dw 0AA55h


:manchev
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Boot loader code

Post by Pype.Clicker »

i guess you missed step 0:
Pype.Clicker wrote: all you have to do is move your floppy bootsector to another location and load the MBR of your HDD to 0:0x7C00.
Think about it: your bootsector from the floppy has been loaded at 0:0x7C00 by the bios and this is just where you ask the HDD bootsector to be loaded :-/ guess what happens when the INT13 returns ? it starts executing random stuff from the HDD bootsector ... you may be pretty pleased if your MBR hasn't been damaged in the operation.

So step 0: copy [0:07C00..0:7DFF --> 0:8000..0:81FF] and jmp $continue+2

once you're safely at 0:8xxx, you can go for loading ....

< rant -- do not take it personnally >
Damn, guys, i think Internet destroyed your brains. I had to figure all this by myself by disasembling the MBR by hand in DEBUG.EXE (and i had to read the bootsector and store it in a file myself too ... because there was nothing like rawcopy in the MS-DOS distribution)!

Be smart : echo Questions > /dev/brain
< /rant >

If you don't understand what you are doing, do something else -- or you 'll end up in an system crash and you will complain we gave you bad info.
manchev

Re:Boot loader code

Post by manchev »

Oooh, stop, I know what I am doing. I'm just a newbie in asm so I know what to do but don't know how. So I'm not going to wipe my MBR or something. Bye for now and thanks for the unvaluable information ;)



:manchev
manchev

Re:Boot loader code

Post by manchev »

By the way this code works just fine under VMware, but doesn't want to make it under normal conditions... Any ideas? And here it is:

[bits 16]
[org 0]

begin:
;;;;;;;;;;;;;;;;;;;;;;;
cli
mov ax,0x9000
mov ss,ax
mov sp,0xffff
sti
;;;;;;;;;;;;;;;;;;;;;;;;
mov ah,02h
mov al,01h
mov ch,00h
mov cl,01h
mov dh,00h
mov dl,80h
mov bx,0x0000
mov es,bx
mov bx,0x7C00
int 13h

jmp 0x0000:0x7C00

times 512-($-$$)-2 db 0
dw 0AA55h


:manchev
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Boot loader code

Post by Pype.Clicker »

as you still don't have a "move the bootsector away" copy loop, it can't run. If it does under VMware, then it's because VMware doesn't perfectly emulate the PC or (more probably) because you were lucky and that the bootsector of VMware is written so that when you start executing it in its middle ...

it's enough that your 'real' MBR has a string (like "no operating system detected") where VMware MBR has code ...
beyond infinity lazy

Re:Boot loader code

Post by beyond infinity lazy »

oi, pype swinging clubs *dodgingaway* but for I have not the manner to hold my trap ....

Hm, I understand that it is difficult to deduce (I always induce proofs from good assumptions and experiments) knowledge from raw code. Not everybody has the ability or the time to do reasonable reverse engineering. But sometimes, there comes the lad/gal, the question and an inevitable glowing look of feeling struck in a queer manner. In such moments, one feels as if the guy asking questions over there just doesn't want to use his grey cells. And this is a shame for pype n others have to do reasonable thinking and experimenting to achieve his knowledge ere they start coding anything.

the more, a sentence like:
You are fantastic boyz, but can you tell me just how exactly to "load" the MBR and then execute it like BIOS normally does? Thanks in advance!

makes ME f. ex. feel as if someone tries to fool me.

And Yes, I am ranting, And NO, I am not a Nice Guy. D[a]mn it.

@pype: some guys just don't want to do research. It is but so easy to ask ask ask - and afterwards wondering why nothing runs *ggg*

@manchev: You can also load the mbr to 0x0000:0x8000 or other adress. The important thingy is: you overwrite your old code by doing it your way. You canna do things THAT simple, gosh.
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:Boot loader code

Post by Pype.Clicker »

beyond infinity lazy wrote: @manchev: You can also load the mbr to 0x0000:0x8000 or other adress. The important thingy is: you overwrite your old code by doing it your way. You canna do things THAT simple, gosh.
YeeekZ ! alert !

Loading and executing the MBR@0000:8000 will fail if the MBR contains (as it should) a JMP 0000:7C00 instruction to avoid the fact BIOS can load it at 07C0:0000 or 0000:7C00
beyond infinity lazy

Re:Boot loader code

Post by beyond infinity lazy »

Ok, this problem, I didn't respect, sorry.
Post Reply