2 Stages bootloading from Hard Drive

Started by Curious, April 20, 2012, 11:09:53 AM

Previous topic - Next topic

Curious

Hi guys, I'm very new to all this, I've tried many thing here and there to find the right way to load the 2nd sector to RAM and write something on the screen but I've failed...
Here's the latest code I patched together from many codes I read here and found by googling!

Bootload:

.MODEL  TINY
        .CODE

;----------------------------------------------------------------------------------

LoadOfs EQU     0
LoadSeg EQU     1000h

        ORG     0

Boot0:  jmp short Boot1
        NOP     ; - - - Mods to deadndave's code: Win2K error fix,
                ; - - - original was a zero.

;----------------------------------------------------------------------------------

;---------------------- initialize SS:SP

Boot1:
        xor     di,di
        mov     ss,di
        mov     sp,7C00h        ;SS:SP = 0000:7C00

;---------------------- load the 2nd sector from hard drive
     mov ax, LoadSeg ;read sector into address 0x1000:0
     mov es, ax
     xor bx, bx
     mov ah, 02h; read sector function
;     mov ah, 0Ah; read sector function (Long)
     mov al, 1; read 1 sector
;     mov ch,1; read from track 1
     mov ch,0; read from track 2
     mov cl,2; reading 2nd sector
     mov dh, 0
     mov dl, 0
     int 13h
; --------------

    mov ah, 2
    mov bh, 0
    mov dh, 14
    mov dl, 65
    int 10h

;------------ wait for a key
    xor ah, ah
    int 16h



;---------------------- make the jump to sect2
     db      0EAh    ;JMP FAR
     dw      LoadOfs ;offset
     dw      LoadSeg ;segment




;---------------------- bootloader signature

        ORG     1FEh
        dw      0AA55h

;----------------------------------------------------------------------------------

        END     Boot0


2nd Stage:

        .MODEL  TINY
        .CODE

;----------------------------------------------------------------------------------

LoadOfs EQU     0               ;must match the value in the bootloader source file

;----------------------------------------------------------------------------------

;---------------------- initialize ES segment register

        ORG     0

Start:  push    cs
        pop     es
   
;---------------------- writing a message on screen at startup - we can't use int 21h

        mov     bp,offset Msg0+LoadOfs
        mov     cx,sizeof Msg0
        xor     dx,dx           ;row 0, column 0
        mov     bx,7            ;page 0, attribute = 7 (white on black)
        mov     ax,1301h        ;function 13 - cursor mode 1
        int     10h

;---------------------- done - halt

Halt0:  jmp     Halt0

;---------------------- data area in code segment

Msg0    db      "We be bootin2!"

;----------------------------------------------------------------------------------

        END     Start


Can you tell me what's wrong with the code?

Oh and please note that I want to boot from HDD not floppy disks (or virtual FDDs for that matter)
P.S: I'm using VMWare as the virtual machine and DiskExplorer to write the binary code to the first sector... (and the 2nd one!)

FORTRANS

Hi,

   You have;


        mov ah, 2
        mov dl, 47
        int 21h


and you do not have DOS loaded.  You are booting so your
invoking INT 21H is a bad idea.  The Interrupt Vector Table
will not have a valid entry at boot time.

HTH,

Steve N.

Curious

Quote from: FORTRANS on April 20, 2012, 12:54:51 PM
Hi,

   You have;


        mov ah, 2
        mov dl, 47
        int 21h


and you do not have DOS loaded.  You are booting so your
invoking INT 21H is a bad idea.  The Interrupt Vector Table
will not have a valid entry at boot time.

HTH,

Steve N.

Oops, my bad, I changed that into (also updated the first post!)
    mov ah, 2
    mov bh, 0
    mov dh, 14
    mov dl, 65
    int 10h


... But still nothing!

Curious

I guess there's something wrong with this part of the code
;---------------------- load the 2nd sector from hard drive
     mov ax, LoadSeg ;read sector into address 0x1000:0
     mov es, ax
     xor bx, bx
;     mov ah, 2; read sector function (Floppy)
     mov ah, 0Ah; read sector function (HDD)
     mov al, 1; read 1 sector
     mov ch,1;
     mov cl,2; reading 2nd sector
     mov dh, 0
     mov dl, 0
     int 13h


Specially this line that should boot from HDD not FDD
;     mov ah, 2; read sector function (Floppy)
     mov ah, 0Ah; read sector function (HDD)

dedndave

       xor     di,di
       mov     ss,di
       mov     sp,7C00h        ;SS:SP = 0000:7C00

did i write that - lol
it's best to disable interrupts when altering the stack segment
       xor     di,di
       cli
       mov     sp,7C00h
       mov     ss,di
       sti


at any rate...
under VmWare, you will not be able to access the hard disk directly (thank goodness)

function 0Ah is read sector long
function 2 is the function you want - you just have to reference the correct drive (80h)

dedndave

in order to do what you are trying to do..
you will have to format a "scratch" drive and boot from it

the first sector on a hard drive is the partition table
the next sector is the boot sector of the first partition

Curious

Hi dedndave,
I added a 'cli' before that part and a 'sti' right after it
       cli
       xor     di,di
       mov     ss,di
       mov     sp,7C00h        ;SS:SP = 0000:7C00
       sti


and used the the following function of INT 13h as you said
mov ah, 2; read sector function

And changed:
mov dl,0
to
mov dl,80h

Quoteunder VmWare, you will not be able to access the hard disk directly (thank goodness)
WTH? How can I test all this then? Isn't VMWare supposed to work for this too? (Accessing the virtual hard disk not the real one?!)
I have a cool disk for working on this, I've successfully booted a simple code that writes a 'hello world' on the screen which worked both under VMWare and the cool disk booting on a real system that I allowed to boo from USB

Is there any code with 2nd stage loading code I can learn from? (even better if works in VMWare)

P.S:
The VMWare machine I set up is a one with MSDos installed on it and then changed the boot sector (which worked for the simple task of booting but not loading the 2nd stage!)

dedndave

well - you may be able to access something under VmWare, but it shouldn't be a real hard disk
i never tried it   :P

i guess i am in the same boat as you
you want to play with boot sectors but have no floppy drive   :(

if i want to play - i use my old win98 machine, which has floppy drives
but - you might consider playing with a USB stick

QuoteThe VMWare machine I set up is a one with MSDos installed on it and then changed the boot
sector (which worked for the simple task of booting but not loading the 2nd stage!)

the second "stage" is a sector - maybe you didn't have it in the right place
or - more correctly, VmWare probably formats the drive and has 2 copies of the FAT and a root directory
you want to calculate and access the right sector

Curious

omg, I thought I should replace the very first 512 bytes of the media! So you say I should let the MS-DOS 5.0 bootsector to be in its place, right?

But then I'll get:
QuoteRemove disks or other Media.
Press any key to restart
Won't I?

I guess I should go after creating a floppy disk image and load it in VMWare then, I'll let you know

Oh, and I tried to create a bootable sector with more than 512 bytes, I put the boot signature in its place (511th and 512th bytes) and forced the program to jump pass the 512th byte but couldn't make anything out of it! It is so unstable for some reason that I don't understand yet!

dedndave

well - sectors are 512 bytes
read sector long simply adds four bytes for the CRC
i don't think you want to write the CRC - let the disk controller take care of that

FORTRANS

Hi,

   Since you are using a virtual machine, maybe you could add
to it a floppy image and see if you can get the original code to
work.  Just for a sanity check.

   Second thought, if your hard drive is supposed to be formatted,
won't your MBR have to have support for any existing partitions?
Otherwise you will wipe the existing information on the disk.  If
you do not write the second sector to the first sector of a partition,
you will be treating the hard drive as a large floppy (I think) and
will not be "following the rules".  (If you are writing to a Partion Boot
Record and not the MBR, please forget this paragraph.)  Any
thoughts Dave?

Cheers,

Steve N.

MichaelW

Quote from: dedndave on April 20, 2012, 03:48:36 PM
the first sector on a hard drive is the partition table
the next sector is the boot sector of the first partition

Normally the entire first track is reserved, but I suppose if you're already overwriting the MBR and don't have an OS, there is no need to follow any convention :P
eschew obfuscation

dedndave

sorry Michael - i was thinking about FAT drives   :P

Curious

Hey guys,
Nah, it didn't work either (or I couldn't make it work!  :lol )

I made a floppy disk image using VMWare, formatted it using ms-dos and wrote the 512 bytes to the sector 0, it is working but not the kernel loading part :(

I guess I'm doing something wrong here  :dazzled:

Any clue? Any step by step previously proven to work tutorial or document would be nice to get started

dedndave

find some way to verify that it is loading the second sector
you might try loading the sector, then displaying the first several bytes of it in hex (easy to code)
it may be that the sector you are loading is not the same one you have written