blob: 2d417d47198888597bf27db5f2be4b79ff366e5e [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
Pali Rohár2972d7d2023-03-29 21:25:54 +020026void fit_print_header(const void *fit, struct image_tool_params *params)
27{
28 fit_print_contents(fit);
29}
30
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010031int fit_verify_header(unsigned char *ptr, int image_size,
32 struct image_tool_params *params)
33{
Stefan Eichenberger6f08eee2022-01-10 18:48:32 +010034 int ret;
35
36 if (fdt_check_header(ptr) != EXIT_SUCCESS)
Jordan Handd32aa3c2019-03-05 14:47:56 -080037 return EXIT_FAILURE;
38
Stefan Eichenberger6f08eee2022-01-10 18:48:32 +010039 ret = fit_check_format(ptr, IMAGE_SIZE_INVAL);
40 if (ret) {
41 if (ret != -EADDRNOTAVAIL)
42 return EXIT_FAILURE;
43 fprintf(stderr, "Image contains unit addresses @, this will break signing\n");
44 }
45
Jordan Handd32aa3c2019-03-05 14:47:56 -080046 return EXIT_SUCCESS;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010047}
48
49int fit_check_image_types(uint8_t type)
50{
51 if (type == IH_TYPE_FLATDT)
52 return EXIT_SUCCESS;
53 else
54 return EXIT_FAILURE;
55}
56
Simon Glassa9468112014-06-02 22:04:53 -060057int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
Luca Boccassi7d574852019-05-14 19:35:02 +010058 void **blobp, struct stat *sbuf, bool delete_on_error,
59 bool read_only)
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010060{
61 void *ptr;
62 int fd;
63
64 /* Load FIT blob into memory (we need to write hashes/signatures) */
Luca Boccassi7d574852019-05-14 19:35:02 +010065 fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010066
67 if (fd < 0) {
68 fprintf(stderr, "%s: Can't open %s: %s\n",
69 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060070 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010071 }
72
73 if (fstat(fd, sbuf) < 0) {
74 fprintf(stderr, "%s: Can't stat %s: %s\n",
75 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060076 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010077 }
78
Simon Glassa9468112014-06-02 22:04:53 -060079 if (size_inc) {
80 sbuf->st_size += size_inc;
81 if (ftruncate(fd, sbuf->st_size)) {
82 fprintf(stderr, "%s: Can't expand %s: %s\n",
83 cmdname, fname, strerror(errno));
84 goto err;
85 }
86 }
87
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010088 errno = 0;
Luca Boccassi7d574852019-05-14 19:35:02 +010089 ptr = mmap(0, sbuf->st_size,
90 (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
91 fd, 0);
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010092 if ((ptr == MAP_FAILED) || (errno != 0)) {
93 fprintf(stderr, "%s: Can't read %s: %s\n",
94 cmdname, fname, strerror(errno));
Simon Glassef0af642014-06-02 22:04:52 -060095 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +010096 }
97
98 /* check if ptr has a valid blob */
99 if (fdt_check_header(ptr)) {
100 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
Simon Glassef0af642014-06-02 22:04:52 -0600101 goto err;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100102 }
103
Simon Glassa9468112014-06-02 22:04:53 -0600104 /* expand if needed */
105 if (size_inc) {
106 int ret;
107
108 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
109 if (ret) {
110 fprintf(stderr, "%s: Cannot expand FDT: %s\n",
111 cmdname, fdt_strerror(ret));
112 goto err;
113 }
114 }
115
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100116 *blobp = ptr;
117 return fd;
Simon Glassef0af642014-06-02 22:04:52 -0600118
119err:
120 if (fd >= 0)
121 close(fd);
122 if (delete_on_error)
123 unlink(fname);
124
125 return -1;
Heiko Schocher6bf4ca02014-03-03 12:19:29 +0100126}
Simon Glasse291a5c2021-11-12 12:28:04 -0700127
128int copyfile(const char *src, const char *dst)
129{
130 int fd_src = -1, fd_dst = -1;
131 void *buf = NULL;
132 ssize_t size;
133 size_t count;
134 int ret = -1;
135
136 fd_src = open(src, O_RDONLY);
137 if (fd_src < 0) {
138 printf("Can't open file %s (%s)\n", src, strerror(errno));
139 goto out;
140 }
141
Simon Glass7ae46c32021-11-12 12:28:05 -0700142 fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Simon Glasse291a5c2021-11-12 12:28:04 -0700143 if (fd_dst < 0) {
144 printf("Can't open file %s (%s)\n", dst, strerror(errno));
145 goto out;
146 }
147
148 buf = calloc(1, 512);
149 if (!buf) {
150 printf("Can't allocate buffer to copy file\n");
151 goto out;
152 }
153
154 while (1) {
155 size = read(fd_src, buf, 512);
156 if (size < 0) {
157 printf("Can't read file %s\n", src);
158 goto out;
159 }
160 if (!size)
161 break;
162
163 count = size;
164 size = write(fd_dst, buf, count);
165 if (size < 0) {
166 printf("Can't write file %s\n", dst);
167 goto out;
168 }
169 }
170
171 ret = 0;
172
173 out:
174 if (fd_src >= 0)
175 close(fd_src);
176 if (fd_dst >= 0)
177 close(fd_dst);
178 if (buf)
179 free(buf);
180
181 return ret;
182}
Simon Glass2d2384b2021-11-12 12:28:13 -0700183
184void summary_show(struct image_summary *summary, const char *imagefile,
185 const char *keydest)
186{
187 if (summary->sig_offset) {
188 printf("Signature written to '%s', node '%s'\n", imagefile,
189 summary->sig_path);
190 if (keydest) {
191 printf("Public key written to '%s', node '%s'\n",
192 keydest, summary->keydest_path);
193 }
194 }
195}