blob: 4d2ee6c0a803a2b9909a57727a09e7c8059185c3 [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);
25 if (GPT_SUCCESS != retval)
26 return retval;
27
28 GptRepair(gpt);
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -070029 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -070030}
31
Louis Yung-Chieh Lo49fa8e52010-04-30 16:10:48 -070032
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070033int GptNextKernelEntry(GptData* gpt, uint64_t* start_sector, uint64_t* size) {
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 int i;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070040
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070041 /* 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
43 * priority. */
44 if (gpt->current_kernel != CGPT_KERNEL_ENTRY_NOT_FOUND) {
45 for (i = gpt->current_kernel + 1; i < header->number_of_entries; i++) {
46 e = entries + i;
47 if (!IsKernelEntry(e))
48 continue;
49 if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
50 continue;
51 if (GetEntryPriority(e) == gpt->current_priority) {
52 gpt->current_kernel = i;
53 *start_sector = e->starting_lba;
54 *size = e->ending_lba - e->starting_lba + 1;
55 return GPT_SUCCESS;
56 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070057 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070058 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070059
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070060 /* We're still here, so scan for the remaining kernel with the
61 * highest priority less than the previous attempt. */
62 for (i = 0, e = entries; i < header->number_of_entries; i++, e++) {
63 int current_prio = GetEntryPriority(e);
64 if (!IsKernelEntry(e))
65 continue;
66 if (!(GetEntrySuccessful(e) || GetEntryTries(e)))
67 continue;
68 if (current_prio >= gpt->current_priority)
69 continue; /* Already returned this kernel in a previous call */
70 if (current_prio > new_prio) {
71 new_kernel = i;
72 new_prio = current_prio;
73 }
74 }
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070075
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070076 /* 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
78 * also fail. */
79 gpt->current_kernel = new_kernel;
80 gpt->current_priority = new_prio;
81
82 if (CGPT_KERNEL_ENTRY_NOT_FOUND == new_kernel)
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070083 return GPT_ERROR_NO_VALID_KERNEL;
84
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070085 e = entries + new_kernel;
86 *start_sector = e->starting_lba;
87 *size = e->ending_lba - e->starting_lba + 1;
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -070088 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -070089}
90
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070091
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070092int GptUpdateKernelEntry(GptData* gpt, uint32_t update_type) {
93 GptHeader* header = (GptHeader*)gpt->primary_header;
94 GptEntry* entries = (GptEntry*)gpt->primary_entries;
95 GptEntry* e = entries + gpt->current_kernel;
vbendebf7a45cc2010-06-21 08:44:16 -070096 uint16_t previous_attr = e->attrs.fields.gpt_att;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -070097
Randall Spangler3dcf9dc2010-06-02 12:46:17 -070098 if (gpt->current_kernel == CGPT_KERNEL_ENTRY_NOT_FOUND)
99 return GPT_ERROR_INVALID_UPDATE_TYPE;
100 if (!IsKernelEntry(e))
101 return GPT_ERROR_INVALID_UPDATE_TYPE;
102
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700103 switch (update_type) {
104 case GPT_UPDATE_ENTRY_TRY: {
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700105 /* Used up a try */
106 int tries;
107 if (GetEntrySuccessful(e))
108 return GPT_SUCCESS; /* Successfully booted this partition, so
109 * tries field is ignored. */
110 tries = GetEntryTries(e);
111 if (tries > 1) {
112 /* Still have tries left */
113 SetEntryTries(e, tries - 1);
114 break;
115 }
116 /* Out of tries, so drop through and mark partition bad. */
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700117 }
118 case GPT_UPDATE_ENTRY_BAD: {
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700119 /* Giving up on this partition entirely. */
vbendebf7a45cc2010-06-21 08:44:16 -0700120 e->attrs.fields.gpt_att = previous_attr & ~(
121 CGPT_ATTRIBUTE_SUCCESSFUL_MASK |
122 CGPT_ATTRIBUTE_TRIES_MASK |
123 CGPT_ATTRIBUTE_PRIORITY_MASK);
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700124 break;
125 }
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700126 default:
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700127 return GPT_ERROR_INVALID_UPDATE_TYPE;
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700128 }
129
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700130 /* If no change to attributes, we're done */
vbendebf7a45cc2010-06-21 08:44:16 -0700131 if (e->attrs.fields.gpt_att == previous_attr)
Randall Spangler3dcf9dc2010-06-02 12:46:17 -0700132 return GPT_SUCCESS;
133
134 /* Update the CRCs */
135 header->entries_crc32 = Crc32((const uint8_t *)entries,
136 header->size_of_entry *
137 header->number_of_entries);
138 header->header_crc32 = HeaderCrc(header);
139 gpt->modified |= GPT_MODIFIED_HEADER1 | GPT_MODIFIED_ENTRIES1;
140
141 /* 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
143 * to update the GPT on disk so it doesn't matter. */
144 gpt->valid_headers = MASK_PRIMARY;
145 gpt->valid_entries = MASK_PRIMARY;
146 GptRepair(gpt);
Louis Yung-Chieh Lob17db3c2010-05-05 11:21:08 -0700147
Louis Yung-Chieh Lo37f6b552010-04-22 21:22:22 -0700148 return GPT_SUCCESS;
Louis Yung-Chieh Loe1a25ab2010-04-20 10:52:41 -0700149}