Home | History | Annotate | Line # | Download | only in gpt
create.c revision 1.8
      1 /*-
      2  * Copyright (c) 2002 Marcel Moolenaar
      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  *
      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.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #if HAVE_NBTOOL_CONFIG_H
     28 #include "nbtool_config.h"
     29 #endif
     30 
     31 #include <sys/cdefs.h>
     32 #ifdef __FBSDID
     33 __FBSDID("$FreeBSD: src/sbin/gpt/create.c,v 1.11 2005/08/31 01:47:19 marcel Exp $");
     34 #endif
     35 #ifdef __RCSID
     36 __RCSID("$NetBSD: create.c,v 1.8 2014/09/29 20:28:57 christos Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 #include <sys/bootblock.h>
     41 
     42 #include <err.h>
     43 #include <stddef.h>
     44 #include <stdio.h>
     45 #include <stdlib.h>
     46 #include <string.h>
     47 #include <unistd.h>
     48 
     49 #include "map.h"
     50 #include "gpt.h"
     51 
     52 static int force;
     53 static int primary_only;
     54 
     55 const char createmsg[] = "create [-fp] device ...";
     56 
     57 __dead static void
     58 usage_create(void)
     59 {
     60 
     61 	fprintf(stderr,
     62 	    "usage: %s %s\n", getprogname(), createmsg);
     63 	exit(1);
     64 }
     65 
     66 static void
     67 create(int fd)
     68 {
     69 	uuid_t uuid;
     70 	off_t blocks, last;
     71 	map_t *gpt, *tpg;
     72 	map_t *tbl, *lbt;
     73 	map_t *map;
     74 	struct mbr *mbr;
     75 	struct gpt_hdr *hdr;
     76 	struct gpt_ent *ent;
     77 	unsigned int i;
     78 
     79 	last = mediasz / secsz - 1LL;
     80 
     81 	if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
     82 	    map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
     83 		warnx("%s: error: device already contains a GPT", device_name);
     84 		return;
     85 	}
     86 	map = map_find(MAP_TYPE_MBR);
     87 	if (map != NULL) {
     88 		if (!force) {
     89 			warnx("%s: error: device contains a MBR", device_name);
     90 			return;
     91 		}
     92 
     93 		/* Nuke the MBR in our internal map. */
     94 		map->map_type = MAP_TYPE_UNUSED;
     95 	}
     96 
     97 	/*
     98 	 * Create PMBR.
     99 	 */
    100 	if (map_find(MAP_TYPE_PMBR) == NULL) {
    101 		if (map_free(0LL, 1LL) == 0) {
    102 			warnx("%s: error: no room for the PMBR", device_name);
    103 			return;
    104 		}
    105 		mbr = gpt_read(fd, 0LL, 1);
    106 		memset(mbr, 0, sizeof(*mbr));
    107 		mbr->mbr_sig = htole16(MBR_SIG);
    108 		mbr->mbr_part[0].part_shd = 0x00;
    109 		mbr->mbr_part[0].part_ssect = 0x02;
    110 		mbr->mbr_part[0].part_scyl = 0x00;
    111 		mbr->mbr_part[0].part_typ = MBR_PTYPE_PMBR;
    112 		mbr->mbr_part[0].part_ehd = 0xfe;
    113 		mbr->mbr_part[0].part_esect = 0xff;
    114 		mbr->mbr_part[0].part_ecyl = 0xff;
    115 		mbr->mbr_part[0].part_start_lo = htole16(1);
    116 		if (last > 0xffffffff) {
    117 			mbr->mbr_part[0].part_size_lo = htole16(0xffff);
    118 			mbr->mbr_part[0].part_size_hi = htole16(0xffff);
    119 		} else {
    120 			mbr->mbr_part[0].part_size_lo = htole16(last);
    121 			mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
    122 		}
    123 		map = map_add(0LL, 1LL, MAP_TYPE_PMBR, mbr);
    124 		gpt_write(fd, map);
    125 	}
    126 
    127 	/* Get the amount of free space after the MBR */
    128 	blocks = map_free(1LL, 0LL);
    129 	if (blocks == 0LL) {
    130 		warnx("%s: error: no room for the GPT header", device_name);
    131 		return;
    132 	}
    133 
    134 	/* Don't create more than parts entries. */
    135 	if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
    136 		blocks = (parts * sizeof(struct gpt_ent)) / secsz;
    137 		if ((parts * sizeof(struct gpt_ent)) % secsz)
    138 			blocks++;
    139 		blocks++;		/* Don't forget the header itself */
    140 	}
    141 
    142 	/* Never cross the median of the device. */
    143 	if ((blocks + 1LL) > ((last + 1LL) >> 1))
    144 		blocks = ((last + 1LL) >> 1) - 1LL;
    145 
    146 	/*
    147 	 * Get the amount of free space at the end of the device and
    148 	 * calculate the size for the GPT structures.
    149 	 */
    150 	map = map_last();
    151 	if (map->map_type != MAP_TYPE_UNUSED) {
    152 		warnx("%s: error: no room for the backup header", device_name);
    153 		return;
    154 	}
    155 
    156 	if (map->map_size < blocks)
    157 		blocks = map->map_size;
    158 	if (blocks == 1LL) {
    159 		warnx("%s: error: no room for the GPT table", device_name);
    160 		return;
    161 	}
    162 
    163 	blocks--;		/* Number of blocks in the GPT table. */
    164 	gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
    165 	tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
    166 	    calloc(blocks, secsz));
    167 	if (gpt == NULL || tbl == NULL)
    168 		return;
    169 
    170 	hdr = gpt->map_data;
    171 	memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
    172 	hdr->hdr_revision = htole32(GPT_HDR_REVISION);
    173 	hdr->hdr_size = htole32(GPT_SIZE);
    174 	hdr->hdr_lba_self = htole64(gpt->map_start);
    175 	hdr->hdr_lba_alt = htole64(last);
    176 	hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
    177 	hdr->hdr_lba_end = htole64(last - blocks - 1LL);
    178 	uuid_create(&uuid, NULL);
    179 	le_uuid_enc(hdr->hdr_uuid, &uuid);
    180 	hdr->hdr_lba_table = htole64(tbl->map_start);
    181 	hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
    182 	if (le32toh(hdr->hdr_entries) > parts)
    183 		hdr->hdr_entries = htole32(parts);
    184 	hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
    185 
    186 	ent = tbl->map_data;
    187 	for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    188 		uuid_create(&uuid, NULL);
    189 		le_uuid_enc(ent[i].ent_uuid, &uuid);
    190 	}
    191 
    192 	hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
    193 	    le32toh(hdr->hdr_entsz)));
    194 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    195 
    196 	gpt_write(fd, gpt);
    197 	gpt_write(fd, tbl);
    198 
    199 	/*
    200 	 * Create backup GPT if the user didn't suppress it.
    201 	 */
    202 	if (!primary_only) {
    203 		tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR,
    204 		    calloc(1, secsz));
    205 		lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
    206 		    tbl->map_data);
    207 		memcpy(tpg->map_data, gpt->map_data, secsz);
    208 		hdr = tpg->map_data;
    209 		hdr->hdr_lba_self = htole64(tpg->map_start);
    210 		hdr->hdr_lba_alt = htole64(gpt->map_start);
    211 		hdr->hdr_lba_table = htole64(lbt->map_start);
    212 		hdr->hdr_crc_self = 0;		/* Don't ever forget this! */
    213 		hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    214 		gpt_write(fd, lbt);
    215 		gpt_write(fd, tpg);
    216 	}
    217 }
    218 
    219 int
    220 cmd_create(int argc, char *argv[])
    221 {
    222 	int ch, fd;
    223 
    224 	while ((ch = getopt(argc, argv, "fp")) != -1) {
    225 		switch(ch) {
    226 		case 'f':
    227 			force = 1;
    228 			break;
    229 		case 'p':
    230 			primary_only = 1;
    231 			break;
    232 		default:
    233 			usage_create();
    234 		}
    235 	}
    236 
    237 	if (argc == optind)
    238 		usage_create();
    239 
    240 	while (optind < argc) {
    241 		fd = gpt_open(argv[optind++]);
    242 		if (fd == -1) {
    243 			warn("unable to open device '%s'", device_name);
    244 			continue;
    245 		}
    246 
    247 		create(fd);
    248 
    249 		gpt_close(fd);
    250 	}
    251 
    252 	return (0);
    253 }
    254