1/*- 2 * Copyright (c) 2005 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__FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3.10.1 2010/02/10 00:26:20 kensmith Exp $"); 29 30#include <sys/types.h> 31 32#include <err.h> 33#include <stddef.h> 34#include <stdio.h> 35#include <stdlib.h> 36#include <string.h> 37#include <unistd.h> 38 39#include "map.h" 40#include "gpt.h" 41 42static int all; 43static uuid_t type; 44static off_t block, size; 45static unsigned int entry; 46static uint8_t *name; 47 48static void 49usage_label(void) 50{ 51 const char *common = "<-l label | -f file> device ..."; 52 53 fprintf(stderr, 54 "usage: %s -a %s\n" 55 " %s [-b lba] [-i index] [-s lba] [-t uuid] %s\n", 56 getprogname(), common, getprogname(), common); 57 exit(1); 58} 59 60static void 61label(int fd) 62{ 63 uuid_t uuid; 64 map_t *gpt, *tpg; 65 map_t *tbl, *lbt; 66 map_t *m; 67 struct gpt_hdr *hdr; 68 struct gpt_ent *ent; 69 unsigned int i; 70 71 gpt = map_find(MAP_TYPE_PRI_GPT_HDR); 72 if (gpt == NULL) { 73 warnx("%s: error: no primary GPT header; run create or recover", 74 device_name); 75 return; 76 } 77 78 tpg = map_find(MAP_TYPE_SEC_GPT_HDR); 79 if (tpg == NULL) { 80 warnx("%s: error: no secondary GPT header; run recover", 81 device_name); 82 return; 83 } 84 85 tbl = map_find(MAP_TYPE_PRI_GPT_TBL); 86 lbt = map_find(MAP_TYPE_SEC_GPT_TBL); 87 if (tbl == NULL || lbt == NULL) { 88 warnx("%s: error: run recover -- trust me", device_name); 89 return; 90 } 91 92 /* Relabel all matching entries in the map. */ 93 for (m = map_first(); m != NULL; m = m->map_next) { 94 if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1) 95 continue; 96 if (entry > 0 && entry != m->map_index) 97 continue; 98 if (block > 0 && block != m->map_start) 99 continue; 100 if (size > 0 && size != m->map_size) 101 continue; 102 103 i = m->map_index - 1; 104 105 hdr = gpt->map_data; 106 ent = (void*)((char*)tbl->map_data + i * 107 le32toh(hdr->hdr_entsz)); 108 le_uuid_dec(&ent->ent_type, &uuid); 109 if (!uuid_is_nil(&type, NULL) && 110 !uuid_equal(&type, &uuid, NULL)) 111 continue; 112 113 /* Label the primary entry. */ 114 utf8_to_utf16(name, ent->ent_name, 36); 115 116 hdr->hdr_crc_table = htole32(crc32(tbl->map_data, 117 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz))); 118 hdr->hdr_crc_self = 0; 119 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); 120 121 gpt_write(fd, gpt); 122 gpt_write(fd, tbl); 123 124 hdr = tpg->map_data; 125 ent = (void*)((char*)lbt->map_data + i * 126 le32toh(hdr->hdr_entsz)); 127 128 /* Label the secundary entry. */ 129 utf8_to_utf16(name, ent->ent_name, 36); 130 131 hdr->hdr_crc_table = htole32(crc32(lbt->map_data, 132 le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz))); 133 hdr->hdr_crc_self = 0; 134 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); 135 136 gpt_write(fd, lbt); 137 gpt_write(fd, tpg); 138 139#ifdef __APPLE__ 140 printf("%ss%u labeled\n", device_name, m->map_index); 141#else 142 printf("%sp%u labeled\n", device_name, m->map_index); 143#endif 144 } 145} 146 147static void 148name_from_file(const char *fn) 149{ 150 FILE *f; 151 char *p; 152 size_t maxlen = 1024; 153 size_t len; 154 155 if (strcmp(fn, "-") != 0) { 156 f = fopen(fn, "r"); 157 if (f == NULL) 158 err(1, "unable to open file %s", fn); 159 } else 160 f = stdin; 161 name = malloc(maxlen); 162 len = fread(name, 1, maxlen - 1, f); 163 if (ferror(f)) 164 err(1, "unable to read label from file %s", fn); 165 if (f != stdin) 166 fclose(f); 167 name[len] = '\0'; 168 /* Only keep the first line, excluding the newline character. */ 169 p = strchr((const char *)name, '\n'); 170 if (p != NULL) 171 *p = '\0'; 172} 173 174int 175cmd_label(int argc, char *argv[]) 176{ 177 char *p; 178 int ch, fd; 179 180 /* Get the label options */ 181 while ((ch = getopt(argc, argv, "ab:f:i:l:s:t:")) != -1) { 182 switch(ch) { 183 case 'a': 184 if (all > 0) 185 usage_label(); 186 all = 1; 187 break; 188 case 'b': 189 if (block > 0) 190 usage_label(); 191 block = strtoll(optarg, &p, 10); 192 if (*p != 0 || block < 1) 193 usage_label(); 194 break; 195 case 'f': 196 if (name != NULL) 197 usage_label(); 198 name_from_file(optarg); 199 break; 200 case 'i': 201 if (entry > 0) 202 usage_label(); 203 entry = strtol(optarg, &p, 10); 204 if (*p != 0 || entry < 1) 205 usage_label(); 206 break; 207 case 'l': 208 if (name != NULL) 209 usage_label(); 210 name = (uint8_t *)strdup(optarg); 211 break; 212 case 's': 213 if (size > 0) 214 usage_label(); 215 size = strtoll(optarg, &p, 10); 216 if (*p != 0 || size < 1) 217 usage_label(); 218 break; 219 case 't': 220 if (!uuid_is_nil(&type, NULL)) 221 usage_label(); 222 if (parse_uuid(optarg, &type) != 0) 223 usage_label(); 224 break; 225 default: 226 usage_label(); 227 } 228 } 229 230 if (!all ^ 231 (block > 0 || entry > 0 || size > 0 || !uuid_is_nil(&type, NULL))) 232 usage_label(); 233 234 if (name == NULL || argc == optind) 235 usage_label(); 236 237 while (optind < argc) { 238 fd = gpt_open(argv[optind++]); 239 if (fd == -1) { 240 warn("unable to open device '%s'", device_name); 241 return (1); 242 } 243 244 label(fd); 245 246 gpt_close(fd); 247 } 248 249 return (0); 250} 251