About USB boot from usb

2019-08-19 14:19发布

问题:

I want to make ipl and boot os from my usb. I just want to read 360 sectors(512 bytes per sector).I checked code for several times and cannot find some mistakes.I have already debug it on qemu but it always returns "load error"(as in this code, it jumps to error section).It seems lba search did not work and always get carry flag 1 when I do it. my qemu program is like this

qemu-system-i386 -usb  ipl.bin

and ipl.bin is compiled binary file made from assembler code below.

; haribote-ipl
; TAB=4


; 以下は標準的なFAT32usbメモリのための記述

    [bits 16]
    [org 0x7c00]
        ;; BPB Structure                                                                                                                     
JMP     entry           ;BS_jmpBoot                                                                                                  
BS_OEMName      DB "HARIBOTE"
BPB_BytsPerSec  DW 0x0200
BPB_SecPerClus  DB 0x01
BPB_RsvdSecCnt  DW 0x0020
BPB_NumFATs     DB 0x02
BPB_RootEntCnt  DW 0x0000
BPB_TotSec16    DW 0x0000
BPB_Media       DB 0xf8
BPB_FATSz16     DW 0x0000
BPB_SecPerTrk   DW 0x0001
BPB_NumHeads    DW 0x0001
BPB_HiDDSec     DD 0x00000000
BPB_TotSec32    DD 0x00ee5000
BPB_FATSz32     DD 0x00000020
BPB_ExtFlags    DW 0x0000
BPB_FSVer       DW 0x0000
BPB_RootClus    DD 0x00000002
BPB_FSInfo      DW 0x0001
BPB_BkBootSec   DW 0x0006
        times   12      DB 0    ;BPB_Reserverd                                                                                               
BS_DrvNum       DB 0x80
BS_Reserved1    DB 0x00
BS_BootSig      DB 0x29
BS_VolID        DD 0xa0a615c
BS_VolLab       DB "ISHIHA BOOT"
BS_FileSysType  DB "FAT32   "

; プログラム本体

entry:  
              ;初始化寄存器
        MOV AX, 0 
            MOV DS, AX   
            MOV ES, AX
        MOV BX, AX
prepare:
        CLI     ; BIOSがSTIし忘れていても大丈夫なために
            MOV     [drv],DL ; 起動ドライブ番号がDLに入っている(BIOSがDLに入れてからMBRを起動するので)
            CMP     DL,0x80
            JB      error    ; HDD系デバイスでなければエラー
            MOV     AH,0x41
            MOV     BX,0x55aa
            INT     0x13
            JC      error
            CMP     BX,0xaa55
            JNE     error
            TEST    CL,0x01
            JZ      error
readloop:
        MOV  CL, 0
retry:
        MOV AH, 0x0e
        MOV AL, CL
        ADD AL, 48
        MOV BX, 15
        INT 0x10
        MOV DL, 0x80 
            MOV AX, 0x4200
        MOV SI, packet 
            INT 0x13
            JNC next
        ADD CL, 1
        MOV DL, 0x80 
        MOV AH, 0x00
        INT 0x13
        CMP CL, 6
        JAE error
        JMP retry
next:


        MOV AX,[bufferoff]  
            MOV BX,bufferoff  
            ADD AX,0x0200  
            MOV [BX],AX

        MOV AX,[blockNum]  
        MOV BX,blockNum  
        ADD AX,1  
        MOV [BX],AX

        CMP AX, 360
        JB readloop

        JMP 0xc200
error:
        MOV     SI,msg
putloop:
        MOV     AL,[SI]
        ADD     SI,1            ; SIに1を足す
        CMP     AL,0
        JE      fin
        MOV     AH,0x0e         ; 一文字表示ファンクション
        MOV     BX,15           ; カラーコード
        INT     0x10            ; ビデオBIOS呼び出し
        JMP     putloop


fin:
        HLT                     ; 何かあるまでCPUを停止させる
        JMP     fin             ; 無限ループ
msg:
        DB      0x0a, 0x0a      ; 改行を2つ
        DB      "load error"
        DB      0x0a            ; 改行
        DB      0


drv:        DB     0x80
packet:  
packet_size:    DB 0x10             ;packet大小,16个字节  
reserved:       DB 0  
count:          DW 1       ;读1个扇区  
bufferoff:      DW 0          ;读到内存0x0820处,偏移地址  
bufferseg:      DW 0x0820       ;段地址  
blockNum:       DD 1               ;起始LBA块  
                DD 0  
        RESB    0x01fe-($-$$)       ; 0x7dfeまでを0x00で埋める命令
        DB      0x55, 0xaa