blob: 01e02649e1e20fdf5db00690b9fae7eebca82ac3 [file] [log] [blame]
John Rigby3decb142011-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 Denk3765b3e2013-10-07 13:07:26 +020014 * SPDX-License-Identifier: GPL-2.0+
John Rigby3decb142011-07-21 09:10:30 -040015 */
16
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070017#include "imagetool.h"
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040018#include <compiler.h>
John Rigby3decb142011-07-21 09:10:30 -040019#include <image.h>
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040020#include "gpheader.h"
John Rigby3decb142011-07-21 09:10:30 -040021#include "omapimage.h"
22
Philipp Tomsichc8e1ca32017-12-04 17:04:02 +010023#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
24
John Rigby3decb142011-07-21 09:10:30 -040025/* Header size is CH header rounded up to 512 bytes plus GP header */
26#define OMAP_CH_HDR_SIZE 512
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040027#define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE)
John Rigby3decb142011-07-21 09:10:30 -040028
Tom Rini79b9ebb2012-10-16 13:06:06 +000029static int do_swap32 = 0;
30
John Rigby3decb142011-07-21 09:10:30 -040031static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
32
33static int omapimage_check_image_types(uint8_t type)
34{
35 if (type == IH_TYPE_OMAPIMAGE)
36 return EXIT_SUCCESS;
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040037 return EXIT_FAILURE;
John Rigby3decb142011-07-21 09:10:30 -040038}
39
40static int omapimage_verify_header(unsigned char *ptr, int image_size,
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -070041 struct image_tool_params *params)
John Rigby3decb142011-07-21 09:10:30 -040042{
43 struct ch_toc *toc = (struct ch_toc *)ptr;
44 struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040045 uint32_t offset, size;
John Rigby3decb142011-07-21 09:10:30 -040046
47 while (toc->section_offset != 0xffffffff
48 && toc->section_size != 0xffffffff) {
Tom Rini79b9ebb2012-10-16 13:06:06 +000049 if (do_swap32) {
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040050 offset = cpu_to_be32(toc->section_offset);
51 size = cpu_to_be32(toc->section_size);
Tom Rini79b9ebb2012-10-16 13:06:06 +000052 } else {
53 offset = toc->section_offset;
54 size = toc->section_size;
55 }
John Rigby3decb142011-07-21 09:10:30 -040056 if (!offset || !size)
57 return -1;
58 if (offset >= OMAP_CH_HDR_SIZE ||
59 offset+size >= OMAP_CH_HDR_SIZE)
60 return -1;
61 toc++;
62 }
Tom Rini79b9ebb2012-10-16 13:06:06 +000063
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040064 return gph_verify_header(gph, do_swap32);
John Rigby3decb142011-07-21 09:10:30 -040065}
66
67static void omapimage_print_section(struct ch_settings *chs)
68{
69 const char *section_name;
70
71 if (chs->section_key)
72 section_name = "CHSETTINGS";
73 else
74 section_name = "UNKNOWNKEY";
75
76 printf("%s (%x) "
77 "valid:%x "
78 "version:%x "
79 "reserved:%x "
80 "flags:%x\n",
81 section_name,
82 chs->section_key,
83 chs->valid,
84 chs->version,
85 chs->reserved,
86 chs->flags);
87}
88
89static void omapimage_print_header(const void *ptr)
90{
91 const struct ch_toc *toc = (struct ch_toc *)ptr;
92 const struct gp_header *gph =
93 (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040094 uint32_t offset, size;
John Rigby3decb142011-07-21 09:10:30 -040095
96 while (toc->section_offset != 0xffffffff
97 && toc->section_size != 0xffffffff) {
Tom Rini79b9ebb2012-10-16 13:06:06 +000098 if (do_swap32) {
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -040099 offset = cpu_to_be32(toc->section_offset);
100 size = cpu_to_be32(toc->section_size);
Tom Rini79b9ebb2012-10-16 13:06:06 +0000101 } else {
102 offset = toc->section_offset;
103 size = toc->section_size;
104 }
John Rigby3decb142011-07-21 09:10:30 -0400105
106 if (offset >= OMAP_CH_HDR_SIZE ||
107 offset+size >= OMAP_CH_HDR_SIZE)
108 exit(EXIT_FAILURE);
109
110 printf("Section %s offset %x length %x\n",
111 toc->section_name,
112 toc->section_offset,
113 toc->section_size);
114
115 omapimage_print_section((struct ch_settings *)(ptr+offset));
116 toc++;
117 }
118
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400119 gph_print_header(gph, do_swap32);
John Rigby3decb142011-07-21 09:10:30 -0400120}
121
122static int toc_offset(void *hdr, void *member)
123{
124 return member - hdr;
125}
126
127static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
Guilherme Maciel Ferreiraf86ed6a2013-12-01 12:43:10 -0700128 struct image_tool_params *params)
John Rigby3decb142011-07-21 09:10:30 -0400129{
130 struct ch_toc *toc = (struct ch_toc *)ptr;
131 struct ch_settings *chs = (struct ch_settings *)
132 (ptr + 2 * sizeof(*toc));
133 struct gp_header *gph = (struct gp_header *)(ptr + OMAP_CH_HDR_SIZE);
134
135 toc->section_offset = toc_offset(ptr, chs);
136 toc->section_size = sizeof(struct ch_settings);
137 strcpy((char *)toc->section_name, "CHSETTINGS");
138
139 chs->section_key = KEY_CHSETTINGS;
140 chs->valid = 0;
141 chs->version = 1;
142 chs->reserved = 0;
143 chs->flags = 0;
144
145 toc++;
146 memset(toc, 0xff, sizeof(*toc));
147
Lokesh Vutla3a0e70f2017-12-28 20:40:00 +0530148 gph_set_header(gph, sbuf->st_size - OMAP_CH_HDR_SIZE,
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400149 params->addr, 0);
Tom Rini79b9ebb2012-10-16 13:06:06 +0000150
151 if (strncmp(params->imagename, "byteswap", 8) == 0) {
152 do_swap32 = 1;
153 int swapped = 0;
154 uint32_t *data = (uint32_t *)ptr;
Philipp Tomsichc8e1ca32017-12-04 17:04:02 +0100155 const off_t size_in_words =
156 DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t));
Tom Rini79b9ebb2012-10-16 13:06:06 +0000157
Philipp Tomsichc8e1ca32017-12-04 17:04:02 +0100158 while (swapped < size_in_words) {
Karicheri, Muralidharanbf411ea2014-04-04 13:16:48 -0400159 *data = cpu_to_be32(*data);
Tom Rini79b9ebb2012-10-16 13:06:06 +0000160 swapped++;
161 data++;
162 }
163 }
John Rigby3decb142011-07-21 09:10:30 -0400164}
165
John Rigby3decb142011-07-21 09:10:30 -0400166/*
167 * omapimage parameters
168 */
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -0200169U_BOOT_IMAGE_TYPE(
170 omapimage,
171 "TI OMAP CH/GP Boot Image support",
172 OMAP_FILE_HDR_SIZE,
173 (void *)&omapimage_header,
174 gpimage_check_params,
175 omapimage_verify_header,
176 omapimage_print_header,
177 omapimage_set_header,
178 NULL,
179 omapimage_check_image_types,
180 NULL,
181 NULL
182);