Home | History | Annotate | Line # | Download | only in gpt
add.c revision 1.10
      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.1  christos #include <sys/cdefs.h>
     28   1.2  christos #ifdef __FBSDID
     29   1.2  christos __FBSDID("$FreeBSD: src/sbin/gpt/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $");
     30   1.2  christos #endif
     31   1.2  christos #ifdef __RCSID
     32  1.10       riz __RCSID("$NetBSD: add.c,v 1.10 2011/01/06 17:51:28 riz Exp $");
     33   1.2  christos #endif
     34   1.1  christos 
     35   1.1  christos #include <sys/types.h>
     36   1.1  christos 
     37   1.1  christos #include <err.h>
     38   1.1  christos #include <stddef.h>
     39   1.1  christos #include <stdio.h>
     40   1.1  christos #include <stdlib.h>
     41   1.1  christos #include <string.h>
     42   1.1  christos #include <unistd.h>
     43   1.2  christos #include <inttypes.h>
     44   1.1  christos 
     45   1.1  christos #include "map.h"
     46   1.1  christos #include "gpt.h"
     47   1.1  christos 
     48   1.1  christos static uuid_t type;
     49   1.1  christos static off_t block, size;
     50   1.1  christos static unsigned int entry;
     51   1.1  christos 
     52   1.7  uebayasi const char addmsg[] = "add [-b lba] [-i index] [-s lba] [-t type] "
     53   1.5       riz 	"device ...";
     54   1.5       riz 
     55   1.1  christos static void
     56   1.1  christos usage_add(void)
     57   1.1  christos {
     58   1.1  christos 
     59   1.1  christos 	fprintf(stderr,
     60   1.5       riz 	    "usage: %s %s\n",
     61   1.5       riz 	    getprogname(), addmsg);
     62   1.1  christos 	exit(1);
     63   1.1  christos }
     64   1.1  christos 
     65   1.1  christos static void
     66   1.1  christos add(int fd)
     67   1.1  christos {
     68   1.1  christos 	map_t *gpt, *tpg;
     69   1.1  christos 	map_t *tbl, *lbt;
     70   1.1  christos 	map_t *map;
     71   1.1  christos 	struct gpt_hdr *hdr;
     72   1.1  christos 	struct gpt_ent *ent;
     73   1.1  christos 	unsigned int i;
     74   1.1  christos 
     75   1.1  christos 	gpt = map_find(MAP_TYPE_PRI_GPT_HDR);
     76   1.3        he 	ent = NULL;
     77   1.1  christos 	if (gpt == NULL) {
     78   1.1  christos 		warnx("%s: error: no primary GPT header; run create or recover",
     79   1.1  christos 		    device_name);
     80   1.1  christos 		return;
     81   1.1  christos 	}
     82   1.1  christos 
     83   1.1  christos 	tpg = map_find(MAP_TYPE_SEC_GPT_HDR);
     84   1.1  christos 	if (tpg == NULL) {
     85   1.1  christos 		warnx("%s: error: no secondary GPT header; run recover",
     86   1.1  christos 		    device_name);
     87   1.1  christos 		return;
     88   1.1  christos 	}
     89   1.1  christos 
     90   1.1  christos 	tbl = map_find(MAP_TYPE_PRI_GPT_TBL);
     91   1.1  christos 	lbt = map_find(MAP_TYPE_SEC_GPT_TBL);
     92   1.1  christos 	if (tbl == NULL || lbt == NULL) {
     93   1.1  christos 		warnx("%s: error: run recover -- trust me", device_name);
     94   1.1  christos 		return;
     95   1.1  christos 	}
     96   1.1  christos 
     97   1.1  christos 	hdr = gpt->map_data;
     98   1.1  christos 	if (entry > le32toh(hdr->hdr_entries)) {
     99   1.1  christos 		warnx("%s: error: index %u out of range (%u max)", device_name,
    100   1.1  christos 		    entry, le32toh(hdr->hdr_entries));
    101   1.1  christos 		return;
    102   1.1  christos 	}
    103   1.1  christos 
    104   1.1  christos 	if (entry > 0) {
    105   1.1  christos 		i = entry - 1;
    106   1.1  christos 		ent = (void*)((char*)tbl->map_data + i *
    107   1.1  christos 		    le32toh(hdr->hdr_entsz));
    108   1.2  christos 		if (!uuid_is_nil((uuid_t *)&ent->ent_type, NULL)) {
    109   1.1  christos 			warnx("%s: error: entry at index %u is not free",
    110   1.1  christos 			    device_name, entry);
    111   1.1  christos 			return;
    112   1.1  christos 		}
    113   1.1  christos 	} else {
    114   1.1  christos 		/* Find empty slot in GPT table. */
    115   1.1  christos 		for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
    116   1.1  christos 			ent = (void*)((char*)tbl->map_data + i *
    117   1.1  christos 			    le32toh(hdr->hdr_entsz));
    118   1.2  christos 			if (uuid_is_nil((uuid_t *)&ent->ent_type, NULL))
    119   1.1  christos 				break;
    120   1.1  christos 		}
    121   1.1  christos 		if (i == le32toh(hdr->hdr_entries)) {
    122   1.1  christos 			warnx("%s: error: no available table entries",
    123   1.1  christos 			    device_name);
    124   1.1  christos 			return;
    125   1.1  christos 		}
    126   1.1  christos 	}
    127   1.1  christos 
    128   1.1  christos 	map = map_alloc(block, size);
    129   1.1  christos 	if (map == NULL) {
    130   1.1  christos 		warnx("%s: error: no space available on device", device_name);
    131   1.1  christos 		return;
    132   1.1  christos 	}
    133   1.1  christos 
    134   1.2  christos 	le_uuid_enc((uuid_t *)&ent->ent_type, &type);
    135   1.1  christos 	ent->ent_lba_start = htole64(map->map_start);
    136   1.1  christos 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
    137   1.1  christos 
    138   1.1  christos 	hdr->hdr_crc_table = htole32(crc32(tbl->map_data,
    139   1.1  christos 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    140   1.1  christos 	hdr->hdr_crc_self = 0;
    141   1.1  christos 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    142   1.1  christos 
    143   1.1  christos 	gpt_write(fd, gpt);
    144   1.1  christos 	gpt_write(fd, tbl);
    145   1.1  christos 
    146   1.1  christos 	hdr = tpg->map_data;
    147   1.1  christos 	ent = (void*)((char*)lbt->map_data + i * le32toh(hdr->hdr_entsz));
    148   1.1  christos 
    149   1.1  christos 	le_uuid_enc(&ent->ent_type, &type);
    150   1.1  christos 	ent->ent_lba_start = htole64(map->map_start);
    151   1.1  christos 	ent->ent_lba_end = htole64(map->map_start + map->map_size - 1LL);
    152   1.1  christos 
    153   1.1  christos 	hdr->hdr_crc_table = htole32(crc32(lbt->map_data,
    154   1.1  christos 	    le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz)));
    155   1.1  christos 	hdr->hdr_crc_self = 0;
    156   1.1  christos 	hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
    157   1.1  christos 
    158   1.1  christos 	gpt_write(fd, lbt);
    159   1.1  christos 	gpt_write(fd, tpg);
    160   1.1  christos 
    161   1.2  christos #ifdef __FreeBSD__
    162   1.1  christos 	printf("%sp%u added\n", device_name, i + 1);
    163   1.2  christos #endif
    164   1.2  christos #ifdef __NetBSD__
    165   1.2  christos 	printf("Partition added, use:\n");
    166  1.10       riz 	printf("\tdkctl %s addwedge <wedgename> %" PRIu64 " %" PRIu64
    167  1.10       riz 	    " <type>\n", device_arg, map->map_start, map->map_size);
    168   1.2  christos 	printf("to create a wedge for it\n");
    169   1.2  christos #endif
    170   1.1  christos }
    171   1.1  christos 
    172   1.1  christos int
    173   1.1  christos cmd_add(int argc, char *argv[])
    174   1.1  christos {
    175   1.1  christos 	char *p;
    176   1.1  christos 	int ch, fd;
    177   1.1  christos 
    178   1.1  christos 	/* Get the migrate options */
    179   1.1  christos 	while ((ch = getopt(argc, argv, "b:i:s:t:")) != -1) {
    180   1.1  christos 		switch(ch) {
    181   1.1  christos 		case 'b':
    182   1.1  christos 			if (block > 0)
    183   1.1  christos 				usage_add();
    184   1.4       riz 			block = strtoll(optarg, &p, 10);
    185   1.1  christos 			if (*p != 0 || block < 1)
    186   1.1  christos 				usage_add();
    187   1.1  christos 			break;
    188   1.1  christos 		case 'i':
    189   1.1  christos 			if (entry > 0)
    190   1.1  christos 				usage_add();
    191   1.4       riz 			entry = strtoul(optarg, &p, 10);
    192   1.1  christos 			if (*p != 0 || entry < 1)
    193   1.1  christos 				usage_add();
    194   1.1  christos 			break;
    195   1.1  christos 		case 's':
    196   1.1  christos 			if (size > 0)
    197   1.1  christos 				usage_add();
    198   1.4       riz 			size = strtoll(optarg, &p, 10);
    199   1.1  christos 			if (*p != 0 || size < 1)
    200   1.1  christos 				usage_add();
    201   1.1  christos 			break;
    202   1.1  christos 		case 't':
    203   1.1  christos 			if (!uuid_is_nil(&type, NULL))
    204   1.1  christos 				usage_add();
    205   1.1  christos 			if (parse_uuid(optarg, &type) != 0)
    206   1.1  christos 				usage_add();
    207   1.1  christos 			break;
    208   1.1  christos 		default:
    209   1.1  christos 			usage_add();
    210   1.1  christos 		}
    211   1.1  christos 	}
    212   1.1  christos 
    213   1.1  christos 	if (argc == optind)
    214   1.1  christos 		usage_add();
    215   1.1  christos 
    216   1.9  jakllsch 	/* Create NetBSD FFS partitions by default. */
    217   1.1  christos 	if (uuid_is_nil(&type, NULL)) {
    218   1.9  jakllsch 		uuid_t nb_ffs = GPT_ENT_TYPE_NETBSD_FFS;
    219   1.9  jakllsch 		type = nb_ffs;
    220   1.1  christos 	}
    221   1.1  christos 
    222   1.1  christos 	while (optind < argc) {
    223   1.1  christos 		fd = gpt_open(argv[optind++]);
    224   1.1  christos 		if (fd == -1) {
    225   1.1  christos 			warn("unable to open device '%s'", device_name);
    226   1.1  christos 			continue;
    227   1.1  christos 		}
    228   1.1  christos 
    229   1.1  christos 		add(fd);
    230   1.1  christos 
    231   1.1  christos 		gpt_close(fd);
    232   1.1  christos 	}
    233   1.1  christos 
    234   1.1  christos 	return (0);
    235   1.1  christos }
    236