1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Philippe Reynes <philippe.reynes@softathome.com>
7 #include <asm/global_data.h>
8 DECLARE_GLOBAL_DATA_PTR;
12 #include <u-boot/sha256.h>
17 * This value is used to skip the header before really launching the image
19 ulong image_load_offset;
22 * This function gathers information about the signature check
23 * that could be done before launching the image.
26 * < 0 => an error has occurred
30 static int image_pre_load_sig_setup(struct image_sig_info *info)
32 const void *algo_name, *padding_name, *key, *mandatory;
36 char *sig_info_path = NULL;
39 log_err("ERROR: info is NULL for image pre-load sig check\n");
44 memset(info, 0, sizeof(*info));
46 sig_info_path = env_get("pre_load_sig_info_path");
48 sig_info_path = IMAGE_PRE_LOAD_PATH;
50 node = fdt_path_offset(gd_fdt_blob(), sig_info_path);
52 log_info("INFO: no info for image pre-load sig check\n");
57 algo_name = fdt_getprop(gd_fdt_blob(), node,
58 IMAGE_PRE_LOAD_PROP_ALGO_NAME, NULL);
60 printf("ERROR: no algo_name for image pre-load sig check\n");
65 padding_name = fdt_getprop(gd_fdt_blob(), node,
66 IMAGE_PRE_LOAD_PROP_PADDING_NAME, NULL);
68 log_info("INFO: no padding_name provided, so using pkcs-1.5\n");
69 padding_name = "pkcs-1.5";
72 sig_size = fdt_getprop(gd_fdt_blob(), node,
73 IMAGE_PRE_LOAD_PROP_SIG_SIZE, NULL);
75 log_err("ERROR: no signature-size for image pre-load sig check\n");
80 key = fdt_getprop(gd_fdt_blob(), node,
81 IMAGE_PRE_LOAD_PROP_PUBLIC_KEY, &key_len);
83 log_err("ERROR: no key for image pre-load sig check\n");
88 info->algo_name = (char *)algo_name;
89 info->padding_name = (char *)padding_name;
90 info->key = (uint8_t *)key;
91 info->key_len = key_len;
92 info->sig_size = fdt32_to_cpu(*sig_size);
94 mandatory = fdt_getprop(gd_fdt_blob(), node,
95 IMAGE_PRE_LOAD_PROP_MANDATORY, NULL);
96 if (mandatory && !strcmp((char *)mandatory, "yes"))
99 /* Compute signature information */
100 info->sig_info.name = info->algo_name;
101 info->sig_info.padding = image_get_padding_algo(info->padding_name);
102 info->sig_info.checksum = image_get_checksum_algo(info->sig_info.name);
103 info->sig_info.crypto = image_get_crypto_algo(info->sig_info.name);
104 info->sig_info.key = info->key;
105 info->sig_info.keylen = info->key_len;
111 static int image_pre_load_sig_get_magic(ulong addr, u32 *magic)
113 struct sig_header_s *sig_header;
116 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
118 log_err("ERROR: can't map first header\n");
123 *magic = fdt32_to_cpu(sig_header->magic);
125 unmap_sysmem(sig_header);
131 static int image_pre_load_sig_get_header_size(ulong addr, u32 *header_size)
133 struct sig_header_s *sig_header;
136 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
138 log_err("ERROR: can't map first header\n");
143 *header_size = fdt32_to_cpu(sig_header->header_size);
145 unmap_sysmem(sig_header);
153 * < 0 => no magic and magic mandatory (or error when reading magic)
155 * 1 => magic NOT found
157 static int image_pre_load_sig_check_magic(struct image_sig_info *info, ulong addr)
162 ret = image_pre_load_sig_get_magic(addr, &magic);
166 if (magic != IMAGE_PRE_LOAD_SIG_MAGIC) {
167 if (info->mandatory) {
168 log_err("ERROR: signature is mandatory\n");
176 ret = 0; /* magic found */
182 static int image_pre_load_sig_check_header_sig(struct image_sig_info *info, ulong addr)
185 struct image_region reg;
190 /* Only map header of the header and its signature */
191 header = (void *)map_sysmem(addr, SIG_HEADER_LEN + info->sig_size);
193 log_err("ERROR: can't map header\n");
199 reg.size = SIG_HEADER_LEN;
201 sig = (uint8_t *)header + SIG_HEADER_LEN;
202 sig_len = info->sig_size;
204 ret = info->sig_info.crypto->verify(&info->sig_info, ®, 1, sig, sig_len);
206 log_err("ERROR: header signature check has failed (err=%d)\n", ret);
212 unmap_sysmem(header);
218 static int image_pre_load_sig_check_img_sig_sha256(struct image_sig_info *info, ulong addr)
220 struct sig_header_s *sig_header;
221 u32 header_size, offset_img_sig;
223 u8 sha256_img_sig[SHA256_SUM_LEN];
226 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
228 log_err("ERROR: can't map first header\n");
233 header_size = fdt32_to_cpu(sig_header->header_size);
234 offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
236 header = (void *)map_sysmem(addr, header_size);
238 log_err("ERROR: can't map header\n");
243 sha256_csum_wd(header + offset_img_sig, info->sig_size,
244 sha256_img_sig, CHUNKSZ_SHA256);
246 ret = memcmp(sig_header->sha256_img_sig, sha256_img_sig, SHA256_SUM_LEN);
248 log_err("ERROR: sha256 of image signature is invalid\n");
254 unmap_sysmem(header);
256 unmap_sysmem(sig_header);
261 static int image_pre_load_sig_check_img_sig(struct image_sig_info *info, ulong addr)
263 struct sig_header_s *sig_header;
264 u32 header_size, image_size, offset_img_sig;
266 struct image_region reg;
271 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
273 log_err("ERROR: can't map first header\n");
278 header_size = fdt32_to_cpu(sig_header->header_size);
279 image_size = fdt32_to_cpu(sig_header->image_size);
280 offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
282 unmap_sysmem(sig_header);
284 image = (void *)map_sysmem(addr, header_size + image_size);
286 log_err("ERROR: can't map full image\n");
291 reg.data = image + header_size;
292 reg.size = image_size;
294 sig = (uint8_t *)image + offset_img_sig;
295 sig_len = info->sig_size;
297 ret = info->sig_info.crypto->verify(&info->sig_info, ®, 1, sig, sig_len);
299 log_err("ERROR: signature check has failed (err=%d)\n", ret);
301 goto out_unmap_image;
304 log_info("INFO: signature check has succeed\n");
313 int image_pre_load_sig(ulong addr)
315 struct image_sig_info info;
318 ret = image_pre_load_sig_setup(&info);
326 ret = image_pre_load_sig_check_magic(&info, addr);
334 /* Check the signature of the signature header */
335 ret = image_pre_load_sig_check_header_sig(&info, addr);
339 /* Check sha256 of the image signature */
340 ret = image_pre_load_sig_check_img_sig_sha256(&info, addr);
344 /* Check the image signature */
345 ret = image_pre_load_sig_check_img_sig(&info, addr);
349 ret = image_pre_load_sig_get_header_size(addr, &header_size);
351 log_err("%s: can't get header size\n", __func__);
356 image_load_offset += header_size;
363 int image_pre_load(ulong addr)
367 image_load_offset = 0;
369 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD_SIG))
370 ret = image_pre_load_sig(addr);