Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 1 | /* |
Boyan Karatotev | ec48d52 | 2025-03-19 11:31:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2025, Arm Limited and Contributors. All rights reserved. |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 3 | * |
dp-arm | 82cb2c1 | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Manish Pandey | 4ef449c | 2021-11-12 12:59:09 +0000 | [diff] [blame] | 8 | #include <inttypes.h> |
Antonio Nino Diaz | 39b6cc6 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 9 | #include <stdio.h> |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <common/debug.h> |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 13 | #include <common/tf_crc32.h> |
Antonio Nino Diaz | 09d40e0 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 14 | #include <drivers/io/io_storage.h> |
Sughosh Ganu | 3cb1065 | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 15 | #include <drivers/partition/efi.h> |
Antonio Nino Diaz | 09d40e0 | 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 | f8631f5 | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 21 | static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE]; |
Florian La Roche | 9822852 | 2019-01-27 14:30:12 +0100 | [diff] [blame] | 22 | static partition_entry_list_t list; |
Haojian Zhuang | 201b66b | 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 | 39b6cc6 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 32 | len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name); |
Haojian Zhuang | 201b66b | 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 | 4ef449c | 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 | 201b66b | 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 | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 52 | int result; |
Chris Webb | 21a77e0 | 2024-07-04 12:33:15 +0100 | [diff] [blame] | 53 | mbr_entry_t tmp; |
Haojian Zhuang | 201b66b | 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 | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 59 | VERBOSE("Failed to seek (%i)\n", result); |
Haojian Zhuang | 201b66b | 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 | f8631f5 | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 63 | PLAT_PARTITION_BLOCK_SIZE, &bytes_read); |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 64 | if ((result != 0) || (bytes_read != PLAT_PARTITION_BLOCK_SIZE)) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 65 | VERBOSE("Failed to read data (%i)\n", result); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 66 | return result; |
| 67 | } |
| 68 | |
| 69 | /* Check MBR boot signature. */ |
Haojian Zhuang | f8631f5 | 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 | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 72 | VERBOSE("MBR boot signature failure\n"); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 73 | return -ENOENT; |
| 74 | } |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 75 | |
Chris Webb | 21a77e0 | 2024-07-04 12:33:15 +0100 | [diff] [blame] | 76 | memcpy(&tmp, mbr_sector + MBR_PRIMARY_ENTRY_OFFSET, sizeof(tmp)); |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 77 | |
Chris Webb | 21a77e0 | 2024-07-04 12:33:15 +0100 | [diff] [blame] | 78 | if ((tmp.sector_nums == 0) || (tmp.sector_nums == UINT32_MAX)) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 79 | VERBOSE("MBR header entry has an invalid number of sectors\n"); |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 80 | return -EINVAL; |
| 81 | } |
| 82 | |
Chris Webb | 21a77e0 | 2024-07-04 12:33:15 +0100 | [diff] [blame] | 83 | memcpy(mbr_entry, &tmp, sizeof(mbr_entry_t)); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | /* |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 88 | * Load GPT header and check the GPT signature and header CRC. |
Paul Beesley | 8aabea3 | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 89 | * If partition numbers could be found, check & update it. |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 90 | */ |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 91 | static int load_gpt_header(uintptr_t image_handle, size_t header_offset, |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 92 | gpt_header_t *header) |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 93 | { |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 94 | size_t bytes_read; |
| 95 | int result; |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 96 | uint32_t header_crc, calc_crc; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 97 | |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 98 | result = io_seek(image_handle, IO_SEEK_SET, header_offset); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 99 | if (result != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 100 | VERBOSE("Failed to seek into the GPT image at offset (%zu)\n", |
| 101 | header_offset); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 102 | return result; |
| 103 | } |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 104 | result = io_read(image_handle, (uintptr_t)header, |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 105 | sizeof(gpt_header_t), &bytes_read); |
| 106 | if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 107 | 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 Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 110 | return result; |
| 111 | } |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 112 | if (memcmp(header->signature, GPT_SIGNATURE, |
| 113 | sizeof(header->signature)) != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 114 | VERBOSE("GPT header signature failure\n"); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 115 | return -EINVAL; |
| 116 | } |
| 117 | |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 118 | /* |
| 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-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 123 | header_crc = header->header_crc; |
| 124 | header->header_crc = 0U; |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 125 | |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 126 | calc_crc = tf_crc32(0U, (uint8_t *)header, sizeof(gpt_header_t)); |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 127 | 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-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 133 | header->header_crc = header_crc; |
Rohit Ner | a283d19 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 134 | |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 135 | /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */ |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 136 | list.entry_count = header->list_num; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 137 | if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) { |
| 138 | list.entry_count = PLAT_PARTITION_MAX_ENTRIES; |
| 139 | } |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 140 | |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 144 | /* |
| 145 | * Load a single MBR entry based on details from MBR header. |
| 146 | */ |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 147 | static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry, |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 148 | int part_number) |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 149 | { |
| 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 Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 158 | VERBOSE("Failed to seek (%i)\n", result); |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 159 | return result; |
| 160 | } |
| 161 | result = io_read(image_handle, (uintptr_t)&mbr_sector, |
Haojian Zhuang | f8631f5 | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 162 | PLAT_PARTITION_BLOCK_SIZE, &bytes_read); |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 163 | if (result != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 164 | VERBOSE("Failed to read data (%i)\n", result); |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 165 | return result; |
| 166 | } |
| 167 | |
| 168 | /* Check MBR boot signature. */ |
Haojian Zhuang | f8631f5 | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 169 | if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) || |
| 170 | (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 171 | VERBOSE("MBR Entry boot signature failure\n"); |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 172 | 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 Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 182 | /* |
| 183 | * Load MBR entries based on max number of partition entries. |
| 184 | */ |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 185 | static int load_mbr_entries(uintptr_t image_handle) |
| 186 | { |
| 187 | mbr_entry_t mbr_entry; |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 188 | unsigned int i; |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 189 | |
| 190 | list.entry_count = MBR_PRIMARY_ENTRY_NUMBER; |
| 191 | |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 192 | for (i = 0U; i < list.entry_count; i++) { |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 193 | 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 Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 202 | /* |
| 203 | * Try to read and load a single GPT entry. |
| 204 | */ |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 205 | static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry) |
| 206 | { |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 207 | size_t bytes_read = 0U; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 208 | int result; |
| 209 | |
| 210 | assert(entry != NULL); |
| 211 | result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t), |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 212 | &bytes_read); |
| 213 | if ((result != 0) || (sizeof(gpt_entry_t) != bytes_read)) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 214 | 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 Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 217 | return -EINVAL; |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 220 | return result; |
| 221 | } |
| 222 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 223 | /* |
| 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-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 227 | static int load_partition_gpt(uintptr_t image_handle, gpt_header_t header) |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 228 | { |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 229 | const signed long long gpt_entry_offset = LBA(header.part_lba); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 230 | gpt_entry_t entry; |
laurenw-arm | 7a9e9f6 | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 231 | int result; |
| 232 | unsigned int i; |
| 233 | uint32_t calc_crc = 0U; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 234 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 235 | result = io_seek(image_handle, IO_SEEK_SET, gpt_entry_offset); |
| 236 | if (result != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 237 | VERBOSE("Failed to seek (%i), Failed loading GPT partition" |
| 238 | "table entries\n", result); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 239 | return result; |
| 240 | } |
| 241 | |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 242 | for (i = 0U; i < list.entry_count; i++) { |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 243 | result = load_gpt_entry(image_handle, &entry); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 244 | if (result != 0) { |
laurenw-arm | 7a9e9f6 | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 245 | VERBOSE("Failed to load gpt entry data(%u) error is (%i)\n", |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 246 | i, result); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 247 | return result; |
| 248 | } |
| 249 | |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 250 | result = parse_gpt_entry(&entry, &list.list[i]); |
| 251 | if (result != 0) { |
laurenw-arm | 7a9e9f6 | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 252 | 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 Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 258 | break; |
| 259 | } |
laurenw-arm | 7a9e9f6 | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 260 | |
| 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 Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 266 | } |
| 267 | if (i == 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 268 | VERBOSE("No Valid GPT Entries found\n"); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 269 | return -EINVAL; |
| 270 | } |
laurenw-arm | 7a9e9f6 | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 271 | |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 272 | /* |
| 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-arm | 7a9e9f6 | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 279 | /* |
| 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 Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 305 | /* |
| 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 | */ |
| 309 | static int load_backup_gpt(unsigned int image_id, unsigned int sector_nums) |
| 310 | { |
| 311 | int result; |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 312 | gpt_header_t header; |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 313 | 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 Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 320 | VERBOSE("Failed to obtain reference to image id=%u (%i)\n", |
| 321 | image_id, result); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 322 | 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 Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 342 | VERBOSE("Failed to access image id (%i)\n", result); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 343 | 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-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 349 | result = load_gpt_header(image_handle, gpt_header_offset, &header); |
| 350 | if ((result != 0) || (header.part_lba == 0)) { |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 351 | 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-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 360 | header.part_lba = 0; |
| 361 | result = load_partition_gpt(image_handle, header); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 362 | |
| 363 | out: |
| 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 | */ |
| 373 | static int load_primary_gpt(uintptr_t image_handle, unsigned int first_lba) |
| 374 | { |
| 375 | int result; |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 376 | size_t gpt_header_offset; |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 377 | gpt_header_t header; |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 378 | |
| 379 | /* Try to load Primary GPT header from LBA1 */ |
| 380 | gpt_header_offset = LBA(first_lba); |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 381 | result = load_gpt_header(image_handle, gpt_header_offset, &header); |
| 382 | if ((result != 0) || (header.part_lba == 0)) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 383 | VERBOSE("Failed to retrieve Primary GPT header," |
| 384 | "trying to retrieve back-up GPT header\n"); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 385 | return result; |
| 386 | } |
| 387 | |
laurenw-arm | 17a261d | 2024-01-31 16:21:48 -0600 | [diff] [blame] | 388 | return load_partition_gpt(image_handle, header); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Load the partition table info based on the image id provided. |
| 393 | */ |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 394 | int load_partition_table(unsigned int image_id) |
| 395 | { |
| 396 | uintptr_t dev_handle, image_handle, image_spec = 0; |
Boyan Karatotev | ec48d52 | 2025-03-19 11:31:34 +0000 | [diff] [blame] | 397 | mbr_entry_t mbr_entry = {0}; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 398 | int result; |
| 399 | |
| 400 | result = plat_get_image_source(image_id, &dev_handle, &image_spec); |
| 401 | if (result != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 402 | VERBOSE("Failed to obtain reference to image id=%u (%i)\n", |
| 403 | image_id, result); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 404 | return result; |
| 405 | } |
| 406 | |
| 407 | result = io_open(dev_handle, image_spec, &image_handle); |
| 408 | if (result != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 409 | VERBOSE("Failed to access image id=%u (%i)\n", image_id, result); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 410 | return result; |
| 411 | } |
| 412 | |
| 413 | result = load_mbr_header(image_handle, &mbr_entry); |
| 414 | if (result != 0) { |
Govindraj Raja | 0f23e7e | 2023-10-12 17:31:41 -0500 | [diff] [blame] | 415 | VERBOSE("Failed to access image id=%u (%i)\n", image_id, result); |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 416 | goto out; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 417 | } |
| 418 | if (mbr_entry.type == PARTITION_TYPE_GPT) { |
Bogdan Roman | 2fac89d | 2024-09-26 16:53:32 +0300 | [diff] [blame] | 419 | if (mbr_entry.first_lba != 1U) { |
| 420 | VERBOSE("MBR header may have an invalid first LBA\n"); |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 424 | result = load_primary_gpt(image_handle, mbr_entry.first_lba); |
Govindraj Raja | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 425 | if (result != 0) { |
Govindraj Raja | ad2dd65 | 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 | fce8a70 | 2023-09-21 18:04:00 -0500 | [diff] [blame] | 429 | } |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 430 | } else { |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 431 | result = load_mbr_entries(image_handle); |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 432 | } |
Loh Tien Hock | 30f833c | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 433 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 434 | out: |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 435 | io_close(image_handle); |
| 436 | return result; |
| 437 | } |
| 438 | |
Govindraj Raja | ad2dd65 | 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 | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 442 | const partition_entry_t *get_partition_entry(const char *name) |
| 443 | { |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 444 | unsigned int i; |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 445 | |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 446 | for (i = 0U; i < list.entry_count; i++) { |
Haojian Zhuang | 201b66b | 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 | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 454 | /* |
Sughosh Ganu | 37e81a6 | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 455 | * Try retrieving a partition table entry based on the partition type GUID. |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 456 | */ |
Sughosh Ganu | 37e81a6 | 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 | 564f5d4 | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 459 | { |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 460 | unsigned int i; |
Lionel Debieve | 564f5d4 | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 461 | |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 462 | for (i = 0U; i < list.entry_count; i++) { |
Sughosh Ganu | 37e81a6 | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 463 | if (guidcmp(type_guid, &list.list[i].type_guid) == 0) { |
Lionel Debieve | 564f5d4 | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 464 | return &list.list[i]; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return NULL; |
| 469 | } |
| 470 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 471 | /* |
Sughosh Ganu | 37e81a6 | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 472 | * Try retrieving a partition table entry based on the unique partition GUID. |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 473 | */ |
Sughosh Ganu | 37e81a6 | 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 | 3cb1065 | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 476 | { |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 477 | unsigned int i; |
Sughosh Ganu | 3cb1065 | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 478 | |
laurenw-arm | ce57431 | 2024-02-29 15:34:39 -0600 | [diff] [blame] | 479 | for (i = 0U; i < list.entry_count; i++) { |
Sughosh Ganu | 37e81a6 | 2024-02-02 15:32:10 +0530 | [diff] [blame] | 480 | if (guidcmp(part_guid, &list.list[i].part_guid) == 0) { |
Sughosh Ganu | 3cb1065 | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 481 | return &list.list[i]; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Govindraj Raja | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 488 | /* |
| 489 | * Return entry to the list of partition table entries. |
| 490 | */ |
Haojian Zhuang | 201b66b | 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 | ad2dd65 | 2023-10-03 16:39:32 -0500 | [diff] [blame] | 496 | /* |
| 497 | * Try loading partition table info for the given image ID. |
| 498 | */ |
Haojian Zhuang | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 499 | void partition_init(unsigned int image_id) |
| 500 | { |
Govindraj Raja | f08460d | 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 | 201b66b | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 516 | } |