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: restore.c,v 1.22 2025/12/17 15:56:06 nia Exp $"); 37 #endif 38 39 #include <sys/types.h> 40 #if defined(HAVE_SYS_ENDIAN_H) || ! defined(HAVE_NBTOOL_CONFIG_H) 41 #include <sys/endian.h> 42 #endif 43 #include <sys/bootblock.h> 44 #include <sys/disklabel_gpt.h> 45 46 #include <err.h> 47 #include <stddef.h> 48 #include <stdio.h> 49 #include <stdlib.h> 50 #include <string.h> 51 #include <unistd.h> 52 #include <prop/proplib.h> 53 54 #include "map.h" 55 #include "gpt.h" 56 #include "gpt_private.h" 57 58 static int cmd_restore(gpt_t, int, char *[]); 59 60 static const char *restorehelp[] = { 61 "[-F] [-i infile]", 62 }; 63 64 const struct gpt_cmd c_restore = { 65 "restore", 66 cmd_restore, 67 restorehelp, __arraycount(restorehelp), 68 GPT_SYNC, 69 }; 70 71 #define usage() gpt_usage(NULL, &c_restore) 72 73 #define PROP_ERR(x) if (!(x)) { \ 74 gpt_warnx(gpt, "proplib failure"); \ 75 return -1; \ 76 } 77 78 static int 79 restore_mbr(gpt_t gpt, struct mbr *mbr, prop_dictionary_t mbr_dict, off_t last) 80 { 81 unsigned int i; 82 struct mbr_part *part; 83 84 PROP_ERR(prop_dictionary_get_uint(mbr_dict, "index", &i)); 85 part = &mbr->mbr_part[i]; 86 87 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "flag", &part->part_flag)); 88 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "start_head", 89 &part->part_shd)); 90 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "start_sector", 91 &part->part_ssect )); 92 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "start_cylinder", 93 &part->part_scyl)); 94 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "type", 95 &part->part_typ)); 96 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "end_head", 97 &part->part_ehd)); 98 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "end_sector", 99 &part->part_esect)); 100 PROP_ERR(prop_dictionary_get_uint8(mbr_dict, "end_cylinder", 101 &part->part_ecyl)); 102 PROP_ERR(prop_dictionary_get_uint16(mbr_dict, "lba_start_low", 103 &part->part_start_lo)); 104 part->part_start_lo = htole16(part->part_start_lo); 105 PROP_ERR(prop_dictionary_get_uint16(mbr_dict, "lba_start_high", 106 &part->part_start_hi)); 107 part->part_start_hi = htole16(part->part_start_hi); 108 109 /* adjust PMBR size to size of device */ 110 if (part->part_typ == MBR_PTYPE_PMBR) { 111 if (last > 0xffffffff) { 112 mbr->mbr_part[0].part_size_lo = htole16(0xffff); 113 mbr->mbr_part[0].part_size_hi = htole16(0xffff); 114 } else { 115 mbr->mbr_part[0].part_size_lo = htole16((uint16_t)last); 116 mbr->mbr_part[0].part_size_hi = htole16( 117 (uint16_t)((uint64_t)last >> 16)); 118 } 119 } else { 120 PROP_ERR(prop_dictionary_get_uint16(mbr_dict, "lba_size_low", 121 &part->part_size_lo)); 122 part->part_size_lo = htole16(part->part_size_lo); 123 PROP_ERR(prop_dictionary_get_uint16(mbr_dict, "lba_size_high", 124 &part->part_size_hi)); 125 part->part_size_hi = htole16(part->part_size_hi); 126 } 127 return 0; 128 } 129 130 static int 131 restore_ent(gpt_t gpt, prop_dictionary_t gpt_dict, void *secbuf, 132 u_int gpt_size __unused, u_int entries) 133 { 134 unsigned int i; 135 struct gpt_ent ent; 136 const char *s; 137 138 memset(&ent, 0, sizeof(ent)); 139 PROP_ERR(prop_dictionary_get_string(gpt_dict, "type", &s)); 140 if (gpt_uuid_parse(s, ent.ent_type) != 0) { 141 gpt_warnx(gpt, "%s: not able to convert to an UUID", s); 142 return -1; 143 } 144 PROP_ERR(prop_dictionary_get_string(gpt_dict, "guid", &s)); 145 if (gpt_uuid_parse(s, ent.ent_guid) != 0) { 146 gpt_warnx(gpt, "%s: not able to convert to an UUID", s); 147 return -1; 148 } 149 PROP_ERR(prop_dictionary_get_uint64(gpt_dict, "start", 150 &ent.ent_lba_start)); 151 ent.ent_lba_start = htole64(ent.ent_lba_start); 152 PROP_ERR(prop_dictionary_get_uint64(gpt_dict, "end", 153 &ent.ent_lba_end)); 154 ent.ent_lba_end = htole64(ent.ent_lba_end); 155 PROP_ERR(prop_dictionary_get_uint64(gpt_dict, "attributes", 156 &ent.ent_attr)); 157 ent.ent_attr = htole64(ent.ent_attr); 158 159 if (prop_dictionary_get_string(gpt_dict, "name", &s)) { 160 utf8_to_utf16((const uint8_t *)s, ent.ent_name, 161 __arraycount(ent.ent_name)); 162 } 163 PROP_ERR(prop_dictionary_get_uint(gpt_dict, "index", &i)); 164 if (i > entries) { 165 gpt_warnx(gpt, "Entity index out of bounds %u > %u\n", 166 i, entries); 167 return -1; 168 } 169 memcpy((char *)secbuf + gpt->secsz + ((i - 1) * sizeof(ent)), 170 &ent, sizeof(ent)); 171 return 0; 172 } 173 174 static int 175 restore(gpt_t gpt, const char *infile, int force) 176 { 177 gpt_uuid_t gpt_guid, uuid; 178 off_t firstdata, last, lastdata, gpe_start, gpe_end; 179 map_t map; 180 struct mbr *mbr; 181 struct gpt_hdr *hdr; 182 unsigned int i, gpt_size; 183 prop_dictionary_t props, gpt_dict, mbr_dict, type_dict; 184 prop_object_iterator_t propiter; 185 prop_data_t propdata; 186 prop_array_t mbr_array, gpt_array; 187 prop_number_t propnum; 188 unsigned int entries; 189 const char *s; 190 void *secbuf = NULL; 191 int rv = -1; 192 193 last = gpt->mediasz / gpt->secsz - 1LL; 194 195 if (map_find(gpt, MAP_TYPE_PRI_GPT_HDR) != NULL || 196 map_find(gpt, MAP_TYPE_SEC_GPT_HDR) != NULL) { 197 if (!force) { 198 gpt_warnx(gpt, "Device contains a GPT"); 199 return -1; 200 } 201 } 202 map = map_find(gpt, MAP_TYPE_MBR); 203 if (map != NULL) { 204 if (!force) { 205 gpt_warnx(gpt, "Device contains an MBR"); 206 return -1; 207 } 208 /* Nuke the MBR in our internal map. */ 209 map->map_type = MAP_TYPE_UNUSED; 210 } 211 212 props = prop_dictionary_internalize_from_file( 213 strcmp(infile, "-") == 0 ? "/dev/stdin" : infile); 214 if (props == NULL) { 215 gpt_warnx(gpt, "Unable to read/parse backup file"); 216 return -1; 217 } 218 219 propnum = prop_dictionary_get(props, "sector_size"); 220 PROP_ERR(propnum); 221 if (!prop_number_equals_signed(propnum, gpt->secsz)) { 222 gpt_warnx(gpt, "Sector size does not match backup"); 223 prop_object_release(props); 224 return -1; 225 } 226 227 gpt_dict = prop_dictionary_get(props, "GPT_HDR"); 228 PROP_ERR(gpt_dict); 229 230 propnum = prop_dictionary_get(gpt_dict, "revision"); 231 PROP_ERR(propnum); 232 if (!prop_number_equals_unsigned(propnum, 0x10000)) { 233 gpt_warnx(gpt, "backup is not revision 1.0"); 234 prop_object_release(gpt_dict); 235 prop_object_release(props); 236 return -1; 237 } 238 239 PROP_ERR(prop_dictionary_get_uint(gpt_dict, "entries", &entries)); 240 241 gpt_size = (u_int)(entries * sizeof(struct gpt_ent) / gpt->secsz); 242 if (gpt_size * sizeof(struct gpt_ent) % gpt->secsz) 243 gpt_size++; 244 245 PROP_ERR(prop_dictionary_get_string(gpt_dict, "guid", &s)); 246 if (gpt_uuid_parse(s, gpt_guid) != 0) { 247 gpt_warnx(gpt, "%s: not able to convert to an UUID", s); 248 goto out; 249 } 250 firstdata = gpt_size + 2; /* PMBR and GPT header */ 251 lastdata = last - gpt_size - 1; /* alt. GPT table and header */ 252 253 type_dict = prop_dictionary_get(props, "GPT_TBL"); 254 PROP_ERR(type_dict); 255 gpt_array = prop_dictionary_get(type_dict, "gpt_array"); 256 PROP_ERR(gpt_array); 257 propiter = prop_array_iterator(gpt_array); 258 PROP_ERR(propiter); 259 while ((gpt_dict = prop_object_iterator_next(propiter)) != NULL) { 260 PROP_ERR(prop_dictionary_get_string(gpt_dict, "type", &s)); 261 if (gpt_uuid_parse(s, uuid) != 0) { 262 gpt_warnx(gpt, "%s: not able to convert to an UUID", s); 263 goto out; 264 } 265 if (gpt_uuid_is_nil(uuid)) 266 continue; 267 PROP_ERR(prop_dictionary_get_int64(gpt_dict, "start", 268 &gpe_start)); 269 PROP_ERR(prop_dictionary_get_int64(gpt_dict, "end", 270 &gpe_end)); 271 if (gpe_start < firstdata || gpe_end > lastdata) { 272 gpt_warnx(gpt, "Backup GPT doesn't fit"); 273 goto out; 274 } 275 } 276 prop_object_iterator_release(propiter); 277 278 /* GPT TABLE + GPT HEADER */ 279 if ((secbuf = calloc(gpt_size + 1, gpt->secsz)) == NULL) { 280 gpt_warn(gpt, "not enough memory to create a sector buffer"); 281 goto out; 282 } 283 284 if (lseek(gpt->fd, 0LL, SEEK_SET) == -1) { 285 gpt_warn(gpt, "Can't seek to beginning"); 286 goto out; 287 } 288 for (i = 0; i < firstdata; i++) { 289 if (write(gpt->fd, secbuf, gpt->secsz) != (ssize_t)gpt->secsz) { 290 gpt_warn(gpt, "Error writing"); 291 goto out; 292 } 293 } 294 if (lseek(gpt->fd, (lastdata + 1) * gpt->secsz, SEEK_SET) == -1) { 295 gpt_warn(gpt, "Can't seek to end"); 296 goto out; 297 } 298 for (i = (u_int)(lastdata + 1); i <= (u_int)last; i++) { 299 if (write(gpt->fd, secbuf, gpt->secsz) != (ssize_t)gpt->secsz) { 300 gpt_warn(gpt, "Error writing"); 301 goto out; 302 } 303 } 304 305 mbr = secbuf; 306 type_dict = prop_dictionary_get(props, "MBR"); 307 PROP_ERR(type_dict); 308 propdata = prop_dictionary_get(type_dict, "code"); 309 PROP_ERR(propdata); 310 memcpy(mbr->mbr_code, prop_data_value(propdata), 311 sizeof(mbr->mbr_code)); 312 mbr_array = prop_dictionary_get(type_dict, "mbr_array"); 313 PROP_ERR(mbr_array); 314 propiter = prop_array_iterator(mbr_array); 315 PROP_ERR(propiter); 316 while ((mbr_dict = prop_object_iterator_next(propiter)) != NULL) { 317 if (restore_mbr(gpt, mbr, mbr_dict, last) == -1) 318 goto out; 319 } 320 321 prop_object_iterator_release(propiter); 322 mbr->mbr_sig = htole16(MBR_SIG); 323 if (lseek(gpt->fd, 0LL, SEEK_SET) == -1 || 324 write(gpt->fd, mbr, gpt->secsz) != (ssize_t)gpt->secsz) { 325 gpt_warn(gpt, "Unable to seek/write MBR"); 326 return -1; 327 } 328 329 propiter = prop_array_iterator(gpt_array); 330 PROP_ERR(propiter); 331 332 while ((gpt_dict = prop_object_iterator_next(propiter)) != NULL) { 333 if (restore_ent(gpt, gpt_dict, secbuf, gpt_size, entries) == -1) 334 goto out; 335 } 336 prop_object_iterator_release(propiter); 337 338 size_t len = gpt_size * gpt->secsz; 339 if (lseek(gpt->fd, 2 * gpt->secsz, SEEK_SET) == -1 || 340 write(gpt->fd, (char *)secbuf + gpt->secsz, len) != (ssize_t) len) { 341 gpt_warn(gpt, "Unable to write primary GPT"); 342 goto out; 343 } 344 345 if (lseek(gpt->fd, (lastdata + 1) * gpt->secsz, SEEK_SET) == -1 || 346 write(gpt->fd, (char *)secbuf + gpt->secsz, len) != (ssize_t) len) { 347 gpt_warn(gpt, "Unable to write secondary GPT"); 348 goto out; 349 } 350 351 memset(secbuf, 0, gpt->secsz); 352 hdr = secbuf; 353 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig)); 354 hdr->hdr_revision = htole32(GPT_HDR_REVISION); 355 hdr->hdr_size = htole32(GPT_HDR_SIZE); 356 hdr->hdr_lba_self = htole64(GPT_HDR_BLKNO); 357 hdr->hdr_lba_alt = htole64((uint64_t)last); 358 hdr->hdr_lba_start = htole64((uint64_t)firstdata); 359 hdr->hdr_lba_end = htole64((uint64_t)lastdata); 360 gpt_uuid_copy(hdr->hdr_guid, gpt_guid); 361 hdr->hdr_lba_table = htole64(2); 362 hdr->hdr_entries = htole32(entries); 363 hdr->hdr_entsz = htole32(sizeof(struct gpt_ent)); 364 hdr->hdr_crc_table = htole32(crc32((char *)secbuf + gpt->secsz, len)); 365 hdr->hdr_crc_self = htole32(crc32(hdr, GPT_HDR_SIZE)); 366 if (lseek(gpt->fd, gpt->secsz, SEEK_SET) == -1 || 367 write(gpt->fd, hdr, gpt->secsz) != (ssize_t)gpt->secsz) { 368 gpt_warn(gpt, "Unable to write primary header"); 369 goto out; 370 } 371 372 hdr->hdr_lba_self = htole64((uint64_t)last); 373 hdr->hdr_lba_alt = htole64(GPT_HDR_BLKNO); 374 hdr->hdr_lba_table = htole64((uint64_t)(lastdata + 1)); 375 hdr->hdr_crc_self = 0; 376 hdr->hdr_crc_self = htole32(crc32(hdr, GPT_HDR_SIZE)); 377 if (lseek(gpt->fd, last * gpt->secsz, SEEK_SET) == -1 || 378 write(gpt->fd, hdr, gpt->secsz) != (ssize_t)gpt->secsz) { 379 gpt_warn(gpt, "Unable to write secondary header"); 380 goto out; 381 } 382 rv = 0; 383 384 out: 385 free(secbuf); 386 prop_object_release(props); 387 return rv; 388 } 389 390 static int 391 cmd_restore(gpt_t gpt, int argc, char *argv[]) 392 { 393 int ch; 394 int force = 0; 395 const char *infile = "-"; 396 397 while ((ch = getopt(argc, argv, "Fi:")) != -1) { 398 switch(ch) { 399 case 'i': 400 infile = optarg; 401 break; 402 case 'F': 403 force = 1; 404 break; 405 default: 406 return usage(); 407 } 408 } 409 410 if (argc != optind) 411 return usage(); 412 413 return restore(gpt, infile, force); 414 } 415