Debian Codegraph @

util/cbfstool/platform_fixups.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <commonlib/endian.h>
4 #include <string.h>
5
6 #include "cbfs.h"
7 #include "cbfs_sections.h"
8 #include "elfparsing.h"
9
10 #define MBN_V7 7
11
12 struct mbnv7_hashtbl_seg {
13 uint32_t reserved;
14 uint32_t version;
15 uint32_t common_metadata_size;
16 uint32_t qti_metadata_size;
17 uint32_t oem_metadata_size;
18 uint32_t hash_table_size;
19 uint32_t qti_signature_size;
20 uint32_t qti_certificate_chain_size;
21 uint32_t oem_signature_size;
22 uint32_t oem_certificate_chain_size;
23 };
24
25 enum hash_table_algo {
26 QUALCOMM_HASH_TABLE_ALGO_SHA384 = 3,
27 };
28
29 struct common_metadata {
30 uint32_t major;
31 uint32_t minor;
32 uint32_t software_id;
33 uint32_t secondary_software_id;
34 uint32_t hash_table_algorithm;
35 uint32_t measurement_register_target;
36 };
37
38 static void *qualcomm_find_hash_mbnv7(struct buffer *elf, size_t bb_offset,
39 struct vb2_hash *real_hash)
40 {
41 void *hashtable = NULL, *bb_hash = NULL;
42 struct common_metadata *cmn = NULL;
43 struct mbnv7_hashtbl_seg *mbn;
44 struct parsed_elf pelf;
45 int bb_segment = -1;
46 Elf64_Phdr *ph;
47
48 while (true) {
49 size_t tmp;
50
51 if (!buffer_check_magic(elf, ELFMAG, 4) ||
52 parse_elf(elf, &pelf, ELF_PARSE_PHDR))
53 return NULL; /* Not an ELF -- guess not a Qualcomm MBN after all? */
54
55 ph = &pelf.phdr[pelf.ehdr.e_phnum - 1];
56
57 tmp = ph->p_offset + ph->p_filesz;
58
59 if (buffer_offset(elf) < bb_offset &&
60 bb_offset < (buffer_offset(elf) + tmp)) /* found BB */
61 break;
62
63 parsed_elf_destroy(&pelf);
64
65 buffer_seek(elf, tmp);
66 }
67
68 /* Qualcomm stores an array of SHA-384 hashes in a special ELF segment,
69 one for each segment in order. */
70 for (int i = 0; i < pelf.ehdr.e_phnum; i++) {
71 ph = &pelf.phdr[i];
72 mbn = buffer_get(elf) + ph->p_offset;
73
74 if (((ph->p_flags & PF_QC_SG_MASK) == PF_QC_SG_HASH) &&
75 (mbn->version == MBN_V7)) {
76 cmn = buffer_get(elf) + ph->p_offset +
77 sizeof(*mbn);
78
79 hashtable = buffer_get(elf) + ph->p_offset +
80 sizeof(*mbn) + mbn->common_metadata_size +
81 mbn->qti_metadata_size +
82 mbn->oem_metadata_size;
83 } else if (ph->p_type == PT_LOAD && ph->p_flags == (PF_X | PF_W | PF_R)) {
84 bb_segment = i; /* Found the bootblock segment -- store its index. */
85 }
86 }
87
88 if (!hashtable || !cmn)
89 goto destroy_elf;
90
91 /* MBNv7 supports only SHA384, ensure that. */
92 if (cmn->hash_table_algorithm != QUALCOMM_HASH_TABLE_ALGO_SHA384)
93 goto destroy_elf;
94
95 bb_hash = hashtable + bb_segment * VB2_SHA384_DIGEST_SIZE;
96
97 /* Pass out the actual hash of the current bootblock segment in |real_hash|. */
98 if (vb2_hash_calculate(false, buffer_get(elf) + pelf.phdr[bb_segment].p_offset,
99 pelf.phdr[bb_segment].p_filesz, VB2_HASH_SHA384, real_hash)) {
100 ERROR("fixups: vboot digest error\n");
101 goto destroy_elf;
102 } /* Return pointer to where the bootblock hash needs to go in Qualcomm's table. */
103
104 destroy_elf:
105 parsed_elf_destroy(&pelf);
106 return bb_hash;
107 }
108
109 /*
110 * NOTE: This currently only implements support for MBN version 6 (as used by sc7180). Support
111 * for other MBN versions could probably be added but may require more parsing to tell them
112 * apart, and minor modifications (e.g. different hash algorithm). Add later as needed.
113 */
114 static void *qualcomm_find_hash(struct buffer *in, size_t bb_offset, struct vb2_hash *real_hash)
115 {
116 Elf64_Phdr *ph;
117 struct buffer elf;
118 buffer_clone(&elf, in);
119
120 /* When buffer_size(&elf) becomes this small, we know we've searched through 32KiB (or
121 the whole bootblock) without finding anything, so we know we can stop looking. */
122 size_t search_end_size = MIN(0, buffer_size(in) - 32 * KiB);
123
124 /* To identify a Qualcomm image, first we find the GPT header... */
125 while (buffer_size(&elf) > search_end_size &&
126 !buffer_check_magic(&elf, "EFI PART", 8))
127 buffer_seek(&elf, 512);
128
129 /* ...then shortly afterwards there's an ELF header... */
130 while (buffer_size(&elf) > search_end_size &&
131 !buffer_check_magic(&elf, ELFMAG, 4))
132 buffer_seek(&elf, 512);
133
134 if (buffer_size(&elf) <= search_end_size)
135 return NULL; /* Doesn't seem to be a Qualcomm image. */
136
137 struct parsed_elf pelf;
138 if (parse_elf(&elf, &pelf, ELF_PARSE_PHDR))
139 return NULL; /* Not an ELF -- guess not a Qualcomm MBN after all? */
140
141 /*
142 * Check if it is a multi ELF or single ELF. If bb_offset is beyond the
143 * final segment of first ELF, it is a multi ELF.
144 */
145 ph = &pelf.phdr[pelf.ehdr.e_phnum - 1];
146 if (bb_offset > (ph->p_offset + ph->p_filesz)) { /* MBNv7 multi ELF */
147 parsed_elf_destroy(&pelf);
148 return qualcomm_find_hash_mbnv7(&elf, bb_offset, real_hash);
149 }
150
151 /* Qualcomm stores an array of SHA-384 hashes in a special ELF segment. One special one
152 to start with, and then one for each segment in order. */
153 void *bb_hash = NULL;
154 void *hashtable = NULL;
155 int i;
156 int bb_segment = -1;
157 for (i = 0; i < pelf.ehdr.e_phnum; i++) {
158 ph = &pelf.phdr[i];
159 if ((ph->p_flags & PF_QC_SG_MASK) == PF_QC_SG_HASH) {
160 if ((int)ph->p_filesz !=
161 (pelf.ehdr.e_phnum + 1) * VB2_SHA384_DIGEST_SIZE) {
162 ERROR("fixups: Qualcomm hash segment has wrong size!\n");
163 goto destroy_elf;
164 } /* Found the table with the hashes -- store its address. */
165 hashtable = buffer_get(&elf) + ph->p_offset;
166 } else if (bb_segment < 0 && ph->p_offset + ph->p_filesz < buffer_size(&elf) &&
167 buffer_offset(&elf) + ph->p_offset <= bb_offset &&
168 buffer_offset(&elf) + ph->p_offset + ph->p_filesz > bb_offset) {
169 bb_segment = i; /* Found the bootblock segment -- store its index. */
170 }
171 }
172 if (!hashtable) /* ELF but no special QC hash segment -- guess not QC after all? */
173 goto destroy_elf;
174 if (bb_segment < 0) { /* Can assume it's QC if we found the special segment. */
175 ERROR("fixups: Cannot find bootblock code in Qualcomm MBN!\n");
176 goto destroy_elf;
177 }
178
179 /* Pass out the actual hash of the current bootblock segment in |real_hash|. */
180 if (vb2_hash_calculate(false, buffer_get(&elf) + pelf.phdr[bb_segment].p_offset,
181 pelf.phdr[bb_segment].p_filesz, VB2_HASH_SHA384, real_hash)) {
182 ERROR("fixups: vboot digest error\n");
183 goto destroy_elf;
184 } /* Return pointer to where the bootblock hash needs to go in Qualcomm's table. */
185 bb_hash = hashtable + (bb_segment + 1) * VB2_SHA384_DIGEST_SIZE;
186
187 destroy_elf:
188 parsed_elf_destroy(&pelf);
189 return bb_hash;
190 }
191
192 static bool qualcomm_probe(struct buffer *buffer, size_t offset)
193 {
194 struct vb2_hash real_hash;
195 void *table_hash = qualcomm_find_hash(buffer, offset, &real_hash);
196 if (!table_hash)
197 return false;
198
199 if (memcmp(real_hash.raw, table_hash, VB2_SHA384_DIGEST_SIZE)) {
200 ERROR("fixups: Identified Qualcomm MBN, but existing hash doesn't match!\n");
201 return false;
202 }
203
204 return true;
205 }
206
207 static int qualcomm_fixup(struct buffer *buffer, size_t offset)
208 {
209 struct vb2_hash real_hash;
210 void *table_hash = qualcomm_find_hash(buffer, offset, &real_hash);
211 if (!table_hash) {
212 ERROR("fixups: Cannot find Qualcomm MBN headers anymore!\n");
213 return -1;
214 }
215
216 memcpy(table_hash, real_hash.raw, VB2_SHA384_DIGEST_SIZE);
217 INFO("fixups: Updated Qualcomm MBN header bootblock hash.\n");
218 return 0;
219 }
220
221 /*
222 * MediaTek bootblock.bin layout (see util/mediatek/gen-bl-img.py):
223 * header 2048 bytes
224 * gfh info 176 bytes, where bytes 32-35 (in little endian) is the
225 * total size excluding the header (gfh info + data + hash)
226 * data `data_size` bytes
227 * hash 32 bytes, SHA256 of "gfh info + data"
228 * padding
229 */
230 #define MEDIATEK_BOOTBLOCK_HEADER_SIZE 2048
231 #define MEDIATEK_BOOTBLOCK_GFH_SIZE 176
232 static void *mediatek_find_hash(struct buffer *bootblock, struct vb2_hash *real_hash)
233 {
234 struct buffer buffer;
235 size_t data_size;
236 const char emmc_magic[] = "EMMC_BOOT";
237 const char sf_magic[] = "SF_BOOT";
238 const char brlyt_magic[] = "BRLYT";
239 const size_t brlyt_offset = 512;
240
241 buffer_clone(&buffer, bootblock);
242
243 /* Doesn't seem to be MediaTek image */
244 if (buffer_size(&buffer) <
245 MEDIATEK_BOOTBLOCK_HEADER_SIZE + MEDIATEK_BOOTBLOCK_GFH_SIZE)
246 return NULL;
247
248 /* Check header magic */
249 if (!buffer_check_magic(&buffer, emmc_magic, strlen(emmc_magic)) &&
250 !buffer_check_magic(&buffer, sf_magic, strlen(sf_magic)))
251 return NULL;
252
253 /* Check "BRLYT" */
254 buffer_seek(&buffer, brlyt_offset);
255 if (!buffer_check_magic(&buffer, brlyt_magic, strlen(brlyt_magic)))
256 return NULL;
257
258 buffer_seek(&buffer, MEDIATEK_BOOTBLOCK_HEADER_SIZE - brlyt_offset);
259 data_size = read_le32(buffer_get(&buffer) + 32);
260 if (data_size <= MEDIATEK_BOOTBLOCK_GFH_SIZE + VB2_SHA256_DIGEST_SIZE) {
261 ERROR("fixups: MediaTek: data size too small: %zu\n", data_size);
262 return NULL;
263 }
264 data_size -= MEDIATEK_BOOTBLOCK_GFH_SIZE + VB2_SHA256_DIGEST_SIZE;
265
266 if (buffer_size(&buffer) <
267 MEDIATEK_BOOTBLOCK_GFH_SIZE + data_size + VB2_SHA256_DIGEST_SIZE) {
268 ERROR("fixups: MediaTek: not enough data: %zu\n", buffer_size(&buffer));
269 return NULL;
270 }
271
272 if (vb2_hash_calculate(false, buffer_get(&buffer),
273 MEDIATEK_BOOTBLOCK_GFH_SIZE + data_size,
274 VB2_HASH_SHA256, real_hash)) {
275 ERROR("fixups: MediaTek: vboot digest error\n");
276 return NULL;
277 }
278
279 buffer_seek(&buffer, MEDIATEK_BOOTBLOCK_GFH_SIZE + data_size);
280 return buffer_get(&buffer);
281 }
282
283 static bool mediatek_probe(struct buffer *buffer)
284 {
285 struct vb2_hash real_hash;
286 void *hash = mediatek_find_hash(buffer, &real_hash);
287 if (!hash)
288 return false;
289
290 if (memcmp(real_hash.raw, hash, VB2_SHA256_DIGEST_SIZE)) {
291 ERROR("fixups: Found MediaTek bootblock, but existing hash doesn't match!\n");
292 return false;
293 }
294
295 return true;
296 }
297
298 static int mediatek_fixup(struct buffer *buffer, unused size_t offset)
299 {
300 struct vb2_hash real_hash;
301 void *hash = mediatek_find_hash(buffer, &real_hash);
302 if (!hash) {
303 ERROR("fixups: Cannot find MediaTek header anymore!\n");
304 return -1;
305 }
306
307 memcpy(hash, real_hash.raw, VB2_SHA256_DIGEST_SIZE);
308 INFO("fixups: Updated MediaTek bootblock hash.\n");
309 return 0;
310 }
311
312 platform_fixup_func platform_fixups_probe(struct buffer *buffer, size_t offset,
313 const char *region_name)
314 {
315 if (!strcmp(region_name, SECTION_NAME_BOOTBLOCK)) {
316 if (qualcomm_probe(buffer, offset))
317 return qualcomm_fixup;
318 else if (mediatek_probe(buffer))
319 return mediatek_fixup;
320 } else if (!strcmp(region_name, SECTION_NAME_PRIMARY_CBFS)) {
321 /* TODO: add fixups for primary CBFS bootblock platforms, if needed */
322 } else {
323 ERROR("%s called for unexpected FMAP region %s!\n", __func__, region_name);
324 }
325
326 return NULL;
327 }