blob: 564d29d7ea60760e98e0c218aca439da1982ec0d [file] [log] [blame]
Haojian Zhuang201b66b2016-07-28 14:19:36 +08001/*
Boyan Karatotevec48d522025-03-19 11:31:34 +00002 * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved.
Haojian Zhuang201b66b2016-07-28 14:19:36 +08003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Haojian Zhuang201b66b2016-07-28 14:19:36 +08005 */
6
7#include <assert.h>
Manish Pandey4ef449c2021-11-12 12:59:09 +00008#include <inttypes.h>
Antonio Nino Diaz39b6cc62018-08-16 16:46:06 +01009#include <stdio.h>
Haojian Zhuang201b66b2016-07-28 14:19:36 +080010#include <string.h>
11
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000012#include <common/debug.h>
Rohit Nera283d192022-05-06 07:58:21 +000013#include <common/tf_crc32.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000014#include <drivers/io/io_storage.h>
Sughosh Ganu3cb10652021-11-10 13:00:30 +053015#include <drivers/partition/efi.h>
Antonio Nino Diaz09d40e02018-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 Zhuangf8631f52019-09-14 18:01:16 +080021static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE];
Florian La Roche98228522019-01-27 14:30:12 +010022static partition_entry_list_t list;
Haojian Zhuang201b66b2016-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 Diaz39b6cc62018-08-16 16:46:06 +010032 len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name);
Haojian Zhuang201b66b2016-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 Pandey4ef449c2021-11-12 12:59:09 +000037 VERBOSE("%d: %s %" PRIx64 "-%" PRIx64 "\n", i + 1, name, list.list[i].start,
Haojian Zhuang201b66b2016-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 Zhuang201b66b2016-07-28 14:19:36 +080052 int result;
Chris Webb21a77e02024-07-04 12:33:15 +010053 mbr_entry_t tmp;
Haojian Zhuang201b66b2016-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 Raja0f23e7e2023-10-12 17:31:41 -050059 VERBOSE("Failed to seek (%i)\n", result);
Haojian Zhuang201b66b2016-07-28 14:19:36 +080060 return result;
61 }
62 result = io_read(image_handle, (uintptr_t)&mbr_sector,
Haojian Zhuangf8631f52019-09-14 18:01:16 +080063 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Govindraj Rajafce8a702023-09-21 18:04:00 -050064 if ((result != 0) || (bytes_read != PLAT_PARTITION_BLOCK_SIZE)) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -050065 VERBOSE("Failed to read data (%i)\n", result);
Haojian Zhuang201b66b2016-07-28 14:19:36 +080066 return result;
67 }
68
69 /* Check MBR boot signature. */
Haojian Zhuangf8631f52019-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 Raja0f23e7e2023-10-12 17:31:41 -050072 VERBOSE("MBR boot signature failure\n");
Haojian Zhuang201b66b2016-07-28 14:19:36 +080073 return -ENOENT;
74 }
Govindraj Rajafce8a702023-09-21 18:04:00 -050075
Chris Webb21a77e02024-07-04 12:33:15 +010076 memcpy(&tmp, mbr_sector + MBR_PRIMARY_ENTRY_OFFSET, sizeof(tmp));
Govindraj Rajafce8a702023-09-21 18:04:00 -050077
Chris Webb21a77e02024-07-04 12:33:15 +010078 if ((tmp.sector_nums == 0) || (tmp.sector_nums == UINT32_MAX)) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -050079 VERBOSE("MBR header entry has an invalid number of sectors\n");
Govindraj Rajafce8a702023-09-21 18:04:00 -050080 return -EINVAL;
81 }
82
Chris Webb21a77e02024-07-04 12:33:15 +010083 memcpy(mbr_entry, &tmp, sizeof(mbr_entry_t));
Haojian Zhuang201b66b2016-07-28 14:19:36 +080084 return 0;
85}
86
87/*
Rohit Nera283d192022-05-06 07:58:21 +000088 * Load GPT header and check the GPT signature and header CRC.
Paul Beesley8aabea32019-01-11 18:26:51 +000089 * If partition numbers could be found, check & update it.
Haojian Zhuang201b66b2016-07-28 14:19:36 +080090 */
Govindraj Rajafce8a702023-09-21 18:04:00 -050091static int load_gpt_header(uintptr_t image_handle, size_t header_offset,
laurenw-arm17a261d2024-01-31 16:21:48 -060092 gpt_header_t *header)
Haojian Zhuang201b66b2016-07-28 14:19:36 +080093{
Haojian Zhuang201b66b2016-07-28 14:19:36 +080094 size_t bytes_read;
95 int result;
Rohit Nera283d192022-05-06 07:58:21 +000096 uint32_t header_crc, calc_crc;
Haojian Zhuang201b66b2016-07-28 14:19:36 +080097
Govindraj Rajafce8a702023-09-21 18:04:00 -050098 result = io_seek(image_handle, IO_SEEK_SET, header_offset);
Haojian Zhuang201b66b2016-07-28 14:19:36 +080099 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500100 VERBOSE("Failed to seek into the GPT image at offset (%zu)\n",
101 header_offset);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800102 return result;
103 }
laurenw-arm17a261d2024-01-31 16:21:48 -0600104 result = io_read(image_handle, (uintptr_t)header,
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800105 sizeof(gpt_header_t), &bytes_read);
106 if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500107 VERBOSE("GPT header read error(%i) or read mismatch occurred,"
108 "expected(%zu) and actual(%zu)\n", result,
109 sizeof(gpt_header_t), bytes_read);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800110 return result;
111 }
laurenw-arm17a261d2024-01-31 16:21:48 -0600112 if (memcmp(header->signature, GPT_SIGNATURE,
113 sizeof(header->signature)) != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500114 VERBOSE("GPT header signature failure\n");
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800115 return -EINVAL;
116 }
117
Rohit Nera283d192022-05-06 07:58:21 +0000118 /*
119 * UEFI Spec 2.8 March 2019 Page 119: HeaderCRC32 value is
120 * computed by setting this field to 0, and computing the
121 * 32-bit CRC for HeaderSize bytes.
122 */
laurenw-arm17a261d2024-01-31 16:21:48 -0600123 header_crc = header->header_crc;
124 header->header_crc = 0U;
Rohit Nera283d192022-05-06 07:58:21 +0000125
laurenw-arm17a261d2024-01-31 16:21:48 -0600126 calc_crc = tf_crc32(0U, (uint8_t *)header, sizeof(gpt_header_t));
Rohit Nera283d192022-05-06 07:58:21 +0000127 if (header_crc != calc_crc) {
128 ERROR("Invalid GPT Header CRC: Expected 0x%x but got 0x%x.\n",
129 header_crc, calc_crc);
130 return -EINVAL;
131 }
132
laurenw-arm17a261d2024-01-31 16:21:48 -0600133 header->header_crc = header_crc;
Rohit Nera283d192022-05-06 07:58:21 +0000134
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800135 /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */
laurenw-arm17a261d2024-01-31 16:21:48 -0600136 list.entry_count = header->list_num;
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800137 if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) {
138 list.entry_count = PLAT_PARTITION_MAX_ENTRIES;
139 }
Govindraj Rajafce8a702023-09-21 18:04:00 -0500140
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800141 return 0;
142}
143
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500144/*
145 * Load a single MBR entry based on details from MBR header.
146 */
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800147static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry,
Govindraj Rajafce8a702023-09-21 18:04:00 -0500148 int part_number)
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800149{
150 size_t bytes_read;
151 uintptr_t offset;
152 int result;
153
154 assert(mbr_entry != NULL);
155 /* MBR partition table is in LBA0. */
156 result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
157 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500158 VERBOSE("Failed to seek (%i)\n", result);
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800159 return result;
160 }
161 result = io_read(image_handle, (uintptr_t)&mbr_sector,
Haojian Zhuangf8631f52019-09-14 18:01:16 +0800162 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800163 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500164 VERBOSE("Failed to read data (%i)\n", result);
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800165 return result;
166 }
167
168 /* Check MBR boot signature. */
Haojian Zhuangf8631f52019-09-14 18:01:16 +0800169 if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
170 (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500171 VERBOSE("MBR Entry boot signature failure\n");
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800172 return -ENOENT;
173 }
174 offset = (uintptr_t)&mbr_sector +
175 MBR_PRIMARY_ENTRY_OFFSET +
176 MBR_PRIMARY_ENTRY_SIZE * part_number;
177 memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
178
179 return 0;
180}
181
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500182/*
183 * Load MBR entries based on max number of partition entries.
184 */
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800185static int load_mbr_entries(uintptr_t image_handle)
186{
187 mbr_entry_t mbr_entry;
laurenw-armce574312024-02-29 15:34:39 -0600188 unsigned int i;
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800189
190 list.entry_count = MBR_PRIMARY_ENTRY_NUMBER;
191
laurenw-armce574312024-02-29 15:34:39 -0600192 for (i = 0U; i < list.entry_count; i++) {
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800193 load_mbr_entry(image_handle, &mbr_entry, i);
194 list.list[i].start = mbr_entry.first_lba * 512;
195 list.list[i].length = mbr_entry.sector_nums * 512;
196 list.list[i].name[0] = mbr_entry.type;
197 }
198
199 return 0;
200}
201
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500202/*
203 * Try to read and load a single GPT entry.
204 */
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800205static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry)
206{
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500207 size_t bytes_read = 0U;
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800208 int result;
209
210 assert(entry != NULL);
211 result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t),
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500212 &bytes_read);
213 if ((result != 0) || (sizeof(gpt_entry_t) != bytes_read)) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500214 VERBOSE("GPT Entry read error(%i) or read mismatch occurred,"
215 "expected(%zu) and actual(%zu)\n", result,
216 sizeof(gpt_entry_t), bytes_read);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800217 return -EINVAL;
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500218 }
219
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800220 return result;
221}
222
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500223/*
224 * Retrieve each entry in the partition table, parse the data from each
225 * entry and store them in the list of partition table entries.
226 */
laurenw-arm17a261d2024-01-31 16:21:48 -0600227static int load_partition_gpt(uintptr_t image_handle, gpt_header_t header)
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800228{
laurenw-arm17a261d2024-01-31 16:21:48 -0600229 const signed long long gpt_entry_offset = LBA(header.part_lba);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800230 gpt_entry_t entry;
laurenw-arm7a9e9f62024-01-31 16:21:48 -0600231 int result;
232 unsigned int i;
233 uint32_t calc_crc = 0U;
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800234
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500235 result = io_seek(image_handle, IO_SEEK_SET, gpt_entry_offset);
236 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500237 VERBOSE("Failed to seek (%i), Failed loading GPT partition"
238 "table entries\n", result);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500239 return result;
240 }
241
laurenw-armce574312024-02-29 15:34:39 -0600242 for (i = 0U; i < list.entry_count; i++) {
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800243 result = load_gpt_entry(image_handle, &entry);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500244 if (result != 0) {
laurenw-arm7a9e9f62024-01-31 16:21:48 -0600245 VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n",
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500246 i, result);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500247 return result;
248 }
249
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800250 result = parse_gpt_entry(&entry, &list.list[i]);
251 if (result != 0) {
laurenw-arm7a9e9f62024-01-31 16:21:48 -0600252 result = io_seek(image_handle, IO_SEEK_SET,
253 (gpt_entry_offset + (i * sizeof(gpt_entry_t))));
254 if (result != 0) {
255 VERBOSE("Failed to seek (%i)\n", result);
256 return result;
257 }
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800258 break;
259 }
laurenw-arm7a9e9f62024-01-31 16:21:48 -0600260
261 /*
262 * Calculate CRC of Partition entry array to compare with CRC
263 * value in header
264 */
265 calc_crc = tf_crc32(calc_crc, (uint8_t *)&entry, sizeof(gpt_entry_t));
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800266 }
267 if (i == 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500268 VERBOSE("No Valid GPT Entries found\n");
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800269 return -EINVAL;
270 }
laurenw-arm7a9e9f62024-01-31 16:21:48 -0600271
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800272 /*
273 * Only records the valid partition number that is loaded from
274 * partition table.
275 */
276 list.entry_count = i;
277 dump_entries(list.entry_count);
278
laurenw-arm7a9e9f62024-01-31 16:21:48 -0600279 /*
280 * If there are less valid entries than the possible number of entries
281 * from the header, continue to load the partition entry table to
282 * calculate the full CRC in order to check against the partition CRC
283 * from the header for validation.
284 */
285 for (; i < header.list_num; i++) {
286 result = load_gpt_entry(image_handle, &entry);
287 if (result != 0) {
288 VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n",
289 i, result);
290 return result;
291 }
292
293 calc_crc = tf_crc32(calc_crc, (uint8_t *)&entry, sizeof(gpt_entry_t));
294 }
295
296 if (header.part_crc != calc_crc) {
297 ERROR("Invalid GPT Partition Array Entry CRC: Expected 0x%x"
298 " but got 0x%x.\n", header.part_crc, calc_crc);
299 return -EINVAL;
300 }
301
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800302 return 0;
303}
304
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500305/*
306 * Try retrieving and parsing the backup-GPT header and backup GPT entries.
307 * Last 33 blocks contains the backup-GPT entries and header.
308 */
309static int load_backup_gpt(unsigned int image_id, unsigned int sector_nums)
310{
311 int result;
laurenw-arm17a261d2024-01-31 16:21:48 -0600312 gpt_header_t header;
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500313 size_t gpt_header_offset;
314 uintptr_t dev_handle, image_spec, image_handle;
315 io_block_spec_t *block_spec;
316 int part_num_entries;
317
318 result = plat_get_image_source(image_id, &dev_handle, &image_spec);
319 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500320 VERBOSE("Failed to obtain reference to image id=%u (%i)\n",
321 image_id, result);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500322 return result;
323 }
324
325 block_spec = (io_block_spec_t *)image_spec;
326 /*
327 * We need to read 32 blocks of GPT entries and one block of GPT header
328 * try mapping only last 33 last blocks from the image to read the
329 * Backup-GPT header and its entries.
330 */
331 part_num_entries = (PLAT_PARTITION_MAX_ENTRIES / 4);
332 /* Move the offset base to LBA-33 */
333 block_spec->offset += LBA(sector_nums - part_num_entries);
334 /*
335 * Set length as LBA-33, 32 blocks of backup-GPT entries and one
336 * block of backup-GPT header.
337 */
338 block_spec->length = LBA(part_num_entries + 1);
339
340 result = io_open(dev_handle, image_spec, &image_handle);
341 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500342 VERBOSE("Failed to access image id (%i)\n", result);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500343 return result;
344 }
345
346 INFO("Trying to retrieve back-up GPT header\n");
347 /* Last block is backup-GPT header, after the end of GPT entries */
348 gpt_header_offset = LBA(part_num_entries);
laurenw-arm17a261d2024-01-31 16:21:48 -0600349 result = load_gpt_header(image_handle, gpt_header_offset, &header);
350 if ((result != 0) || (header.part_lba == 0)) {
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500351 ERROR("Failed to retrieve Backup GPT header,"
352 "Partition maybe corrupted\n");
353 goto out;
354 }
355
356 /*
357 * Note we mapped last 33 blocks(LBA-33), first block here starts with
358 * entries while last block was header.
359 */
laurenw-arm17a261d2024-01-31 16:21:48 -0600360 header.part_lba = 0;
361 result = load_partition_gpt(image_handle, header);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500362
363out:
364 io_close(image_handle);
365 return result;
366}
367
368/*
369 * Load a GPT partition, Try retrieving and parsing the primary GPT header,
370 * if its corrupted try loading backup GPT header and then retrieve list
371 * of partition table entries found from the GPT.
372 */
373static int load_primary_gpt(uintptr_t image_handle, unsigned int first_lba)
374{
375 int result;
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500376 size_t gpt_header_offset;
laurenw-arm17a261d2024-01-31 16:21:48 -0600377 gpt_header_t header;
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500378
379 /* Try to load Primary GPT header from LBA1 */
380 gpt_header_offset = LBA(first_lba);
laurenw-arm17a261d2024-01-31 16:21:48 -0600381 result = load_gpt_header(image_handle, gpt_header_offset, &header);
382 if ((result != 0) || (header.part_lba == 0)) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500383 VERBOSE("Failed to retrieve Primary GPT header,"
384 "trying to retrieve back-up GPT header\n");
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500385 return result;
386 }
387
laurenw-arm17a261d2024-01-31 16:21:48 -0600388 return load_partition_gpt(image_handle, header);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500389}
390
391/*
392 * Load the partition table info based on the image id provided.
393 */
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800394int load_partition_table(unsigned int image_id)
395{
396 uintptr_t dev_handle, image_handle, image_spec = 0;
Boyan Karatotevec48d522025-03-19 11:31:34 +0000397 mbr_entry_t mbr_entry = {0};
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800398 int result;
399
400 result = plat_get_image_source(image_id, &dev_handle, &image_spec);
401 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500402 VERBOSE("Failed to obtain reference to image id=%u (%i)\n",
403 image_id, result);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800404 return result;
405 }
406
407 result = io_open(dev_handle, image_spec, &image_handle);
408 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500409 VERBOSE("Failed to access image id=%u (%i)\n", image_id, result);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800410 return result;
411 }
412
413 result = load_mbr_header(image_handle, &mbr_entry);
414 if (result != 0) {
Govindraj Raja0f23e7e2023-10-12 17:31:41 -0500415 VERBOSE("Failed to access image id=%u (%i)\n", image_id, result);
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500416 goto out;
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800417 }
418 if (mbr_entry.type == PARTITION_TYPE_GPT) {
Bogdan Roman2fac89d2024-09-26 16:53:32 +0300419 if (mbr_entry.first_lba != 1U) {
420 VERBOSE("MBR header may have an invalid first LBA\n");
421 return -EINVAL;
422 }
423
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500424 result = load_primary_gpt(image_handle, mbr_entry.first_lba);
Govindraj Rajafce8a702023-09-21 18:04:00 -0500425 if (result != 0) {
Govindraj Rajaad2dd652023-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 Rajafce8a702023-09-21 18:04:00 -0500429 }
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800430 } else {
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800431 result = load_mbr_entries(image_handle);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800432 }
Loh Tien Hock30f833c2019-02-11 10:56:28 +0800433
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500434out:
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800435 io_close(image_handle);
436 return result;
437}
438
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500439/*
440 * Try retrieving a partition table entry based on the name of the partition.
441 */
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800442const partition_entry_t *get_partition_entry(const char *name)
443{
laurenw-armce574312024-02-29 15:34:39 -0600444 unsigned int i;
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800445
laurenw-armce574312024-02-29 15:34:39 -0600446 for (i = 0U; i < list.entry_count; i++) {
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800447 if (strcmp(name, list.list[i].name) == 0) {
448 return &list.list[i];
449 }
450 }
451 return NULL;
452}
453
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500454/*
Sughosh Ganu37e81a62024-02-02 15:32:10 +0530455 * Try retrieving a partition table entry based on the partition type GUID.
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500456 */
Sughosh Ganu37e81a62024-02-02 15:32:10 +0530457const partition_entry_t *get_partition_entry_by_type(
458 const struct efi_guid *type_guid)
Lionel Debieve564f5d42022-02-24 18:56:28 +0100459{
laurenw-armce574312024-02-29 15:34:39 -0600460 unsigned int i;
Lionel Debieve564f5d42022-02-24 18:56:28 +0100461
laurenw-armce574312024-02-29 15:34:39 -0600462 for (i = 0U; i < list.entry_count; i++) {
Sughosh Ganu37e81a62024-02-02 15:32:10 +0530463 if (guidcmp(type_guid, &list.list[i].type_guid) == 0) {
Lionel Debieve564f5d42022-02-24 18:56:28 +0100464 return &list.list[i];
465 }
466 }
467
468 return NULL;
469}
470
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500471/*
Sughosh Ganu37e81a62024-02-02 15:32:10 +0530472 * Try retrieving a partition table entry based on the unique partition GUID.
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500473 */
Sughosh Ganu37e81a62024-02-02 15:32:10 +0530474const partition_entry_t *get_partition_entry_by_guid(
475 const struct efi_guid *part_guid)
Sughosh Ganu3cb10652021-11-10 13:00:30 +0530476{
laurenw-armce574312024-02-29 15:34:39 -0600477 unsigned int i;
Sughosh Ganu3cb10652021-11-10 13:00:30 +0530478
laurenw-armce574312024-02-29 15:34:39 -0600479 for (i = 0U; i < list.entry_count; i++) {
Sughosh Ganu37e81a62024-02-02 15:32:10 +0530480 if (guidcmp(part_guid, &list.list[i].part_guid) == 0) {
Sughosh Ganu3cb10652021-11-10 13:00:30 +0530481 return &list.list[i];
482 }
483 }
484
485 return NULL;
486}
487
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500488/*
489 * Return entry to the list of partition table entries.
490 */
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800491const partition_entry_list_t *get_partition_entry_list(void)
492{
493 return &list;
494}
495
Govindraj Rajaad2dd652023-10-03 16:39:32 -0500496/*
497 * Try loading partition table info for the given image ID.
498 */
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800499void partition_init(unsigned int image_id)
500{
Govindraj Rajaf08460d2023-10-12 16:41:07 -0500501 int ret;
502
503 ret = load_partition_table(image_id);
504 if (ret != 0) {
505 ERROR("Failed to parse partition with image id = %u\n",
506 image_id);
507 }
508}
509
510/*
511 * Load a GPT based image.
512 */
513int gpt_partition_init(void)
514{
515 return load_partition_table(GPT_IMAGE_ID);
Haojian Zhuang201b66b2016-07-28 14:19:36 +0800516}