Page 1 of 2

Boot loader code

Posted: Sun May 18, 2003 12:13 pm
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

Re:Boot loader code

Posted: Sun May 18, 2003 8:24 pm
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

Re:Boot loader code

Posted: Mon May 19, 2003 1:21 am
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 ...

Re:Boot loader code

Posted: Mon May 19, 2003 6:10 am
by Perica
..

Re:Boot loader code

Posted: Mon May 19, 2003 6:21 am
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"...

Re:Boot loader code

Posted: Tue May 20, 2003 8:51 am
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

Re:Boot loader code

Posted: Tue May 20, 2003 9:11 am
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 ?

Re:Boot loader code

Posted: Tue May 20, 2003 3:54 pm
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

Re:Boot loader code

Posted: Tue May 20, 2003 4:13 pm
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.

Re:Boot loader code

Posted: Tue May 20, 2003 4:19 pm
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

Re:Boot loader code

Posted: Tue May 20, 2003 7:45 pm
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

Re:Boot loader code

Posted: Wed May 21, 2003 1:31 am
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 ...

Re:Boot loader code

Posted: Wed May 21, 2003 2:04 am
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.

Re:Boot loader code

Posted: Wed May 21, 2003 2:31 am
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

Re:Boot loader code

Posted: Wed May 21, 2003 3:07 am
by beyond infinity lazy
Ok, this problem, I didn't respect, sorry.