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