1 // SPDX-License-Identifier: GPL-2.0+
7 #include <asm/global_data.h>
8 DECLARE_GLOBAL_DATA_PTR;
12 #include <u-boot/sha256.h>
14 #define IMAGE_PRE_LOAD_SIG_MAGIC 0x55425348
15 #define IMAGE_PRE_LOAD_SIG_OFFSET_MAGIC 0
16 #define IMAGE_PRE_LOAD_SIG_OFFSET_IMG_LEN 4
17 #define IMAGE_PRE_LOAD_SIG_OFFSET_SIG 8
19 #define IMAGE_PRE_LOAD_PATH "/image/pre-load/sig"
20 #define IMAGE_PRE_LOAD_PROP_ALGO_NAME "algo-name"
21 #define IMAGE_PRE_LOAD_PROP_PADDING_NAME "padding-name"
22 #define IMAGE_PRE_LOAD_PROP_SIG_SIZE "signature-size"
23 #define IMAGE_PRE_LOAD_PROP_PUBLIC_KEY "public-key"
24 #define IMAGE_PRE_LOAD_PROP_MANDATORY "mandatory"
26 #ifndef CONFIG_SYS_BOOTM_LEN
27 /* use 8MByte as default max gunzip size */
28 #define CONFIG_SYS_BOOTM_LEN 0x800000
32 * Information in the device-tree about the signature in the header
34 struct image_sig_info {
35 char *algo_name; /* Name of the algo (eg: sha256,rsa2048) */
36 char *padding_name; /* Name of the padding */
37 u8 *key; /* Public signature key */
38 int key_len; /* Length of the public key */
39 u32 sig_size; /* size of the signature (in the header) */
40 int mandatory; /* Set if the signature is mandatory */
42 struct image_sign_info sig_info; /* Signature info */
46 * Header of the signature header
57 u8 sha256_img_sig[SHA256_SUM_LEN];
60 #define SIG_HEADER_LEN (sizeof(struct sig_header_s))
65 * This value is used to skip the header before really launching the image
67 ulong image_load_offset;
70 * This function gathers information about the signature check
71 * that could be done before launching the image.
74 * < 0 => an error has occurred
78 static int image_pre_load_sig_setup(struct image_sig_info *info)
80 const void *algo_name, *padding_name, *key, *mandatory;
86 log_err("ERROR: info is NULL for image pre-load sig check\n");
91 memset(info, 0, sizeof(*info));
93 node = fdt_path_offset(gd_fdt_blob(), IMAGE_PRE_LOAD_PATH);
95 log_info("INFO: no info for image pre-load sig check\n");
100 algo_name = fdt_getprop(gd_fdt_blob(), node,
101 IMAGE_PRE_LOAD_PROP_ALGO_NAME, NULL);
103 printf("ERROR: no algo_name for image pre-load sig check\n");
108 padding_name = fdt_getprop(gd_fdt_blob(), node,
109 IMAGE_PRE_LOAD_PROP_PADDING_NAME, NULL);
111 log_info("INFO: no padding_name provided, so using pkcs-1.5\n");
112 padding_name = "pkcs-1.5";
115 sig_size = fdt_getprop(gd_fdt_blob(), node,
116 IMAGE_PRE_LOAD_PROP_SIG_SIZE, NULL);
118 log_err("ERROR: no signature-size for image pre-load sig check\n");
123 key = fdt_getprop(gd_fdt_blob(), node,
124 IMAGE_PRE_LOAD_PROP_PUBLIC_KEY, &key_len);
126 log_err("ERROR: no key for image pre-load sig check\n");
131 info->algo_name = (char *)algo_name;
132 info->padding_name = (char *)padding_name;
133 info->key = (uint8_t *)key;
134 info->key_len = key_len;
135 info->sig_size = fdt32_to_cpu(*sig_size);
137 mandatory = fdt_getprop(gd_fdt_blob(), node,
138 IMAGE_PRE_LOAD_PROP_MANDATORY, NULL);
139 if (mandatory && !strcmp((char *)mandatory, "yes"))
142 /* Compute signature information */
143 info->sig_info.name = info->algo_name;
144 info->sig_info.padding = image_get_padding_algo(info->padding_name);
145 info->sig_info.checksum = image_get_checksum_algo(info->sig_info.name);
146 info->sig_info.crypto = image_get_crypto_algo(info->sig_info.name);
147 info->sig_info.key = info->key;
148 info->sig_info.keylen = info->key_len;
154 static int image_pre_load_sig_get_magic(ulong addr, u32 *magic)
156 struct sig_header_s *sig_header;
159 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
161 log_err("ERROR: can't map first header\n");
166 *magic = fdt32_to_cpu(sig_header->magic);
168 unmap_sysmem(sig_header);
174 static int image_pre_load_sig_get_header_size(ulong addr, u32 *header_size)
176 struct sig_header_s *sig_header;
179 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
181 log_err("ERROR: can't map first header\n");
186 *header_size = fdt32_to_cpu(sig_header->header_size);
188 unmap_sysmem(sig_header);
196 * < 0 => no magic and magic mandatory (or error when reading magic)
198 * 1 => magic NOT found
200 static int image_pre_load_sig_check_magic(struct image_sig_info *info, ulong addr)
205 ret = image_pre_load_sig_get_magic(addr, &magic);
209 if (magic != IMAGE_PRE_LOAD_SIG_MAGIC) {
210 if (info->mandatory) {
211 log_err("ERROR: signature is mandatory\n");
219 ret = 0; /* magic found */
225 static int image_pre_load_sig_check_header_sig(struct image_sig_info *info, ulong addr)
228 struct image_region reg;
233 /* Only map header of the header and its signature */
234 header = (void *)map_sysmem(addr, SIG_HEADER_LEN + info->sig_size);
236 log_err("ERROR: can't map header\n");
242 reg.size = SIG_HEADER_LEN;
244 sig = (uint8_t *)header + SIG_HEADER_LEN;
245 sig_len = info->sig_size;
247 ret = info->sig_info.crypto->verify(&info->sig_info, ®, 1, sig, sig_len);
249 log_err("ERROR: header signature check has failed (err=%d)\n", ret);
255 unmap_sysmem(header);
261 static int image_pre_load_sig_check_img_sig_sha256(struct image_sig_info *info, ulong addr)
263 struct sig_header_s *sig_header;
264 u32 header_size, offset_img_sig;
266 u8 sha256_img_sig[SHA256_SUM_LEN];
269 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
271 log_err("ERROR: can't map first header\n");
276 header_size = fdt32_to_cpu(sig_header->header_size);
277 offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
279 header = (void *)map_sysmem(addr, header_size);
281 log_err("ERROR: can't map header\n");
286 sha256_csum_wd(header + offset_img_sig, info->sig_size,
287 sha256_img_sig, CHUNKSZ_SHA256);
289 ret = memcmp(sig_header->sha256_img_sig, sha256_img_sig, SHA256_SUM_LEN);
291 log_err("ERROR: sha256 of image signature is invalid\n");
297 unmap_sysmem(header);
299 unmap_sysmem(sig_header);
304 static int image_pre_load_sig_check_img_sig(struct image_sig_info *info, ulong addr)
306 struct sig_header_s *sig_header;
307 u32 header_size, image_size, offset_img_sig;
309 struct image_region reg;
314 sig_header = (struct sig_header_s *)map_sysmem(addr, SIG_HEADER_LEN);
316 log_err("ERROR: can't map first header\n");
321 header_size = fdt32_to_cpu(sig_header->header_size);
322 image_size = fdt32_to_cpu(sig_header->image_size);
323 offset_img_sig = fdt32_to_cpu(sig_header->offset_img_sig);
325 unmap_sysmem(sig_header);
327 image = (void *)map_sysmem(addr, header_size + image_size);
329 log_err("ERROR: can't map full image\n");
334 reg.data = image + header_size;
335 reg.size = image_size;
337 sig = (uint8_t *)image + offset_img_sig;
338 sig_len = info->sig_size;
340 ret = info->sig_info.crypto->verify(&info->sig_info, ®, 1, sig, sig_len);
342 log_err("ERROR: signature check has failed (err=%d)\n", ret);
344 goto out_unmap_image;
347 log_info("INFO: signature check has succeed\n");
356 int image_pre_load_sig(ulong addr)
358 struct image_sig_info info;
361 ret = image_pre_load_sig_setup(&info);
369 ret = image_pre_load_sig_check_magic(&info, addr);
377 /* Check the signature of the signature header */
378 ret = image_pre_load_sig_check_header_sig(&info, addr);
382 /* Check sha256 of the image signature */
383 ret = image_pre_load_sig_check_img_sig_sha256(&info, addr);
387 /* Check the image signature */
388 ret = image_pre_load_sig_check_img_sig(&info, addr);
392 ret = image_pre_load_sig_get_header_size(addr, &header_size);
394 log_err("%s: can't get header size\n", __func__);
399 image_load_offset += header_size;
406 int image_pre_load(ulong addr)
410 image_load_offset = 0;
412 if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD_SIG))
413 ret = image_pre_load_sig(addr);