blob: cd89145867d5b17351b27112017ef7d83a7b80d7 [file] [log] [blame]
Stefano Babic38855512011-10-17 00:07:43 +00001/*
2 * (C) Copyright 2011
3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Stefano Babic38855512011-10-17 00:07:43 +00006 */
7
8/* Required to obtain the getline prototype from stdio.h */
9#define _GNU_SOURCE
10
11#include "mkimage.h"
12#include "aisimage.h"
13#include <image.h>
14
15#define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
16#define WORD_ALIGN0 4
17#define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
18#define MAX_CMD_BUFFER 4096
Stefano Babic38855512011-10-17 00:07:43 +000019
20static uint32_t ais_img_size;
21
22/*
23 * Supported commands for configuration file
24 */
25static table_entry_t aisimage_cmds[] = {
26 {CMD_DATA, "DATA", "Reg Write Data"},
27 {CMD_FILL, "FILL", "Fill range with pattern"},
28 {CMD_CRCON, "CRCON", "CRC Enable"},
29 {CMD_CRCOFF, "CRCOFF", "CRC Disable"},
30 {CMD_CRCCHECK, "CRCCHECK", "CRC Validate"},
31 {CMD_JMPCLOSE, "JMPCLOSE", "Jump & Close"},
32 {CMD_JMP, "JMP", "Jump"},
33 {CMD_SEQREAD, "SEQREAD", "Sequential read"},
34 {CMD_PLL0, "PLL0", "PLL0"},
35 {CMD_PLL1, "PLL1", "PLL1"},
36 {CMD_CLK, "CLK", "Clock configuration"},
37 {CMD_DDR2, "DDR2", "DDR2 Configuration"},
38 {CMD_EMIFA, "EMIFA", "EMIFA"},
39 {CMD_EMIFA_ASYNC, "EMIFA_ASYNC", "EMIFA Async"},
40 {CMD_PLL, "PLL", "PLL & Clock configuration"},
41 {CMD_PSC, "PSC", "PSC setup"},
42 {CMD_PINMUX, "PINMUX", "Pinmux setup"},
43 {CMD_BOOTTABLE, "BOOT_TABLE", "Boot table command"},
44 {-1, "", ""},
45};
46
47static struct ais_func_exec {
48 uint32_t index;
49 uint32_t argcnt;
50} ais_func_table[] = {
51 [CMD_PLL0] = {0, 2},
52 [CMD_PLL1] = {1, 2},
53 [CMD_CLK] = {2, 1},
54 [CMD_DDR2] = {3, 8},
55 [CMD_EMIFA] = {4, 5},
56 [CMD_EMIFA_ASYNC] = {5, 5},
57 [CMD_PLL] = {6, 3},
58 [CMD_PSC] = {7, 1},
59 [CMD_PINMUX] = {8, 3}
60};
61
62static struct cmd_table_t {
63 uint32_t nargs;
64 uint32_t AIS_cmd;
65} cmd_table[] = {
66 [CMD_FILL] = { 4, AIS_CMD_FILL},
67 [CMD_CRCON] = { 0, AIS_CMD_ENCRC},
68 [CMD_CRCOFF] = { 0, AIS_CMD_DISCRC},
69 [CMD_CRCCHECK] = { 2, AIS_CMD_ENCRC},
70 [CMD_JMPCLOSE] = { 1, AIS_CMD_JMPCLOSE},
71 [CMD_JMP] = { 1, AIS_CMD_JMP},
72 [CMD_SEQREAD] = { 0, AIS_CMD_SEQREAD},
73 [CMD_PLL0] = { 2, AIS_CMD_FNLOAD},
74 [CMD_PLL1] = { 2, AIS_CMD_FNLOAD},
75 [CMD_CLK] = { 1, AIS_CMD_FNLOAD},
76 [CMD_DDR2] = { 8, AIS_CMD_FNLOAD},
77 [CMD_EMIFA] = { 5, AIS_CMD_FNLOAD},
78 [CMD_EMIFA_ASYNC] = { 5, AIS_CMD_FNLOAD},
79 [CMD_PLL] = { 3, AIS_CMD_FNLOAD},
80 [CMD_PSC] = { 1, AIS_CMD_FNLOAD},
81 [CMD_PINMUX] = { 3, AIS_CMD_FNLOAD},
82 [CMD_BOOTTABLE] = { 4, AIS_CMD_BOOTTBL},
83};
84
85static uint32_t get_cfg_value(char *token, char *name, int linenr)
86{
87 char *endptr;
88 uint32_t value;
89
90 errno = 0;
91 value = strtoul(token, &endptr, 16);
92 if (errno || (token == endptr)) {
93 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
94 name, linenr, token);
95 exit(EXIT_FAILURE);
96 }
97 return value;
98}
99
100static int get_ais_table_id(uint32_t *ptr)
101{
102
103 int i;
104 int func_no;
105
106 for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
107 if (*ptr == cmd_table[i].AIS_cmd) {
108 if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD)
109 return i;
110
111 func_no = ((struct ais_cmd_func *)ptr)->func_args
112 & 0xFFFF;
113 if (func_no == ais_func_table[i].index)
114 return i;
115 }
116 }
117
118 return -1;
119}
120
121static void aisimage_print_header(const void *hdr)
122{
123 struct ais_header *ais_hdr = (struct ais_header *)hdr;
124 uint32_t *ptr;
125 struct ais_cmd_load *ais_load;
126 int id;
127
128 if (ais_hdr->magic != AIS_MAGIC_WORD) {
129 fprintf(stderr, "Error: - AIS Magic Number not found\n");
130 return;
131 }
132 fprintf(stdout, "Image Type: TI Davinci AIS Boot Image\n");
133 fprintf(stdout, "AIS magic : %08x\n", ais_hdr->magic);
134 ptr = (uint32_t *)&ais_hdr->magic;
135 ptr++;
136
137 while (*ptr != AIS_CMD_JMPCLOSE) {
138 /* Check if we find the image */
139 if (*ptr == AIS_CMD_LOAD) {
140 ais_load = (struct ais_cmd_load *)ptr;
141 fprintf(stdout, "Image at : 0x%08x size 0x%08x\n",
142 ais_load->addr,
143 ais_load->size);
144 ptr = ais_load->data + ais_load->size / sizeof(*ptr);
145 continue;
146 }
147
148 id = get_ais_table_id(ptr);
149 if (id < 0) {
150 fprintf(stderr, "Error: - AIS Image corrupted\n");
151 return;
152 }
153 fprintf(stdout, "AIS cmd : %s\n",
154 get_table_entry_name(aisimage_cmds, NULL, id));
155 ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1;
156 if (((void *)ptr - hdr) > ais_img_size) {
157 fprintf(stderr,
158 "AIS Image not terminated by JMPCLOSE\n");
159 return;
160 }
161 }
162}
163
164static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
165 uint32_t *parms, struct image_type_params *tparams,
Simon Glass4c9b59f2011-11-19 08:20:54 +0000166 uint32_t *ptr)
Stefano Babic38855512011-10-17 00:07:43 +0000167{
168 int i;
169
170 *ptr++ = cmd_table[cmd].AIS_cmd;
171 if (IS_FNC_EXEC(cmd))
172 *ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index;
173
174 /* Copy parameters */
175 for (i = 0; i < nargs; i++)
176 *ptr++ = cpu_to_le32(parms[i]);
177
178 return ptr;
179
180}
181
182static uint32_t *ais_alloc_buffer(struct mkimage_params *params)
183{
184 int dfd;
185 struct stat sbuf;
186 char *datafile = params->datafile;
187 uint32_t *ptr;
188
189 dfd = open(datafile, O_RDONLY|O_BINARY);
190 if (dfd < 0) {
191 fprintf(stderr, "%s: Can't open %s: %s\n",
192 params->cmdname, datafile, strerror(errno));
193 exit(EXIT_FAILURE);
194 }
195
196 if (fstat(dfd, &sbuf) < 0) {
197 fprintf(stderr, "%s: Can't stat %s: %s\n",
198 params->cmdname, datafile, strerror(errno));
199 exit(EXIT_FAILURE);
200 }
201
202 /*
203 * Place for header is allocated. The size is taken from
204 * the size of the datafile, that the ais_image_generate()
205 * will copy into the header. Copying the datafile
206 * is not left to the main program, because after the datafile
207 * the header must be terminated with the Jump & Close command.
208 */
209 ais_img_size = WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER;
210 ptr = (uint32_t *)malloc(WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER);
211 if (!ptr) {
212 fprintf(stderr, "%s: malloc return failure: %s\n",
213 params->cmdname, strerror(errno));
214 exit(EXIT_FAILURE);
215 }
216
217 close(dfd);
218
219 return ptr;
220}
221
222static uint32_t *ais_copy_image(struct mkimage_params *params,
223 uint32_t *aisptr)
224
225{
226 int dfd;
227 struct stat sbuf;
228 char *datafile = params->datafile;
229 void *ptr;
230
231 dfd = open(datafile, O_RDONLY|O_BINARY);
232 if (dfd < 0) {
233 fprintf(stderr, "%s: Can't open %s: %s\n",
234 params->cmdname, datafile, strerror(errno));
235 exit(EXIT_FAILURE);
236 }
237
238 if (fstat(dfd, &sbuf) < 0) {
239 fprintf(stderr, "%s: Can't stat %s: %s\n",
240 params->cmdname, datafile, strerror(errno));
241 exit(EXIT_FAILURE);
242 }
243
244 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
245 *aisptr++ = AIS_CMD_LOAD;
246 *aisptr++ = params->ep;
247 *aisptr++ = sbuf.st_size;
248 memcpy((void *)aisptr, ptr, sbuf.st_size);
249 aisptr += WORD_ALIGN(sbuf.st_size) / sizeof(uint32_t);
250
251 (void) munmap((void *)ptr, sbuf.st_size);
252 (void) close(dfd);
253
254 return aisptr;
255
256}
257
258static int aisimage_generate(struct mkimage_params *params,
259 struct image_type_params *tparams)
260{
261 FILE *fd = NULL;
262 char *line = NULL;
263 char *token, *saveptr1, *saveptr2;
264 int lineno = 0;
265 int fld;
266 size_t len;
267 int32_t cmd;
268 uint32_t nargs, cmd_parms[10];
269 uint32_t value, size;
270 char *name = params->imagename;
Simon Glass4c9b59f2011-11-19 08:20:54 +0000271 uint32_t *aishdr;
Stefano Babic38855512011-10-17 00:07:43 +0000272
273 fd = fopen(name, "r");
274 if (fd == 0) {
275 fprintf(stderr,
276 "Error: %s - Can't open AIS configuration\n", name);
277 exit(EXIT_FAILURE);
278 }
279
280 /*
281 * the size of the header is variable and is computed
282 * scanning the configuration file.
283 */
284 tparams->header_size = 0;
285
286 /*
287 * Start allocating a buffer suitable for most command
288 * The buffer is then reallocated if it is too small
289 */
290 aishdr = ais_alloc_buffer(params);
291 tparams->hdr = aishdr;
292 *aishdr++ = AIS_MAGIC_WORD;
293
294 /* Very simple parsing, line starting with # are comments
295 * and are dropped
296 */
297 while ((getline(&line, &len, fd)) > 0) {
298 lineno++;
299
300 token = strtok_r(line, "\r\n", &saveptr1);
301 if (token == NULL)
302 continue;
303
304 /* Check inside the single line */
305 line = token;
306 fld = CFG_COMMAND;
307 cmd = CMD_INVALID;
308 nargs = 0;
309 while (token != NULL) {
310 token = strtok_r(line, " \t", &saveptr2);
311 if (token == NULL)
312 break;
313
314 /* Drop all text starting with '#' as comments */
315 if (token[0] == '#')
316 break;
317
318 switch (fld) {
319 case CFG_COMMAND:
320 cmd = get_table_entry_id(aisimage_cmds,
321 "aisimage commands", token);
322 if (cmd < 0) {
323 fprintf(stderr,
324 "Error: %s[%d] - Invalid command"
325 "(%s)\n", name, lineno, token);
326
327 exit(EXIT_FAILURE);
328 }
329 break;
330 case CFG_VALUE:
331 value = get_cfg_value(token, name, lineno);
332 cmd_parms[nargs++] = value;
333 if (nargs > cmd_table[cmd].nargs) {
334 fprintf(stderr,
335 "Error: %s[%d] - too much arguments:"
336 "(%s) for command %s\n", name,
337 lineno, token,
338 aisimage_cmds[cmd].sname);
339 exit(EXIT_FAILURE);
340 }
341 break;
342 }
343 line = NULL;
344 fld = CFG_VALUE;
345 }
346 if (cmd != CMD_INVALID) {
347 /* Now insert the command into the header */
348 aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms,
Simon Glass4c9b59f2011-11-19 08:20:54 +0000349 tparams, aishdr);
Stefano Babic38855512011-10-17 00:07:43 +0000350 }
351
352 }
353 fclose(fd);
354
355 aishdr = ais_copy_image(params, aishdr);
356
357 /* Add Jmp & Close */
358 *aishdr++ = AIS_CMD_JMPCLOSE;
359 *aishdr++ = params->ep;
360
361 size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t);
362 tparams->header_size = size;
363
364 return 0;
365}
366
367static int aisimage_check_image_types(uint8_t type)
368{
369 if (type == IH_TYPE_AISIMAGE)
370 return EXIT_SUCCESS;
371 else
372 return EXIT_FAILURE;
373}
374
375static int aisimage_verify_header(unsigned char *ptr, int image_size,
376 struct mkimage_params *params)
377{
378 struct ais_header *ais_hdr = (struct ais_header *)ptr;
379
380 if (ais_hdr->magic != AIS_MAGIC_WORD)
381 return -FDT_ERR_BADSTRUCTURE;
382
383 /* Store the total size to remember in print_hdr */
384 ais_img_size = image_size;
385
386 return 0;
387}
388
389static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
390 struct mkimage_params *params)
391{
392}
393
394int aisimage_check_params(struct mkimage_params *params)
395{
396 if (!params)
397 return CFG_INVALID;
398 if (!strlen(params->imagename)) {
399 fprintf(stderr, "Error: %s - Configuration file not specified, "
400 "it is needed for aisimage generation\n",
401 params->cmdname);
402 return CFG_INVALID;
403 }
404 /*
405 * Check parameters:
406 * XIP is not allowed and verify that incompatible
407 * parameters are not sent at the same time
408 * For example, if list is required a data image must not be provided
409 */
410 return (params->dflag && (params->fflag || params->lflag)) ||
411 (params->fflag && (params->dflag || params->lflag)) ||
412 (params->lflag && (params->dflag || params->fflag)) ||
413 (params->xflag) || !(strlen(params->imagename));
414}
415
416/*
417 * aisimage parameters
418 */
419static struct image_type_params aisimage_params = {
420 .name = "TI Davinci AIS Boot Image support",
421 .header_size = 0,
422 .hdr = NULL,
423 .check_image_type = aisimage_check_image_types,
424 .verify_header = aisimage_verify_header,
425 .print_header = aisimage_print_header,
426 .set_header = aisimage_set_header,
427 .check_params = aisimage_check_params,
428 .vrec_header = aisimage_generate,
429};
430
431void init_ais_image_type(void)
432{
433 mkimage_register(&aisimage_params);
434}