gpt_misc.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Copyright (c) 2013 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_CGPT_MISC_H_
  6. #define VBOOT_REFERENCE_CGPT_MISC_H_
  7. #include "gpt.h"
  8. #include "vboot_api.h"
  9. enum {
  10. GPT_SUCCESS = 0,
  11. GPT_ERROR_NO_VALID_KERNEL,
  12. GPT_ERROR_INVALID_HEADERS,
  13. GPT_ERROR_INVALID_ENTRIES,
  14. GPT_ERROR_INVALID_SECTOR_SIZE,
  15. GPT_ERROR_INVALID_SECTOR_NUMBER,
  16. GPT_ERROR_INVALID_UPDATE_TYPE,
  17. GPT_ERROR_CRC_CORRUPTED,
  18. GPT_ERROR_OUT_OF_REGION,
  19. GPT_ERROR_START_LBA_OVERLAP,
  20. GPT_ERROR_END_LBA_OVERLAP,
  21. GPT_ERROR_DUP_GUID,
  22. GPT_ERROR_INVALID_FLASH_GEOMETRY,
  23. GPT_ERROR_NO_SUCH_ENTRY,
  24. /* Number of errors */
  25. GPT_ERROR_COUNT
  26. };
  27. /* Bit masks for GptData.modified field. */
  28. #define GPT_MODIFIED_HEADER1 0x01
  29. #define GPT_MODIFIED_HEADER2 0x02
  30. #define GPT_MODIFIED_ENTRIES1 0x04
  31. #define GPT_MODIFIED_ENTRIES2 0x08
  32. /*
  33. * The 'update_type' of GptUpdateKernelEntry(). We expose TRY and BAD only
  34. * because those are what verified boot needs. For more precise control on GPT
  35. * attribute bits, please refer to gpt_internal.h.
  36. */
  37. enum {
  38. /*
  39. * System will be trying to boot the currently selected kernel
  40. * partition. Update its try count if necessary.
  41. */
  42. GPT_UPDATE_ENTRY_TRY = 1,
  43. /*
  44. * The currently selected kernel partition failed validation. Mark
  45. * entry as invalid.
  46. */
  47. GPT_UPDATE_ENTRY_BAD = 2,
  48. /*
  49. * Used for fastboot mode. If kernel partition slot is marked active,
  50. * its GPT entry is marked with S1,P2,T0.
  51. */
  52. GPT_UPDATE_ENTRY_ACTIVE = 3,
  53. /*
  54. * Used for fastboot mode. If kernel partition slot is marked invalid,
  55. * its GPT entry is marked with S0,P0,T0.
  56. */
  57. GPT_UPDATE_ENTRY_INVALID = 4,
  58. };
  59. /* If this bit is 1, the GPT is stored in another from the streaming data */
  60. #define GPT_FLAG_EXTERNAL 0x1
  61. /*
  62. * A note about stored_on_device and gpt_drive_sectors:
  63. *
  64. * This code is used by both the "cgpt" utility and depthcharge/vboot. ATM,
  65. * depthcharge does not have logic to properly setup stored_on_device and
  66. * gpt_drive_sectors, but it does do a memset(gpt, 0, sizeof(GptData)). And so,
  67. * GPT_STORED_ON_DEVICE should be 0 to make stored_on_device compatible with
  68. * present behavior. At the same time, in vboot_kernel:LoadKernel(), and
  69. * cgpt_common:GptLoad(), we need to have simple shims to set gpt_drive_sectors
  70. * to drive_sectors.
  71. *
  72. * TODO(namnguyen): Remove those shims when the firmware can set these fields.
  73. */
  74. typedef struct {
  75. /* Fill in the following fields before calling GptInit() */
  76. /* GPT primary header, from sector 1 of disk (size: 512 bytes) */
  77. uint8_t *primary_header;
  78. /* GPT secondary header, from last sector of disk (size: 512 bytes) */
  79. uint8_t *secondary_header;
  80. /* Primary GPT table, follows primary header */
  81. uint8_t *primary_entries;
  82. /* Secondary GPT table, precedes secondary header */
  83. uint8_t *secondary_entries;
  84. /* Size of a LBA sector, in bytes */
  85. uint32_t sector_bytes;
  86. /* Size of drive (that the partitions are on) in LBA sectors */
  87. uint64_t streaming_drive_sectors;
  88. /* Size of the device that holds the GPT structures, 512-byte sectors */
  89. uint64_t gpt_drive_sectors;
  90. /* Flags */
  91. uint32_t flags;
  92. /* Outputs */
  93. /* Which inputs have been modified? GPT_MODIFIED_* */
  94. uint8_t modified;
  95. /*
  96. * The current chromeos kernel index in partition table. -1 means not
  97. * found on drive. Note that GPT partition numbers are traditionally
  98. * 1-based, but we're using a zero-based index here.
  99. */
  100. int current_kernel;
  101. /* Internal variables */
  102. uint8_t valid_headers, valid_entries, ignored;
  103. int current_priority;
  104. } GptData;
  105. /**
  106. * Initializes the GPT data structure's internal state.
  107. *
  108. * The following fields must be filled before calling this function:
  109. *
  110. * primary_header
  111. * secondary_header
  112. * primary_entries
  113. * secondary_entries
  114. * sector_bytes
  115. * drive_sectors
  116. * stored_on_device
  117. * gpt_device_sectors
  118. *
  119. * On return the modified field may be set, if the GPT data has been modified
  120. * and should be written to disk.
  121. *
  122. * Returns GPT_SUCCESS if successful, non-zero if error:
  123. * GPT_ERROR_INVALID_HEADERS, both partition table headers are invalid, enters
  124. * recovery mode,
  125. * GPT_ERROR_INVALID_ENTRIES, both partition table entries are invalid, enters
  126. * recovery mode,
  127. * GPT_ERROR_INVALID_SECTOR_SIZE, size of a sector is not supported,
  128. * GPT_ERROR_INVALID_SECTOR_NUMBER, number of sectors in drive is invalid (too
  129. * small) */
  130. int GptInit(GptData *gpt);
  131. /**
  132. * Return the nth instance of parition entry matching the partition type guid
  133. * from the gpt table. Instance value starts from 0. If the entry is not found,
  134. * it returns NULL.
  135. */
  136. GptEntry *GptFindNthEntry(GptData *gpt, const Guid *guid, unsigned int n);
  137. /**
  138. * Allocate and read GPT data from the drive. The sector_bytes and
  139. * drive_sectors fields should be filled on input. The primary and secondary
  140. * header and entries are filled on output.
  141. *
  142. * Returns 0 if successful, 1 if error.
  143. */
  144. int AllocAndReadGptData(VbExDiskHandle_t disk_handle, GptData *gptdata);
  145. /**
  146. * Write any changes for the GPT data back to the drive, then free the buffers.
  147. */
  148. int WriteAndFreeGptData(VbExDiskHandle_t disk_handle, GptData *gptdata);
  149. /**
  150. * Return 1 if the entry is unused, 0 if it is used.
  151. */
  152. int IsUnusedEntry(const GptEntry *e);
  153. /**
  154. * Return size(in lba) of a partition represented by given GPT entry.
  155. */
  156. size_t GptGetEntrySizeLba(const GptEntry *e);
  157. /**
  158. * Return size(in bytes) of a partition represented by given GPT entry.
  159. */
  160. size_t GptGetEntrySizeBytes(const GptData *gpt, const GptEntry *e);
  161. /**
  162. * Updates the kernel entry with the specified index, using the specified type
  163. * of update (GPT_UPDATE_ENTRY_*).
  164. *
  165. * On return the modified field may be set, if the GPT data has been modified
  166. * and should be written to disk.
  167. *
  168. * Returns GPT_SUCCESS if successful, else
  169. * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given.
  170. */
  171. int GptUpdateKernelWithEntry(GptData *gpt, GptEntry *e, uint32_t update_type);
  172. /**
  173. * Updates the kernel entry identified by current_kernel field. If
  174. * current_kernel is not set it returns an error.
  175. *
  176. * Returns GPT_SUCCESS if successful, else
  177. * GPT_ERROR_INVALID_UPDATE_TYPE, invalid 'update_type' is given.
  178. */
  179. int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type);
  180. /* Getters and setters for partition attribute fields. */
  181. int GetEntryLegacyBoot(const GptEntry *e);
  182. int GetEntrySuccessful(const GptEntry *e);
  183. int GetEntryPriority(const GptEntry *e);
  184. int GetEntryTries(const GptEntry *e);
  185. void SetEntryLegacyBoot(GptEntry *e, int legacy_boot);
  186. void SetEntrySuccessful(GptEntry *e, int successful);
  187. void SetEntryPriority(GptEntry *e, int priority);
  188. void SetEntryTries(GptEntry *e, int tries);
  189. #endif /* VBOOT_REFERENCE_CGPT_MISC_H_ */