blob: 555fe7fd08e19d1bccfdc39448e8b9106849bc28 [file] [log] [blame]
Haojian Zhuang91f56462016-07-28 14:19:36 +08001/*
laurenw-arm24afcdd2024-01-31 16:21:48 -06002 * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
Haojian Zhuang91f56462016-07-28 14:19:36 +08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Haojian Zhuang91f56462016-07-28 14:19:36 +08005 */
6
7#include <assert.h>
Manish Pandey9b384b32021-11-12 12:59:09 +00008#include <inttypes.h>
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01009#include <stdio.h>
Haojian Zhuang91f56462016-07-28 14:19:36 +080010#include <string.h>
11
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
Rohit Nerc15dcd72022-05-06 07:58:21 +000013#include <common/tf_crc32.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/io/io_storage.h>
Sughosh Ganu47b642e2021-11-10 13:00:30 +053015#include <drivers/partition/efi.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <drivers/partition/partition.h>
17#include <drivers/partition/gpt.h>
18#include <drivers/partition/mbr.h>
19#include <plat/common/platform.h>
20
Haojian Zhuang42a746d2019-09-14 18:01:16 +080021static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE];
Florian La Roche231c2442019-01-27 14:30:12 +010022static partition_entry_list_t list;
Haojian Zhuang91f56462016-07-28 14:19:36 +080023
24#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
25static void dump_entries(int num)
26{
27 char name[EFI_NAMELEN];
28 int i, j, len;
29
30 VERBOSE("Partition table with %d entries:\n", num);
31 for (i = 0; i < num; i++) {
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010032 len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name);
Haojian Zhuang91f56462016-07-28 14:19:36 +080033 for (j = 0; j < EFI_NAMELEN - len - 1; j++) {
34 name[len + j] = ' ';
35 }
36 name[EFI_NAMELEN - 1] = '\0';
Manish Pandey9b384b32021-11-12 12:59:09 +000037 VERBOSE("%d: %s %" PRIx64 "-%" PRIx64 "\n", i + 1, name, list.list[i].start,
Haojian Zhuang91f56462016-07-28 14:19:36 +080038 list.list[i].start + list.list[i].length - 4);
39 }
40}
41#else
42#define dump_entries(num) ((void)num)
43#endif
44
45/*
46 * Load the first sector that carries MBR header.
47 * The MBR boot signature should be always valid whether it's MBR or GPT.
48 */
49static int load_mbr_header(uintptr_t image_handle, mbr_entry_t *mbr_entry)
50{
51 size_t bytes_read;
Haojian Zhuang91f56462016-07-28 14:19:36 +080052 int result;
Govindraj Raja998b1e22023-09-21 18:04:00 -050053 mbr_entry_t *tmp;
Haojian Zhuang91f56462016-07-28 14:19:36 +080054
55 assert(mbr_entry != NULL);
56 /* MBR partition table is in LBA0. */
57 result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
58 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -050059 VERBOSE("Failed to seek (%i)\n", result);
Haojian Zhuang91f56462016-07-28 14:19:36 +080060 return result;
61 }
62 result = io_read(image_handle, (uintptr_t)&mbr_sector,
Haojian Zhuang42a746d2019-09-14 18:01:16 +080063 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Govindraj Raja998b1e22023-09-21 18:04:00 -050064 if ((result != 0) || (bytes_read != PLAT_PARTITION_BLOCK_SIZE)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -050065 VERBOSE("Failed to read data (%i)\n", result);
Haojian Zhuang91f56462016-07-28 14:19:36 +080066 return result;
67 }
68
69 /* Check MBR boot signature. */
Haojian Zhuang42a746d2019-09-14 18:01:16 +080070 if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
71 (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -050072 VERBOSE("MBR boot signature failure\n");
Haojian Zhuang91f56462016-07-28 14:19:36 +080073 return -ENOENT;
74 }
Govindraj Raja998b1e22023-09-21 18:04:00 -050075
76 tmp = (mbr_entry_t *)(&mbr_sector[MBR_PRIMARY_ENTRY_OFFSET]);
77
78 if (tmp->first_lba != 1) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -050079 VERBOSE("MBR header may have an invalid first LBA\n");
Govindraj Raja998b1e22023-09-21 18:04:00 -050080 return -EINVAL;
81 }
82
83 if ((tmp->sector_nums == 0) || (tmp->sector_nums == UINT32_MAX)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -050084 VERBOSE("MBR header entry has an invalid number of sectors\n");
Govindraj Raja998b1e22023-09-21 18:04:00 -050085 return -EINVAL;
86 }
87
88 memcpy(mbr_entry, tmp, sizeof(mbr_entry_t));
Haojian Zhuang91f56462016-07-28 14:19:36 +080089 return 0;
90}
91
92/*
Rohit Nerc15dcd72022-05-06 07:58:21 +000093 * Load GPT header and check the GPT signature and header CRC.
Paul Beesley1fbc97b2019-01-11 18:26:51 +000094 * If partition numbers could be found, check & update it.
Haojian Zhuang91f56462016-07-28 14:19:36 +080095 */
Govindraj Raja998b1e22023-09-21 18:04:00 -050096static int load_gpt_header(uintptr_t image_handle, size_t header_offset,
laurenw-arm24afcdd2024-01-31 16:21:48 -060097 gpt_header_t *header)
Haojian Zhuang91f56462016-07-28 14:19:36 +080098{
Haojian Zhuang91f56462016-07-28 14:19:36 +080099 size_t bytes_read;
100 int result;
Rohit Nerc15dcd72022-05-06 07:58:21 +0000101 uint32_t header_crc, calc_crc;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800102
Govindraj Raja998b1e22023-09-21 18:04:00 -0500103 result = io_seek(image_handle, IO_SEEK_SET, header_offset);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800104 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500105 VERBOSE("Failed to seek into the GPT image at offset (%zu)\n",
106 header_offset);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800107 return result;
108 }
laurenw-arm24afcdd2024-01-31 16:21:48 -0600109 result = io_read(image_handle, (uintptr_t)header,
Haojian Zhuang91f56462016-07-28 14:19:36 +0800110 sizeof(gpt_header_t), &bytes_read);
111 if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500112 VERBOSE("GPT header read error(%i) or read mismatch occurred,"
113 "expected(%zu) and actual(%zu)\n", result,
114 sizeof(gpt_header_t), bytes_read);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800115 return result;
116 }
laurenw-arm24afcdd2024-01-31 16:21:48 -0600117 if (memcmp(header->signature, GPT_SIGNATURE,
118 sizeof(header->signature)) != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500119 VERBOSE("GPT header signature failure\n");
Haojian Zhuang91f56462016-07-28 14:19:36 +0800120 return -EINVAL;
121 }
122
Rohit Nerc15dcd72022-05-06 07:58:21 +0000123 /*
124 * UEFI Spec 2.8 March 2019 Page 119: HeaderCRC32 value is
125 * computed by setting this field to 0, and computing the
126 * 32-bit CRC for HeaderSize bytes.
127 */
laurenw-arm24afcdd2024-01-31 16:21:48 -0600128 header_crc = header->header_crc;
129 header->header_crc = 0U;
Rohit Nerc15dcd72022-05-06 07:58:21 +0000130
laurenw-arm24afcdd2024-01-31 16:21:48 -0600131 calc_crc = tf_crc32(0U, (uint8_t *)header, sizeof(gpt_header_t));
Rohit Nerc15dcd72022-05-06 07:58:21 +0000132 if (header_crc != calc_crc) {
133 ERROR("Invalid GPT Header CRC: Expected 0x%x but got 0x%x.\n",
134 header_crc, calc_crc);
135 return -EINVAL;
136 }
137
laurenw-arm24afcdd2024-01-31 16:21:48 -0600138 header->header_crc = header_crc;
Rohit Nerc15dcd72022-05-06 07:58:21 +0000139
Haojian Zhuang91f56462016-07-28 14:19:36 +0800140 /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */
laurenw-arm24afcdd2024-01-31 16:21:48 -0600141 list.entry_count = header->list_num;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800142 if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) {
143 list.entry_count = PLAT_PARTITION_MAX_ENTRIES;
144 }
Govindraj Raja998b1e22023-09-21 18:04:00 -0500145
Haojian Zhuang91f56462016-07-28 14:19:36 +0800146 return 0;
147}
148
Govindraj Raja232a11f2023-10-03 16:39:32 -0500149/*
150 * Load a single MBR entry based on details from MBR header.
151 */
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800152static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry,
Govindraj Raja998b1e22023-09-21 18:04:00 -0500153 int part_number)
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800154{
155 size_t bytes_read;
156 uintptr_t offset;
157 int result;
158
159 assert(mbr_entry != NULL);
160 /* MBR partition table is in LBA0. */
161 result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
162 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500163 VERBOSE("Failed to seek (%i)\n", result);
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800164 return result;
165 }
166 result = io_read(image_handle, (uintptr_t)&mbr_sector,
Haojian Zhuang42a746d2019-09-14 18:01:16 +0800167 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800168 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500169 VERBOSE("Failed to read data (%i)\n", result);
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800170 return result;
171 }
172
173 /* Check MBR boot signature. */
Haojian Zhuang42a746d2019-09-14 18:01:16 +0800174 if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
175 (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500176 VERBOSE("MBR Entry boot signature failure\n");
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800177 return -ENOENT;
178 }
179 offset = (uintptr_t)&mbr_sector +
180 MBR_PRIMARY_ENTRY_OFFSET +
181 MBR_PRIMARY_ENTRY_SIZE * part_number;
182 memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
183
184 return 0;
185}
186
Govindraj Raja232a11f2023-10-03 16:39:32 -0500187/*
188 * Load MBR entries based on max number of partition entries.
189 */
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800190static int load_mbr_entries(uintptr_t image_handle)
191{
192 mbr_entry_t mbr_entry;
193 int i;
194
195 list.entry_count = MBR_PRIMARY_ENTRY_NUMBER;
196
197 for (i = 0; i < list.entry_count; i++) {
198 load_mbr_entry(image_handle, &mbr_entry, i);
199 list.list[i].start = mbr_entry.first_lba * 512;
200 list.list[i].length = mbr_entry.sector_nums * 512;
201 list.list[i].name[0] = mbr_entry.type;
202 }
203
204 return 0;
205}
206
Govindraj Raja232a11f2023-10-03 16:39:32 -0500207/*
208 * Try to read and load a single GPT entry.
209 */
Haojian Zhuang91f56462016-07-28 14:19:36 +0800210static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry)
211{
Govindraj Raja232a11f2023-10-03 16:39:32 -0500212 size_t bytes_read = 0U;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800213 int result;
214
215 assert(entry != NULL);
216 result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t),
Govindraj Raja232a11f2023-10-03 16:39:32 -0500217 &bytes_read);
218 if ((result != 0) || (sizeof(gpt_entry_t) != bytes_read)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500219 VERBOSE("GPT Entry read error(%i) or read mismatch occurred,"
220 "expected(%zu) and actual(%zu)\n", result,
221 sizeof(gpt_entry_t), bytes_read);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800222 return -EINVAL;
Govindraj Raja232a11f2023-10-03 16:39:32 -0500223 }
224
Haojian Zhuang91f56462016-07-28 14:19:36 +0800225 return result;
226}
227
Govindraj Raja232a11f2023-10-03 16:39:32 -0500228/*
229 * Retrieve each entry in the partition table, parse the data from each
230 * entry and store them in the list of partition table entries.
231 */
laurenw-arm24afcdd2024-01-31 16:21:48 -0600232static int load_partition_gpt(uintptr_t image_handle, gpt_header_t header)
Haojian Zhuang91f56462016-07-28 14:19:36 +0800233{
laurenw-arm24afcdd2024-01-31 16:21:48 -0600234 const signed long long gpt_entry_offset = LBA(header.part_lba);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800235 gpt_entry_t entry;
laurenw-arm5be73cd2024-01-31 16:21:48 -0600236 int result;
237 unsigned int i;
238 uint32_t calc_crc = 0U;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800239
Govindraj Raja232a11f2023-10-03 16:39:32 -0500240 result = io_seek(image_handle, IO_SEEK_SET, gpt_entry_offset);
241 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500242 VERBOSE("Failed to seek (%i), Failed loading GPT partition"
243 "table entries\n", result);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500244 return result;
245 }
246
laurenw-arm5be73cd2024-01-31 16:21:48 -0600247 for (i = 0; i < (unsigned int)list.entry_count; i++) {
Haojian Zhuang91f56462016-07-28 14:19:36 +0800248 result = load_gpt_entry(image_handle, &entry);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500249 if (result != 0) {
laurenw-arm5be73cd2024-01-31 16:21:48 -0600250 VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n",
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500251 i, result);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500252 return result;
253 }
254
Haojian Zhuang91f56462016-07-28 14:19:36 +0800255 result = parse_gpt_entry(&entry, &list.list[i]);
256 if (result != 0) {
laurenw-arm5be73cd2024-01-31 16:21:48 -0600257 result = io_seek(image_handle, IO_SEEK_SET,
258 (gpt_entry_offset + (i * sizeof(gpt_entry_t))));
259 if (result != 0) {
260 VERBOSE("Failed to seek (%i)\n", result);
261 return result;
262 }
Haojian Zhuang91f56462016-07-28 14:19:36 +0800263 break;
264 }
laurenw-arm5be73cd2024-01-31 16:21:48 -0600265
266 /*
267 * Calculate CRC of Partition entry array to compare with CRC
268 * value in header
269 */
270 calc_crc = tf_crc32(calc_crc, (uint8_t *)&entry, sizeof(gpt_entry_t));
Haojian Zhuang91f56462016-07-28 14:19:36 +0800271 }
272 if (i == 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500273 VERBOSE("No Valid GPT Entries found\n");
Haojian Zhuang91f56462016-07-28 14:19:36 +0800274 return -EINVAL;
275 }
laurenw-arm5be73cd2024-01-31 16:21:48 -0600276
Haojian Zhuang91f56462016-07-28 14:19:36 +0800277 /*
278 * Only records the valid partition number that is loaded from
279 * partition table.
280 */
281 list.entry_count = i;
282 dump_entries(list.entry_count);
283
laurenw-arm5be73cd2024-01-31 16:21:48 -0600284 /*
285 * If there are less valid entries than the possible number of entries
286 * from the header, continue to load the partition entry table to
287 * calculate the full CRC in order to check against the partition CRC
288 * from the header for validation.
289 */
290 for (; i < header.list_num; i++) {
291 result = load_gpt_entry(image_handle, &entry);
292 if (result != 0) {
293 VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n",
294 i, result);
295 return result;
296 }
297
298 calc_crc = tf_crc32(calc_crc, (uint8_t *)&entry, sizeof(gpt_entry_t));
299 }
300
301 if (header.part_crc != calc_crc) {
302 ERROR("Invalid GPT Partition Array Entry CRC: Expected 0x%x"
303 " but got 0x%x.\n", header.part_crc, calc_crc);
304 return -EINVAL;
305 }
306
Haojian Zhuang91f56462016-07-28 14:19:36 +0800307 return 0;
308}
309
Govindraj Raja232a11f2023-10-03 16:39:32 -0500310/*
311 * Try retrieving and parsing the backup-GPT header and backup GPT entries.
312 * Last 33 blocks contains the backup-GPT entries and header.
313 */
314static int load_backup_gpt(unsigned int image_id, unsigned int sector_nums)
315{
316 int result;
laurenw-arm24afcdd2024-01-31 16:21:48 -0600317 gpt_header_t header;
Govindraj Raja232a11f2023-10-03 16:39:32 -0500318 size_t gpt_header_offset;
319 uintptr_t dev_handle, image_spec, image_handle;
320 io_block_spec_t *block_spec;
321 int part_num_entries;
322
323 result = plat_get_image_source(image_id, &dev_handle, &image_spec);
324 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500325 VERBOSE("Failed to obtain reference to image id=%u (%i)\n",
326 image_id, result);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500327 return result;
328 }
329
330 block_spec = (io_block_spec_t *)image_spec;
331 /*
332 * We need to read 32 blocks of GPT entries and one block of GPT header
333 * try mapping only last 33 last blocks from the image to read the
334 * Backup-GPT header and its entries.
335 */
336 part_num_entries = (PLAT_PARTITION_MAX_ENTRIES / 4);
337 /* Move the offset base to LBA-33 */
338 block_spec->offset += LBA(sector_nums - part_num_entries);
339 /*
340 * Set length as LBA-33, 32 blocks of backup-GPT entries and one
341 * block of backup-GPT header.
342 */
343 block_spec->length = LBA(part_num_entries + 1);
344
345 result = io_open(dev_handle, image_spec, &image_handle);
346 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500347 VERBOSE("Failed to access image id (%i)\n", result);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500348 return result;
349 }
350
351 INFO("Trying to retrieve back-up GPT header\n");
352 /* Last block is backup-GPT header, after the end of GPT entries */
353 gpt_header_offset = LBA(part_num_entries);
laurenw-arm24afcdd2024-01-31 16:21:48 -0600354 result = load_gpt_header(image_handle, gpt_header_offset, &header);
355 if ((result != 0) || (header.part_lba == 0)) {
Govindraj Raja232a11f2023-10-03 16:39:32 -0500356 ERROR("Failed to retrieve Backup GPT header,"
357 "Partition maybe corrupted\n");
358 goto out;
359 }
360
361 /*
362 * Note we mapped last 33 blocks(LBA-33), first block here starts with
363 * entries while last block was header.
364 */
laurenw-arm24afcdd2024-01-31 16:21:48 -0600365 header.part_lba = 0;
366 result = load_partition_gpt(image_handle, header);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500367
368out:
369 io_close(image_handle);
370 return result;
371}
372
373/*
374 * Load a GPT partition, Try retrieving and parsing the primary GPT header,
375 * if its corrupted try loading backup GPT header and then retrieve list
376 * of partition table entries found from the GPT.
377 */
378static int load_primary_gpt(uintptr_t image_handle, unsigned int first_lba)
379{
380 int result;
Govindraj Raja232a11f2023-10-03 16:39:32 -0500381 size_t gpt_header_offset;
laurenw-arm24afcdd2024-01-31 16:21:48 -0600382 gpt_header_t header;
Govindraj Raja232a11f2023-10-03 16:39:32 -0500383
384 /* Try to load Primary GPT header from LBA1 */
385 gpt_header_offset = LBA(first_lba);
laurenw-arm24afcdd2024-01-31 16:21:48 -0600386 result = load_gpt_header(image_handle, gpt_header_offset, &header);
387 if ((result != 0) || (header.part_lba == 0)) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500388 VERBOSE("Failed to retrieve Primary GPT header,"
389 "trying to retrieve back-up GPT header\n");
Govindraj Raja232a11f2023-10-03 16:39:32 -0500390 return result;
391 }
392
laurenw-arm24afcdd2024-01-31 16:21:48 -0600393 return load_partition_gpt(image_handle, header);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500394}
395
396/*
397 * Load the partition table info based on the image id provided.
398 */
Haojian Zhuang91f56462016-07-28 14:19:36 +0800399int load_partition_table(unsigned int image_id)
400{
401 uintptr_t dev_handle, image_handle, image_spec = 0;
402 mbr_entry_t mbr_entry;
403 int result;
404
405 result = plat_get_image_source(image_id, &dev_handle, &image_spec);
406 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500407 VERBOSE("Failed to obtain reference to image id=%u (%i)\n",
408 image_id, result);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800409 return result;
410 }
411
412 result = io_open(dev_handle, image_spec, &image_handle);
413 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500414 VERBOSE("Failed to access image id=%u (%i)\n", image_id, result);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800415 return result;
416 }
417
418 result = load_mbr_header(image_handle, &mbr_entry);
419 if (result != 0) {
Govindraj Raja1341a3f2023-10-12 17:31:41 -0500420 VERBOSE("Failed to access image id=%u (%i)\n", image_id, result);
Govindraj Raja232a11f2023-10-03 16:39:32 -0500421 goto out;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800422 }
423 if (mbr_entry.type == PARTITION_TYPE_GPT) {
Govindraj Raja232a11f2023-10-03 16:39:32 -0500424 result = load_primary_gpt(image_handle, mbr_entry.first_lba);
Govindraj Raja998b1e22023-09-21 18:04:00 -0500425 if (result != 0) {
Govindraj Raja232a11f2023-10-03 16:39:32 -0500426 io_close(image_handle);
427 return load_backup_gpt(BKUP_GPT_IMAGE_ID,
428 mbr_entry.sector_nums);
Govindraj Raja998b1e22023-09-21 18:04:00 -0500429 }
Haojian Zhuang91f56462016-07-28 14:19:36 +0800430 } else {
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800431 result = load_mbr_entries(image_handle);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800432 }
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800433
Govindraj Raja232a11f2023-10-03 16:39:32 -0500434out:
Haojian Zhuang91f56462016-07-28 14:19:36 +0800435 io_close(image_handle);
436 return result;
437}
438
Govindraj Raja232a11f2023-10-03 16:39:32 -0500439/*
440 * Try retrieving a partition table entry based on the name of the partition.
441 */
Haojian Zhuang91f56462016-07-28 14:19:36 +0800442const partition_entry_t *get_partition_entry(const char *name)
443{
444 int i;
445
446 for (i = 0; i < list.entry_count; i++) {
447 if (strcmp(name, list.list[i].name) == 0) {
448 return &list.list[i];
449 }
450 }
451 return NULL;
452}
453
Govindraj Raja232a11f2023-10-03 16:39:32 -0500454/*
455 * Try retrieving a partition table entry based on the GUID.
456 */
Lionel Debievea88ca2e2022-02-24 18:56:28 +0100457const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_uuid)
458{
459 int i;
460
461 for (i = 0; i < list.entry_count; i++) {
462 if (guidcmp(type_uuid, &list.list[i].type_guid) == 0) {
463 return &list.list[i];
464 }
465 }
466
467 return NULL;
468}
469
Govindraj Raja232a11f2023-10-03 16:39:32 -0500470/*
471 * Try retrieving a partition table entry based on the UUID.
472 */
Sughosh Ganu47b642e2021-11-10 13:00:30 +0530473const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid)
474{
475 int i;
476
477 for (i = 0; i < list.entry_count; i++) {
478 if (guidcmp(part_uuid, &list.list[i].part_guid) == 0) {
479 return &list.list[i];
480 }
481 }
482
483 return NULL;
484}
485
Govindraj Raja232a11f2023-10-03 16:39:32 -0500486/*
487 * Return entry to the list of partition table entries.
488 */
Haojian Zhuang91f56462016-07-28 14:19:36 +0800489const partition_entry_list_t *get_partition_entry_list(void)
490{
491 return &list;
492}
493
Govindraj Raja232a11f2023-10-03 16:39:32 -0500494/*
495 * Try loading partition table info for the given image ID.
496 */
Haojian Zhuang91f56462016-07-28 14:19:36 +0800497void partition_init(unsigned int image_id)
498{
Govindraj Raja20431cf2023-10-12 16:41:07 -0500499 int ret;
500
501 ret = load_partition_table(image_id);
502 if (ret != 0) {
503 ERROR("Failed to parse partition with image id = %u\n",
504 image_id);
505 }
506}
507
508/*
509 * Load a GPT based image.
510 */
511int gpt_partition_init(void)
512{
513 return load_partition_table(GPT_IMAGE_ID);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800514}