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 DPRINTF(fmt, args...) printf("%s: " fmt "\n" , __func__ , ## args)
43 # define DPRINTF(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, "%4ld%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));
106 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
108 struct disk_devdesc dev;
109 struct print_args *pa, bsd;
110 struct open_disk *od;
111 struct ptable *table;
117 pa = (struct print_args *)arg;
118 od = (struct open_disk *)pa->dev->dd.d_opendata;
119 sectsize = od->sectorsize;
120 partsize = part->end - part->start + 1;
121 sprintf(line, " %s%s: %s\t%s\n", pa->prefix, pname,
122 parttype2str(part->type),
123 pa->verbose ? display_size(partsize, sectsize) : "");
124 if (pager_output(line))
127 if (part->type == PART_FREEBSD) {
128 /* Open slice with BSD label */
129 dev.dd.d_dev = pa->dev->dd.d_dev;
130 dev.dd.d_unit = pa->dev->dd.d_unit;
131 dev.d_slice = part->index;
132 dev.d_partition = -1;
133 if (disk_open(&dev, partsize, sectsize) == 0) {
135 * disk_open() for partition -1 on a bsd slice assumes
136 * you want the first bsd partition. Reset things so
137 * that we're looking at the start of the raw slice.
139 dev.d_partition = -1;
140 dev.d_offset = part->start;
141 table = ptable_open(&dev, partsize, sectsize, ptblread);
143 sprintf(line, " %s%s", pa->prefix, pname);
146 bsd.verbose = pa->verbose;
147 res = ptable_iterate(table, &bsd, ptable_print);
158 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
160 struct open_disk *od;
161 struct print_args pa;
163 /* Disk should be opened */
164 od = (struct open_disk *)dev->dd.d_opendata;
167 pa.verbose = verbose;
168 return (ptable_iterate(od->table, &pa, ptable_print));
172 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
174 struct open_disk *od;
177 od = (struct open_disk *)dev->dd.d_opendata;
178 ret = dev->dd.d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
179 blocks * od->sectorsize, buf, NULL);
185 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
187 struct open_disk *od;
190 od = (struct open_disk *)dev->dd.d_opendata;
191 ret = dev->dd.d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
192 blocks * od->sectorsize, buf, NULL);
198 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
200 struct open_disk *od = dev->dd.d_opendata;
206 case DIOCGSECTORSIZE:
207 *(u_int *)data = od->sectorsize;
210 if (dev->d_offset == 0)
211 *(uint64_t *)data = od->mediasize;
213 *(uint64_t *)data = od->entrysize * od->sectorsize;
223 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
225 struct disk_devdesc partdev;
226 struct open_disk *od;
227 struct ptable *table;
228 struct ptable_entry part;
229 int rc, slice, partition;
232 od = (struct open_disk *)malloc(sizeof(struct open_disk));
234 DPRINTF("no memory");
237 dev->dd.d_opendata = od;
239 od->mediasize = mediasize;
240 od->sectorsize = sectorsize;
242 * While we are reading disk metadata, make sure we do it relative
243 * to the start of the disk
245 memcpy(&partdev, dev, sizeof(partdev));
246 partdev.d_offset = 0;
247 partdev.d_slice = -1;
248 partdev.d_partition = -1;
252 slice = dev->d_slice;
253 partition = dev->d_partition;
255 DPRINTF("%s unit %d, slice %d, partition %d => %p",
256 disk_fmtdev(dev), dev->dd.d_unit, dev->d_slice, dev->d_partition, od);
258 /* Determine disk layout. */
259 od->table = ptable_open(&partdev, mediasize / sectorsize, sectorsize,
261 if (od->table == NULL) {
262 DPRINTF("Can't read partition table");
267 if (ptable_getsize(od->table, &mediasize) != 0) {
271 od->mediasize = mediasize;
273 if (ptable_gettype(od->table) == PTABLE_BSD &&
275 /* It doesn't matter what value has d_slice */
276 rc = ptable_getpart(od->table, &part, partition);
278 dev->d_offset = part.start;
279 od->entrysize = part.end - part.start + 1;
281 } else if (ptable_gettype(od->table) == PTABLE_ISO9660) {
283 od->entrysize = mediasize;
284 } else if (slice >= 0) {
285 /* Try to get information about partition */
287 rc = ptable_getbestpart(od->table, &part);
289 rc = ptable_getpart(od->table, &part, slice);
290 if (rc != 0) /* Partition doesn't exist */
292 dev->d_offset = part.start;
293 od->entrysize = part.end - part.start + 1;
295 if (ptable_gettype(od->table) == PTABLE_GPT) {
297 goto out; /* Nothing more to do */
298 } else if (partition == 255) {
300 * When we try to open GPT partition, but partition
301 * table isn't GPT, reset d_partition value to -1
302 * and try to autodetect appropriate value.
307 * If d_partition < 0 and we are looking at a BSD slice,
308 * then try to read BSD label, otherwise return the
311 if (partition == -1 &&
312 part.type != PART_FREEBSD)
314 /* Try to read BSD label */
315 table = ptable_open(dev, part.end - part.start + 1,
316 od->sectorsize, ptblread);
318 DPRINTF("Can't read BSD label");
323 * If slice contains BSD label and d_partition < 0, then
324 * assume the 'a' partition. Otherwise just return the
325 * whole MBR slice, because it can contain ZFS.
328 if (ptable_gettype(table) != PTABLE_BSD)
332 rc = ptable_getpart(table, &part, partition);
335 dev->d_offset += part.start;
336 od->entrysize = part.end - part.start + 1;
343 if (od->table != NULL)
344 ptable_close(od->table);
346 DPRINTF("%s could not open", disk_fmtdev(dev));
348 /* Save the slice and partition number to the dev */
349 dev->d_slice = slice;
350 dev->d_partition = partition;
351 DPRINTF("%s offset %lld => %p", disk_fmtdev(dev),
352 (long long)dev->d_offset, od);
358 disk_close(struct disk_devdesc *dev)
360 struct open_disk *od;
362 od = (struct open_disk *)dev->dd.d_opendata;
363 DPRINTF("%s closed => %p", disk_fmtdev(dev), od);
364 ptable_close(od->table);
370 disk_fmtdev(struct disk_devdesc *dev)
372 static char buf[128];
375 cp = buf + sprintf(buf, "%s%d", dev->dd.d_dev->dv_name, dev->dd.d_unit);
376 if (dev->d_slice >= 0) {
377 #ifdef LOADER_GPT_SUPPORT
378 if (dev->d_partition == 255) {
379 sprintf(cp, "p%d:", dev->d_slice);
383 #ifdef LOADER_MBR_SUPPORT
384 cp += sprintf(cp, "s%d", dev->d_slice);
387 if (dev->d_partition >= 0)
388 cp += sprintf(cp, "%c", dev->d_partition + 'a');
394 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
396 int unit, slice, partition;
401 unit = slice = partition = -1;
402 if (*np != '\0' && *np != ':') {
403 unit = strtol(np, &cp, 10);
406 #ifdef LOADER_GPT_SUPPORT
409 slice = strtol(np, &cp, 10);
412 /* we don't support nested partitions on GPT */
413 if (*cp != '\0' && *cp != ':')
418 #ifdef LOADER_MBR_SUPPORT
421 slice = strtol(np, &cp, 10);
426 if (*cp != '\0' && *cp != ':') {
427 partition = *cp - 'a';
435 if (*cp != '\0' && *cp != ':')
437 dev->dd.d_unit = unit;
438 dev->d_slice = slice;
439 dev->d_partition = partition;
441 *path = (*cp == '\0') ? cp: cp + 1;