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
29 #include <sys/queue.h>
32 #include <bootstrap.h>
39 # define DPRINTF(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
41 # define DPRINTF(fmt, args...) ((void)0)
52 struct disk_devdesc *dev;
57 /* Convert size to a human-readable number. */
59 display_size(uint64_t size, u_int sectorsize)
64 size = size * sectorsize / 1024;
66 if (size >= 10485760000LL) {
69 } else if (size >= 10240000) {
72 } else if (size >= 10000) {
76 snprintf(buf, sizeof(buf), "%4ld%cB", (long)size, unit);
81 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
83 struct disk_devdesc *dev;
86 dev = (struct disk_devdesc *)d;
87 od = (struct open_disk *)dev->dd.d_opendata;
90 * The strategy function assumes the offset is in units of 512 byte
91 * sectors. For larger sector sizes, we need to adjust the offset to
92 * match the actual sector size.
94 offset *= (od->sectorsize / 512);
96 * As the GPT backup partition is located at the end of the disk,
97 * to avoid reading past disk end, flag bcache not to use RA.
99 return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
100 blocks * od->sectorsize, (char *)buf, NULL));
104 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
106 struct disk_devdesc dev;
107 struct print_args *pa, bsd;
108 struct open_disk *od;
109 struct ptable *table;
115 pa = (struct print_args *)arg;
116 od = (struct open_disk *)pa->dev->dd.d_opendata;
117 sectsize = od->sectorsize;
118 partsize = part->end - part->start + 1;
119 snprintf(line, sizeof(line), " %s%s: %s", pa->prefix, pname,
120 parttype2str(part->type));
121 if (pager_output(line))
125 /* Emit extra tab when the line is shorter than 3 tab stops */
126 if (strlen(line) < 24)
127 (void) pager_output("\t");
129 snprintf(line, sizeof(line), "\t%s",
130 display_size(partsize, sectsize));
131 if (pager_output(line))
134 if (pager_output("\n"))
138 if (part->type == PART_FREEBSD) {
139 /* Open slice with BSD label */
140 dev.dd.d_dev = pa->dev->dd.d_dev;
141 dev.dd.d_unit = pa->dev->dd.d_unit;
142 dev.d_slice = part->index;
143 dev.d_partition = D_PARTNONE;
144 if (disk_open(&dev, partsize, sectsize) == 0) {
145 table = ptable_open(&dev, partsize, sectsize, ptblread);
147 snprintf(line, sizeof(line), " %s%s",
151 bsd.verbose = pa->verbose;
152 res = ptable_iterate(table, &bsd, ptable_print);
163 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
165 struct open_disk *od;
166 struct print_args pa;
168 /* Disk should be opened */
169 od = (struct open_disk *)dev->dd.d_opendata;
172 pa.verbose = verbose;
173 return (ptable_iterate(od->table, &pa, ptable_print));
177 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
179 struct open_disk *od;
182 od = (struct open_disk *)dev->dd.d_opendata;
183 ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
184 blocks * od->sectorsize, buf, NULL);
190 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
192 struct open_disk *od;
195 od = (struct open_disk *)dev->dd.d_opendata;
196 ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
197 blocks * od->sectorsize, buf, NULL);
203 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
205 struct open_disk *od = dev->dd.d_opendata;
211 case DIOCGSECTORSIZE:
212 *(u_int *)data = od->sectorsize;
215 if (dev->d_offset == 0)
216 *(uint64_t *)data = od->mediasize;
218 *(uint64_t *)data = od->entrysize * od->sectorsize;
228 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
230 struct disk_devdesc partdev;
231 struct open_disk *od;
232 struct ptable *table;
233 struct ptable_entry part;
234 int rc, slice, partition;
236 if (sectorsize == 0) {
237 DPRINTF("unknown sector size");
241 od = (struct open_disk *)malloc(sizeof(struct open_disk));
243 DPRINTF("no memory");
246 dev->dd.d_opendata = od;
248 od->mediasize = mediasize;
249 od->sectorsize = sectorsize;
251 * While we are reading disk metadata, make sure we do it relative
252 * to the start of the disk
254 memcpy(&partdev, dev, sizeof(partdev));
255 partdev.d_offset = 0;
256 partdev.d_slice = D_SLICENONE;
257 partdev.d_partition = D_PARTNONE;
261 slice = dev->d_slice;
262 partition = dev->d_partition;
264 DPRINTF("%s unit %d, slice %d, partition %d => %p", disk_fmtdev(dev),
265 dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
267 /* Determine disk layout. */
268 od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
270 if (od->table == NULL) {
271 DPRINTF("Can't read partition table");
276 if (ptable_getsize(od->table, &mediasize) != 0) {
280 od->mediasize = mediasize;
282 if (ptable_gettype(od->table) == PTABLE_BSD &&
284 /* It doesn't matter what value has d_slice */
285 rc = ptable_getpart(od->table, &part, partition);
287 dev->d_offset = part.start;
288 od->entrysize = part.end - part.start + 1;
290 } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
292 od->entrysize = mediasize;
293 } else if (slice >= 0) {
294 /* Try to get information about partition */
296 rc = ptable_getbestpart(od->table, &part);
298 rc = ptable_getpart(od->table, &part, slice);
299 if (rc != 0) /* Partition doesn't exist */
301 dev->d_offset = part.start;
302 od->entrysize = part.end - part.start + 1;
304 if (ptable_gettype(od->table) == PTABLE_GPT) {
305 partition = D_PARTISGPT;
306 goto out; /* Nothing more to do */
307 } else if (partition == D_PARTISGPT) {
309 * When we try to open GPT partition, but partition
310 * table isn't GPT, reset partition value to
311 * D_PARTWILD and try to autodetect appropriate value.
313 partition = D_PARTWILD;
317 * If partition is D_PARTNONE, then disk_open() was called
318 * to open raw MBR slice.
320 if (partition == D_PARTNONE)
324 * If partition is D_PARTWILD and we are looking at a BSD slice,
325 * then try to read BSD label, otherwise return the
328 if (partition == D_PARTWILD &&
329 part.type != PART_FREEBSD)
331 /* Try to read BSD label */
332 table = ptable_open(dev, part.end - part.start + 1,
333 od->sectorsize, ptblread);
335 DPRINTF("Can't read BSD label");
340 * If slice contains BSD label and partition < 0, then
341 * assume the 'a' partition. Otherwise just return the
342 * whole MBR slice, because it can contain ZFS.
345 if (ptable_gettype(table) != PTABLE_BSD)
349 rc = ptable_getpart(table, &part, partition);
352 dev->d_offset += part.start;
353 od->entrysize = part.end - part.start + 1;
360 if (od->table != NULL)
361 ptable_close(od->table);
363 DPRINTF("%s could not open", disk_fmtdev(dev));
365 /* Save the slice and partition number to the dev */
366 dev->d_slice = slice;
367 dev->d_partition = partition;
368 DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
369 (long long)dev->d_offset, od);
375 disk_close(struct disk_devdesc *dev)
377 struct open_disk *od;
379 od = (struct open_disk *)dev->dd.d_opendata;
380 DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
381 ptable_close(od->table);
387 disk_fmtdev(struct devdesc *vdev)
389 struct disk_devdesc *dev = (struct disk_devdesc *)vdev;
390 static char buf[128];
393 assert(vdev->d_dev->dv_type == DEVT_DISK);
394 cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
395 if (dev->d_slice > D_SLICENONE) {
396 #ifdef LOADER_GPT_SUPPORT
397 if (dev->d_partition == D_PARTISGPT) {
398 sprintf(cp, "p%d:", dev->d_slice);
402 #ifdef LOADER_MBR_SUPPORT
403 cp += sprintf(cp, "s%d", dev->d_slice);
406 if (dev->d_partition > D_PARTNONE)
407 cp += sprintf(cp, "%c", dev->d_partition + 'a');
413 disk_parsedev(struct devdesc **idev, const char *devspec, const char **path)
415 int unit, slice, partition;
418 struct disk_devdesc *dev;
420 np = devspec + 4; /* Skip the leading 'disk' */
423 * If there is path/file info after the device info, then any missing
424 * slice or partition info should be considered a request to search for
425 * an appropriate partition. Otherwise we want to open the raw device
426 * itself and not try to fill in missing info by searching.
428 if ((cp = strchr(np, ':')) != NULL && cp[1] != '\0') {
430 partition = D_PARTWILD;
433 partition = D_PARTNONE;
436 if (*np != '\0' && *np != ':') {
437 unit = strtol(np, &cp, 10);
440 #ifdef LOADER_GPT_SUPPORT
443 slice = strtol(np, &cp, 10);
446 /* we don't support nested partitions on GPT */
447 if (*cp != '\0' && *cp != ':')
449 partition = D_PARTISGPT;
452 #ifdef LOADER_MBR_SUPPORT
455 slice = strtol(np, &cp, 10);
460 if (*cp != '\0' && *cp != ':') {
461 partition = *cp - 'a';
469 if (*cp != '\0' && *cp != ':')
471 dev = malloc(sizeof(*dev));
474 dev->dd.d_unit = unit;
475 dev->d_slice = slice;
476 dev->d_partition = partition;
479 *path = (*cp == '\0') ? cp: cp + 1;