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 /* Integers to store parsed argument. */ |
| 15 static int help, raw; |
| 16 |
| 17 /* The structure for getopt_long(). When you add/delete any line, please refine |
| 18 * attribute_comments[] and third parameter of getopt_long() too. */ |
| 19 static struct option show_options[] = { |
| 20 {.name = "help", .has_arg = no_argument, .flag = 0, .val = 'h'}, |
| 21 {.name = "raw", .has_arg = no_argument, .flag = 0, .val = 'r'}, |
| 22 }; |
| 23 |
| 24 /* Extra information than struct option, please update this structure if you |
| 25 * add/remove any line in attribute_options[]. */ |
| 26 static struct option_details show_options_details[] = { |
| 27 /* help */ |
| 28 { .comment = "print this help", |
| 29 .validator = AssignTrue, |
| 30 .valid_range = 0, |
| 31 .parsed = &help}, |
| 32 /* raw */ |
| 33 { .comment = "print raw data (byte-by-byte)", |
| 34 .validator = AssignTrue, |
| 35 .valid_range = 0, |
| 36 .parsed = &raw}, |
| 37 }; |
| 38 |
| 39 void ShowHelp() { |
| 40 printf("\nUsage: %s show [OPTIONS] device_name\n\n", progname); |
| 41 ShowOptions(show_options, show_options_details, ARRAY_COUNT(show_options)); |
| 42 printf("\n"); |
| 43 } |
| 44 |
| 45 /* Generate output like: |
| 46 * |
| 47 * {AB-CD-EF-01} |
| 48 * |
| 49 * Needs (size*3-1+3) bytes of space in 'buf'. |
| 50 */ |
| 51 static short Uint8To2Chars(const uint8_t t) { |
| 52 int h = t >> 4; |
| 53 int l = t & 0xf; |
| 54 h = (h >= 0xA) ? h - 0xA + 'A' : h + '0'; |
| 55 l = (l >= 0xA) ? l - 0xA + 'A' : l + '0'; |
| 56 return (h << 8) + l; |
| 57 } |
| 58 static void RawDump(const uint8_t *memory, const int size, char *buf) { |
| 59 int i; |
| 60 buf[0] = '{'; |
| 61 for (i = 0; i < size; ++i) { |
| 62 short c2 = Uint8To2Chars(memory[i]); |
| 63 buf[i * 3 + 1] = c2 >> 8; |
| 64 buf[i * 3 + 2] = c2 & 0xff; |
| 65 if (i != (size - 1)) |
| 66 buf[i * 3 + 3] = '-'; |
| 67 } |
| 68 buf[i * 3 + 0] = '}'; |
| 69 buf[i * 3 + 1] = '\0'; |
| 70 } |
| 71 |
| 72 /* Parses all options (and validates them), then opens the drive and sets |
| 73 * corresponding bits in GPT entry. */ |
| 74 int CgptShow(int argc, char *argv[]) { |
| 75 struct drive drive; |
| 76 int i; |
| 77 |
| 78 /* I know this is NOT the perfect place to put code to make options[] and |
| 79 * details[] are synced. But this is the best place we have right now since C |
| 80 * preprocessor doesn't know sizeof() for #if directive. */ |
| 81 assert(ARRAY_COUNT(show_options) == |
| 82 ARRAY_COUNT(show_options_details)); |
| 83 |
| 84 help = raw = NOT_INITED; |
| 85 |
| 86 if (CGPT_OK != HandleOptions(argc, argv, |
| 87 "hr", |
| 88 ARRAY_COUNT(show_options), |
| 89 show_options, |
| 90 show_options_details)) |
| 91 return CGPT_FAILED; |
| 92 if (help != NOT_INITED) { |
| 93 ShowHelp(); |
| 94 return CGPT_FAILED; |
| 95 } |
| 96 |
| 97 if (CGPT_OK != OpenDriveInLastArgument(argc, argv, &drive)) |
| 98 return CGPT_FAILED; |
| 99 |
| 100 #define TITLE_FMT "%7s%7s%7s %s\n" |
| 101 #define GPT_FMT "%7d%7d%7s %s\n" |
| 102 #define GPT_MORE "%7s%7s%7s %s\n", "", "", "" |
| 103 #define PARTITION_FMT "%7d%7d%7d %s\n" |
| 104 #define PARTITION_MORE "%7s%7s%7s %s%s\n", "", "", "" |
| 105 printf(TITLE_FMT, "start", "size", "index", "contents"); |
| 106 printf(GPT_FMT, 0, GPT_PMBR_SECTOR, "", "PMBR"); |
| 107 printf(GPT_FMT, (int)GPT_PMBR_SECTOR, |
| 108 (int)GPT_HEADER_SECTOR, "", "Pri GPT header"); |
| 109 printf(GPT_FMT, (int)(GPT_PMBR_SECTOR + GPT_HEADER_SECTOR), |
| 110 (int)GPT_ENTRIES_SECTORS, "", "Pri GPT table"); |
| 111 |
| 112 for (i = 0; i < GetNumberOfEntries(&drive.gpt); ++i) { |
| 113 static Guid zero = {{{0, 0, 0, 0, 0, {0, 0, 0, 0, 0, 0}}}}; |
| 114 char contents[128]; |
| 115 GptEntry *entry; |
| 116 entry = GetEntry(&drive.gpt, PRIMARY, i); |
| 117 |
| 118 if (!Memcmp(&zero, &entry->type, sizeof(zero))) continue; |
| 119 |
| 120 if (raw == NOT_INITED) { |
| 121 /* TODO(yjlou): support pretty dump */ |
| 122 snprintf(contents, sizeof(contents), |
| 123 "* Not supported yet *"); |
| 124 printf(PARTITION_FMT, (int)entry->starting_lba, |
| 125 (int)(entry->ending_lba - entry->starting_lba + 1), |
| 126 i, contents); |
| 127 } else { |
| 128 char type[50], unique[50], attributes[26]; |
| 129 |
| 130 snprintf(contents, sizeof(contents), |
| 131 "%s", ""); |
| 132 printf(PARTITION_FMT, (int)entry->starting_lba, |
| 133 (int)(entry->ending_lba - entry->starting_lba + 1), |
| 134 i, contents); |
| 135 RawDump((uint8_t*)&entry->type, 16, type); |
| 136 printf(PARTITION_MORE, "type: ", type); |
| 137 RawDump((uint8_t*)&entry->unique, 16, unique); |
| 138 printf(PARTITION_MORE, "uuid: ", unique); |
| 139 RawDump((uint8_t*)&entry->attributes, 8, attributes); |
| 140 printf(PARTITION_MORE, "attr: ", attributes); |
| 141 } |
| 142 } |
| 143 |
| 144 printf(GPT_FMT, (int)(drive.gpt.drive_sectors - GPT_HEADER_SECTOR - |
| 145 GPT_ENTRIES_SECTORS), |
| 146 (int)GPT_ENTRIES_SECTORS, "", "Sec GPT table"); |
| 147 printf(GPT_FMT, (int)(drive.gpt.drive_sectors - GPT_HEADER_SECTOR), |
| 148 (int)GPT_HEADER_SECTOR, "", "Sec GPT header"); |
| 149 |
| 150 DriveClose(&drive); |
| 151 |
| 152 return CGPT_OK; |
| 153 } |
OLD | NEW |