Page 1 of 1

USB not booting Legacy Mode correctly.

Posted: Sat Jan 23, 2016 9:39 am
by zehawk
Hey guys!

So I got a new computer today, and tried to boot my OS from a USB on there. Legacy mode, and prior, on other systems, it worked fine. However, for whatever reason it refuses to work on this laptop. I'm curious what could possibly be the problem.

In the BIOS, I set the Legacy Mode to [Enabled] and Secure Boot to [Disabled]. Afterwards, I went to the Boot Order Menu (F9) and selected the option: USB Hard Drive - 8.07. However, upon the USB being 'ran', it comes up with the message:

Reboot and select proper boot device, or Insert boot device and press any key to continue

Obviously the USB for whatever reason isn't being ran correctly. The code for the test OS has ran correctly on another computer (With a different USB, mind you, and no, I do not have access to that USB). It's interesting to note that even on that computer, USB's of size greater than 8 GB wouldn't run for some reason. The size of this USB is 8 gb, but i'm not sure if that's relevant to anything.

Any idea on how I can get the USB to correctly boot in Legacy Mode?

Here's the code to the OS:

Code: Select all

BITS 16

org 0x7C00

start:
        mov ax, 0
        mov ds, ax
        mov es, ax
        mov fs, ax
        mov gs, ax

        mov ss, ax ;Puts 0 into the segment pointer, we are using real memory.
        mov sp, 0x7C00 ;Moves 7C00 into the stack pointer, so that all data <7C00 is stack.

        call print_string ;Calls print string.
        jmp Exit


;Prints the test string for now.
print_string:

        mov si, MESSAGE

    .nextChar:

        mov ah, 0x0E
        mov al, [si]
        cmp al, 0x0
        je .end

        int 10h
        add si, 1
        jmp .nextChar
    .end:
        ret


    MESSAGE db "Hello world!", 0

Exit:


times 510-($-$$) db 0   ; Pad remainder of boot sector with 0s
dw 0xAA55      ; The standard PC boot signature


Any ideas or suggestions would be greatly appreciated! (And if your response is to use a Virtual Emulator, I don't want that as an answer for this particular case for the sole fact that I'm retrying to get it working on physical hardware like it did prior before going back to emulation.)

Re: USB not booting Legacy Mode correctly.

Posted: Sat Jan 23, 2016 9:52 am
by Combuster
BIOSes are quite fragile when it comes to USB hardware, and often don't respect traditional booting mechanisms - i.e. the 55 AA is not going to be enough. It might be very likely that it's looking for a BPB and/or a partition table. Your bootsector contains neither.

Rumours have it that such BIOSes probes, and uses the validity of these structures to decide what drive number and disk functions to provide, whereas others make that choice blindly. I'm sure someone else has a more complete story on the subject matter - at least there's one hidden deep down in forum history.

Re: USB not booting Legacy Mode correctly.

Posted: Sat Jan 23, 2016 10:42 am
by shmx
Try to create MBR.

Re: USB not booting Legacy Mode correctly.

Posted: Sat Jan 23, 2016 11:17 am
by Octocontrabass
In order to reliably boot a USB drive in hard disk mode, you must have a valid partition table with at least one active partition. "Valid" means all fields must contain values that agree with each other and the size of the disk. The start and end sectors must be valid for both C/H/S (using 255 heads and 63 sectors per track, rounded down to the last whole cylinder if the partition extends into the partial cylinder at the end of the disk) and LBA.

I have seen at least one BIOS that will skip the MBR code entirely under certain circumstances; whatever causes it occurs when the active partition contains a valid FAT filesystem but does not occur when the active partition has no filesystem. You may wish to keep this in mind if you're planning on using any filesystem with similar headers as FAT.
zehawk wrote:It's interesting to note that even on that computer, USB's of size greater than 8 GB wouldn't run for some reason.
Since your boot sector does not meet all of the above requirements, that computer may have been trying to boot in floppy disk mode. In floppy disk mode, it's impossible to access more than the first ~8GB of the disk, which may be the reason why that computer refused to boot.

Re: USB not booting Legacy Mode correctly.

Posted: Sat Jan 23, 2016 12:07 pm
by Brynet-Inc
I've seen a few systems recently that won't boot from USB in legacy mode unless using F12 boot menu. I wouldn't be surprised if they were avoiding the USB controller initialization simply to gain faster boot times.

...of course, it could be a matter of code neglect. There probably isn't a whole lot of testing being done for legacy features.

Re: USB not booting Legacy Mode correctly.

Posted: Sat Jan 23, 2016 1:20 pm
by zehawk
Hmm, so I will go ahead and try creating a partition table then. It's interesting to see the different BIOSes treating USB's differently. The fact that the last computer went to floppy drive emulation and that's what made it work seems interesting, unfortunately it seems 'll have to try and add the Hard drive specifications to this USB just to test a simple hello world bootloader. But it isn't the end of the world. Thanks guys!

Note: My computer doesn't have a F12 BIOS menu, but the F9 one is the one I've been using. Same principle though. I'll post my progress if there is any, and if anything else comes up I'll be sure to ask.

Re: USB not booting Legacy Mode correctly.

Posted: Sun Jan 24, 2016 7:12 am
by sydgrew
Doris boot sector: worth posting perhaps because this code works on every machine I've tested.

Code: Select all

section .text

[BITS 16]

; This ORG seems to be essential, perhaps for the lgdt
[ORG 0x7c00]

; 1) Set up a stack, and set ds and es. Note that the
; cli should be the SECOND instruction.
_start: xor   ax,ax
	cli
	mov   ds,ax
	mov   es,ax
	mov   ax,0x9000
	mov   ss,ax
	mov   sp,0x0ff8
	cld

; Now 2) change the a20 state as necessary. 3) go into flat real mode.
; 4) enable interrupts. 5) read further sectors. 6) jump to them and
; carry on.

; (I have the gdt in this boot sector at present, but both it and
; the "going into flat real mode" could actually be in the "further
; sectors" mentioned above.)

; Then at the end of this boot sector there is a partition
; table. I have tested this code on about ten computers so far,
; and only one of them - an old Acer Aspire 5720AG - required the presence
; of this partition table before it would boot. I copied the table from
; somewhere - a Kolibri USB probably. The only part I actually use is the first
; byte, the drive number.

; ---------------------------------------------------------------------
; Partition table, at a fixed place - 446 or 1beh
	TIMES 446-($-$$) db 0
partab:	db  80h,0,1,1
	db  0ch,67h,0e0h,52h
	db  80h,1fh,0,0
	db  78h,0b0h,0e6h,0
	TIMES 16*3 db 0

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

; And a boot sector signature.
	TIMES 510-($-$$) db 0
	db  0x55,0xaa

; =====================================================================
I use linux dd to copy the sector(s) to the USB.