blob: 8b3b64895d6eb285b0c4c86f5618e2b6b332da10 [file] [log] [blame]
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -07001/* 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
3 * found in the LICENSE file.
4 */
5
Louis Yung-Chieh Lo0dce41c2010-05-17 22:45:30 -07006#include "cgptlib.h"
Louis Yung-Chieh Lo0dce41c2010-05-17 22:45:30 -07007#include "cgptlib_internal.h"
Louis Yung-Chieh Lo49fa8e52010-04-30 16:10:48 -07008#include "crc32.h"
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -07009#include "gpt.h"
10#include "utility.h"
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -070011
Bill Richardson77d26e52010-06-04 12:26:42 -070012/* global types to compare against */
13const Guid guid_unused = GPT_ENT_TYPE_UNUSED;
14const Guid guid_chromeos_kernel = GPT_ENT_TYPE_CHROMEOS_KERNEL;
15
Louis Yung-Chieh Lo418ad3b2010-05-27 11:21:17 +080016
Louis Yung-Chieh Lo418ad3b2010-05-27 11:21:17 +080017int GptInit(GptData *gpt) {
18 int retval;
19
Louis Yung-Chieh Lo418ad3b2010-05-27 11:21:17 +080020 gpt->modified = 0;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070021 gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070022 gpt->current_priority = 999;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070023
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070024 retval = GptSanityCheck(gpt);
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070025 if (GPT_SUCCESS != retval) {
26 debug("GptInit() failed sanity check\n");
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070027 return retval;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070028 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070029
30 GptRepair(gpt);
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -070031 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -070032}
33
Louis Yung-Chieh Lo49fa8e52010-04-30 16:10:48 -070034
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070035int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size) {
36 GptHeader* header = (GptHeader*)gpt->primary_header;
37 GptEntry* entries = (GptEntry*)gpt->primary_entries;
38 GptEntry* e;
39 int new_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
40 int new_prio = 0;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070041 uint32_t i;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070042
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070043 /* If we already found a kernel, continue the scan at the current
44 * kernel's prioity, in case there is another kernel with the same
45 * priority. */
46 if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) {
47 for (i = gpt->current_kernel + 1; i < header->number_of_entries; i++) {
48 e = entries + i;
49 if (!IsKernelEntry(e))
50 continue;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070051 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));
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070054 if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
55 continue;
56 if (GetEntryPriority(e) == gpt->current_priority) {
57 gpt->current_kernel = i;
58 *start_sector = e->starting_lba;
59 *size = e->ending_lba - e->starting_lba + 1;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070060 debug("GptNextKernelEntry likes that one\n");
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070061 return GPT_SUCCESS;
62 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070063 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070064 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070065
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070066 /* We're still here, so scan for the remaining kernel with the
67 * highest priority less than the previous attempt. */
68 for (i = 0, e = entries; i < header->number_of_entries; i++, e++) {
69 int current_prio = GetEntryPriority(e);
70 if (!IsKernelEntry(e))
71 continue;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070072 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));
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070075 if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
76 continue;
77 if (current_prio >= gpt->current_priority)
78 continue; /* Already returned this kernel in a previous call */
79 if (current_prio > new_prio) {
80 new_kernel = i;
81 new_prio = current_prio;
82 }
83 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070084
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070085 /* Save what we found. Note that if we didn't find a new kernel,
86 * new_prio will still be -1, so future calls to this function will
87 * also fail. */
88 gpt->current_kernel = new_kernel;
89 gpt->current_priority = new_prio;
90
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070091 if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel) {
92 debug("GptNextKernelEntry no more kernels\n");
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070093 return GPT_ERROR_NO_VALID_KERNEL;
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070094 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070095
Randall Spanglerbeb5bae2010-06-21 16:33:26 -070096 debug("GptNextKernelEntry likes that one\n");
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070097 e = entries + new_kernel;
98 *start_sector = e->starting_lba;
99 *size = e->ending_lba - e->starting_lba + 1;
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -0700100 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -0700101}
102
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700103
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700104int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type) {
105 GptHeader* header = (GptHeader*)gpt->primary_header;
106 GptEntry* entries = (GptEntry*)gpt->primary_entries;
107 GptEntry* e = entries + gpt->current_kernel;
vbendebf7a45cc2010-06-21 08:44:16 -0700108 uint16_t previous_attr = e->attrs.fields.gpt_att;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700109
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700110 if (gpt->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND)
111 return GPT_ERROR_INVALID_UPDATE_TYPE;
112 if (!IsKernelEntry(e))
113 return GPT_ERROR_INVALID_UPDATE_TYPE;
114
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700115 switch (update_type) {
116 case GPT_UPDATE_ENTRY_TRY: {
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700117 /* Used up a try */
118 int tries;
119 if (GetEntrySuccessful(e))
120 return GPT_SUCCESS; /* Successfully booted this partition, so
121 * tries field is ignored. */
122 tries = GetEntryTries(e);
123 if (tries > 1) {
124 /* Still have tries left */
125 SetEntryTries(e, tries - 1);
126 break;
127 }
128 /* Out of tries, so drop through and mark partition bad. */
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700129 }
130 case GPT_UPDATE_ENTRY_BAD: {
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700131 /* Giving up on this partition entirely. */
vbendebf7a45cc2010-06-21 08:44:16 -0700132 e->attrs.fields.gpt_att = previous_attr & ~(
133 CGPT_ATTRIBUTE_SUCCESSFUL_MASK |
134 CGPT_ATTRIBUTE_TRIES_MASK |
135 CGPT_ATTRIBUTE_PRIORITY_MASK);
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700136 break;
137 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700138 default:
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700139 return GPT_ERROR_INVALID_UPDATE_TYPE;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700140 }
141
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700142 /* If no change to attributes, we're done */
vbendebf7a45cc2010-06-21 08:44:16 -0700143 if (e->attrs.fields.gpt_att == previous_attr)
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700144 return GPT_SUCCESS;
145
146 /* Update the CRCs */
147 header->entries_crc32 = Crc32((const uint8_t *)entries,
148 header->size_of_entry *
149 header->number_of_entries);
150 header->header_crc32 = HeaderCrc(header);
151 gpt->modified |= GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1;
152
153 /* Use the repair function to update the other copy of the GPT.
154 * This is a tad inefficient, but is much faster than the disk I/O
155 * to update the GPT on disk so it doesn't matter. */
156 gpt->valid_headers = MASK_PRIMARY;
157 gpt->valid_entries = MASK_PRIMARY;
158 GptRepair(gpt);
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700159
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -0700160 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -0700161}