有没有办法用 int 13h 读取放在 head = 1 上的内核?

Is there any way to read the kernel placed on head = 1 with int 13h?

当我按下回车键时,我的引导加载程序必须进入内核,但它没有反应。 Bootloader位于第一个磁道的第一个扇区,head - 0,但内核的地址是:sector 3,track -0,head 1。

我什至把内核放在了第一个磁道的第 5 个扇区。我已经有从软盘读取和写入的功能,试图从我的内核所在的头 1 读取,并且它读取正确。但是当我放入bootloader相应的磁头、扇区和磁道时,在读取int 13h/ah=02h时它什么也没做。

BootLoader

readingProcess:
        xor AX, AX     
    mov DS, AX      
    mov BX, 0060h   
    cli           
    mov ss,bx      
    mov sp,ax    
    sti           
    cld

    mov AH, 02h   
    mov AL, 25          
    mov CH, 0           
    mov CL, 3           
    mov DL, 0           
    mov DH, 1               

    mov BX, 0060h
    mov ES, BX
    xor BX, BX

    int 13h

    ;go to kernel
    jmp 0060h:0000h
ret

所以,现在我真的不明白它是如何工作的,因为当我尝试写入软盘时,磁头 1,它应该几乎在我的 .img 文件的中间,但它出现在文件的开头。我认为一定有一个公式可以帮助我找到正确的轨道和扇区。

查看您的代码后,看来这完全是使用 获取磁盘映像文件中给定偏移量的磁道(柱面)、磁头和扇区值的问题。您想为 Int 13h/ah=2(磁盘读取)使用正确的值。

根据我之前的回答,我给出的公式为:

CHS tuples can be mapped to LBA address with the following formula:

LBA = (C × HPC + H) × SPT + (S - 1)

where C, H and S are the cylinder number, the head number, and the sector number

LBA is the logical block address
HPC is the maximum number of heads per cylinder (reported by 
    disk drive, typically 16 for 28-bit LBA)
SPT is the maximum number of sectors per track (reported by
    disk drive, typically 63 for 28-bit LBA)
LBA addresses can be mapped to CHS tuples with the following formula 
    ("mod" is the modulo operation, i.e. the remainder, and "÷" is 
    integer division, i.e. the quotient of the division where any 
    fractional part is discarded):

    C = LBA ÷ (HPC × SPT)
    H = (LBA ÷ SPT) mod HPC
    S = (LBA mod SPT) + 1

有关驱动器寻址的更多信息,请参见维基百科 article

一张1.44MiB的软盘共有2880个扇区,HPC(Heads per cylinder)为2,SPT(Sectors Per Track为18),每个扇区为512字节。您需要的是 LBA。 LBA 只是您写入内核的偏移量(以字节为单位)除以 512。在您的 Appender 程序中,您将内核副本放置在偏移量 738304 处。LBA=738304/512=1442.

您只需将 LBA 1442 转换为 CHS 并将这些数字插入代码中。

 Formula:
    C = LBA ÷ (HPC × SPT)
    H = (LBA ÷ SPT) mod HPC
    S = (LBA mod SPT) + 1

 We know:
    LBA = 1442
    HPC = 2
    SPT = 18        

进行替换我们得到:

C = 1442 ÷ (2 × 18)
H = (1442 ÷ 18) mod 2
S = (1442 mod 18) + 1

÷表示整数除法,舍去余数(分数)。 mod表示做除法保留余数,把整部分丢掉。

C = 1442 ÷ (2 × 18) = 40.0555556 = 40
H = (1442 ÷ 18) mod 2 = 80 mod 2 = 0 (80 mod 2 = 40 remainder 0)
S = (1442 mod 18) + 1 = (2) + 1 = 3 (1442 mod 18 = 80 remainder 2)

我们现在知道 LBA 1442 的 Cylinder(Track) = 40,Head = 0,Sector = 3。您的代码将在 Int 13h/AH=2 BIOS 调用中使用这些值。你会在 DH 中放置 40,在 CH 中放置 0,在 CL.[=25 中放置 3 =]

您似乎还在磁盘映像中的偏移量 2048 处放置了内核副本。偏移量 2048 是 4 (2048/512) 的 LBA 使用上面的公式和值你会得到 Cylinder(Track) = 0, Head = 0, Sector = 5。你将 0 放在 DHCH 中的 0 个和 CL 中的 5 个。

特别说明:只有扇区号从1开始。柱面(磁道)和磁头从0开始。


如果给定柱面(磁道)、磁头和扇区,并且需要知道磁盘上对应的 LBA,则公式为:

LBA = (C × HPC + H) × SPT + (S − 1) 

如果您有 (40, 0, 3) 的 CHS 和 1.44MiB 驱动器几何结构(HPC=2 和 SPT=18),则 LBA=(40*2+0)*1​​8+(3-1)= 1442. 1442*512=738304 将是该扇区的磁盘偏移量。

如果您有 (0, 0, 5) 的 CHS 和 1.44MiB 驱动器几何结构(HPC=2 和 SPT=18),则 LBA=(0*2+0)*1​​8+(5-1)= 4. 4*512=2048 将是该扇区的磁盘偏移量。

这 2 个计算是本答案第一部分计算结果的倒数。