blob: c84816f41d3d8836773f027a7d0f98b7ff3f1eef [file] [log] [blame]
Haojian Zhuang91f56462016-07-28 14:19:36 +08001/*
Rohit Nerc15dcd72022-05-06 07:58:21 +00002 * Copyright (c) 2016-2022, 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;
52 uintptr_t offset;
53 int result;
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 Zhuang42a746d2019-09-14 18:01:16 +080063 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Haojian Zhuang91f56462016-07-28 14:19:36 +080064 if (result != 0) {
65 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 }
74 offset = (uintptr_t)&mbr_sector + MBR_PRIMARY_ENTRY_OFFSET;
75 memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
76 return 0;
77}
78
79/*
Rohit Nerc15dcd72022-05-06 07:58:21 +000080 * Load GPT header and check the GPT signature and header CRC.
Paul Beesley1fbc97b2019-01-11 18:26:51 +000081 * If partition numbers could be found, check & update it.
Haojian Zhuang91f56462016-07-28 14:19:36 +080082 */
83static int load_gpt_header(uintptr_t image_handle)
84{
85 gpt_header_t header;
86 size_t bytes_read;
87 int result;
Rohit Nerc15dcd72022-05-06 07:58:21 +000088 uint32_t header_crc, calc_crc;
Haojian Zhuang91f56462016-07-28 14:19:36 +080089
90 result = io_seek(image_handle, IO_SEEK_SET, GPT_HEADER_OFFSET);
91 if (result != 0) {
92 return result;
93 }
94 result = io_read(image_handle, (uintptr_t)&header,
95 sizeof(gpt_header_t), &bytes_read);
96 if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) {
97 return result;
98 }
99 if (memcmp(header.signature, GPT_SIGNATURE,
100 sizeof(header.signature)) != 0) {
101 return -EINVAL;
102 }
103
Rohit Nerc15dcd72022-05-06 07:58:21 +0000104 /*
105 * UEFI Spec 2.8 March 2019 Page 119: HeaderCRC32 value is
106 * computed by setting this field to 0, and computing the
107 * 32-bit CRC for HeaderSize bytes.
108 */
109 header_crc = header.header_crc;
110 header.header_crc = 0U;
111
112 calc_crc = tf_crc32(0U, (uint8_t *)&header, DEFAULT_GPT_HEADER_SIZE);
113 if (header_crc != calc_crc) {
114 ERROR("Invalid GPT Header CRC: Expected 0x%x but got 0x%x.\n",
115 header_crc, calc_crc);
116 return -EINVAL;
117 }
118
119 header.header_crc = header_crc;
120
Haojian Zhuang91f56462016-07-28 14:19:36 +0800121 /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */
122 list.entry_count = header.list_num;
123 if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) {
124 list.entry_count = PLAT_PARTITION_MAX_ENTRIES;
125 }
126 return 0;
127}
128
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800129static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry,
130 int part_number)
131{
132 size_t bytes_read;
133 uintptr_t offset;
134 int result;
135
136 assert(mbr_entry != NULL);
137 /* MBR partition table is in LBA0. */
138 result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
139 if (result != 0) {
140 WARN("Failed to seek (%i)\n", result);
141 return result;
142 }
143 result = io_read(image_handle, (uintptr_t)&mbr_sector,
Haojian Zhuang42a746d2019-09-14 18:01:16 +0800144 PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800145 if (result != 0) {
146 WARN("Failed to read data (%i)\n", result);
147 return result;
148 }
149
150 /* Check MBR boot signature. */
Haojian Zhuang42a746d2019-09-14 18:01:16 +0800151 if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
152 (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800153 return -ENOENT;
154 }
155 offset = (uintptr_t)&mbr_sector +
156 MBR_PRIMARY_ENTRY_OFFSET +
157 MBR_PRIMARY_ENTRY_SIZE * part_number;
158 memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
159
160 return 0;
161}
162
163static int load_mbr_entries(uintptr_t image_handle)
164{
165 mbr_entry_t mbr_entry;
166 int i;
167
168 list.entry_count = MBR_PRIMARY_ENTRY_NUMBER;
169
170 for (i = 0; i < list.entry_count; i++) {
171 load_mbr_entry(image_handle, &mbr_entry, i);
172 list.list[i].start = mbr_entry.first_lba * 512;
173 list.list[i].length = mbr_entry.sector_nums * 512;
174 list.list[i].name[0] = mbr_entry.type;
175 }
176
177 return 0;
178}
179
Haojian Zhuang91f56462016-07-28 14:19:36 +0800180static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry)
181{
182 size_t bytes_read;
183 int result;
184
185 assert(entry != NULL);
186 result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t),
187 &bytes_read);
188 if (sizeof(gpt_entry_t) != bytes_read)
189 return -EINVAL;
190 return result;
191}
192
193static int verify_partition_gpt(uintptr_t image_handle)
194{
195 gpt_entry_t entry;
196 int result, i;
197
198 for (i = 0; i < list.entry_count; i++) {
199 result = load_gpt_entry(image_handle, &entry);
200 assert(result == 0);
201 result = parse_gpt_entry(&entry, &list.list[i]);
202 if (result != 0) {
203 break;
204 }
205 }
206 if (i == 0) {
207 return -EINVAL;
208 }
209 /*
210 * Only records the valid partition number that is loaded from
211 * partition table.
212 */
213 list.entry_count = i;
214 dump_entries(list.entry_count);
215
216 return 0;
217}
218
219int load_partition_table(unsigned int image_id)
220{
221 uintptr_t dev_handle, image_handle, image_spec = 0;
222 mbr_entry_t mbr_entry;
223 int result;
224
225 result = plat_get_image_source(image_id, &dev_handle, &image_spec);
226 if (result != 0) {
227 WARN("Failed to obtain reference to image id=%u (%i)\n",
228 image_id, result);
229 return result;
230 }
231
232 result = io_open(dev_handle, image_spec, &image_handle);
233 if (result != 0) {
234 WARN("Failed to access image id=%u (%i)\n", image_id, result);
235 return result;
236 }
237
238 result = load_mbr_header(image_handle, &mbr_entry);
239 if (result != 0) {
240 WARN("Failed to access image id=%u (%i)\n", image_id, result);
241 return result;
242 }
243 if (mbr_entry.type == PARTITION_TYPE_GPT) {
244 result = load_gpt_header(image_handle);
245 assert(result == 0);
246 result = io_seek(image_handle, IO_SEEK_SET, GPT_ENTRY_OFFSET);
247 assert(result == 0);
248 result = verify_partition_gpt(image_handle);
249 } else {
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800250 result = load_mbr_entries(image_handle);
Haojian Zhuang91f56462016-07-28 14:19:36 +0800251 }
Loh Tien Hock8defbae2019-02-11 10:56:28 +0800252
Haojian Zhuang91f56462016-07-28 14:19:36 +0800253 io_close(image_handle);
254 return result;
255}
256
257const partition_entry_t *get_partition_entry(const char *name)
258{
259 int i;
260
261 for (i = 0; i < list.entry_count; i++) {
262 if (strcmp(name, list.list[i].name) == 0) {
263 return &list.list[i];
264 }
265 }
266 return NULL;
267}
268
Sughosh Ganu47b642e2021-11-10 13:00:30 +0530269const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid)
270{
271 int i;
272
273 for (i = 0; i < list.entry_count; i++) {
274 if (guidcmp(part_uuid, &list.list[i].part_guid) == 0) {
275 return &list.list[i];
276 }
277 }
278
279 return NULL;
280}
281
Haojian Zhuang91f56462016-07-28 14:19:36 +0800282const partition_entry_list_t *get_partition_entry_list(void)
283{
284 return &list;
285}
286
287void partition_init(unsigned int image_id)
288{
289 load_partition_table(image_id);
290}