3 * Linaro LTD, www.linaro.org
4 * Author: John Rigby <john.rigby@linaro.org>
5 * Based on TI's signGP.c
8 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
11 * Marvell Semiconductor <www.marvell.com>
12 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
14 * SPDX-License-Identifier: GPL-2.0+
17 #include "imagetool.h"
19 #include "omapimage.h"
21 /* Header size is CH header rounded up to 512 bytes plus GP header */
22 #define OMAP_CH_HDR_SIZE 512
23 #define OMAP_GP_HDR_SIZE (sizeof(struct gp_header))
24 #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE+OMAP_GP_HDR_SIZE)
26 static int do_swap32 = 0;
28 static uint32_t omapimage_swap32(uint32_t data)
31 result = (data & 0xFF000000) >> 24;
32 result |= (data & 0x00FF0000) >> 8;
33 result |= (data & 0x0000FF00) << 8;
34 result |= (data & 0x000000FF) << 24;
38 static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
40 static int omapimage_check_image_types(uint8_t type)
42 if (type == IH_TYPE_OMAPIMAGE)
50 * Only the simplest image type is currently supported:
51 * TOC pointing to CHSETTINGS
55 * padding to OMAP_CH_HDR_SIZE bytes
61 static int valid_gph_size(uint32_t size)
66 static int valid_gph_load_addr(uint32_t load_addr)
71 static int omapimage_verify_header(unsigned char *ptr, int image_size,
72 struct image_tool_params *params)
74 struct ch_toc *toc = (struct ch_toc *)ptr;
75 struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
76 uint32_t offset, size, gph_size, gph_load_addr;
78 while (toc->section_offset != 0xffffffff
79 && toc->section_size != 0xffffffff) {
81 offset = omapimage_swap32(toc->section_offset);
82 size = omapimage_swap32(toc->section_size);
84 offset = toc->section_offset;
85 size = toc->section_size;
89 if (offset >= OMAP_CH_HDR_SIZE ||
90 offset+size >= OMAP_CH_HDR_SIZE)
96 gph_size = omapimage_swap32(gph->size);
97 gph_load_addr = omapimage_swap32(gph->load_addr);
100 gph_load_addr = gph->load_addr;
103 if (!valid_gph_size(gph_size))
105 if (!valid_gph_load_addr(gph_load_addr))
111 static void omapimage_print_section(struct ch_settings *chs)
113 const char *section_name;
115 if (chs->section_key)
116 section_name = "CHSETTINGS";
118 section_name = "UNKNOWNKEY";
133 static void omapimage_print_header(const void *ptr)
135 const struct ch_toc *toc = (struct ch_toc *)ptr;
136 const struct gp_header *gph =
137 (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
138 uint32_t offset, size, gph_size, gph_load_addr;
140 while (toc->section_offset != 0xffffffff
141 && toc->section_size != 0xffffffff) {
143 offset = omapimage_swap32(toc->section_offset);
144 size = omapimage_swap32(toc->section_size);
146 offset = toc->section_offset;
147 size = toc->section_size;
150 if (offset >= OMAP_CH_HDR_SIZE ||
151 offset+size >= OMAP_CH_HDR_SIZE)
154 printf("Section %s offset %x length %x\n",
159 omapimage_print_section((struct ch_settings *)(ptr+offset));
164 gph_size = omapimage_swap32(gph->size);
165 gph_load_addr = omapimage_swap32(gph->load_addr);
167 gph_size = gph->size;
168 gph_load_addr = gph->load_addr;
171 if (!valid_gph_size(gph_size)) {
172 fprintf(stderr, "Error: invalid image size %x\n", gph_size);
176 if (!valid_gph_load_addr(gph_load_addr)) {
177 fprintf(stderr, "Error: invalid image load address %x\n",
182 printf("GP Header: Size %x LoadAddr %x\n", gph_size, gph_load_addr);
185 static int toc_offset(void *hdr, void *member)
190 static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
191 struct image_tool_params *params)
193 struct ch_toc *toc = (struct ch_toc *)ptr;
194 struct ch_settings *chs = (struct ch_settings *)
195 (ptr + 2 * sizeof(*toc));
196 struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
198 toc->section_offset = toc_offset(ptr, chs);
199 toc->section_size = sizeof(struct ch_settings);
200 strcpy((char *)toc->section_name, "CHSETTINGS");
202 chs->section_key = KEY_CHSETTINGS;
209 memset(toc, 0xff, sizeof(*toc));
211 gph->size = sbuf->st_size - OMAP_FILE_HDR_SIZE;
212 gph->load_addr = params->addr;
214 if (strncmp(params->imagename, "byteswap", 8) == 0) {
217 uint32_t *data = (uint32_t *)ptr;
219 while (swapped <= (sbuf->st_size / sizeof(uint32_t))) {
220 *data = omapimage_swap32(*data);
227 int omapimage_check_params(struct image_tool_params *params)
229 return (params->dflag && (params->fflag || params->lflag)) ||
230 (params->fflag && (params->dflag || params->lflag)) ||
231 (params->lflag && (params->dflag || params->fflag));
235 * omapimage parameters
237 static struct image_type_params omapimage_params = {
238 .name = "TI OMAP CH/GP Boot Image support",
239 .header_size = OMAP_FILE_HDR_SIZE,
240 .hdr = (void *)&omapimage_header,
241 .check_image_type = omapimage_check_image_types,
242 .verify_header = omapimage_verify_header,
243 .print_header = omapimage_print_header,
244 .set_header = omapimage_set_header,
245 .check_params = omapimage_check_params,
248 void init_omap_image_type(void)
250 register_image_type(&omapimage_params);