blob: 2cf606204d87a52b0a2b56da3e4b3a7031f02a9e [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
Louis Yung-Chieh Lo418ad3b2010-05-27 11:21:17 +080012
Louis Yung-Chieh Lo418ad3b2010-05-27 11:21:17 +080013int GptInit(GptData *gpt) {
14 int retval;
15
Louis Yung-Chieh Lo418ad3b2010-05-27 11:21:17 +080016 gpt->modified = 0;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070017 gpt->current_kernel = CGPT_KERNEL_ENTRY_NOT_FOUND;
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070018 gpt->current_priority = 999;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070019
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070020 retval = GptSanityCheck(gpt);
21 if (GPT_SUCCESS != retval)
22 return retval;
23
24 GptRepair(gpt);
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -070025 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -070026}
27
Louis Yung-Chieh Lo49fa8e52010-04-30 16:10:48 -070028
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070029int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size) {
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 int i;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070036
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070037 /* If we already found a kernel, continue the scan at the current
38 * kernel's prioity, in case there is another kernel with the same
39 * priority. */
40 if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) {
41 for (i = gpt->current_kernel + 1; i < header->number_of_entries; i++) {
42 e = entries + i;
43 if (!IsKernelEntry(e))
44 continue;
45 if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
46 continue;
47 if (GetEntryPriority(e) == gpt->current_priority) {
48 gpt->current_kernel = i;
49 *start_sector = e->starting_lba;
50 *size = e->ending_lba - e->starting_lba + 1;
51 return GPT_SUCCESS;
52 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070053 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070054 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070055
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070056 /* We're still here, so scan for the remaining kernel with the
57 * highest priority less than the previous attempt. */
58 for (i = 0, e = entries; i < header->number_of_entries; i++, e++) {
59 int current_prio = GetEntryPriority(e);
60 if (!IsKernelEntry(e))
61 continue;
62 if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
63 continue;
64 if (current_prio >= gpt->current_priority)
65 continue; /* Already returned this kernel in a previous call */
66 if (current_prio > new_prio) {
67 new_kernel = i;
68 new_prio = current_prio;
69 }
70 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070071
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070072 /* Save what we found. Note that if we didn't find a new kernel,
73 * new_prio will still be -1, so future calls to this function will
74 * also fail. */
75 gpt->current_kernel = new_kernel;
76 gpt->current_priority = new_prio;
77
78 if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel)
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070079 return GPT_ERROR_NO_VALID_KERNEL;
80
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070081 e = entries + new_kernel;
82 *start_sector = e->starting_lba;
83 *size = e->ending_lba - e->starting_lba + 1;
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -070084 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -070085}
86
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070087
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070088int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type) {
89 GptHeader* header = (GptHeader*)gpt->primary_header;
90 GptEntry* entries = (GptEntry*)gpt->primary_entries;
91 GptEntry* e = entries + gpt->current_kernel;
92 uint64_t previous_attr = e->attributes;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070093
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070094 /* TODO: need a better return code for these errors? */
95 if (gpt->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND)
96 return GPT_ERROR_INVALID_UPDATE_TYPE;
97 if (!IsKernelEntry(e))
98 return GPT_ERROR_INVALID_UPDATE_TYPE;
99
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700100 switch (update_type) {
101 case GPT_UPDATE_ENTRY_TRY: {
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700102 /* Used up a try */
103 int tries;
104 if (GetEntrySuccessful(e))
105 return GPT_SUCCESS; /* Successfully booted this partition, so
106 * tries field is ignored. */
107 tries = GetEntryTries(e);
108 if (tries > 1) {
109 /* Still have tries left */
110 SetEntryTries(e, tries - 1);
111 break;
112 }
113 /* Out of tries, so drop through and mark partition bad. */
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700114 }
115 case GPT_UPDATE_ENTRY_BAD: {
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700116 /* Giving up on this partition entirely. */
117 e->attributes &= ~(CGPT_ATTRIBUTE_SUCCESSFUL_MASK |
118 CGPT_ATTRIBUTE_TRIES_MASK |
119 CGPT_ATTRIBUTE_PRIORITY_MASK);
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700120 break;
121 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700122 default:
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700123 return GPT_ERROR_INVALID_UPDATE_TYPE;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700124 }
125
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700126 /* If no change to attributes, we're done */
127 if (e->attributes == previous_attr)
128 return GPT_SUCCESS;
129
130 /* Update the CRCs */
131 header->entries_crc32 = Crc32((const uint8_t *)entries,
132 header->size_of_entry *
133 header->number_of_entries);
134 header->header_crc32 = HeaderCrc(header);
135 gpt->modified |= GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1;
136
137 /* Use the repair function to update the other copy of the GPT.
138 * This is a tad inefficient, but is much faster than the disk I/O
139 * to update the GPT on disk so it doesn't matter. */
140 gpt->valid_headers = MASK_PRIMARY;
141 gpt->valid_entries = MASK_PRIMARY;
142 GptRepair(gpt);
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700143
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -0700144 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -0700145}