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