Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 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 | #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
| 6 | #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |
| 7 | |
| 8 | #define _GNU_SOURCE |
| 9 | #define _FILE_OFFSET_BITS 64 |
| 10 | #include <features.h> |
| 11 | #include <stdint.h> |
| 12 | #include "endian.h" |
| 13 | #include "gpt.h" |
| 14 | #include "cgptlib.h" |
| 15 | |
| 16 | |
| 17 | // Just for clarity |
| 18 | enum { |
| 19 | CGPT_OK = 0, |
| 20 | CGPT_FAILED, |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | struct legacy_partition { |
| 25 | uint8_t status; |
| 26 | uint8_t f_head; |
| 27 | uint8_t f_sect; |
| 28 | uint8_t f_cyl; |
| 29 | uint8_t type; |
| 30 | uint8_t l_head; |
| 31 | uint8_t l_sect; |
| 32 | uint8_t l_cyl; |
| 33 | uint32_t f_lba; |
| 34 | uint32_t num_sect; |
| 35 | } __attribute__((packed)); |
| 36 | |
| 37 | |
| 38 | // syslinux uses this format: |
| 39 | struct pmbr { |
| 40 | uint8_t bootcode[424]; |
| 41 | Guid boot_guid; |
| 42 | uint32_t disk_id; |
| 43 | uint8_t magic[2]; // 0x1d, 0x9a |
| 44 | struct legacy_partition part[4]; |
| 45 | uint8_t sig[2]; // 0x55, 0xaa |
| 46 | } __attribute__((packed)); |
| 47 | |
| 48 | void PMBRToStr(struct pmbr *pmbr, char *str); |
| 49 | |
| 50 | // Handle to the drive storing the GPT. |
| 51 | struct drive { |
| 52 | int fd; /* file descriptor */ |
| 53 | uint64_t size; /* total size (in bytes) */ |
| 54 | GptData gpt; |
| 55 | struct pmbr pmbr; |
| 56 | }; |
| 57 | |
| 58 | |
| 59 | int DriveOpen(const char *drive_path, struct drive *drive); |
| 60 | int DriveClose(struct drive *drive, int update_as_needed); |
| 61 | int CheckValid(const struct drive *drive); |
| 62 | |
| 63 | /* GUID conversion functions. Accepted format: |
| 64 | * |
| 65 | * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B" |
| 66 | * |
| 67 | * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing |
| 68 | * '\0'). |
| 69 | */ |
| 70 | #define GUID_STRLEN 37 |
| 71 | int StrToGuid(const char *str, Guid *guid); |
| 72 | void GuidToStr(const Guid *guid, char *str); |
| 73 | int IsZero(const Guid *guid); |
| 74 | |
| 75 | |
| 76 | int ReadPMBR(struct drive *drive); |
| 77 | int WritePMBR(struct drive *drive); |
| 78 | |
| 79 | |
| 80 | /* Convert UTF16 string to UTF8. Rewritten from gpt utility. |
| 81 | * Caller must prepare enough space for UTF8. The rough estimation is: |
| 82 | * |
| 83 | * utf8 length = bytecount(utf16) * 1.5 |
| 84 | */ |
| 85 | void UTF16ToUTF8(const uint16_t *utf16, uint8_t *utf8); |
| 86 | /* Convert UTF8 string to UTF16. Rewritten from gpt utility. |
| 87 | * Caller must prepare enough space for UTF16. The conservative estimation is: |
| 88 | * |
| 89 | * utf16 bytecount = bytecount(utf8) / 3 * 4 |
| 90 | */ |
| 91 | void UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16); |
| 92 | |
| 93 | /* Helper functions for supported GPT types. */ |
| 94 | int ResolveType(const Guid *type, char *buf); |
| 95 | int SupportedType(const char *name, Guid *type); |
| 96 | void PrintTypes(void); |
| 97 | void EntryDetails(GptEntry *entry, int index, int raw); |
| 98 | |
| 99 | uint32_t GetNumberOfEntries(const GptData *gpt); |
| 100 | GptEntry *GetEntry(GptData *gpt, int secondary, int entry_index); |
| 101 | void SetPriority(GptData *gpt, int secondary, int entry_index, int priority); |
| 102 | int GetPriority(GptData *gpt, int secondary, int entry_index); |
| 103 | void SetTries(GptData *gpt, int secondary, int entry_index, int tries); |
| 104 | int GetTries(GptData *gpt, int secondary, int entry_index); |
| 105 | void SetSuccessful(GptData *gpt, int secondary, int entry_index, int success); |
| 106 | int GetSuccessful(GptData *gpt, int secondary, int entry_index); |
| 107 | |
| 108 | uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers); |
| 109 | uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries); |
| 110 | void UpdateCrc(GptData *gpt); |
| 111 | int IsSynonymous(const GptHeader* a, const GptHeader* b); |
| 112 | |
| 113 | // For usage and error messages. |
| 114 | extern const char* progname; |
| 115 | extern const char* command; |
| 116 | void Error(const char *format, ...); |
| 117 | |
| 118 | |
| 119 | // Command functions. |
| 120 | int cmd_show(int argc, char *argv[]); |
| 121 | int cmd_repair(int argc, char *argv[]); |
| 122 | int cmd_create(int argc, char *argv[]); |
| 123 | int cmd_add(int argc, char *argv[]); |
| 124 | int cmd_boot(int argc, char *argv[]); |
Bill Richardson | 4a20931 | 2010-07-02 11:34:38 -0700 | [diff] [blame] | 125 | int cmd_find(int argc, char *argv[]); |
Bill Richardson | f1372d9 | 2010-06-11 09:15:55 -0700 | [diff] [blame] | 126 | |
| 127 | #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0])) |
| 128 | const char *GptError(int errnum); |
| 129 | |
| 130 | |
| 131 | #endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_ |