cgptlib_internal.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_CGPTLIB_INTERNAL_H_
  6. #define VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_
  7. #include "sysincludes.h"
  8. #include "cgptlib.h"
  9. #include "gpt.h"
  10. /*
  11. * If gpt->current_kernel is this value, means either:
  12. * 1. an initial value before scanning GPT entries,
  13. * 2. after scanning, no any valid kernel is found.
  14. */
  15. #define CGPT_KERNEL_ENTRY_NOT_FOUND (-1)
  16. /*
  17. * Bit definitions and masks for GPT attributes.
  18. *
  19. * 63-61 -- (reserved)
  20. * 60 -- read-only
  21. * 59-57 -- (reserved)
  22. * 56 -- success
  23. * 55-52 -- tries
  24. * 51-48 -- priority
  25. * 47-3 -- UEFI: reserved for future use
  26. * 2 -- UEFI: Legacy BIOS bootable
  27. * 1 -- UEFI: partition is not mapped
  28. * 0 -- UEFI: partition is required
  29. */
  30. #define CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET (56 - 48)
  31. #define CGPT_ATTRIBUTE_MAX_SUCCESSFUL (1ULL)
  32. #define CGPT_ATTRIBUTE_SUCCESSFUL_MASK (CGPT_ATTRIBUTE_MAX_SUCCESSFUL << \
  33. CGPT_ATTRIBUTE_SUCCESSFUL_OFFSET)
  34. #define CGPT_ATTRIBUTE_TRIES_OFFSET (52 - 48)
  35. #define CGPT_ATTRIBUTE_MAX_TRIES (15ULL)
  36. #define CGPT_ATTRIBUTE_TRIES_MASK (CGPT_ATTRIBUTE_MAX_TRIES << \
  37. CGPT_ATTRIBUTE_TRIES_OFFSET)
  38. #define CGPT_ATTRIBUTE_PRIORITY_OFFSET (48 - 48)
  39. #define CGPT_ATTRIBUTE_MAX_PRIORITY (15ULL)
  40. #define CGPT_ATTRIBUTE_PRIORITY_MASK (CGPT_ATTRIBUTE_MAX_PRIORITY << \
  41. CGPT_ATTRIBUTE_PRIORITY_OFFSET)
  42. #define CGPT_ATTRIBUTE_LEGACY_BOOT_OFFSET (2)
  43. #define CGPT_ATTRIBUTE_MAX_LEGACY_BOOT (1ULL)
  44. /* Defines ChromeOS-specific limitation on GPT */
  45. #define MIN_SIZE_OF_HEADER 92
  46. #define MAX_SIZE_OF_HEADER 512
  47. #define MIN_SIZE_OF_ENTRY 128
  48. #define MAX_SIZE_OF_ENTRY 512
  49. #define SIZE_OF_ENTRY_MULTIPLE 8
  50. #define MIN_NUMBER_OF_ENTRIES 16
  51. #define MAX_NUMBER_OF_ENTRIES 128
  52. /* Defines GPT sizes */
  53. #define GPT_PMBR_SECTORS 1 /* size (in sectors) of PMBR */
  54. #define GPT_HEADER_SECTORS 1
  55. /*
  56. * Alias name of index in internal array for primary and secondary header and
  57. * entries.
  58. */
  59. enum {
  60. /* constants for index */
  61. PRIMARY = 0,
  62. SECONDARY = 1,
  63. ANY_VALID = 9999, /* accept any between primary and secondary */
  64. /* constants for bit mask */
  65. MASK_NONE = 0,
  66. MASK_PRIMARY = 1,
  67. MASK_SECONDARY = 2,
  68. MASK_BOTH = 3,
  69. };
  70. /**
  71. * Verify GptData parameters are sane.
  72. */
  73. int CheckParameters(GptData* gpt);
  74. /**
  75. * Check header fields.
  76. *
  77. * Returns 0 if header is valid, 1 if invalid.
  78. */
  79. int CheckHeader(GptHeader *h, int is_secondary,
  80. uint64_t streaming_drive_sectors,
  81. uint64_t gpt_drive_sectors, uint32_t flags);
  82. /**
  83. * Calculate and return the header CRC.
  84. */
  85. uint32_t HeaderCrc(GptHeader *h);
  86. /**
  87. * Check entries.
  88. *
  89. * Returns 0 if entries are valid, 1 if invalid.
  90. */
  91. int CheckEntries(GptEntry *entries, GptHeader *h);
  92. /**
  93. * Return 0 if the GptHeaders are the same for all fields which don't differ
  94. * between the primary and secondary headers - that is, all fields other than:
  95. *
  96. * my_lba
  97. * alternate_lba
  98. * entries_lba
  99. */
  100. int HeaderFieldsSame(GptHeader *h1, GptHeader *h2);
  101. /**
  102. * Check GptData, headers, entries.
  103. *
  104. * If successful, sets gpt->valid_headers and gpt->valid_entries and returns
  105. * GPT_SUCCESS.
  106. *
  107. * On error, returns a GPT_ERROR_* return code.
  108. */
  109. int GptSanityCheck(GptData *gpt);
  110. /**
  111. * Repair GPT data by copying from one set of valid headers/entries to the
  112. * other. Assumes GptSanityCheck() has been run to determine which headers
  113. * and/or entries are already valid.
  114. *
  115. * The caller must make sure that even if one of the entries table is invalid
  116. * then corresponding buffer is allocated and big enough to accommodate entries
  117. * from the other (good) table.
  118. */
  119. void GptRepair(GptData *gpt);
  120. /**
  121. * Called when the primary entries are modified and the CRCs need to be
  122. * recalculated and propagated to the secondary entries
  123. */
  124. void GptModified(GptData *gpt);
  125. /**
  126. * Return 1 if the entry is a Chrome OS kernel partition, else 0.
  127. */
  128. int IsKernelEntry(const GptEntry *e);
  129. /**
  130. * Copy the current kernel partition's UniquePartitionGuid to the dest.
  131. */
  132. void GetCurrentKernelUniqueGuid(GptData *gpt, void *dest);
  133. /**
  134. * Return a pointer to text describing the passed in error.
  135. */
  136. const char *GptErrorText(int error_code);
  137. /**
  138. * Return number of 512-byte sectors required to store the entries table.
  139. */
  140. size_t CalculateEntriesSectors(GptHeader* h);
  141. #endif /* VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_ */