【问题标题】:Issue loading sector from hard disk问题从硬盘加载扇区
【发布时间】:2017-05-17 05:55:39
【问题描述】:

我编写了一个 MBR 引导加载程序,它检测到分区但无法加载它们。中断 13 失败了,有谁知道我做错了什么,我敢肯定这是愚蠢的尝试修复它几个小时

我的引导加载程序代码是:

[BITS 16]   ;tell the assembler that its a 16 bit code
[ORG 0]
%define PARTITION_TABLE_START 0x1be
%define PARTITION_TABLE_END 0x1ee
%define PARTITION_ENTRY_SIZE 16
%define COPY_DEST 0x7e00
_begin:
; We must copy the bootloader into a different area of memory
mov ax, 0
mov ds, ax
_copy:
mov si, 0x7c00+_begin
mov di, COPY_DEST
mov cx, 0
._continue:
lodsb
stosb
cmp cx, 0x200
je ._finish
inc cx  
jmp ._continue
._finish:
jmp 0x7e0:_start
_start:
; We are running at 0x7e00 now
mov ax, 0x7e0
mov ds, ax
mov es, ax
mov ss, ax

; Save drive number
mov byte[_drive_no], dl

mov si, _welcome_message
call _print


mov si, _find_message
call _print

mov bx, PARTITION_TABLE_START
_csearch:
cmp byte [bx], 0x80
je _bootable_found
cmp bx, PARTITION_TABLE_END
jae _no_bootable_found
add bx, PARTITION_ENTRY_SIZE
jmp _csearch


_bootable_found:
mov si, _found_message
call _print
; BX Contains current entry position
mov ah, 0x02
mov al, 1
mov dh, [bx+1], ; Head
mov cl, [bx+2] ; Sector
shr cl, 2 ; Ignore bit 6-7 they are not for us
mov ch, [bx+3] ; Cylinder (warning only 8 bits supported)
mov byte dl, [_drive_no] ; Drive number
; Destination: 0x7c00
push ax
mov ax, 0x7c0
mov es, ax
mov bx, 0
pop ax
int 0x13
jc _read_error
mov si, _press_any_key_to_load
call _print
mov ah, 0
int 0x16
; Read success lets jump to the new bootloader
jmp 0x7c0:0

_read_error:
mov si, _read_error_msg
call _print
jmp $

_no_bootable_found:
mov si, _no_partition
call _print
jmp $


_print:
    mov ah, 0x0e
._loop: 
    lodsb
    cmp al, 0
    je ._done
    int 0x10
    jmp ._loop
._done:
    ret
_welcome_message: db 'Welcome to NibbleBits bootloader', 10, 13, 0
_find_message: db 'Will find first active partition', 10, 13, 0
_found_message: db 'Active partition found', 10, 13, 0
_no_partition: db 'No active partition could be found', 10, 13, 0
_read_error_msg: db 'Failed to load partition', 10, 13, 0
_press_any_key_to_load: db 'Press any key to load the partition', 10, 13, 0
_drive_no: db 0

TIMES 510 - ($ - $$) db 0   ;fill the rest of sector with 0
DW 0xAA55           ; add boot signature at the end of bootloader
_end:

【问题讨论】:

  • 按照论坛规则,亲爱的你应该在这里提供代码。
  • 您可以使用 REP MOVSB 简化整个 LODSB/STOSB。但是有了那个意思,STOSB 依赖于 ES 的设置。您展示了 mov es, axmov ds, ax 。这可能不是问题,但首先应该解决的问题(它可能不适用于所有硬件)但在许多虚拟环境中它可能已经是 0。
  • 我假设您在使用此代码创建 MBR 后,正在从其他地方将分区表复制到引导加载程序中?
  • 是的 Michael 我目前在十六进制编辑器中手动输入分区表 我编写了一个程序来为我做这件事,但它有一些错误,就像我早上 5 点做的那样大声笑可能很简单。任何人都可以看到我的问题。我相信我正在设置头,部门等正确。在这里寻找:wiki.osdev.org/Partition_Table
  • shr cl, 2 将扇区号除以 4。您确定这就是您想要的吗?如果您的意图只是将第 6 位和第 7 位归零,也许您的意思是 and cl, 0x3f ?更好的是,在从分区表中获取这些值之后,我不会对它们做任何事情。将它们按原样读入 dh、cl、ch。

标签: assembly nasm intel x86-16 bootloader


【解决方案1】:

事实证明我的代码很好,确实改变了 Michael Petch 提出的一些东西,但实际加载很好。问题是,当我使用 linux 的“dd”程序复制引导扇区时,它会截断整个文件。我通过启用一个标志来防止截断来解决这个问题。

【讨论】:

    猜你喜欢
    • 2019-12-05
    • 2021-09-10
    • 2013-07-06
    • 2013-03-18
    • 2013-07-15
    • 2012-03-29
    • 2020-06-12
    • 2011-11-09
    • 2014-12-21
    相关资源
    最近更新 更多