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 if(disk->c == 0 || disk->h == 0 || disk->s == 0)
69 t = (Table*)(buf + Toffset);
72 * look for an MBR first in the /dev/sdXX/data partition, otherwise
73 * attempt to fall back on the current partition.
75 rawname = malloc(strlen(disk->prefix) + 5); /* prefix + "data" + nul */
79 strcpy(rawname, disk->prefix);
80 strcat(rawname, "data");
81 rawfd = open(rawname, OREAD);
84 && seek(rawfd, 0, 0) >= 0
85 && readn(rawfd, buf, 512) == 512
86 && t->magic[0] == Magic0
87 && t->magic[1] == Magic1) {
92 if(seek(disk->fd, 0, 0) < 0
93 || readn(disk->fd, buf, 512) != 512
94 || t->magic[0] != Magic0
95 || t->magic[1] != Magic1) {
101 for(i=0; i<NTentry; i++) {
102 if(t->entry[i].type == 0)
105 t->entry[i].ends &= 63;
107 h = t->entry[i].endh;
108 s = t->entry[i].ends;
111 * Only accept the partition info if every
112 * partition is consistent.
114 if(h != t->entry[i].endh || s != t->entry[i].ends)
122 disk->h = h+1; /* heads count from 0 */
123 disk->s = s; /* sectors count from 1 */
124 disk->c = disk->secs / (disk->h*disk->s);
125 disk->chssrc = Gpart;
130 * If there is ATA geometry, use it, perhaps massaged.
133 drivergeometry(Disk *disk)
137 if(disk->c == 0 || disk->h == 0 || disk->s == 0)
140 disk->chssrc = Gdisk;
151 for(m = 2; m*disk->h < 256; m *= 2) {
152 if(disk->c/m < 1024) {
159 /* set to 255, 63 and be done with it */
162 disk->c = disk->secs / (disk->h * disk->s);
168 * There's no ATA geometry and no partitions.
169 * Our guess is as good as anyone's.
181 guessgeometry(Disk *disk)
186 disk->chssrc = Gguess;
188 for(i=0; i<nelem(guess); i++)
189 if(c*guess[i].h*guess[i].s >= disk->secs) {
190 disk->h = guess[i].h;
191 disk->s = guess[i].s;
192 disk->c = disk->secs / (disk->h * disk->s);
196 /* use maximum values */
199 disk->c = disk->secs / (disk->h * disk->s);
204 findgeometry(Disk *disk)
206 if(partitiongeometry(disk) < 0
207 && drivergeometry(disk) < 0
208 && guessgeometry(disk) < 0) { /* can't happen */
209 print("we're completely confused about your disk; sorry\n");
219 if((d = dirfstat(disk->fd)) == nil){
225 disk->size = d->length;
226 disk->secs = disk->size / disk->secsize;
231 return mkwidth(disk);
241 Binit(&b, disk->ctlfd, OREAD);
242 while(p = Brdline(&b, '\n')) {
243 p[Blinelen(&b)-1] = '\0';
244 nf = tokenize(p, f, nelem(f));
245 if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
246 disk->secsize = strtoll(f[2], 0, 0);
248 disk->c = strtol(f[3], 0, 0);
249 disk->h = strtol(f[4], 0, 0);
250 disk->s = strtol(f[5], 0, 0);
253 if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
254 disk->offset = strtoll(f[2], 0, 0);
255 disk->secs = strtoll(f[3], 0, 0) - disk->offset;
260 disk->size = disk->secs * disk->secsize;
261 if(disk->size <= 0) {
262 strcpy(disk->part, "");
264 return openfile(disk);
268 return mkwidth(disk);
272 opendisk(char *disk, int rdonly, int noctl)
277 d = mallocz(sizeof(*d), 1);
281 d->fd = d->wfd = d->ctlfd = -1;
284 d->fd = open(disk, OREAD);
286 werrstr("cannot open disk file");
292 d->wfd = open(disk, OWRITE);
300 p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
309 /* check for floppy(3) disk */
312 if(q[0] == 'f' && q[1] == 'd' && isdigit(q[2]) && strcmp(q+3, "disk") == 0) {
314 if((d->ctlfd = open(p, ORDWR)) >= 0) {
323 /* attempt to find sd(3) disk or partition */
324 if(q = strrchr(p, '/'))
330 if((d->ctlfd = open(p, ORDWR)) >= 0) {
334 d->part = strdup(disk+(q-p));
348 /* assume we just have a normal file */