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>
41 # define DEBUG(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
43 # define DEBUG(fmt, args...)
54 struct disk_devdesc *dev;
59 /* Convert size to a human-readable number. */
61 display_size(uint64_t size, u_int sectorsize)
66 size = size * sectorsize / 1024;
68 if (size >= 10485760000LL) {
71 } else if (size >= 10240000) {
74 } else if (size >= 10000) {
78 sprintf(buf, "%ld%cB", (long)size, unit);
83 ptblread(void *d, void *buf, size_t blocks, uint64_t offset)
85 struct disk_devdesc *dev;
88 dev = (struct disk_devdesc *)d;
89 od = (struct open_disk *)dev->dd.d_opendata;
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.
96 offset *= (od->sectorsize / 512);
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.
101 return (dev->dd.d_dev->dv_strategy(dev, F_READ | F_NORA, offset,
102 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;
116 pa = (struct print_args *)arg;
117 od = (struct open_disk *)pa->dev->dd.d_opendata;
118 sprintf(line, " %s%s: %s", pa->prefix, pname,
119 parttype2str(part->type));
121 sprintf(line, "%-*s%s", PWIDTH, line,
122 display_size(part->end - part->start + 1,
125 if (pager_output(line))
128 if (part->type == PART_FREEBSD) {
129 /* Open slice with BSD label */
130 dev.dd.d_dev = pa->dev->dd.d_dev;
131 dev.dd.d_unit = pa->dev->dd.d_unit;
132 dev.d_slice = part->index;
133 dev.d_partition = -1;
134 if (disk_open(&dev, part->end - part->start + 1,
135 od->sectorsize) == 0) {
136 table = ptable_open(&dev, part->end - part->start + 1,
137 od->sectorsize, ptblread);
139 sprintf(line, " %s%s", pa->prefix, pname);
142 bsd.verbose = pa->verbose;
143 res = ptable_iterate(table, &bsd, ptable_print);
155 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
157 struct open_disk *od;
158 struct print_args pa;
160 /* Disk should be opened */
161 od = (struct open_disk *)dev->dd.d_opendata;
164 pa.verbose = verbose;
165 return (ptable_iterate(od->table, &pa, ptable_print));
169 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
171 struct open_disk *od;
174 od = (struct open_disk *)dev->dd.d_opendata;
175 ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
176 blocks * od->sectorsize, buf, NULL);
182 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
184 struct open_disk *od;
187 od = (struct open_disk *)dev->dd.d_opendata;
188 ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
189 blocks * od->sectorsize, buf, NULL);
195 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
197 struct open_disk *od = dev->dd.d_opendata;
203 case DIOCGSECTORSIZE:
204 *(u_int *)data = od->sectorsize;
207 if (dev->d_offset == 0)
208 *(uint64_t *)data = od->mediasize;
210 *(uint64_t *)data = od->entrysize * od->sectorsize;
220 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
222 struct disk_devdesc partdev;
223 struct open_disk *od;
224 struct ptable *table;
225 struct ptable_entry part;
226 int rc, slice, partition;
229 od = (struct open_disk *)malloc(sizeof(struct open_disk));
234 dev->dd.d_opendata = od;
236 od->mediasize = mediasize;
237 od->sectorsize = sectorsize;
239 * While we are reading disk metadata, make sure we do it relative
240 * to the start of the disk
242 memcpy(&partdev, dev, sizeof(partdev));
243 partdev.d_offset = 0;
244 partdev.d_slice = -1;
245 partdev.d_partition = -1;
249 slice = dev->d_slice;
250 partition = dev->d_partition;
252 DEBUG("%s unit %d, slice %d, partition %d => %p",
253 disk_fmtdev(dev), dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
255 /* Determine disk layout. */
256 od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
258 if (od->table == NULL) {
259 DEBUG("Can't read partition table");
264 if (ptable_getsize(od->table, &mediasize) != 0) {
268 if (mediasize > od->mediasize) {
269 od->mediasize = mediasize;
272 if (ptable_gettype(od->table) == PTABLE_BSD &&
274 /* It doesn't matter what value has d_slice */
275 rc = ptable_getpart(od->table, &part, partition);
277 dev->d_offset = part.start;
278 od->entrysize = part.end - part.start + 1;
280 } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
282 od->entrysize = mediasize;
283 } else if (slice >= 0) {
284 /* Try to get information about partition */
286 rc = ptable_getbestpart(od->table, &part);
288 rc = ptable_getpart(od->table, &part, slice);
289 if (rc != 0) /* Partition doesn't exist */
291 dev->d_offset = part.start;
292 od->entrysize = part.end - part.start + 1;
294 if (ptable_gettype(od->table) == PTABLE_GPT) {
296 goto out; /* Nothing more to do */
297 } else if (partition == 255) {
299 * When we try to open GPT partition, but partition
300 * table isn't GPT, reset d_partition value to -1
301 * and try to autodetect appropriate value.
306 * If d_partition < 0 and we are looking at a BSD slice,
307 * then try to read BSD label, otherwise return the
310 if (partition == -1 &&
311 part.type != PART_FREEBSD)
313 /* Try to read BSD label */
314 table = ptable_open(dev, part.end - part.start + 1,
315 od->sectorsize, ptblread);
317 DEBUG("Can't read BSD label");
322 * If slice contains BSD label and d_partition < 0, then
323 * assume the 'a' partition. Otherwise just return the
324 * whole MBR slice, because it can contain ZFS.
327 if (ptable_gettype(table) != PTABLE_BSD)
331 rc = ptable_getpart(table, &part, partition);
334 dev->d_offset += part.start;
335 od->entrysize = part.end - part.start + 1;
342 if (od->table != NULL)
343 ptable_close(od->table);
345 DEBUG("%s could not open", disk_fmtdev(dev));
347 /* Save the slice and partition number to the dev */
348 dev->d_slice = slice;
349 dev->d_partition = partition;
350 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
351 (long long)dev->d_offset, od);
357 disk_close(struct disk_devdesc *dev)
359 struct open_disk *od;
361 od = (struct open_disk *)dev->dd.d_opendata;
362 DEBUG("%s closed => %p", disk_fmtdev(dev), od);
363 ptable_close(od->table);
369 disk_fmtdev(struct disk_devdesc *dev)
371 static char buf[128];
374 cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
375 if (dev->d_slice >= 0) {
376 #ifdef LOADER_GPT_SUPPORT
377 if (dev->d_partition == 255) {
378 sprintf(cp, "p%d:", dev->d_slice);
382 #ifdef LOADER_MBR_SUPPORT
383 cp += sprintf(cp, "s%d", dev->d_slice);
386 if (dev->d_partition >= 0)
387 cp += sprintf(cp, "%c", dev->d_partition + 'a');
393 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
395 int unit, slice, partition;
400 unit = slice = partition = -1;
401 if (*np != '\0' && *np != ':') {
402 unit = strtol(np, &cp, 10);
405 #ifdef LOADER_GPT_SUPPORT
408 slice = strtol(np, &cp, 10);
411 /* we don't support nested partitions on GPT */
412 if (*cp != '\0' && *cp != ':')
417 #ifdef LOADER_MBR_SUPPORT
420 slice = strtol(np, &cp, 10);
425 if (*cp != '\0' && *cp != ':') {
426 partition = *cp - 'a';
434 if (*cp != '\0' && *cp != ':')
436 dev->dd.d_unit = unit;
437 dev->d_slice = slice;
438 dev->d_partition = partition;
440 *path = (*cp == '\0') ? cp: cp + 1;