1 1.1 jnemeth /*- 2 1.1 jnemeth * Copyright (c) 2002 Marcel Moolenaar 3 1.1 jnemeth * All rights reserved. 4 1.1 jnemeth * 5 1.1 jnemeth * Redistribution and use in source and binary forms, with or without 6 1.1 jnemeth * modification, are permitted provided that the following conditions 7 1.1 jnemeth * are met: 8 1.1 jnemeth * 9 1.1 jnemeth * 1. Redistributions of source code must retain the above copyright 10 1.1 jnemeth * notice, this list of conditions and the following disclaimer. 11 1.1 jnemeth * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 jnemeth * notice, this list of conditions and the following disclaimer in the 13 1.1 jnemeth * documentation and/or other materials provided with the distribution. 14 1.1 jnemeth * 15 1.1 jnemeth * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 jnemeth * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 jnemeth * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 jnemeth * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 jnemeth * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 1.1 jnemeth * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 1.1 jnemeth * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 1.1 jnemeth * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 1.1 jnemeth * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 1.1 jnemeth * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 1.1 jnemeth */ 26 1.1 jnemeth 27 1.4 christos #if HAVE_NBTOOL_CONFIG_H 28 1.4 christos #include "nbtool_config.h" 29 1.4 christos #endif 30 1.4 christos 31 1.1 jnemeth #include <sys/cdefs.h> 32 1.1 jnemeth #ifdef __FBSDID 33 1.1 jnemeth __FBSDID("$FreeBSD: src/sbin/gpt/show.c,v 1.14 2006/06/22 22:22:32 marcel Exp $"); 34 1.1 jnemeth #endif 35 1.1 jnemeth #ifdef __RCSID 36 1.21 christos __RCSID("$NetBSD: backup.c,v 1.21 2025/02/23 20:47:19 christos Exp $"); 37 1.1 jnemeth #endif 38 1.1 jnemeth 39 1.1 jnemeth #include <sys/bootblock.h> 40 1.1 jnemeth #include <sys/types.h> 41 1.1 jnemeth 42 1.20 thorpej #include <assert.h> 43 1.1 jnemeth #include <err.h> 44 1.20 thorpej #include <limits.h> 45 1.1 jnemeth #include <stddef.h> 46 1.1 jnemeth #include <stdio.h> 47 1.1 jnemeth #include <stdlib.h> 48 1.1 jnemeth #include <string.h> 49 1.1 jnemeth #include <unistd.h> 50 1.1 jnemeth #include <prop/proplib.h> 51 1.1 jnemeth 52 1.1 jnemeth #include "map.h" 53 1.1 jnemeth #include "gpt.h" 54 1.10 christos #include "gpt_private.h" 55 1.1 jnemeth 56 1.11 christos static const char *backuphelp[] = { 57 1.14 christos "[-o outfile]", 58 1.11 christos }; 59 1.11 christos 60 1.11 christos static int cmd_backup(gpt_t, int, char *[]); 61 1.11 christos 62 1.21 christos const struct gpt_cmd c_backup = { 63 1.11 christos "backup", 64 1.11 christos cmd_backup, 65 1.11 christos backuphelp, __arraycount(backuphelp), 66 1.11 christos GPT_READONLY, 67 1.11 christos }; 68 1.1 jnemeth 69 1.11 christos #define usage() gpt_usage(NULL, &c_backup) 70 1.1 jnemeth 71 1.16 christos #define PROP_ERR(x) if (!(x)) goto cleanup 72 1.1 jnemeth 73 1.12 christos static int 74 1.12 christos store_mbr(gpt_t gpt, unsigned int i, const struct mbr *mbr, 75 1.12 christos prop_array_t *mbr_array) 76 1.12 christos { 77 1.12 christos prop_dictionary_t mbr_dict; 78 1.12 christos const struct mbr_part *par = &mbr->mbr_part[i]; 79 1.12 christos 80 1.12 christos if (mbr->mbr_part[i].part_typ == MBR_PTYPE_UNUSED) 81 1.12 christos return 0; 82 1.12 christos 83 1.12 christos mbr_dict = prop_dictionary_create(); 84 1.12 christos PROP_ERR(mbr_dict); 85 1.20 thorpej assert(i <= INT_MAX); 86 1.20 thorpej PROP_ERR(prop_dictionary_set_int(mbr_dict, "index", (int)i)); 87 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "flag", par->part_flag)); 88 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "start_head", 89 1.19 thorpej par->part_shd)); 90 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "start_sector", 91 1.19 thorpej par->part_ssect)); 92 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "start_cylinder", 93 1.19 thorpej par->part_scyl)); 94 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "type", par->part_typ)); 95 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "end_head", par->part_ehd)); 96 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "end_sector", 97 1.19 thorpej par->part_esect)); 98 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "end_cylinder", 99 1.19 thorpej par->part_ecyl)); 100 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "lba_start_low", 101 1.19 thorpej le16toh(par->part_start_lo))); 102 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "lba_start_high", 103 1.19 thorpej le16toh(par->part_start_hi))); 104 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "lba_size_low", 105 1.19 thorpej le16toh(par->part_size_lo))); 106 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(mbr_dict, "lba_size_high", 107 1.19 thorpej le16toh(par->part_size_hi))); 108 1.19 thorpej 109 1.12 christos if (*mbr_array == NULL) { 110 1.12 christos *mbr_array = prop_array_create(); 111 1.12 christos PROP_ERR(*mbr_array); 112 1.12 christos } 113 1.19 thorpej PROP_ERR(prop_array_add_and_rel(*mbr_array, mbr_dict)); 114 1.12 christos return 0; 115 1.16 christos cleanup: 116 1.16 christos if (mbr_dict) 117 1.16 christos prop_object_release(mbr_dict); 118 1.16 christos gpt_warnx(gpt, "proplib failure"); 119 1.16 christos return -1; 120 1.12 christos } 121 1.12 christos 122 1.12 christos static int 123 1.21 christos store_gpt(gpt_t gpt __unused, const struct gpt_hdr *hdr, 124 1.21 christos prop_dictionary_t *type_dict) 125 1.12 christos { 126 1.12 christos char buf[128]; 127 1.12 christos 128 1.12 christos *type_dict = prop_dictionary_create(); 129 1.12 christos PROP_ERR(type_dict); 130 1.19 thorpej PROP_ERR(prop_dictionary_set_uint(*type_dict, "revision", 131 1.19 thorpej le32toh(hdr->hdr_revision))); 132 1.12 christos gpt_uuid_snprintf(buf, sizeof(buf), "%d", hdr->hdr_guid); 133 1.19 thorpej PROP_ERR(prop_dictionary_set_string(*type_dict, "guid", buf)); 134 1.20 thorpej assert(le32toh(hdr->hdr_entries) <= INT32_MAX); 135 1.20 thorpej PROP_ERR(prop_dictionary_set_int32(*type_dict, "entries", 136 1.20 thorpej (int32_t)le32toh(hdr->hdr_entries))); 137 1.12 christos return 0; 138 1.16 christos cleanup: 139 1.16 christos if (*type_dict) 140 1.16 christos prop_object_release(*type_dict); 141 1.16 christos return -1; 142 1.12 christos } 143 1.12 christos 144 1.12 christos static int 145 1.12 christos store_tbl(gpt_t gpt, const map_t m, prop_dictionary_t *type_dict) 146 1.12 christos { 147 1.12 christos const struct gpt_ent *ent; 148 1.12 christos unsigned int i; 149 1.12 christos prop_dictionary_t gpt_dict; 150 1.12 christos prop_array_t gpt_array; 151 1.12 christos char buf[128]; 152 1.12 christos uint8_t utfbuf[__arraycount(ent->ent_name) * 3 + 1]; 153 1.12 christos bool rc; 154 1.12 christos 155 1.16 christos *type_dict = NULL; 156 1.16 christos 157 1.16 christos gpt_array = prop_array_create(); 158 1.16 christos PROP_ERR(gpt_array); 159 1.16 christos 160 1.12 christos *type_dict = prop_dictionary_create(); 161 1.12 christos PROP_ERR(*type_dict); 162 1.12 christos 163 1.12 christos ent = m->map_data; 164 1.12 christos for (i = 1, ent = m->map_data; 165 1.12 christos (const char *)ent < (const char *)(m->map_data) + 166 1.12 christos m->map_size * gpt->secsz; i++, ent++) { 167 1.12 christos gpt_dict = prop_dictionary_create(); 168 1.12 christos PROP_ERR(gpt_dict); 169 1.20 thorpej assert(i <= INT_MAX); 170 1.20 thorpej PROP_ERR(prop_dictionary_set_int(gpt_dict, "index", (int)i)); 171 1.12 christos gpt_uuid_snprintf(buf, sizeof(buf), "%d", ent->ent_type); 172 1.19 thorpej PROP_ERR(prop_dictionary_set_string(gpt_dict, "type", buf)); 173 1.12 christos gpt_uuid_snprintf(buf, sizeof(buf), "%d", ent->ent_guid); 174 1.19 thorpej PROP_ERR(prop_dictionary_set_string(gpt_dict, "guid", buf)); 175 1.19 thorpej PROP_ERR(prop_dictionary_set_uint64(gpt_dict, "start", 176 1.19 thorpej le64toh(ent->ent_lba_start))); 177 1.19 thorpej PROP_ERR(prop_dictionary_set_uint64(gpt_dict, "end", 178 1.19 thorpej le64toh(ent->ent_lba_end))); 179 1.19 thorpej PROP_ERR(prop_dictionary_set_uint64(gpt_dict, "attributes", 180 1.19 thorpej le64toh(ent->ent_attr))); 181 1.18 christos utf16_to_utf8(ent->ent_name, __arraycount(ent->ent_name), 182 1.18 christos utfbuf, __arraycount(utfbuf)); 183 1.12 christos if (utfbuf[0] != '\0') { 184 1.19 thorpej PROP_ERR(prop_dictionary_set_string(gpt_dict, "name", 185 1.19 thorpej (char *)utfbuf)); 186 1.12 christos } 187 1.12 christos rc = prop_array_add(gpt_array, gpt_dict); 188 1.12 christos PROP_ERR(rc); 189 1.12 christos } 190 1.19 thorpej rc = prop_dictionary_set_and_rel(*type_dict, "gpt_array", gpt_array); 191 1.12 christos PROP_ERR(rc); 192 1.12 christos return 0; 193 1.16 christos cleanup: 194 1.16 christos if (*type_dict) 195 1.16 christos prop_object_release(*type_dict); 196 1.16 christos if (gpt_array) 197 1.16 christos prop_object_release(gpt_array); 198 1.16 christos return -1; 199 1.12 christos } 200 1.12 christos 201 1.10 christos static int 202 1.14 christos backup(gpt_t gpt, const char *outfile) 203 1.1 jnemeth { 204 1.10 christos map_t m; 205 1.1 jnemeth struct mbr *mbr; 206 1.1 jnemeth unsigned int i; 207 1.12 christos prop_dictionary_t props, type_dict; 208 1.12 christos prop_array_t mbr_array; 209 1.12 christos char *propext; 210 1.1 jnemeth bool rc; 211 1.13 christos FILE *fp; 212 1.1 jnemeth 213 1.1 jnemeth props = prop_dictionary_create(); 214 1.1 jnemeth PROP_ERR(props); 215 1.20 thorpej assert(gpt->secsz <= INT_MAX); 216 1.20 thorpej PROP_ERR(prop_dictionary_set_int(props, "sector_size", 217 1.20 thorpej (int)gpt->secsz)); 218 1.10 christos m = map_first(gpt); 219 1.1 jnemeth while (m != NULL) { 220 1.1 jnemeth switch (m->map_type) { 221 1.1 jnemeth case MAP_TYPE_MBR: 222 1.1 jnemeth case MAP_TYPE_PMBR: 223 1.1 jnemeth type_dict = prop_dictionary_create(); 224 1.1 jnemeth PROP_ERR(type_dict); 225 1.1 jnemeth mbr = m->map_data; 226 1.19 thorpej PROP_ERR(prop_dictionary_set_data_nocopy(type_dict, 227 1.19 thorpej "code", mbr->mbr_code, sizeof(mbr->mbr_code))); 228 1.1 jnemeth mbr_array = NULL; 229 1.1 jnemeth for (i = 0; i < 4; i++) { 230 1.12 christos if (store_mbr(gpt, i, mbr, &mbr_array) == -1) 231 1.16 christos goto cleanup; 232 1.1 jnemeth } 233 1.1 jnemeth if (mbr_array != NULL) { 234 1.19 thorpej rc = prop_dictionary_set_and_rel(type_dict, 235 1.1 jnemeth "mbr_array", mbr_array); 236 1.1 jnemeth PROP_ERR(rc); 237 1.1 jnemeth } 238 1.19 thorpej rc = prop_dictionary_set_and_rel(props, "MBR", 239 1.19 thorpej type_dict); 240 1.1 jnemeth PROP_ERR(rc); 241 1.1 jnemeth break; 242 1.1 jnemeth case MAP_TYPE_PRI_GPT_HDR: 243 1.12 christos if (store_gpt(gpt, m->map_data, &type_dict) == -1) 244 1.16 christos goto cleanup; 245 1.12 christos 246 1.19 thorpej rc = prop_dictionary_set_and_rel(props, "GPT_HDR", 247 1.19 thorpej type_dict); 248 1.1 jnemeth PROP_ERR(rc); 249 1.1 jnemeth break; 250 1.1 jnemeth case MAP_TYPE_PRI_GPT_TBL: 251 1.12 christos if (store_tbl(gpt, m, &type_dict) == -1) 252 1.16 christos goto cleanup; 253 1.19 thorpej rc = prop_dictionary_set_and_rel(props, "GPT_TBL", 254 1.19 thorpej type_dict); 255 1.1 jnemeth PROP_ERR(rc); 256 1.1 jnemeth break; 257 1.1 jnemeth } 258 1.1 jnemeth m = m->map_next; 259 1.1 jnemeth } 260 1.1 jnemeth propext = prop_dictionary_externalize(props); 261 1.1 jnemeth PROP_ERR(propext); 262 1.1 jnemeth prop_object_release(props); 263 1.14 christos fp = strcmp(outfile, "-") == 0 ? stdout : fopen(outfile, "w"); 264 1.14 christos if (fp == NULL) { 265 1.13 christos gpt_warn(gpt, "Can't open `%s'", outfile); 266 1.16 christos free(propext); 267 1.16 christos goto cleanup; 268 1.13 christos } 269 1.13 christos fputs(propext, fp); 270 1.15 christos if (fp != stdout) 271 1.14 christos fclose(fp); 272 1.3 jnemeth free(propext); 273 1.10 christos return 0; 274 1.16 christos cleanup: 275 1.16 christos if (props) 276 1.16 christos prop_object_release(props); 277 1.16 christos return -1; 278 1.1 jnemeth } 279 1.1 jnemeth 280 1.11 christos static int 281 1.10 christos cmd_backup(gpt_t gpt, int argc, char *argv[]) 282 1.1 jnemeth { 283 1.13 christos int ch; 284 1.14 christos const char *outfile = "-"; 285 1.13 christos 286 1.13 christos while ((ch = getopt(argc, argv, "o:")) != -1) { 287 1.13 christos switch(ch) { 288 1.13 christos case 'o': 289 1.13 christos outfile = optarg; 290 1.13 christos break; 291 1.13 christos default: 292 1.13 christos return usage(); 293 1.13 christos } 294 1.13 christos } 295 1.10 christos if (argc != optind) 296 1.11 christos return usage(); 297 1.1 jnemeth 298 1.14 christos return backup(gpt, outfile); 299 1.1 jnemeth } 300