1207753Smm/*- 2207753Smm * Copyright (c) 2005 Marcel Moolenaar 3207753Smm * All rights reserved. 4207753Smm * 5207753Smm * Redistribution and use in source and binary forms, with or without 6207753Smm * modification, are permitted provided that the following conditions 7207753Smm * are met: 8207753Smm * 9207753Smm * 1. Redistributions of source code must retain the above copyright 10207753Smm * notice, this list of conditions and the following disclaimer. 11207753Smm * 2. Redistributions in binary form must reproduce the above copyright 12207753Smm * notice, this list of conditions and the following disclaimer in the 13207753Smm * documentation and/or other materials provided with the distribution. 14207753Smm * 15207753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16207753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17207753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18207753Smm * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19207753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20207753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21207753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22207753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23207753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24207753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25207753Smm */ 26207753Smm 27207753Smm#include <sys/cdefs.h> 28207753Smm#ifdef __FBSDID 29207753Smm__FBSDID("$FreeBSD: src/sbin/gpt/label.c,v 1.3 2006/10/04 18:20:25 marcel Exp $"); 30207753Smm#endif 31207753Smm#ifdef __RCSID 32207753Smm__RCSID("$NetBSD: label.c,v 1.7 2009/03/12 22:59:03 dyoung Exp $"); 33207753Smm#endif 34207753Smm 35207753Smm#include <sys/types.h> 36207753Smm 37207753Smm#include <err.h> 38207753Smm#include <stddef.h> 39207753Smm#include <stdio.h> 40207753Smm#include <stdlib.h> 41207753Smm#include <string.h> 42207753Smm#include <unistd.h> 43207753Smm 44207753Smm#include "map.h" 45207753Smm#include "gpt.h" 46207753Smm 47207753Smmstatic int all; 48207753Smmstatic uuid_t type; 49207753Smmstatic off_t block, size; 50207753Smmstatic unsigned int entry; 51207753Smmstatic uint8_t *name; 52207753Smm 53207753Smmconst char labelmsg1[] = "label -a <-l label | -f file> device ..."; 54207753Smmconst char labelmsg2[] = "label [-b lba] [-i index] [-s lba]"; 55207753Smmconst char labelmsg3[] = " [-t uuid] <-l label | -f file> device ..."; 56207753Smm 57207753Smm__dead static void 58207753Smmusage_label(void) 59207753Smm{ 60207753Smm fprintf(stderr, 61207753Smm "usage: %s %s\n" 62207753Smm " %s %s\n" 63207753Smm " %*s %s\n", getprogname(), labelmsg1, 64207753Smm getprogname(), labelmsg2, (int)strlen(getprogname()), "", labelmsg3); 65207753Smm exit(1); 66207753Smm} 67207753Smm 68207753Smmstatic void 69207753Smmlabel(int fd) 70207753Smm{ 71207753Smm uuid_t uuid; 72207753Smm map_t *gpt, *tpg; 73207753Smm map_t *tbl, *lbt; 74207753Smm map_t *m; 75207753Smm struct gpt_hdr *hdr; 76207753Smm struct gpt_ent *ent; 77207753Smm unsigned int i; 78207753Smm 79207753Smm gpt = map_find(MAP_TYPE_PRI_GPT_HDR); 80207753Smm if (gpt == NULL) { 81207753Smm warnx("%s: error: no primary GPT header; run create or recover", 82207753Smm device_name); 83207753Smm return; 84207753Smm } 85207753Smm 86207753Smm tpg = map_find(MAP_TYPE_SEC_GPT_HDR); 87207753Smm if (tpg == NULL) { 88207753Smm warnx("%s: error: no secondary GPT header; run recover", 89207753Smm device_name); 90207753Smm return; 91207753Smm } 92207753Smm 93207753Smm tbl = map_find(MAP_TYPE_PRI_GPT_TBL); 94207753Smm lbt = map_find(MAP_TYPE_SEC_GPT_TBL); 95207753Smm if (tbl == NULL || lbt == NULL) { 96207753Smm warnx("%s: error: run recover -- trust me", device_name); 97207753Smm return; 98207753Smm } 99207753Smm 100207753Smm /* Relabel all matching entries in the map. */ 101207753Smm for (m = map_first(); m != NULL; m = m->map_next) { 102207753Smm if (m->map_type != MAP_TYPE_GPT_PART || m->map_index < 1) 103207753Smm continue; 104207753Smm if (entry > 0 && entry != m->map_index) 105207753Smm continue; 106207753Smm if (block > 0 && block != m->map_start) 107207753Smm continue; 108207753Smm if (size > 0 && size != m->map_size) 109207753Smm continue; 110207753Smm 111207753Smm i = m->map_index - 1; 112207753Smm 113207753Smm hdr = gpt->map_data; 114207753Smm ent = (void*)((char*)tbl->map_data + i * 115207753Smm le32toh(hdr->hdr_entsz)); 116207753Smm le_uuid_dec(&ent->ent_type, &uuid); 117207753Smm if (!uuid_is_nil(&type, NULL) && 118207753Smm !uuid_equal(&type, &uuid, NULL)) 119207753Smm continue; 120207753Smm 121207753Smm /* Label the primary entry. */ 122207753Smm utf8_to_utf16(name, ent->ent_name, 36); 123207753Smm 124207753Smm hdr->hdr_crc_table = htole32(crc32(tbl->map_data, 125207753Smm le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz))); 126207753Smm hdr->hdr_crc_self = 0; 127207753Smm hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); 128207753Smm 129207753Smm gpt_write(fd, gpt); 130207753Smm gpt_write(fd, tbl); 131207753Smm 132207753Smm hdr = tpg->map_data; 133207753Smm ent = (void*)((char*)lbt->map_data + i * 134207753Smm le32toh(hdr->hdr_entsz)); 135207753Smm 136207753Smm /* Label the secundary entry. */ 137207753Smm utf8_to_utf16(name, ent->ent_name, 36); 138207753Smm 139207753Smm hdr->hdr_crc_table = htole32(crc32(lbt->map_data, 140207753Smm le32toh(hdr->hdr_entries) * le32toh(hdr->hdr_entsz))); 141207753Smm hdr->hdr_crc_self = 0; 142207753Smm hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size))); 143207753Smm 144207753Smm gpt_write(fd, lbt); 145207753Smm gpt_write(fd, tpg); 146207753Smm 147207753Smm#ifdef __FreeBSD__ 148207753Smm printf("%sp%u labeled\n", device_name, m->map_index); 149207753Smm#endif 150207753Smm#ifdef __NetBSD__ 151207753Smm printf("partition %d on %s labeled %s\n", m->map_index, 152207753Smm device_name, name); 153207753Smm#endif 154207753Smm } 155207753Smm} 156207753Smm 157207753Smmstatic void 158207753Smmname_from_file(const char *fn) 159207753Smm{ 160207753Smm FILE *f; 161207753Smm char *p; 162207753Smm size_t maxlen = 1024; 163207753Smm size_t len; 164207753Smm 165207753Smm if (strcmp(fn, "-") != 0) { 166 f = fopen(fn, "r"); 167 if (f == NULL) 168 err(1, "unable to open file %s", fn); 169 } else 170 f = stdin; 171 name = malloc(maxlen); 172 len = fread(name, 1, maxlen - 1, f); 173 if (ferror(f)) 174 err(1, "unable to read label from file %s", fn); 175 if (f != stdin) 176 fclose(f); 177 name[len] = '\0'; 178 /* Only keep the first line, excluding the newline character. */ 179 p = strchr((const char *)name, '\n'); 180 if (p != NULL) 181 *p = '\0'; 182} 183 184int 185cmd_label(int argc, char *argv[]) 186{ 187 char *p; 188 int ch, fd; 189 190 /* Get the label options */ 191 while ((ch = getopt(argc, argv, "ab:f:i:l:s:t:")) != -1) { 192 switch(ch) { 193 case 'a': 194 if (all > 0) 195 usage_label(); 196 all = 1; 197 break; 198 case 'b': 199 if (block > 0) 200 usage_label(); 201 block = strtoll(optarg, &p, 10); 202 if (*p != 0 || block < 1) 203 usage_label(); 204 break; 205 case 'f': 206 if (name != NULL) 207 usage_label(); 208 name_from_file(optarg); 209 break; 210 case 'i': 211 if (entry > 0) 212 usage_label(); 213 entry = strtoul(optarg, &p, 10); 214 if (*p != 0 || entry < 1) 215 usage_label(); 216 break; 217 case 'l': 218 if (name != NULL) 219 usage_label(); 220 name = (uint8_t *)strdup(optarg); 221 break; 222 case 's': 223 if (size > 0) 224 usage_label(); 225 size = strtoll(optarg, &p, 10); 226 if (*p != 0 || size < 1) 227 usage_label(); 228 break; 229 case 't': 230 if (!uuid_is_nil(&type, NULL)) 231 usage_label(); 232 if (parse_uuid(optarg, &type) != 0) 233 usage_label(); 234 break; 235 default: 236 usage_label(); 237 } 238 } 239 240 if (!all ^ 241 (block > 0 || entry > 0 || size > 0 || !uuid_is_nil(&type, NULL))) 242 usage_label(); 243 244 if (name == NULL || argc == optind) 245 usage_label(); 246 247 while (optind < argc) { 248 fd = gpt_open(argv[optind++]); 249 if (fd == -1) { 250 warn("unable to open device '%s'", device_name); 251 continue; 252 } 253 254 label(fd); 255 256 gpt_close(fd); 257 } 258 259 return (0); 260} 261