1 // SPDX-License-Identifier: GPL-2.0+
4 * Andreas Bießmann <andreas@biessmann.org>
12 #define pr_err(fmt, args...) fprintf(stderr, "atmelimage Error: " fmt, ##args)
14 static int atmel_check_image_type(uint8_t type)
16 if (type == IH_TYPE_ATMELIMAGE)
22 static uint32_t nand_pmecc_header[52];
25 * A helper struct for parsing the mkimage -n parameter
27 * Keep in same order as the configs array!
29 static struct pmecc_config {
39 * Strings used for configure the PMECC header via -n mkimage switch
41 * We estimate a coma separated list of key=value pairs. The mkimage -n
42 * parameter argument should not contain any whitespace.
44 * Keep in same order as struct pmecc_config!
46 static const char * const configs[] = {
55 static int atmel_find_pmecc_parameter_in_token(const char *token)
60 debug("token: '%s'\n", token);
62 for (pos = 0; pos < ARRAY_SIZE(configs); pos++) {
63 if (strncmp(token, configs[pos], strlen(configs[pos])) == 0) {
64 param = strstr(token, "=");
69 debug("\t%s parameter: '%s'\n", configs[pos], param);
73 pmecc.use_pmecc = strtol(param, NULL, 10);
76 pmecc.sector_per_page = strtol(param, NULL, 10);
79 pmecc.spare_size = strtol(param, NULL, 10);
82 pmecc.ecc_bits = strtol(param, NULL, 10);
85 pmecc.sector_size = strtol(param, NULL, 10);
88 pmecc.ecc_offset = strtol(param, NULL, 10);
95 pr_err("Could not find parameter in token '%s'\n", token);
99 static int atmel_parse_pmecc_params(char *txt)
103 token = strtok(txt, ",");
104 while (token != NULL) {
105 if (atmel_find_pmecc_parameter_in_token(token))
108 token = strtok(NULL, ",");
114 static int atmel_verify_header(unsigned char *ptr, int image_size,
115 struct image_tool_params *params)
117 uint32_t *ints = (uint32_t *)ptr;
119 size_t size = image_size;
121 /* check if we have an PMECC header attached */
122 for (pos = 0; pos < ARRAY_SIZE(nand_pmecc_header); pos++)
123 if (ints[pos] >> 28 != 0xC)
126 if (pos == ARRAY_SIZE(nand_pmecc_header)) {
127 ints += ARRAY_SIZE(nand_pmecc_header);
128 size -= sizeof(nand_pmecc_header);
131 /* check the seven interrupt vectors of binary */
132 for (pos = 0; pos < 7; pos++) {
133 debug("atmelimage: interrupt vector #%zu is 0x%08X\n", pos+1,
136 * all vectors except the 6'th one must contain valid
140 /* 6'th vector has image size set, check later */
142 if ((ints[pos] & 0xff000000) == 0xea000000)
145 if ((ints[pos] & 0xfffff000) == 0xe59ff000)
146 /* valid LDR (I=0, P=1, U=1, B=0, W=0, L=1) */
148 /* ouch, one of the checks has missed ... */
152 return ints[5] != cpu_to_le32(size);
155 static void atmel_print_pmecc_header(const uint32_t word)
159 printf("\t\tPMECC header\n");
161 printf("\t\t====================\n");
163 val = (word >> 18) & 0x1ff;
164 printf("\t\teccOffset: %9i\n", val);
166 val = (((word >> 16) & 0x3) == 0) ? 512 : 1024;
167 printf("\t\tsectorSize: %8i\n", val);
169 if (((word >> 13) & 0x7) <= 2)
170 val = (2 << ((word >> 13) & 0x7));
172 val = (12 << (((word >> 13) & 0x7) - 3));
173 printf("\t\teccBitReq: %9i\n", val);
175 val = (word >> 4) & 0x1ff;
176 printf("\t\tspareSize: %9i\n", val);
178 val = (1 << ((word >> 1) & 0x3));
179 printf("\t\tnbSectorPerPage: %3i\n", val);
181 printf("\t\tusePmecc: %10i\n", word & 0x1);
182 printf("\t\t====================\n");
185 static void atmel_print_header(const void *ptr, struct image_tool_params *params)
187 uint32_t *ints = (uint32_t *)ptr;
190 /* check if we have an PMECC header attached */
191 for (pos = 0; pos < ARRAY_SIZE(nand_pmecc_header); pos++)
192 if (ints[pos] >> 28 != 0xC)
195 if (pos == ARRAY_SIZE(nand_pmecc_header)) {
196 printf("Image Type:\tATMEL ROM-Boot Image with PMECC Header\n");
197 atmel_print_pmecc_header(ints[0]);
200 printf("Image Type:\tATMEL ROM-Boot Image without PMECC Header\n");
203 printf("\t\t6'th vector has %u set\n", le32_to_cpu(ints[pos]));
206 static void atmel_set_header(void *ptr, struct stat *sbuf, int ifd,
207 struct image_tool_params *params)
209 /* just save the image size into 6'th interrupt vector */
210 uint32_t *ints = (uint32_t *)ptr;
213 size_t size = sbuf->st_size;
215 for (cnt = 0; cnt < ARRAY_SIZE(nand_pmecc_header); cnt++)
216 if (ints[cnt] >> 28 != 0xC)
219 if (cnt == ARRAY_SIZE(nand_pmecc_header)) {
220 pos += ARRAY_SIZE(nand_pmecc_header);
221 size -= sizeof(nand_pmecc_header);
224 ints[pos] = cpu_to_le32(size);
227 static int atmel_check_params(struct image_tool_params *params)
229 if (strlen(params->imagename) > 0)
230 if (atmel_parse_pmecc_params(params->imagename))
233 return !(!params->eflag &&
236 ((params->dflag && !params->lflag) ||
237 (params->lflag && !params->dflag)));
240 static int atmel_vrec_header(struct image_tool_params *params,
241 struct image_type_params *tparams)
246 if (strlen(params->imagename) == 0)
251 tmp |= (pmecc.ecc_offset & 0x1ff) << 18;
253 switch (pmecc.sector_size) {
262 pr_err("Wrong sectorSize (%i) for PMECC header\n",
267 switch (pmecc.ecc_bits) {
285 pr_err("Wrong eccBits (%i) for PMECC header\n",
290 tmp |= (pmecc.spare_size & 0x1ff) << 4;
292 switch (pmecc.sector_per_page) {
307 pr_err("Wrong sectorPerPage (%i) for PMECC header\n",
308 pmecc.sector_per_page);
315 for (pos = 0; pos < ARRAY_SIZE(nand_pmecc_header); pos++)
316 nand_pmecc_header[pos] = tmp;
318 debug("PMECC header filled 52 times with 0x%08X\n", tmp);
320 tparams->header_size = sizeof(nand_pmecc_header);
321 tparams->hdr = nand_pmecc_header;
328 "ATMEL ROM-Boot Image support",
336 atmel_check_image_type,