Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 1 | /* |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 2 | * Copyright (c) 2016-2023, 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) { |
| 59 | WARN("Failed to seek (%i)\n", result); |
| 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)) { |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 65 | WARN("Failed to read data (%i)\n", result); |
| 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)) { |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 72 | return -ENOENT; |
| 73 | } |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 74 | |
| 75 | tmp = (mbr_entry_t *)(&mbr_sector[MBR_PRIMARY_ENTRY_OFFSET]); |
| 76 | |
| 77 | if (tmp->first_lba != 1) { |
| 78 | WARN("MBR header may have an invalid first LBA\n"); |
| 79 | return -EINVAL; |
| 80 | } |
| 81 | |
| 82 | if ((tmp->sector_nums == 0) || (tmp->sector_nums == UINT32_MAX)) { |
| 83 | WARN("MBR header entry has an invalid number of sectors\n"); |
| 84 | return -EINVAL; |
| 85 | } |
| 86 | |
| 87 | memcpy(mbr_entry, tmp, sizeof(mbr_entry_t)); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 92 | * Load GPT header and check the GPT signature and header CRC. |
Paul Beesley | 1fbc97b | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 93 | * If partition numbers could be found, check & update it. |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 94 | */ |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 95 | static int load_gpt_header(uintptr_t image_handle, size_t header_offset, |
| 96 | unsigned long long *part_lba) |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 97 | { |
| 98 | gpt_header_t header; |
| 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) { |
| 105 | return result; |
| 106 | } |
| 107 | result = io_read(image_handle, (uintptr_t)&header, |
| 108 | sizeof(gpt_header_t), &bytes_read); |
| 109 | if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) { |
| 110 | return result; |
| 111 | } |
| 112 | if (memcmp(header.signature, GPT_SIGNATURE, |
| 113 | sizeof(header.signature)) != 0) { |
| 114 | return -EINVAL; |
| 115 | } |
| 116 | |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 117 | /* |
| 118 | * UEFI Spec 2.8 March 2019 Page 119: HeaderCRC32 value is |
| 119 | * computed by setting this field to 0, and computing the |
| 120 | * 32-bit CRC for HeaderSize bytes. |
| 121 | */ |
| 122 | header_crc = header.header_crc; |
| 123 | header.header_crc = 0U; |
| 124 | |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 125 | calc_crc = tf_crc32(0U, (uint8_t *)&header, sizeof(gpt_header_t)); |
Rohit Ner | c15dcd7 | 2022-05-06 07:58:21 +0000 | [diff] [blame] | 126 | if (header_crc != calc_crc) { |
| 127 | ERROR("Invalid GPT Header CRC: Expected 0x%x but got 0x%x.\n", |
| 128 | header_crc, calc_crc); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | header.header_crc = header_crc; |
| 133 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 134 | /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */ |
| 135 | list.entry_count = header.list_num; |
| 136 | if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) { |
| 137 | list.entry_count = PLAT_PARTITION_MAX_ENTRIES; |
| 138 | } |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 139 | |
| 140 | *part_lba = header.part_lba; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 141 | return 0; |
| 142 | } |
| 143 | |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 144 | 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^] | 145 | int part_number) |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 146 | { |
| 147 | size_t bytes_read; |
| 148 | uintptr_t offset; |
| 149 | int result; |
| 150 | |
| 151 | assert(mbr_entry != NULL); |
| 152 | /* MBR partition table is in LBA0. */ |
| 153 | result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET); |
| 154 | if (result != 0) { |
| 155 | WARN("Failed to seek (%i)\n", result); |
| 156 | return result; |
| 157 | } |
| 158 | result = io_read(image_handle, (uintptr_t)&mbr_sector, |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 159 | PLAT_PARTITION_BLOCK_SIZE, &bytes_read); |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 160 | if (result != 0) { |
| 161 | WARN("Failed to read data (%i)\n", result); |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | /* Check MBR boot signature. */ |
Haojian Zhuang | 42a746d | 2019-09-14 18:01:16 +0800 | [diff] [blame] | 166 | if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) || |
| 167 | (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) { |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 168 | return -ENOENT; |
| 169 | } |
| 170 | offset = (uintptr_t)&mbr_sector + |
| 171 | MBR_PRIMARY_ENTRY_OFFSET + |
| 172 | MBR_PRIMARY_ENTRY_SIZE * part_number; |
| 173 | memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t)); |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int load_mbr_entries(uintptr_t image_handle) |
| 179 | { |
| 180 | mbr_entry_t mbr_entry; |
| 181 | int i; |
| 182 | |
| 183 | list.entry_count = MBR_PRIMARY_ENTRY_NUMBER; |
| 184 | |
| 185 | for (i = 0; i < list.entry_count; i++) { |
| 186 | load_mbr_entry(image_handle, &mbr_entry, i); |
| 187 | list.list[i].start = mbr_entry.first_lba * 512; |
| 188 | list.list[i].length = mbr_entry.sector_nums * 512; |
| 189 | list.list[i].name[0] = mbr_entry.type; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 195 | static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry) |
| 196 | { |
| 197 | size_t bytes_read; |
| 198 | int result; |
| 199 | |
| 200 | assert(entry != NULL); |
| 201 | result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t), |
| 202 | &bytes_read); |
| 203 | if (sizeof(gpt_entry_t) != bytes_read) |
| 204 | return -EINVAL; |
| 205 | return result; |
| 206 | } |
| 207 | |
| 208 | static int verify_partition_gpt(uintptr_t image_handle) |
| 209 | { |
| 210 | gpt_entry_t entry; |
| 211 | int result, i; |
| 212 | |
| 213 | for (i = 0; i < list.entry_count; i++) { |
| 214 | result = load_gpt_entry(image_handle, &entry); |
| 215 | assert(result == 0); |
| 216 | result = parse_gpt_entry(&entry, &list.list[i]); |
| 217 | if (result != 0) { |
| 218 | break; |
| 219 | } |
| 220 | } |
| 221 | if (i == 0) { |
| 222 | return -EINVAL; |
| 223 | } |
| 224 | /* |
| 225 | * Only records the valid partition number that is loaded from |
| 226 | * partition table. |
| 227 | */ |
| 228 | list.entry_count = i; |
| 229 | dump_entries(list.entry_count); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int load_partition_table(unsigned int image_id) |
| 235 | { |
| 236 | uintptr_t dev_handle, image_handle, image_spec = 0; |
| 237 | mbr_entry_t mbr_entry; |
| 238 | int result; |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 239 | size_t gpt_header_offset; |
| 240 | unsigned long long part_lba = 0, gpt_entry_offset = 0; |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 241 | |
| 242 | result = plat_get_image_source(image_id, &dev_handle, &image_spec); |
| 243 | if (result != 0) { |
| 244 | WARN("Failed to obtain reference to image id=%u (%i)\n", |
| 245 | image_id, result); |
| 246 | return result; |
| 247 | } |
| 248 | |
| 249 | result = io_open(dev_handle, image_spec, &image_handle); |
| 250 | if (result != 0) { |
| 251 | WARN("Failed to access image id=%u (%i)\n", image_id, result); |
| 252 | return result; |
| 253 | } |
| 254 | |
| 255 | result = load_mbr_header(image_handle, &mbr_entry); |
| 256 | if (result != 0) { |
| 257 | WARN("Failed to access image id=%u (%i)\n", image_id, result); |
| 258 | return result; |
| 259 | } |
| 260 | if (mbr_entry.type == PARTITION_TYPE_GPT) { |
Govindraj Raja | 998b1e2 | 2023-09-21 18:04:00 -0500 | [diff] [blame^] | 261 | /* Try to load GPT header from LBA-1 */ |
| 262 | gpt_header_offset = mbr_entry.first_lba * PLAT_PARTITION_BLOCK_SIZE; |
| 263 | result = load_gpt_header(image_handle, gpt_header_offset, &part_lba); |
| 264 | if (result != 0 || part_lba == 0) { |
| 265 | WARN("Failed to retrieve Primary GPT header\n"); |
| 266 | return result; |
| 267 | } |
| 268 | |
| 269 | gpt_entry_offset = part_lba * PLAT_PARTITION_BLOCK_SIZE; |
| 270 | result = io_seek(image_handle, IO_SEEK_SET, gpt_entry_offset); |
| 271 | if (result != 0) { |
| 272 | WARN("Failed to seek (%i)," |
| 273 | "Failed loading GPT partition table entries\n", |
| 274 | result); |
| 275 | return result; |
| 276 | } |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 277 | result = verify_partition_gpt(image_handle); |
| 278 | } else { |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 279 | result = load_mbr_entries(image_handle); |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 280 | } |
Loh Tien Hock | 8defbae | 2019-02-11 10:56:28 +0800 | [diff] [blame] | 281 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 282 | io_close(image_handle); |
| 283 | return result; |
| 284 | } |
| 285 | |
| 286 | const partition_entry_t *get_partition_entry(const char *name) |
| 287 | { |
| 288 | int i; |
| 289 | |
| 290 | for (i = 0; i < list.entry_count; i++) { |
| 291 | if (strcmp(name, list.list[i].name) == 0) { |
| 292 | return &list.list[i]; |
| 293 | } |
| 294 | } |
| 295 | return NULL; |
| 296 | } |
| 297 | |
Lionel Debieve | a88ca2e | 2022-02-24 18:56:28 +0100 | [diff] [blame] | 298 | const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_uuid) |
| 299 | { |
| 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < list.entry_count; i++) { |
| 303 | if (guidcmp(type_uuid, &list.list[i].type_guid) == 0) { |
| 304 | return &list.list[i]; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return NULL; |
| 309 | } |
| 310 | |
Sughosh Ganu | 47b642e | 2021-11-10 13:00:30 +0530 | [diff] [blame] | 311 | const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | for (i = 0; i < list.entry_count; i++) { |
| 316 | if (guidcmp(part_uuid, &list.list[i].part_guid) == 0) { |
| 317 | return &list.list[i]; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return NULL; |
| 322 | } |
| 323 | |
Haojian Zhuang | 91f5646 | 2016-07-28 14:19:36 +0800 | [diff] [blame] | 324 | const partition_entry_list_t *get_partition_entry_list(void) |
| 325 | { |
| 326 | return &list; |
| 327 | } |
| 328 | |
| 329 | void partition_init(unsigned int image_id) |
| 330 | { |
| 331 | load_partition_table(image_id); |
| 332 | } |