Merge git://git.denx.de/u-boot-arc
[platform/kernel/u-boot.git] / tools / omapimage.c
1 /*
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  *
14  * SPDX-License-Identifier:     GPL-2.0+
15  */
16
17 #include "imagetool.h"
18 #include <compiler.h>
19 #include <image.h>
20 #include "gpheader.h"
21 #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
25 #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE)
26
27 static int do_swap32 = 0;
28
29 static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
30
31 static int omapimage_check_image_types(uint8_t type)
32 {
33         if (type == IH_TYPE_OMAPIMAGE)
34                 return EXIT_SUCCESS;
35         return EXIT_FAILURE;
36 }
37
38 static int omapimage_verify_header(unsigned char *ptr, int image_size,
39                         struct image_tool_params *params)
40 {
41         struct ch_toc *toc = (struct ch_toc *)ptr;
42         struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
43         uint32_t offset, size;
44
45         while (toc->section_offset != 0xffffffff
46                         && toc->section_size != 0xffffffff) {
47                 if (do_swap32) {
48                         offset = cpu_to_be32(toc->section_offset);
49                         size = cpu_to_be32(toc->section_size);
50                 } else {
51                         offset = toc->section_offset;
52                         size = toc->section_size;
53                 }
54                 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         }
61
62         return gph_verify_header(gph, do_swap32);
63 }
64
65 static 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
87 static 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);
92         uint32_t offset, size;
93
94         while (toc->section_offset != 0xffffffff
95                         && toc->section_size != 0xffffffff) {
96                 if (do_swap32) {
97                         offset = cpu_to_be32(toc->section_offset);
98                         size = cpu_to_be32(toc->section_size);
99                 } else {
100                         offset = toc->section_offset;
101                         size = toc->section_size;
102                 }
103
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
117         gph_print_header(gph, do_swap32);
118 }
119
120 static int toc_offset(void *hdr, void *member)
121 {
122         return member - hdr;
123 }
124
125 static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
126                                 struct image_tool_params *params)
127 {
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
146         gph_set_header(gph, sbuf->st_size - OMAP_FILE_HDR_SIZE,
147                        params->addr, 0);
148
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))) {
155                         *data = cpu_to_be32(*data);
156                         swapped++;
157                         data++;
158                 }
159         }
160 }
161
162 /*
163  * omapimage parameters
164  */
165 U_BOOT_IMAGE_TYPE(
166         omapimage,
167         "TI OMAP CH/GP Boot Image support",
168         OMAP_FILE_HDR_SIZE,
169         (void *)&omapimage_header,
170         gpimage_check_params,
171         omapimage_verify_header,
172         omapimage_print_header,
173         omapimage_set_header,
174         NULL,
175         omapimage_check_image_types,
176         NULL,
177         NULL
178 );