| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "2common.h" |
| 7 |
#include "2sysincludes.h" |
| 8 |
#include "cgptlib.h" |
| 9 |
#include "cgptlib_internal.h" |
| 10 |
#include "crc32.h" |
| 11 |
#include "gpt.h" |
| 12 |
#include "vboot_api.h" |
| 13 |
|
| 14 |
int GptInit(GptData *gpt) |
| 15 |
{ |
| 16 |
int retval; |
| 17 |
|
| 18 |
gpt->modified = 0; |
| 19 |
gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND; |
| 20 |
gpt->current_priority = 999; |
| 21 |
|
| 22 |
retval = GptValidityCheck(gpt); |
| 23 |
if (GPT_SUCCESS != retval) { |
| 24 |
VB2_DEBUG("GptInit() failed validity check\n"); |
| 25 |
return retval; |
| 26 |
} |
| 27 |
|
| 28 |
GptRepair(gpt); |
| 29 |
return GPT_SUCCESS; |
| 30 |
} |
| 31 |
|
| 32 |
int GptNextKernelEntry(GptData *gpt, uint64_t *start_sector, uint64_t *size) |
| 33 |
{ |
| 34 |
GptHeader *header = (GptHeader *)gpt->primary_header; |
| 35 |
GptEntry *entries = (GptEntry *)gpt->primary_entries; |
| 36 |
GptEntry *e; |
| 37 |
int new_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND; |
| 38 |
int new_prio = 0; |
| 39 |
uint32_t i; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) { |
| 47 |
for (i = gpt->current_kernel + 1; |
| 48 |
i < header->number_of_entries; i++) { |
| 49 |
e = entries + i; |
| 50 |
if (!IsKernelEntry(e)) |
| 51 |
continue; |
| 52 |
VB2_DEBUG("GptNextKernelEntry looking at same prio " |
| 53 |
"partition %d\n", i+1); |
| 54 |
VB2_DEBUG("GptNextKernelEntry s%d t%d p%d\n", |
| 55 |
GetEntrySuccessful(e), GetEntryTries(e), |
| 56 |
GetEntryPriority(e)); |
| 57 |
if (!(GetEntrySuccessful(e) || GetEntryTries(e))) |
| 58 |
continue; |
| 59 |
if (GetEntryPriority(e) == gpt->current_priority) { |
| 60 |
gpt->current_kernel = i; |
| 61 |
*start_sector = e->starting_lba; |
| 62 |
*size = e->ending_lba - e->starting_lba + 1; |
| 63 |
VB2_DEBUG("GptNextKernelEntry likes it\n"); |
| 64 |
return GPT_SUCCESS; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
for (i = 0, e = entries; i < header->number_of_entries; i++, e++) { |
| 74 |
int current_prio = GetEntryPriority(e); |
| 75 |
if (!IsKernelEntry(e)) |
| 76 |
continue; |
| 77 |
VB2_DEBUG("GptNextKernelEntry looking at new prio " |
| 78 |
"partition %d\n", i+1); |
| 79 |
VB2_DEBUG("GptNextKernelEntry s%d t%d p%d\n", |
| 80 |
GetEntrySuccessful(e), GetEntryTries(e), |
| 81 |
GetEntryPriority(e)); |
| 82 |
if (!(GetEntrySuccessful(e) || GetEntryTries(e))) |
| 83 |
continue; |
| 84 |
if (current_prio >= gpt->current_priority) { |
| 85 |
|
| 86 |
continue; |
| 87 |
} |
| 88 |
if (current_prio > new_prio) { |
| 89 |
new_kernel = i; |
| 90 |
new_prio = current_prio; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
gpt->current_kernel = new_kernel; |
| 100 |
gpt->current_priority = new_prio; |
| 101 |
|
| 102 |
if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) { |
| 103 |
VB2_DEBUG("GptNextKernelEntry no more kernels\n"); |
| 104 |
return GPT_ERROR_NO_VALID_KERNEL; |
| 105 |
} |
| 106 |
|
| 107 |
VB2_DEBUG("GptNextKernelEntry likes partition %d\n", new_kernel + 1); |
| 108 |
e = entries + new_kernel; |
| 109 |
*start_sector = e->starting_lba; |
| 110 |
*size = e->ending_lba - e->starting_lba + 1; |
| 111 |
return GPT_SUCCESS; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
int GptUpdateKernelWithEntry(GptData *gpt, GptEntry *e, uint32_t update_type) |
| 120 |
{ |
| 121 |
int modified = 0; |
| 122 |
|
| 123 |
if (!IsKernelEntry(e)) |
| 124 |
return GPT_ERROR_INVALID_UPDATE_TYPE; |
| 125 |
|
| 126 |
switch (update_type) { |
| 127 |
case GPT_UPDATE_ENTRY_TRY: { |
| 128 |
|
| 129 |
int tries; |
| 130 |
if (GetEntrySuccessful(e)) { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
return GPT_SUCCESS; |
| 136 |
} |
| 137 |
tries = GetEntryTries(e); |
| 138 |
if (tries > 1) { |
| 139 |
|
| 140 |
modified = 1; |
| 141 |
SetEntryTries(e, tries - 1); |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
VBOOT_FALLTHROUGH; |
| 146 |
} |
| 147 |
case GPT_UPDATE_ENTRY_BAD: { |
| 148 |
|
| 149 |
if (!GetEntrySuccessful(e)) { |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
modified = 1; |
| 155 |
SetEntryTries(e, 0); |
| 156 |
SetEntryPriority(e, 0); |
| 157 |
} |
| 158 |
break; |
| 159 |
} |
| 160 |
case GPT_UPDATE_ENTRY_ACTIVE: { |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
modified = 1; |
| 166 |
SetEntryTries(e, 0); |
| 167 |
SetEntryPriority(e, 2); |
| 168 |
SetEntrySuccessful(e, 1); |
| 169 |
break; |
| 170 |
} |
| 171 |
case GPT_UPDATE_ENTRY_INVALID: { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
modified = 1; |
| 177 |
SetEntryTries(e, 0); |
| 178 |
SetEntryPriority(e, 0); |
| 179 |
SetEntrySuccessful(e, 0); |
| 180 |
break; |
| 181 |
} |
| 182 |
default: |
| 183 |
return GPT_ERROR_INVALID_UPDATE_TYPE; |
| 184 |
} |
| 185 |
|
| 186 |
if (modified) { |
| 187 |
GptModified(gpt); |
| 188 |
} |
| 189 |
|
| 190 |
return GPT_SUCCESS; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
int GptUpdateKernelEntry(GptData *gpt, uint32_t update_type) |
| 199 |
{ |
| 200 |
GptEntry *entries = (GptEntry *)gpt->primary_entries; |
| 201 |
GptEntry *e = entries + gpt->current_kernel; |
| 202 |
|
| 203 |
if (gpt->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND) |
| 204 |
return GPT_ERROR_INVALID_UPDATE_TYPE; |
| 205 |
|
| 206 |
return GptUpdateKernelWithEntry(gpt, e, update_type); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
GptEntry *GptFindNthEntry(GptData *gpt, const Guid *guid, unsigned int n) |
| 216 |
{ |
| 217 |
GptHeader *header = (GptHeader *)gpt->primary_header; |
| 218 |
GptEntry *entries = (GptEntry *)gpt->primary_entries; |
| 219 |
GptEntry *e; |
| 220 |
int i; |
| 221 |
|
| 222 |
for (i = 0, e = entries; i < header->number_of_entries; i++, e++) { |
| 223 |
if (!memcmp(&e->type, guid, sizeof(*guid))) { |
| 224 |
if (n == 0) |
| 225 |
return e; |
| 226 |
n--; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
return NULL; |
| 231 |
} |