blob: 52b63296f8918c59f181b1d49d2371fc2b46a88b [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{
Simon Glassc5819702021-02-15 17:08:09 -070029 if (fdt_check_header(ptr) != EXIT_SUCCESS ||
30 fit_check_format(ptr, IMAGE_SIZE_INVAL))
Jordan Handd32aa3c2019-03-05 14:47:56 -080031 return EXIT_FAILURE;
32
33 return EXIT_SUCCESS;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010034}
35
36int fit_check_image_types(uint8_t type)
37{
38 if (type == IH_TYPE_FLATDT)
39 return EXIT_SUCCESS;
40 else
41 return EXIT_FAILURE;
42}
43
Simon Glassa9468112014-06-02 22:04:53 -060044int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
Luca Boccassi7d574852019-05-14 19:35:02 +010045 void **blobp, struct stat *sbuf, bool delete_on_error,
46 bool read_only)
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010047{
48 void *ptr;
49 int fd;
50
51 /* Load FIT blob into memory (we need to write hashes/signatures) */
Luca Boccassi7d574852019-05-14 19:35:02 +010052 fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010053
54 if (fd < 0) {
55 fprintf(stderr, "%s: Can't open %s: %s\n",
56 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060057 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010058 }
59
60 if (fstat(fd, sbuf) < 0) {
61 fprintf(stderr, "%s: Can't stat %s: %s\n",
62 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060063 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010064 }
65
Simon Glassa9468112014-06-02 22:04:53 -060066 if (size_inc) {
67 sbuf->st_size += size_inc;
68 if (ftruncate(fd, sbuf->st_size)) {
69 fprintf(stderr, "%s: Can't expand %s: %s\n",
70 cmdname, fname, strerror(errno));
71 goto err;
72 }
73 }
74
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010075 errno = 0;
Luca Boccassi7d574852019-05-14 19:35:02 +010076 ptr = mmap(0, sbuf->st_size,
77 (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
78 fd, 0);
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010079 if ((ptr == MAP_FAILED) || (errno != 0)) {
80 fprintf(stderr, "%s: Can't read %s: %s\n",
81 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060082 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010083 }
84
85 /* check if ptr has a valid blob */
86 if (fdt_check_header(ptr)) {
87 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
Simon Glassef0af642014-06-02 22:04:52 -060088 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010089 }
90
Simon Glassa9468112014-06-02 22:04:53 -060091 /* expand if needed */
92 if (size_inc) {
93 int ret;
94
95 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
96 if (ret) {
97 fprintf(stderr, "%s: Cannot expand FDT: %s\n",
98 cmdname, fdt_strerror(ret));
99 goto err;
100 }
101 }
102
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100103 *blobp = ptr;
104 return fd;
Simon Glassef0af642014-06-02 22:04:52 -0600105
106err:
107 if (fd >= 0)
108 close(fd);
109 if (delete_on_error)
110 unlink(fname);
111
112 return -1;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100113}