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>
29 __FBSDID("$FreeBSD$");
32 #include <sys/queue.h>
35 #include <bootstrap.h>
42 # define DPRINTF(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
44 # define DPRINTF(fmt, args...) ((void)0)
55 struct disk_devdesc *dev;
60 /* Convert size to a human-readable number. */
62 display_size(uint64_t size, u_int sectorsize)
67 size = size * sectorsize / 1024;
69 if (size >= 10485760000LL) {
72 } else if (size >= 10240000) {
75 } else if (size >= 10000) {
79 snprintf(buf, sizeof(buf), "%4ld%cB", (long)size, unit);
84 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
86 struct disk_devdesc *dev;
89 dev = (struct disk_devdesc *)d;
90 od = (struct open_disk *)dev->dd.d_opendata;
93 * The strategy function assumes the offset is in units of 512 byte
94 * sectors. For larger sector sizes, we need to adjust the offset to
95 * match the actual sector size.
97 offset *= (od->sectorsize / 512);
99 * As the GPT backup partition is located at the end of the disk,
100 * to avoid reading past disk end, flag bcache not to use RA.
102 return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
103 blocks * od->sectorsize, (char *)buf, NULL));
107 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
109 struct disk_devdesc dev;
110 struct print_args *pa, bsd;
111 struct open_disk *od;
112 struct ptable *table;
118 pa = (struct print_args *)arg;
119 od = (struct open_disk *)pa->dev->dd.d_opendata;
120 sectsize = od->sectorsize;
121 partsize = part->end - part->start + 1;
122 snprintf(line, sizeof(line), " %s%s: %s", pa->prefix, pname,
123 parttype2str(part->type));
124 if (pager_output(line))
128 /* Emit extra tab when the line is shorter than 3 tab stops */
129 if (strlen(line) < 24)
130 (void) pager_output("\t");
132 snprintf(line, sizeof(line), "\t%s",
133 display_size(partsize, sectsize));
134 if (pager_output(line))
137 if (pager_output("\n"))
141 if (part->type == PART_FREEBSD) {
142 /* Open slice with BSD label */
143 dev.dd.d_dev = pa->dev->dd.d_dev;
144 dev.dd.d_unit = pa->dev->dd.d_unit;
145 dev.d_slice = part->index;
146 dev.d_partition = D_PARTNONE;
147 if (disk_open(&dev, partsize, sectsize) == 0) {
148 table = ptable_open(&dev, partsize, sectsize, ptblread);
150 snprintf(line, sizeof(line), " %s%s",
154 bsd.verbose = pa->verbose;
155 res = ptable_iterate(table, &bsd, ptable_print);
166 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
168 struct open_disk *od;
169 struct print_args pa;
171 /* Disk should be opened */
172 od = (struct open_disk *)dev->dd.d_opendata;
175 pa.verbose = verbose;
176 return (ptable_iterate(od->table, &pa, ptable_print));
180 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
182 struct open_disk *od;
185 od = (struct open_disk *)dev->dd.d_opendata;
186 ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
187 blocks * od->sectorsize, buf, NULL);
193 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
195 struct open_disk *od;
198 od = (struct open_disk *)dev->dd.d_opendata;
199 ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
200 blocks * od->sectorsize, buf, NULL);
206 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
208 struct open_disk *od = dev->dd.d_opendata;
214 case DIOCGSECTORSIZE:
215 *(u_int *)data = od->sectorsize;
218 if (dev->d_offset == 0)
219 *(uint64_t *)data = od->mediasize;
221 *(uint64_t *)data = od->entrysize * od->sectorsize;
231 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
233 struct disk_devdesc partdev;
234 struct open_disk *od;
235 struct ptable *table;
236 struct ptable_entry part;
237 int rc, slice, partition;
239 if (sectorsize == 0) {
240 DPRINTF("unknown sector size");
244 od = (struct open_disk *)malloc(sizeof(struct open_disk));
246 DPRINTF("no memory");
249 dev->dd.d_opendata = od;
251 od->mediasize = mediasize;
252 od->sectorsize = sectorsize;
254 * While we are reading disk metadata, make sure we do it relative
255 * to the start of the disk
257 memcpy(&partdev, dev, sizeof(partdev));
258 partdev.d_offset = 0;
259 partdev.d_slice = D_SLICENONE;
260 partdev.d_partition = D_PARTNONE;
264 slice = dev->d_slice;
265 partition = dev->d_partition;
267 DPRINTF("%s unit %d, slice %d, partition %d => %p", disk_fmtdev(dev),
268 dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
270 /* Determine disk layout. */
271 od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
273 if (od->table == NULL) {
274 DPRINTF("Can't read partition table");
279 if (ptable_getsize(od->table, &mediasize) != 0) {
283 od->mediasize = mediasize;
285 if (ptable_gettype(od->table) == PTABLE_BSD &&
287 /* It doesn't matter what value has d_slice */
288 rc = ptable_getpart(od->table, &part, partition);
290 dev->d_offset = part.start;
291 od->entrysize = part.end - part.start + 1;
293 } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
295 od->entrysize = mediasize;
296 } else if (slice >= 0) {
297 /* Try to get information about partition */
299 rc = ptable_getbestpart(od->table, &part);
301 rc = ptable_getpart(od->table, &part, slice);
302 if (rc != 0) /* Partition doesn't exist */
304 dev->d_offset = part.start;
305 od->entrysize = part.end - part.start + 1;
307 if (ptable_gettype(od->table) == PTABLE_GPT) {
308 partition = D_PARTISGPT;
309 goto out; /* Nothing more to do */
310 } else if (partition == D_PARTISGPT) {
312 * When we try to open GPT partition, but partition
313 * table isn't GPT, reset partition value to
314 * D_PARTWILD and try to autodetect appropriate value.
316 partition = D_PARTWILD;
320 * If partition is D_PARTNONE, then disk_open() was called
321 * to open raw MBR slice.
323 if (partition == D_PARTNONE)
327 * If partition is D_PARTWILD and we are looking at a BSD slice,
328 * then try to read BSD label, otherwise return the
331 if (partition == D_PARTWILD &&
332 part.type != PART_FREEBSD)
334 /* Try to read BSD label */
335 table = ptable_open(dev, part.end - part.start + 1,
336 od->sectorsize, ptblread);
338 DPRINTF("Can't read BSD label");
343 * If slice contains BSD label and partition < 0, then
344 * assume the 'a' partition. Otherwise just return the
345 * whole MBR slice, because it can contain ZFS.
348 if (ptable_gettype(table) != PTABLE_BSD)
352 rc = ptable_getpart(table, &part, partition);
355 dev->d_offset += part.start;
356 od->entrysize = part.end - part.start + 1;
363 if (od->table != NULL)
364 ptable_close(od->table);
366 DPRINTF("%s could not open", disk_fmtdev(dev));
368 /* Save the slice and partition number to the dev */
369 dev->d_slice = slice;
370 dev->d_partition = partition;
371 DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
372 (long long)dev->d_offset, od);
378 disk_close(struct disk_devdesc *dev)
380 struct open_disk *od;
382 od = (struct open_disk *)dev->dd.d_opendata;
383 DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
384 ptable_close(od->table);
390 disk_fmtdev(struct devdesc *vdev)
392 struct disk_devdesc *dev = (struct disk_devdesc *)vdev;
393 static char buf[128];
396 assert(vdev->d_dev->dv_type == DEVT_DISK);
397 cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
398 if (dev->d_slice > D_SLICENONE) {
399 #ifdef LOADER_GPT_SUPPORT
400 if (dev->d_partition == D_PARTISGPT) {
401 sprintf(cp, "p%d:", dev->d_slice);
405 #ifdef LOADER_MBR_SUPPORT
406 cp += sprintf(cp, "s%d", dev->d_slice);
409 if (dev->d_partition > D_PARTNONE)
410 cp += sprintf(cp, "%c", dev->d_partition + 'a');
416 disk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
418 int unit, slice, partition;
421 struct disk_devdesc *dev;
423 np = devspec + 4; /* Skip the leading 'disk' */
426 * If there is path/file info after the device info, then any missing
427 * slice or partition info should be considered a request to search for
428 * an appropriate partition. Otherwise we want to open the raw device
429 * itself and not try to fill in missing info by searching.
431 if ((cp = strchr(np, ':')) != NULL && cp[1] != '\0') {
433 partition = D_PARTWILD;
436 partition = D_PARTNONE;
439 if (*np != '\0' && *np != ':') {
440 unit = strtol(np, &cp, 10);
443 #ifdef LOADER_GPT_SUPPORT
446 slice = strtol(np, &cp, 10);
449 /* we don't support nested partitions on GPT */
450 if (*cp != '\0' && *cp != ':')
452 partition = D_PARTISGPT;
455 #ifdef LOADER_MBR_SUPPORT
458 slice = strtol(np, &cp, 10);
463 if (*cp != '\0' && *cp != ':') {
464 partition = *cp - 'a';
472 if (*cp != '\0' && *cp != ':')
474 dev = malloc(sizeof(*dev));
477 dev->dd.d_unit = unit;
478 dev->d_slice = slice;
479 dev->d_partition = partition;
482 *path = (*cp == '\0') ? cp: cp + 1;