Bootloader garbage loading on real hardware

I am trying to write my own bootloader. Although it works great on QEMU, Bochs, and VirtualBox, I can't get it to work on my laptop.

On my laptop, the bootloader behaves differently to all emulators hanging from a seemingly random place, refusing to print, even skipping some instructions jmp $.

While I have a lot of problems with "real equipment", I think there is one reason for them.

The following code is a short bootloader that should print a “TEST” message 3 times, then hang, jumping to the same place:

[BITS 16]                                                                          
[ORG 0x7C00]                                                                                                    
    jmp 0x0000:start_16  ; In case bootloader is at 0x07C0:0x0000                                                             
start_16:                                                                          
    xor ax, ax                                                                 
    mov ds, ax                                                                 
    mov es, ax                                                                 
    cli                             ; Disable interrupts                       
    mov ss, ax                                                                 
    mov sp, 0x7C00                                                             
    sti                             ; Enable interrupts                        
    cld                             ; Clear Direction Flag                     
    ; Store the drive number                                                   
    mov [drive_number], dl                                                     
    ; Print message(s)                                                         
    mov si, msg                                                                
    call print_string                                                          
    mov si, msg                                                                
    call print_string                                                          
    mov si, msg                                                                
    call print_string                                                          

    jmp $   ; HALT                                                                                   

; print_string                                                                     
;       si      = string                                                           
print_string:                                                                      
    pusha                                                                      
    mov ah, 0x0E                                                               
.repeat:                                                                           
    lodsb                                                                      
    cmp al, 0x00                                                               
    je .done                                                                   
    int 0x10                                                                   
    jmp short .repeat                                                          
.done:                                                                             
    popa                                                                       
    ret                                                                        

; Variables                                                                        
drive_number db 0x00                                                               
msg db 'TEST', 0x0D, 0x0A, 0x00                                                    
times 510-($-$$) db 0x00                                                           
db 0x55                                                                            
db 0xAA

Compile and emulate code with

$ nasm -f bin bootloader.asm
$ qemu-system-x86_64 bootloader

On emulators, it prints "TEST" three times and freezes. On my laptop it prints "TEST" followed by 3 strange characters:

Bootloader output on my laptop.

Most bootloader code from http://wiki.osdev.org does not work either. For example, none of the code snippets from http://wiki.osdev.org/Babystep2 work on my laptop.

What is wrong with my code? How can i fix this?


Additional Information

If I delete 2 unnecessary mov si, msg, the message "TEST" will be printed twice .

A laptop:

  • Asus Vivobook S200,
  • Processor: Intel i3-3217U
  • BIOS: American Megatrends, version 210.
  • The computer works just fine with any other bootloader, such as Grub.

Build and Burn:

$ nasm -f bin bootloader.asm
$ qemu-system-x86_64 bootloader # TEST 1 
$ sudo dd if=/dev/zero of=/dev/sdd bs=1M count=1 # clean the USB 
$ sudo dd if=bootloader of=/dev/sdd conv=fsync # write to USB 
$ qemu-system-x86_64 /dev/sdd # TEST 2 

Change 1

Ross Ridge noted in the comments that the Ω♣|first 3 bytes of the bootloader.

2

​​ :

print_string:                                                                      
    pusha                                                                          
.repeat:                                                                        
    mov ah, 0x0E                                                                
    xor bx, bx                                                                  
    cld                             ; Clear Direction Flag                      
    lodsb                                                                       
    cmp al, 0x00                                                                
    je .done                                                                    
    int 0x10                                                                    
    jmp short .repeat                                                           
.done:                                                                          
    popa                                                                        
    ret 

msg db 'TEST', 0x00 

TEST. .

3

, dumpregs . int 0x10 . dumpregs drive_number jmp $ . 1 . : dumpregs around disk assignment

: https://gist.github.com/anonymous/0ddc146f73ff3a13dd35

4

:

$ ndisasm -b16 bootload2 -o 0x7c00

https://gist.github.com/anonymous/c9384fbec25513e3b815

+5
2

, BIOS BIOS, , , . , , , USB- , , , BPB . BPB, ( NOP) bootsector. :

[BITS 16]                                                                          
[ORG 0x7C00]
    xor ax,ax                                                                                                
    jmp 0x0000:start_16  ; In case bootloader is at 0x07C0:0x0000                                                             
start_16:                       

, - BPB. , BPB , BPB 4, far jump 5 .

, BPB, :

[BITS 16]                                                                          
[ORG 0x7C00]
    jmp start
    nop
    resb 8 + 25
start:                                                                                          
    jmp 0x0000:start_16  ; In case bootloader is at 0x07C0:0x0000                                                             
start_16:                       

, , :

dumpregs:
    push    es
    pusha
    push    0xb800
    pop es  
    mov di, [vidmem_ptr]
    mov bp, sp
    mov cx, 8
dump_loop:
    dec bp
    dec bp
    mov ax, [bp + 16]
    call    printhex2
    inc di
    inc di
    loop    dump_loop
    mov [vidmem_ptr], di
    popa
    pop es
    ret

printhex2:
    push    ax
    mov al, ah
    call    convhex1
    pop ax
convhex1:
    aam 16
    ; DB    0D4h, 16
    xchg    al, ah
    call    convhex
    mov al, ah
convhex:
    cmp al, 10
    jb  lessthan_10
    add al, 'A' - '0' - 10
lessthan_10:
    add al, '0'
    stosb
    mov al, 7
    stosb
    ret

vidmem_ptr dw 5 * 80 * 2 ; start at row 5

. PUSHA/POPA: AX CX DX BX SP BP SI DI

INT 0x10 , :

Screenshot of boot with debug output

, , 8 8 . BIOS, , .

+4

. , :

a) USB-

b) BIOS , . , .

c) BIOS "" BPB.

d) BPB, / , ( ).

, , . , BIOS BPB, "BPB-" , , , , , -. , BIOS 0x1C 0x1F 0x24.

+2

Source: https://habr.com/ru/post/1625658/


All Articles