blob: 7b6551111c9d056ca03b72a45c4ff0a9140b2d25 [file] [log] [blame]
Haojian Zhuang91f56462016-07-28 14:19:36 +08001/*
Govindraj Raja998b1e22023-09-21 18:04:00 -05002 * Copyright (c) 2016-2023, Arm Limited and Contributors. All rights reserved.
Haojian Zhuang91f56462016-07-28 14:19:36 +08003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Haojian Zhuang91f56462016-07-28 14:19:36 +08005 */
6
7#include <assert.h>
Manish Pandey9b384b32021-11-12 12:59:09 +00008#include <inttypes.h>
Antonio Nino Diaz00086e32018-08-16 16:46:06 +01009#include <stdio.h>
Haojian Zhuang91f56462016-07-28 14:19:36 +080010#include <string.h>
11
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <common/debug.h>
Rohit Nerc15dcd72022-05-06 07:58:21 +000013#include <common/tf_crc32.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014#include <drivers/io/io_storage.h>
Sughosh Ganu47b642e2021-11-10 13:00:30 +053015#include <drivers/partition/efi.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <drivers/partition/partition.h>
17#include <drivers/partition/gpt.h>
18#include <drivers/partition/mbr.h>
19#include <plat/common/platform.h>
20
Haojian Zhuang42a746d2019-09-14 18:01:16 +080021static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE];
Florian La Roche231c2442019-01-27 14:30:12 +010022static partition_entry_list_t list;
Haojian Zhuang91f56462016-07-28 14:19:36 +080023
24#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
25static void dump_entries(int num)
26{
27 char name[EFI_NAMELEN];
28 int i, j, len;
29
30 VERBOSE("Partition table with %d entries:\n", num);
31 for (i = 0; i < num; i++) {
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010032 len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name);
Haojian Zhuang91f56462016-07-28 14:19:36 +080033 for (j = 0; j < EFI_NAMELEN - len - 1; j++) {
34 name[len + j] = ' ';
35 }
36 name[EFI_NAMELEN - 1] = '\0';
Manish Pandey9b384b32021-11-12 12:59:09 +000037 VERBOSE("%d: %s %" PRIx64 "-%" PRIx64 "\n", i + 1, name, list.list[i].start,
Haojian Zhuang91f56462016-07-28 14:19:36 +080038 list.list[i].start + list.list[i].length - 4);
39 }
40}
41#else
42#define dump_entries(num) ((void)num)
43#endif
44
45/*
46 * Load the first sector that carries MBR header.
47 * The MBR boot signature should be always valid whether it's MBR or GPT.
48 */
49static int load_mbr_header(uintptr_t image_handle, mbr_entry_t *mbr_entry)
50{
51 size_t bytes_read;
Haojian Zhuang91f56462016-07-28 14:19:36 +080052 int result;
Govindraj Raja998b1e22023-09-21 18:04:00 -050053 mbr_entry_t *tmp;
Haojian Zhuang91f56462016-07-28 14:19:36 +080054
55 assert(mbr_entry != NULL);
56 /* MBR partition table is in LBA0. */
57 result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
58 if (result != 0) {
59 WARN("Failed to seek (%i)\n", result);
60 return result;
61 }
62 result = io_read(image_handle, (uintptr_t)&mbr_sector,
Haojian Zhuang42a746d2019-09-14 18:01:16 +080063 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Govindraj Raja998b1e22023-09-21 18:04:00 -050064 if ((result != 0) || (bytes_read != PLAT_PARTITION_BLOCK_SIZE)) {
Haojian Zhuang91f56462016-07-28 14:19:36 +080065 WARN("Failed to read data (%i)\n", result);
66 return result;
67 }
68
69 /* Check MBR boot signature. */
Haojian Zhuang42a746d2019-09-14 18:01:16 +080070 if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
71 (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
Haojian Zhuang91f56462016-07-28 14:19:36 +080072 return -ENOENT;
73 }
Govindraj Raja998b1e22023-09-21 18:04:00 -050074
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 Zhuang91f56462016-07-28 14:19:36 +080088 return 0;
89}
90
91/*
Rohit Nerc15dcd72022-05-06 07:58:21 +000092 * Load GPT header and check the GPT signature and header CRC.
Paul Beesley1fbc97b2019-01-11 18:26:51 +000093 * If partition numbers could be found, check & update it.
Haojian Zhuang91f56462016-07-28 14:19:36 +080094 */
Govindraj Raja998b1e22023-09-21 18:04:00 -050095static int load_gpt_header(uintptr_t image_handle, size_t header_offset,
96 unsigned long long *part_lba)
Haojian Zhuang91f56462016-07-28 14:19:36 +080097{
98 gpt_header_t header;
99 size_t bytes_read;
100 int result;
Rohit Nerc15dcd72022-05-06 07:58:21 +0000101 uint32_t header_crc, calc_crc;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800102
Govindraj Raja998b1e22023-09-21 18:04:00 -0500103 result = io_seek(image_handle, IO_SEEK_SET, header_offset);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800104 if (result != 0) {
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 Nerc15dcd72022-05-06 07:58:21 +0000117 /*
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 Raja998b1e22023-09-21 18:04:00 -0500125 calc_crc = tf_crc32(0U, (uint8_t *)&header, sizeof(gpt_header_t));
Rohit Nerc15dcd72022-05-06 07:58:21 +0000126 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 Zhuang91f56462016-07-28 14:19:36 +0800134 /* 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 Raja998b1e22023-09-21 18:04:00 -0500139
140 *part_lba = header.part_lba;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800141 return 0;
142}
143
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800144static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry,
Govindraj Raja998b1e22023-09-21 18:04:00 -0500145 int part_number)
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800146{
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 Zhuang42a746d2019-09-14 18:01:16 +0800159 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800160 if (result != 0) {
161 WARN("Failed to read data (%i)\n", result);
162 return result;
163 }
164
165 /* Check MBR boot signature. */
Haojian Zhuang42a746d2019-09-14 18:01:16 +0800166 if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
167 (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800168 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
178static 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 Zhuang91f56462016-07-28 14:19:36 +0800195static 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
208static 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
234int 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 Raja998b1e22023-09-21 18:04:00 -0500239 size_t gpt_header_offset;
240 unsigned long long part_lba = 0, gpt_entry_offset = 0;
Haojian Zhuang91f56462016-07-28 14:19:36 +0800241
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 Raja998b1e22023-09-21 18:04:00 -0500261 /* 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 Zhuang91f56462016-07-28 14:19:36 +0800277 result = verify_partition_gpt(image_handle);
278 } else {
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800279 result = load_mbr_entries(image_handle);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800280 }
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800281
Haojian Zhuang91f56462016-07-28 14:19:36 +0800282 io_close(image_handle);
283 return result;
284}
285
286const 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 Debievea88ca2e2022-02-24 18:56:28 +0100298const 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 Ganu47b642e2021-11-10 13:00:30 +0530311const 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 Zhuang91f56462016-07-28 14:19:36 +0800324const partition_entry_list_t *get_partition_entry_list(void)
325{
326 return &list;
327}
328
329void partition_init(unsigned int image_id)
330{
331 load_partition_table(image_id);
332}