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