blob: cdf987d3c1389004ccf98135cd1d88aa54e174ff [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher6bf4ca02014-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 Schocher6bf4ca02014-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 Handd32aa3c2019-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 Schocher6bf4ca02014-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 Glassa9468112014-06-02 22:04:53 -060043int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
Luca Boccassi7d574852019-05-14 19:35:02 +010044 void **blobp, struct stat *sbuf, bool delete_on_error,
45 bool read_only)
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010046{
47 void *ptr;
48 int fd;
49
50 /* Load FIT blob into memory (we need to write hashes/signatures) */
Luca Boccassi7d574852019-05-14 19:35:02 +010051 fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010052
53 if (fd < 0) {
54 fprintf(stderr, "%s: Can't open %s: %s\n",
55 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060056 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010057 }
58
59 if (fstat(fd, sbuf) < 0) {
60 fprintf(stderr, "%s: Can't stat %s: %s\n",
61 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060062 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010063 }
64
Simon Glassa9468112014-06-02 22:04:53 -060065 if (size_inc) {
66 sbuf->st_size += size_inc;
67 if (ftruncate(fd, sbuf->st_size)) {
68 fprintf(stderr, "%s: Can't expand %s: %s\n",
69 cmdname, fname, strerror(errno));
70 goto err;
71 }
72 }
73
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010074 errno = 0;
Luca Boccassi7d574852019-05-14 19:35:02 +010075 ptr = mmap(0, sbuf->st_size,
76 (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
77 fd, 0);
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010078 if ((ptr == MAP_FAILED) || (errno != 0)) {
79 fprintf(stderr, "%s: Can't read %s: %s\n",
80 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060081 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010082 }
83
84 /* check if ptr has a valid blob */
85 if (fdt_check_header(ptr)) {
86 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
Simon Glassef0af642014-06-02 22:04:52 -060087 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010088 }
89
Simon Glassa9468112014-06-02 22:04:53 -060090 /* expand if needed */
91 if (size_inc) {
92 int ret;
93
94 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
95 if (ret) {
96 fprintf(stderr, "%s: Cannot expand FDT: %s\n",
97 cmdname, fdt_strerror(ret));
98 goto err;
99 }
100 }
101
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100102 *blobp = ptr;
103 return fd;
Simon Glassef0af642014-06-02 22:04:52 -0600104
105err:
106 if (fd >= 0)
107 close(fd);
108 if (delete_on_error)
109 unlink(fname);
110
111 return -1;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100112}