cgptlib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include "2sysincludes.h"
  6. #include "2common.h"
  7. #include "sysincludes.h"
  8. #include "cgptlib.h"
  9. #include "cgptlib_internal.h"
  10. #include "crc32.h"
  11. #include "gpt.h"
  12. #include "utility.h"
  13. #include "vboot_api.h"
  14. int GptInit(GptData *gpt)
  15. {
  16. int retval;
  17. gpt->modified = 0;
  18. gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
  19. gpt->current_priority = 999;
  20. retval = GptSanityCheck(gpt);
  21. if (GPT_SUCCESS != retval) {
  22. VB2_DEBUG("GptInit() failed sanity check\n");
  23. return retval;
  24. }
  25. GptRepair(gpt);
  26. return GPT_SUCCESS;
  27. }
  28. int GptNextKernelEntry(GptData *gpt, uint64_t *start_sector, uint64_t *size)
  29. {
  30. GptHeader *header = (GptHeader *)gpt->primary_header;
  31. GptEntry *entries = (GptEntry *)gpt->primary_entries;
  32. GptEntry *e;
  33. int new_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
  34. int new_prio = 0;
  35. uint32_t i;
  36. /*
  37. * If we already found a kernel, continue the scan at the current
  38. * kernel's priority, in case there is another kernel with the same
  39. * priority.
  40. */
  41. if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) {
  42. for (i = gpt->current_kernel + 1;
  43. i < header->number_of_entries; i++) {
  44. e = entries + i;
  45. if (!IsKernelEntry(e))
  46. continue;
  47. VB2_DEBUG("GptNextKernelEntry looking at same prio "
  48. "partition %d\n", i+1);
  49. VB2_DEBUG("GptNextKernelEntry s%d t%d p%d\n",
  50. GetEntrySuccessful(e), GetEntryTries(e),
  51. GetEntryPriority(e));
  52. if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
  53. continue;
  54. if (GetEntryPriority(e) == gpt->current_priority) {
  55. gpt->current_kernel = i;
  56. *start_sector = e->starting_lba;
  57. *size = e->ending_lba - e->starting_lba + 1;
  58. VB2_DEBUG("GptNextKernelEntry likes it\n");
  59. return GPT_SUCCESS;
  60. }
  61. }
  62. }
  63. /*
  64. * We're still here, so scan for the remaining kernel with the highest
  65. * priority less than the previous attempt.
  66. */
  67. for (i = 0, e = entries; i < header->number_of_entries; i++, e++) {
  68. int current_prio = GetEntryPriority(e);
  69. if (!IsKernelEntry(e))
  70. continue;
  71. VB2_DEBUG("GptNextKernelEntry looking at new prio "
  72. "partition %d\n", i+1);
  73. VB2_DEBUG("GptNextKernelEntry s%d t%d p%d\n",
  74. GetEntrySuccessful(e), GetEntryTries(e),
  75. GetEntryPriority(e));
  76. if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
  77. continue;
  78. if (current_prio >= gpt->current_priority) {
  79. /* Already returned this kernel in a previous call */
  80. continue;
  81. }
  82. if (current_prio > new_prio) {
  83. new_kernel = i;
  84. new_prio = current_prio;
  85. }
  86. }
  87. /*
  88. * Save what we found. Note that if we didn't find a new kernel,
  89. * new_prio will still be -1, so future calls to this function will
  90. * also fail.
  91. */
  92. gpt->current_kernel = new_kernel;
  93. gpt->current_priority = new_prio;
  94. if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) {
  95. VB2_DEBUG("GptNextKernelEntry no more kernels\n");
  96. return GPT_ERROR_NO_VALID_KERNEL;
  97. }
  98. VB2_DEBUG("GptNextKernelEntry likes partition %d\n", new_kernel + 1);
  99. e = entries + new_kernel;
  100. *start_sector = e->starting_lba;
  101. *size = e->ending_lba - e->starting_lba + 1;
  102. return GPT_SUCCESS;
  103. }
  104. /*
  105. * Func: GptUpdateKernelWithEntry
  106. * Desc: This function updates the given kernel entry according to the provided
  107. * update_type.
  108. */
  109. int GptUpdateKernelWithEntry(GptData *gpt, GptEntry *e, uint32_t update_type)
  110. {
  111. int modified = 0;
  112. if (!IsKernelEntry(e))
  113. return GPT_ERROR_INVALID_UPDATE_TYPE;
  114. switch (update_type) {
  115. case GPT_UPDATE_ENTRY_TRY: {
  116. /* Used up a try */
  117. int tries;
  118. if (GetEntrySuccessful(e)) {
  119. /*
  120. * Successfully booted this partition, so tries field
  121. * is ignored.
  122. */
  123. return GPT_SUCCESS;
  124. }
  125. tries = GetEntryTries(e);
  126. if (tries > 1) {
  127. /* Still have tries left */
  128. modified = 1;
  129. SetEntryTries(e, tries - 1);
  130. break;
  131. }
  132. /* Out of tries, so drop through and mark partition bad. */
  133. }
  134. case GPT_UPDATE_ENTRY_BAD: {
  135. /* Giving up on this partition entirely. */
  136. if (!GetEntrySuccessful(e)) {
  137. /*
  138. * Only clear tries and priority if the successful bit
  139. * is not set.
  140. */
  141. modified = 1;
  142. SetEntryTries(e, 0);
  143. SetEntryPriority(e, 0);
  144. }
  145. break;
  146. }
  147. case GPT_UPDATE_ENTRY_ACTIVE: {
  148. /*
  149. * Used for fastboot mode. If kernel partition slot is marked
  150. * active, its GPT entry is marked with S1,P2,T0.
  151. */
  152. modified = 1;
  153. SetEntryTries(e, 0);
  154. SetEntryPriority(e, 2);
  155. SetEntrySuccessful(e, 1);
  156. break;
  157. }
  158. case GPT_UPDATE_ENTRY_INVALID: {
  159. /*
  160. * Used for fastboot mode. If kernel partition slot is marked
  161. * invalid, its GPT entry is marked with S0,P0,T0
  162. */
  163. modified = 1;
  164. SetEntryTries(e, 0);
  165. SetEntryPriority(e, 0);
  166. SetEntrySuccessful(e, 0);
  167. break;
  168. }
  169. default:
  170. return GPT_ERROR_INVALID_UPDATE_TYPE;
  171. }
  172. if (modified) {
  173. GptModified(gpt);
  174. }
  175. return GPT_SUCCESS;
  176. }
  177. /*
  178. * Func: GptUpdateKernelEntry
  179. * Desc: This function updates current_kernel entry with provided
  180. * update_type. If current_kernel is not set, then it returns error.
  181. */
  182. int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type)
  183. {
  184. GptEntry *entries = (GptEntry *)gpt->primary_entries;
  185. GptEntry *e = entries + gpt->current_kernel;
  186. if (gpt->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND)
  187. return GPT_ERROR_INVALID_UPDATE_TYPE;
  188. return GptUpdateKernelWithEntry(gpt, e, update_type);
  189. }
  190. /*
  191. * Func: GptFindNthEntry
  192. * Desc: This function returns the nth instance of parition entry matching the
  193. * partition type guid from the gpt table. Instance value starts from 0. If the
  194. * entry is not found it returns NULL.
  195. */
  196. GptEntry *GptFindNthEntry(GptData *gpt, const Guid *guid, unsigned int n)
  197. {
  198. GptHeader *header = (GptHeader *)gpt->primary_header;
  199. GptEntry *entries = (GptEntry *)gpt->primary_entries;
  200. GptEntry *e;
  201. int i;
  202. for (i = 0, e = entries; i < header->number_of_entries; i++, e++) {
  203. if (!memcmp(&e->type, guid, sizeof(*guid))) {
  204. if (n == 0)
  205. return e;
  206. n--;
  207. }
  208. }
  209. return NULL;
  210. }