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->d_opendata;
90 return (dev->d_dev->dv_strategy(dev, F_READ, offset,
91 blocks * od->sectorsize, (char *)buf, NULL));
96 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
98 struct disk_devdesc dev;
99 struct print_args *pa, bsd;
100 struct open_disk *od;
101 struct ptable *table;
105 pa = (struct print_args *)arg;
106 od = (struct open_disk *)pa->dev->d_opendata;
107 sprintf(line, " %s%s: %s", pa->prefix, pname,
108 parttype2str(part->type));
110 sprintf(line, "%-*s%s", PWIDTH, line,
111 display_size(part->end - part->start + 1,
114 if (pager_output(line))
117 if (part->type == PART_FREEBSD) {
118 /* Open slice with BSD label */
119 dev.d_dev = pa->dev->d_dev;
120 dev.d_unit = pa->dev->d_unit;
121 dev.d_slice = part->index;
122 dev.d_partition = -1;
123 if (disk_open(&dev, part->end - part->start + 1,
124 od->sectorsize) == 0) {
125 table = ptable_open(&dev, part->end - part->start + 1,
126 od->sectorsize, ptblread);
128 sprintf(line, " %s%s", pa->prefix, pname);
131 bsd.verbose = pa->verbose;
132 res = ptable_iterate(table, &bsd, ptable_print);
144 disk_print(struct disk_devdesc *dev, char *prefix, int verbose)
146 struct open_disk *od;
147 struct print_args pa;
149 /* Disk should be opened */
150 od = (struct open_disk *)dev->d_opendata;
153 pa.verbose = verbose;
154 return (ptable_iterate(od->table, &pa, ptable_print));
158 disk_read(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
160 struct open_disk *od;
163 od = (struct open_disk *)dev->d_opendata;
164 ret = dev->d_dev->dv_strategy(dev, F_READ, dev->d_offset + offset,
165 blocks * od->sectorsize, buf, NULL);
171 disk_write(struct disk_devdesc *dev, void *buf, uint64_t offset, u_int blocks)
173 struct open_disk *od;
176 od = (struct open_disk *)dev->d_opendata;
177 ret = dev->d_dev->dv_strategy(dev, F_WRITE, dev->d_offset + offset,
178 blocks * od->sectorsize, buf, NULL);
184 disk_ioctl(struct disk_devdesc *dev, u_long cmd, void *data)
186 struct open_disk *od = dev->d_opendata;
192 case DIOCGSECTORSIZE:
193 *(u_int *)data = od->sectorsize;
196 if (dev->d_offset == 0)
197 *(uint64_t *)data = od->mediasize;
199 *(uint64_t *)data = od->entrysize * od->sectorsize;
209 disk_open(struct disk_devdesc *dev, uint64_t mediasize, u_int sectorsize)
211 struct open_disk *od;
212 struct ptable *table;
213 struct ptable_entry part;
214 int rc, slice, partition;
218 * While we are reading disk metadata, make sure we do it relative
219 * to the start of the disk
223 slice = dev->d_slice;
224 partition = dev->d_partition;
225 od = (struct open_disk *)malloc(sizeof(struct open_disk));
230 dev->d_opendata = od;
232 od->mediasize = mediasize;
233 od->sectorsize = sectorsize;
234 DEBUG("%s unit %d, slice %d, partition %d => %p",
235 disk_fmtdev(dev), dev->d_unit, dev->d_slice, dev->d_partition, od);
237 /* Determine disk layout. */
238 od->table = ptable_open(dev, mediasize / sectorsize, sectorsize,
240 if (od->table == NULL) {
241 DEBUG("Can't read partition table");
246 if (ptable_getsize(od->table, &mediasize) != 0) {
250 if (mediasize > od->mediasize) {
251 od->mediasize = mediasize;
254 if (ptable_gettype(od->table) == PTABLE_BSD &&
256 /* It doesn't matter what value has d_slice */
257 rc = ptable_getpart(od->table, &part, partition);
259 dev->d_offset = part.start;
260 od->entrysize = part.end - part.start + 1;
262 } else if (slice >= 0) {
263 /* Try to get information about partition */
265 rc = ptable_getbestpart(od->table, &part);
267 rc = ptable_getpart(od->table, &part, slice);
268 if (rc != 0) /* Partition doesn't exist */
270 dev->d_offset = part.start;
271 od->entrysize = part.end - part.start + 1;
273 if (ptable_gettype(od->table) == PTABLE_GPT) {
275 goto out; /* Nothing more to do */
276 } else if (partition == 255) {
278 * When we try to open GPT partition, but partition
279 * table isn't GPT, reset d_partition value to -1
280 * and try to autodetect appropriate value.
285 * If d_partition < 0 and we are looking at a BSD slice,
286 * then try to read BSD label, otherwise return the
289 if (partition == -1 &&
290 part.type != PART_FREEBSD)
292 /* Try to read BSD label */
293 table = ptable_open(dev, part.end - part.start + 1,
294 od->sectorsize, ptblread);
296 DEBUG("Can't read BSD label");
301 * If slice contains BSD label and d_partition < 0, then
302 * assume the 'a' partition. Otherwise just return the
303 * whole MBR slice, because it can contain ZFS.
306 if (ptable_gettype(table) != PTABLE_BSD)
310 rc = ptable_getpart(table, &part, partition);
313 dev->d_offset += part.start;
314 od->entrysize = part.end - part.start + 1;
321 if (od->table != NULL)
322 ptable_close(od->table);
324 DEBUG("%s could not open", disk_fmtdev(dev));
326 /* Save the slice and partition number to the dev */
327 dev->d_slice = slice;
328 dev->d_partition = partition;
329 DEBUG("%s offset %lld => %p", disk_fmtdev(dev),
330 (long long)dev->d_offset, od);
336 disk_close(struct disk_devdesc *dev)
338 struct open_disk *od;
340 od = (struct open_disk *)dev->d_opendata;
341 DEBUG("%s closed => %p", disk_fmtdev(dev), od);
342 ptable_close(od->table);
348 disk_fmtdev(struct disk_devdesc *dev)
350 static char buf[128];
353 cp = buf + sprintf(buf, "%s%d", dev->d_dev->dv_name, dev->d_unit);
354 if (dev->d_slice >= 0) {
355 #ifdef LOADER_GPT_SUPPORT
356 if (dev->d_partition == 255) {
357 sprintf(cp, "p%d:", dev->d_slice);
361 #ifdef LOADER_MBR_SUPPORT
362 cp += sprintf(cp, "s%d", dev->d_slice);
365 if (dev->d_partition >= 0)
366 cp += sprintf(cp, "%c", dev->d_partition + 'a');
372 disk_parsedev(struct disk_devdesc *dev, const char *devspec, const char **path)
374 int unit, slice, partition;
379 unit = slice = partition = -1;
380 if (*np != '\0' && *np != ':') {
381 unit = strtol(np, &cp, 10);
384 #ifdef LOADER_GPT_SUPPORT
387 slice = strtol(np, &cp, 10);
390 /* we don't support nested partitions on GPT */
391 if (*cp != '\0' && *cp != ':')
396 #ifdef LOADER_MBR_SUPPORT
399 slice = strtol(np, &cp, 10);
404 if (*cp != '\0' && *cp != ':') {
405 partition = *cp - 'a';
413 if (*cp != '\0' && *cp != ':')
416 dev->d_slice = slice;
417 dev->d_partition = partition;
419 *path = (*cp == '\0') ? cp: cp + 1;