1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
.TH DISKS 2
.SH NAME
disks: Disk, PCpart, readn, chstext \- generic disk and partition interface
.SH SYNOPSIS
.EX
include "disks.m";
disks := load Disks Disks->PATH;
Disk: adt {
prefix: string; # prefix before partition name
part: string; # partition name (nil if not partition)
fd: ref Sys->FD;
wfd: ref Sys->FD;
ctlfd: ref Sys->FD;
rdonly: int; # non-zero if readonly
dtype: string; # device type
secs: big; # number of sectors in device or partition
secsize: int; # device's sector size
size: big; # size of device or partition
offset: big; # within larger disk, perhaps
width: int; # of disk size in bytes as decimal string
c: int; # geometry: cyl, head, sectors
h: int;
s: int;
chssrc: string; # source of c/h/s values
open: fn(f: string, mode: int, noctl: int): ref Disk;
};
PCpart: adt {
active: int; # Active or 0
ptype: int;
base: big; # base block address
offset: big; # block offset from base to partition
size: big; # in sectors
extract: fn(a: array of byte, d: ref Disk): PCpart;
bytes: fn(p: self PCpart, d: ref Disk): array of byte;
};
init: fn();
readn: fn(fd: ref Sys->FD, buf: array of byte, n: int): int;
chstext: fn(p: array of byte): string;
.EE
.SH DESCRIPTION
.B Disks
provides a simple way to gather
and use information about
.IR floppy (3)
and
.IR sd (3)
disks and disk partitions,
as well as plain files.
.PP
.B Init
must be called before invoking any other operations of the module
.PP
.B Disk.open
opens
.I file
and returns a reference to a
.B Disk
value to represent the disk.
.I Mode
should be either
.BR Sys->OREAD
or
.BR Sys->ORDWR
to establish the open mode.
.B Open
always opens
.I file
for reading and stores that file descriptor in
the element
.IR fd .
If the mode is not
.BR Sys->OREAD ,
.I opendisk
also opens
.I file
for writing and stores that file descriptor in
.BR wfd .
The two file descriptors are kept separate to
help prevent accidents.
If
.I noctl
is not set,
.B open
looks for a
.B ctl
file in the same directory as the
disk file;
if it finds one, it declares
the disk to be
an
.IR sd (3)
device,
setting
.B dtype
to
\f5"sd"\fP.
If the passed
.I file
is named
.BI fd n disk \fR,
it looks for a file
.BI fd n ctl \fR,
and if it finds that,
declares the disk to be
a floppy disk, of type
\f5"floppy"\fP.
If either control
file is found, it is opened for reading
and writing, and the resulting file descriptor
is saved as
.BR ctlfd .
Otherwise the returned disk
has type
\f5"file"\fP.
.PP
.B Open
then stores the file's length
(as given by
.IR sys-stat (2))
in
.BR size .
If the disk is an
.IR sd (3)
partition,
.B open
reads the sector size from the control
file and stores it in
.BR secsize ;
otherwise the sector size is assumed to be 512,
as is the case for floppy disks.
.B Open
stores the disk size measured in sectors in
.BR secs .
.PP
If the disk is an
.IR sd (3)
partition,
.B open
parses the
control
file to find the partition's offset
within its disk;
otherwise it sets
.B offset
to zero.
If the disk is an ATA disk,
.B open
reads
the disk geometry (number of cylinders, heads, and sectors)
from the
.B geometry
line in the
.I sd
control file;
otherwise it sets these to zero as well.
.B Name
is initialized with the base name of
the disk partition, and is useful for forming messages to the
.I sd
control file.
.B Prefix
is set to the original
.I file
name without the
.B name
suffix.
.PP
The IBM PC BIOS interface allocates
10 bits for the number of cylinders, 8 for
the number of heads, and 6 for the number of sectors per track.
Disk geometries are not quite so simple
anymore, but to keep the interface useful,
modern disks and BIOSes present geometries
that still fit within these constraints.
These numbers are still used when partitioning
and formatting disks.
.B Open
employs a number of heuristics to discover this
supposed geometry and store it in the
.BR c ,
.BR h ,
and
.B s
elements of
.BR Disk .
Disk offsets in partition tables and
in FAT descriptors are stored in a form
dependent upon these numbers, so
.I opendisk
works hard to report numbers that
agree with those used by other operating
systems; the numbers bear little or no resemblance
to reality.
.PP
.B Chssrc
names the source of the geometry values:
.B disk
(values returned by disk itself);
.B part
(values stored in PC partition table);
or
.B guess
(calculated by module's heuristics).
.PP
.B Readn
attempts to read exactly
.I n
bytes from file
.I fd
into
.IR buf ,
using as many
.IR sys-read (2)
calls as required.
It helps insulate a program from any peculiar underlying IO boundaries
of a device.
It returns less than
.I n
only if end-of-file is reached.
It returns -1 if the first read fails.
.PP
The PC BIOS and operating systems support an arcane system of disk partitions:
a partition table at the end of the master boot record
defines up to four partitions.
One (or perhaps more) of those can be an extended partition
that heads a chain (or perhaps roots a tree) of partition tables
elsewhere on disk, allowing many more than four partitions in all.
.B Disks
represents a partition table entry by a value of type
.BR PCpart .
It provides the following operations and values:
.TP
.B active
Has the value
.B Disks->Active
if it is bootable, and zero otherwise.
.TP
.B ptype
Partition type;
.B Disks->Type9
is used by Plan 9 and Inferno (see
.IR prep (8)).
.TP
.B base
Address of the extended partition that started the chain (or rooted the tree) containing this partition.
Zero for primary partitions defined by the master boot record.
.TP
.B offset
Block address of the start of the partition relative to the
.BR base .
.TP
.B size
Size of the partition in sectors.
.TP
.BI extract( "a, d" )
Extracts the relevant data from an array of bytes
.I a
containing a PC-format partition table entry on
.B Disk
.IR d ,
and returns a
.B PCpart
value that represents the partition.
.TP
.IB pc .bytes( d )
Return an array of bytes containing the PC-format partition
table entry corresponding to
.IR pc .
It will always be
.B TentrySize
bytes long.
.PP
Several other values are defined here for convenience:
.TP
Active
Value for
.B PCpart.active
if the partition is bootable.
.TP
.B Type9
Partition type used by Plan 9 and Inferno.
.TP
.B Toffset
Offset (in bytes) of the partition table in a master boot record or extended partition start sector.
.TP
.B TentrySize
Size in bytes of a partition table entry.
.TP
.B NTentry
Number of table entries.
.TP
.B Magic0
.PD0
.TP
.B Magic1
Each sector containing a partition table should end with
these two bytes (a master boot record must end with them).
.PP
.B Chstext
takes a 3-byte array containing the packed cylinder/head/sector
representation of a disk address and returns the corresponding text
in the form
.BI c / h / s.
.SH SOURCE
.B /appl/lib/disks.b
.SH SEE ALSO
.IR scsiio (2),
.IR floppy (3),
.IR sd (3),
.IR prep (3)
|