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