2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
30 #include <sys/queue.h>
33 #include <bootstrap.h>
40 # define DPRINTF(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
42 # define DPRINTF(fmt, args...) ((void)0)
53 struct disk_devdesc *dev;
58 /* Convert size to a human-readable number. */
60 display_size(uint64_t size, u_int sectorsize)
65 size = size * sectorsize / 1024;
67 if (size >= 10485760000LL) {
70 } else if (size >= 10240000) {
73 } else if (size >= 10000) {
77 snprintf(buf, sizeof(buf), "%4ld%cB", (long)size, unit);
82 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
84 struct disk_devdesc *dev;
87 dev = (struct disk_devdesc *)d;
88 od = (struct open_disk *)dev->dd.d_opendata;
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.
95 offset *= (od->sectorsize / 512);
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.
100 return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
101 blocks * od->sectorsize, (char *)buf, NULL));
105 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
107 struct disk_devdesc dev;
108 struct print_args *pa, bsd;
109 struct open_disk *od;
110 struct ptable *table;
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))
126 /* Emit extra tab when the line is shorter than 3 tab stops */
127 if (strlen(line) < 24)
128 (void) pager_output("\t");
130 snprintf(line, sizeof(line), "\t%s",
131 display_size(partsize, sectsize));
132 if (pager_output(line))
135 if (pager_output("\n"))
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);
148 snprintf(line, sizeof(line), " %s%s",
152 bsd.verbose = pa->verbose;
153 res = ptable_iterate(table, &bsd, ptable_print);
164 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
166 struct open_disk *od;
167 struct print_args pa;
169 /* Disk should be opened */
170 od = (struct open_disk *)dev->dd.d_opendata;
173 pa.verbose = verbose;
174 return (ptable_iterate(od->table, &pa, ptable_print));
178 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
180 struct open_disk *od;
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);
191 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
193 struct open_disk *od;
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);
204 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
206 struct open_disk *od = dev->dd.d_opendata;
212 case DIOCGSECTORSIZE:
213 *(u_int *)data = od->sectorsize;
216 if (dev->d_offset == 0)
217 *(uint64_t *)data = od->mediasize;
219 *(uint64_t *)data = od->entrysize * od->sectorsize;
229 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
231 struct disk_devdesc partdev;
232 struct open_disk *od;
233 struct ptable *table;
234 struct ptable_entry part;
235 int rc, slice, partition;
237 if (sectorsize == 0) {
238 DPRINTF("unknown sector size");
242 od = (struct open_disk *)malloc(sizeof(struct open_disk));
244 DPRINTF("no memory");
247 dev->dd.d_opendata = od;
249 od->mediasize = mediasize;
250 od->sectorsize = sectorsize;
252 * While we are reading disk metadata, make sure we do it relative
253 * to the start of the disk
255 memcpy(&partdev, dev, sizeof(partdev));
256 partdev.d_offset = 0;
257 partdev.d_slice = D_SLICENONE;
258 partdev.d_partition = D_PARTNONE;
262 slice = dev->d_slice;
263 partition = dev->d_partition;
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);
268 /* Determine disk layout. */
269 od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
271 if (od->table == NULL) {
272 DPRINTF("Can't read partition table");
277 if (ptable_getsize(od->table, &mediasize) != 0) {
281 od->mediasize = mediasize;
283 if (ptable_gettype(od->table) == PTABLE_BSD &&
285 /* It doesn't matter what value has d_slice */
286 rc = ptable_getpart(od->table, &part, partition);
288 dev->d_offset = part.start;
289 od->entrysize = part.end - part.start + 1;
291 } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
293 od->entrysize = mediasize;
294 } else if (slice >= 0) {
295 /* Try to get information about partition */
297 rc = ptable_getbestpart(od->table, &part);
299 rc = ptable_getpart(od->table, &part, slice);
300 if (rc != 0) /* Partition doesn't exist */
302 dev->d_offset = part.start;
303 od->entrysize = part.end - part.start + 1;
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) {
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.
314 partition = D_PARTWILD;
318 * If partition is D_PARTNONE, then disk_open() was called
319 * to open raw MBR slice.
321 if (partition == D_PARTNONE)
325 * If partition is D_PARTWILD and we are looking at a BSD slice,
326 * then try to read BSD label, otherwise return the
329 if (partition == D_PARTWILD &&
330 part.type != PART_FREEBSD)
332 /* Try to read BSD label */
333 table = ptable_open(dev, part.end - part.start + 1,
334 od->sectorsize, ptblread);
336 DPRINTF("Can't read BSD label");
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.
346 if (ptable_gettype(table) != PTABLE_BSD)
350 rc = ptable_getpart(table, &part, partition);
353 dev->d_offset += part.start;
354 od->entrysize = part.end - part.start + 1;
361 if (od->table != NULL)
362 ptable_close(od->table);
364 DPRINTF("%s could not open", disk_fmtdev(dev));
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);
376 disk_close(struct disk_devdesc *dev)
378 struct open_disk *od;
380 od = (struct open_disk *)dev->dd.d_opendata;
381 DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
382 ptable_close(od->table);
388 disk_fmtdev(struct devdesc *vdev)
390 struct disk_devdesc *dev = (struct disk_devdesc *)vdev;
391 static char buf[128];
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);
403 #ifdef LOADER_MBR_SUPPORT
404 cp += sprintf(cp, "s%d", dev->d_slice);
407 if (dev->d_partition > D_PARTNONE)
408 cp += sprintf(cp, "%c", dev->d_partition + 'a');
414 disk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
416 int unit, slice, partition;
419 struct disk_devdesc *dev;
421 np = devspec + 4; /* Skip the leading 'disk' */
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.
429 if ((cp = strchr(np, ':')) != NULL && cp[1] != '\0') {
431 partition = D_PARTWILD;
434 partition = D_PARTNONE;
437 if (*np != '\0' && *np != ':') {
438 unit = strtol(np, &cp, 10);
441 #ifdef LOADER_GPT_SUPPORT
444 slice = strtol(np, &cp, 10);
447 /* we don't support nested partitions on GPT */
448 if (*cp != '\0' && *cp != ':')
450 partition = D_PARTISGPT;
453 #ifdef LOADER_MBR_SUPPORT
456 slice = strtol(np, &cp, 10);
461 if (*cp != '\0' && *cp != ':') {
462 partition = *cp - 'a';
470 if (*cp != '\0' && *cp != ':')
472 dev = malloc(sizeof(*dev));
475 dev->dd.d_unit = unit;
476 dev->d_slice = slice;
477 dev->d_partition = partition;
480 *path = (*cp == '\0') ? cp: cp + 1;