1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2008 Semihalf
4 *
5 * (C) Copyright 2000-2004
6 * DENX Software Engineering
7 * Wolfgang Denk, wd@denx.de
8 *
9 * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
10 *		default_image specific code abstracted from mkimage.c
11 *		some functions added to address abstraction
12 *
13 * All rights reserved.
14 */
15
16#include "imagetool.h"
17#include "mkimage.h"
18
19#include <image.h>
20#include <tee/optee.h>
21#include <u-boot/crc.h>
22#include <imximage.h>
23
24static struct legacy_img_hdr header;
25
26static int image_check_image_types(uint8_t type)
27{
28	if (((type > IH_TYPE_INVALID) && (type < IH_TYPE_FLATDT)) ||
29	    (type == IH_TYPE_KERNEL_NOLOAD) || (type == IH_TYPE_FIRMWARE_IVT) ||
30	    (type == IH_TYPE_FDT_LEGACY))
31		return EXIT_SUCCESS;
32	else
33		return EXIT_FAILURE;
34}
35
36static int image_check_params(struct image_tool_params *params)
37{
38	return	((params->dflag && (params->fflag || params->lflag)) ||
39		(params->fflag && (params->dflag || params->lflag)) ||
40		(params->lflag && (params->dflag || params->fflag)));
41}
42
43static void image_print_header(const void *ptr, struct image_tool_params *params)
44{
45	image_print_contents(ptr);
46}
47
48static int image_verify_header(unsigned char *ptr, int image_size,
49			struct image_tool_params *params)
50{
51	uint32_t len;
52	const unsigned char *data;
53	uint32_t checksum;
54	struct legacy_img_hdr header;
55	struct legacy_img_hdr *hdr = &header;
56
57	if (image_size < sizeof(struct legacy_img_hdr)) {
58		debug("%s: Bad image size: \"%s\" is no valid image\n",
59		      params->cmdname, params->imagefile);
60		return -FDT_ERR_BADSTRUCTURE;
61	}
62
63	/*
64	 * create copy of header so that we can blank out the
65	 * checksum field for checking - this can't be done
66	 * on the PROT_READ mapped data.
67	 */
68	memcpy(hdr, ptr, sizeof(struct legacy_img_hdr));
69
70	if (be32_to_cpu(hdr->ih_magic) != IH_MAGIC) {
71		debug("%s: Bad Magic Number: \"%s\" is no valid image\n",
72		      params->cmdname, params->imagefile);
73		return -FDT_ERR_BADMAGIC;
74	}
75
76	data = (const unsigned char *)hdr;
77	len  = sizeof(struct legacy_img_hdr);
78
79	checksum = be32_to_cpu(hdr->ih_hcrc);
80	hdr->ih_hcrc = cpu_to_be32(0);	/* clear for re-calculation */
81
82	if (crc32(0, data, len) != checksum) {
83		debug("%s: ERROR: \"%s\" has bad header checksum!\n",
84		      params->cmdname, params->imagefile);
85		return -FDT_ERR_BADSTATE;
86	}
87
88	data = (const unsigned char *)ptr + sizeof(struct legacy_img_hdr);
89	len = image_get_data_size(hdr);
90
91	if (image_get_type(hdr) == IH_TYPE_FIRMWARE_IVT)
92		/* Add size of CSF minus IVT */
93		len -= 0x2060 - sizeof(flash_header_v2_t);
94
95	if (image_size - sizeof(struct legacy_img_hdr) < len) {
96		debug("%s: Bad image size: \"%s\" is no valid image\n",
97		      params->cmdname, params->imagefile);
98		return -FDT_ERR_BADSTRUCTURE;
99	}
100
101	checksum = be32_to_cpu(hdr->ih_dcrc);
102	if (crc32(0, data, len) != checksum) {
103		debug("%s: ERROR: \"%s\" has corrupted data!\n",
104		      params->cmdname, params->imagefile);
105		return -FDT_ERR_BADSTRUCTURE;
106	}
107	return 0;
108}
109
110static void image_set_header(void *ptr, struct stat *sbuf, int ifd,
111				struct image_tool_params *params)
112{
113	uint32_t checksum;
114	time_t time;
115	uint32_t imagesize;
116	uint32_t ep;
117	uint32_t addr;
118	int type;
119	struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)ptr;
120
121	checksum = crc32(0,
122			(const unsigned char *)(ptr +
123				sizeof(struct legacy_img_hdr)),
124			sbuf->st_size - sizeof(struct legacy_img_hdr));
125
126	time = imagetool_get_source_date(params->cmdname, sbuf->st_mtime);
127	ep = params->ep;
128	addr = params->addr;
129
130	if (params->type == IH_TYPE_FIRMWARE_IVT)
131		/* Add size of CSF minus IVT */
132		imagesize = sbuf->st_size - sizeof(struct legacy_img_hdr)
133			    + 0x2060 - sizeof(flash_header_v2_t);
134
135	else
136		imagesize = sbuf->st_size - sizeof(struct legacy_img_hdr);
137
138	if (params->type == IH_TYPE_FDT_LEGACY)
139		type = IH_TYPE_FLATDT;
140	else
141		type = params->type;
142
143	if (params->os == IH_OS_TEE) {
144		addr = optee_image_get_load_addr(hdr);
145		ep = optee_image_get_entry_point(hdr);
146	}
147
148	/* Build new header */
149	image_set_magic(hdr, IH_MAGIC);
150	image_set_time(hdr, time);
151	image_set_size(hdr, imagesize);
152	image_set_load(hdr, addr);
153	image_set_ep(hdr, ep);
154	image_set_dcrc(hdr, checksum);
155	image_set_os(hdr, params->os);
156	image_set_arch(hdr, params->arch);
157	image_set_type(hdr, type);
158	image_set_comp(hdr, params->comp);
159
160	image_set_name(hdr, params->imagename);
161
162	checksum = crc32(0, (const unsigned char *)hdr,
163				sizeof(struct legacy_img_hdr));
164
165	image_set_hcrc(hdr, checksum);
166}
167
168static int image_extract_subimage(void *ptr, struct image_tool_params *params)
169{
170	const struct legacy_img_hdr *hdr = (const struct legacy_img_hdr *)ptr;
171	ulong file_data;
172	ulong file_len;
173
174	if (image_check_type(hdr, IH_TYPE_MULTI)) {
175		ulong idx = params->pflag;
176		ulong count;
177
178		/* get the number of data files present in the image */
179		count = image_multi_count(hdr);
180
181		/* retrieve the "data file" at the idx position */
182		image_multi_getimg(hdr, idx, &file_data, &file_len);
183
184		if ((file_len == 0) || (idx >= count)) {
185			fprintf(stderr, "%s: No such data file %ld in \"%s\"\n",
186				params->cmdname, idx, params->imagefile);
187			return -1;
188		}
189	} else {
190		file_data = image_get_data(hdr);
191		file_len = image_get_size(hdr);
192	}
193
194	/* save the "data file" into the file system */
195	return imagetool_save_subimage(params->outfile, file_data, file_len);
196}
197
198/*
199 * Default image type parameters definition
200 */
201U_BOOT_IMAGE_TYPE(
202	defimage,
203	"Default Image support",
204	sizeof(struct legacy_img_hdr),
205	(void *)&header,
206	image_check_params,
207	image_verify_header,
208	image_print_header,
209	image_set_header,
210	image_extract_subimage,
211	image_check_image_types,
212	NULL,
213	NULL
214);
215