blob: 9506390214ce028bbe0045d88161538ec652da2d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher3cb743c2014-03-03 12:19:29 +01002/*
3 * (C) Copyright 2014
4 * DENX Software Engineering
5 * Heiko Schocher <hs@denx.de>
6 *
7 * (C) Copyright 2008 Semihalf
8 *
9 * (C) Copyright 2000-2004
10 * DENX Software Engineering
11 * Wolfgang Denk, wd@denx.de
12 *
13 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
14 * FIT image specific code abstracted from mkimage.c
15 * some functions added to address abstraction
16 *
17 * All rights reserved.
Heiko Schocher3cb743c2014-03-03 12:19:29 +010018 */
19
20#include "imagetool.h"
21#include "mkimage.h"
22#include "fit_common.h"
23#include <image.h>
24#include <u-boot/crc.h>
25
26int fit_verify_header(unsigned char *ptr, int image_size,
27 struct image_tool_params *params)
28{
Jordan Hand256b7f92019-03-05 14:47:56 -080029 if (fdt_check_header(ptr) != EXIT_SUCCESS || !fit_check_format(ptr))
30 return EXIT_FAILURE;
31
32 return EXIT_SUCCESS;
Heiko Schocher3cb743c2014-03-03 12:19:29 +010033}
34
35int fit_check_image_types(uint8_t type)
36{
37 if (type == IH_TYPE_FLATDT)
38 return EXIT_SUCCESS;
39 else
40 return EXIT_FAILURE;
41}
42
Simon Glass802aa822014-06-02 22:04:53 -060043int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
44 void **blobp, struct stat *sbuf, bool delete_on_error)
Heiko Schocher3cb743c2014-03-03 12:19:29 +010045{
46 void *ptr;
47 int fd;
48
49 /* Load FIT blob into memory (we need to write hashes/signatures) */
50 fd = open(fname, O_RDWR | O_BINARY);
51
52 if (fd < 0) {
53 fprintf(stderr, "%s: Can't open %s: %s\n",
54 cmdname, fname, strerror(errno));
Simon Glassd25c0d92014-06-02 22:04:52 -060055 goto err;
Heiko Schocher3cb743c2014-03-03 12:19:29 +010056 }
57
58 if (fstat(fd, sbuf) < 0) {
59 fprintf(stderr, "%s: Can't stat %s: %s\n",
60 cmdname, fname, strerror(errno));
Simon Glassd25c0d92014-06-02 22:04:52 -060061 goto err;
Heiko Schocher3cb743c2014-03-03 12:19:29 +010062 }
63
Simon Glass802aa822014-06-02 22:04:53 -060064 if (size_inc) {
65 sbuf->st_size += size_inc;
66 if (ftruncate(fd, sbuf->st_size)) {
67 fprintf(stderr, "%s: Can't expand %s: %s\n",
68 cmdname, fname, strerror(errno));
69 goto err;
70 }
71 }
72
Heiko Schocher3cb743c2014-03-03 12:19:29 +010073 errno = 0;
74 ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
75 if ((ptr == MAP_FAILED) || (errno != 0)) {
76 fprintf(stderr, "%s: Can't read %s: %s\n",
77 cmdname, fname, strerror(errno));
Simon Glassd25c0d92014-06-02 22:04:52 -060078 goto err;
Heiko Schocher3cb743c2014-03-03 12:19:29 +010079 }
80
81 /* check if ptr has a valid blob */
82 if (fdt_check_header(ptr)) {
83 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
Simon Glassd25c0d92014-06-02 22:04:52 -060084 goto err;
Heiko Schocher3cb743c2014-03-03 12:19:29 +010085 }
86
Simon Glass802aa822014-06-02 22:04:53 -060087 /* expand if needed */
88 if (size_inc) {
89 int ret;
90
91 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
92 if (ret) {
93 fprintf(stderr, "%s: Cannot expand FDT: %s\n",
94 cmdname, fdt_strerror(ret));
95 goto err;
96 }
97 }
98
Heiko Schocher3cb743c2014-03-03 12:19:29 +010099 *blobp = ptr;
100 return fd;
Simon Glassd25c0d92014-06-02 22:04:52 -0600101
102err:
103 if (fd >= 0)
104 close(fd);
105 if (delete_on_error)
106 unlink(fname);
107
108 return -1;
Heiko Schocher3cb743c2014-03-03 12:19:29 +0100109}