0

I've written some simple bootloader that works good in QEMU and I decided to try it on real machine. I copied image file to flash drive and tried to load from it. But for some reason I've been getting just black screen with cursor somewhere near the top left corner and it made me think that it might be some problem with displaying strings on the screen. To check that idea I wrote simplistic bootloader that shows a message "Loading Image " and checks whether IO extensions (int 13h ah=42) are supported.If it's supported shows the message "Extensions are here". So in qemu it works and I'm getting these two messages as expected. But on my computer instead of two lines I'm getting one line like this "LoaExtensions are here". To print message I use int 10h ah=0Eh I tried to set different video modes using ah=0 function of the int 10h but I've always got the same result. Could you please help me to understand what could be wrong? This is the code:

bits    16                      ; we are in 16 bit real mode
org     0                       ; we will set regisers later
start:  jmp Main                    ; jump to start of bootloader


msgLoading              db "Loading Image ", 0x0D, 0x0A, 0x00
msgSectorReadingError   db "ERROR: Can't read sectors from disk",0x0D,0x0A, 0x00
msgExtNotSupported      db "ERROR: BIOS doesn't support LBA",0x0D,0x0A, 0x00
msgErrWhileLoadingImage db "ERROR: Couldn't read seconday image",0x0D,0x0A, 0x00
msgExtSupported         db "Extensions are here",0x0D,0x0A, 0x00
imgName                 db "STAGE2  SYS"
;************************************************;


;************************************************;
;   Prints a string
;   DS=>SI: 0 terminated string
;************************************************;
Print:
            lodsb               ; load next byte from string from SI to AL
            or  al, al          ; Does AL=0?
            jz  PrintDone       ; Yep, null terminator found-bail out
            mov ah, 0xE         ; Nope-Print the character
            int 0x10
            jmp Print           ; Repeat until null terminator found
PrintDone:
            ret             ; we are done, so return

;************************************************;
;   Set Video mode
;
;************************************************;
SetVideoMode:
            mov ax,0x0002
            int 0x10
            ret


;***********************************************;
;   Checks whether BIOS supports extensions
;************************************************;
CheckExtenstions:

        xor ax,ax
        mov ah,0x41
        xor dx,dx
        mov dl,BYTE[driveNumber]
        mov bx,0x55AA
        int 0x13
        cmp bx,0xAA55
        jnz .error
        test cl,0x1
        jz .error
        jmp .exit
.error:
        mov si,msgExtNotSupported
        call Print
        cli
        hlt
.exit:
        mov si,msgExtSupported
        call Print
        cli 
        hlt
        ret




Main:

        ;----------------------------------------------------
        ; code located at 0000:7C00, adjust segment registers
        ;----------------------------------------------------

        cli                             ; disable interrupts
        mov     ax, 0x07C0              ; setup registers to point to our segment
        mov     ds, ax
        mov     es, ax
        mov     fs, ax
        mov     gs, ax

        ;----------------------------------------------------
        ; create stack
        ;----------------------------------------------------

        mov     ax, 0x0000              ; set the stack
        mov     ss, ax
        mov     sp, 0xFFFE
        mov     BYTE[driveNumber],dl
        sti                             ; restore interrupts
        call SetVideoMode
        mov     si, msgLoading          ; Show Loading Message
        call    Print

        call CheckExtenstions

        cli
        hlt

TIMES 510-($-$$) DB 0
DW 0xAA55 
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • what image format are you using? And how did you copy the image to the flash drive – preciousbetine Nov 25 '18 at 18:16
  • Like this nasm bootloader.asm -o boot.bin dd if=boot.bin of=/dev/sdc bs=512 count=1. I've just tried exactly the same flash drive with another real machine and it worked as expected. So looks like something wrong with my first real machine. – Andrew Bolotov Nov 25 '18 at 18:24
  • 1
    I recommend you look at this answer,if I had to guess it is because you don't have a BIOS Parameter Block area at the beginning of your bootloader. Your BIOS is probably overwriting the are with drive geometry information and thus clobbering some of your code. That would likely be the case if booting USB with Floppy emulation: https://stackoverflow.com/a/47320115/3857942 – Michael Petch Nov 25 '18 at 20:10
  • I configured my BIOS to emulate Flash drive as HDD. I print the DL and I get 80h. Will try to add BPB to check. Thanks! – Andrew Bolotov Nov 25 '18 at 20:45
  • 1
    If you are using USB HDD emulation you shouldn't need a BPB. Although on some BIOSes in HDD Emulation mode you need a partition table with an active partition set, although you would e having the BIOS tell you thee isn't a bootable device orrefuses to boot from the device. – Michael Petch Nov 25 '18 at 20:51
  • But you can try it. But you bootloader almost seems to be acting like it was booted as USB FDD – Michael Petch Nov 25 '18 at 20:53
  • You were right. I added BPB and now I see strings printed properly. This thing actually confuses me a lot. I enhanced my simple bootloader by capability to print registers. So this is what I have right after booting in DL=80h. Then I try to get disk geometry by issuing int 13h ah=02. And I'm getting DX=3F03 CX=E6FF which means that my emulated HDD has 64 heads, 923 Cylinders and 63 sectors per cylinder and it gives me 1,8Gb in total (my flash drive is 2Gb). – Andrew Bolotov Nov 25 '18 at 20:58
  • Also I've checked my another flash drive with CentOS on it that I used to install OS on the same computers I'm running tests on. It has partition table on it but it doesn't have BPB. Can you please explain why it works and don't get overwritten by BIOS? Thanks! – Andrew Bolotov Nov 25 '18 at 21:02
  • 1
    Just conducted some small experiment. I created partition table using fdisk with one partition on it and I didn't mark it as bootable. Then I created FAT16 on the partition and copied my bootloader bin file (w/o 55AA signature) using dd command. And it worked. So looks like if there is no either BPB or partition table my BIOS does something wrong with my code. – Andrew Bolotov Nov 25 '18 at 21:21
  • 2
    All BIOSes have quirks. But in general with USB HDD you need a partition table (some BIOSes actually need one marked as active as well).With USB FDD you need a BPB. With USB HDD you shouldn't need both a BPB and Partition Table (Should work with just the partition table) – Michael Petch Nov 25 '18 at 21:28

0 Answers0