blob: b79c1c3b648bdebaf33bfcf8bad2d36a1be72a26 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
John Rigby34d12a12011-07-21 09:10:30 -04002/*
3 * (C) Copyright 2010
4 * Linaro LTD, www.linaro.org
5 * Author: John Rigby <john.rigby@linaro.org>
6 * Based on TI's signGP.c
7 *
8 * (C) Copyright 2009
9 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
10 *
11 * (C) Copyright 2008
12 * Marvell Semiconductor <www.marvell.com>
13 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
John Rigby34d12a12011-07-21 09:10:30 -040014 */
15
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070016#include "imagetool.h"
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040017#include <compiler.h>
John Rigby34d12a12011-07-21 09:10:30 -040018#include <image.h>
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040019#include "gpheader.h"
John Rigby34d12a12011-07-21 09:10:30 -040020#include "omapimage.h"
21
Philipp Tomsich070aa2d2017-12-04 17:04:02 +010022#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
23
John Rigby34d12a12011-07-21 09:10:30 -040024/* Header size is CH header rounded up to 512 bytes plus GP header */
25#define OMAP_CH_HDR_SIZE 512
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040026#define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE)
John Rigby34d12a12011-07-21 09:10:30 -040027
Tom Rinid505e902012-10-16 13:06:06 +000028static int do_swap32 = 0;
29
John Rigby34d12a12011-07-21 09:10:30 -040030static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
31
32static int omapimage_check_image_types(uint8_t type)
33{
34 if (type == IH_TYPE_OMAPIMAGE)
35 return EXIT_SUCCESS;
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040036 return EXIT_FAILURE;
John Rigby34d12a12011-07-21 09:10:30 -040037}
38
39static int omapimage_verify_header(unsigned char *ptr, int image_size,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070040 struct image_tool_params *params)
John Rigby34d12a12011-07-21 09:10:30 -040041{
42 struct ch_toc *toc = (struct ch_toc *)ptr;
43 struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040044 uint32_t offset, size;
John Rigby34d12a12011-07-21 09:10:30 -040045
46 while (toc->section_offset != 0xffffffff
47 && toc->section_size != 0xffffffff) {
Tom Rinid505e902012-10-16 13:06:06 +000048 if (do_swap32) {
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040049 offset = cpu_to_be32(toc->section_offset);
50 size = cpu_to_be32(toc->section_size);
Tom Rinid505e902012-10-16 13:06:06 +000051 } else {
52 offset = toc->section_offset;
53 size = toc->section_size;
54 }
John Rigby34d12a12011-07-21 09:10:30 -040055 if (!offset || !size)
56 return -1;
57 if (offset >= OMAP_CH_HDR_SIZE ||
58 offset+size >= OMAP_CH_HDR_SIZE)
59 return -1;
60 toc++;
61 }
Tom Rinid505e902012-10-16 13:06:06 +000062
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040063 return gph_verify_header(gph, do_swap32);
John Rigby34d12a12011-07-21 09:10:30 -040064}
65
66static void omapimage_print_section(struct ch_settings *chs)
67{
68 const char *section_name;
69
70 if (chs->section_key)
71 section_name = "CHSETTINGS";
72 else
73 section_name = "UNKNOWNKEY";
74
75 printf("%s (%x) "
76 "valid:%x "
77 "version:%x "
78 "reserved:%x "
79 "flags:%x\n",
80 section_name,
81 chs->section_key,
82 chs->valid,
83 chs->version,
84 chs->reserved,
85 chs->flags);
86}
87
Pali Rohár0ed41e22023-03-29 21:25:54 +020088static void omapimage_print_header(const void *ptr, struct image_tool_params *params)
John Rigby34d12a12011-07-21 09:10:30 -040089{
90 const struct ch_toc *toc = (struct ch_toc *)ptr;
91 const struct gp_header *gph =
92 (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040093 uint32_t offset, size;
John Rigby34d12a12011-07-21 09:10:30 -040094
95 while (toc->section_offset != 0xffffffff
96 && toc->section_size != 0xffffffff) {
Tom Rinid505e902012-10-16 13:06:06 +000097 if (do_swap32) {
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040098 offset = cpu_to_be32(toc->section_offset);
99 size = cpu_to_be32(toc->section_size);
Tom Rinid505e902012-10-16 13:06:06 +0000100 } else {
101 offset = toc->section_offset;
102 size = toc->section_size;
103 }
John Rigby34d12a12011-07-21 09:10:30 -0400104
105 if (offset >= OMAP_CH_HDR_SIZE ||
106 offset+size >= OMAP_CH_HDR_SIZE)
107 exit(EXIT_FAILURE);
108
109 printf("Section %s offset %x length %x\n",
110 toc->section_name,
111 toc->section_offset,
112 toc->section_size);
113
114 omapimage_print_section((struct ch_settings *)(ptr+offset));
115 toc++;
116 }
117
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400118 gph_print_header(gph, do_swap32);
John Rigby34d12a12011-07-21 09:10:30 -0400119}
120
121static int toc_offset(void *hdr, void *member)
122{
123 return member - hdr;
124}
125
126static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700127 struct image_tool_params *params)
John Rigby34d12a12011-07-21 09:10:30 -0400128{
129 struct ch_toc *toc = (struct ch_toc *)ptr;
130 struct ch_settings *chs = (struct ch_settings *)
131 (ptr + 2 * sizeof(*toc));
132 struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
133
134 toc->section_offset = toc_offset(ptr, chs);
135 toc->section_size = sizeof(struct ch_settings);
136 strcpy((char *)toc->section_name, "CHSETTINGS");
137
138 chs->section_key = KEY_CHSETTINGS;
139 chs->valid = 0;
140 chs->version = 1;
141 chs->reserved = 0;
142 chs->flags = 0;
143
144 toc++;
145 memset(toc, 0xff, sizeof(*toc));
146
Lokesh Vutla20d068e2017-12-28 20:40:00 +0530147 gph_set_header(gph, sbuf->st_size - OMAP_CH_HDR_SIZE,
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400148 params->addr, 0);
Tom Rinid505e902012-10-16 13:06:06 +0000149
150 if (strncmp(params->imagename, "byteswap", 8) == 0) {
151 do_swap32 = 1;
152 int swapped = 0;
153 uint32_t *data = (uint32_t *)ptr;
Philipp Tomsich070aa2d2017-12-04 17:04:02 +0100154 const off_t size_in_words =
155 DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t));
Tom Rinid505e902012-10-16 13:06:06 +0000156
Philipp Tomsich070aa2d2017-12-04 17:04:02 +0100157 while (swapped < size_in_words) {
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400158 *data = cpu_to_be32(*data);
Tom Rinid505e902012-10-16 13:06:06 +0000159 swapped++;
160 data++;
161 }
162 }
John Rigby34d12a12011-07-21 09:10:30 -0400163}
164
John Rigby34d12a12011-07-21 09:10:30 -0400165/*
166 * omapimage parameters
167 */
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -0200168U_BOOT_IMAGE_TYPE(
169 omapimage,
170 "TI OMAP CH/GP Boot Image support",
171 OMAP_FILE_HDR_SIZE,
172 (void *)&omapimage_header,
173 gpimage_check_params,
174 omapimage_verify_header,
175 omapimage_print_header,
176 omapimage_set_header,
177 NULL,
178 omapimage_check_image_types,
179 NULL,
180 NULL
181);