]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libdisk/disk.c
Add alignment hints for e512 ATA drives.
[plan9front.git] / sys / src / libdisk / disk.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <disk.h>
6
7 static Disk*
8 mkwidth(Disk *disk)
9 {
10         char buf[40];
11
12         sprint(buf, "%lld", disk->size);
13         disk->width = strlen(buf);
14         return disk;
15 }
16
17 /*
18  * Discover the disk geometry by various sleazeful means.
19  * 
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 
23  * right count.
24  * 
25  * If that fails, we'll try looking at the geometry that the ATA
26  * driver supplied, if any, and translate that as a
27  * BIOS might. 
28  * 
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
32  * the geometries.
33  */
34 typedef struct Table  Table;
35 typedef struct Tentry Tentry;
36 struct 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 */
47 };
48 enum {
49         Toffset         = 446,          /* offset of partition table in sector */
50         Magic0          = 0x55,
51         Magic1          = 0xAA,
52         NTentry         = 4,
53 };
54 struct Table {
55         Tentry  entry[NTentry];
56         uchar   magic[2];
57 };
58 static int
59 partitiongeometry(Disk *disk)
60 {
61         char *rawname;
62         int i, h, rawfd, s;
63         uchar buf[512];
64         Table *t;
65
66         t = (Table*)(buf + Toffset);
67
68         /*
69          * look for an MBR first in the /dev/sdXX/data partition, otherwise
70          * attempt to fall back on the current partition.
71          */
72         rawname = malloc(strlen(disk->prefix) + 5);     /* prefix + "data" + nul */
73         if(rawname == nil)
74                 return -1;
75
76         strcpy(rawname, disk->prefix);
77         strcat(rawname, "data");
78         rawfd = open(rawname, OREAD);
79         free(rawname);
80         if(rawfd >= 0
81         && seek(rawfd, 0, 0) >= 0
82         && readn(rawfd, buf, 512) == 512
83         && t->magic[0] == Magic0
84         && t->magic[1] == Magic1) {
85                 close(rawfd);
86         } else {
87                 if(rawfd >= 0)
88                         close(rawfd);
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) {
93                         return -1;
94                 }
95         }
96
97         h = s = -1;
98         for(i=0; i<NTentry; i++) {
99                 if(t->entry[i].type == 0)
100                         continue;
101
102                 t->entry[i].ends &= 63;
103                 if(h == -1) {
104                         h = t->entry[i].endh;
105                         s = t->entry[i].ends;
106                 } else {
107                         /*
108                          * Only accept the partition info if every
109                          * partition is consistent.
110                          */
111                         if(h != t->entry[i].endh || s != t->entry[i].ends)
112                                 return -1;
113                 }
114         }
115
116         if(h < 0 || s <= 0)
117                 return -1;
118
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;
123         return 0;
124 }
125
126 /*
127  * If there is ATA geometry, use it, perhaps massaged.
128  */
129 static int
130 drivergeometry(Disk *disk)
131 {
132         int m;
133
134         if(disk->c == 0 || disk->h == 0 || disk->s == 0)
135                 return -1;
136
137         disk->chssrc = Gdisk;
138         if(disk->c < 1024)
139                 return 0;
140
141         switch(disk->h) {
142         case 15:
143                 disk->h = 255;
144                 disk->c /= 17;
145                 return 0;
146
147         default:
148                 for(m = 2; m*disk->h < 256; m *= 2) {
149                         if(disk->c/m < 1024) {
150                                 disk->c /= m;
151                                 disk->h *= m;
152                                 return 0;
153                         }
154                 }
155
156                 /* set to 255, 63 and be done with it */
157                 disk->h = 255;
158                 disk->s = 63;
159                 disk->c = disk->secs / (disk->h * disk->s);
160                 return 0;
161         }
162 }
163
164 /*
165  * There's no ATA geometry and no partitions.
166  * Our guess is as good as anyone's.
167  */
168 static struct {
169         int h;
170         int s;
171 } guess[] = {
172         64, 32,
173         64, 63,
174         128, 63,
175         255, 63,
176 };
177 static int
178 guessgeometry(Disk *disk)
179 {
180         int i;
181         long c;
182
183         disk->chssrc = Gguess;
184         c = 1024;
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);
190                         return 0;
191                 }
192
193         /* use maximum values */
194         disk->h = 255;
195         disk->s = 63;
196         disk->c = disk->secs / (disk->h * disk->s);
197         return 0;
198 }
199
200 static void
201 findgeometry(Disk *disk)
202 {
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");
207                 assert(0);
208         }
209 }
210
211 static Disk*
212 openfile(Disk *disk)
213 {
214         Dir *d;
215
216         if((d = dirfstat(disk->fd)) == nil){
217                 free(disk);
218                 return nil;
219         }
220
221         disk->secsize = 512;
222         disk->size = d->length;
223         disk->secs = disk->size / disk->secsize;
224         disk->offset = 0;
225         free(d);
226
227         findgeometry(disk);
228         return mkwidth(disk);
229 }
230
231 static Disk*
232 opensd(Disk *disk)
233 {
234         Biobuf b;
235         char *p, *f[10];
236         int nf;
237
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);
244                         if(nf >= 6) {
245                                 disk->c = strtol(f[3], 0, 0);
246                                 disk->h = strtol(f[4], 0, 0);
247                                 disk->s = strtol(f[5], 0, 0);
248                         }
249                 }
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);
253                 }
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;
257                 }
258         }
259
260         if (!disk->psecsize) disk->psecsize = disk->secsize;    
261         disk->size = disk->secs * disk->secsize;
262         if(disk->size <= 0) {
263                 strcpy(disk->part, "");
264                 disk->type = Tfile;
265                 return openfile(disk);
266         }
267
268         findgeometry(disk);
269         return mkwidth(disk);
270 }
271
272 Disk*
273 opendisk(char *disk, int rdonly, int noctl)
274 {
275         char *p, *q;
276         Disk *d;
277
278         d = mallocz(sizeof(*d), 1);
279         if(d == nil)
280                 return nil;
281
282         d->fd = d->wfd = d->ctlfd = -1;
283         d->rdonly = rdonly;
284
285         d->fd = open(disk, OREAD);
286         if(d->fd < 0) {
287                 werrstr("cannot open disk file");
288                 free(d);
289                 return nil;
290         }
291
292         if(rdonly == 0) {
293                 d->wfd = open(disk, OWRITE);
294                 if(d->wfd < 0)
295                         d->rdonly = 1;
296         }
297
298         if(noctl)
299                 return openfile(d);
300
301         p = malloc(strlen(disk) + 4);   /* 4: slop for "ctl\0" */
302         if(p == nil) {
303                 close(d->wfd);
304                 close(d->fd);
305                 free(d);
306                 return nil;
307         }
308         strcpy(p, disk);
309
310         /* check for floppy(3) disk */
311         if(strlen(p) >= 7) {
312                 q = p+strlen(p)-7;
313                 if(q[0] == 'f' && q[1] == 'd' && isdigit(q[2]) && strcmp(q+3, "disk") == 0) {
314                         strcpy(q+3, "ctl");
315                         if((d->ctlfd = open(p, ORDWR)) >= 0) {
316                                 *q = '\0';
317                                 d->prefix = p;
318                                 d->type = Tfloppy;
319                                 return openfile(d);
320                         }
321                 }
322         }
323
324         /* attempt to find sd(3) disk or partition */
325         if(q = strrchr(p, '/'))
326                 q++;
327         else
328                 q = p;
329
330         strcpy(q, "ctl");
331         if((d->ctlfd = open(p, ORDWR)) >= 0) {
332                 *q = '\0';
333                 d->prefix = p;
334                 d->type = Tsd;
335                 d->part = strdup(disk+(q-p));
336                 if(d->part == nil){
337                         close(d->ctlfd);
338                         close(d->wfd);
339                         close(d->fd);
340                         free(p);
341                         free(d);
342                         return nil;
343                 }
344                 return opensd(d);
345         }
346
347         *q = '\0';
348         d->prefix = p;
349         /* assume we just have a normal file */
350         d->type = Tfile;
351         return openfile(d);
352 }