Licenses: Clarify exceptions for standalone apps
[platform/kernel/u-boot.git] / tools / omapimage.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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>
14  */
15
16 #include "imagetool.h"
17 #include <compiler.h>
18 #include <image.h>
19 #include "gpheader.h"
20 #include "omapimage.h"
21
22 #define DIV_ROUND_UP(n, d)     (((n) + (d) - 1) / (d))
23
24 /* Header size is CH header rounded up to 512 bytes plus GP header */
25 #define OMAP_CH_HDR_SIZE 512
26 #define OMAP_FILE_HDR_SIZE (OMAP_CH_HDR_SIZE + GPIMAGE_HDR_SIZE)
27
28 static int do_swap32 = 0;
29
30 static uint8_t omapimage_header[OMAP_FILE_HDR_SIZE];
31
32 static int omapimage_check_image_types(uint8_t type)
33 {
34         if (type == IH_TYPE_OMAPIMAGE)
35                 return EXIT_SUCCESS;
36         return EXIT_FAILURE;
37 }
38
39 static int omapimage_verify_header(unsigned char *ptr, int image_size,
40                         struct image_tool_params *params)
41 {
42         struct ch_toc *toc = (struct ch_toc *)ptr;
43         struct gp_header *gph = (struct gp_header *)(ptr+OMAP_CH_HDR_SIZE);
44         uint32_t offset, size;
45
46         while (toc->section_offset != 0xffffffff
47                         && toc->section_size != 0xffffffff) {
48                 if (do_swap32) {
49                         offset = cpu_to_be32(toc->section_offset);
50                         size = cpu_to_be32(toc->section_size);
51                 } else {
52                         offset = toc->section_offset;
53                         size = toc->section_size;
54                 }
55                 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         }
62
63         return gph_verify_header(gph, do_swap32);
64 }
65
66 static 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
88 static void omapimage_print_header(const void *ptr)
89 {
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);
93         uint32_t offset, size;
94
95         while (toc->section_offset != 0xffffffff
96                         && toc->section_size != 0xffffffff) {
97                 if (do_swap32) {
98                         offset = cpu_to_be32(toc->section_offset);
99                         size = cpu_to_be32(toc->section_size);
100                 } else {
101                         offset = toc->section_offset;
102                         size = toc->section_size;
103                 }
104
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
118         gph_print_header(gph, do_swap32);
119 }
120
121 static int toc_offset(void *hdr, void *member)
122 {
123         return member - hdr;
124 }
125
126 static void omapimage_set_header(void *ptr, struct stat *sbuf, int ifd,
127                                 struct image_tool_params *params)
128 {
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
147         gph_set_header(gph, sbuf->st_size - OMAP_CH_HDR_SIZE,
148                        params->addr, 0);
149
150         if (strncmp(params->imagename, "byteswap", 8) == 0) {
151                 do_swap32 = 1;
152                 int swapped = 0;
153                 uint32_t *data = (uint32_t *)ptr;
154                 const off_t size_in_words =
155                         DIV_ROUND_UP(sbuf->st_size, sizeof(uint32_t));
156
157                 while (swapped < size_in_words) {
158                         *data = cpu_to_be32(*data);
159                         swapped++;
160                         data++;
161                 }
162         }
163 }
164
165 /*
166  * omapimage parameters
167  */
168 U_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 );