| OLD | NEW |
| 1 /* Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. | 3 * found in the LICENSE file. |
| 4 */ | 4 */ |
| 5 | 5 |
| 6 #include "cgptlib.h" | 6 #include "cgptlib.h" |
| 7 #include "cgptlib_internal.h" | 7 #include "cgptlib_internal.h" |
| 8 #include "crc32.h" | 8 #include "crc32.h" |
| 9 #include "gpt.h" | 9 #include "gpt.h" |
| 10 #include "utility.h" | 10 #include "utility.h" |
| 11 | 11 |
| 12 /* global types to compare against */ | 12 /* global types to compare against */ |
| 13 const Guid guid_unused = GPT_ENT_TYPE_UNUSED; | 13 const Guid guid_unused = GPT_ENT_TYPE_UNUSED; |
| 14 const Guid guid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; | 14 const Guid guid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL; |
| 15 | 15 |
| 16 | 16 |
| 17 int GptInit(GptData *gpt) { | 17 int GptInit(GptData *gpt) { |
| 18 int retval; | 18 int retval; |
| 19 | 19 |
| 20 gpt->modified = 0; | 20 gpt->modified = 0; |
| 21 gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND; | 21 gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND; |
| 22 gpt->current_priority = 999; | 22 gpt->current_priority = 999; |
| 23 | 23 |
| 24 retval = GptSanityCheck(gpt); | 24 retval = GptSanityCheck(gpt); |
| 25 if (GPT_SUCCESS != retval) | 25 if (GPT_SUCCESS != retval) { |
| 26 debug("GptInit() failed sanity check\n"); |
| 26 return retval; | 27 return retval; |
| 28 } |
| 27 | 29 |
| 28 GptRepair(gpt); | 30 GptRepair(gpt); |
| 29 return GPT_SUCCESS; | 31 return GPT_SUCCESS; |
| 30 } | 32 } |
| 31 | 33 |
| 32 | 34 |
| 33 int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size) { | 35 int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size) { |
| 34 GptHeader* header = (GptHeader*)gpt->primary_header; | 36 GptHeader* header = (GptHeader*)gpt->primary_header; |
| 35 GptEntry* entries = (GptEntry*)gpt->primary_entries; | 37 GptEntry* entries = (GptEntry*)gpt->primary_entries; |
| 36 GptEntry* e; | 38 GptEntry* e; |
| 37 int new_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND; | 39 int new_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND; |
| 38 int new_prio = 0; | 40 int new_prio = 0; |
| 39 int i; | 41 uint32_t i; |
| 40 | 42 |
| 41 /* If we already found a kernel, continue the scan at the current | 43 /* If we already found a kernel, continue the scan at the current |
| 42 * kernel's prioity, in case there is another kernel with the same | 44 * kernel's prioity, in case there is another kernel with the same |
| 43 * priority. */ | 45 * priority. */ |
| 44 if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) { | 46 if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) { |
| 45 for (i = gpt->current_kernel + 1; i < header->number_of_entries; i++) { | 47 for (i = gpt->current_kernel + 1; i < header->number_of_entries; i++) { |
| 46 e = entries + i; | 48 e = entries + i; |
| 47 if (!IsKernelEntry(e)) | 49 if (!IsKernelEntry(e)) |
| 48 continue; | 50 continue; |
| 51 debug("GptNextKernelEntry looking at same prio partition %d\n", i); |
| 52 debug("GptNextKernelEntry s%d t%d p%d\n", |
| 53 GetEntrySuccessful(e), GetEntryTries(e), GetEntryPriority(e)); |
| 49 if (!(GetEntrySuccessful(e) || GetEntryTries(e))) | 54 if (!(GetEntrySuccessful(e) || GetEntryTries(e))) |
| 50 continue; | 55 continue; |
| 51 if (GetEntryPriority(e) == gpt->current_priority) { | 56 if (GetEntryPriority(e) == gpt->current_priority) { |
| 52 gpt->current_kernel = i; | 57 gpt->current_kernel = i; |
| 53 *start_sector = e->starting_lba; | 58 *start_sector = e->starting_lba; |
| 54 *size = e->ending_lba - e->starting_lba + 1; | 59 *size = e->ending_lba - e->starting_lba + 1; |
| 60 debug("GptNextKernelEntry likes that one\n"); |
| 55 return GPT_SUCCESS; | 61 return GPT_SUCCESS; |
| 56 } | 62 } |
| 57 } | 63 } |
| 58 } | 64 } |
| 59 | 65 |
| 60 /* We're still here, so scan for the remaining kernel with the | 66 /* We're still here, so scan for the remaining kernel with the |
| 61 * highest priority less than the previous attempt. */ | 67 * highest priority less than the previous attempt. */ |
| 62 for (i = 0, e = entries; i < header->number_of_entries; i++, e++) { | 68 for (i = 0, e = entries; i < header->number_of_entries; i++, e++) { |
| 63 int current_prio = GetEntryPriority(e); | 69 int current_prio = GetEntryPriority(e); |
| 64 if (!IsKernelEntry(e)) | 70 if (!IsKernelEntry(e)) |
| 65 continue; | 71 continue; |
| 72 debug("GptNextKernelEntry looking at new prio partition %d\n", i); |
| 73 debug("GptNextKernelEntry s%d t%d p%d\n", |
| 74 GetEntrySuccessful(e), GetEntryTries(e), GetEntryPriority(e)); |
| 66 if (!(GetEntrySuccessful(e) || GetEntryTries(e))) | 75 if (!(GetEntrySuccessful(e) || GetEntryTries(e))) |
| 67 continue; | 76 continue; |
| 68 if (current_prio >= gpt->current_priority) | 77 if (current_prio >= gpt->current_priority) |
| 69 continue; /* Already returned this kernel in a previous call */ | 78 continue; /* Already returned this kernel in a previous call */ |
| 70 if (current_prio > new_prio) { | 79 if (current_prio > new_prio) { |
| 71 new_kernel = i; | 80 new_kernel = i; |
| 72 new_prio = current_prio; | 81 new_prio = current_prio; |
| 73 } | 82 } |
| 74 } | 83 } |
| 75 | 84 |
| 76 /* Save what we found. Note that if we didn't find a new kernel, | 85 /* Save what we found. Note that if we didn't find a new kernel, |
| 77 * new_prio will still be -1, so future calls to this function will | 86 * new_prio will still be -1, so future calls to this function will |
| 78 * also fail. */ | 87 * also fail. */ |
| 79 gpt->current_kernel = new_kernel; | 88 gpt->current_kernel = new_kernel; |
| 80 gpt->current_priority = new_prio; | 89 gpt->current_priority = new_prio; |
| 81 | 90 |
| 82 if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) | 91 if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) { |
| 92 debug("GptNextKernelEntry no more kernels\n"); |
| 83 return GPT_ERROR_NO_VALID_KERNEL; | 93 return GPT_ERROR_NO_VALID_KERNEL; |
| 94 } |
| 84 | 95 |
| 96 debug("GptNextKernelEntry likes that one\n"); |
| 85 e = entries + new_kernel; | 97 e = entries + new_kernel; |
| 86 *start_sector = e->starting_lba; | 98 *start_sector = e->starting_lba; |
| 87 *size = e->ending_lba - e->starting_lba + 1; | 99 *size = e->ending_lba - e->starting_lba + 1; |
| 88 return GPT_SUCCESS; | 100 return GPT_SUCCESS; |
| 89 } | 101 } |
| 90 | 102 |
| 91 | 103 |
| 92 int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type) { | 104 int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type) { |
| 93 GptHeader* header = (GptHeader*)gpt->primary_header; | 105 GptHeader* header = (GptHeader*)gpt->primary_header; |
| 94 GptEntry* entries = (GptEntry*)gpt->primary_entries; | 106 GptEntry* entries = (GptEntry*)gpt->primary_entries; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 152 |
| 141 /* Use the repair function to update the other copy of the GPT. | 153 /* Use the repair function to update the other copy of the GPT. |
| 142 * This is a tad inefficient, but is much faster than the disk I/O | 154 * This is a tad inefficient, but is much faster than the disk I/O |
| 143 * to update the GPT on disk so it doesn't matter. */ | 155 * to update the GPT on disk so it doesn't matter. */ |
| 144 gpt->valid_headers = MASK_PRIMARY; | 156 gpt->valid_headers = MASK_PRIMARY; |
| 145 gpt->valid_entries = MASK_PRIMARY; | 157 gpt->valid_entries = MASK_PRIMARY; |
| 146 GptRepair(gpt); | 158 GptRepair(gpt); |
| 147 | 159 |
| 148 return GPT_SUCCESS; | 160 return GPT_SUCCESS; |
| 149 } | 161 } |
| OLD | NEW |