set.c revision 1.7
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.7 2015/12/01 09:05:33 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 55const char setmsg[] = "set -a attribute -i index"; 56 57static int 58usage_set(void) 59{ 60 61 fprintf(stderr, 62 "usage: %s %s\n", getprogname(), setmsg); 63 return -1; 64} 65 66static int 67set(gpt_t gpt) 68{ 69 struct gpt_hdr *hdr; 70 struct gpt_ent *ent; 71 unsigned int i; 72 73 74 if ((hdr = gpt_hdr(gpt)) == NULL) 75 return -1; 76 77 78 if (entry > le32toh(hdr->hdr_entries)) { 79 gpt_warnx(gpt, "Index %u out of range (%u max)", 80 entry, le32toh(hdr->hdr_entries)); 81 return -1; 82 } 83 84 i = entry - 1; 85 ent = gpt_ent_primary(gpt, i); 86 if (gpt_uuid_is_nil(ent->ent_type)) { 87 gpt_warnx(gpt, "Entry at index %u is unused", entry); 88 return -1; 89 } 90 91 ent->ent_attr |= attributes; 92 93 if (gpt_write_primary(gpt) == -1) 94 return -1; 95 96 ent = gpt_ent_backup(gpt, i); 97 ent->ent_attr |= attributes; 98 99 if (gpt_write_backup(gpt) == -1) 100 return -1; 101 102 gpt_msg(gpt, "Partition %d attributes updated", entry); 103 return 0; 104} 105 106int 107cmd_set(gpt_t gpt, int argc, char *argv[]) 108{ 109 char *p; 110 int ch; 111 112 while ((ch = getopt(argc, argv, "a:i:")) != -1) { 113 switch(ch) { 114 case 'a': 115 if (strcmp(optarg, "biosboot") == 0) 116 attributes |= GPT_ENT_ATTR_LEGACY_BIOS_BOOTABLE; 117 else if (strcmp(optarg, "bootme") == 0) 118 attributes |= GPT_ENT_ATTR_BOOTME; 119 else if (strcmp(optarg, "bootonce") == 0) 120 attributes |= GPT_ENT_ATTR_BOOTONCE; 121 else if (strcmp(optarg, "bootfailed") == 0) 122 attributes |= GPT_ENT_ATTR_BOOTFAILED; 123 else 124 usage_set(); 125 break; 126 case 'i': 127 if (entry > 0) 128 usage_set(); 129 entry = strtoul(optarg, &p, 10); 130 if (*p != 0 || entry < 1) 131 usage_set(); 132 break; 133 default: 134 return usage_set(); 135 } 136 } 137 138 if (argc != optind) 139 return usage_set(); 140 141 if (entry == 0 || attributes == 0) 142 return usage_set(); 143 144 return set(gpt); 145} 146