blob: 1e0c16479681aec2d8621eabd953ff9a8cdb203c [file] [log] [blame]
John Rigby34d12a12011-07-21 09:10:30 -04001/*
2 * (C) Copyright 2010
3 * Linaro LTD, www.linaro.org
4 * Author: John Rigby <john.rigby@linaro.org>
5 * Based on TI's signGP.c
6 *
7 * (C) Copyright 2009
8 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
9 *
10 * (C) Copyright 2008
11 * Marvell Semiconductor <www.marvell.com>
12 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
13 *
Wolfgang Denkbd8ec7e2013-10-07 13:07:26 +020014 * SPDX-License-Identifier: GPL-2.0+
John Rigby34d12a12011-07-21 09:10:30 -040015 */
16
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070017#include "imagetool.h"
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040018#include <compiler.h>
John Rigby34d12a12011-07-21 09:10:30 -040019#include <image.h>
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040020#include "gpheader.h"
John Rigby34d12a12011-07-21 09:10:30 -040021#include "omapimage.h"
22
23/* Header size is CH header rounded up to 512 bytes plus GP header */
24#define OMAP_CH_HDR_SIZE 512
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040025#define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE)
John Rigby34d12a12011-07-21 09:10:30 -040026
Tom Rinid505e902012-10-16 13:06:06 +000027static int do_swap32 = 0;
28
John Rigby34d12a12011-07-21 09:10:30 -040029static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
30
31static int omapimage_check_image_types(uint8_t type)
32{
33 if (type == IH_TYPE_OMAPIMAGE)
34 return EXIT_SUCCESS;
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040035 return EXIT_FAILURE;
John Rigby34d12a12011-07-21 09:10:30 -040036}
37
38static int omapimage_verify_header(unsigned char *ptr, int image_size,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -070039 struct image_tool_params *params)
John Rigby34d12a12011-07-21 09:10:30 -040040{
41 struct ch_toc *toc = (struct ch_toc *)ptr;
42 struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040043 uint32_t offset, size;
John Rigby34d12a12011-07-21 09:10:30 -040044
45 while (toc->section_offset != 0xffffffff
46 && toc->section_size != 0xffffffff) {
Tom Rinid505e902012-10-16 13:06:06 +000047 if (do_swap32) {
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040048 offset = cpu_to_be32(toc->section_offset);
49 size = cpu_to_be32(toc->section_size);
Tom Rinid505e902012-10-16 13:06:06 +000050 } else {
51 offset = toc->section_offset;
52 size = toc->section_size;
53 }
John Rigby34d12a12011-07-21 09:10:30 -040054 if (!offset || !size)
55 return -1;
56 if (offset >= OMAP_CH_HDR_SIZE ||
57 offset+size >= OMAP_CH_HDR_SIZE)
58 return -1;
59 toc++;
60 }
Tom Rinid505e902012-10-16 13:06:06 +000061
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040062 return gph_verify_header(gph, do_swap32);
John Rigby34d12a12011-07-21 09:10:30 -040063}
64
65static void omapimage_print_section(struct ch_settings *chs)
66{
67 const char *section_name;
68
69 if (chs->section_key)
70 section_name = "CHSETTINGS";
71 else
72 section_name = "UNKNOWNKEY";
73
74 printf("%s (%x) "
75 "valid:%x "
76 "version:%x "
77 "reserved:%x "
78 "flags:%x\n",
79 section_name,
80 chs->section_key,
81 chs->valid,
82 chs->version,
83 chs->reserved,
84 chs->flags);
85}
86
87static void omapimage_print_header(const void *ptr)
88{
89 const struct ch_toc *toc = (struct ch_toc *)ptr;
90 const struct gp_header *gph =
91 (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040092 uint32_t offset, size;
John Rigby34d12a12011-07-21 09:10:30 -040093
94 while (toc->section_offset != 0xffffffff
95 && toc->section_size != 0xffffffff) {
Tom Rinid505e902012-10-16 13:06:06 +000096 if (do_swap32) {
Karicheri, Muralidharan82777932014-04-04 13:16:48 -040097 offset = cpu_to_be32(toc->section_offset);
98 size = cpu_to_be32(toc->section_size);
Tom Rinid505e902012-10-16 13:06:06 +000099 } else {
100 offset = toc->section_offset;
101 size = toc->section_size;
102 }
John Rigby34d12a12011-07-21 09:10:30 -0400103
104 if (offset >= OMAP_CH_HDR_SIZE ||
105 offset+size >= OMAP_CH_HDR_SIZE)
106 exit(EXIT_FAILURE);
107
108 printf("Section %s offset %x length %x\n",
109 toc->section_name,
110 toc->section_offset,
111 toc->section_size);
112
113 omapimage_print_section((struct ch_settings *)(ptr+offset));
114 toc++;
115 }
116
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400117 gph_print_header(gph, do_swap32);
John Rigby34d12a12011-07-21 09:10:30 -0400118}
119
120static int toc_offset(void *hdr, void *member)
121{
122 return member - hdr;
123}
124
125static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700126 struct image_tool_params *params)
John Rigby34d12a12011-07-21 09:10:30 -0400127{
128 struct ch_toc *toc = (struct ch_toc *)ptr;
129 struct ch_settings *chs = (struct ch_settings *)
130 (ptr + 2 * sizeof(*toc));
131 struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
132
133 toc->section_offset = toc_offset(ptr, chs);
134 toc->section_size = sizeof(struct ch_settings);
135 strcpy((char *)toc->section_name, "CHSETTINGS");
136
137 chs->section_key = KEY_CHSETTINGS;
138 chs->valid = 0;
139 chs->version = 1;
140 chs->reserved = 0;
141 chs->flags = 0;
142
143 toc++;
144 memset(toc, 0xff, sizeof(*toc));
145
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400146 gph_set_header(gph, sbuf->st_size - OMAP_FILE_HDR_SIZE,
147 params->addr, 0);
Tom Rinid505e902012-10-16 13:06:06 +0000148
149 if (strncmp(params->imagename, "byteswap", 8) == 0) {
150 do_swap32 = 1;
151 int swapped = 0;
152 uint32_t *data = (uint32_t *)ptr;
153
154 while (swapped <= (sbuf->st_size / sizeof(uint32_t))) {
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400155 *data = cpu_to_be32(*data);
Tom Rinid505e902012-10-16 13:06:06 +0000156 swapped++;
157 data++;
158 }
159 }
John Rigby34d12a12011-07-21 09:10:30 -0400160}
161
John Rigby34d12a12011-07-21 09:10:30 -0400162/*
163 * omapimage parameters
164 */
165static struct image_type_params omapimage_params = {
166 .name = "TI OMAP CH/GP Boot Image support",
167 .header_size = OMAP_FILE_HDR_SIZE,
168 .hdr = (void *)&omapimage_header,
169 .check_image_type = omapimage_check_image_types,
170 .verify_header = omapimage_verify_header,
171 .print_header = omapimage_print_header,
172 .set_header = omapimage_set_header,
Karicheri, Muralidharan82777932014-04-04 13:16:48 -0400173 .check_params = gpimage_check_params,
John Rigby34d12a12011-07-21 09:10:30 -0400174};
175
176void init_omap_image_type(void)
177{
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -0700178 register_image_type(&omapimage_params);
John Rigby34d12a12011-07-21 09:10:30 -0400179}