xref: /openbmc/u-boot/tools/fit_common.c (revision cd6cc344)
1 /*
2  * (C) Copyright 2014
3  * DENX Software Engineering
4  * Heiko Schocher <hs@denx.de>
5  *
6  * (C) Copyright 2008 Semihalf
7  *
8  * (C) Copyright 2000-2004
9  * DENX Software Engineering
10  * Wolfgang Denk, wd@denx.de
11  *
12  * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
13  *		FIT image specific code abstracted from mkimage.c
14  *		some functions added to address abstraction
15  *
16  * All rights reserved.
17  *
18  * SPDX-License-Identifier:	GPL-2.0+
19  */
20 
21 #include "imagetool.h"
22 #include "mkimage.h"
23 #include "fit_common.h"
24 #include <image.h>
25 #include <u-boot/crc.h>
26 
27 int fit_verify_header(unsigned char *ptr, int image_size,
28 			struct image_tool_params *params)
29 {
30 	return fdt_check_header(ptr);
31 }
32 
33 int fit_check_image_types(uint8_t type)
34 {
35 	if (type == IH_TYPE_FLATDT)
36 		return EXIT_SUCCESS;
37 	else
38 		return EXIT_FAILURE;
39 }
40 
41 int mmap_fdt(char *cmdname, const char *fname, void **blobp,
42 		struct stat *sbuf, int useunlink)
43 {
44 	void *ptr;
45 	int fd;
46 
47 	/* Load FIT blob into memory (we need to write hashes/signatures) */
48 	fd = open(fname, O_RDWR | O_BINARY);
49 
50 	if (fd < 0) {
51 		fprintf(stderr, "%s: Can't open %s: %s\n",
52 			cmdname, fname, strerror(errno));
53 		if (useunlink)
54 			unlink(fname);
55 		return -1;
56 	}
57 
58 	if (fstat(fd, sbuf) < 0) {
59 		fprintf(stderr, "%s: Can't stat %s: %s\n",
60 			cmdname, fname, strerror(errno));
61 		if (useunlink)
62 			unlink(fname);
63 		return -1;
64 	}
65 
66 	errno = 0;
67 	ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
68 	if ((ptr == MAP_FAILED) || (errno != 0)) {
69 		fprintf(stderr, "%s: Can't read %s: %s\n",
70 			cmdname, fname, strerror(errno));
71 		if (useunlink)
72 			unlink(fname);
73 		return -1;
74 	}
75 
76 	/* check if ptr has a valid blob */
77 	if (fdt_check_header(ptr)) {
78 		fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
79 		if (useunlink)
80 			unlink(fname);
81 		return -1;
82 	}
83 
84 	*blobp = ptr;
85 	return fd;
86 }
87