int 13h 42h doesn't load anything in Bochs

前端 未结 1 543
感动是毒
感动是毒 2020-12-01 20:10

I changed my bootloader from CHS to LBA, so I replaced int 13h 02h with int 13h 42h. It works correctly in QEMU, however, I have troubles running i

相关标签:
1条回答
  • 2020-12-01 21:00

    LBA Disk Access Availability

    Not all BIOSes support the extended disk read and write functions (although on modern hardware they almost all likely will). Not all BIOSes support extended disk reads of floppies via Int 13h/AH=42h. This is true of BOCHS as well. You can test whether extended disk functions are available on a drive via Int 13/AH=41h/BX=55AAh. This does an extended disk installation check.

    If you want to test your code on BOCHS using extended disk reads and LBA you will have to create a hard disk image and modify BOCHS to boot from it instead of floppy. The minimum size hard disk image size that BOCHS supports is one with CHS = 1/16/63 which is 512*16*63 = 516096 bytes or 1008 sectors of 512 bytes each.

    You can modify your bochsrc.txt to be:

    megs: 32
    romimage: file=/usr/share/bochs/BIOS-bochs-latest, address=0xfffe0000
    vgaromimage: file=/usr/share/bochs/VGABIOS-lgpl-latest
    boot: c
    ata0: enabled=1, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
    ata0-master: type=disk, path="disk.img", mode=flat, cylinders=0, heads=0, spt=0, model="Generic 1234", biosdetect=auto, translation=auto
    panic: action=ask
    log: bochsout.txt
    mouse: enabled=0
    keyboard: type=mf, serial_delay=200, paste_delay=100000
    display_library: x, options="gui_debug"
    

    I use a disk image called disk.img. You can create it and place the sectors you generate into it with commands like:

    nasm -f bin main.asm -o main.bin
    

    Create an image of 516096 bytes:

    dd if=/dev/zero of=disk.img count=1008 bs=512
    

    Place the boot sectors into the beginning of disk.img without truncating the file:

    dd if=main.bin of=disk.img conv=notrunc
    

    I have additional information on using DD to create disk images in this Stackoverflow answer.


    General Code Observations

    Although the disk image is part of the issue with BOCHS you do have some coding issues. You can't assume the value of CS will be set to what you think when control is transferred from the BIOS to your bootloader. See my General Bootloader Tips for more information. If you want DS and ES to be zero (which is what you'd need with org 0x7c00), you should modify the start of your code to be something like:

    org 0x7c00
    bits 16
    
    boot:
        cli
    
        xor ax, ax     ; Explicitly set DS and ES to 0
        mov ds, ax
        mov es, ax
    

    To test out your code I added this after bootend:

    bootend:
    
        ; Print MDP to upper left of screen in white on light magenta
        mov ax, 0xb800
        mov es, ax
        mov word [es:0x0000], 0x57<<8 | 'M'
        mov word [es:0x0002], 0x57<<8 | 'D'
        mov word [es:0x0004], 0x57<<8 | 'P'
    
        ; Infinite loop so we don't have the CPU wander memory
        cli
    endloop:
        hlt
        jmp endloop
    

    I'm not sure this line is a typo or you are using some type of pre-processor on your assembly files before passing them to NASM. Normally this line would be a problem with the % symbol before 1:

    dw %1 ; Number of sectors to be loaded
    

    NASM would only support this directly:

    dw 1  ; Number of sectors to be loaded
    

    Real Hardware / USB / Laptop Issues

    If you are attempting to use USB to boot on real hardware then you may encounter another issue even if you get it working in BOCHS with the changes above. If your BIOS is set to do USB FDD emulation (and not USB HDD or something else) you may need to add a Boot Parameter Block(BPB) to the beginning of your bootloader. You can create a fake one like this:

    org 0x7c00
    bits 16
    
    boot:
        jmp main
        TIMES 3-($-$$) DB 0x90   ; Support 2 or 3 byte encoded JMPs before BPB.
    
        ; Dos 4.0 EBPB 1.44MB floppy
        OEMname:           db    "mkfs.fat"  ; mkfs.fat is what OEMname mkdosfs uses
        bytesPerSector:    dw    512
        sectPerCluster:    db    1
        reservedSectors:   dw    1
        numFAT:            db    2
        numRootDirEntries: dw    224
        numSectors:        dw    2880
        mediaType:         db    0xf0
        numFATsectors:     dw    9
        sectorsPerTrack:   dw    18
        numHeads:          dw    2
        numHiddenSectors:  dd    0
        numSectorsHuge:    dd    0
        driveNum:          db    0
        reserved:          db    0
        signature:         db    0x29
        volumeID:          dd    0x2d7e5a1a
        volumeLabel:       db    "NO NAME    "
        fileSysType:       db    "FAT12   "
    
    main:
        cli
    
        xor ax, ax     ; Explicitly set DS and ES to 0
        mov ds, ax
        mov es, ax
        [rest of your code here]
    

    If you were to modify your code to have the layout above the Unix/Linux file command may be able to dump out the BPB data that it thinks makes up your MBR in the disk image. Run the command file disk.img and you may get this output:

    disk.img: DOS/MBR boot sector, code offset 0x3c+2, OEM-ID "mkfs.fat", root entries 224, sectors 2880 (volumes <=32 MB) , sectors/FAT 9, sectors/track 18, serial number 0x2d7e5a1a, unlabeled, FAT (12 bit)


    Complete Example with Int 13h Extension Checks

    The following code will check that Int 13h extensions are available in the BIOS and will also determine if the drive in DL supports Int 13h extensions. If there is a failure the code will print an appropriate error. A print_string function is used to display strings to the console using BIOS TTY output and a print_hex_word function is provided to display the boot drive number in hexadecimal.

    org 0x7c00
    bits 16
    
    section .text
    boot:
        jmp main
        TIMES 3-($-$$) DB 0x90   ; Support 2 or 3 byte encoded JMPs before BPB.
    
        ; Dos 4.0 EBPB 1.44MB floppy
        OEMname:           db    "mkfs.fat"  ; mkfs.fat is what OEMname mkdosfs uses
        bytesPerSector:    dw    512
        sectPerCluster:    db    1
        reservedSectors:   dw    1
        numFAT:            db    2
        numRootDirEntries: dw    224
        numSectors:        dw    2880
        mediaType:         db    0xf0
        numFATsectors:     dw    9
        sectorsPerTrack:   dw    18
        numHeads:          dw    2
        numHiddenSectors:  dd    0
        numSectorsHuge:    dd    0
        driveNum:          db    0
        reserved:          db    0
        signature:         db    0x29
        volumeID:          dd    0x2d7e5a1a
        volumeLabel:       db    "NO NAME    "
        fileSysType:       db    "FAT12   "
    
    main:
        cli
        cld                   ; String instructions forward movement
    
        xor ax, ax
        mov ds, ax
        mov es, ax
        ; Setup 4K stack before this bootloader
        mov ss, ax
        mov sp, 0x7c00
    
        ; Display a banner to know our bootloader is executing
        mov si, msg_booting
        call print_string
    
        ; Check that Int 13h Extensions are available
        ; http://www.ctyme.com/intr/rb-0706.htm
    
        mov ah, 0x41          ; Int 13h/AH=41h: Check if extensions present
        mov bx, 0x55aa
        int 0x13
        jc  ext_drv_none      ; CF set - no extensions available for drive
        cmp bx, 0xaa55        ; Is BX 0xaa55?
        jnz ext_none          ;     If not, int 13h extensions not supported
                              ;     by BIOS at all.
    
        ; Int 13h extensions supported by BIOS and drive at this point
        ; Load next sectors
        mov si, DAP
        mov ah, 42h
        ; DL didn't changed
        int 13h
        ; Start
        jmp bootend
    
    ; Error: BIOS doesn't support Int 13h extensions
    ext_none:
        mov si, err_no_extensions
        call print_string
        jmp error_end
    
    ; Error: BIOS supports Int 13h extensions but not for drive in DL
    ext_drv_none:
        mov si, err_no_drv_ext_support
        call print_string
    
        ; Print the boot drive number in hex
        xor dh, dh            ; Zero extended drive number to all of DX
        push word 0x00        ; Attribute and page number to write to
        push dx               ; The value to write as hex
        call print_hex_word
    
    
    error_end:
        cli
    .loop:
        hlt
        jmp .loop
    
    ; Print 16 bit value passed on stack as first parameter
    ; in hexadecimal. Use page number and foreground color
    ; passed in second parameter. This routine will work on 8086+
    ; processors. This code takes advantage of packed BCD to
    ; determine the ASCII values to print. This code could have
    ; used compare and branch to do the same or a translation table.
    
    print_hex_word:
        push bp
        mov bp, sp      ; BP=SP, on 8086 can't use sp in memory operand
        push dx         ; Save all registers we clobber
        push cx
        push bx
        push ax
    
        mov cx, 0x0404  ; CH = number of nibbles to process = 4 (4*4=16 bits)
                        ; CL = Number of bits to rotate each iteration = 4 (a nibble)
        mov dx, [bp+4]  ; DX = word parameter on stack at [bp+4] to print
        mov bx, [bp+6]  ; BX = page / foreground attr is at [bp+6]
    
    .loop:
        rol dx, cl      ; Roll 4 bits left. Lower nibble is value to print
        mov ax, 0x0e0f  ; AH=0E (BIOS tty print),AL=mask to get lower nibble
        and al, dl      ; AL=copy of lower nibble
        add al, 0x90    ; Work as if we are packed BCD
        daa             ; Decimal adjust after add.
                        ;    If nibble in AL was between 0 and 9, then CF=0 and
                        ;    AL=0x90 to 0x99
                        ;    If nibble in AL was between A and F, then CF=1 and
                        ;    AL=0x00 to 0x05
        adc al, 0x40    ; AL=0xD0 to 0xD9
                        ; or AL=0x41 to 0x46
        daa             ; AL=0x30 to 0x39 (ASCII '0' to '9')
                        ; or AL=0x41 to 0x46 (ASCII 'A' to 'F')
        int 0x10        ; Print ASCII character in AL
        dec ch
        jnz .loop       ; Go back if more nibbles to process
    
        pop ax          ; Restore registers
        pop bx
        pop cx
        pop dx
        pop bp
        ret
    
    ; Print string pointed to by DS:SI using
    ; BIOS TTY output via int 10h/AH=0eh
    
    print_string:
        push ax
        push si
        mov ah, 0Eh       ; int 10h 'print char' function
    
    .repeat:
        lodsb             ; Get character from string
        test al, al
        je .done      ; If char is zero, end of string
        int 10h           ; Otherwise, print it
        jmp .repeat
    .done:
        pop si
        pop ax
        ret
    
    ; Disk address packet
    DAP:
        db 10h, 0
        dw 1 ; Number of sectors to be loaded
        dd bootend
        dq 1
    
    msg_booting:            db "Booting... ", 0x00
    err_no_extensions:      db "Int 13h extensions not supported by BIOS", 0x00
    err_no_drv_ext_support: db "Int 13h Extensions not supported on drive 0x", 0x00
    
    ; Fill the rest of bootsector with zeroes and end it
    times 510 - ($ - boot) db 0
    dw 0xAA55
    bootend:
    
        mov ax, 0xb800
        mov es, ax
        mov word [es:0x0000], 0x57<<8 | 'M'
        mov word [es:0x0002], 0x57<<8 | 'D'
        mov word [es:0x0004], 0x57<<8 | 'P'
        cli
        hlt
    

    Using the information to make this a hard disk image above, when I run it in BOCHS I get this output:

    If I boot this same image as a floppy disk (disk A:) in BOCHS the error message now tells me that Int 13h extensions aren't available for drive 0x0000:

    If you find that you need to read a device that doesn't support Int 13h extensions you will have to fall back to the standard Int 13h disk subfunctions for reading (AH=02)/writing (AH=03).

    0 讨论(0)
提交回复
热议问题