12 sprint(buf, "%lld", disk->size);
13 disk->width = strlen(buf);
18 * Discover the disk geometry by various sleazeful means.
20 * First, if there is a partition table in sector 0,
21 * see if all the partitions have the same end head
22 * and sector; if so, we'll assume that that's the
25 * If that fails, we'll try looking at the geometry that the ATA
26 * driver supplied, if any, and translate that as a
29 * If that too fails, which should only happen on a SCSI
30 * disk with no currently defined partitions, we'll try
31 * various common (h, s) pairs used by BIOSes when faking
34 typedef struct Table Table;
35 typedef struct Tentry Tentry;
37 uchar active; /* active flag */
38 uchar starth; /* starting head */
39 uchar starts; /* starting sector */
40 uchar startc; /* starting cylinder */
41 uchar type; /* partition type */
42 uchar endh; /* ending head */
43 uchar ends; /* ending sector */
44 uchar endc; /* ending cylinder */
45 uchar xlba[4]; /* starting LBA from beginning of disc */
46 uchar xsize[4]; /* size in sectors */
49 Toffset = 446, /* offset of partition table in sector */
55 Tentry entry[NTentry];
59 partitiongeometry(Disk *disk)
66 t = (Table*)(buf + Toffset);
69 * look for an MBR first in the /dev/sdXX/data partition, otherwise
70 * attempt to fall back on the current partition.
72 rawname = malloc(strlen(disk->prefix) + 5); /* prefix + "data" + nul */
76 strcpy(rawname, disk->prefix);
77 strcat(rawname, "data");
78 rawfd = open(rawname, OREAD);
81 && seek(rawfd, 0, 0) >= 0
82 && readn(rawfd, buf, 512) == 512
83 && t->magic[0] == Magic0
84 && t->magic[1] == Magic1) {
89 if(seek(disk->fd, 0, 0) < 0
90 || readn(disk->fd, buf, 512) != 512
91 || t->magic[0] != Magic0
92 || t->magic[1] != Magic1) {
98 for(i=0; i<NTentry; i++) {
99 if(t->entry[i].type == 0)
102 t->entry[i].ends &= 63;
104 h = t->entry[i].endh;
105 s = t->entry[i].ends;
108 * Only accept the partition info if every
109 * partition is consistent.
111 if(h != t->entry[i].endh || s != t->entry[i].ends)
119 disk->h = h+1; /* heads count from 0 */
120 disk->s = s; /* sectors count from 1 */
121 disk->c = disk->secs / (disk->h*disk->s);
122 disk->chssrc = Gpart;
127 * If there is ATA geometry, use it, perhaps massaged.
130 drivergeometry(Disk *disk)
134 if(disk->c == 0 || disk->h == 0 || disk->s == 0)
137 disk->chssrc = Gdisk;
148 for(m = 2; m*disk->h < 256; m *= 2) {
149 if(disk->c/m < 1024) {
156 /* set to 255, 63 and be done with it */
159 disk->c = disk->secs / (disk->h * disk->s);
162 return -1; /* not reached */
166 * There's no ATA geometry and no partitions.
167 * Our guess is as good as anyone's.
179 guessgeometry(Disk *disk)
184 disk->chssrc = Gguess;
186 for(i=0; i<nelem(guess); i++)
187 if(c*guess[i].h*guess[i].s >= disk->secs) {
188 disk->h = guess[i].h;
189 disk->s = guess[i].s;
190 disk->c = disk->secs / (disk->h * disk->s);
194 /* use maximum values */
197 disk->c = disk->secs / (disk->h * disk->s);
202 findgeometry(Disk *disk)
204 if(partitiongeometry(disk) < 0
205 && drivergeometry(disk) < 0
206 && guessgeometry(disk) < 0) { /* can't happen */
207 print("we're completely confused about your disk; sorry\n");
217 if((d = dirfstat(disk->fd)) == nil){
223 disk->size = d->length;
224 disk->secs = disk->size / disk->secsize;
229 return mkwidth(disk);
239 Binit(&b, disk->ctlfd, OREAD);
240 while(p = Brdline(&b, '\n')) {
241 p[Blinelen(&b)-1] = '\0';
242 nf = tokenize(p, f, nelem(f));
243 if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
244 disk->secsize = strtoll(f[2], 0, 0);
246 disk->c = strtol(f[3], 0, 0);
247 disk->h = strtol(f[4], 0, 0);
248 disk->s = strtol(f[5], 0, 0);
251 if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
252 disk->offset = strtoll(f[2], 0, 0);
253 disk->secs = strtoll(f[3], 0, 0) - disk->offset;
258 disk->size = disk->secs * disk->secsize;
259 if(disk->size <= 0) {
260 strcpy(disk->part, "");
262 return openfile(disk);
266 return mkwidth(disk);
270 opendisk(char *disk, int rdonly, int noctl)
275 d = malloc(sizeof(*d));
279 d->fd = d->wfd = d->ctlfd = -1;
282 d->fd = open(disk, OREAD);
284 werrstr("cannot open disk file");
290 d->wfd = open(disk, OWRITE);
298 p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
307 /* check for floppy(3) disk */
310 if(q[0] == 'f' && q[1] == 'd' && isdigit((uchar)q[2]) && strcmp(q+3, "disk") == 0) {
312 if((d->ctlfd = open(p, ORDWR)) >= 0) {
321 /* attempt to find sd(3) disk or partition */
322 if(q = strrchr(p, '/'))
328 if((d->ctlfd = open(p, ORDWR)) >= 0) {
332 d->part = strdup(disk+(q-p));
346 /* assume we just have a normal file */