step1:安装Bochs
Bochs is a highly portable open source IA-32 (x86) PC emulator written
in C++, that runs on most popular platforms. It includes emulation of
the Intel x86 CPU, common I/O devices, and a custom BIOS. Bochs can be compiled to emulate many different x86 CPUs, from early 386 to the most recent x86-64 Intel and AMD processors which may even not reached the market yet.
简而言之,这是一个可以供我们调试的虚拟机(南京大学的NEMU的高级版)。
https://www.cnblogs.com/cmoses/p/10329042.html
启动过程可以参考这篇博客,写的很详细了。
需要注意的一点就是路径里不可以有中文。
step2:读取磁盘
ATA 接口的官方文档 https://wiki.osdev.org/ATA_PIO_Mode
An ATA bus typically has ten I/O ports which control its behavior. For
the primary bus, these I/O ports are usually 0x1F0 (the “I/O” port
base) through 0x1F7 and 0x3F6 (the “Control” port base) through 0x3F7.
一个ATA总线一般有十个端口,其中0x1F0 到 0x1F7 是I/O端口
0x3F6到0x3F7是控制端口
上图是8个控制端口的说明,在现在我们只需要向+2号端口送入要读的扇区数,向+3,4,5,6号送入要读的LBA28就可以了
要注意在+6号端口的第四位是LBA28的高四位,DRV用于选择主盘(1)或者从盘(0)。第六位LBA设置为1,表示使用LBA模式读取
输入读取的地址之后,就要再输入读取的命令
这时候就要看Status寄存器的字段意义了。+7号寄存器在读的时候是状态Status,写的时候是Command。这里我们写入0x20就代表开始读数据了
开始读数据时,BSY会设置为1,代表现在硬盘忙。等到硬盘准备好数据了,会把DRQ设置为1。
我们只要阻塞循环检测DRQ的位置就行了。
最后等DRQ=1时,循环读256个2Byte数据(从0x1F0端口读,这个端口一定是16位的)。