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 02:46
中国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,145 Replies 27
Original Poster Posted 2004-08-09 00:00 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
The basic BIOS Int 13H call is a disk basic input/output interrupt call provided by BIOS. It can complete functions such as disk (including hard disk and floppy disk) reset, read/write, verification, positioning, diagnosis, formatting, etc.
It uses the CHS addressing method, so it can only access a hard disk of about 8 GB at most.
The purpose of extending the Int13H interface is to expand the functions of BIOS, so that it can support hard disks with more than 1024 cylinders, as well as functions such as locking, unlocking and ejecting of removable media.
DAP is based on the absolute sector address, so using DAP, Int13H can easily surpass the limit of 1024 cylinders because it doesn't need the concept of CHS at all.
I debugged successfully under win98 and dos71 with qb45
'In qbasic, to call the interrupt, 1 must add the L command, such as QB/L 2. Must reference qb.bi', as follows

'$INCLUDE: 'qb.bi'
DIM SHARED ax, bx, cx, dx, bp, si, di, ds, flags, es '// Global sharing
BUFFdat$ = STRING$(512, 0) '// Create a read/write sector buffer (512 bytes)
d1% = varSEG(buffdat$) '// Get the segment address of the buffer
d2% = SADD(buffdat$) '// Get the offset address of the buffer
'// Write data such as segment address, offset address, read/write command, etc. into the data packet (DAP) as required
dat1$ = CHR$(&H10) + CHR$(0) + MKI$(1) + MKI$(d2%) + MKI$(d1%) + MKL$(0) + MKL$(0)

ds = varSEG(dat1$) '// DS = segment address of the data packet (DAP)
si = SADD(dat1$) '// SI = offset address of the data packet (DAP)
ax = &H4200 '// AX = call the read sector command of extended INT13
dx = &H80 '// DX = the first hard disk
xint &H13 '// Call INT13 interrupt

'//------Save the content of the read MBR sector to the TESTHD.DAT file on drive D------
OPEN "d:\testhd.dat" FOR BINARY AS #1
PUT 1,1,BUFFDAT$
CLOSE #1
'//----------------------------------------------------------------------
END

'//Subroutine for interrupt call
SUB xint (num%)
DIM inregs AS RegTypeX
DIM outregs AS RegTypeX
inregs.ax = ax
inregs.bx = bx
inregs.cx = cx
inregs.dx = dx
inregs.si = si
inregs.di = di
inregs.ds = ds
inregs.es = es
CALL INTERRUPTX(num%, inregs, outregs)
ax = outregs.ax
bx = outregs.bx
cx = outregs.cx
dx = outregs.dx
si = outregs.si
di = outregs.di
ds = outregs.ds
es = outregs.es
END SUB


[ Last edited by qb45 on 2006-7-9 at 11:14 ]
Floor 2 Posted 2004-08-23 00:00 ·  中国 辽宁 朝阳 联通
铂金会员
★★★★
痴迷DOS者
Credits 5,798
Posts 1,924
Joined 2003-06-20 00:00
23-year member
UID 5583
Gender Male
From 金獅電腦軟體工作室
Status Offline
熟能生巧,巧能生精,一艺不精,终生无成,精亦求精,始有所成,臻于完美,永无止境!
金狮電腦軟體工作室愿竭诚为您服务!
QQ群:8393170(定期清理不发言者)
个人网站:http://www.520269.cn
电子邮件:doujiehui@vip.qq.com
微信公众号: doujiehui
Floor 3 Posted 2005-05-30 00:00 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Excuse me, everyone, why can't I use the extended int13h when I directly fill in the DAP parameters of the sector to be read in debug? For example, to read the content of the first physical sector of the first hard disk to the address 3333:3333, should the DAP data packet be as follows:

10 00 01 00 33 33 33 33 00 00 00 00 00 00 00 00

debug
-a
mmmm:nnnn mov ah,42
mmmm:nnnn mov dl,80
mmmm:nnnn mov di,2000
mmmm:nnnn int13
mmmm:nnnn int3
mmmm:nnnn
-e2000 10 00 01 00 33 33 33 33 00 00 00 00 00 00 00 00
-g

After execution, an error is returned under DOS7.10, and even if I check the memory address 3333:3333, there is no data to be read. It crashes in the DOS window of Win98, and a warm boot doesn't work.
Floor 4 Posted 2005-06-10 00:00 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
The structure of the Disk Address Packet (DAP) is as follows: (DAP is based on absolute sector addresses and can easily exceed the limit of 1024 cylinders)

struct DiskAddressPacket
BYTE PacketSize; // Packet size:
// (Fixed value, always equal to 16, that is, 10H, refers to the storage space occupied by this structure)
BYTE Reserved; // ==0
WORD BlockCount; // Number of data blocks to be transferred (in sectors)
DWORD BufferAddr; // Transfer buffer address (segment:offset)
QWORD BlockNum; // Absolute starting block address of the disk

In the extended Int13H call, the following register conventions are generally used:
ds:si ==> Disk address packet (disk address packet)
dl ==> Drive number
ah ==> Function code / return code

Please take a close look at the call specification of the extended INT13, and you will find that
ds:si ==> Disk address packet
而你的程序中是
mmmm:nnnn mov di,2000

You have made SI into DI! If you know a little Qbasic, you can find this mistake from my program!
Floor 5 Posted 2005-06-30 00:00 ·  中国 上海 黄浦区 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Thanks to the previous answer, according to your method, it can indeed be successfully read!
But the information I initially obtained does write DI, and it is mentioned more than once in this article. The following is the relevant information I found. Almost all the detailed explanations of extended int13h on the Internet are derived from this same article. Later, I re-read it according to the error you pointed out, and I found that in the "3. Interface Specification" section, it does write "ds:SI == disk address packet". But in the subsequent "3. API Details", all the entry parameters are written as "ds:DI = disk address packet". I don't know if it's a typo by the author or my misunderstanding?
http://www.kaijia.net/info_Print.asp?ArticleID=542


[ Last edited by dosforever on 2005-7-22 at 12:57 ]
Floor 6 Posted 2005-07-05 00:00 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
I have the same experience as you. At that time, I needed to use QBASIC language to make a disk editor. Because the old INT13 had limitations, so I searched everywhere for materials. I also posted a thread in this forum asking where I could find such materials. As a result, a netizen gave me a foreigner's interrupt大全 website, and the materials were very complete. Unfortunately, I couldn't understand English. Later, I found Chinese materials in Luo Yunbin's assembly language. I then referred to this material to experiment with reading a sector in QBASIC, but it didn't succeed. Later, I experimented many times, got stuck many times, and my hair turned white many times. At that time, I really thought QBASIC didn't have this ability. Then I thought again, all are programming languages, QBASIC should be able to do it, there's nothing that can't be done. When there was no operating system at that time, QBASIC was also used as an operating system. Didn't Bill Gates say: Give me BASIC, and there's nothing that can't be done! Then I asked experts everywhere. Fortunately, I met a netizen on QQ who was proficient in programming and English. I asked him to read the English for me. Haha, it turned out that the Chinese material was wrong! I felt that the person who translated this material deliberately made mistakes. He might think that as long as you are fond of programming and willing to study, you can find the mistakes. In fact, there are not only this mistake in this material, the most important mistake is this one, and then there is another mistake. When you read the first 63 sectors, there is no problem, but as long as you read and write more than 63 sectors, you will find that what is read is not what you want! There are a total of 3 mistakes in this material! You can look at the English materials. I don't remember the specific website, but you can still find the help thread I posted in this forum. There are netizens who gave this website. I also mentioned the technical materials download area in the "Data Recovery Forum", and the mistakes in the INT13 Chinese materials. They made some changes, but not very well. But in the programming discussion area of the forum, there are experts who wrote threads pointing out these 3 mistakes.
Floor 7 Posted 2005-08-31 16:08 ·  中国 湖北 天门 联通
中级用户
★★
绝不写垃圾帖
Credits 322
Posts 99
Joined 2004-07-03 00:00
22-year member
UID 27774
Gender Male
From 湖北
Status Offline
I was also tricked by that material. The program has been having problems and I don't know where they are. It turns out it's the fault of SI and DI. It seems that it's better to not fully rely on books.
从95年开始用DOS3.2,96年在Windows3.1里认识了鼠标,97年开始用Win95,98年装过NetWare,99年迷过Linux,现在用WinXP、WinME和DOS7.1。一回首,从盲到忙,从忙到茫。
Floor 8 Posted 2005-08-31 16:46 ·  中国 湖北 宜昌 电信
高级用户
★★
Credits 677
Posts 194
Joined 2003-09-13 00:00
22-year member
UID 9778
Gender Male
Status Offline
Yeah, in many programming materials, there are some very small but crucial errors. Sometimes the author can't verify it. Now the internet is popular with you copying me and me copying you, and finally everyone has the same flavor!

Fortunately, I only know qbasic, so the troubles are relatively less. When I encounter problems I can't solve, I can just say: QBASIC may not have this ability

[ Last edited by qb45 on 2005-8-31 at 17:01 ]
Floor 9 Posted 2006-06-30 18:21 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Haven't learned QB, try it, seems to understand,
Now just give it a try, come back later...
Floor 10 Posted 2006-06-30 19:45 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
It worked!

Another:
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$?
The following code is divided into two times:
d1% = varSEG(buffdat$)
d2% = SADD(buffdat$)
Because, after buffdat is created, its address is fixed.
There is no need to decompose its address, right?
Floor 11 Posted 2006-06-30 20:36 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Calling the extended 13h interrupt in C language to read and write large hard drives is solved!

The main code is as follows: (tested correctly)

#include <dos.h>
#include <stdio.h>
void main(){
unsigned char buf[51200];
FILE *f;
union REGS in,out;
struct DiskAddressPacket
{
unsigned char PacketSize; /* Packet size (16 bytes) */
unsigned char Reserved; /* ==0 */
unsigned int BlockCount; /* Number of data blocks to transfer (in sectors) */
unsigned long BufferAddr;/* Transfer buffer address (segment:offset) */
unsigned long BlockNum;/* Starting absolute block address of the disk */
/*unsigned long ab; */
}dap={16,0,100,0,16434495};
dap.BufferAddr=(unsigned long)buf;

in.h.ah = 0x42;
in.h.dl = 0x80;
in.x.si = &dap;
/* Disk Address Packet (Disk Address Packet) */

int86(0x13,&in,&out);

f=fopen("int13ext.dat","wb+");
fwrite(buf,512,100,f);
fclose(f);
}
Floor 12 Posted 2006-06-30 20:41 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Also found a small oddity:
In the program for testing my read/write small hard disk,按理说it should only be able to read and write 1023 (3FF, 1111111111) cylinders (0-1022)
But, it can read and write 1024 cylinders!
Puzzled!
Anyone who knows, please speak up...
Floor 13 Posted 2006-06-30 20:49 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
TO DOSforever:
In DEBUG, you first need to load the DAP, right?
How did you load it?
Floor 14 Posted 2006-06-30 22:07 ·  中国 上海 电信
金牌会员
★★★★
Credits 4,639
Posts 2,239
Joined 2005-01-30 00:00
21-year member
UID 35785
Gender Male
Status Offline
Hehe he he……
It was , didn't I write above how it was ? Pay attention to the middle

-e2000 10 00 01 00 33 33 33 33 00 00 00 00 00 00 00 00

That line. That is, manually fill in a specific parameter to test the extended call of int13h.

Originally, the forum where I posted this was DVBBS, and after switching to DISCUZ!, there was some deformation in typesetting. What should be line-wrapped is not line-wrapped, all crowded together.
Floor 15 Posted 2006-07-01 00:55 ·  中国 北京 联通
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
22-year member
UID 7105
Gender Male
Status Offline
Oh, I didn't pay much attention just now,
Now, I know, I'll try it later...
Thanks.
Forum Jump: