diff options
author | peter <peter@FreeBSD.org> | 2002-07-20 19:27:43 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 2002-07-20 19:27:43 +0000 |
commit | 6dfc24537dd08c0fd54fef53634257ed5b85aa8e (patch) | |
tree | 58b29ec4f042260b9ce91a33c9f2cd135c628d9c | |
parent | aefe01915ae8c5993f364f973809e39b9773eb59 (diff) | |
download | FreeBSD-src-6dfc24537dd08c0fd54fef53634257ed5b85aa8e.zip FreeBSD-src-6dfc24537dd08c0fd54fef53634257ed5b85aa8e.tar.gz |
Grab a snapshot of Marcel's gpt tool. This is still a work-in-progress
but is useful to have handy. EFI GPT partitions are used instead of the
fdisk+disklabel combination. They are pure 64 bit LBA, are fully
extensible, support up to 16383 partitons per disk, etc.
-rw-r--r-- | sbin/gpt/Makefile | 8 | ||||
-rw-r--r-- | sbin/gpt/destroy.c | 114 | ||||
-rw-r--r-- | sbin/gpt/gpt.c | 466 | ||||
-rw-r--r-- | sbin/gpt/gpt.h | 70 | ||||
-rw-r--r-- | sbin/gpt/map.c | 230 | ||||
-rw-r--r-- | sbin/gpt/map.h | 59 | ||||
-rw-r--r-- | sbin/gpt/migrate.c | 324 | ||||
-rw-r--r-- | sbin/gpt/mkdisk.sh | 11 | ||||
-rw-r--r-- | sbin/gpt/recover.c | 92 | ||||
-rw-r--r-- | sbin/gpt/show.c | 80 |
10 files changed, 1454 insertions, 0 deletions
diff --git a/sbin/gpt/Makefile b/sbin/gpt/Makefile new file mode 100644 index 0000000..6500d1f --- /dev/null +++ b/sbin/gpt/Makefile @@ -0,0 +1,8 @@ +# $FreeBSD$ + +PROG= gpt +SRCS= destroy.c gpt.c map.c migrate.c recover.c show.c +WARNS= 4 +NOMAN= not yet + +.include <bsd.prog.mk> diff --git a/sbin/gpt/destroy.c b/sbin/gpt/destroy.c new file mode 100644 index 0000000..101f975 --- /dev/null +++ b/sbin/gpt/destroy.c @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/uuid.h> +#include <sys/gpt.h> + +#include <err.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "map.h" +#include "gpt.h" + +int recoverable; + +static void +usage_destroy(void) +{ + + fprintf(stderr, + "usage: %s [-r] device ...\n", getprogname()); + exit(1); +} + +static void +destroy(int fd) +{ + map_t *pri_hdr, *sec_hdr; + + pri_hdr = map_find(MAP_TYPE_PRI_GPT_HDR); + sec_hdr = map_find(MAP_TYPE_SEC_GPT_HDR); + + if (pri_hdr == NULL && sec_hdr == NULL) { + warnx("%s: error: device doesn't contain a GPT", device_name); + return; + } + + if (recoverable && sec_hdr == NULL) { + warnx("%s: error: recoverability not possible", device_name); + return; + } + + if (pri_hdr != NULL) { + bzero(pri_hdr->map_data, secsz); + gpt_write(fd, pri_hdr); + } + + if (!recoverable && sec_hdr != NULL) { + bzero(sec_hdr->map_data, secsz); + gpt_write(fd, sec_hdr); + } +} + +int +cmd_destroy(int argc, char *argv[]) +{ + int ch, fd; + + while ((ch = getopt(argc, argv, "r")) != -1) { + switch(ch) { + case 'r': + recoverable = 1; + break; + default: + usage_destroy(); + } + } + + if (argc == optind) + usage_destroy(); + + while (optind < argc) { + fd = gpt_open(argv[optind++]); + if (fd == -1) { + warn("unable to open device '%s'", device_name); + continue; + } + + destroy(fd); + + gpt_close(fd); + } + + return (0); +} diff --git a/sbin/gpt/gpt.c b/sbin/gpt/gpt.c new file mode 100644 index 0000000..409ee72 --- /dev/null +++ b/sbin/gpt/gpt.c @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * CRC32 code derived from work by Gary S. Brown. + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/param.h> +#include <sys/types.h> +#include <sys/disk.h> +#include <sys/stat.h> +#include <sys/uuid.h> +#include <sys/gpt.h> + +#include <err.h> +#include <errno.h> +#include <fcntl.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "map.h" +#include "gpt.h" + +char device_name[MAXPATHLEN]; + +off_t mediasz; + +u_int parts; +u_int secsz; + +int readonly, verbose; + +static uint32_t crc32_tab[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, + 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, + 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, + 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, + 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, + 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, + 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, + 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, + 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, + 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, + 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, + 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, + 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, + 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, + 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, + 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, + 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, + 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, + 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, + 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, + 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, + 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, + 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, + 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, + 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, + 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, + 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, + 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, + 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, + 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, + 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, + 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, + 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + +uint32_t +crc32(const void *buf, size_t size) +{ + const uint8_t *p; + uint32_t crc; + + p = buf; + crc = ~0U; + + while (size--) + crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8); + + return crc ^ ~0U; +} + +void +unicode16(short *dst, const wchar_t *src, size_t len) +{ + while (len-- && *src != 0) + *dst++ = *src++; + if (len) + *dst = 0; +} + +static char * +uuid_string(uuid_t *uuid) +{ + static char buf[48]; + + sprintf(buf, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", + uuid->time_low, uuid->time_mid, uuid->time_hi_and_version, + uuid->clock_seq_hi_and_reserved, uuid->clock_seq_low, + uuid->node[0], uuid->node[1], uuid->node[2], uuid->node[3], + uuid->node[4], uuid->node[5]); + return buf; +} + +void* +gpt_read(int fd, off_t lba, size_t count) +{ + off_t ofs; + void *buf; + + count *= secsz; + buf = malloc(count); + if (buf == NULL) + return (NULL); + + ofs = lba * secsz; + if (lseek(fd, ofs, SEEK_SET) == ofs && + read(fd, buf, count) == (ssize_t)count) + return (buf); + + free(buf); + return (NULL); +} + +int +gpt_write(int fd, map_t *map) +{ + off_t ofs; + size_t count; + + count = map->map_size * secsz; + ofs = map->map_start * secsz; + if (lseek(fd, ofs, SEEK_SET) == ofs && + write(fd, map->map_data, count) == (ssize_t)count) + return (0); + return (-1); +} + +static int +gpt_gpt(int fd, off_t lba) +{ + off_t size; + struct gpt_ent *ent; + struct gpt_hdr *hdr; + char *p; + map_t *m; + size_t blocks, tblsz; + unsigned int i; + uint32_t crc; + + hdr = gpt_read(fd, lba, 1); + if (hdr == NULL) + return (-1); + + if (memcmp(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig))) + goto fail_hdr; + + crc = hdr->hdr_crc_self; + hdr->hdr_crc_self = 0; + if (crc32(hdr, hdr->hdr_size) != crc) { + if (verbose) + warnx("%s: Bad CRC in GPT header at sector %llu", + device_name, (long long)lba); + goto fail_hdr; + } + + tblsz = hdr->hdr_entries * hdr->hdr_entsz; + blocks = tblsz / secsz + ((tblsz % secsz) ? 1 : 0); + + /* Use generic pointer to deal with hdr->hdr_entsz != sizeof(*ent). */ + p = gpt_read(fd, hdr->hdr_lba_table, blocks); + if (p == NULL) + return (-1); + + if (crc32(p, tblsz) != hdr->hdr_crc_table) { + if (verbose) + warnx("%s: Bad CRC in GPT table at sector %llu", + device_name, (long long)hdr->hdr_lba_table); + goto fail_ent; + } + + if (verbose > 1) + warnx("%s: %s GPT at sector %llu", device_name, + (lba == 1) ? "Pri" : "Sec", (long long)lba); + + m = map_add(lba, 1, (lba == 1) + ? MAP_TYPE_PRI_GPT_HDR : MAP_TYPE_SEC_GPT_HDR, hdr); + if (m == NULL) + return (-1); + + m = map_add(hdr->hdr_lba_table, blocks, (lba == 1) + ? MAP_TYPE_PRI_GPT_TBL : MAP_TYPE_SEC_GPT_TBL, p); + if (m == NULL) + return (-1); + + if (lba != 1) + return (0); + + for (i = 0; i < hdr->hdr_entries; i++) { + uuid_t unused = GPT_ENT_TYPE_UNUSED; + + ent = (void*)(p + i * hdr->hdr_entsz); + if (!memcmp(&ent->ent_type, &unused, sizeof(uuid_t))) + continue; + + size = ent->ent_lba_end - ent->ent_lba_start + 1LL; + + if (verbose > 2) + warnx( + "%s: GPT partition: type=%s, start=%llu, size=%llu", + device_name, uuid_string(&ent->ent_type), + (long long)ent->ent_lba_start, (long long)size); + m = map_add(ent->ent_lba_start, size, MAP_TYPE_GPT_PART, NULL); + if (m == NULL) + return (-1); + } + return (0); + + fail_ent: + free(p); + + fail_hdr: + free(hdr); + return (0); +} + +int +gpt_open(const char *dev) +{ + struct stat sb; + struct mbr *mbr; + map_t *m; + uint32_t size, start; + unsigned int i; + int fd; + + if (!stat(dev, &sb)) { + strlcpy(device_name, dev, sizeof(device_name)); + goto found; + } + + snprintf(device_name, sizeof(device_name), "/dev/%s", dev); + if (!stat(device_name, &sb)) + goto found; + + strlcpy(device_name, dev, sizeof(device_name)); + return (-1); + + found: + fd = open(device_name, (readonly) ? O_RDONLY : O_RDWR|O_EXCL); + if (fd == -1) + return (-1); + + if ((sb.st_mode & S_IFMT) != S_IFREG) { + if (ioctl(fd, DIOCGSECTORSIZE, &secsz) == -1 || + ioctl(fd, DIOCGMEDIASIZE, &mediasz) == -1) + goto close; + } else { + secsz = 512; /* Fixed size for files. */ + if (sb.st_size % secsz) { + errno = EINVAL; + goto close; + } + mediasz = sb.st_size; + } + + /* + * We require an absolute minimum of 6 sectors. One for the MBR, + * 2 for the GPT header, 2 for the GPT table and one to hold some + * user data. Let's catch this extreme border case here so that + * we don't have to worry about it later. + */ + if (mediasz / secsz < 6) { + errno = ENODEV; + goto close; + } + + if (verbose) + warnx("%s: mediasize=%llu; sectorsize=%u; blocks=%llu", + device_name, (long long)mediasz, secsz, + (long long)(mediasz / secsz)); + + map_init(mediasz / secsz); + + /* + * MBR + */ + mbr = gpt_read(fd, 0LL, 1); + if (mbr == NULL) + goto close; + + if (mbr->mbr_sig == MBR_SIG) { + if (verbose > 1) + warnx("%s: MBR at sector 0", device_name); + m = map_add(0LL, 1LL, MAP_TYPE_MBR, mbr); + if (m == NULL) + goto close; + for (i = 0; i < 4; i++) { + start = mbr->mbr_part[i].part_start_hi; + start = (start << 16) + mbr->mbr_part[i].part_start_lo; + size = mbr->mbr_part[i].part_size_hi; + size = (size << 16) + mbr->mbr_part[i].part_size_lo; + if (start != 0 || size != 0) { + if (verbose > 2) + warnx( + "%s: MBR partition: type=%d, start=%llu, size=%llu", device_name, + mbr->mbr_part[i].part_typ, + (long long)start, (long long)size); + if (mbr->mbr_part[i].part_typ != 0xee) { + m = map_add(start, size, + MAP_TYPE_MBR_PART, NULL); + if (m == NULL) + goto close; + } + } + } + } else { + if (verbose) + warnx("%s: MBR not found", device_name); + free(mbr); + } + + /* + * GPT + */ + if (gpt_gpt(fd, 1LL) == -1) + goto close; + if (gpt_gpt(fd, mediasz / secsz - 1LL) == -1) + goto close; + + return (fd); + + close: + close(fd); + return (-1); +} + +void +gpt_close(int fd) +{ + /* XXX post processing? */ + close(fd); +} + +static struct { + int (*fptr)(int, char *[]); + const char *name; +} cmdsw[] = { + { NULL, "add" }, + { NULL, "create" }, + { NULL, "delete" }, + { cmd_destroy, "destroy" }, + { NULL, "help" }, + { cmd_migrate, "migrate" }, + { cmd_recover, "recover" }, + { NULL, "rename" }, + { cmd_show, "show" }, + { NULL, "verify" }, + { NULL, NULL } +}; + +static void +usage(void) +{ + + fprintf(stderr, + "usage: %s [-rv] [-p nparts] command [options] device\n", + getprogname()); + exit(1); +} + +static void +prefix(const char *cmd) +{ + char *pfx; + const char *prg; + + prg = getprogname(); + pfx = malloc(strlen(prg) + strlen(cmd) + 2); + /* Don't bother failing. It's not important */ + if (pfx == NULL) + return; + + sprintf(pfx, "%s %s", prg, cmd); + setprogname(pfx); +} + +int +main(int argc, char *argv[]) +{ + char *cmd, *p; + int ch, i; + + /* Get the generic options */ + while ((ch = getopt(argc, argv, "p:rv")) != -1) { + switch(ch) { + case 'p': + if (parts > 0) + usage(); + parts = strtol(optarg, &p, 10); + if (*p != 0 || parts < 1) + usage(); + break; + case 'r': + readonly = 1; + break; + case 'v': + verbose++; + break; + default: + usage(); + } + } + if (!parts) + parts = 128; + + if (argc == optind) + usage(); + + cmd = argv[optind++]; + for (i = 0; cmdsw[i].name != NULL && strcmp(cmd, cmdsw[i].name); i++); + + if (cmdsw[i].fptr == NULL) + errx(1, "unknown command: %s", cmd); + + prefix(cmd); + return ((*cmdsw[i].fptr)(argc, argv)); +} diff --git a/sbin/gpt/gpt.h b/sbin/gpt/gpt.h new file mode 100644 index 0000000..d6d0b71 --- /dev/null +++ b/sbin/gpt/gpt.h @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _GPT_H_ +#define _GPT_H_ + +struct mbr { + uint16_t mbr_code[223]; + struct { + uint8_t part_flag; /* bootstrap flags */ + uint8_t part_shd; /* starting head */ + uint8_t part_ssect; /* starting sector */ + uint8_t part_scyl; /* starting cylinder */ + uint8_t part_typ; /* partition type */ + uint8_t part_ehd; /* end head */ + uint8_t part_esect; /* end sector */ + uint8_t part_ecyl; /* end cylinder */ + uint16_t part_start_lo; /* absolute starting ... */ + uint16_t part_start_hi; /* ... sector number */ + uint16_t part_size_lo; /* partition size ... */ + uint16_t part_size_hi; /* ... in sectors */ + } mbr_part[4]; + uint16_t mbr_sig; +#define MBR_SIG 0xAA55 +}; + +extern char device_name[]; +extern off_t mediasz; +extern u_int parts; +extern u_int secsz; +extern int readonly, verbose; + +uint32_t crc32(const void *, size_t); +void gpt_close(int); +int gpt_open(const char *); +void* gpt_read(int, off_t, size_t); +int gpt_write(int, map_t *); +void unicode16(short *, const wchar_t *, size_t); + +int cmd_destroy(int, char *[]); +int cmd_migrate(int, char *[]); +int cmd_recover(int, char *[]); +int cmd_show(int, char *[]); + +#endif /* _GPT_H_ */ diff --git a/sbin/gpt/map.c b/sbin/gpt/map.c new file mode 100644 index 0000000..93d0f61 --- /dev/null +++ b/sbin/gpt/map.c @@ -0,0 +1,230 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <err.h> +#include <stdio.h> +#include <stdlib.h> + +#include "map.h" + +static int lbawidth; +static map_t *mediamap; + +static map_t * +mkmap(off_t start, off_t size, int type) +{ + map_t *m; + + m = malloc(sizeof(*m)); + if (m == NULL) + return (NULL); + m->map_start = start; + m->map_size = size; + m->map_next = m->map_prev = NULL; + m->map_type = type; + m->map_data = NULL; + return (m); +} + +map_t * +map_add(off_t start, off_t size, int type, void *data) +{ + map_t *m, *n, *p; + + n = mediamap; + while (n != NULL && n->map_start + n->map_size <= start) + n = n->map_next; + if (n == NULL) + return (NULL); + + if (n->map_start + n->map_size < start + size) { + warnx("error: bogus map"); + return (0); + } + + if (n->map_start == start && n->map_size == size) { + if (n->map_type != MAP_TYPE_UNUSED) + warnx("warning: partition(%llu,%llu) mirrored", + (long long)start, (long long)size); + n->map_type = type; + n->map_data = data; + return (n); + } + + if (n->map_type != MAP_TYPE_UNUSED) { + warnx( + "error: partition(%llu,%llu) overlaps partition(%llu,%llu)", + (long long)start, (long long)size, + (long long)n->map_start, (long long)n->map_size); + return (0); + } + + m = mkmap(start, size, type); + if (m == NULL) + return (NULL); + + m->map_data = data; + + if (start == n->map_start) { + m->map_prev = n->map_prev; + m->map_next = n; + if (m->map_prev != NULL) + m->map_prev->map_next = m; + else + mediamap = m; + n->map_prev = m; + n->map_start += size; + n->map_size -= size; + } else if (start + size == n->map_start + n->map_size) { + p = n; + m->map_next = p->map_next; + m->map_prev = p; + if (m->map_next != NULL) + m->map_next->map_prev = m; + p->map_next = m; + p->map_size -= size; + } else { + p = mkmap(n->map_start, start - n->map_start, n->map_type); + n->map_start += p->map_size + m->map_size; + n->map_size -= (p->map_size + m->map_size); + p->map_prev = n->map_prev; + m->map_prev = p; + n->map_prev = m; + m->map_next = n; + p->map_next = m; + if (p->map_prev != NULL) + p->map_prev->map_next = p; + else + mediamap = p; + } + + return (m); +} + +map_t * +map_find(int type) +{ + map_t *m; + + m = mediamap; + while (m != NULL && m->map_type != type) + m = m->map_next; + return (m); +} + +map_t * +map_first(void) +{ + return mediamap; +} + +map_t * +map_last(void) +{ + map_t *m; + + m = mediamap; + while (m != NULL && m->map_next != NULL) + m = m->map_next; + return (m); +} + +off_t +map_unused(off_t start, off_t size) +{ + map_t *m; + + m = mediamap; + + while (m != NULL && m->map_start + m->map_size <= start) + m = m->map_next; + if (m == NULL || m->map_type != MAP_TYPE_UNUSED) + return (0LL); + if (size) + return ((m->map_start + m->map_size >= start + size) ? 1 : 0); + return (m->map_size - (start - m->map_start)); +} + +void +map_dump(void) +{ + off_t end; + map_t *m; + + printf(" %*s", lbawidth, "start"); + printf(" %*s", lbawidth, "end"); + printf(" %*s", lbawidth, "size"); + printf(" %s\n", "contents"); + + m = mediamap; + while (m != NULL) { + end = m->map_start + m->map_size - 1; + printf(" %*llu", lbawidth, (long long)m->map_start); + printf(" %*llu", lbawidth, (long long)end); + printf(" %*llu", lbawidth, (long long)m->map_size); + + putchar(' '); putchar(' '); + switch (m->map_type) { + case MAP_TYPE_MBR: + printf("MBR"); + break; + case MAP_TYPE_PRI_GPT_HDR: + printf("Pri GPT header"); + break; + case MAP_TYPE_SEC_GPT_HDR: + printf("Sec GPT header"); + break; + case MAP_TYPE_PRI_GPT_TBL: + printf("Pri GPT table"); + break; + case MAP_TYPE_SEC_GPT_TBL: + printf("Sec GPT table"); + break; + case MAP_TYPE_MBR_PART: + printf("MBR partition"); + break; + case MAP_TYPE_GPT_PART: + printf("GPT partition"); + break; + } + putchar('\n'); + m = m->map_next; + } +} + +void +map_init(off_t size) +{ + char buf[32]; + + mediamap = mkmap(0LL, size, MAP_TYPE_UNUSED); + lbawidth = sprintf(buf, "%llu", (long long)size); + if (lbawidth < 5) + lbawidth = 5; +} diff --git a/sbin/gpt/map.h b/sbin/gpt/map.h new file mode 100644 index 0000000..ac726e1 --- /dev/null +++ b/sbin/gpt/map.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _MAP_H_ +#define _MAP_H_ + +typedef struct map { + off_t map_start; + off_t map_size; + struct map *map_next; + struct map *map_prev; + int map_type; +#define MAP_TYPE_UNUSED 0 +#define MAP_TYPE_MBR 1 +#define MAP_TYPE_MBR_PART 2 +#define MAP_TYPE_PRI_GPT_HDR 3 +#define MAP_TYPE_SEC_GPT_HDR 4 +#define MAP_TYPE_PRI_GPT_TBL 5 +#define MAP_TYPE_SEC_GPT_TBL 6 +#define MAP_TYPE_GPT_PART 7 + void *map_data; +} map_t; + +map_t *map_add(off_t, off_t, int, void*); +map_t *map_find(int); +map_t *map_first(void); +map_t *map_last(void); + +off_t map_unused(off_t, off_t); + +void map_dump(void); +void map_init(off_t); + +#endif /* _MAP_H_ */ diff --git a/sbin/gpt/migrate.c b/sbin/gpt/migrate.c new file mode 100644 index 0000000..c27bff6 --- /dev/null +++ b/sbin/gpt/migrate.c @@ -0,0 +1,324 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/disklabel.h> +#include <sys/uuid.h> +#include <sys/gpt.h> + +#include <err.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "map.h" +#include "gpt.h" + +int keep, slice; + +static void +usage_migrate(void) +{ + + fprintf(stderr, + "usage: %s [-ks] device\n", getprogname()); + exit(1); +} + +static struct gpt_ent* +migrate_disklabel(int fd, off_t start, struct gpt_ent *ent) +{ + char *buf; + struct disklabel *dl; + int i; + + buf = gpt_read(fd, start + LABELSECTOR, 1); + dl = (void*)(buf + LABELOFFSET); + + if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC) { + warnx("%s: warning: FreeBSD slice without disklabel", + device_name); + return (ent); + } + + for (i = 0; i < dl->d_npartitions; i++) { + switch (dl->d_partitions[i].p_fstype) { + case FS_SWAP: { + uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP; + ent->ent_type = swap; + unicode16(ent->ent_name, + L"FreeBSD swap partition", 36); + break; + } + case FS_BSDFFS: { + uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS; + ent->ent_type = ufs; + unicode16(ent->ent_name, + L"FreeBSD UFS partition", 36); + break; + } + case FS_VINUM: { + uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM; + ent->ent_type = vinum; + unicode16(ent->ent_name, + L"FreeBSD vinum partition", 36); + break; + } + default: + continue; + } + + ent->ent_lba_start = dl->d_partitions[i].p_offset; + ent->ent_lba_end = ent->ent_lba_start + + dl->d_partitions[i].p_size - 1LL; + ent++; + } + + return (ent); +} + +static void +migrate(int fd) +{ + off_t blocks, last; + map_t *gpt, *tpg; + map_t *tbl, *lbt; + map_t *map; + struct gpt_hdr *hdr; + struct gpt_ent *ent; + struct mbr *mbr; + uint32_t start, size; + unsigned int i; + + last = mediasz / secsz - 1LL; + + map = map_find(MAP_TYPE_MBR); + if (map == NULL || map_find(MAP_TYPE_MBR_PART) == NULL) { + warnx("%s: error: no partitions to convert", + device_name); + return; + } + + mbr = map->map_data; + + if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL || + map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) { + warnx("%s: error: device already contains a GPT", device_name); + return; + } + + /* Get the amount of free space after the MBR */ + blocks = map_unused(1LL, 0LL); + if (blocks == 0LL) { + warnx("%s: error: no room for the GPT header", device_name); + return; + } + + /* Don't create more than parts entries. */ + if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) { + blocks = (parts * sizeof(struct gpt_ent)) / secsz; + if ((parts * sizeof(struct gpt_ent)) % secsz) + blocks++; + blocks++; /* Don't forget the header itself */ + } + + /* Never cross the median of the device. */ + if ((blocks + 1LL) > ((last + 1LL) >> 1)) + blocks = ((last + 1LL) >> 1) - 1LL; + + /* + * Get the amount of free space at the end of the device and + * calculate the size for the GPT structures. + */ + map = map_last(); + if (map->map_type != MAP_TYPE_UNUSED) { + warnx("%s: error: no room for the backup header", device_name); + return; + } + + if (map->map_size < blocks) + blocks = map->map_size; + if (blocks == 1LL) { + warnx("%s: error: no room for the GPT table", device_name); + return; + } + + blocks--; /* Number of blocks in the GPT table. */ + gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz)); + tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL, + calloc(blocks, secsz)); + if (gpt == NULL || tbl == NULL) + return; + + lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL, + tbl->map_data); + tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz)); + + hdr = gpt->map_data; + memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)); + hdr->hdr_revision = GPT_HDR_REVISION; + /* + * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus + * contains padding we must not include in the size. + */ + hdr->hdr_size = offsetof(struct gpt_hdr, padding); + hdr->hdr_lba_self = gpt->map_start; + hdr->hdr_lba_alt = tpg->map_start; + hdr->hdr_lba_start = tbl->map_start + blocks; + hdr->hdr_lba_end = lbt->map_start - 1LL; + uuidgen(&hdr->hdr_uuid, 1); + hdr->hdr_lba_table = tbl->map_start; + hdr->hdr_entries = (blocks * secsz) / sizeof(struct gpt_ent); + if (hdr->hdr_entries > parts) + hdr->hdr_entries = parts; + hdr->hdr_entsz = sizeof(struct gpt_ent); + + ent = tbl->map_data; + for (i = 0; i < hdr->hdr_entries; i++) + uuidgen(&ent[i].ent_uuid, 1); + + /* Mirror partitions. */ + for (i = 0; i < 4; i++) { + start = mbr->mbr_part[i].part_start_hi; + start = (start << 16) + mbr->mbr_part[i].part_start_lo; + size = mbr->mbr_part[i].part_size_hi; + size = (size << 16) + mbr->mbr_part[i].part_size_lo; + + switch (mbr->mbr_part[i].part_typ) { + case 165: { /* FreeBSD */ + if (slice) { + uuid_t freebsd = GPT_ENT_TYPE_FREEBSD; + ent->ent_type = freebsd; + ent->ent_lba_start = start; + ent->ent_lba_end = start + size - 1LL; + unicode16(ent->ent_name, + L"FreeBSD disklabel partition", 36); + ent++; + } else + ent = migrate_disklabel(fd, start, ent); + break; + } + case 239: { /* EFI */ + uuid_t efi_slice = GPT_ENT_TYPE_EFI; + ent->ent_type = efi_slice; + ent->ent_lba_start = start; + ent->ent_lba_end = start + size - 1LL; + unicode16(ent->ent_name, L"EFI system partition", 36); + ent++; + break; + } + default: + continue; + } + } + ent = tbl->map_data; + + hdr->hdr_crc_table = crc32(ent, hdr->hdr_entries * hdr->hdr_entsz); + hdr->hdr_crc_self = crc32(hdr, hdr->hdr_size); + + gpt_write(fd, gpt); + gpt_write(fd, tbl); + + /* + * Create backup GPT. + */ + memcpy(tpg->map_data, gpt->map_data, secsz); + hdr = tpg->map_data; + hdr->hdr_lba_self = tpg->map_start; + hdr->hdr_lba_alt = gpt->map_start; + hdr->hdr_lba_table = lbt->map_start; + hdr->hdr_crc_self = 0; /* Don't ever forget this! */ + hdr->hdr_crc_self = crc32(hdr, hdr->hdr_size); + + gpt_write(fd, lbt); + gpt_write(fd, tpg); + + if (!keep) { + map = map_find(MAP_TYPE_MBR); + mbr = map->map_data; + /* + * Turn the MBR into a Protective MBR. + */ + bzero(mbr->mbr_part, sizeof(mbr->mbr_part)); + mbr->mbr_part[0].part_shd = 0xff; + mbr->mbr_part[0].part_ssect = 0xff; + mbr->mbr_part[0].part_scyl = 0xff; + mbr->mbr_part[0].part_typ = 0xee; + mbr->mbr_part[0].part_ehd = 0xff; + mbr->mbr_part[0].part_esect = 0xff; + mbr->mbr_part[0].part_ecyl = 0xff; + mbr->mbr_part[0].part_start_lo = 1; + if (mediasz > 0xffffffff) { + mbr->mbr_part[0].part_size_lo = 0xffff; + mbr->mbr_part[0].part_size_hi = 0xffff; + } else { + mbr->mbr_part[0].part_size_lo = mediasz & 0xffff; + mbr->mbr_part[0].part_size_hi = mediasz >> 16; + } + gpt_write(fd, map); + } +} + +int +cmd_migrate(int argc, char *argv[]) +{ + int ch, fd; + + /* Get the migrate options */ + while ((ch = getopt(argc, argv, "ks")) != -1) { + switch(ch) { + case 'k': + keep = 1; + break; + case 's': + slice = 1; + break; + default: + usage_migrate(); + } + } + + if (argc == optind) + usage_migrate(); + + while (optind < argc) { + fd = gpt_open(argv[optind++]); + if (fd == -1) { + warn("unable to open device '%s'", device_name); + continue; + } + + migrate(fd); + + gpt_close(fd); + } + + return (0); +} diff --git a/sbin/gpt/mkdisk.sh b/sbin/gpt/mkdisk.sh new file mode 100644 index 0000000..ebac19a --- /dev/null +++ b/sbin/gpt/mkdisk.sh @@ -0,0 +1,11 @@ +#!/bin/sh +# $FreeBSD$ + +dd if=/dev/zero of=disk count=125307 +sudo mdconfig -a -t vnode -f disk -u 4 +sudo fdisk -f - md4 <<EOF +g c567 h13 s17 +p 1 165 221 124865 +EOF +sudo disklabel -w -r md4 pan60 +sudo mdconfig -d -u 4 diff --git a/sbin/gpt/recover.c b/sbin/gpt/recover.c new file mode 100644 index 0000000..65f40a7 --- /dev/null +++ b/sbin/gpt/recover.c @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/uuid.h> +#include <sys/gpt.h> + +#include <err.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "map.h" +#include "gpt.h" + +int recoverable; + +static void +usage_recover(void) +{ + + fprintf(stderr, + "usage: %s device ...\n", getprogname()); + exit(1); +} + +static void +recover(int fd) +{ + + /* TODO */ +} + +int +cmd_recover(int argc, char *argv[]) +{ + int ch, fd; + + while ((ch = getopt(argc, argv, "r")) != -1) { + switch(ch) { + case 'r': + recoverable = 1; + break; + default: + usage_recover(); + } + } + + if (argc == optind) + usage_recover(); + + while (optind < argc) { + fd = gpt_open(argv[optind++]); + if (fd == -1) { + warn("unable to open device '%s'", device_name); + continue; + } + + recover(fd); + + gpt_close(fd); + } + + return (0); +} diff --git a/sbin/gpt/show.c b/sbin/gpt/show.c new file mode 100644 index 0000000..0eff2d3 --- /dev/null +++ b/sbin/gpt/show.c @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2002 Marcel Moolenaar + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/uuid.h> +#include <sys/gpt.h> + +#include <err.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include "map.h" +#include "gpt.h" + +static void +usage_show(void) +{ + + fprintf(stderr, + "usage: %s device ...\n", getprogname()); + exit(1); +} + +int +cmd_show(int argc, char *argv[]) +{ + int ch, fd; + + while ((ch = getopt(argc, argv, "")) != -1) { + switch(ch) { + default: + usage_show(); + } + } + + if (argc == optind) + usage_show(); + + while (optind < argc) { + fd = gpt_open(argv[optind++]); + if (fd == -1) { + warn("unable to open device '%s'", device_name); + continue; + } + + map_dump(); + + gpt_close(fd); + } + + return (0); +} |