boot USB memory stick

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.
OS ?

boot USB memory stick

Post by OS ? »

Hi all.
I have a small problem :) I have been following the following university course (part of it) http://flint.cs.yale.edu/cs422/assignments/1.html and have produced the following code.

Code: Select all

org 0x7c00
format binary

Start:                  jmp Begin
                        nop

Begin:                  xor ax,ax
                        cli
                        mov ds,ax
                        mov ss,ax
                        mov es,ax
                        mov sp,0x7c00
                        sti
                        push dx

                        ;Set 80x50 text mode
                        mov ax, 0003h
                        int 10h ;first set mode 03h
                        xor bx, bx
                        mov ax, 1112h
                        int 10h ;load 8x8 font (we hope that controller has an internal font that we can use)
                        mov si,Title
                        call WriteString

                        ;;;Load next sector
                        mov ah,2                 ;;;Function number
                        mov al,1                 ;;;Sectors to read
                        mov ch,0                 ;;;Cylinder number lower 8 bits
                        mov cl,2                 ;;;Bits 7..6 upper cylinder number to make 10 bits,
                                                 ;;;5..0 starting sector number

                        mov dl,0x80              ;;;Drive number
                        mov dh,0                 ;;;Starting head number
                        mov bx,0x7c00+0x0200     ;;;Offset to load sector
                        int 0x13
                        jc BootFailure
                        mov si,Success
                        call WriteString
                        jmp $

BootFailure:        mov si,BootFailed
                        call WriteString
                        ;;;Obtain key press
                        xor ax,ax
                        int 0x16
                        ;;;Shut down the machine
                        mov ax,0x5304
                        xor bx,bx
                        int 0x15
                        mov ax,0x5301
                        int 0x15
                        mov ax,0x5308
                        mov bx,1
                        mov cx,bx
                        int 0x15
                        mov ax,0x530d
                        int 0x15
                        mov ax,0x530f
                        int 0x15
                        mov ax,0x5305
                        xor bx,bx
                        mov cx,102
                        int 0x15
                        mov ax,0x5307
                        mov bx,1
                        mov cx,3
                        int 0x15
                        jmp $
;;;Enter with al containing character to display
WriteChar:          mov ah,0x0e
                        mov bh,0x00
                        mov bl,0x02
                        int 0x10
                        ret

;;;Enter with si ptr to string
WriteString:        mov al,byte[si]
                        call WriteChar
                        inc si
                        cmp byte[si],0
                        jne WriteString
                        ret

Title:                 db 'OS 0.1',0x0d,0x0a
                        db 'Starting....',0x0d,0x0a,0x00
SectorLoaded:    db 'Sector loaded',0x00
BootFailed:         db 'BOOT FAILED, press any key quit.',0x00

times 510-($-Start)     db 0
BootSignature:          dw 0xAA55
Success:                db 'just babble!!!!!',0x00 
It seems to boot OK but when it loads the next sector it dosen't seem to load it in the right location. :-\
Any help would be appreciated :)
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:boot USB memory stick

Post by Brendan »

Hi,

You're trying to load the sector into the area from 0x007A00 to 0x007BFF, which is the same area you're using for the stack.

If it loads the sector correctly, then the BIOS should crash when it tries to return.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Ryu

Re:boot USB memory stick

Post by Ryu »

Code: Select all

org 0x7c00
format binary
Begin:                xor ax,ax
                        cli
                        mov ds,ax  ;<--- DS segment zer0
                        mov ss,ax
                        mov es,ax ;<--- ES segment zer0
                        mov sp,0x7c00
                        sti

 .................

                        mov dl,0x80              ;;;Drive number
                        mov dh,0                 ;;;Starting head number
                        mov bx,0x7c00+0x0200 ; <--ES:7E00h we are ok.
                        int 0x13
..................
                        mov si,Success ;<-- heres the problem
                        call WriteString
                        jmp $
  
I don't know at the top of my head weather int 10h uses the ES or DS segment with the offset, so check up on your resources. But as you can see both segments are equal to 0 and offset Success is 512. Your printing characters at 0:200h but the intention is 7C0:200h so have ES and DS set to 07C0h.

edit: One more thing once ES=DS=7C0h you might want to change mov bx,0x7c00+0x0200 to mov bx, 200h.
Ryu

Re:boot USB memory stick

Post by Ryu »

Hmm but now I'm confused about org 0x7c00.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:boot USB memory stick

Post by Brendan »

Hi,
Ryu wrote:I don't know at the top of my head weather int 10h uses the ES or DS segment with the offset, so check up on your resources. But as you can see both segments are equal to 0 and offset Success is 512. Your printing characters at 0:200h but the intention is 7C0:200h so have ES and DS set to 07C0h.
Int 0x10 uses ES:BX for the area to load data to.

The original segmentation is also correct (just different to what you're probably using). It doesn't matter if you use org=0x7C00 with CS=ES=DS=ES=0, or if you use org=0x0000 with CS=ES=DS=ES=0x07C0 - it works out the same in the end...

[edit] You are right about CS though - it's not set anywhere and the BIOS could have used CS=0x07C0 or CS=0x0000...[/edit]


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Ryu

Re:boot USB memory stick

Post by Ryu »

I'm just so use to CS=ES=DS=7C0h sorry, nevermind my comment :)

edit: It would be safer to do a far jump to set CS=0 then.
OS ?

Re:boot USB memory stick

Post by OS ? »

Hi all.
Thanks for the comments. Just to clarify. The stack starts at 0x7c00 and moves down so that shoudn't be getting in the way :) The print routine works it's like everything works, it loads in the next sector (the usb memory stick flashes to say there is a read/write going on) but when I try to display the string in question it prints out some garbage each time. :-\
I'am still no closer then I was last night.
The application that I'am using to write the boot block is winimage 8.0 I wounder is that the problem?
Thanks.
OS ?

Re:boot USB memory stick

Post by OS ? »

Hi all.
Just done a test! Instead of using a usb memory stick used a floppy disk for the boot image replaced any sector loading code with the one for floopy disk. Everything worked (used rawwritewin). I wounder does winimage just write the boot block (512 bytes) and nothing else?
Thanks.
Ryu

Re:boot USB memory stick

Post by Ryu »

Well winimage didn't work for me, I think it requires some unkown bin format for the boot sector. Loading my 512 bytes boot.bin to it didn't work. Theres tools such as dskprobe.exe part of the xp sp2 advance tools pack, also one tool I find handy is mkbt.exe because being a command line, they are pretty easily found by a google search and capable of writting to flash drives, but mkbt is specifically to write to boot sector only.
Dex4u

Re:boot USB memory stick

Post by Dex4u »

Maybe you should look at the differanc between booting floppy and booting Hdd ;).
You will find for a start that the keyfob is formated fat16, even when emulating a floppy, but it may even be fat32 ?, as you have drive number as 80h.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:boot USB memory stick

Post by Candy »

A USB stick is formatted in whichever filesystem you dare put on it. I've had my USB sticks formatted with at least FAT16, FAT32, NTFS and EXT2 at times. You can also partition them. My current one is partitioned into a 600M FAT32 partition, a 250M EXT2 partition and a 140M partition for my own FS tests.

Booting from them is exactly the same as booting a harddisk I think. It's emulated as one so I assume it'll function like one. At the moment you enter protected mode, of course, the emulation will fall away and you'll need to access it using your own USB driver (or just not access it at all...).
OS ?

Re:boot USB memory stick

Post by OS ? »

Hi all.
Thanks for the reply's. It would seem that if I use linux as my development platform things would be alot easier ;)
The trouble is that I thought the boot sector on the stick is at sector 1 track 0, etc,... but it isn't. I think ??? Anyway I thought it would act like a floppy disk but I in the first sector would be loaded execute that then load in the second one and take it from there. I have gone back to winimage to create an image of the usb stick and inject the boot binary but winimage crashes. Am I wrong with this line of enquiry? I have thought about writing a boot sector that uses fat32 and place the required files as normal on the disk, but I don't want to go down that road.
Any help would be appreciated. :)
Dex4u

Re:boot USB memory stick

Post by Dex4u »

Well my OS boots from any USB fob that the BIOS can boot from (eg: most latter pc). the problem is that some BIOS boot USB by emulating Hdd and other by emulating floppy (formated fat16)
The way i do it ( to keep it simple) is to format the USB fat16 and put sys file on it, this is done in windows.
So it like a win startup disk (this work with freeDos too), as my OS can boot from dos this works fine, i can also can read/write to the USB key fob, WITHOUT USB driver, by going back and forth to realmode, using int 13h, i also have a pmode floppy and hdd driver.
First thing to do is go to your BIOS setup with the usb plugged in and see what it is written in boot order, as it will see it as a floppy or hdd.
Once you know that you can code for that emulation.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:boot USB memory stick

Post by Candy »

OS ? wrote: The trouble is that I thought the boot sector on the stick is at sector 1 track 0, etc,... but it isn't. I think ??? Anyway I thought it would act like a floppy disk but I in the first sector would be loaded execute that then load in the second one and take it from there. I have gone back to winimage to create an image of the usb stick and inject the boot binary but winimage crashes. Am I wrong with this line of enquiry? I have thought about writing a boot sector that uses fat32 and place the required files as normal on the disk, but I don't want to go down that road.
All disks, be they floppy or harddisk-type, boot from the very first sector. It contains bootable code with a 0xAA55 (or 0x55 0xAA) signature at the end. The bios can check this signature after which it loads whatever is on the disk and then jumps to it, the BIOS-disk-id in the dl register. It does not matter what exact type of disk you have. You can access any and all of them with int13/ah=02. Your BIOS probably also supports int13 extensions on them, but I can't guarantee that.

There is no very important distinction between floppies and harddisks on the BIOS level.

As Dex4u said, you can always fall back to realmode and use the BIOS support. For performance reasons however, this isn't the quickest way. Writing your own USB support is probably a lot faster. Try searching for USB EHCI, UHCI and OHCI documentation. The first is the USB2 controller, the latter are the USB1 controller types. VIA and Intel mainboard have UHCI, most of the others have OHCI. EHCI leaves usb1 device handling to an underlying UHCI or OHCI controller. Then, you need to support the USB storage generic class, plus the RBC SCSI commands they carry. When in doubt, google and read about acronyms you read on the subject.
Kemp

Re:boot USB memory stick

Post by Kemp »

Just a quick point that no-one seems to have picked up, the boot sector is sector 0, not 1.
Post Reply