China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 21:57
中国DOS联盟论坛 » GRUB4DOS、SYSLINUX及其它启动管理软件讨论专区 » Discussion on Obtaining Disk Parameters c/h/s View 2,527 Replies 1
Original Poster Posted 2009-01-09 17:05 ·  中国 山西 太原 联通
银牌会员
★★★
Credits 1,009
Posts 353
Joined 2003-09-04 00:00
22-year member
UID 9419
Gender Male
Status Offline
When the system starts, it needs to look for IO.SYS, NTLDR, GRLDR, etc. If the system doesn't support extended reading, it needs to obtain the number of disk heads and the number of sectors per track. There are four common methods.
1. Obtained using int13/ah=08.
CL (bits 7-6) + CH (bits 7-0) = number of cylinders, DH = number of heads, CL (bits 5-0) = number of sectors per track.
Note: The number of cylinders and heads start from 0, and the number of sectors starts from 1.
For a USB flash drive or img disk image recognized as a floppy disk by the BIOS, BL=10, but some BIOS have bugs and give incorrect parameters.
Since the maximum number of cylinders is 3FF, the maximum total number of bytes of the disk is 8Gb, and it cannot be represented if exceeded.
2. Obtained using int13/ah=48.
Offset 04 of DS:SI = number of cylinders, offset 08 = number of heads, offset 0C = number of sectors per track, offset 10 = total number of sectors of the disk.
Note: The number of cylinders, heads, and sectors start from 1.
The flag at offset 02 (bits 7-0) should be tested. If bit 1 is 0, the acquisition of c/h/s parameters is not supported, and the given c/h/s parameters may be incorrect, but the total number of sectors of the disk is still correct.
3. Obtained using the BPB parameter table of the boot sector DBR.
For FAT12, offset 1A = number of heads, offset 18 = number of sectors per track, offset 13 = total number of sectors of this partition.
For FAT16, offset 1A = number of heads, offset 18 = number of sectors per track, offset 20 = total number of sectors of this partition.
For FAT32, EXT2, EXT3, offset 1A = number of heads, offset 18 = number of sectors per track, offset 20 = total number of sectors of this partition.
For NTFS, offset 1A = number of heads, offset 18 = number of sectors per track, offset 28 = total number of sectors of this partition.
Note: The number of cylinders, heads, and sectors start from 1.
4. Obtained using the master boot record MBR.
Offset 1C3 = number of heads, offset 1C4 (bits 5-0) = number of sectors per track, offset 1CA = total number of sectors of the partition.
Note: The number of heads starts from 0, and the number of sectors starts from 1.
I think the 3rd and 4th methods are the most convenient and reliable. Because when looking for IO.SYS, NTLDR, GRLDR, etc., in addition to obtaining the h/s parameters, other parameters of the BPB parameter table need to be obtained, such as the number of sectors per cluster, the number of reserved sectors, the number of hidden sectors, the logical cluster number of $MFT (Master File Table), the node size, etc. Since MBR and BPB need to be used, why not obtain them together, instead of going the long way and making it more complicated? Maybe someone says that the h/s parameters may be damaged by viruses, but aren't the other parameters of MBR and BPB also likely to be damaged?
5. For the detection of c/h/s parameters, grldrstart.S uses a successive testing method, detecting the number of heads from 1 to FF, at least 1 time, at most 255 times, with an average of 128 times. If the bisection approximation method is used, only 8 or 9 times are needed.
The program code is as follows:

testing: pusha
push ds
push es
mov bx,5000h
mov es,bx
mov ds,bx
xor bp,bp
;Initialize test sector parameters
mov cx,20h ;Initial test value
mov bh,40h ;Test upper limit + 1
xor bl,bl ;Test lower limit
read: pusha
xor bx,bx ;Read to 5000:0000
cmp bp,0 ;Test sector number?
jne read_head ;No
;Test number of sectors per track
mov ax,0201h
xor ch,ch
xor dh,dh
int 13h
jmp read_end ;Success
read_head: ;Test maximum number of heads
mov dh,cl
mov ax,0201h
mov cx,1
int 13h
read_end: popa
jb failure
;Process successful test
mov ch,cl ;Save successful value
mov bl,cl ;Correct lower limit
mov cl,bh
;Calculate new test value
new: sub cl,bl
shr cl,1
add cl,bl
cmp cl,0
je err
cmp ch,cl ;Exit after testing
je done
jmp read
;Process failed test
failure: mov bh,cl ;Correct upper limit
jmp new
done: test bp,1 ;Finished testing maximum number of heads?
je done_sector ;No
inc ch
mov cs:,ch ;Save successfully tested number of heads
jmp test_end
done_sector:
mov cs:,ch ;Save successfully tested number of sectors per head
;Test maximum number of heads
or bp,1 ;Test maximum number of heads flag
;Set specifically for the bug of virtual machine vpc
;Read 1 sector 0-0-1 -> 5000:0000
xor bx,bx
mov ax,0201h
mov cx,1
xor dh,dh
int 13h
;Read 1 sector 0-2-1 -> 5000:0200
mov ax,0201h
mov dh,2
mov bx,0200h
int 13h
;Compare 5000:0000<>5000:0200
xor si,si
mov di,0200h
mov cx,80h
repz cmpsd
jne general ;Not equal, normal testing
mov byte ptr cs:,2
jmp test_end
general: ;Normal testing of maximum number of heads
;Initialize test head parameters
mov cx,7fh ;Initial test value
mov bh,0ffh ;Test upper limit + 1
xor bl,bl ;Test lower limit
jmp read
err: stc
test_end:
pop es
pop ds
popa
ret

Upload a detection program, please change rar to com.
Attachments
test.rar (706 bytes, Downloads: 23)
Floor 2 Posted 2009-01-11 00:29 ·  中国 福建 厦门 电信
初级用户
★★
Credits 156
Posts 76
Joined 2007-08-20 11:52
18-year member
UID 95399
Gender Male
Status Offline
Support it..
Too advanced, I can't understand. Usually I just use winimage to see ^_^
Forum Jump: