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