1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ctype.h>
5 #include <disk.h>
6 
7 static Disk*
mkwidth(Disk * 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
partitiongeometry(Disk * disk)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 == -1)
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
drivergeometry(Disk * disk)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 
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  * There's no ATA geometry and no partitions.
165  * Our guess is as good as anyone's.
166  */
167 static struct {
168 	int h;
169 	int s;
170 } guess[] = {
171 	64, 32,
172 	64, 63,
173 	128, 63,
174 	255, 63,
175 };
176 static int
guessgeometry(Disk * disk)177 guessgeometry(Disk *disk)
178 {
179 	int i;
180 	long c;
181 
182 	disk->chssrc = Gguess;
183 	c = 1024;
184 	for(i=0; i<nelem(guess); i++)
185 		if(c*guess[i].h*guess[i].s >= disk->secs) {
186 			disk->h = guess[i].h;
187 			disk->s = guess[i].s;
188 			disk->c = disk->secs / (disk->h * disk->s);
189 			return 0;
190 		}
191 
192 	/* use maximum values */
193 	disk->h = 255;
194 	disk->s = 63;
195 	disk->c = disk->secs / (disk->h * disk->s);
196 	return 0;
197 }
198 
199 static void
findgeometry(Disk * disk)200 findgeometry(Disk *disk)
201 {
202 	if(partitiongeometry(disk) < 0
203 	&& drivergeometry(disk) < 0
204 	&& guessgeometry(disk) < 0) {	/* can't happen */
205 		print("we're completely confused about your disk; sorry\n");
206 		assert(0);
207 	}
208 }
209 
210 static Disk*
openfile(Disk * disk)211 openfile(Disk *disk)
212 {
213 	Dir *d;
214 
215 	if((d = dirfstat(disk->fd)) == nil){
216 		free(disk);
217 		return nil;
218 	}
219 
220 	disk->secsize = 512;
221 	disk->size = d->length;
222 	disk->secs = disk->size / disk->secsize;
223 	disk->offset = 0;
224 	free(d);
225 
226 	findgeometry(disk);
227 	return mkwidth(disk);
228 }
229 
230 static Disk*
opensd(Disk * disk)231 opensd(Disk *disk)
232 {
233 	Biobuf b;
234 	char *p, *f[10];
235 	int nf;
236 
237 	Binit(&b, disk->ctlfd, OREAD);
238 	while(p = Brdline(&b, '\n')) {
239 		p[Blinelen(&b)-1] = '\0';
240 		nf = tokenize(p, f, nelem(f));
241 		if(nf >= 3 && strcmp(f[0], "geometry") == 0) {
242 			disk->secsize = strtoll(f[2], 0, 0);
243 			if(nf >= 6) {
244 				disk->c = strtol(f[3], 0, 0);
245 				disk->h = strtol(f[4], 0, 0);
246 				disk->s = strtol(f[5], 0, 0);
247 			}
248 		}
249 		if(nf >= 4 && strcmp(f[0], "part") == 0 && strcmp(f[1], disk->part) == 0) {
250 			disk->offset = strtoll(f[2], 0, 0);
251 			disk->secs = strtoll(f[3], 0, 0) - disk->offset;
252 		}
253 	}
254 
255 
256 	disk->size = disk->secs * disk->secsize;
257 	if(disk->size <= 0) {
258 		strcpy(disk->part, "");
259 		disk->type = Tfile;
260 		return openfile(disk);
261 	}
262 
263 	findgeometry(disk);
264 	return mkwidth(disk);
265 }
266 
267 Disk*
opendisk(char * disk,int rdonly,int noctl)268 opendisk(char *disk, int rdonly, int noctl)
269 {
270 	char *p, *q;
271 	Disk *d;
272 
273 	d = malloc(sizeof(*d));
274 	if(d == nil)
275 		return nil;
276 
277 	d->fd = d->wfd = d->ctlfd = -1;
278 	d->rdonly = rdonly;
279 
280 	d->fd = open(disk, OREAD);
281 	if(d->fd < 0) {
282 		werrstr("cannot open disk file");
283 		free(d);
284 		return nil;
285 	}
286 
287 	if(rdonly == 0) {
288 		d->wfd = open(disk, OWRITE);
289 		if(d->wfd < 0)
290 			d->rdonly = 1;
291 	}
292 
293 	if(noctl)
294 		return openfile(d);
295 
296 	p = malloc(strlen(disk) + 4);	/* 4: slop for "ctl\0" */
297 	if(p == nil) {
298 		close(d->wfd);
299 		close(d->fd);
300 		free(d);
301 		return nil;
302 	}
303 	strcpy(p, disk);
304 
305 	/* check for floppy(3) disk */
306 	if(strlen(p) >= 7) {
307 		q = p+strlen(p)-7;
308 		if(q[0] == 'f' && q[1] == 'd' && isdigit((uchar)q[2]) && strcmp(q+3, "disk") == 0) {
309 			strcpy(q+3, "ctl");
310 			if((d->ctlfd = open(p, ORDWR)) >= 0) {
311 				*q = '\0';
312 				d->prefix = p;
313 				d->type = Tfloppy;
314 				return openfile(d);
315 			}
316 		}
317 	}
318 
319 	/* attempt to find sd(3) disk or partition */
320 	if(q = strrchr(p, '/'))
321 		q++;
322 	else
323 		q = p;
324 
325 	strcpy(q, "ctl");
326 	if((d->ctlfd = open(p, ORDWR)) >= 0) {
327 		*q = '\0';
328 		d->prefix = p;
329 		d->type = Tsd;
330 		d->part = strdup(disk+(q-p));
331 		if(d->part == nil){
332 			close(d->ctlfd);
333 			close(d->wfd);
334 			close(d->fd);
335 			free(p);
336 			free(d);
337 			return nil;
338 		}
339 		return opensd(d);
340 	}
341 
342 	*q = '\0';
343 	d->prefix = p;
344 	/* assume we just have a normal file */
345 	d->type = Tfile;
346 	return openfile(d);
347 }
348