1/*- 2 * Copyright (c) 2012 Andrey V. Elsukov <ae@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD$"); 29 30#include <sys/types.h> 31#include <sys/stat.h> 32 33#include <err.h> 34#include <fcntl.h> 35#include <libgeom.h> 36#include <libutil.h> 37#include <part.h> 38#include <stdio.h> 39#include <unistd.h> 40 41struct disk { 42 const char *name; 43 uint64_t mediasize; 44 uint16_t sectorsize; 45 46 int fd; 47 int file; 48 off_t offset; 49}; 50 51static int 52diskread(void *arg, void *buf, size_t blocks, off_t offset) 53{ 54 struct disk *dp; 55 56 dp = (struct disk *)arg; 57 printf("%s: read %lu blocks from the offset %jd [+%jd]\n", dp->name, 58 blocks, offset, dp->offset); 59 if (offset >= dp->mediasize / dp->sectorsize) 60 return (-1); 61 62 return (pread(dp->fd, buf, blocks * dp->sectorsize, 63 (offset + dp->offset) * dp->sectorsize) != blocks * dp->sectorsize); 64} 65 66static const char* 67ptable_type2str(const struct ptable *table) 68{ 69 70 switch (ptable_gettype(table)) { 71 case PTABLE_NONE: 72 return ("None"); 73 case PTABLE_BSD: 74 return ("BSD"); 75 case PTABLE_MBR: 76 return ("MBR"); 77 case PTABLE_GPT: 78 return ("GPT"); 79 case PTABLE_VTOC8: 80 return ("VTOC8"); 81 }; 82 return ("Unknown"); 83} 84 85#define PWIDTH 35 86static void 87ptable_print(void *arg, const char *pname, const struct ptable_entry *part) 88{ 89 struct ptable *table; 90 struct disk *dp, bsd; 91 char line[80], size[6]; 92 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); 102 bsd.name = line; 103 bsd.fd = dp->fd; 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); 110 if (table == NULL) 111 return; 112 ptable_iterate(table, &bsd, ptable_print); 113 ptable_close(table); 114 } 115} 116#undef PWIDTH 117 118static void 119inspect_disk(struct disk *dp) 120{ 121 struct ptable *table; 122 123 table = ptable_open(dp, dp->mediasize / dp->sectorsize, 124 dp->sectorsize, diskread); 125 if (table == NULL) { 126 printf("ptable_open failed\n"); 127 return; 128 } 129 printf("Partition table detected: %s\n", ptable_type2str(table)); 130 ptable_iterate(table, dp, ptable_print); 131 ptable_close(table); 132} 133 134int 135main(int argc, char **argv) 136{ 137 struct stat sb; 138 struct disk d; 139 140 if (argc < 2) 141 errx(1, "Usage: %s <GEOM provider name> | " 142 "<disk image file name>", argv[0]); 143 d.name = argv[1]; 144 if (stat(d.name, &sb) == 0 && S_ISREG(sb.st_mode)) { 145 d.fd = open(d.name, O_RDONLY); 146 if (d.fd < 0) 147 err(1, "open %s", d.name); 148 d.mediasize = sb.st_size; 149 d.sectorsize = 512; 150 d.file = 1; 151 } else { 152 d.fd = g_open(d.name, 0); 153 if (d.fd < 0) 154 err(1, "g_open %s", d.name); 155 d.mediasize = g_mediasize(d.fd); 156 d.sectorsize = g_sectorsize(d.fd); 157 d.file = 0; 158 } 159 d.offset = 0; 160 printf("%s \"%s\" opened\n", d.file ? "Disk image": "GEOM provider", 161 d.name); 162 printf("Mediasize: %ju Bytes (%ju sectors)\nSectorsize: %u Bytes\n", 163 d.mediasize, d.mediasize / d.sectorsize, d.sectorsize); 164 165 inspect_disk(&d); 166 167 if (d.file) 168 close(d.fd); 169 else 170 g_close(d.fd); 171 return (0); 172} 173