2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
52 diskread(void *arg, void *buf, size_t blocks, off_t offset)
56 dp = (struct disk *)arg;
57 printf("%s: read %d blocks from the offset %jd [+%jd]\n", dp->name,
58 blocks, offset, dp->offset);
59 if (offset >= dp->mediasize / dp->sectorsize)
62 return (pread(dp->fd, buf, blocks * dp->sectorsize,
63 (offset + dp->offset) * dp->sectorsize) != blocks * dp->sectorsize);
67 ptable_type2str(const struct ptable *table)
70 switch (ptable_gettype(table)) {
87 ptable_print(void *arg, const char *pname, const struct ptable_entry *part)
91 char line[80], size[6];
93 dp = (struct disk *)arg;
94 sprintf(line, " %s%s: %s", dp->file ? "disk0": dp->name, pname,
95 parttype2str(part->type));
96 humanize_number(size, sizeof(size),
97 (part->end - part->start + 1) * dp->sectorsize, "",
98 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL);
99 printf("%-*s%s\n", PWIDTH, line, size);
100 if (part->type == PART_FREEBSD) {
101 sprintf(line, "%s%s", dp->file ? "disk0": dp->name, pname);
104 bsd.file = 0; /* to use dp->name in the next sprintf */
105 bsd.offset = dp->offset + part->start;
106 bsd.sectorsize = dp->sectorsize;
107 bsd.mediasize = (part->end - part->start + 1) * dp->sectorsize;
108 table = ptable_open(&bsd, bsd.mediasize / bsd.sectorsize,
109 bsd.sectorsize, diskread);
112 ptable_iterate(table, &bsd, ptable_print);
119 inspect_disk(struct disk *dp)
121 struct ptable *table;
123 table = ptable_open(dp, dp->mediasize / dp->sectorsize,
124 dp->sectorsize, diskread);
126 printf("ptable_open failed\n");
129 printf("Partition table detected: %s\n", ptable_type2str(table));
130 ptable_iterate(table, dp, ptable_print);
135 main(int argc, char **argv)
141 errx(1, "Usage: %s <GEOM provider name> | "
142 "<disk image file name>", argv[0]);
144 if (stat(d.name, &sb) == 0 && S_ISREG(sb.st_mode)) {
145 d.fd = open(d.name, O_RDONLY);
147 err(1, "open %s", d.name);
148 d.mediasize = sb.st_size;
152 d.fd = g_open(d.name, 0);
154 err(1, "g_open %s", d.name);
155 d.mediasize = g_mediasize(d.fd);
156 d.sectorsize = g_sectorsize(d.fd);
160 printf("%s \"%s\" opened\n", d.file ? "Disk image": "GEOM provider",
162 printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n",
163 d.mediasize, d.mediasize / d.sectorsize, d.sectorsize);