set.c revision 1.8
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/add.c,v 1.14 2006/06/22 22:05:28 marcel Exp $"); 34#endif 35#ifdef __RCSID 36__RCSID("$NetBSD: set.c,v 1.8 2015/12/01 16:32:19 christos Exp $"); 37#endif 38 39#include <sys/types.h> 40 41#include <err.h> 42#include <stddef.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> 47 48#include "map.h" 49#include "gpt.h" 50#include "gpt_private.h" 51 52static unsigned int entry; 53static uint64_t attributes; 54 55static int cmd_set(gpt_t, int, char *[]); 56 57static const char *sethelp[] = { 58 "-a attribute -i index", 59}; 60 61struct gpt_cmd c_set = { 62 "set", 63 cmd_set, 64 sethelp, __arraycount(sethelp), 65 0, 66}; 67 68#define usage() gpt_usage(NULL, &c_set) 69 70static int 71set(gpt_t gpt) 72{ 73 struct gpt_hdr *hdr; 74 struct gpt_ent *ent; 75 unsigned int i; 76 77 78 if ((hdr = gpt_hdr(gpt)) == NULL) 79 return -1; 80 81 82 if (entry > le32toh(hdr->hdr_entries)) { 83 gpt_warnx(gpt, "Index %u out of range (%u max)", 84 entry, le32toh(hdr->hdr_entries)); 85 return -1; 86 } 87 88 i = entry - 1; 89 ent = gpt_ent_primary(gpt, i); 90 if (gpt_uuid_is_nil(ent->ent_type)) { 91 gpt_warnx(gpt, "Entry at index %u is unused", entry); 92 return -1; 93 } 94 95 ent->ent_attr |= attributes; 96 97 if (gpt_write_primary(gpt) == -1) 98 return -1; 99 100 ent = gpt_ent_backup(gpt, i); 101 ent->ent_attr |= attributes; 102 103 if (gpt_write_backup(gpt) == -1) 104 return -1; 105 106 gpt_msg(gpt, "Partition %d attributes updated", entry); 107 return 0; 108} 109 110static int 111cmd_set(gpt_t gpt, int argc, char *argv[]) 112{ 113 char *p; 114 int ch; 115 116 while ((ch = getopt(argc, argv, "a:i:")) != -1) { 117 switch(ch) { 118 case 'a': 119 if (strcmp(optarg, "biosboot") == 0) 120 attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE; 121 else if (strcmp(optarg, "bootme") == 0) 122 attributes |= GPT_ENT_ATTR_BOOTME; 123 else if (strcmp(optarg, "bootonce") == 0) 124 attributes |= GPT_ENT_ATTR_BOOTONCE; 125 else if (strcmp(optarg, "bootfailed") == 0) 126 attributes |= GPT_ENT_ATTR_BOOTFAILED; 127 else 128 return usage(); 129 break; 130 case 'i': 131 if (entry > 0) 132 usage(); 133 entry = strtoul(optarg, &p, 10); 134 if (*p != 0 || entry < 1) 135 return usage(); 136 break; 137 default: 138 return usage(); 139 } 140 } 141 142 if (argc != optind) 143 return usage(); 144 145 if (entry == 0 || attributes == 0) 146 return usage(); 147 148 return set(gpt); 149} 150