xref: /openbmc/u-boot/tools/fit_common.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
26bf4ca07SHeiko Schocher /*
36bf4ca07SHeiko Schocher  * (C) Copyright 2014
46bf4ca07SHeiko Schocher  * DENX Software Engineering
56bf4ca07SHeiko Schocher  * Heiko Schocher <hs@denx.de>
66bf4ca07SHeiko Schocher  *
76bf4ca07SHeiko Schocher  * (C) Copyright 2008 Semihalf
86bf4ca07SHeiko Schocher  *
96bf4ca07SHeiko Schocher  * (C) Copyright 2000-2004
106bf4ca07SHeiko Schocher  * DENX Software Engineering
116bf4ca07SHeiko Schocher  * Wolfgang Denk, wd@denx.de
126bf4ca07SHeiko Schocher  *
136bf4ca07SHeiko Schocher  * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
146bf4ca07SHeiko Schocher  *		FIT image specific code abstracted from mkimage.c
156bf4ca07SHeiko Schocher  *		some functions added to address abstraction
166bf4ca07SHeiko Schocher  *
176bf4ca07SHeiko Schocher  * All rights reserved.
186bf4ca07SHeiko Schocher  */
196bf4ca07SHeiko Schocher 
206bf4ca07SHeiko Schocher #include "imagetool.h"
216bf4ca07SHeiko Schocher #include "mkimage.h"
226bf4ca07SHeiko Schocher #include "fit_common.h"
236bf4ca07SHeiko Schocher #include <image.h>
246bf4ca07SHeiko Schocher #include <u-boot/crc.h>
256bf4ca07SHeiko Schocher 
266bf4ca07SHeiko Schocher int fit_verify_header(unsigned char *ptr, int image_size,
276bf4ca07SHeiko Schocher 			struct image_tool_params *params)
286bf4ca07SHeiko Schocher {
296bf4ca07SHeiko Schocher 	return fdt_check_header(ptr);
306bf4ca07SHeiko Schocher }
316bf4ca07SHeiko Schocher 
326bf4ca07SHeiko Schocher int fit_check_image_types(uint8_t type)
336bf4ca07SHeiko Schocher {
346bf4ca07SHeiko Schocher 	if (type == IH_TYPE_FLATDT)
356bf4ca07SHeiko Schocher 		return EXIT_SUCCESS;
366bf4ca07SHeiko Schocher 	else
376bf4ca07SHeiko Schocher 		return EXIT_FAILURE;
386bf4ca07SHeiko Schocher }
396bf4ca07SHeiko Schocher 
40a9468115SSimon Glass int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
41a9468115SSimon Glass 	     void **blobp, struct stat *sbuf, bool delete_on_error)
426bf4ca07SHeiko Schocher {
436bf4ca07SHeiko Schocher 	void *ptr;
446bf4ca07SHeiko Schocher 	int fd;
456bf4ca07SHeiko Schocher 
466bf4ca07SHeiko Schocher 	/* Load FIT blob into memory (we need to write hashes/signatures) */
476bf4ca07SHeiko Schocher 	fd = open(fname, O_RDWR | O_BINARY);
486bf4ca07SHeiko Schocher 
496bf4ca07SHeiko Schocher 	if (fd < 0) {
506bf4ca07SHeiko Schocher 		fprintf(stderr, "%s: Can't open %s: %s\n",
516bf4ca07SHeiko Schocher 			cmdname, fname, strerror(errno));
52ef0af64bSSimon Glass 		goto err;
536bf4ca07SHeiko Schocher 	}
546bf4ca07SHeiko Schocher 
556bf4ca07SHeiko Schocher 	if (fstat(fd, sbuf) < 0) {
566bf4ca07SHeiko Schocher 		fprintf(stderr, "%s: Can't stat %s: %s\n",
576bf4ca07SHeiko Schocher 			cmdname, fname, strerror(errno));
58ef0af64bSSimon Glass 		goto err;
596bf4ca07SHeiko Schocher 	}
606bf4ca07SHeiko Schocher 
61a9468115SSimon Glass 	if (size_inc) {
62a9468115SSimon Glass 		sbuf->st_size += size_inc;
63a9468115SSimon Glass 		if (ftruncate(fd, sbuf->st_size)) {
64a9468115SSimon Glass 			fprintf(stderr, "%s: Can't expand %s: %s\n",
65a9468115SSimon Glass 				cmdname, fname, strerror(errno));
66a9468115SSimon Glass 		goto err;
67a9468115SSimon Glass 		}
68a9468115SSimon Glass 	}
69a9468115SSimon Glass 
706bf4ca07SHeiko Schocher 	errno = 0;
716bf4ca07SHeiko Schocher 	ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
726bf4ca07SHeiko Schocher 	if ((ptr == MAP_FAILED) || (errno != 0)) {
736bf4ca07SHeiko Schocher 		fprintf(stderr, "%s: Can't read %s: %s\n",
746bf4ca07SHeiko Schocher 			cmdname, fname, strerror(errno));
75ef0af64bSSimon Glass 		goto err;
766bf4ca07SHeiko Schocher 	}
776bf4ca07SHeiko Schocher 
786bf4ca07SHeiko Schocher 	/* check if ptr has a valid blob */
796bf4ca07SHeiko Schocher 	if (fdt_check_header(ptr)) {
806bf4ca07SHeiko Schocher 		fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
81ef0af64bSSimon Glass 		goto err;
826bf4ca07SHeiko Schocher 	}
836bf4ca07SHeiko Schocher 
84a9468115SSimon Glass 	/* expand if needed */
85a9468115SSimon Glass 	if (size_inc) {
86a9468115SSimon Glass 		int ret;
87a9468115SSimon Glass 
88a9468115SSimon Glass 		ret = fdt_open_into(ptr, ptr, sbuf->st_size);
89a9468115SSimon Glass 		if (ret) {
90a9468115SSimon Glass 			fprintf(stderr, "%s: Cannot expand FDT: %s\n",
91a9468115SSimon Glass 				cmdname, fdt_strerror(ret));
92a9468115SSimon Glass 			goto err;
93a9468115SSimon Glass 		}
94a9468115SSimon Glass 	}
95a9468115SSimon Glass 
966bf4ca07SHeiko Schocher 	*blobp = ptr;
976bf4ca07SHeiko Schocher 	return fd;
98ef0af64bSSimon Glass 
99ef0af64bSSimon Glass err:
100ef0af64bSSimon Glass 	if (fd >= 0)
101ef0af64bSSimon Glass 		close(fd);
102ef0af64bSSimon Glass 	if (delete_on_error)
103ef0af64bSSimon Glass 		unlink(fname);
104ef0af64bSSimon Glass 
105ef0af64bSSimon Glass 	return -1;
1066bf4ca07SHeiko Schocher }
107