使用bootsector创建FAT分区作为MBR

时间:2018-01-26 15:38:30

标签: assembly x86 osdev fat mbr

我正在帮朋友为他的操作系统编写一个bootloader。我已经为他编写了一个启动参数块。但是,当我使用DD将其刻录到1.44MiB软盘映像的第一个扇区时,它似乎是有效的,它不能作为FAT16文件系统安装。但是,我没有看到代码存在任何实际问题。在这里(它的评论也非常好):

BITS 16

jmp short start

iOEM db "ShapeOS"
iSectSize dw 0x200 ; bytes / sector
iClustSize db 1    ; 1 sector per cluster (for simplicity)
iResCnt dw 1       ; number of reserved sectors
iFatCnt db 2       ; # of fat copies
iRootSize dw 224   ; size of root dir
iTotalSect dw 2880 ; total sectors
iMedia db 0xF0     ; media descriptor
iFatSize dw 9      ; size of each FAT
iTrackSect dw 9    ; sectors per track
iHeadCnt dw 2      ; number of r/w heads
iHiddentSect dd 0  ; number of hidden sectors
iSect32 dd 0       ; number of > 32MB sectors
iBootDrive db 0    ; holds drive of bootsector
iReserved db 0     ; empty reserved attribute
iBootSign db 0x29  ; extended bootsig
iVolID db "seri"   ; disk serial
acVolumeLabel db "MYVOLUME  " ; volume label
acFSType db "FAT16  "         ; fs type

start:
    cli
    mov ax, 0x07C0  
    add ax, 288     
    mov ss, ax
    mov sp, 4096

    mov ax, 0x07C0      
    mov ds, ax
    sti

    call clear_screen

    mov si, intro
    call puts16
    mov si, loadmsg
    call puts16
    mov si, failed
    call puts16

    jmp $           


    intro db 'Shaper Bootloader, written by KingLuigi4932 and Kerndever', 0xD, 0xA, 0
    loadmsg db 'Loading kernel... ', 0
    failed db 'Failed!', 0xD, 0xA, 0


puts16:
    mov ah, 0Eh     

.repeat:
    lodsb           
    cmp al, 0
    je .done        
    int 10h         
    jmp .repeat

.done:
    ret

clear_screen:
    mov AX, 1003h
    mov BL, 00h
    int 10h

    ; Clear screen
    ;; Set mode to clear screen for all bioses
    mov AH, 00h
    int 10h

    ;; Fill screen with blue background
    mov AH, 09h
    mov AL, 20h
    mov BH, 00h
    mov BL, 1Eh
    mov CX, 2000h
    int 10h

    ret

times 510-($-$$) db 0
dw 0xAA55

我用来将它放在软盘映像的第一个扇区上的命令是:

dd if=/dev/zero of=fat.img bs=512 count=2880

然后:

dd status=noxfer conv=notrunc if=bootloader.bin of=fat.img

谢谢!

1 个答案:

答案 0 :(得分:3)

作为commented by Shift_Left,BPB中的所有内容都在错误的位置。

  • BIOS参数块必须从引导扇区内的偏移量3开始 偏移0处的跳转指令可以是近跳转(jmp near start)或短跳转,然后是填充nop指令(jmp short start nop)。

    < / LI>
  • BPB包含3个字符串字段,您已将它们全部定义为1个字符短!

    iOEM          db "ShapeOS "     ; Must have 8 characters!
    acVolumeLabel db "MYVOLUME   "  ; Must have 11 characters!
    acFSType      db "FAT16   "     ; Must have 8 characters!
    

鉴于引导扇区将位于线性地址7C00h的内存中,并且您将DS段寄存器初始化为07C0h,最好将此代码与ORG 0指令组合在一起。

ORG 0
BITS 16

jmp near start

iOEM          db "ShapeOS "
iSectSize     dw 0x200         ; bytes / sector
iClustSize    db 1             ; 1 sector per cluster (for simplicity)
iResCnt       dw 1             ; number of reserved sectors
iFatCnt       db 2             ; # of fat copies
iRootSize     dw 224           ; size of root dir
iTotalSect    dw 2880          ; total sectors
iMedia        db 0xF0          ; media descriptor
iFatSize      dw 9             ; size of each FAT
iTrackSect    dw 9             ; sectors per track
iHeadCnt      dw 2             ; number of r/w heads
iHiddentSect  dd 0             ; number of hidden sectors
iSect32       dd 0             ; number of > 32MB sectors
iBootDrive    db 0             ; holds drive of bootsector
iReserved     db 0             ; empty reserved attribute
iBootSign     db 0x29          ; extended bootsig
iVolID        db "seri"        ; disk serial
acVolumeLabel db "MYVOLUME   " ; volume label
acFSType      db "FAT16   "    ; fs type

start: