- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个用 x86 NASM 程序集编写的引导加载程序,旨在从 FAT16 格式的磁盘加载内核 (R.BIN)。它并没有这样做,在合并了用于调试的短消息(A、B、C、D、J、!;涉及它们的行用星号标记)之后,我发现引导加载程序一直到了它完美地跳转到加载的内核,尽管它没有跳转到内核,而是似乎再次加载自身。
为了测试引导加载程序,我安装了一个空白镜像,我一直在使用以下命令将引导加载程序写入其中:dd if=LOADER.BIN of=/dev/loop0
(我也复制了R.BIN进去)之后,我将保存这个新图像,并在 Virtualbox 中对其进行测试。
这是我的完整代码:
BITS 16
jmp main ; Jump to main bootloader
nop ; Pad out remaining bytes until boot descriptor
; Disk descriptor
OEM_name db "MARSHMAL" ; Disk label
bytes_sector dw 0x0200 ; Bytes per sector
sectors_cluster db 0x01 ; Sectors per cluster
sectors_record dw 0x0001 ; Sectors reserved for boot record
fats db 0x02 ; Number of file allocation tables
max_root_entries dw 0x0200 ; Max number of root entries
sectors dw 0x0B40 ; Number of sectors
medium_type db 0xF0 ; Type of medium (removable or fixed?)
sectors_fat dw 0x0009 ; Sectors per file allocation table
sectors_track dw 0x0012 ; Sectors per track
heads dw 0x0002 ; Number of heads
hidden_sectors dd 0x00000000 ; Number of sectors before partition
total_sectors dd 0x00000000 ; Number of sectors in medium (zero because 2B != 0)
drive_number db 0x00 ; Drive number (for BIOS int 0x13)
drive_signature db 0x00 ; NOT USED
ext_signature db 0x29 ; Extended boot signature
volume_serial dd 0x00000000 ; Volume's serial number
volume_label db "MARSHMALLOW"; Volume label
fs_type db "FAT16 " ; Filesystem type
main:
mov ax, 0x07C0
add ax, 0x0220
mov ss, ax
mov sp, 0x1000 ; 4K of stack
mov ax, 0x07C0
mov ds, ax
mov byte [drive_num], dl ; Save boot drive number
mov bx, ds
mov es, bx ; Set ES to Data Segment
mov bx, disk_buffer ; Set BX to disk buffer
mov ax, 0x13 ; Start of root = sectors_record + fats * sectors_fat = 1 + 2 * 9 = logical 19
call ls_hts ; Convert logical 19 to head, track and sector
mov al, 0x0E ; Number of sectors in root = max_root_entries * 32 / bytes_sector = 224 * 32 / 512 = 14
mov si, a ; Read root dir message*
call print_str ; Print!*
.read_disk:
int 0x13 ; BIOS disk interrupt
jnc .search_init ; If successful, get ready to search the disk
call reset_disk ; Otherwise, reset the disk
jmp .read_disk ; And retry
.search_init:
mov si, success ; Success message*
call print_str ; Print!*
mov ax, ds
mov es, ax ; Move data segment to extra segment
mov di, disk_buffer ; Location of disk buffer (ES:DI will be the location of the root entry we will be checking)
mov si, r_name ; Location of filename of R (DS:SI will be the location of the string to compare to the root entry)
mov bx, 0x00 ; Start at root entry 0
push si ; Push*
mov si, b ; Search message*
call print_str ; Print!
pop si ; Pop*
.check_entry:
mov cx, 0x0B ; Compare the first 11 bytes
push si ; Push filename location to stack
rep cmpsb ; Compare the two strings
pop si ; Restore filename location to SI
je .found_entry ; If equal, we found the root entry!
add di, 0x15 ; Otherwise, move to next entry
inc bx ; Number of next entry
cmp bx, max_root_entries ; Have we gone through all root entries?
jg .missing ; If so, R is missing
jmp .check_entry ; Otherwise, look at this next entry
.found_entry:
mov si, success ; Success message*
call print_str ; Print!*
mov ax, word [es:di+0x0F]
mov word [cluster], ax ; Move starting cluster number to our spot in memory
mov bx, disk_buffer ; ES:BX points to disk buffer
mov ax, 0x01 ; 1st FAT begins at logical sector 1
call ls_hts ; Convert to head, track and sector
mov al, sectors_fat ; Read all sectors in FAT
mov si, c ; Read FAT message*
call print_str ; Print!*
.read_fat:
int 0x13 ; BIOS disk interrupt
jnc .read_cluster ; If successful, load the first cluster of the file
call reset_disk ; Otherwise, reset the disk
jmp .read_fat ; And try again
.read_cluster:
mov si, d ; Attempt to read cluster message*
call print_str ; Print!*
mov ax, 0x2000
mov es, ax ; Segment into which we will load R
mov bx, word [buffer_pointer] ; Spot into which we will load this cluster
mov ax, word [cluster] ; Cluster to read
add ax, 0x1F ; Convert to logical sector
call ls_hts ; Convert to head, track and sector
mov al, sectors_cluster ; Read the number of sectors in 1 cluster
int 0x13 ; BIOS disk interrupt
jnc .find_next_cluster ; If successful, find the next cluster
call reset_disk ; Otherwise, reset the disk
jmp .read_cluster ; And try again
.find_next_cluster:
mov si, success ; Success message*
call print_str ; Print!*
mov ax, word [cluster] ; Location of current cluster
mov bx, 0x02 ; There are two bytes per entry in FAT16
mul bx ; The memory location of CLUSTER should fit in AL
mov si, disk_buffer ; Location of start of FAT
add si, ax ; Add the number of bytes until current cluster
mov ax, word [ds:si] ; Number of next cluster
mov word [cluster], ax ; Store this
cmp ax, 0xFFF8 ; Check whether this next cluster is an end-of-file marker
jae .jump ; If it is, we have fully loaded the kernel
jge .jump
add word [buffer_pointer], 0x0200 ; Otherwise, increment the buffer pointer a sector length
jmp .read_cluster ; And load it into memory
.jump:
mov si, loaded ; Loaded kernel message
call print_str ; Print!
mov dl, byte [drive_num] ; Make the boot drive number accessible to R
jmp 0x2000:0x0000 ; Jump to R's location!
.missing:
mov si, m_r_missing ; Display the missing message
call rsod ; Display it in a Red Screen of Death
reset_disk:
pusha ; Push register states to stack
mov ax, 0x00 ; RESET disk
mov dl, byte [drive_num] ; Boot drive number
int 0x13 ; BIOS disk interrupt
jc .disk_fail ; If failed, fatal error and reboot
popa ; Restore register states
ret ; And retry
.disk_fail:
mov si, m_disk_error ; Display the disk error message
call rsod ; Display it in a Red Screen of Death
print_str: ; Prints string pointed to by REGISTER SI to cursor location (si=str)
pusha ; Push register states to stack
mov ah, 0x0E ; BIOS will PRINT
.repeat:
lodsb ; Load next character from SI
cmp al, 0x00 ; Is this a null character?
je .ret ; If it is, return to caller
int 0x10 ; Otherwise, BIOS interrupt
jmp .repeat ; Do this again
.ret:
mov ah, 0x00 ; Read keyboard buffer
int 0x16 ; BIOS keyboard interrupt
popa ; Restore register states
ret ; Return to caller
ls_hts: ; Convert logical sector to head, track, and sector configuration for int 0x13 (AX = logical sector)
mov dx, 0x00 ; Upper word of dividend is 0
div word [sectors_track] ; Divide to find the number of tracks before this
mov cl, dl ; The remainder is the number of the sector within the track
add cl, 0x01 ; Sectors start at 1, not 0
mov dx, 0x00 ; Upper word of dividend is 0
div word [heads] ; Divide by number of heads/sides
mov dh, dl ; The remainder is the head number (it should only take up the lower half of DX)
mov ch, al ; The quotient is the track number (it should only take up the lower half of CX)
mov dl, byte [drive_num] ; Boot drive number
mov ah, 0x02 ; READ disk sectors
ret ; Return to caller
rsod: ; Red Screen of Death (SI = line to print)
mov al, 0x20 ; SPACE
mov bh, 0x00 ; Page 0
mov bl, 0x40 ; Red background
mov cx, 0x50 ; Enough to fit the screen width
.repeat:
mov ah, 0x09 ; Write character and attribute
int 0x10 ; BIOS VGA interrupt
mov ah, 0x03 ; Get cursor position
int 0x10 ; BIOS VGA interrupt
cmp dh, 0x1A ; Have we gone all the way down the screen?
jge .write ; If we have, return to caller
inc dh ; Otherwise, next row down
mov ah, 0x02 ; Set cursor position
int 0x10 ; BIOS VGA interrupt
jmp .repeat ; Do this again for the next line
.write:
mov ah, 0x02 ; Set cursor position
mov dh, 0x01 ; Row 1
mov dl, 0x03 ; Col 3
int 0x10 ; BIOS VGA interrupt
push si ; Push line to stack
mov si, fatal ; Prepare to display "FATAL" message
call print_str ; Print!
pop si ; Restore line and prepare to print it
call print_str ; Print!
mov si, press_a_key ; Prepare to display prompt
call print_str ; Print!
int 0x19 ; Reboot
data:
r_name db "R BIN" ; Filename of R
cluster dw 0x0000 ; Cluster that we are working with
buffer_pointer dw 0x0000 ; Pointer to offset of buffer
drive_num db 0x00 ; Boot drive number
fatal db "FATAL: ", 0x00 ; Fatal error message
press_a_key db "! Press a key", 0x00; Instruct the user to press a key and reboot
m_r_missing db "R missing", 0x00 ; Missing message
m_disk_error db "Disk failed", 0x00 ; Disk error message
a db "A", 0x00 ; About to read root dir*
b db "B", 0x00 ; About to search root dir*
c db "C", 0x00 ; About to read FAT*
d db "D", 0x00 ; About to attempt cluster read*
success db "!", 0x00 ; Success!*
loaded db "J", 0x00 ; Loaded R message*
times 510-($-$$) db 0x00 ; Pad remainder of boot sector
sig dw 0xAA55 ; Boot signature
disk_buffer: ; Space in memory for loading disk contents
正如我所说,引导加载程序似乎正在按计划执行所有操作,直到跳转到内核,然后在调试消息开始时重新启动并再次循环。
最佳答案
mov di, disk_buffer ; Location of disk buffer (ES:DI will be the location of the root entry we will be checking)
mov si, r_name ; Location of filename of R (DS:SI will be the location of the string to compare to the root entry)
mov bx, 0x00 ; Start at root entry 0
push si ; Push*
mov si, b ; Search message*
call print_str ; Print!
pop si ; Pop*
.check_entry:
mov cx, 0x0B ; Compare the first 11 bytes
push si ; Push filename location to stack
rep cmpsb ; Compare the two strings
pop si ; Restore filename location to SI
je .found_entry ; If equal, we found the root entry!
add di, 0x15 !!!!!!!!!!!! ; Otherwise, move to next entry
inc bx ; Number of next entry
cmp bx, max_root_entries
最好写成repe cmpsb
,以更好地表明您正在比较是否相等!
在.check_entry中,您总是将21添加到DI
中。这是不正确的!如果 repe cmpsb
报告 NotEqual,则 DI
可能位于任何位置。您需要将 DI
放回 32 字节记录的开头,然后添加 32 以转到下一个根记录。
.check_entry:
mov cx, 0x0B ; Compare the first 11 bytes
push si di ; Push filename location to stack
repe cmpsb ; Compare the two strings
pop di si ; Restore filename location to SI
je .found_entry ; If equal, we found the root entry!
add di, 0x20 ; Otherwise, move to next entry
inc bx ; Number of next entry
cmp bx, max_root_entries
jg .missing ; If so, R is missing
jmp .check_entry ; Otherwise, look at this next entry
.found_entry:
mov si, success ; Success message*
call print_str ; Print!*
mov ax, [es:di+0x1A]
mov [cluster], ax
请注意更新您以后创建的根记录中的所有引用。
上面我已将 [es:di+0x0F]
更改为现在正确的形式 [es:di+0x1A]
。
关于assembly - 引导加载程序加载自身而不是内核,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56944933/
总的来说,我对 Linux 内核和操作系统非常感兴趣。我想知道的是,内核的文件类型或扩展名是什么?它显然没有 .exe 或 .out 扩展名,因为它们用于安装在操作系统上的应用程序。 内核只是一个二进
我需要为 Raspbian Linux 内核添加一个自己的系统调用。现在我在搜索了大约 2 天以找到解决方案后陷入困境。 要加一个系统调用,我基本上是按照大纲来的( http://elinux.org
对于一个学术项目,我希望将源文件 (myfile.c) 添加到 kernel/目录,与exit.c相同的目录和 fork.c .构建系统似乎不会自动获取新文件,因为我在 myfile.c 中定义的函数
浏览器排行榜 浏览器市占率排行榜全球榜 。 浏览器市占率排行榜中国榜 -快科技 。 如果按照浏览器内核来看, Chromium 内核的市场占有率无疑是最大的,一家独大
给定一个进程或线程的任务结构,迭代属于同一进程的所有其他线程的习惯用法是什么? 最佳答案 Linux 不区分进程(任务)和线程。库调用 fork() 和 pthread_create() 使用相同的系
我正在用c(不是linux。完全从头开始)从头开始制作一个内核,但我遇到了一些问题。我有这个代码: #include "timer.h" int ms = 0; void timer_handler(
我正在从头开始制作一个 C 内核,我实际上只是从网站上复制了这段代码,因为我的代码无法工作,所以我很困惑。 void kmain(void) { const char *str = "my f
我不确定,如果我完全理解上述差异,所以我想自己解释一下,你可以打断我,只要我有错:“内核是创建内核线程的初始代码段。内核线程是由内核管理的进程。用户线程是进程的一部分。如果你有一个单线程进程,那么整个
看一下struct file 定义from this code Linux 内核版本 2.6.18。 我正在尝试比较代码中的两个 struct file 变量,并确定它们是否指的是同一个文件。该结构中
我试图在 Linux 启动时使嵌入式设备中的 LED 闪烁。基本上,LED 闪烁表明 Linux 正在启动。为了使 LED 闪烁,我正在做以下事情 在 init/main.c 中创建了一个全局定时器(
我有一些在 FreeBSD 和 Linux 上运行的特定硬件。 我必须做一个用户空间应用程序,它将使用内核/用户空间应用程序之间的共享内存与驱动程序一起工作。我的应用程序对来自用户空间的共享内存进行忙
我在哪里可以找到 linux 内核中相应函数的解释,特别是对于 ICMPv4? 例如:icmp_reply、icmp_send等 感谢您的帮助。 最好的,阿里木 最佳答案 探索 Linux 内核中的
我在 Linux Kernel 3.4 上工作,我有以下代码: /* Proximity sensor calibration values */ unsigned int als_kadc;
我正在阅读“罗伯特·洛夫 (Robert Love) 撰写的 Linux 内核开发第 3 版”,以大致了解 Linux 内核的工作原理..(2.6.2.3) 我对等待队列的工作方式感到困惑,例如这段代
我之前也问过同样的问题,但是我的帖子不知为何被删除了。 无论如何,我正在尝试使用 C++ 并编写一个允许我直接访问内存并向其中写入内容的程序。我听说我需要对内核做一些事情,因为它是连接操作系统和应用程
在尝试了解 Ruby 执行方法时,我找到了这篇关于在 Ruby 中运行命令的五种方法的博文 http://mentalized.net/journal/2010/03/08/5_ways_to_run
是否有 Linux 发行版(Minix 除外)包含良好的源代码文档?或者,是否有一些好的文档来描述一般的 Linux 源代码? 我已经下载了内核源代码,但是(不出所料)我有点不知所措,我想知道是否有一
有谁知道 linux 中的哪个函数或文件包含查找用于 bind() 系统调用的随机端口的算法?我到处寻找,在 Linux 源代码中找不到包含此算法的方法。 谢谢! 最佳答案 这是一段又长又复杂的代码,
前言 首先,对于有科班背景的读者,可以跳过本系列文章。这些文章的主要目的是通过简单易懂的汇总,帮助非科班出身的读者理解底层知识,进一步了解为什么在面试中会涉及这些底层问题。否则,某些概念将始终
CentOS7.2与CentOS6区别及特点 Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化。内核初始化的
我是一名优秀的程序员,十分优秀!