Home | History | Annotate | Line # | Download | only in gpt
create.c revision 1.15
      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.15  christos __RCSID("$NetBSD: create.c,v 1.15 2015/12/01 16:32:19 christos Exp $");
     37   1.2  christos #endif
     38   1.1  christos 
     39   1.1  christos #include <sys/types.h>
     40  1.14  christos #include <sys/param.h>
     41  1.14  christos #include <sys/stat.h>
     42   1.7  jakllsch #include <sys/bootblock.h>
     43   1.1  christos 
     44   1.1  christos #include <err.h>
     45   1.1  christos #include <stddef.h>
     46   1.1  christos #include <stdio.h>
     47   1.1  christos #include <stdlib.h>
     48   1.1  christos #include <string.h>
     49   1.1  christos #include <unistd.h>
     50   1.1  christos 
     51   1.1  christos #include "map.h"
     52   1.1  christos #include "gpt.h"
     53  1.14  christos #include "gpt_private.h"
     54   1.1  christos 
     55   1.1  christos static int force;
     56  1.14  christos static u_int parts;
     57   1.1  christos static int primary_only;
     58  1.15  christos static int cmd_create(gpt_t, int, char *[]);
     59   1.1  christos 
     60  1.15  christos static const char *createhelp[] = {
     61  1.15  christos     "[-fP] [-p <partitions>]",
     62  1.15  christos };
     63  1.15  christos 
     64  1.15  christos struct gpt_cmd c_create = {
     65  1.15  christos 	"create",
     66  1.15  christos 	cmd_create,
     67  1.15  christos 	createhelp, __arraycount(createhelp),
     68  1.15  christos 	0,
     69  1.15  christos };
     70   1.3       riz 
     71  1.15  christos #define usage() gpt_usage(NULL, &c_create)
     72   1.1  christos 
     73   1.1  christos 
     74  1.14  christos static int
     75  1.14  christos create(gpt_t gpt)
     76   1.1  christos {
     77   1.1  christos 	off_t blocks, last;
     78  1.14  christos 	map_t map;
     79   1.1  christos 	struct mbr *mbr;
     80   1.1  christos 	struct gpt_hdr *hdr;
     81   1.1  christos 	struct gpt_ent *ent;
     82   1.1  christos 	unsigned int i;
     83  1.14  christos 	void *p;
     84   1.1  christos 
     85  1.14  christos 	last = gpt->mediasz / gpt->secsz - 1LL;
     86   1.1  christos 
     87  1.14  christos 	if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) != NULL ||
     88  1.14  christos 	    map_find(gpt, MAP_TYPE_SEC_GPT_HDR) != NULL) {
     89  1.14  christos 		gpt_warnx(gpt, "Device already contains a GPT");
     90  1.14  christos 		return -1;
     91   1.1  christos 	}
     92  1.14  christos 	map = map_find(gpt, MAP_TYPE_MBR);
     93   1.1  christos 	if (map != NULL) {
     94   1.1  christos 		if (!force) {
     95  1.14  christos 			gpt_warnx(gpt, "Device contains a MBR");
     96  1.14  christos 			return -1;
     97   1.1  christos 		}
     98   1.1  christos 		/* Nuke the MBR in our internal map. */
     99   1.1  christos 		map->map_type = MAP_TYPE_UNUSED;
    100   1.1  christos 	}
    101   1.1  christos 
    102   1.1  christos 	/*
    103   1.1  christos 	 * Create PMBR.
    104   1.1  christos 	 */
    105  1.14  christos 	if (map_find(gpt, MAP_TYPE_PMBR) == NULL) {
    106  1.14  christos 		if (map_free(gpt, 0LL, 1LL) == 0) {
    107  1.14  christos 			gpt_warnx(gpt, "No room for the PMBR");
    108  1.14  christos 			return -1;
    109  1.14  christos 		}
    110  1.14  christos 		mbr = gpt_read(gpt, 0LL, 1);
    111  1.14  christos 		if (mbr == NULL) {
    112  1.14  christos 			gpt_warnx(gpt, "Error reading MBR");
    113  1.14  christos 			return -1;
    114   1.1  christos 		}
    115   1.8  christos 		memset(mbr, 0, sizeof(*mbr));
    116   1.1  christos 		mbr->mbr_sig = htole16(MBR_SIG);
    117  1.14  christos 		gpt_create_pmbr_part(mbr->mbr_part, last);
    118  1.14  christos 
    119  1.14  christos 		map = map_add(gpt, 0LL, 1LL, MAP_TYPE_PMBR, mbr);
    120  1.14  christos 		gpt_write(gpt, map);
    121   1.1  christos 	}
    122   1.1  christos 
    123   1.1  christos 	/* Get the amount of free space after the MBR */
    124  1.14  christos 	blocks = map_free(gpt, 1LL, 0LL);
    125   1.1  christos 	if (blocks == 0LL) {
    126  1.14  christos 		gpt_warnx(gpt, "No room for the GPT header");
    127  1.14  christos 		return -1;
    128   1.1  christos 	}
    129   1.1  christos 
    130   1.1  christos 	/* Don't create more than parts entries. */
    131  1.14  christos 	if ((uint64_t)(blocks - 1) * gpt->secsz >
    132  1.14  christos 	    parts * sizeof(struct gpt_ent)) {
    133  1.14  christos 		blocks = (parts * sizeof(struct gpt_ent)) / gpt->secsz;
    134  1.14  christos 		if ((parts * sizeof(struct gpt_ent)) % gpt->secsz)
    135   1.1  christos 			blocks++;
    136   1.1  christos 		blocks++;		/* Don't forget the header itself */
    137   1.1  christos 	}
    138   1.1  christos 
    139   1.1  christos 	/* Never cross the median of the device. */
    140   1.1  christos 	if ((blocks + 1LL) > ((last + 1LL) >> 1))
    141   1.1  christos 		blocks = ((last + 1LL) >> 1) - 1LL;
    142   1.1  christos 
    143   1.1  christos 	/*
    144   1.1  christos 	 * Get the amount of free space at the end of the device and
    145   1.1  christos 	 * calculate the size for the GPT structures.
    146   1.1  christos 	 */
    147  1.14  christos 	map = map_last(gpt);
    148   1.1  christos 	if (map->map_type != MAP_TYPE_UNUSED) {
    149  1.14  christos 		gpt_warnx(gpt, "No room for the backup header");
    150  1.14  christos 		return -1;
    151   1.1  christos 	}
    152   1.1  christos 
    153   1.1  christos 	if (map->map_size < blocks)
    154   1.1  christos 		blocks = map->map_size;
    155   1.1  christos 	if (blocks == 1LL) {
    156  1.14  christos 		gpt_warnx(gpt, "No room for the GPT table");
    157  1.14  christos 		return -1;
    158  1.14  christos 	}
    159  1.14  christos 
    160  1.14  christos 	if ((p = calloc(1, gpt->secsz)) == NULL) {
    161  1.14  christos 		gpt_warnx(gpt, "Can't allocate the GPT");
    162  1.14  christos 		return -1;
    163  1.14  christos 	}
    164  1.14  christos 	if ((gpt->gpt = map_add(gpt, 1LL, 1LL,
    165  1.14  christos 	    MAP_TYPE_PRI_GPT_HDR, p)) == NULL) {
    166  1.14  christos 		free(p);
    167  1.14  christos 		gpt_warnx(gpt, "Can't add the GPT");
    168  1.14  christos 		return -1;
    169   1.1  christos 	}
    170   1.1  christos 
    171   1.1  christos 	blocks--;		/* Number of blocks in the GPT table. */
    172  1.14  christos 	if ((p = calloc(blocks, gpt->secsz)) == NULL) {
    173  1.14  christos 		gpt_warnx(gpt, "Can't allocate the GPT table");
    174  1.14  christos 		return -1;
    175  1.14  christos 	}
    176  1.14  christos 	if ((gpt->tbl = map_add(gpt, 2LL, blocks,
    177  1.14  christos 	    MAP_TYPE_PRI_GPT_TBL, p)) == NULL) {
    178  1.14  christos 		free(p);
    179  1.14  christos 		gpt_warnx(gpt, "Can't add the GPT table");
    180  1.14  christos 		return -1;
    181  1.14  christos 	}
    182   1.1  christos 
    183  1.14  christos 	hdr = gpt->gpt->map_data;
    184   1.1  christos 	memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
    185   1.1  christos 	hdr->hdr_revision = htole32(GPT_HDR_REVISION);
    186   1.9  christos 	hdr->hdr_size = htole32(GPT_HDR_SIZE);
    187  1.14  christos 	hdr->hdr_lba_self = htole64(gpt->gpt->map_start);
    188   1.1  christos 	hdr->hdr_lba_alt = htole64(last);
    189  1.14  christos 	hdr->hdr_lba_start = htole64(gpt->tbl->map_start + blocks);
    190   1.1  christos 	hdr->hdr_lba_end = htole64(last - blocks - 1LL);
    191  1.12  christos 	gpt_uuid_generate(hdr->hdr_guid);
    192  1.14  christos 	hdr->hdr_lba_table = htole64(gpt->tbl->map_start);
    193  1.14  christos 	hdr->hdr_entries = htole32((blocks * gpt->secsz) /
    194  1.14  christos 	    sizeof(struct gpt_ent));
    195   1.1  christos 	if (le32toh(hdr->hdr_entries) > parts)
    196   1.1  christos 		hdr->hdr_entries = htole32(parts);
    197   1.1  christos 	hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
    198   1.1  christos 
    199  1.14  christos 	ent = gpt->tbl->map_data;
    200   1.1  christos 	for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    201  1.12  christos 		gpt_uuid_generate(ent[i].ent_guid);
    202   1.1  christos 	}
    203   1.1  christos 
    204  1.14  christos 	if (gpt_write_primary(gpt) == -1)
    205  1.14  christos 		return -1;
    206   1.1  christos 
    207   1.1  christos 	/*
    208   1.1  christos 	 * Create backup GPT if the user didn't suppress it.
    209   1.1  christos 	 */
    210   1.1  christos 	if (!primary_only) {
    211  1.14  christos 		// XXX: error checks
    212  1.14  christos 		gpt->tpg = map_add(gpt, last, 1LL, MAP_TYPE_SEC_GPT_HDR,
    213  1.14  christos 		    calloc(1, gpt->secsz));
    214  1.14  christos 		gpt->lbt = map_add(gpt, last - blocks, blocks,
    215  1.14  christos 		    MAP_TYPE_SEC_GPT_TBL, gpt->tbl->map_data);
    216  1.14  christos 		memcpy(gpt->tpg->map_data, gpt->gpt->map_data, gpt->secsz);
    217  1.14  christos 		hdr = gpt->tpg->map_data;
    218  1.14  christos 		hdr->hdr_lba_self = htole64(gpt->tpg->map_start);
    219  1.14  christos 		hdr->hdr_lba_alt = htole64(gpt->gpt->map_start);
    220  1.14  christos 		hdr->hdr_lba_table = htole64(gpt->lbt->map_start);
    221  1.14  christos 		if (gpt_write_backup(gpt) == -1)
    222  1.14  christos 			return -1;
    223   1.1  christos 	}
    224  1.14  christos 	return 0;
    225   1.1  christos }
    226   1.1  christos 
    227  1.15  christos static int
    228  1.14  christos cmd_create(gpt_t gpt, int argc, char *argv[])
    229   1.1  christos {
    230  1.14  christos 	int ch;
    231   1.1  christos 
    232  1.14  christos 	parts = 128;
    233  1.14  christos 
    234  1.14  christos 	while ((ch = getopt(argc, argv, "fPp:")) != -1) {
    235   1.1  christos 		switch(ch) {
    236   1.1  christos 		case 'f':
    237   1.1  christos 			force = 1;
    238   1.1  christos 			break;
    239  1.14  christos 		case 'P':
    240  1.14  christos 			primary_only = 1;
    241  1.14  christos 			break;
    242   1.1  christos 		case 'p':
    243  1.14  christos 			parts = atoi(optarg);
    244   1.1  christos 			break;
    245   1.1  christos 		default:
    246  1.15  christos 			return usage();
    247   1.1  christos 		}
    248   1.1  christos 	}
    249   1.1  christos 
    250  1.14  christos 	if (argc != optind)
    251  1.15  christos 		return usage();
    252   1.1  christos 
    253  1.14  christos 	return create(gpt);
    254   1.1  christos }
    255