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);
165 * There's no ATA geometry and no partitions.
166 * Our guess is as good as anyone's.
178 guessgeometry(Disk *disk)
183 disk->chssrc = Gguess;
185 for(i=0; i<nelem(guess); i++)
186 if(c*guess[i].h*guess[i].s >= disk->secs) {
187 disk->h = guess[i].h;
188 disk->s = guess[i].s;
189 disk->c = disk->secs / (disk->h * disk->s);
193 /* use maximum values */
196 disk->c = disk->secs / (disk->h * disk->s);
201 findgeometry(Disk *disk)
203 if(partitiongeometry(disk) < 0
204 && drivergeometry(disk) < 0
205 && guessgeometry(disk) < 0) { /* can't happen */
206 print("we're completely confused about your disk; sorry\n");
216 if((d = dirfstat(disk->fd)) == nil){
222 disk->size = d->length;
223 disk->secs = disk->size / disk->secsize;
228 return mkwidth(disk);
238 Binit(&b, disk->ctlfd, OREAD);
239 while(p = Brdline(&b, '\n')) {
240 p[Blinelen(&b)-1] = '\0';
241 nf = tokenize(p, f, nelem(f));
242 if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
243 disk->secsize = strtoll(f[2], 0, 0);
245 disk->c = strtol(f[3], 0, 0);
246 disk->h = strtol(f[4], 0, 0);
247 disk->s = strtol(f[5], 0, 0);
250 if(nf >= 3 && strcmp(f[0], "alignment") == 0) {
251 disk->psecsize = strtol(f[1], 0, 0);
252 disk->physalign = strtol(f[2], 0, 0);
254 if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
255 disk->offset = strtoll(f[2], 0, 0);
256 disk->secs = strtoll(f[3], 0, 0) - disk->offset;
260 if (!disk->psecsize) disk->psecsize = disk->secsize;
261 disk->size = disk->secs * disk->secsize;
262 if(disk->size <= 0) {
263 strcpy(disk->part, "");
265 return openfile(disk);
269 return mkwidth(disk);
273 opendisk(char *disk, int rdonly, int noctl)
278 d = mallocz(sizeof(*d), 1);
282 d->fd = d->wfd = d->ctlfd = -1;
285 d->fd = open(disk, OREAD);
287 werrstr("cannot open disk file");
293 d->wfd = open(disk, OWRITE);
301 p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
310 /* check for floppy(3) disk */
313 if(q[0] == 'f' && q[1] == 'd' && isdigit(q[2]) && strcmp(q+3, "disk") == 0) {
315 if((d->ctlfd = open(p, ORDWR)) >= 0) {
324 /* attempt to find sd(3) disk or partition */
325 if(q = strrchr(p, '/'))
331 if((d->ctlfd = open(p, ORDWR)) >= 0) {
335 d->part = strdup(disk+(q-p));
349 /* assume we just have a normal file */