xref: /freebsd/stand/common/disk.c (revision 731d06abf2105cc0873fa84e972178f9f37ca760)
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...)
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 	sprintf(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 	sprintf(line, "  %s%s: %s\t%s\n", pa->prefix, pname,
122 	    parttype2str(part->type),
123 	    pa->verbose ? display_size(partsize, sectsize) : "");
124 	if (pager_output(line))
125 		return 1;
126 	res = 0;
127 	if (part->type == PART_FREEBSD) {
128 		/* Open slice with BSD label */
129 		dev.dd.d_dev = pa->dev->dd.d_dev;
130 		dev.dd.d_unit = pa->dev->dd.d_unit;
131 		dev.d_slice = part->index;
132 		dev.d_partition = D_PARTNONE;
133 		if (disk_open(&dev, partsize, sectsize) == 0) {
134 			table = ptable_open(&dev, partsize, sectsize, ptblread);
135 			if (table != NULL) {
136 				sprintf(line, "  %s%s", pa->prefix, pname);
137 				bsd.dev = pa->dev;
138 				bsd.prefix = line;
139 				bsd.verbose = pa->verbose;
140 				res = ptable_iterate(table, &bsd, ptable_print);
141 				ptable_close(table);
142 			}
143 			disk_close(&dev);
144 		}
145 	}
146 
147 	return (res);
148 }
149 
150 int
151 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
152 {
153 	struct open_disk *od;
154 	struct print_args pa;
155 
156 	/* Disk should be opened */
157 	od = (struct open_disk *)dev->dd.d_opendata;
158 	pa.dev = dev;
159 	pa.prefix = prefix;
160 	pa.verbose = verbose;
161 	return (ptable_iterate(od->table, &pa, ptable_print));
162 }
163 
164 int
165 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
166 {
167 	struct open_disk *od;
168 	int ret;
169 
170 	od = (struct open_disk *)dev->dd.d_opendata;
171 	ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
172 	    blocks * od->sectorsize, buf, NULL);
173 
174 	return (ret);
175 }
176 
177 int
178 disk_write(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_WRITE, dev->d_offset + offset,
185 	    blocks * od->sectorsize, buf, NULL);
186 
187 	return (ret);
188 }
189 
190 int
191 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
192 {
193 	struct open_disk *od = dev->dd.d_opendata;
194 
195 	if (od == NULL)
196 		return (ENOTTY);
197 
198 	switch (cmd) {
199 	case DIOCGSECTORSIZE:
200 		*(u_int *)data = od->sectorsize;
201 		break;
202 	case DIOCGMEDIASIZE:
203 		if (dev->d_offset == 0)
204 			*(uint64_t *)data = od->mediasize;
205 		else
206 			*(uint64_t *)data = od->entrysize * od->sectorsize;
207 		break;
208 	default:
209 		return (ENOTTY);
210 	}
211 
212 	return (0);
213 }
214 
215 int
216 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
217 {
218 	struct disk_devdesc partdev;
219 	struct open_disk *od;
220 	struct ptable *table;
221 	struct ptable_entry part;
222 	int rc, slice, partition;
223 
224 	rc = 0;
225 	od = (struct open_disk *)malloc(sizeof(struct open_disk));
226 	if (od == NULL) {
227 		DPRINTF("no memory");
228 		return (ENOMEM);
229 	}
230 	dev->dd.d_opendata = od;
231 	od->entrysize = 0;
232 	od->mediasize = mediasize;
233 	od->sectorsize = sectorsize;
234 	/*
235 	 * While we are reading disk metadata, make sure we do it relative
236 	 * to the start of the disk
237 	 */
238 	memcpy(&partdev, dev, sizeof(partdev));
239 	partdev.d_offset = 0;
240 	partdev.d_slice = D_SLICENONE;
241 	partdev.d_partition = D_PARTNONE;
242 
243 	dev->d_offset = 0;
244 	table = NULL;
245 	slice = dev->d_slice;
246 	partition = dev->d_partition;
247 
248 	DPRINTF("%s unit %d, slice %d, partition %d => %p",
249 	    disk_fmtdev(dev), dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
250 
251 	/* Determine disk layout. */
252 	od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
253 	    ptblread);
254 	if (od->table == NULL) {
255 		DPRINTF("Can't read partition table");
256 		rc = ENXIO;
257 		goto out;
258 	}
259 
260 	if (ptable_getsize(od->table, &mediasize) != 0) {
261 		rc = ENXIO;
262 		goto out;
263 	}
264 	od->mediasize = mediasize;
265 
266 	if (ptable_gettype(od->table) == PTABLE_BSD &&
267 	    partition >= 0) {
268 		/* It doesn't matter what value has d_slice */
269 		rc = ptable_getpart(od->table, &part, partition);
270 		if (rc == 0) {
271 			dev->d_offset = part.start;
272 			od->entrysize = part.end - part.start + 1;
273 		}
274 	} else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
275 		dev->d_offset = 0;
276 		od->entrysize = mediasize;
277 	} else if (slice >= 0) {
278 		/* Try to get information about partition */
279 		if (slice == 0)
280 			rc = ptable_getbestpart(od->table, &part);
281 		else
282 			rc = ptable_getpart(od->table, &part, slice);
283 		if (rc != 0) /* Partition doesn't exist */
284 			goto out;
285 		dev->d_offset = part.start;
286 		od->entrysize = part.end - part.start + 1;
287 		slice = part.index;
288 		if (ptable_gettype(od->table) == PTABLE_GPT) {
289 			partition = 255;
290 			goto out; /* Nothing more to do */
291 		} else if (partition == 255) {
292 			/*
293 			 * When we try to open GPT partition, but partition
294 			 * table isn't GPT, reset d_partition value to -1
295 			 * and try to autodetect appropriate value.
296 			 */
297 			partition = -1;
298 		}
299 		/*
300 		 * If d_partition < 0 and we are looking at a BSD slice,
301 		 * then try to read BSD label, otherwise return the
302 		 * whole MBR slice.
303 		 */
304 		if (partition == -1 &&
305 		    part.type != PART_FREEBSD)
306 			goto out;
307 		/* Try to read BSD label */
308 		table = ptable_open(dev, part.end - part.start + 1,
309 		    od->sectorsize, ptblread);
310 		if (table == NULL) {
311 			DPRINTF("Can't read BSD label");
312 			rc = ENXIO;
313 			goto out;
314 		}
315 		/*
316 		 * If slice contains BSD label and d_partition < 0, then
317 		 * assume the 'a' partition. Otherwise just return the
318 		 * whole MBR slice, because it can contain ZFS.
319 		 */
320 		if (partition < 0) {
321 			if (ptable_gettype(table) != PTABLE_BSD)
322 				goto out;
323 			partition = 0;
324 		}
325 		rc = ptable_getpart(table, &part, partition);
326 		if (rc != 0)
327 			goto out;
328 		dev->d_offset += part.start;
329 		od->entrysize = part.end - part.start + 1;
330 	}
331 out:
332 	if (table != NULL)
333 		ptable_close(table);
334 
335 	if (rc != 0) {
336 		if (od->table != NULL)
337 			ptable_close(od->table);
338 		free(od);
339 		DPRINTF("%s could not open", disk_fmtdev(dev));
340 	} else {
341 		/* Save the slice and partition number to the dev */
342 		dev->d_slice = slice;
343 		dev->d_partition = partition;
344 		DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
345 		    (long long)dev->d_offset, od);
346 	}
347 	return (rc);
348 }
349 
350 int
351 disk_close(struct disk_devdesc *dev)
352 {
353 	struct open_disk *od;
354 
355 	od = (struct open_disk *)dev->dd.d_opendata;
356 	DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
357 	ptable_close(od->table);
358 	free(od);
359 	return (0);
360 }
361 
362 char*
363 disk_fmtdev(struct disk_devdesc *dev)
364 {
365 	static char buf[128];
366 	char *cp;
367 
368 	cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
369 	if (dev->d_slice > D_SLICENONE) {
370 #ifdef LOADER_GPT_SUPPORT
371 		if (dev->d_partition == D_PARTISGPT) {
372 			sprintf(cp, "p%d:", dev->d_slice);
373 			return (buf);
374 		} else
375 #endif
376 #ifdef LOADER_MBR_SUPPORT
377 			cp += sprintf(cp, "s%d", dev->d_slice);
378 #endif
379 	}
380 	if (dev->d_partition > D_PARTNONE)
381 		cp += sprintf(cp, "%c", dev->d_partition + 'a');
382 	strcat(cp, ":");
383 	return (buf);
384 }
385 
386 int
387 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
388 {
389 	int unit, slice, partition;
390 	const char *np;
391 	char *cp;
392 
393 	np = devspec;
394 	unit = -1;
395 	slice = D_SLICEWILD;
396 	partition = D_PARTWILD;
397 	if (*np != '\0' && *np != ':') {
398 		unit = strtol(np, &cp, 10);
399 		if (cp == np)
400 			return (EUNIT);
401 #ifdef LOADER_GPT_SUPPORT
402 		if (*cp == 'p') {
403 			np = cp + 1;
404 			slice = strtol(np, &cp, 10);
405 			if (np == cp)
406 				return (ESLICE);
407 			/* we don't support nested partitions on GPT */
408 			if (*cp != '\0' && *cp != ':')
409 				return (EINVAL);
410 			partition = 255;
411 		} else
412 #endif
413 #ifdef LOADER_MBR_SUPPORT
414 		if (*cp == 's') {
415 			np = cp + 1;
416 			slice = strtol(np, &cp, 10);
417 			if (np == cp)
418 				return (ESLICE);
419 		}
420 #endif
421 		if (*cp != '\0' && *cp != ':') {
422 			partition = *cp - 'a';
423 			if (partition < 0)
424 				return (EPART);
425 			cp++;
426 		}
427 	} else
428 		return (EINVAL);
429 
430 	if (*cp != '\0' && *cp != ':')
431 		return (EINVAL);
432 	dev->dd.d_unit = unit;
433 	dev->d_slice = slice;
434 	dev->d_partition = partition;
435 	if (path != NULL)
436 		*path = (*cp == '\0') ? cp: cp + 1;
437 	return (0);
438 }
439