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