1 /*
2  * Copyright (c) 2016-2022, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include "partition.h"
8 
9 #include <assert.h>
10 #include <errno.h>
11 #include <inttypes.h>
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include "efi.h"
16 #include "gpt.h"
17 #include "mbr.h"
18 
19 #include "io_storage.h"
20 #include "platform.h"
21 #include "soft_crc.h"
22 
23 #define PLAT_LOG_MODULE_NAME "partition"
24 #include "platform_log.h"
25 
26 static uint8_t mbr_sector[PLAT_PARTITION_BLOCK_SIZE];
27 static partition_entry_list_t list;
28 
29 #if LOG_LEVEL >= LOG_LEVEL_DEBUG
dump_entries(int num)30 static void dump_entries(int num) {
31     char name[EFI_NAMELEN];
32     int i, j, len;
33 
34     VERBOSE("Partition table with %d entries:", num);
35     for (i = 0; i < num; i++) {
36         len = snprintf(name, EFI_NAMELEN, "%s", list.list[i].name);
37         for (j = 0; j < EFI_NAMELEN - len - 1; j++) {
38             name[len + j] = ' ';
39         }
40         name[EFI_NAMELEN - 1] = '\0';
41         VERBOSE("%d: %s %x%x %d", i + 1, name,
42                 (uint32_t)(list.list[i].start >> 32),
43                 (uint32_t)list.list[i].start,
44                 (uint32_t)(list.list[i].start + list.list[i].length - 4));
45     }
46 }
47 #else
48 #define dump_entries(num) ((void)num)
49 #endif
50 
51 /*
52  * Load the first sector that carries MBR header.
53  * The MBR boot signature should be always valid whether it's MBR or GPT.
54  */
load_mbr_header(uintptr_t image_handle,mbr_entry_t * mbr_entry)55 static int load_mbr_header(uintptr_t image_handle, mbr_entry_t *mbr_entry) {
56     size_t bytes_read;
57     uintptr_t offset;
58     int result;
59 
60     assert(mbr_entry != NULL);
61     /* MBR partition table is in LBA0. */
62     result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
63     if (result != 0) {
64         WARN("Failed to seek (%i)\n", result);
65         return result;
66     }
67     result = io_read(image_handle, (uintptr_t)&mbr_sector,
68                      PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
69     if (result != 0) {
70         WARN("Failed to read data (%i)\n", result);
71         return result;
72     }
73 
74     /* Check MBR boot signature. */
75     if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
76         (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
77         ERROR("MBR signature isn't correct");
78         return -ENOENT;
79     }
80     offset = (uintptr_t)&mbr_sector + MBR_PRIMARY_ENTRY_OFFSET;
81     memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
82     return 0;
83 }
84 
85 /*
86  * Load GPT header and check the GPT signature and header CRC.
87  * If partition numbers could be found, check & update it.
88  */
load_gpt_header(uintptr_t image_handle)89 static int load_gpt_header(uintptr_t image_handle) {
90     gpt_header_t header;
91     size_t bytes_read;
92     int result;
93     uint32_t header_crc, calc_crc;
94 
95     result = io_seek(image_handle, IO_SEEK_SET, GPT_HEADER_OFFSET);
96     if (result != 0) {
97         return result;
98     }
99     result = io_read(image_handle, (uintptr_t)&header, sizeof(gpt_header_t),
100                      &bytes_read);
101     if ((result != 0) || (sizeof(gpt_header_t) != bytes_read)) {
102         return result;
103     }
104     if (memcmp(header.signature, GPT_SIGNATURE, sizeof(header.signature)) !=
105         0) {
106         return -EINVAL;
107     }
108 
109     /*
110      * UEFI Spec 2.8 March 2019 Page 119: HeaderCRC32 value is
111      * computed by setting this field to 0, and computing the
112      * 32-bit CRC for HeaderSize bytes.
113      */
114     header_crc = header.header_crc;
115     header.header_crc = 0U;
116 
117     calc_crc = crc32((uint8_t *)&header, DEFAULT_GPT_HEADER_SIZE);
118     if (header_crc != calc_crc) {
119         ERROR("Invalid GPT Header CRC: Expected 0x%x but got 0x%x.\n",
120               header_crc, calc_crc);
121         return -EINVAL;
122     }
123 
124     header.header_crc = header_crc;
125 
126     /* partition numbers can't exceed PLAT_PARTITION_MAX_ENTRIES */
127     list.entry_count = header.list_num;
128     if (list.entry_count > PLAT_PARTITION_MAX_ENTRIES) {
129         list.entry_count = PLAT_PARTITION_MAX_ENTRIES;
130     }
131     return 0;
132 }
133 
load_mbr_entry(uintptr_t image_handle,mbr_entry_t * mbr_entry,int part_number)134 static int load_mbr_entry(uintptr_t image_handle, mbr_entry_t *mbr_entry,
135                           int part_number) {
136     size_t bytes_read;
137     uintptr_t offset;
138     int result;
139 
140     assert(mbr_entry != NULL);
141     /* MBR partition table is in LBA0. */
142     result = io_seek(image_handle, IO_SEEK_SET, MBR_OFFSET);
143     if (result != 0) {
144         WARN("Failed to seek (%i)\n", result);
145         return result;
146     }
147     result = io_read(image_handle, (uintptr_t)&mbr_sector,
148                      PLAT_PARTITION_BLOCK_SIZE, &bytes_read);
149     if (result != 0) {
150         WARN("Failed to read data (%i)\n", result);
151         return result;
152     }
153 
154     /* Check MBR boot signature. */
155     if ((mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 2] != MBR_SIGNATURE_FIRST) ||
156         (mbr_sector[LEGACY_PARTITION_BLOCK_SIZE - 1] != MBR_SIGNATURE_SECOND)) {
157         return -ENOENT;
158     }
159     offset = (uintptr_t)&mbr_sector + MBR_PRIMARY_ENTRY_OFFSET +
160              MBR_PRIMARY_ENTRY_SIZE * part_number;
161     memcpy(mbr_entry, (void *)offset, sizeof(mbr_entry_t));
162 
163     return 0;
164 }
165 
load_mbr_entries(uintptr_t image_handle)166 static int load_mbr_entries(uintptr_t image_handle) {
167     mbr_entry_t mbr_entry;
168     int i;
169 
170     list.entry_count = MBR_PRIMARY_ENTRY_NUMBER;
171 
172     for (i = 0; i < list.entry_count; i++) {
173         load_mbr_entry(image_handle, &mbr_entry, i);
174         list.list[i].start = mbr_entry.first_lba * 512;
175         list.list[i].length = mbr_entry.sector_nums * 512;
176         list.list[i].name[0] = mbr_entry.type;
177     }
178 
179     return 0;
180 }
181 
load_gpt_entry(uintptr_t image_handle,gpt_entry_t * entry)182 static int load_gpt_entry(uintptr_t image_handle, gpt_entry_t *entry) {
183     size_t bytes_read;
184     int result;
185 
186     assert(entry != NULL);
187     result = io_read(image_handle, (uintptr_t)entry, sizeof(gpt_entry_t),
188                      &bytes_read);
189     if (sizeof(gpt_entry_t) != bytes_read) return -EINVAL;
190     return result;
191 }
192 
verify_partition_gpt(uintptr_t image_handle)193 static int verify_partition_gpt(uintptr_t image_handle) {
194     gpt_entry_t entry;
195     int result, i;
196 
197     for (i = 0; i < list.entry_count; i++) {
198         result = load_gpt_entry(image_handle, &entry);
199         assert(result == 0);
200         if (result != 0) {
201             break;
202         }
203         result = parse_gpt_entry(&entry, &list.list[i]);
204         if (result != 0) {
205             break;
206         }
207     }
208     if (i == 0) {
209         return -EINVAL;
210     }
211     /*
212      * Only records the valid partition number that is loaded from
213      * partition table.
214      */
215     list.entry_count = i;
216     dump_entries(list.entry_count);
217 
218     return 0;
219 }
220 
load_partition_table(unsigned int image_id)221 int load_partition_table(unsigned int image_id) {
222     uintptr_t dev_handle, image_handle, image_spec = 0;
223     mbr_entry_t mbr_entry;
224     int result;
225 
226     result = plat_get_image_source(image_id, &dev_handle, &image_spec);
227     if (result != 0) {
228         WARN("Failed to obtain reference to image id=%u (%i)\n", image_id,
229              result);
230         return result;
231     }
232 
233     result = io_open(dev_handle, image_spec, &image_handle);
234     if (result != 0) {
235         WARN("Failed to open image id=%u (%i)\n", image_id, result);
236         return result;
237     }
238 
239     result = load_mbr_header(image_handle, &mbr_entry);
240     if (result != 0) {
241         ERROR("Loading mbr header failed with image id=%u (%i)\n", image_id,
242               result);
243         return result;
244     }
245     if (mbr_entry.type == PARTITION_TYPE_GPT) {
246         INFO("Loading gpt header");
247         result = load_gpt_header(image_handle);
248         assert(result == 0);
249         if (result != 0) {
250             ERROR("Failed load gpt header! %i", result);
251             goto load_partition_table_exit;
252         }
253         result = io_seek(image_handle, IO_SEEK_SET, GPT_ENTRY_OFFSET);
254         assert(result == 0);
255         if (result != 0) {
256             ERROR("Failed seek gpt header! %i", result);
257             goto load_partition_table_exit;
258         }
259         result = verify_partition_gpt(image_handle);
260         if (result != 0) {
261             ERROR("Failed verify gpt partition %i", result);
262             goto load_partition_table_exit;
263         }
264     } else {
265         result = load_mbr_entries(image_handle);
266     }
267 
268 load_partition_table_exit:
269     io_close(image_handle);
270     return result;
271 }
272 
get_partition_entry(const char * name)273 const partition_entry_t *get_partition_entry(const char *name) {
274     int i;
275 
276     for (i = 0; i < list.entry_count; i++) {
277         if (strcmp(name, list.list[i].name) == 0) {
278             return &list.list[i];
279         }
280     }
281     return NULL;
282 }
283 
get_partition_entry_by_type(const uuid_t * type_uuid)284 const partition_entry_t *get_partition_entry_by_type(const uuid_t *type_uuid) {
285     int i;
286 
287     for (i = 0; i < list.entry_count; i++) {
288         if (guidcmp(type_uuid, &list.list[i].type_guid) == 0) {
289             return &list.list[i];
290         }
291     }
292 
293     return NULL;
294 }
295 
get_partition_replica_by_type(const uuid_t * type_uuid)296 const partition_entry_t *get_partition_replica_by_type(const uuid_t *type_uuid) {
297     int count = 0;
298     int i;
299 
300     for (i = 0; i < list.entry_count; i++) {
301         if (guidcmp(type_uuid, &list.list[i].type_guid) == 0) {
302             if (++count == 2)
303                  return &list.list[i];
304         }
305     }
306 
307     return NULL;
308 }
309 
get_partition_entry_by_uuid(const uuid_t * part_uuid)310 const partition_entry_t *get_partition_entry_by_uuid(const uuid_t *part_uuid) {
311     int i;
312 
313     for (i = 0; i < list.entry_count; i++) {
314         if (guidcmp(part_uuid, &list.list[i].part_guid) == 0) {
315             return &list.list[i];
316         }
317     }
318 
319     return NULL;
320 }
321 
get_partition_entry_list(void)322 const partition_entry_list_t *get_partition_entry_list(void) { return &list; }
323 
partition_init(unsigned int image_id)324 void partition_init(unsigned int image_id) { load_partition_table(image_id); }
325