| OLD | NEW |
| (Empty) |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | |
| 2 * Use of this source code is governed by a BSD-style license that can be | |
| 3 * found in the LICENSE file. | |
| 4 * | |
| 5 * Update GPT attribute bits. | |
| 6 */ | |
| 7 #include <getopt.h> | |
| 8 #include <stdio.h> | |
| 9 #include <stdlib.h> | |
| 10 #include "cgpt.h" | |
| 11 #include "cgptlib_internal.h" | |
| 12 #include "cgpt_tofix.h" | |
| 13 #include "utility.h" | |
| 14 | |
| 15 static struct number_range range_1_0 = {1, 0}; | |
| 16 static struct number_range range_15_0 = {15, 0}; | |
| 17 static struct number_range range_16_1 = {16, 1}; | |
| 18 | |
| 19 /* Integers to store parsed argument. */ | |
| 20 static int help, partition, successful, tries, priority; | |
| 21 | |
| 22 /* The structure for getopt_long(). When you add/delete any line, please refine | |
| 23 * attribute_comments[] and third parameter of getopt_long() too. */ | |
| 24 static struct option attribute_options[] = { | |
| 25 {.name = "help", .has_arg = no_argument, .flag = 0, .val = 'h'}, | |
| 26 {.name = "partition", .has_arg = required_argument, .flag = 0, .val = 'i'}, | |
| 27 {.name = "successful", .has_arg = required_argument, .flag = 0, .val = 's'}, | |
| 28 {.name = "tries", .has_arg = required_argument, .flag = 0, .val = 't'}, | |
| 29 {.name = "priority", .has_arg = required_argument, .flag = 0, .val = 'p'}, | |
| 30 { /* last element, which should be zero. */ } | |
| 31 }; | |
| 32 | |
| 33 /* Extra information than struct option, please update this structure if you | |
| 34 * add/remove any line in attribute_options[]. */ | |
| 35 static struct option_details attribute_options_details[] = { | |
| 36 /* help */ | |
| 37 { .comment = "print this help", | |
| 38 .validator = AssignTrue, | |
| 39 .valid_range = 0, | |
| 40 .parsed = &help}, | |
| 41 /* partition */ | |
| 42 { .comment = "partition number " | |
| 43 "(default: first ChromeOS kernel)", | |
| 44 .validator = InNumberRange, | |
| 45 .valid_range = &range_16_1, | |
| 46 .parsed = &partition}, | |
| 47 /* successful */ | |
| 48 { .comment = "mark partition successful", | |
| 49 .validator = InNumberRange, | |
| 50 .valid_range = &range_1_0, | |
| 51 .parsed = &successful}, | |
| 52 /* tries */ | |
| 53 { .comment = "tries", | |
| 54 .validator = InNumberRange, | |
| 55 .valid_range = &range_15_0, | |
| 56 .parsed = &tries}, | |
| 57 /* priority */ | |
| 58 { .comment = "priority to boot", | |
| 59 .validator = InNumberRange, | |
| 60 .valid_range = &range_15_0, | |
| 61 .parsed = &priority}, | |
| 62 { /* last element, which should be zero. */ } | |
| 63 }; | |
| 64 | |
| 65 void AttributeHelp() { | |
| 66 printf("\nUsage: %s attribute [OPTIONS] device_name\n\n", progname); | |
| 67 ShowOptions(attribute_options, attribute_options_details, | |
| 68 ARRAY_COUNT(attribute_options)); | |
| 69 } | |
| 70 | |
| 71 /* Parses all options (and validates them), then opens the drive and sets | |
| 72 * corresponding bits in GPT entry. */ | |
| 73 int CgptAttribute(int argc, char *argv[]) { | |
| 74 struct drive drive; | |
| 75 GptEntry *entry; | |
| 76 | |
| 77 /* I know this is NOT the perfect place to put code to make options[] and | |
| 78 * details[] are synced. But this is the best place we have right now since C | |
| 79 * preprocessor doesn't know sizeof() for #if directive. */ | |
| 80 assert(ARRAY_COUNT(attribute_options) == | |
| 81 ARRAY_COUNT(attribute_options_details)); | |
| 82 | |
| 83 help = partition = successful = tries = priority = NOT_INITED; | |
| 84 | |
| 85 if (CGPT_OK != HandleOptions(argc, argv, | |
| 86 "hi:b:s:t:p:", | |
| 87 ARRAY_COUNT(attribute_options), | |
| 88 attribute_options, | |
| 89 attribute_options_details)) | |
| 90 return CGPT_FAILED; | |
| 91 if (help != NOT_INITED) { | |
| 92 AttributeHelp(); | |
| 93 return CGPT_FAILED; | |
| 94 } | |
| 95 | |
| 96 if (CGPT_OK != OpenDriveInLastArgument(argc, argv, &drive)) | |
| 97 return CGPT_FAILED; | |
| 98 | |
| 99 if (CheckValid(&drive) != CGPT_OK) return CGPT_FAILED; | |
| 100 | |
| 101 /* partition is not specified, search for the first Chromeos kernel. */ | |
| 102 if (partition == NOT_INITED) { | |
| 103 int i; | |
| 104 for (i = 0; i < GetNumberOfEntries(&drive.gpt); ++i) { | |
| 105 entry = GetEntry(&drive.gpt, PRIMARY, i); | |
| 106 if (!Memcmp(&guid_chromeos_kernel, &entry->type, sizeof(Guid))) { | |
| 107 partition = i+1; | |
| 108 break; | |
| 109 } | |
| 110 } | |
| 111 if (partition == NOT_INITED) { | |
| 112 printf("[ERROR] No ChromeOS kernel partition found. " | |
| 113 "Please use --partition to specify.\n"); | |
| 114 return CGPT_FAILED; | |
| 115 } else { | |
| 116 debug("No --partition is specified. " | |
| 117 "Found the first ChromeOS kernel in partition [%d].\n", | |
| 118 partition); | |
| 119 } | |
| 120 } | |
| 121 int index = partition - 1; | |
| 122 | |
| 123 if (successful != NOT_INITED) | |
| 124 SetSuccessful(&drive.gpt, PRIMARY, index, successful); | |
| 125 if (tries != NOT_INITED) | |
| 126 SetTries(&drive.gpt, PRIMARY, index, tries); | |
| 127 if (priority != NOT_INITED) | |
| 128 SetPriority(&drive.gpt, PRIMARY, index, priority); | |
| 129 | |
| 130 /* Display state */ | |
| 131 entry = GetEntry(&drive.gpt, PRIMARY, index); | |
| 132 EntryDetails(entry, index, NOT_INITED); | |
| 133 | |
| 134 /* Claims primary is good, then secondary will be overwritten. */ | |
| 135 /* TODO: rspangler broke this during cgptlib refactoring; need to | |
| 136 * update this to match new internal APIs. */ | |
| 137 RepairEntries(&drive.gpt, MASK_PRIMARY); | |
| 138 RepairHeader(&drive.gpt, MASK_PRIMARY); | |
| 139 | |
| 140 /* Forces headers and entries are modified so that CRC32 will be re-calculated | |
| 141 * and headers and entries will be updated to drive. */ | |
| 142 drive.gpt.modified |= (GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1 | | |
| 143 GPT_MODIFIED_HEADER2 | GPT_MODIFIED_ENTRIES2); | |
| 144 UpdateCrc(&drive.gpt); | |
| 145 DriveClose(&drive); | |
| 146 | |
| 147 return CGPT_OK; | |
| 148 } | |
| OLD | NEW |