The MASM Forum Archive 2004 to 2012

Miscellaneous Forums => 16 bit DOS Programming => Topic started by: Curious on April 20, 2012, 11:09:53 AM

Title: 2 Stages bootloading from Hard Drive
Post by: Curious on April 20, 2012, 11:09:53 AM
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!)
Title: Re: 2 Stages bootloading from Hard Drive
Post by: 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.
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 20, 2012, 01:34:25 PM
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!
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 20, 2012, 03:34:45 PM
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)
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 20, 2012, 03:44:33 PM
       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)
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 20, 2012, 03:48:36 PM
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
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 20, 2012, 04:43:29 PM
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!)
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 20, 2012, 04:50:50 PM
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
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 20, 2012, 06:06:04 PM
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!
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 20, 2012, 08:17:11 PM
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
Title: Re: 2 Stages bootloading from Hard Drive
Post by: FORTRANS on April 20, 2012, 10:02:00 PM
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.
Title: Re: 2 Stages bootloading from Hard Drive
Post by: MichaelW on April 21, 2012, 12:00:50 AM
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
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 21, 2012, 12:43:31 AM
sorry Michael - i was thinking about FAT drives   :P
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 21, 2012, 04:50:19 PM
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
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 21, 2012, 06:08:34 PM
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
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 21, 2012, 07:30:12 PM
Good point, I'll see what I can do ;)
Title: Re: 2 Stages bootloading from Hard Drive
Post by: MichaelW on April 21, 2012, 09:35:17 PM
Or use the BIOS Interrupt 13h extensions to access the drive, in which case the first and second sectors are blocks 0 and 1.
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 22, 2012, 03:21:06 AM
Quote from: MichaelW on April 21, 2012, 09:35:17 PM
Or use the BIOS Interrupt 13h extensions to access the drive, in which case the first and second sectors are blocks 0 and 1.
I wish I knew what you mean! I AM trying to use Int 13h but can't get the code to work! I guess I need some kind of tracing but a bootable program is harder to debug, I guess the only option left is to read the sectors I want and print them to see what I'm actually loading into the RAM (in case I'm successfully loading to RAM!)

Have anyone tried the code I posted in the first post? It is updated with the latest changes you guys suggested...
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 22, 2012, 03:30:26 AM
back in the day....
DOS came with a program called debug.com
if you can get one of those versions - not the exe
you can format a bootable DOS floppy (with io.sys, msdos.sys, and command.com)
delete the command.com file
copy debug.com over there and rename it command.com   :P
then boot up

i don't recall which versions - but some of them did not have the exe loader yet when command.com runs the first time
Title: Re: 2 Stages bootloading from Hard Drive
Post by: sinsi on April 22, 2012, 03:47:21 AM

;     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

Usually the second sector is on track/cylinder 0, you are reading track 1 (mov ch,1).
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 22, 2012, 05:08:12 AM
QuoteUsually the second sector is on track/cylinder 0, you are reading track 1 (mov ch,1).
I checked, it didn't work either!

BTW, is this reading the 2nd sector or not?
     mov cl,2; reading 2nd sector

@dedndave,
That is very clever, I'll see if I can debug the bootloader using this trick  :cheekygreen:
Title: Re: 2 Stages bootloading from Hard Drive
Post by: sinsi on April 22, 2012, 05:24:10 AM
In CHS mode cylinder and head are zero-based, sectors start from 1. Just to be contrary, in LBA mode sectors start from 0
So "mov ch,0" is the first track and "mov cl,2" is the second sector. (Not that it matters here but the upper 2 bits of the track are in CL).

If you like to live dangerously you can debug the whole thing with the debug version of Bochs :bdg
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on April 22, 2012, 08:31:30 AM
Quote from: sinsi on April 22, 2012, 05:24:10 AM
In CHS mode cylinder and head are zero-based, sectors start from 1. Just to be contrary, in LBA mode sectors start from 0
So "mov ch,0" is the first track and "mov cl,2" is the second sector. (Not that it matters here but the upper 2 bits of the track are in CL).
That explains it  :U

Quote
If you like to live dangerously you can debug the whole thing with the debug version of Bochs :bdg
Yeah, that would be an overkill for me right now  :green2


[Progress]
Ok, what dedndave said is totally doable, I've successfully loaded my kernel using MS-DOS bootloader and MIKE OS bootloader (just had to write the bootloader at sector 0 and re-name my kernel file according to bootloader of choice! MIKE OS uses kernel.bin)
My kernel is a simple bi-lingual font mapped kernel for now (around 2KB, hoping to add some more to it)

[Still in research]
1. How can I write my own bootloader from scratch (after seeing a few more bootloaders)
2. How can I boot from HDD not FDD (Still wondering!)

P.S: Booting from FDD is a lot easier in VMWare than mapping the V-OS as a drive then writing the bootloader to sector 0 (which is time consuming, btw), disconnecting the mapped drive and booting the OS all the time, phew!
In a floppy disk with correct BPB format, you just have to copy the file to the mounted image and voila, start you OS and see how it works  :cheekygreen:
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 22, 2012, 12:43:40 PM
yah - Sinsi has you on the right track
make sure you understand how to calculate the cylinder/head/sector for the drive you are working with
then, make sure you know which cylinder/head/sector you are placing the second sector
Title: Re: 2 Stages bootloading from Hard Drive
Post by: FORTRANS on April 22, 2012, 03:09:59 PM
Hi,

   One thing I tried when responding to other boot strapping
threads was to conditionally assemble the program as a
binary to be executed on a real boot-up or as a DOS *.COM
program to debug what it was supposed to do.


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

DOSish  EQU     0       ; Allow for a DOS program or a Boot Loader sector.
IF DOSish
        ORG     100H
ELSE
        ORG     0H
ENDIF


   Due to my Brownian Motion style of programming, that
saved many a reboot.  You assemble as a *.COM and use
DEBUG to get it working as wanted.  This is not for the boot
loader, but for the sector to be loaded in my case.

HTH,

Steve N.
Title: Re: 2 Stages bootloading from Hard Drive
Post by: dedndave on April 22, 2012, 05:02:52 PM
that's a good thought, Steve
debugging is really troubleshooting
the best tool in the toolbox is isolating the problem, often through a process of elimination
it will be a great time saver if you don't have to reboot (even virtual) each time you want to test something
Title: Re: 2 Stages bootloading from Hard Drive
Post by: FORTRANS on April 22, 2012, 07:31:11 PM
Hi Dave,

   Oh, yes.  For simple (read obvious) fixes, an edit, assemble,
and DEBUG cycle could be just a few seconds.  The edit, assemble,
muck about setting up the floppy, shut down, booting, stare at the
screen, and then reboot cycle was always a fair number of minutes.
Admittedly, the DEBUG cycle can eat up time on the harder bits,
but it always was shorter than the multiple reboots that would have
happened otherwise.

Regards,

Steve
Title: Re: 2 Stages bootloading from Hard Drive
Post by: Curious on May 16, 2012, 08:37:07 AM
Hey guys, thanks everyone here for your helps  :cheekygreen:

I kind of lost a lot of code two-three weeks earlier  :(

But I'm re-writing everything from scratch, no biggy  :U