boot from USB flash

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.
Post Reply
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

boot from USB flash

Post by muisei »

Hi,

I'v read almost all the topics in this forum about booting from USB flash, but could 't find solution for my problem, which is:I can't boot from USB flash.

What am I doing:
1.Copy the image file of my os(10MB) to the USB(256MB)
2.Boot from USB
3.MBR loads successfuly(print some messages)
4.Then try to load sector 63(LBA) from the usb, using extended functions(int 13h AH=42h)
4.Then everything hangs

Tried to change the DL register to 80h(HDD) and 0h(Floppy) for the int 13h function.
If MBR boots, then my USB is bootable one, isn't it?
User avatar
smiddy
Member
Member
Posts: 127
Joined: Sun Oct 24, 2004 11:00 pm
Location: In my cube, like a good leming. ;-)

Post by smiddy »

User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

We need to see your code.
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

Post by muisei »

This is the MBR.
How it works:
1.Look in the MBR partition table
2.Read the LBA of the active partition
3.Do some checks
4.Read one sector from the active partition using extended function int13h AH=42h
5.Jump to the new code

When I test on real machine the problem is after that jump.Everything just hangs.

When I test with QEMU everything works fine.I load the OS image to the USB, then start QEMU and tell it to use the stick as a primary HDD.
"All parts should go together without forcing. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer." -- IBM maintenance manual, 1975
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

Post by muisei »

I deleted the code that I posted in the previous message because the problem wasn't there.The problem is in my first stage boot loader, which loads a second stage boot loader.
The enableA20 function is empty because I think that it is not relevant to this problem.
First stage bootloader:

Code: Select all

[BITS 16]
[ORG 0x00]

; This code is loaded at 0x0600
start:
        mov     ax,cs
        mov     ds,ax
        mov     fs,ax
        mov     es,ax

        mov     si,msg_loaded
        call    DisplayMessage

        mov     ah,0
        int     16h

        mov     ax,0x8fc0                       ;This point 0xffff bytes before 0x9fbff(the upper limit)
        mov     ss,ax
        
        mov     ax,0xffff                       ;The stack is growing down from 0x9fbff
        mov     sp,ax

        mov     ax,0x07c0                       ;Code segment of the MBR
        mov     gs,ax

        call    enableA20
        cmp     eax,-1
        jne     .after1
        mov     si,a20disabled
        call    DisplayMessage
        jmp     .end
.after1:

; This will search the MBR for an active partition
; to find the begining of the BabyOS
        mov     cx,4
        mov     bx,446
.loop1:
        cmp     BYTE [gs:bx],0x80               ;The babyOS is installed on the active partition
        je      .load_os                        ;If we find an active then load the kernel and jump to it
        add     ebx,16
        loop    .loop1

        jmp     .end                            ;If we dont find active then infinite loop(We shoud never jump here)
; Load the boot2 loader
.load_os:
        mov     eax,[gs:bx+8]
        inc     eax
        mov     DWORD [packet_begin+8],eax
        mov     dl,0x80
        mov     si,packet_begin
        mov     ah,42h
        int     13h
        jmp     0x0080:0x0000

.end:
        jmp     $

;--------------------------------------------------;
; This subroutine will enable the A20 address line ;
; in the keyboard controller.Takes no arguments.   ;
; Returns 0 in EAX on success, -1 on failure.      ;
; Written for use in 16-bit code, see lines marked ;
; with 32-BIT for use in 32-bit code.              ;
;--------------------------------------------------;
enableA20:
        ret

DisplayMessage:
        pushad
.repeat:
        lodsb                           ;load next character
        or      al,al                   ;test for NUL character
        jz      .done
        mov     ah,0x0E                 ;BIOS teletype
        mov     bh,0x00                 ;display page 0
        mov     bl,0x07                 ;text attribute
        int     0x10                    ;invoke BIOS
        jmp     .repeat
.done:
        popad
        ret

msg_loaded      db      "Boot1 stage loaded",0x0A,0x0D,0x00
a20disabled     db      'Coud not enable A20 gate',0x0A,0x0D,0x00
lba     dd      0

packet_begin:
        db      packet_end-packet_begin-1
        db      0x0             ;Reserved
        db      100             ;Blocks to transfer
        db      0x0             ;Reserved
        dw      0x0000          ;Transfer buffer offset
        dw      0x0080          ;Transfer buffer segment
        dd      0x00            ;LBA 1
        dd      0x00            ;LBA 2
packet_end:

times 510-($-$$) db 0
dw      0xaa55
"All parts should go together without forcing. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer." -- IBM maintenance manual, 1975
User avatar
muisei
Member
Member
Posts: 79
Joined: Sat Sep 23, 2006 2:10 pm
Location: Bulgaria
Contact:

Post by muisei »

I tested it on a toshiba laptop and it worked.On a desktop PC it doesn't want to work.The "legacy USB" support in the PC is enabled and the emulation is of type"Hard Drive".Could this be a bug in the BIOS of the desktop PC?
"All parts should go together without forcing. Therefore, if you can't get them together again, there must be a reason. By all means, do not use a hammer." -- IBM maintenance manual, 1975
Post Reply