cgpt.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #ifndef VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
  5. #define VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_
  6. #include <fcntl.h>
  7. #ifndef HAVE_MACOS
  8. #include <features.h>
  9. #endif
  10. #include <stdint.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "cgpt_endian.h"
  14. #include "cgptlib.h"
  15. #include "gpt.h"
  16. struct legacy_partition {
  17. uint8_t status;
  18. uint8_t f_head;
  19. uint8_t f_sect;
  20. uint8_t f_cyl;
  21. uint8_t type;
  22. uint8_t l_head;
  23. uint8_t l_sect;
  24. uint8_t l_cyl;
  25. uint32_t f_lba;
  26. uint32_t num_sect;
  27. } __attribute__((packed));
  28. // syslinux uses this format:
  29. struct pmbr {
  30. uint8_t bootcode[424];
  31. Guid boot_guid;
  32. uint32_t disk_id;
  33. uint8_t magic[2]; // 0x1d, 0x9a
  34. struct legacy_partition part[4];
  35. uint8_t sig[2]; // 0x55, 0xaa
  36. } __attribute__((packed));
  37. void PMBRToStr(struct pmbr *pmbr, char *str, unsigned int buflen);
  38. // Handle to the drive storing the GPT.
  39. struct drive {
  40. uint64_t size; /* total size (in bytes) */
  41. GptData gpt;
  42. struct pmbr pmbr;
  43. int fd; /* file descriptor */
  44. };
  45. // Opens a block device or file, loads raw GPT data from it.
  46. // 'mode' should be O_RDONLY or O_RDWR.
  47. // If 'drive_size' is 0, both the partitions and GPT structs reside on the same
  48. // 'drive_path'.
  49. // Otherwise, 'drive_size' is taken as the size of the device that all
  50. // partitions will reside on, and 'drive_path' is where we store GPT structs.
  51. //
  52. // Returns CGPT_FAILED if any error happens.
  53. // Returns CGPT_OK if success and information are stored in 'drive'. */
  54. int DriveOpen(const char *drive_path, struct drive *drive, int mode,
  55. uint64_t drive_size);
  56. int DriveClose(struct drive *drive, int update_as_needed);
  57. int CheckValid(const struct drive *drive);
  58. /* Loads sectors from 'drive'.
  59. * *buf is pointed to an allocated memory when returned, and should be
  60. * freed.
  61. *
  62. * drive -- open drive.
  63. * buf -- pointer to buffer pointer
  64. * sector -- offset of starting sector (in sectors)
  65. * sector_bytes -- bytes per sector
  66. * sector_count -- number of sectors to load
  67. *
  68. * Returns CGPT_OK for successful. Aborts if any error occurs.
  69. */
  70. int Load(struct drive *drive, uint8_t **buf,
  71. const uint64_t sector,
  72. const uint64_t sector_bytes,
  73. const uint64_t sector_count);
  74. /* Saves sectors to 'drive'.
  75. *
  76. * drive -- open drive
  77. * buf -- pointer to buffer
  78. * sector -- starting sector offset
  79. * sector_bytes -- bytes per sector
  80. * sector_count -- number of sector to save
  81. *
  82. * Returns CGPT_OK for successful, CGPT_FAILED for failed.
  83. */
  84. int Save(struct drive *drive, const uint8_t *buf,
  85. const uint64_t sector,
  86. const uint64_t sector_bytes,
  87. const uint64_t sector_count);
  88. /* GUID conversion functions. Accepted format:
  89. *
  90. * "C12A7328-F81F-11D2-BA4B-00A0C93EC93B"
  91. *
  92. * At least GUID_STRLEN bytes should be reserved in 'str' (included the tailing
  93. * '\0').
  94. */
  95. #define GUID_STRLEN 37
  96. int StrToGuid(const char *str, Guid *guid);
  97. void GuidToStr(const Guid *guid, char *str, unsigned int buflen);
  98. int GuidEqual(const Guid *guid1, const Guid *guid2);
  99. int IsZero(const Guid *guid);
  100. /* Constant global type values to compare against */
  101. extern const Guid guid_chromeos_firmware;
  102. extern const Guid guid_chromeos_kernel;
  103. extern const Guid guid_chromeos_rootfs;
  104. extern const Guid guid_linux_data;
  105. extern const Guid guid_chromeos_reserved;
  106. extern const Guid guid_efi;
  107. extern const Guid guid_unused;
  108. int ReadPMBR(struct drive *drive);
  109. int WritePMBR(struct drive *drive);
  110. /* Convert possibly unterminated UTF16 string to UTF8.
  111. * Caller must prepare enough space for UTF8, which could be up to
  112. * twice the byte length of UTF16 string plus the terminating '\0'.
  113. *
  114. * Return: CGPT_OK --- all character are converted successfully.
  115. * CGPT_FAILED --- convert error, i.e. output buffer is too short.
  116. */
  117. int UTF16ToUTF8(const uint16_t *utf16, unsigned int maxinput,
  118. uint8_t *utf8, unsigned int maxoutput);
  119. /* Convert null-terminated UTF8 string to UTF16.
  120. * Caller must prepare enough space for UTF16, which is the byte length of UTF8
  121. * plus the terminating 0x0000.
  122. *
  123. * Return: CGPT_OK --- all character are converted successfully.
  124. * CGPT_FAILED --- convert error, i.e. output buffer is too short.
  125. */
  126. int UTF8ToUTF16(const uint8_t *utf8, uint16_t *utf16, unsigned int maxoutput);
  127. /* Helper functions for supported GPT types. */
  128. int ResolveType(const Guid *type, char *buf);
  129. int SupportedType(const char *name, Guid *type);
  130. void PrintTypes(void);
  131. void EntryDetails(GptEntry *entry, uint32_t index, int raw);
  132. uint32_t GetNumberOfEntries(const struct drive *drive);
  133. GptEntry *GetEntry(GptData *gpt, int secondary, uint32_t entry_index);
  134. void SetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index,
  135. int legacy_boot);
  136. int GetLegacyBoot(struct drive *drive, int secondary, uint32_t entry_index);
  137. void SetPriority(struct drive *drive, int secondary, uint32_t entry_index,
  138. int priority);
  139. int GetPriority(struct drive *drive, int secondary, uint32_t entry_index);
  140. void SetTries(struct drive *drive, int secondary, uint32_t entry_index,
  141. int tries);
  142. int GetTries(struct drive *drive, int secondary, uint32_t entry_index);
  143. void SetSuccessful(struct drive *drive, int secondary, uint32_t entry_index,
  144. int success);
  145. int GetSuccessful(struct drive *drive, int secondary, uint32_t entry_index);
  146. void SetRaw(struct drive *drive, int secondary, uint32_t entry_index,
  147. uint32_t raw);
  148. void UpdateAllEntries(struct drive *drive);
  149. uint8_t RepairHeader(GptData *gpt, const uint32_t valid_headers);
  150. uint8_t RepairEntries(GptData *gpt, const uint32_t valid_entries);
  151. void UpdateCrc(GptData *gpt);
  152. int IsSynonymous(const GptHeader* a, const GptHeader* b);
  153. int IsUnused(struct drive *drive, int secondary, uint32_t index);
  154. int IsKernel(struct drive *drive, int secondary, uint32_t index);
  155. // Optional. Applications that need this must provide an implementation.
  156. //
  157. // Explanation:
  158. // Some external utilities need to manipulate the GPT, but don't create new
  159. // partitions from scratch. The cgpt executable uses libuuid to provide this
  160. // functionality, but we don't want to have to build or install a separate
  161. // instance of that library just for the 32-bit static post-install tool,
  162. // which doesn't need this function.
  163. int GenerateGuid(Guid *newguid);
  164. // For usage and error messages.
  165. void Error(const char *format, ...);
  166. void Warning(const char *format, ...);
  167. // Command functions.
  168. int check_int_parse(char option, const char *buf);
  169. int check_int_limit(char option, int val, int low, int high);
  170. int cmd_show(int argc, char *argv[]);
  171. int cmd_repair(int argc, char *argv[]);
  172. int cmd_create(int argc, char *argv[]);
  173. int cmd_add(int argc, char *argv[]);
  174. int cmd_boot(int argc, char *argv[]);
  175. int cmd_find(int argc, char *argv[]);
  176. int cmd_prioritize(int argc, char *argv[]);
  177. int cmd_legacy(int argc, char *argv[]);
  178. #define ARRAY_COUNT(array) (sizeof(array)/sizeof((array)[0]))
  179. const char *GptError(int errnum);
  180. // Size in chars of the GPT Entry's PartitionName field
  181. #define GPT_PARTNAME_LEN 72
  182. /* The standard "assert" macro goes away when NDEBUG is defined. This doesn't.
  183. */
  184. #define require(A) do { \
  185. if (!(A)) { \
  186. fprintf(stderr, "condition (%s) failed at %s:%d\n", \
  187. #A, __FILE__, __LINE__); \
  188. exit(1); } \
  189. } while (0)
  190. #endif // VBOOT_REFERENCE_UTILITY_CGPT_CGPT_H_