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-07-05 03:51
中国DOS联盟论坛 » DOS开发编程 & 发展交流 (开发室) » The original program to extend int13 to read the MBR (hard disk master boot record), and read the hard disk serial number through the port View 8,146 Replies 27
Floor 16 Posted 2006-07-02 05:30 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Originally posted by GOTOmsdos at 2006-6-30 20:41:
Also found a small oddity:
In the program for testing my small hard disk read and write,按理说 it should only be able to read and write 1023 (3FF, 1111111111) cylinders (0-1022)
But, it can read and write 1024 cylinders!

Puzzled beyond measure..

I also don't understand your intention. Since the int13h extended call no longer uses CHS parameters, how do you know you can read and write 1024 cylinders? (It should be the 1024th cylinder) Did you still convert it?
Floor 17 Posted 2006-07-02 17:47 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
Originally posted by GOTOmsdos at 2006-6-30 07:45 PM:
It worked!

Also:
I don't know QB, I don't know if QB can get the address at once?

In the code of qb45:
Can the MKI$ variable take out an address value from buffdat$?
Next...


My program is just a demonstration in principle, just wanting to realize the read and write of extended INT13, and has not done optimization or something. If you want to simplify, only change MKI$(d2%) to MKI$(SADD(buffdat$)). Actually, there is no great significance, but it does seem to have two fewer variables on the surface!

I think in programming, making hard disk utilities is very interesting and very valuable.

On a 486 computer, the BIOS is very old and does not support extended INT13, so large hard disks cannot be read and written, but it is very interesting. We can use IO ports on 486 computers to realize reading and writing of hard disks with a capacity of 137G or more.

Hope everyone can study each other!
Floor 18 Posted 2006-07-02 17:50 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
I don't know if everyone is interested in reading and writing to the hard drive via ports
Floor 19 Posted 2006-07-02 18:16 ·  中国 湖南 长沙 电信
中级用户
★★
Credits 282
Posts 126
Joined 2006-05-17 22:29
20-year member
UID 55724
Status Offline
Originally posted by qb45 at 2006-7-2 17:50:
I don't know if everyone is interested in port reading and writing to the hard disk



Extremely interested!

I've been looking for information on this for a long time
人类存在的目的就是试图理解人类为何存在
Floor 20 Posted 2006-07-03 00:58 ·  中国 广东 中山 电信
高级用户
★★★
Credits 972
Posts 420
Joined 2004-05-16 00:00
22-year member
UID 24467
Gender Male
Status Offline
I/O methods, it seems that CIH has used them
平生进退如飙风
Floor 21 Posted 2006-07-03 02:01 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Originally posted by DOSforever at 2006-7-2 05:30 AM:

I also don't understand your intention. Since the extended call of int13h no longer uses CHS parameters, how do you know that you can read and write 1024 cylinders? (It should be the 1024th cylinder) Did you also convert it?


Yeah, I've been researching hard disk reading and writing these days, and there are tests with conversion to the limit.
Floor 22 Posted 2006-07-04 00:23 ·  中国 上海 虹口区 电信
高级用户
★★
Credits 653
Posts 252
Joined 2006-04-16 19:48
20-year member
UID 53939
Status Offline
So this kind of technical specification must be looked at the official standards~~
Floor 23 Posted 2006-07-06 21:02 ·  中国 广东 中山 电信
高级用户
★★★
Credits 972
Posts 420
Joined 2004-05-16 00:00
22-year member
UID 24467
Gender Male
Status Offline
Floor 24 Posted 2006-07-09 10:56 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
Obtaining the Hard Drive Serial Number (Original Program)
This is an example of programming the hard drive using ports
This program must run under DOS and I have run it successfully under QB4.5 version

There are two IDE interfaces on the mainboard, and each interface is divided into master and slave, so 4 IDE devices can be connected. The port numbers of these 4 are different. This program uses ports 1F0 - 1F7, which are the master interface on the first IDE interface of the mainboard, and this is the most commonly used one. The others just have different port numbers, and the programming method and principle are the same.

'Program to obtain the hard drive serial number (not the logical disk volume label, some people say the volume label of drive C is the hard drive serial number)
'Code: QBASIC, running environment: DOS

OUT &H1F6, &HA0
OUT &h1F2,1
OUT &H1F3, 1
OUT &h1F4,1
OUT &h1F5,1
OUT &H1F7, &HEC 'Command to obtain hard drive information
DO WHILE flag <> &H58
f lag = INP(&H1F7)
if inkey$=chr$(27) then 'If the ESC key is pressed, terminate the program
print "Failed to obtain the hard drive serial number"
end
end if
LOOP
re$ = SPACE$(18)
FOR i% = 1 TO 18
READ a$
H$ = CHR$(VAL("&H" + a$))
MID$(re$, i%, 1) = H$
NEXT i%
duan% = varSEG(re$):offe% = SADD(re$)
DEF SEG = duan%
print "The serial number of this hard drive is ";
FOR i = 1 TO 16
CALL Absolute(r%, offe%) 'Call the embedded assembly machine code in QB
r1% = r% AND &HFF
r2% = (r% AND &HFF00) / &H100
IF i > 9 AND i < 15THEN PRINT CHR$(r1%); CHR$(r2%);
NEXT i
DEF SEG
END
'The data in this DATA is machine code, used to read port word data (the port statements in QB can only read and write ports byte by byte, not word by word)
DATA 55,89,e5,ba,f0,01,ed,86,e0,8b,5e,06,89,07,5d,ca,02 ,00
Floor 25 Posted 2006-07-13 20:40 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Floor 26 Posted 2008-10-27 19:13 ·  中国 江西 南昌 电信
新手上路
Credits 12
Posts 5
Joined 2007-09-11 16:10
18-year member
UID 97111
Gender Male
From 山西省太原市小店区
Status Offline
```c
//rdiskc.c
#include <stdio.h>
#include <math.h>
#include <alloc.h>
#include <string.h>
#ifndef NULL
#define NULL 0
#endif



typedef struct DiskAddressPacket{
char PacketSize;
char Reserved;
unsigned int BlockCount;
unsigned long BufferAddr;
int BlockNum[4];
}*DAP,DiskAddrPack;

extern void DREAD(DAP);

int main(int argc,char *argv[]){
int length,i=0,j=0,k=0,line=16;
unsigned char buffer[512];
DAP psi=(DAP)malloc(sizeof(DiskAddrPack));

if(argc==2&&!strcmp(argv[1],"/?")){
printf("\nbdexpen sector(0x,for 4 section) [line]\n\tline the line on show(must be 16 or 32)");
return 0;
}

if(argc!=5&&argc!=6)
return -1;

for(argv++,i=0;i<4;i++){
length=strlen(*argv);
if(length!=4)
return -2;

for(j=0;j<4;j++){
if((*argv)[j]>='0'&&(*argv)[j]<='9')
(*argv)[j]-='0';
else if((*argv)[j]>='a'&&(*argv)[j]<='f')
(*argv)[j]=(*argv)[j]-'a'+10;
else if((*argv)[j]>='A'&&(*argv)[j]<='F')
(*argv)[j]=(*argv)[j]-'A'+10;
else return -3;
}
psi->BlockNum[3-i]=(int)(*argv)[0]*16*16*16+(int)(*argv)[1]*16*16+(int)(*argv)[2]*16+(int)(*argv)[3];
argv++;
}



if(argc==6){
length=strlen(*argv);
for(j=0,k=0,line=0;j<length;){
if((*argv)[k]<48||(*argv)[k]>57)
return -4;
line+=((int)(*argv)[k++]-48)*(int)(pow(10,--length));
}
printf("%d\n",line);
if(line!=16&&line!=32)
line=16;
}


psi->PacketSize=16;
psi->Reserved=0;
psi->BlockCount=1;
psi->BufferAddr=(unsigned long)buffer;


DREAD(psi);

for(j=0;j<512/line;j++){
printf("%3x:",j*line);
for(k=0;k<line;k++){
printf("%4x",buffer[j*line+k]);
if(k==7||k==15&&line==32||k==23)
printf(" -");
}
printf("\t ");
for(k=0;k<line;k++){
if((buffer[j*line+k])<=128&&buffer[j*line+k]!='\n'&&buffer[j*line+k]!='\t')
printf("%c",buffer[j*line+k]);
else printf("\.");
}
printf("\n");
}
free(psi);
return 0;
}
//rdiska.asm
.model small
.data
.code

public _DREAD

_DREAD proc
push bp
mov bp,sp
push ds
push si

mov ah,42h
mov dl,80h
mov si,word ptr [bp+4]
int 13h

pop si
pop ds
pop bp
ret
_DREAD endp
end

After compiling and linking, it can be executed and can be called like a DOS command. However, the usage method is not written. Probably, you input the linear address of the sector you want to read, divided into 4 segments of 4 bits each in 16 - bit hexadecimal, separated by spaces. In addition, there is a switch to switch between displaying 16 lines/32 lines as the 5th parameter.
```
Floor 27 Posted 2008-10-29 16:04 ·  中国 福建 厦门 电信
高级用户
★★★
Credits 741
Posts 366
Joined 2007-07-25 19:11
18-year member
UID 94024
Gender Male
Status Offline
In the program I tested for reading and writing small hard drives, theoretically, it should only be able to read and write 1023 (3FF, 1111111111) cylinders (0-1022). However, it can read and write 1024 cylinders!

For those supporting INT13 extensions, as long as the final DAP address is the same, they are accessing the same place. CHS has little practical meaning to it. For those not supporting, it will be different. But the problem is that for USB flash drives, many BIOSes report supporting INT13E, but in fact, it's a false appearance (this is also the reason for the poor compatibility of USB flash drive booting). The culprit of this problem is the BIOS. Actually, hard drives conforming to the ATA standard truly use LBA addresses to access (it's not that the number of sectors in different cylinders of antique hard drives is truly different, and CHS is根本 meaningless).
On a 486 computer, the BIOS is very old and doesn't support extended INT13, so it can't read and write large hard drives. But interestingly, we can use I/O ports on a 486 computer to implement reading and writing of hard drives with a capacity of over 137G.

Impossible. The hard drive controller chip of a 486 at most provides LBA28 addresses. The LBA address required for LBA48 is 48 bits, so it can never access a hard drive with a capacity >137G.

[ Last edited by netwinxp on 2008-11-5 at 17:46 ]
Floor 28 Posted 2008-11-20 17:53 ·  中国 湖南 邵阳 电信
新手上路
Credits 11
Posts 6
Joined 2008-11-06 19:09
17-year member
UID 130201
Gender Male
Status Offline
Is there the simplest tool to modify the hard disk number?
Forum Jump: