![如何根据 LBA 计算 CHS?](https://linux22.com/image/1609003/%E5%A6%82%E4%BD%95%E6%A0%B9%E6%8D%AE%20LBA%20%E8%AE%A1%E7%AE%97%20CHS%EF%BC%9F.png)
我正在编写引导扇区,并希望通过它访问软盘上的特定逻辑块地址。我拥有磁盘的所有参数,例如总扇区数、每磁道扇区数、磁头数等。我知道从 CHS 到 LBA 的公式,但我需要一个公式来根据给定的 LBA 地址计算柱面、磁头和扇区。
我居然在MikeOS的源代码中发现了这样一个函数:
l2hts: ; Calculate head, track and sector settings for int 13h
; IN: logical sector in AX, OUT: correct registers for int 13h
push bx
push ax
mov bx, ax ; Save logical sector
mov dx, 0 ; First the sector
div word [SectorsPerTrack]
add dl, 01h ; Physical sectors start at 1
mov cl, dl ; Sectors belong in CL for int 13h
mov ax, bx
mov dx, 0 ; Now calculate the head
div word [SectorsPerTrack]
mov dx, 0
div word [Sides]
mov dh, dl ; Head/side
mov ch, al ; Track
pop ax
pop bx
mov dl, byte [bootdev] ; Set correct device
ret
但我无法弄清楚这是如何工作的。以扇区为例,据我所知,此处扇区的公式是(logical sector % sectors per track) + 1
。这是正确的吗?出于某种原因,这对我来说没有多大意义。也许我只是不完全理解逻辑扇区是如何布局的。
答案1
我找到了答案:
Cylinder = LBA / (HPC * SPT)
Head = (LBA / SPT) % HPC
Sector = (LBA % SPT) + 1
LBA = logical block address
HPC = Heads Per Cylinder (reported by disk drive, typically 16 for 28-bit LBA)
SPT = Sectors Per Track (reported by disk drive, typically 63 for 28-bit LBA)
'%' is a modulus operation
'*' is a multiplication operation
'/' is a division operation