Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 1 | /* |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 2 | * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved. |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Manish Pandey | 9b384b3 | 2021-11-12 12:59:09 +0000 | [diff] [blame] | 8 | #include <inttypes.h> |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 9 | #include <stdio.h> |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <common/debug.h> |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 13 | #include <common/tf_crc32.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <drivers/io/io_storage.h> |
Sughosh Ganu | 47b642e | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 15 | #include <drivers/partition/efi.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 16 | #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 Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 21 | static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE]; |
Florian La Roche | 231c244 | 2019-01-27 14:30:12 +0100 | [diff] [blame] | 22 | static partition_entry_list_t list; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 23 | |
| 24 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 25 | static 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 Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 32 | len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 33 | for (j = 0; j < EFI_NAMELEN - len - 1; j++) { |
| 34 | name[len + j] = ' '; |
| 35 | } |
| 36 | name[EFI_NAMELEN - 1] = '\0'; |
Manish Pandey | 9b384b3 | 2021-11-12 12:59:09 +0000 | [diff] [blame] | 37 | VERBOSE("%d: %s %" PRIx64 "-%" PRIx64 "\n", i + 1, name, list.list[i].start, |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 38 | 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 | */ |
| 49 | static int load_mbr_header(uintptr_t image_handle, mbr_entry_t *mbr_entry) |
| 50 | { |
| 51 | size_t bytes_read; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 52 | int result; |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 53 | mbr_entry_t *tmp; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 54 | |
| 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 Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 59 | VERBOSE("Failed to seek (%i)\n", result); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 60 | return result; |
| 61 | } |
| 62 | result = io_read(image_handle, (uintptr_t)&mbr_sector, |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 63 | PLAT_PARTITION_BLOCK_SIZE, &bytes_read); |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 64 | if ((result != 0) || (bytes_read != PLAT_PARTITION_BLOCK_SIZE)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 65 | VERBOSE("Failed to read data (%i)\n", result); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /* Check MBR boot signature. */ |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 70 | if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) || |
| 71 | (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 72 | VERBOSE("MBR boot signature failure\n"); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 73 | return -ENOENT; |
| 74 | } |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 75 | |
| 76 | tmp = (mbr_entry_t *)(&mbr_sector[MBR_PRIMARY_ENTRY_OFFSET]); |
| 77 | |
| 78 | if (tmp->first_lba != 1) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 79 | VERBOSE("MBR header may have an invalid first LBA\n"); |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 80 | return -EINVAL; |
| 81 | } |
| 82 | |
| 83 | if ((tmp->sector_nums == 0) || (tmp->sector_nums == UINT32_MAX)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 84 | VERBOSE("MBR header entry has an invalid number of sectors\n"); |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 85 | return -EINVAL; |
| 86 | } |
| 87 | |
| 88 | memcpy(mbr_entry, tmp, sizeof(mbr_entry_t)); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | /* |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 93 | * Load GPT header and check the GPT signature and header CRC. |
Paul Beesley | 1fbc97b | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 94 | * If partition numbers could be found, check & update it. |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 95 | */ |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 96 | static int load_gpt_header(uintptr_t image_handle, size_t header_offset, |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 97 | gpt_header_t *header) |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 98 | { |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 99 | size_t bytes_read; |
| 100 | int result; |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 101 | uint32_t header_crc, calc_crc; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 102 | |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 103 | result = io_seek(image_handle, IO_SEEK_SET, header_offset); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 104 | if (result != 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 105 | VERBOSE("Failed to seek into the GPT image at offset (%zu)\n", |
| 106 | header_offset); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 107 | return result; |
| 108 | } |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 109 | result = io_read(image_handle, (uintptr_t)header, |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 110 | sizeof(gpt_header_t), &bytes_read); |
| 111 | if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 112 | 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 Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 115 | return result; |
| 116 | } |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 117 | if (memcmp(header->signature, GPT_SIGNATURE, |
| 118 | sizeof(header->signature)) != 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 119 | VERBOSE("GPT header signature failure\n"); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 120 | return -EINVAL; |
| 121 | } |
| 122 | |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 123 | /* |
| 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-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 128 | header_crc = header->header_crc; |
| 129 | header->header_crc = 0U; |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 130 | |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 131 | calc_crc = tf_crc32(0U, (uint8_t *)header, sizeof(gpt_header_t)); |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 132 | 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-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 138 | header->header_crc = header_crc; |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 139 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 140 | /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */ |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 141 | list.entry_count = header->list_num; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 142 | if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) { |
| 143 | list.entry_count = PLAT_PARTITION_MAX_ENTRIES; |
| 144 | } |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 145 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 146 | return 0; |
| 147 | } |
| 148 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 149 | /* |
| 150 | * Load a single MBR entry based on details from MBR header. |
| 151 | */ |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 152 | static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry, |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 153 | int part_number) |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 154 | { |
| 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 Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 163 | VERBOSE("Failed to seek (%i)\n", result); |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 164 | return result; |
| 165 | } |
| 166 | result = io_read(image_handle, (uintptr_t)&mbr_sector, |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 167 | PLAT_PARTITION_BLOCK_SIZE, &bytes_read); |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 168 | if (result != 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 169 | VERBOSE("Failed to read data (%i)\n", result); |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 170 | return result; |
| 171 | } |
| 172 | |
| 173 | /* Check MBR boot signature. */ |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 174 | if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) || |
| 175 | (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 176 | VERBOSE("MBR Entry boot signature failure\n"); |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 177 | 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 Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 187 | /* |
| 188 | * Load MBR entries based on max number of partition entries. |
| 189 | */ |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 190 | static int load_mbr_entries(uintptr_t image_handle) |
| 191 | { |
| 192 | mbr_entry_t mbr_entry; |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 193 | unsigned int i; |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 194 | |
| 195 | list.entry_count = MBR_PRIMARY_ENTRY_NUMBER; |
| 196 | |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 197 | for (i = 0U; i < list.entry_count; i++) { |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 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 Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 207 | /* |
| 208 | * Try to read and load a single GPT entry. |
| 209 | */ |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 210 | static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry) |
| 211 | { |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 212 | size_t bytes_read = 0U; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 213 | int result; |
| 214 | |
| 215 | assert(entry != NULL); |
| 216 | result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t), |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 217 | &bytes_read); |
| 218 | if ((result != 0) || (sizeof(gpt_entry_t) != bytes_read)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 219 | 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 Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 222 | return -EINVAL; |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 223 | } |
| 224 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 225 | return result; |
| 226 | } |
| 227 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 228 | /* |
| 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-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 232 | static int load_partition_gpt(uintptr_t image_handle, gpt_header_t header) |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 233 | { |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 234 | const signed long long gpt_entry_offset = LBA(header.part_lba); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 235 | gpt_entry_t entry; |
laurenw-arm | 5be73cd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 236 | int result; |
| 237 | unsigned int i; |
| 238 | uint32_t calc_crc = 0U; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 239 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 240 | result = io_seek(image_handle, IO_SEEK_SET, gpt_entry_offset); |
| 241 | if (result != 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 242 | VERBOSE("Failed to seek (%i), Failed loading GPT partition" |
| 243 | "table entries\n", result); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 244 | return result; |
| 245 | } |
| 246 | |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 247 | for (i = 0U; i < list.entry_count; i++) { |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 248 | result = load_gpt_entry(image_handle, &entry); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 249 | if (result != 0) { |
laurenw-arm | 5be73cd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 250 | VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n", |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 251 | i, result); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 252 | return result; |
| 253 | } |
| 254 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 255 | result = parse_gpt_entry(&entry, &list.list[i]); |
| 256 | if (result != 0) { |
laurenw-arm | 5be73cd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 257 | 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 Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 263 | break; |
| 264 | } |
laurenw-arm | 5be73cd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 265 | |
| 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 Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 271 | } |
| 272 | if (i == 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 273 | VERBOSE("No Valid GPT Entries found\n"); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 274 | return -EINVAL; |
| 275 | } |
laurenw-arm | 5be73cd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 276 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 277 | /* |
| 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-arm | 5be73cd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 284 | /* |
| 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 Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 310 | /* |
| 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 | */ |
| 314 | static int load_backup_gpt(unsigned int image_id, unsigned int sector_nums) |
| 315 | { |
| 316 | int result; |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 317 | gpt_header_t header; |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 318 | 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 Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 325 | VERBOSE("Failed to obtain reference to image id=%u (%i)\n", |
| 326 | image_id, result); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 327 | 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 Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 347 | VERBOSE("Failed to access image id (%i)\n", result); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 348 | 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-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 354 | result = load_gpt_header(image_handle, gpt_header_offset, &header); |
| 355 | if ((result != 0) || (header.part_lba == 0)) { |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 356 | 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-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 365 | header.part_lba = 0; |
| 366 | result = load_partition_gpt(image_handle, header); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 367 | |
| 368 | out: |
| 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 | */ |
| 378 | static int load_primary_gpt(uintptr_t image_handle, unsigned int first_lba) |
| 379 | { |
| 380 | int result; |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 381 | size_t gpt_header_offset; |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 382 | gpt_header_t header; |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 383 | |
| 384 | /* Try to load Primary GPT header from LBA1 */ |
| 385 | gpt_header_offset = LBA(first_lba); |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 386 | result = load_gpt_header(image_handle, gpt_header_offset, &header); |
| 387 | if ((result != 0) || (header.part_lba == 0)) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 388 | VERBOSE("Failed to retrieve Primary GPT header," |
| 389 | "trying to retrieve back-up GPT header\n"); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 390 | return result; |
| 391 | } |
| 392 | |
laurenw-arm | 24afcdd | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 393 | return load_partition_gpt(image_handle, header); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /* |
| 397 | * Load the partition table info based on the image id provided. |
| 398 | */ |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 399 | int 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 Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 407 | VERBOSE("Failed to obtain reference to image id=%u (%i)\n", |
| 408 | image_id, result); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 409 | return result; |
| 410 | } |
| 411 | |
| 412 | result = io_open(dev_handle, image_spec, &image_handle); |
| 413 | if (result != 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 414 | VERBOSE("Failed to access image id=%u (%i)\n", image_id, result); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 415 | return result; |
| 416 | } |
| 417 | |
| 418 | result = load_mbr_header(image_handle, &mbr_entry); |
| 419 | if (result != 0) { |
Govindraj Raja | 1341a3f | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 420 | VERBOSE("Failed to access image id=%u (%i)\n", image_id, result); |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 421 | goto out; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 422 | } |
| 423 | if (mbr_entry.type == PARTITION_TYPE_GPT) { |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 424 | result = load_primary_gpt(image_handle, mbr_entry.first_lba); |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 425 | if (result != 0) { |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 426 | io_close(image_handle); |
| 427 | return load_backup_gpt(BKUP_GPT_IMAGE_ID, |
| 428 | mbr_entry.sector_nums); |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 429 | } |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 430 | } else { |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 431 | result = load_mbr_entries(image_handle); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 432 | } |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 433 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 434 | out: |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 435 | io_close(image_handle); |
| 436 | return result; |
| 437 | } |
| 438 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 439 | /* |
| 440 | * Try retrieving a partition table entry based on the name of the partition. |
| 441 | */ |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 442 | const partition_entry_t *get_partition_entry(const char *name) |
| 443 | { |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 444 | unsigned int i; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 445 | |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 446 | for (i = 0U; i < list.entry_count; i++) { |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 447 | if (strcmp(name, list.list[i].name) == 0) { |
| 448 | return &list.list[i]; |
| 449 | } |
| 450 | } |
| 451 | return NULL; |
| 452 | } |
| 453 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 454 | /* |
Sughosh Ganu | 744db7e | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 455 | * Try retrieving a partition table entry based on the partition type GUID. |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 456 | */ |
Sughosh Ganu | 744db7e | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 457 | const partition_entry_t *get_partition_entry_by_type( |
| 458 | const struct efi_guid *type_guid) |
Lionel Debieve | a88ca2e | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 459 | { |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 460 | unsigned int i; |
Lionel Debieve | a88ca2e | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 461 | |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 462 | for (i = 0U; i < list.entry_count; i++) { |
Sughosh Ganu | 744db7e | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 463 | if (guidcmp(type_guid, &list.list[i].type_guid) == 0) { |
Lionel Debieve | a88ca2e | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 464 | return &list.list[i]; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return NULL; |
| 469 | } |
| 470 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 471 | /* |
Sughosh Ganu | 744db7e | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 472 | * Try retrieving a partition table entry based on the unique partition GUID. |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 473 | */ |
Sughosh Ganu | 744db7e | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 474 | const partition_entry_t *get_partition_entry_by_guid( |
| 475 | const struct efi_guid *part_guid) |
Sughosh Ganu | 47b642e | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 476 | { |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 477 | unsigned int i; |
Sughosh Ganu | 47b642e | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 478 | |
laurenw-arm | 34b9503 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 479 | for (i = 0U; i < list.entry_count; i++) { |
Sughosh Ganu | 744db7e | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 480 | if (guidcmp(part_guid, &list.list[i].part_guid) == 0) { |
Sughosh Ganu | 47b642e | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 481 | return &list.list[i]; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 488 | /* |
| 489 | * Return entry to the list of partition table entries. |
| 490 | */ |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 491 | const partition_entry_list_t *get_partition_entry_list(void) |
| 492 | { |
| 493 | return &list; |
| 494 | } |
| 495 | |
Govindraj Raja | 232a11f | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 496 | /* |
| 497 | * Try loading partition table info for the given image ID. |
| 498 | */ |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 499 | void partition_init(unsigned int image_id) |
| 500 | { |
Govindraj Raja | 20431cf | 2023-10-12 16:41:07 -0500 | [diff] [blame] | 501 | 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 | */ |
| 513 | int gpt_partition_init(void) |
| 514 | { |
| 515 | return load_partition_table(GPT_IMAGE_ID); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 516 | } |