xref: /freebsd/stand/common/disk.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/disk.h>
32 #include <sys/queue.h>
33 #include <stand.h>
34 #include <stdarg.h>
35 #include <bootstrap.h>
36 #include <part.h>
37 
38 #include "disk.h"
39 
40 #ifdef DISK_DEBUG
41 # define DPRINTF(fmt, args...)	printf("%s: " fmt "\n" , __func__ , ## args)
42 #else
43 # define DPRINTF(fmt, args...)	((void)0)
44 #endif
45 
46 struct open_disk {
47 	struct ptable		*table;
48 	uint64_t		mediasize;
49 	uint64_t		entrysize;
50 	u_int			sectorsize;
51 };
52 
53 struct print_args {
54 	struct disk_devdesc	*dev;
55 	const char		*prefix;
56 	int			verbose;
57 };
58 
59 /* Convert size to a human-readable number. */
60 static char *
61 display_size(uint64_t size, u_int sectorsize)
62 {
63 	static char buf[80];
64 	char unit;
65 
66 	size = size * sectorsize / 1024;
67 	unit = 'K';
68 	if (size >= 10485760000LL) {
69 		size /= 1073741824;
70 		unit = 'T';
71 	} else if (size >= 10240000) {
72 		size /= 1048576;
73 		unit = 'G';
74 	} else if (size >= 10000) {
75 		size /= 1024;
76 		unit = 'M';
77 	}
78 	snprintf(buf, sizeof(buf), "%4ld%cB", (long)size, unit);
79 	return (buf);
80 }
81 
82 int
83 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
84 {
85 	struct disk_devdesc *dev;
86 	struct open_disk *od;
87 
88 	dev = (struct disk_devdesc *)d;
89 	od = (struct open_disk *)dev->dd.d_opendata;
90 
91 	/*
92 	 * The strategy function assumes the offset is in units of 512 byte
93 	 * sectors. For larger sector sizes, we need to adjust the offset to
94 	 * match the actual sector size.
95 	 */
96 	offset *= (od->sectorsize / 512);
97 	/*
98 	 * As the GPT backup partition is located at the end of the disk,
99 	 * to avoid reading past disk end, flag bcache not to use RA.
100 	 */
101 	return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
102 	    blocks * od->sectorsize, (char *)buf, NULL));
103 }
104 
105 static int
106 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
107 {
108 	struct disk_devdesc dev;
109 	struct print_args *pa, bsd;
110 	struct open_disk *od;
111 	struct ptable *table;
112 	char line[80];
113 	int res;
114 	u_int sectsize;
115 	uint64_t partsize;
116 
117 	pa = (struct print_args *)arg;
118 	od = (struct open_disk *)pa->dev->dd.d_opendata;
119 	sectsize = od->sectorsize;
120 	partsize = part->end - part->start + 1;
121 	snprintf(line, sizeof(line), "  %s%s: %s", pa->prefix, pname,
122 	    parttype2str(part->type));
123 	if (pager_output(line))
124 		return (1);
125 
126 	if (pa->verbose) {
127 		/* Emit extra tab when the line is shorter than 3 tab stops */
128 		if (strlen(line) < 24)
129 			(void) pager_output("\t");
130 
131 		snprintf(line, sizeof(line), "\t%s",
132 		    display_size(partsize, sectsize));
133 		if (pager_output(line))
134 			return (1);
135 	}
136 	if (pager_output("\n"))
137 		return (1);
138 
139 	res = 0;
140 	if (part->type == PART_FREEBSD) {
141 		/* Open slice with BSD label */
142 		dev.dd.d_dev = pa->dev->dd.d_dev;
143 		dev.dd.d_unit = pa->dev->dd.d_unit;
144 		dev.d_slice = part->index;
145 		dev.d_partition = D_PARTNONE;
146 		if (disk_open(&dev, partsize, sectsize) == 0) {
147 			table = ptable_open(&dev, partsize, sectsize, ptblread);
148 			if (table != NULL) {
149 				snprintf(line, sizeof(line), "  %s%s",
150 				    pa->prefix, pname);
151 				bsd.dev = pa->dev;
152 				bsd.prefix = line;
153 				bsd.verbose = pa->verbose;
154 				res = ptable_iterate(table, &bsd, ptable_print);
155 				ptable_close(table);
156 			}
157 			disk_close(&dev);
158 		}
159 	}
160 
161 	return (res);
162 }
163 
164 int
165 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
166 {
167 	struct open_disk *od;
168 	struct print_args pa;
169 
170 	/* Disk should be opened */
171 	od = (struct open_disk *)dev->dd.d_opendata;
172 	pa.dev = dev;
173 	pa.prefix = prefix;
174 	pa.verbose = verbose;
175 	return (ptable_iterate(od->table, &pa, ptable_print));
176 }
177 
178 int
179 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
180 {
181 	struct open_disk *od;
182 	int ret;
183 
184 	od = (struct open_disk *)dev->dd.d_opendata;
185 	ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
186 	    blocks * od->sectorsize, buf, NULL);
187 
188 	return (ret);
189 }
190 
191 int
192 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
193 {
194 	struct open_disk *od;
195 	int ret;
196 
197 	od = (struct open_disk *)dev->dd.d_opendata;
198 	ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
199 	    blocks * od->sectorsize, buf, NULL);
200 
201 	return (ret);
202 }
203 
204 int
205 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
206 {
207 	struct open_disk *od = dev->dd.d_opendata;
208 
209 	if (od == NULL)
210 		return (ENOTTY);
211 
212 	switch (cmd) {
213 	case DIOCGSECTORSIZE:
214 		*(u_int *)data = od->sectorsize;
215 		break;
216 	case DIOCGMEDIASIZE:
217 		if (dev->d_offset == 0)
218 			*(uint64_t *)data = od->mediasize;
219 		else
220 			*(uint64_t *)data = od->entrysize * od->sectorsize;
221 		break;
222 	default:
223 		return (ENOTTY);
224 	}
225 
226 	return (0);
227 }
228 
229 int
230 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
231 {
232 	struct disk_devdesc partdev;
233 	struct open_disk *od;
234 	struct ptable *table;
235 	struct ptable_entry part;
236 	int rc, slice, partition;
237 
238 	if (sectorsize == 0) {
239 		DPRINTF("unknown sector size");
240 		return (ENXIO);
241 	}
242 	rc = 0;
243 	od = (struct open_disk *)malloc(sizeof(struct open_disk));
244 	if (od == NULL) {
245 		DPRINTF("no memory");
246 		return (ENOMEM);
247 	}
248 	dev->dd.d_opendata = od;
249 	od->entrysize = 0;
250 	od->mediasize = mediasize;
251 	od->sectorsize = sectorsize;
252 	/*
253 	 * While we are reading disk metadata, make sure we do it relative
254 	 * to the start of the disk
255 	 */
256 	memcpy(&partdev, dev, sizeof(partdev));
257 	partdev.d_offset = 0;
258 	partdev.d_slice = D_SLICENONE;
259 	partdev.d_partition = D_PARTNONE;
260 
261 	dev->d_offset = 0;
262 	table = NULL;
263 	slice = dev->d_slice;
264 	partition = dev->d_partition;
265 
266 	DPRINTF("%s unit %d, slice %d, partition %d => %p", disk_fmtdev(dev),
267 	    dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
268 
269 	/* Determine disk layout. */
270 	od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
271 	    ptblread);
272 	if (od->table == NULL) {
273 		DPRINTF("Can't read partition table");
274 		rc = ENXIO;
275 		goto out;
276 	}
277 
278 	if (ptable_getsize(od->table, &mediasize) != 0) {
279 		rc = ENXIO;
280 		goto out;
281 	}
282 	od->mediasize = mediasize;
283 
284 	if (ptable_gettype(od->table) == PTABLE_BSD &&
285 	    partition >= 0) {
286 		/* It doesn't matter what value has d_slice */
287 		rc = ptable_getpart(od->table, &part, partition);
288 		if (rc == 0) {
289 			dev->d_offset = part.start;
290 			od->entrysize = part.end - part.start + 1;
291 		}
292 	} else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
293 		dev->d_offset = 0;
294 		od->entrysize = mediasize;
295 	} else if (slice >= 0) {
296 		/* Try to get information about partition */
297 		if (slice == 0)
298 			rc = ptable_getbestpart(od->table, &part);
299 		else
300 			rc = ptable_getpart(od->table, &part, slice);
301 		if (rc != 0) /* Partition doesn't exist */
302 			goto out;
303 		dev->d_offset = part.start;
304 		od->entrysize = part.end - part.start + 1;
305 		slice = part.index;
306 		if (ptable_gettype(od->table) == PTABLE_GPT) {
307 			partition = D_PARTISGPT;
308 			goto out; /* Nothing more to do */
309 		} else if (partition == D_PARTISGPT) {
310 			/*
311 			 * When we try to open GPT partition, but partition
312 			 * table isn't GPT, reset partition value to
313 			 * D_PARTWILD and try to autodetect appropriate value.
314 			 */
315 			partition = D_PARTWILD;
316 		}
317 
318 		/*
319 		 * If partition is D_PARTNONE, then disk_open() was called
320 		 * to open raw MBR slice.
321 		 */
322 		if (partition == D_PARTNONE)
323 			goto out;
324 
325 		/*
326 		 * If partition is D_PARTWILD and we are looking at a BSD slice,
327 		 * then try to read BSD label, otherwise return the
328 		 * whole MBR slice.
329 		 */
330 		if (partition == D_PARTWILD &&
331 		    part.type != PART_FREEBSD)
332 			goto out;
333 		/* Try to read BSD label */
334 		table = ptable_open(dev, part.end - part.start + 1,
335 		    od->sectorsize, ptblread);
336 		if (table == NULL) {
337 			DPRINTF("Can't read BSD label");
338 			rc = ENXIO;
339 			goto out;
340 		}
341 		/*
342 		 * If slice contains BSD label and partition < 0, then
343 		 * assume the 'a' partition. Otherwise just return the
344 		 * whole MBR slice, because it can contain ZFS.
345 		 */
346 		if (partition < 0) {
347 			if (ptable_gettype(table) != PTABLE_BSD)
348 				goto out;
349 			partition = 0;
350 		}
351 		rc = ptable_getpart(table, &part, partition);
352 		if (rc != 0)
353 			goto out;
354 		dev->d_offset += part.start;
355 		od->entrysize = part.end - part.start + 1;
356 	}
357 out:
358 	if (table != NULL)
359 		ptable_close(table);
360 
361 	if (rc != 0) {
362 		if (od->table != NULL)
363 			ptable_close(od->table);
364 		free(od);
365 		DPRINTF("%s could not open", disk_fmtdev(dev));
366 	} else {
367 		/* Save the slice and partition number to the dev */
368 		dev->d_slice = slice;
369 		dev->d_partition = partition;
370 		DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
371 		    (long long)dev->d_offset, od);
372 	}
373 	return (rc);
374 }
375 
376 int
377 disk_close(struct disk_devdesc *dev)
378 {
379 	struct open_disk *od;
380 
381 	od = (struct open_disk *)dev->dd.d_opendata;
382 	DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
383 	ptable_close(od->table);
384 	free(od);
385 	return (0);
386 }
387 
388 char*
389 disk_fmtdev(struct disk_devdesc *dev)
390 {
391 	static char buf[128];
392 	char *cp;
393 
394 	cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
395 	if (dev->d_slice > D_SLICENONE) {
396 #ifdef LOADER_GPT_SUPPORT
397 		if (dev->d_partition == D_PARTISGPT) {
398 			sprintf(cp, "p%d:", dev->d_slice);
399 			return (buf);
400 		} else
401 #endif
402 #ifdef LOADER_MBR_SUPPORT
403 			cp += sprintf(cp, "s%d", dev->d_slice);
404 #endif
405 	}
406 	if (dev->d_partition > D_PARTNONE)
407 		cp += sprintf(cp, "%c", dev->d_partition + 'a');
408 	strcat(cp, ":");
409 	return (buf);
410 }
411 
412 int
413 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
414 {
415 	int unit, slice, partition;
416 	const char *np;
417 	char *cp;
418 
419 	np = devspec;
420 	unit = -1;
421 	/*
422 	 * If there is path/file info after the device info, then any missing
423 	 * slice or partition info should be considered a request to search for
424 	 * an appropriate partition.  Otherwise we want to open the raw device
425 	 * itself and not try to fill in missing info by searching.
426 	 */
427 	if ((cp = strchr(np, ':')) != NULL && cp[1] != '\0') {
428 		slice = D_SLICEWILD;
429 		partition = D_PARTWILD;
430 	} else {
431 		slice = D_SLICENONE;
432 		partition = D_PARTNONE;
433 	}
434 
435 	if (*np != '\0' && *np != ':') {
436 		unit = strtol(np, &cp, 10);
437 		if (cp == np)
438 			return (EUNIT);
439 #ifdef LOADER_GPT_SUPPORT
440 		if (*cp == 'p') {
441 			np = cp + 1;
442 			slice = strtol(np, &cp, 10);
443 			if (np == cp)
444 				return (ESLICE);
445 			/* we don't support nested partitions on GPT */
446 			if (*cp != '\0' && *cp != ':')
447 				return (EINVAL);
448 			partition = D_PARTISGPT;
449 		} else
450 #endif
451 #ifdef LOADER_MBR_SUPPORT
452 		if (*cp == 's') {
453 			np = cp + 1;
454 			slice = strtol(np, &cp, 10);
455 			if (np == cp)
456 				return (ESLICE);
457 		}
458 #endif
459 		if (*cp != '\0' && *cp != ':') {
460 			partition = *cp - 'a';
461 			if (partition < 0)
462 				return (EPART);
463 			cp++;
464 		}
465 	} else
466 		return (EINVAL);
467 
468 	if (*cp != '\0' && *cp != ':')
469 		return (EINVAL);
470 	dev->dd.d_unit = unit;
471 	dev->d_slice = slice;
472 	dev->d_partition = partition;
473 	if (path != NULL)
474 		*path = (*cp == '\0') ? cp: cp + 1;
475 	return (0);
476 }
477