cgpt_show.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // Copyright (c) 2012 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. #define __STDC_FORMAT_MACROS
  5. #include <string.h>
  6. #include "cgpt.h"
  7. #include "cgptlib_internal.h"
  8. #include "crc32.h"
  9. #include "vboot_host.h"
  10. /* Generate output like:
  11. *
  12. * [AB-CD-EF-01] for group = 1
  13. * [ABCD-EF01] for group = 3 (low byte first)
  14. *
  15. * Needs (size*3-1+3) bytes of space in 'buf' (included the tailing '\0').
  16. */
  17. #define BUFFER_SIZE(size) (size *3 - 1 + 3)
  18. static short Uint8To2Chars(const uint8_t t) {
  19. int h = t >> 4;
  20. int l = t & 0xf;
  21. h = (h >= 0xA) ? h - 0xA + 'A' : h + '0';
  22. l = (l >= 0xA) ? l - 0xA + 'A' : l + '0';
  23. return (h << 8) + l;
  24. }
  25. static void RawDump(const uint8_t *memory, const int size,
  26. char *buf, int group) {
  27. int i, outlen = 0;
  28. buf[outlen++] = '[';
  29. for (i = 0; i < size; ++i) {
  30. short c2 = Uint8To2Chars(memory[i]);
  31. buf[outlen++] = c2 >> 8;
  32. buf[outlen++] = c2 & 0xff;
  33. if (i != (size - 1) && ((i + 1) % group) == 0)
  34. buf[outlen++] = '-';
  35. }
  36. buf[outlen++] = ']';
  37. buf[outlen++] = '\0';
  38. }
  39. /* Output formatters */
  40. #define TITLE_FMT "%12s%12s%8s %s\n"
  41. #define GPT_FMT "%12d%12d%8s %s\n"
  42. #define GPT_MORE "%12s%12s%8s ", "", "", ""
  43. #define PARTITION_FMT "%12d%12d%8d %s\n"
  44. #define PARTITION_MORE "%12s%12s%8s %s%s\n", "", "", ""
  45. void PrintSignature(const char *indent, const char *sig, size_t n, int raw) {
  46. size_t i;
  47. printf("%sSig: ", indent);
  48. if (!raw) {
  49. printf("[");
  50. for (i = 0; i < n; ++i)
  51. printf("%c", sig[i]);
  52. printf("]");
  53. } else {
  54. char *buf = malloc(BUFFER_SIZE(n));
  55. RawDump((uint8_t *)sig, n, buf, 1);
  56. printf("%s", buf);
  57. free(buf);
  58. }
  59. printf("\n");
  60. }
  61. static void HeaderDetails(GptHeader *header, GptEntry *entries,
  62. const char *indent, int raw) {
  63. PrintSignature(indent, header->signature, sizeof(header->signature), raw);
  64. printf("%sRev: 0x%08x\n", indent, header->revision);
  65. printf("%sSize: %d\n", indent, header->size);
  66. printf("%sHeader CRC: 0x%08x %s\n", indent, header->header_crc32,
  67. (HeaderCrc(header) != header->header_crc32) ? "(INVALID)" : "");
  68. printf("%sMy LBA: %lld\n", indent, (long long)header->my_lba);
  69. printf("%sAlternate LBA: %lld\n", indent, (long long)header->alternate_lba);
  70. printf("%sFirst LBA: %lld\n", indent, (long long)header->first_usable_lba);
  71. printf("%sLast LBA: %lld\n", indent, (long long)header->last_usable_lba);
  72. { /* For disk guid */
  73. char buf[GUID_STRLEN];
  74. GuidToStr(&header->disk_uuid, buf, GUID_STRLEN);
  75. printf("%sDisk UUID: %s\n", indent, buf);
  76. }
  77. printf("%sEntries LBA: %lld\n", indent, (long long)header->entries_lba);
  78. printf("%sNumber of entries: %d\n", indent, header->number_of_entries);
  79. printf("%sSize of entry: %d\n", indent, header->size_of_entry);
  80. printf("%sEntries CRC: 0x%08x %s\n", indent, header->entries_crc32,
  81. header->entries_crc32 !=
  82. Crc32((const uint8_t *)entries,header->size_of_entry *
  83. header->number_of_entries)
  84. ? "INVALID" : ""
  85. );
  86. }
  87. void EntryDetails(GptEntry *entry, uint32_t index, int raw) {
  88. char contents[256]; // scratch buffer for formatting output
  89. uint8_t label[GPT_PARTNAME_LEN];
  90. char type[GUID_STRLEN], unique[GUID_STRLEN];
  91. int clen;
  92. UTF16ToUTF8(entry->name, sizeof(entry->name) / sizeof(entry->name[0]),
  93. label, sizeof(label));
  94. require(snprintf(contents, sizeof(contents),
  95. "Label: \"%s\"", label) < sizeof(contents));
  96. printf(PARTITION_FMT, (int)entry->starting_lba,
  97. (int)(entry->ending_lba - entry->starting_lba + 1),
  98. index+1, contents);
  99. if (!raw && CGPT_OK == ResolveType(&entry->type, type)) {
  100. printf(PARTITION_MORE, "Type: ", type);
  101. } else {
  102. GuidToStr(&entry->type, type, GUID_STRLEN);
  103. printf(PARTITION_MORE, "Type: ", type);
  104. }
  105. GuidToStr(&entry->unique, unique, GUID_STRLEN);
  106. printf(PARTITION_MORE, "UUID: ", unique);
  107. clen = 0;
  108. if (!raw) {
  109. if (GuidEqual(&guid_chromeos_kernel, &entry->type)) {
  110. int tries = (entry->attrs.fields.gpt_att &
  111. CGPT_ATTRIBUTE_TRIES_MASK) >>
  112. CGPT_ATTRIBUTE_TRIES_OFFSET;
  113. int successful = (entry->attrs.fields.gpt_att &
  114. CGPT_ATTRIBUTE_SUCCESSFUL_MASK) >>
  115. CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET;
  116. int priority = (entry->attrs.fields.gpt_att &
  117. CGPT_ATTRIBUTE_PRIORITY_MASK) >>
  118. CGPT_ATTRIBUTE_PRIORITY_OFFSET;
  119. clen = snprintf(contents, sizeof(contents),
  120. "priority=%d tries=%d successful=%d ",
  121. priority, tries, successful);
  122. }
  123. if (entry->attrs.fields.system) {
  124. clen += snprintf(contents + clen, sizeof(contents) - clen,
  125. "system=%d ", entry->attrs.fields.system);
  126. require(clen < sizeof(contents));
  127. }
  128. if (entry->attrs.fields.efi_ignore) {
  129. clen += snprintf(contents + clen, sizeof(contents) - clen,
  130. "efi_ignore=%d ", entry->attrs.fields.efi_ignore);
  131. require(clen < sizeof(contents));
  132. }
  133. if (entry->attrs.fields.legacy_boot) {
  134. clen += snprintf(contents + clen, sizeof(contents) - clen,
  135. "legacy_boot=%d ", entry->attrs.fields.legacy_boot);
  136. require(clen < sizeof(contents));
  137. }
  138. } else {
  139. clen = snprintf(contents, sizeof(contents),
  140. "[%x]", entry->attrs.fields.gpt_att);
  141. }
  142. require(clen < sizeof(contents));
  143. if (clen)
  144. printf(PARTITION_MORE, "Attr: ", contents);
  145. }
  146. void EntriesDetails(struct drive *drive, const int secondary, int raw) {
  147. uint32_t i;
  148. for (i = 0; i < GetNumberOfEntries(drive); ++i) {
  149. GptEntry *entry;
  150. entry = GetEntry(&drive->gpt, secondary, i);
  151. if (GuidIsZero(&entry->type))
  152. continue;
  153. EntryDetails(entry, i, raw);
  154. }
  155. }
  156. static int GptShow(struct drive *drive, CgptShowParams *params) {
  157. int gpt_retval;
  158. if (GPT_SUCCESS != (gpt_retval = GptSanityCheck(&drive->gpt))) {
  159. Error("GptSanityCheck() returned %d: %s\n",
  160. gpt_retval, GptError(gpt_retval));
  161. return CGPT_FAILED;
  162. }
  163. if (params->partition) { // show single partition
  164. if (params->partition > GetNumberOfEntries(drive)) {
  165. Error("invalid partition number: %d\n", params->partition);
  166. return CGPT_FAILED;
  167. }
  168. uint32_t index = params->partition - 1;
  169. GptEntry *entry = GetEntry(&drive->gpt, ANY_VALID, index);
  170. char buf[256]; // scratch buffer for string conversion
  171. if (params->single_item) {
  172. switch(params->single_item) {
  173. case 'b':
  174. printf("%" PRId64 "\n", entry->starting_lba);
  175. break;
  176. case 's': {
  177. uint64_t size = 0;
  178. // If these aren't actually defined, don't show anything
  179. if (entry->ending_lba || entry->starting_lba)
  180. size = entry->ending_lba - entry->starting_lba + 1;
  181. printf("%" PRId64 "\n", size);
  182. break;
  183. }
  184. case 't':
  185. GuidToStr(&entry->type, buf, sizeof(buf));
  186. printf("%s\n", buf);
  187. break;
  188. case 'u':
  189. GuidToStr(&entry->unique, buf, sizeof(buf));
  190. printf("%s\n", buf);
  191. break;
  192. case 'l':
  193. UTF16ToUTF8(entry->name, sizeof(entry->name) / sizeof(entry->name[0]),
  194. (uint8_t *)buf, sizeof(buf));
  195. printf("%s\n", buf);
  196. break;
  197. case 'S':
  198. printf("%d\n", GetSuccessful(drive, ANY_VALID, index));
  199. break;
  200. case 'T':
  201. printf("%d\n", GetTries(drive, ANY_VALID, index));
  202. break;
  203. case 'P':
  204. printf("%d\n", GetPriority(drive, ANY_VALID, index));
  205. break;
  206. case 'B':
  207. printf("%d\n", GetLegacyBoot(drive, ANY_VALID, index));
  208. break;
  209. case 'A':
  210. printf("0x%x\n", entry->attrs.fields.gpt_att);
  211. break;
  212. }
  213. } else {
  214. printf(TITLE_FMT, "start", "size", "part", "contents");
  215. EntryDetails(entry, index, params->numeric);
  216. }
  217. } else if (params->quick) { // show all partitions, quickly
  218. uint32_t i;
  219. GptEntry *entry;
  220. char type[GUID_STRLEN];
  221. for (i = 0; i < GetNumberOfEntries(drive); ++i) {
  222. entry = GetEntry(&drive->gpt, ANY_VALID, i);
  223. if (GuidIsZero(&entry->type))
  224. continue;
  225. if (!params->numeric && CGPT_OK == ResolveType(&entry->type, type)) {
  226. } else {
  227. GuidToStr(&entry->type, type, GUID_STRLEN);
  228. }
  229. printf(PARTITION_FMT, (int)entry->starting_lba,
  230. (int)(entry->ending_lba - entry->starting_lba + 1),
  231. i+1, type);
  232. }
  233. } else { // show all partitions
  234. GptEntry *entries;
  235. if (CGPT_OK != ReadPMBR(drive)) {
  236. Error("Unable to read PMBR\n");
  237. return CGPT_FAILED;
  238. }
  239. printf(TITLE_FMT, "start", "size", "part", "contents");
  240. char buf[256]; // buffer for formatted PMBR content
  241. PMBRToStr(&drive->pmbr, buf, sizeof(buf)); // will exit if buf is too small
  242. printf(GPT_FMT, 0, GPT_PMBR_SECTORS, "", buf);
  243. if (drive->gpt.ignored & MASK_PRIMARY) {
  244. printf(GPT_FMT, (int)GPT_PMBR_SECTORS,
  245. (int)GPT_HEADER_SECTORS, "IGNORED", "Pri GPT header");
  246. } else {
  247. if (drive->gpt.valid_headers & MASK_PRIMARY) {
  248. printf(GPT_FMT, (int)GPT_PMBR_SECTORS,
  249. (int)GPT_HEADER_SECTORS, "", "Pri GPT header");
  250. } else {
  251. printf(GPT_FMT, (int)GPT_PMBR_SECTORS,
  252. (int)GPT_HEADER_SECTORS, "INVALID", "Pri GPT header");
  253. }
  254. if (params->debug ||
  255. ((drive->gpt.valid_headers & MASK_PRIMARY) && params->verbose)) {
  256. GptHeader *header;
  257. char indent[64];
  258. require(snprintf(indent, sizeof(indent), GPT_MORE) < sizeof(indent));
  259. header = (GptHeader*)drive->gpt.primary_header;
  260. entries = (GptEntry*)drive->gpt.primary_entries;
  261. HeaderDetails(header, entries, indent, params->numeric);
  262. }
  263. GptHeader* primary_header = (GptHeader*)drive->gpt.primary_header;
  264. printf(GPT_FMT, (int)primary_header->entries_lba,
  265. (int)CalculateEntriesSectors(primary_header),
  266. drive->gpt.valid_entries & MASK_PRIMARY ? "" : "INVALID",
  267. "Pri GPT table");
  268. if (params->debug ||
  269. (drive->gpt.valid_entries & MASK_PRIMARY))
  270. EntriesDetails(drive, PRIMARY, params->numeric);
  271. }
  272. /****************************** Secondary *************************/
  273. if (drive->gpt.ignored & MASK_SECONDARY) {
  274. printf(GPT_FMT, (int)(drive->gpt.gpt_drive_sectors - GPT_HEADER_SECTORS),
  275. (int)GPT_HEADER_SECTORS, "IGNORED", "Sec GPT header");
  276. } else {
  277. GptHeader* secondary_header = (GptHeader*)drive->gpt.secondary_header;
  278. printf(GPT_FMT, (int)secondary_header->entries_lba,
  279. (int)CalculateEntriesSectors(secondary_header),
  280. drive->gpt.valid_entries & MASK_SECONDARY ? "" : "INVALID",
  281. "Sec GPT table");
  282. /* We show secondary table details if any of following is true.
  283. * 1. in debug mode.
  284. * 2. primary table is being ignored
  285. * 3. only secondary is valid.
  286. * 4. secondary is not identical to promary.
  287. */
  288. if (params->debug || (drive->gpt.ignored & MASK_PRIMARY) ||
  289. ((drive->gpt.valid_entries & MASK_SECONDARY) &&
  290. (!(drive->gpt.valid_entries & MASK_PRIMARY) ||
  291. memcmp(drive->gpt.primary_entries, drive->gpt.secondary_entries,
  292. secondary_header->number_of_entries *
  293. secondary_header->size_of_entry)))) {
  294. EntriesDetails(drive, SECONDARY, params->numeric);
  295. }
  296. if (drive->gpt.valid_headers & MASK_SECONDARY) {
  297. printf(GPT_FMT, (int)(drive->gpt.gpt_drive_sectors - GPT_HEADER_SECTORS),
  298. (int)GPT_HEADER_SECTORS, "", "Sec GPT header");
  299. } else {
  300. printf(GPT_FMT, (int)GPT_PMBR_SECTORS,
  301. (int)GPT_HEADER_SECTORS, "INVALID", "Sec GPT header");
  302. }
  303. /* We show secondary header if any of following is true:
  304. * 1. in debug mode.
  305. * 2. only secondary is valid.
  306. * 3. secondary is not synonymous to primary and not ignored.
  307. */
  308. if (params->debug ||
  309. ((drive->gpt.valid_headers & MASK_SECONDARY) &&
  310. (!(drive->gpt.valid_headers & MASK_PRIMARY) ||
  311. !IsSynonymous((GptHeader*)drive->gpt.primary_header,
  312. (GptHeader*)drive->gpt.secondary_header)) &&
  313. params->verbose)) {
  314. GptHeader *header;
  315. char indent[64];
  316. require(snprintf(indent, sizeof(indent), GPT_MORE) < sizeof(indent));
  317. header = (GptHeader*)drive->gpt.secondary_header;
  318. entries = (GptEntry*)drive->gpt.secondary_entries;
  319. HeaderDetails(header, entries, indent, params->numeric);
  320. }
  321. }
  322. }
  323. CheckValid(drive);
  324. return CGPT_OK;
  325. }
  326. int CgptShow(CgptShowParams *params) {
  327. struct drive drive;
  328. if (params == NULL)
  329. return CGPT_FAILED;
  330. if (CGPT_OK != DriveOpen(params->drive_name, &drive, O_RDONLY,
  331. params->drive_size))
  332. return CGPT_FAILED;
  333. if (GptShow(&drive, params))
  334. return CGPT_FAILED;
  335. DriveClose(&drive, 0);
  336. return CGPT_OK;
  337. }