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