]> git.sur5r.net Git - u-boot/blob - tools/image-host.c
Merge git://git.denx.de/u-boot-fsl-qoriq
[u-boot] / tools / image-host.c
1 /*
2  * Copyright (c) 2013, Google Inc.
3  *
4  * (C) Copyright 2008 Semihalf
5  *
6  * (C) Copyright 2000-2006
7  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include "mkimage.h"
13 #include <bootm.h>
14 #include <image.h>
15 #include <version.h>
16
17 /**
18  * fit_set_hash_value - set hash value in requested has node
19  * @fit: pointer to the FIT format image header
20  * @noffset: hash node offset
21  * @value: hash value to be set
22  * @value_len: hash value length
23  *
24  * fit_set_hash_value() attempts to set hash value in a node at offset
25  * given and returns operation status to the caller.
26  *
27  * returns
28  *     0, on success
29  *     -1, on failure
30  */
31 static int fit_set_hash_value(void *fit, int noffset, uint8_t *value,
32                                 int value_len)
33 {
34         int ret;
35
36         ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
37         if (ret) {
38                 printf("Can't set hash '%s' property for '%s' node(%s)\n",
39                        FIT_VALUE_PROP, fit_get_name(fit, noffset, NULL),
40                        fdt_strerror(ret));
41                 return ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;
42         }
43
44         return 0;
45 }
46
47 /**
48  * fit_image_process_hash - Process a single subnode of the images/ node
49  *
50  * Check each subnode and process accordingly. For hash nodes we generate
51  * a hash of the supplised data and store it in the node.
52  *
53  * @fit:        pointer to the FIT format image header
54  * @image_name: name of image being processes (used to display errors)
55  * @noffset:    subnode offset
56  * @data:       data to process
57  * @size:       size of data in bytes
58  * @return 0 if ok, -1 on error
59  */
60 static int fit_image_process_hash(void *fit, const char *image_name,
61                 int noffset, const void *data, size_t size)
62 {
63         uint8_t value[FIT_MAX_HASH_LEN];
64         const char *node_name;
65         int value_len;
66         char *algo;
67         int ret;
68
69         node_name = fit_get_name(fit, noffset, NULL);
70
71         if (fit_image_hash_get_algo(fit, noffset, &algo)) {
72                 printf("Can't get hash algo property for '%s' hash node in '%s' image node\n",
73                        node_name, image_name);
74                 return -ENOENT;
75         }
76
77         if (calculate_hash(data, size, algo, value, &value_len)) {
78                 printf("Unsupported hash algorithm (%s) for '%s' hash node in '%s' image node\n",
79                        algo, node_name, image_name);
80                 return -EPROTONOSUPPORT;
81         }
82
83         ret = fit_set_hash_value(fit, noffset, value, value_len);
84         if (ret) {
85                 printf("Can't set hash value for '%s' hash node in '%s' image node\n",
86                        node_name, image_name);
87                 return ret;
88         }
89
90         return 0;
91 }
92
93 /**
94  * fit_image_write_sig() - write the signature to a FIT
95  *
96  * This writes the signature and signer data to the FIT.
97  *
98  * @fit: pointer to the FIT format image header
99  * @noffset: hash node offset
100  * @value: signature value to be set
101  * @value_len: signature value length
102  * @comment: Text comment to write (NULL for none)
103  *
104  * returns
105  *     0, on success
106  *     -FDT_ERR_..., on failure
107  */
108 static int fit_image_write_sig(void *fit, int noffset, uint8_t *value,
109                 int value_len, const char *comment, const char *region_prop,
110                 int region_proplen)
111 {
112         int string_size;
113         int ret;
114
115         /*
116          * Get the current string size, before we update the FIT and add
117          * more
118          */
119         string_size = fdt_size_dt_strings(fit);
120
121         ret = fdt_setprop(fit, noffset, FIT_VALUE_PROP, value, value_len);
122         if (!ret) {
123                 ret = fdt_setprop_string(fit, noffset, "signer-name",
124                                          "mkimage");
125         }
126         if (!ret) {
127                 ret = fdt_setprop_string(fit, noffset, "signer-version",
128                                   PLAIN_VERSION);
129         }
130         if (comment && !ret)
131                 ret = fdt_setprop_string(fit, noffset, "comment", comment);
132         if (!ret)
133                 ret = fit_set_timestamp(fit, noffset, time(NULL));
134         if (region_prop && !ret) {
135                 uint32_t strdata[2];
136
137                 ret = fdt_setprop(fit, noffset, "hashed-nodes",
138                                    region_prop, region_proplen);
139                 strdata[0] = 0;
140                 strdata[1] = cpu_to_fdt32(string_size);
141                 if (!ret) {
142                         ret = fdt_setprop(fit, noffset, "hashed-strings",
143                                           strdata, sizeof(strdata));
144                 }
145         }
146
147         return ret;
148 }
149
150 static int fit_image_setup_sig(struct image_sign_info *info,
151                 const char *keydir, void *fit, const char *image_name,
152                 int noffset, const char *require_keys)
153 {
154         const char *node_name;
155         char *algo_name;
156
157         node_name = fit_get_name(fit, noffset, NULL);
158         if (fit_image_hash_get_algo(fit, noffset, &algo_name)) {
159                 printf("Can't get algo property for '%s' signature node in '%s' image node\n",
160                        node_name, image_name);
161                 return -1;
162         }
163
164         memset(info, '\0', sizeof(*info));
165         info->keydir = keydir;
166         info->keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
167         info->fit = fit;
168         info->node_offset = noffset;
169         info->algo = image_get_sig_algo(algo_name);
170         info->require_keys = require_keys;
171         if (!info->algo) {
172                 printf("Unsupported signature algorithm (%s) for '%s' signature node in '%s' image node\n",
173                        algo_name, node_name, image_name);
174                 return -1;
175         }
176
177         return 0;
178 }
179
180 /**
181  * fit_image_process_sig- Process a single subnode of the images/ node
182  *
183  * Check each subnode and process accordingly. For signature nodes we
184  * generate a signed hash of the supplised data and store it in the node.
185  *
186  * @keydir:     Directory containing keys to use for signing
187  * @keydest:    Destination FDT blob to write public keys into
188  * @fit:        pointer to the FIT format image header
189  * @image_name: name of image being processes (used to display errors)
190  * @noffset:    subnode offset
191  * @data:       data to process
192  * @size:       size of data in bytes
193  * @comment:    Comment to add to signature nodes
194  * @require_keys: Mark all keys as 'required'
195  * @return 0 if ok, -1 on error
196  */
197 static int fit_image_process_sig(const char *keydir, void *keydest,
198                 void *fit, const char *image_name,
199                 int noffset, const void *data, size_t size,
200                 const char *comment, int require_keys)
201 {
202         struct image_sign_info info;
203         struct image_region region;
204         const char *node_name;
205         uint8_t *value;
206         uint value_len;
207         int ret;
208
209         if (fit_image_setup_sig(&info, keydir, fit, image_name, noffset,
210                                 require_keys ? "image" : NULL))
211                 return -1;
212
213         node_name = fit_get_name(fit, noffset, NULL);
214         region.data = data;
215         region.size = size;
216         ret = info.algo->sign(&info, &region, 1, &value, &value_len);
217         if (ret) {
218                 printf("Failed to sign '%s' signature node in '%s' image node: %d\n",
219                        node_name, image_name, ret);
220
221                 /* We allow keys to be missing */
222                 if (ret == -ENOENT)
223                         return 0;
224                 return -1;
225         }
226
227         ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
228                         NULL, 0);
229         if (ret) {
230                 if (ret == -FDT_ERR_NOSPACE)
231                         return -ENOSPC;
232                 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
233                        node_name, image_name, fdt_strerror(ret));
234                 return -1;
235         }
236         free(value);
237
238         /* Get keyname again, as FDT has changed and invalidated our pointer */
239         info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
240
241         /* Write the public key into the supplied FDT file */
242         if (keydest && info.algo->add_verify_data(&info, keydest)) {
243                 printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
244                        node_name, image_name);
245                 return -1;
246         }
247
248         return 0;
249 }
250
251 /**
252  * fit_image_add_verification_data() - calculate/set verig. data for image node
253  *
254  * This adds hash and signature values for an component image node.
255  *
256  * All existing hash subnodes are checked, if algorithm property is set to
257  * one of the supported hash algorithms, hash value is computed and
258  * corresponding hash node property is set, for example:
259  *
260  * Input component image node structure:
261  *
262  * o image@1 (at image_noffset)
263  *   | - data = [binary data]
264  *   o hash@1
265  *     |- algo = "sha1"
266  *
267  * Output component image node structure:
268  *
269  * o image@1 (at image_noffset)
270  *   | - data = [binary data]
271  *   o hash@1
272  *     |- algo = "sha1"
273  *     |- value = sha1(data)
274  *
275  * For signature details, please see doc/uImage.FIT/signature.txt
276  *
277  * @keydir      Directory containing *.key and *.crt files (or NULL)
278  * @keydest     FDT Blob to write public keys into (NULL if none)
279  * @fit:        Pointer to the FIT format image header
280  * @image_noffset: Requested component image node
281  * @comment:    Comment to add to signature nodes
282  * @require_keys: Mark all keys as 'required'
283  * @return: 0 on success, <0 on failure
284  */
285 int fit_image_add_verification_data(const char *keydir, void *keydest,
286                 void *fit, int image_noffset, const char *comment,
287                 int require_keys)
288 {
289         const char *image_name;
290         const void *data;
291         size_t size;
292         int noffset;
293
294         /* Get image data and data length */
295         if (fit_image_get_data(fit, image_noffset, &data, &size)) {
296                 printf("Can't get image data/size\n");
297                 return -1;
298         }
299
300         image_name = fit_get_name(fit, image_noffset, NULL);
301
302         /* Process all hash subnodes of the component image node */
303         for (noffset = fdt_first_subnode(fit, image_noffset);
304              noffset >= 0;
305              noffset = fdt_next_subnode(fit, noffset)) {
306                 const char *node_name;
307                 int ret = 0;
308
309                 /*
310                  * Check subnode name, must be equal to "hash" or "signature".
311                  * Multiple hash nodes require unique unit node
312                  * names, e.g. hash@1, hash@2, signature@1, etc.
313                  */
314                 node_name = fit_get_name(fit, noffset, NULL);
315                 if (!strncmp(node_name, FIT_HASH_NODENAME,
316                              strlen(FIT_HASH_NODENAME))) {
317                         ret = fit_image_process_hash(fit, image_name, noffset,
318                                                 data, size);
319                 } else if (IMAGE_ENABLE_SIGN && keydir &&
320                            !strncmp(node_name, FIT_SIG_NODENAME,
321                                 strlen(FIT_SIG_NODENAME))) {
322                         ret = fit_image_process_sig(keydir, keydest,
323                                 fit, image_name, noffset, data, size,
324                                 comment, require_keys);
325                 }
326                 if (ret)
327                         return ret;
328         }
329
330         return 0;
331 }
332
333 struct strlist {
334         int count;
335         char **strings;
336 };
337
338 static void strlist_init(struct strlist *list)
339 {
340         memset(list, '\0', sizeof(*list));
341 }
342
343 static void strlist_free(struct strlist *list)
344 {
345         int i;
346
347         for (i = 0; i < list->count; i++)
348                 free(list->strings[i]);
349         free(list->strings);
350 }
351
352 static int strlist_add(struct strlist *list, const char *str)
353 {
354         char *dup;
355
356         dup = strdup(str);
357         list->strings = realloc(list->strings,
358                                 (list->count + 1) * sizeof(char *));
359         if (!list || !str)
360                 return -1;
361         list->strings[list->count++] = dup;
362
363         return 0;
364 }
365
366 static const char *fit_config_get_image_list(void *fit, int noffset,
367                 int *lenp, int *allow_missingp)
368 {
369         static const char default_list[] = FIT_KERNEL_PROP "\0"
370                         FIT_FDT_PROP;
371         const char *prop;
372
373         /* If there is an "image" property, use that */
374         prop = fdt_getprop(fit, noffset, "sign-images", lenp);
375         if (prop) {
376                 *allow_missingp = 0;
377                 return *lenp ? prop : NULL;
378         }
379
380         /* Default image list */
381         *allow_missingp = 1;
382         *lenp = sizeof(default_list);
383
384         return default_list;
385 }
386
387 static int fit_config_get_hash_list(void *fit, int conf_noffset,
388                                     int sig_offset, struct strlist *node_inc)
389 {
390         int allow_missing;
391         const char *prop, *iname, *end;
392         const char *conf_name, *sig_name;
393         char name[200], path[200];
394         int image_count;
395         int ret, len;
396
397         conf_name = fit_get_name(fit, conf_noffset, NULL);
398         sig_name = fit_get_name(fit, sig_offset, NULL);
399
400         /*
401          * Build a list of nodes we need to hash. We always need the root
402          * node and the configuration.
403          */
404         strlist_init(node_inc);
405         snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH, conf_name);
406         if (strlist_add(node_inc, "/") ||
407             strlist_add(node_inc, name))
408                 goto err_mem;
409
410         /* Get a list of images that we intend to sign */
411         prop = fit_config_get_image_list(fit, sig_offset, &len,
412                                         &allow_missing);
413         if (!prop)
414                 return 0;
415
416         /* Locate the images */
417         end = prop + len;
418         image_count = 0;
419         for (iname = prop; iname < end; iname += strlen(iname) + 1) {
420                 int noffset;
421                 int image_noffset;
422                 int hash_count;
423
424                 image_noffset = fit_conf_get_prop_node(fit, conf_noffset,
425                                                        iname);
426                 if (image_noffset < 0) {
427                         printf("Failed to find image '%s' in  configuration '%s/%s'\n",
428                                iname, conf_name, sig_name);
429                         if (allow_missing)
430                                 continue;
431
432                         return -ENOENT;
433                 }
434
435                 ret = fdt_get_path(fit, image_noffset, path, sizeof(path));
436                 if (ret < 0)
437                         goto err_path;
438                 if (strlist_add(node_inc, path))
439                         goto err_mem;
440
441                 snprintf(name, sizeof(name), "%s/%s", FIT_CONFS_PATH,
442                          conf_name);
443
444                 /* Add all this image's hashes */
445                 hash_count = 0;
446                 for (noffset = fdt_first_subnode(fit, image_noffset);
447                      noffset >= 0;
448                      noffset = fdt_next_subnode(fit, noffset)) {
449                         const char *name = fit_get_name(fit, noffset, NULL);
450
451                         if (strncmp(name, FIT_HASH_NODENAME,
452                                     strlen(FIT_HASH_NODENAME)))
453                                 continue;
454                         ret = fdt_get_path(fit, noffset, path, sizeof(path));
455                         if (ret < 0)
456                                 goto err_path;
457                         if (strlist_add(node_inc, path))
458                                 goto err_mem;
459                         hash_count++;
460                 }
461
462                 if (!hash_count) {
463                         printf("Failed to find any hash nodes in configuration '%s/%s' image '%s' - without these it is not possible to verify this image\n",
464                                conf_name, sig_name, iname);
465                         return -ENOMSG;
466                 }
467
468                 image_count++;
469         }
470
471         if (!image_count) {
472                 printf("Failed to find any images for configuration '%s/%s'\n",
473                        conf_name, sig_name);
474                 return -ENOMSG;
475         }
476
477         return 0;
478
479 err_mem:
480         printf("Out of memory processing configuration '%s/%s'\n", conf_name,
481                sig_name);
482         return -ENOMEM;
483
484 err_path:
485         printf("Failed to get path for image '%s' in configuration '%s/%s': %s\n",
486                iname, conf_name, sig_name, fdt_strerror(ret));
487         return -ENOENT;
488 }
489
490 static int fit_config_get_data(void *fit, int conf_noffset, int noffset,
491                 struct image_region **regionp, int *region_countp,
492                 char **region_propp, int *region_proplen)
493 {
494         char * const exc_prop[] = {"data"};
495         struct strlist node_inc;
496         struct image_region *region;
497         struct fdt_region fdt_regions[100];
498         const char *conf_name, *sig_name;
499         char path[200];
500         int count, i;
501         char *region_prop;
502         int ret, len;
503
504         conf_name = fit_get_name(fit, conf_noffset, NULL);
505         sig_name = fit_get_name(fit, conf_noffset, NULL);
506         debug("%s: conf='%s', sig='%s'\n", __func__, conf_name, sig_name);
507
508         /* Get a list of nodes we want to hash */
509         ret = fit_config_get_hash_list(fit, conf_noffset, noffset, &node_inc);
510         if (ret)
511                 return ret;
512
513         /* Get a list of regions to hash */
514         count = fdt_find_regions(fit, node_inc.strings, node_inc.count,
515                         exc_prop, ARRAY_SIZE(exc_prop),
516                         fdt_regions, ARRAY_SIZE(fdt_regions),
517                         path, sizeof(path), 1);
518         if (count < 0) {
519                 printf("Failed to hash configuration '%s/%s': %s\n", conf_name,
520                        sig_name, fdt_strerror(ret));
521                 return -EIO;
522         }
523         if (count == 0) {
524                 printf("No data to hash for configuration '%s/%s': %s\n",
525                        conf_name, sig_name, fdt_strerror(ret));
526                 return -EINVAL;
527         }
528
529         /* Build our list of data blocks */
530         region = fit_region_make_list(fit, fdt_regions, count, NULL);
531         if (!region) {
532                 printf("Out of memory hashing configuration '%s/%s'\n",
533                        conf_name, sig_name);
534                 return -ENOMEM;
535         }
536
537         /* Create a list of all hashed properties */
538         debug("Hash nodes:\n");
539         for (i = len = 0; i < node_inc.count; i++) {
540                 debug("   %s\n", node_inc.strings[i]);
541                 len += strlen(node_inc.strings[i]) + 1;
542         }
543         region_prop = malloc(len);
544         if (!region_prop) {
545                 printf("Out of memory setting up regions for configuration '%s/%s'\n",
546                        conf_name, sig_name);
547                 return -ENOMEM;
548         }
549         for (i = len = 0; i < node_inc.count;
550              len += strlen(node_inc.strings[i]) + 1, i++)
551                 strcpy(region_prop + len, node_inc.strings[i]);
552         strlist_free(&node_inc);
553
554         *region_countp = count;
555         *regionp = region;
556         *region_propp = region_prop;
557         *region_proplen = len;
558
559         return 0;
560 }
561
562 static int fit_config_process_sig(const char *keydir, void *keydest,
563                 void *fit, const char *conf_name, int conf_noffset,
564                 int noffset, const char *comment, int require_keys)
565 {
566         struct image_sign_info info;
567         const char *node_name;
568         struct image_region *region;
569         char *region_prop;
570         int region_proplen;
571         int region_count;
572         uint8_t *value;
573         uint value_len;
574         int ret;
575
576         node_name = fit_get_name(fit, noffset, NULL);
577         if (fit_config_get_data(fit, conf_noffset, noffset, &region,
578                                 &region_count, &region_prop, &region_proplen))
579                 return -1;
580
581         if (fit_image_setup_sig(&info, keydir, fit, conf_name, noffset,
582                                 require_keys ? "conf" : NULL))
583                 return -1;
584
585         ret = info.algo->sign(&info, region, region_count, &value, &value_len);
586         free(region);
587         if (ret) {
588                 printf("Failed to sign '%s' signature node in '%s' conf node\n",
589                        node_name, conf_name);
590
591                 /* We allow keys to be missing */
592                 if (ret == -ENOENT)
593                         return 0;
594                 return -1;
595         }
596
597         ret = fit_image_write_sig(fit, noffset, value, value_len, comment,
598                                 region_prop, region_proplen);
599         if (ret) {
600                 if (ret == -FDT_ERR_NOSPACE)
601                         return -ENOSPC;
602                 printf("Can't write signature for '%s' signature node in '%s' conf node: %s\n",
603                        node_name, conf_name, fdt_strerror(ret));
604                 return -1;
605         }
606         free(value);
607         free(region_prop);
608
609         /* Get keyname again, as FDT has changed and invalidated our pointer */
610         info.keyname = fdt_getprop(fit, noffset, "key-name-hint", NULL);
611
612         /* Write the public key into the supplied FDT file */
613         if (keydest) {
614                 ret = info.algo->add_verify_data(&info, keydest);
615                 if (ret == -ENOSPC)
616                         return -ENOSPC;
617                 if (ret) {
618                         printf("Failed to add verification data for '%s' signature node in '%s' image node\n",
619                                node_name, conf_name);
620                 }
621                 return ret;
622         }
623
624         return 0;
625 }
626
627 static int fit_config_add_verification_data(const char *keydir, void *keydest,
628                 void *fit, int conf_noffset, const char *comment,
629                 int require_keys)
630 {
631         const char *conf_name;
632         int noffset;
633
634         conf_name = fit_get_name(fit, conf_noffset, NULL);
635
636         /* Process all hash subnodes of the configuration node */
637         for (noffset = fdt_first_subnode(fit, conf_noffset);
638              noffset >= 0;
639              noffset = fdt_next_subnode(fit, noffset)) {
640                 const char *node_name;
641                 int ret = 0;
642
643                 node_name = fit_get_name(fit, noffset, NULL);
644                 if (!strncmp(node_name, FIT_SIG_NODENAME,
645                              strlen(FIT_SIG_NODENAME))) {
646                         ret = fit_config_process_sig(keydir, keydest,
647                                 fit, conf_name, conf_noffset, noffset, comment,
648                                 require_keys);
649                 }
650                 if (ret)
651                         return ret;
652         }
653
654         return 0;
655 }
656
657 int fit_add_verification_data(const char *keydir, void *keydest, void *fit,
658                               const char *comment, int require_keys)
659 {
660         int images_noffset, confs_noffset;
661         int noffset;
662         int ret;
663
664         /* Find images parent node offset */
665         images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
666         if (images_noffset < 0) {
667                 printf("Can't find images parent node '%s' (%s)\n",
668                        FIT_IMAGES_PATH, fdt_strerror(images_noffset));
669                 return images_noffset;
670         }
671
672         /* Process its subnodes, print out component images details */
673         for (noffset = fdt_first_subnode(fit, images_noffset);
674              noffset >= 0;
675              noffset = fdt_next_subnode(fit, noffset)) {
676                 /*
677                  * Direct child node of the images parent node,
678                  * i.e. component image node.
679                  */
680                 ret = fit_image_add_verification_data(keydir, keydest,
681                                 fit, noffset, comment, require_keys);
682                 if (ret)
683                         return ret;
684         }
685
686         /* If there are no keys, we can't sign configurations */
687         if (!IMAGE_ENABLE_SIGN || !keydir)
688                 return 0;
689
690         /* Find configurations parent node offset */
691         confs_noffset = fdt_path_offset(fit, FIT_CONFS_PATH);
692         if (confs_noffset < 0) {
693                 printf("Can't find images parent node '%s' (%s)\n",
694                        FIT_CONFS_PATH, fdt_strerror(confs_noffset));
695                 return -ENOENT;
696         }
697
698         /* Process its subnodes, print out component images details */
699         for (noffset = fdt_first_subnode(fit, confs_noffset);
700              noffset >= 0;
701              noffset = fdt_next_subnode(fit, noffset)) {
702                 ret = fit_config_add_verification_data(keydir, keydest,
703                                                        fit, noffset, comment,
704                                                        require_keys);
705                 if (ret)
706                         return ret;
707         }
708
709         return 0;
710 }
711
712 #ifdef CONFIG_FIT_SIGNATURE
713 int fit_check_sign(const void *fit, const void *key)
714 {
715         int cfg_noffset;
716         int ret;
717
718         cfg_noffset = fit_conf_get_node(fit, NULL);
719         if (!cfg_noffset)
720                 return -1;
721
722         printf("Verifying Hash Integrity ... ");
723         ret = fit_config_verify(fit, cfg_noffset);
724         if (ret)
725                 return ret;
726         ret = bootm_host_load_images(fit, cfg_noffset);
727
728         return ret;
729 }
730 #endif