1 // SPDX-License-Identifier: GPL-2.0+
4 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
11 #define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
13 #define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
14 #define MAX_CMD_BUFFER 4096
16 static uint32_t ais_img_size
;
19 * Supported commands for configuration file
21 static table_entry_t aisimage_cmds
[] = {
22 {CMD_DATA
, "DATA", "Reg Write Data"},
23 {CMD_FILL
, "FILL", "Fill range with pattern"},
24 {CMD_CRCON
, "CRCON", "CRC Enable"},
25 {CMD_CRCOFF
, "CRCOFF", "CRC Disable"},
26 {CMD_CRCCHECK
, "CRCCHECK", "CRC Validate"},
27 {CMD_JMPCLOSE
, "JMPCLOSE", "Jump & Close"},
28 {CMD_JMP
, "JMP", "Jump"},
29 {CMD_SEQREAD
, "SEQREAD", "Sequential read"},
30 {CMD_PLL0
, "PLL0", "PLL0"},
31 {CMD_PLL1
, "PLL1", "PLL1"},
32 {CMD_CLK
, "CLK", "Clock configuration"},
33 {CMD_DDR2
, "DDR2", "DDR2 Configuration"},
34 {CMD_EMIFA
, "EMIFA", "EMIFA"},
35 {CMD_EMIFA_ASYNC
, "EMIFA_ASYNC", "EMIFA Async"},
36 {CMD_PLL
, "PLL", "PLL & Clock configuration"},
37 {CMD_PSC
, "PSC", "PSC setup"},
38 {CMD_PINMUX
, "PINMUX", "Pinmux setup"},
39 {CMD_BOOTTABLE
, "BOOT_TABLE", "Boot table command"},
43 static struct ais_func_exec
{
46 } ais_func_table
[] = {
52 [CMD_EMIFA_ASYNC
] = {5, 5},
58 static struct cmd_table_t
{
62 [CMD_FILL
] = { 4, AIS_CMD_FILL
},
63 [CMD_CRCON
] = { 0, AIS_CMD_ENCRC
},
64 [CMD_CRCOFF
] = { 0, AIS_CMD_DISCRC
},
65 [CMD_CRCCHECK
] = { 2, AIS_CMD_ENCRC
},
66 [CMD_JMPCLOSE
] = { 1, AIS_CMD_JMPCLOSE
},
67 [CMD_JMP
] = { 1, AIS_CMD_JMP
},
68 [CMD_SEQREAD
] = { 0, AIS_CMD_SEQREAD
},
69 [CMD_PLL0
] = { 2, AIS_CMD_FNLOAD
},
70 [CMD_PLL1
] = { 2, AIS_CMD_FNLOAD
},
71 [CMD_CLK
] = { 1, AIS_CMD_FNLOAD
},
72 [CMD_DDR2
] = { 8, AIS_CMD_FNLOAD
},
73 [CMD_EMIFA
] = { 5, AIS_CMD_FNLOAD
},
74 [CMD_EMIFA_ASYNC
] = { 5, AIS_CMD_FNLOAD
},
75 [CMD_PLL
] = { 3, AIS_CMD_FNLOAD
},
76 [CMD_PSC
] = { 1, AIS_CMD_FNLOAD
},
77 [CMD_PINMUX
] = { 3, AIS_CMD_FNLOAD
},
78 [CMD_BOOTTABLE
] = { 4, AIS_CMD_BOOTTBL
},
81 static uint32_t get_cfg_value(char *token
, char *name
, int linenr
)
87 value
= strtoul(token
, &endptr
, 16);
88 if (errno
|| (token
== endptr
)) {
89 fprintf(stderr
, "Error: %s[%d] - Invalid hex data(%s)\n",
96 static int get_ais_table_id(uint32_t *ptr
)
102 for (i
= 0; i
< ARRAY_SIZE(cmd_table
); i
++) {
103 if (*ptr
== cmd_table
[i
].AIS_cmd
) {
104 if (cmd_table
[i
].AIS_cmd
!= AIS_CMD_FNLOAD
)
107 func_no
= ((struct ais_cmd_func
*)ptr
)->func_args
109 if (func_no
== ais_func_table
[i
].index
)
117 static void aisimage_print_header(const void *hdr
)
119 struct ais_header
*ais_hdr
= (struct ais_header
*)hdr
;
121 struct ais_cmd_load
*ais_load
;
124 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
) {
125 fprintf(stderr
, "Error: - AIS Magic Number not found\n");
128 fprintf(stdout
, "Image Type: TI Davinci AIS Boot Image\n");
129 fprintf(stdout
, "AIS magic : %08x\n", ais_hdr
->magic
);
130 ptr
= (uint32_t *)&ais_hdr
->magic
;
133 while (*ptr
!= AIS_CMD_JMPCLOSE
) {
134 /* Check if we find the image */
135 if (*ptr
== AIS_CMD_LOAD
) {
136 ais_load
= (struct ais_cmd_load
*)ptr
;
137 fprintf(stdout
, "Image at : 0x%08x size 0x%08x\n",
140 ptr
= ais_load
->data
+ ais_load
->size
/ sizeof(*ptr
);
144 id
= get_ais_table_id(ptr
);
146 fprintf(stderr
, "Error: - AIS Image corrupted\n");
149 fprintf(stdout
, "AIS cmd : %s\n",
150 get_table_entry_name(aisimage_cmds
, NULL
, id
));
151 ptr
+= cmd_table
[id
].nargs
+ IS_FNC_EXEC(id
) + 1;
152 if (((void *)ptr
- hdr
) > ais_img_size
) {
154 "AIS Image not terminated by JMPCLOSE\n");
160 static uint32_t *ais_insert_cmd_header(uint32_t cmd
, uint32_t nargs
,
161 uint32_t *parms
, struct image_type_params
*tparams
,
166 *ptr
++ = cmd_table
[cmd
].AIS_cmd
;
167 if (IS_FNC_EXEC(cmd
))
168 *ptr
++ = ((nargs
& 0xFFFF) << 16) + ais_func_table
[cmd
].index
;
170 /* Copy parameters */
171 for (i
= 0; i
< nargs
; i
++)
172 *ptr
++ = cpu_to_le32(parms
[i
]);
178 static uint32_t *ais_alloc_buffer(struct image_tool_params
*params
)
182 char *datafile
= params
->datafile
;
185 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
187 fprintf(stderr
, "%s: Can't open %s: %s\n",
188 params
->cmdname
, datafile
, strerror(errno
));
192 if (fstat(dfd
, &sbuf
) < 0) {
193 fprintf(stderr
, "%s: Can't stat %s: %s\n",
194 params
->cmdname
, datafile
, strerror(errno
));
199 * Place for header is allocated. The size is taken from
200 * the size of the datafile, that the ais_image_generate()
201 * will copy into the header. Copying the datafile
202 * is not left to the main program, because after the datafile
203 * the header must be terminated with the Jump & Close command.
205 ais_img_size
= WORD_ALIGN(sbuf
.st_size
) + MAX_CMD_BUFFER
;
206 ptr
= (uint32_t *)malloc(WORD_ALIGN(sbuf
.st_size
) + MAX_CMD_BUFFER
);
208 fprintf(stderr
, "%s: malloc return failure: %s\n",
209 params
->cmdname
, strerror(errno
));
218 static uint32_t *ais_copy_image(struct image_tool_params
*params
,
224 char *datafile
= params
->datafile
;
227 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
229 fprintf(stderr
, "%s: Can't open %s: %s\n",
230 params
->cmdname
, datafile
, strerror(errno
));
234 if (fstat(dfd
, &sbuf
) < 0) {
235 fprintf(stderr
, "%s: Can't stat %s: %s\n",
236 params
->cmdname
, datafile
, strerror(errno
));
240 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
241 *aisptr
++ = AIS_CMD_LOAD
;
242 *aisptr
++ = params
->ep
;
243 *aisptr
++ = sbuf
.st_size
;
244 memcpy((void *)aisptr
, ptr
, sbuf
.st_size
);
245 aisptr
+= WORD_ALIGN(sbuf
.st_size
) / sizeof(uint32_t);
247 (void) munmap((void *)ptr
, sbuf
.st_size
);
254 static int aisimage_generate(struct image_tool_params
*params
,
255 struct image_type_params
*tparams
)
259 char *token
, *saveptr1
, *saveptr2
;
264 uint32_t nargs
, cmd_parms
[10];
265 uint32_t value
, size
;
266 char *name
= params
->imagename
;
269 fd
= fopen(name
, "r");
272 "Error: %s - Can't open AIS configuration\n", name
);
277 * the size of the header is variable and is computed
278 * scanning the configuration file.
280 tparams
->header_size
= 0;
283 * Start allocating a buffer suitable for most command
284 * The buffer is then reallocated if it is too small
286 aishdr
= ais_alloc_buffer(params
);
287 tparams
->hdr
= aishdr
;
288 *aishdr
++ = AIS_MAGIC_WORD
;
290 /* Very simple parsing, line starting with # are comments
293 while ((getline(&line
, &len
, fd
)) > 0) {
296 token
= strtok_r(line
, "\r\n", &saveptr1
);
300 /* Check inside the single line */
305 while (token
!= NULL
) {
306 token
= strtok_r(line
, " \t", &saveptr2
);
310 /* Drop all text starting with '#' as comments */
316 cmd
= get_table_entry_id(aisimage_cmds
,
317 "aisimage commands", token
);
320 "Error: %s[%d] - Invalid command"
321 "(%s)\n", name
, lineno
, token
);
327 value
= get_cfg_value(token
, name
, lineno
);
328 cmd_parms
[nargs
++] = value
;
329 if (nargs
> cmd_table
[cmd
].nargs
) {
331 "Error: %s[%d] - too much arguments:"
332 "(%s) for command %s\n", name
,
334 aisimage_cmds
[cmd
].sname
);
342 if (cmd
!= CMD_INVALID
) {
343 /* Now insert the command into the header */
344 aishdr
= ais_insert_cmd_header(cmd
, nargs
, cmd_parms
,
351 aishdr
= ais_copy_image(params
, aishdr
);
353 /* Add Jmp & Close */
354 *aishdr
++ = AIS_CMD_JMPCLOSE
;
355 *aishdr
++ = params
->ep
;
357 size
= (aishdr
- (uint32_t *)tparams
->hdr
) * sizeof(uint32_t);
358 tparams
->header_size
= size
;
363 static int aisimage_check_image_types(uint8_t type
)
365 if (type
== IH_TYPE_AISIMAGE
)
371 static int aisimage_verify_header(unsigned char *ptr
, int image_size
,
372 struct image_tool_params
*params
)
374 struct ais_header
*ais_hdr
= (struct ais_header
*)ptr
;
376 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
)
377 return -FDT_ERR_BADSTRUCTURE
;
379 /* Store the total size to remember in print_hdr */
380 ais_img_size
= image_size
;
385 static void aisimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
386 struct image_tool_params
*params
)
390 int aisimage_check_params(struct image_tool_params
*params
)
394 if (!strlen(params
->imagename
)) {
395 fprintf(stderr
, "Error: %s - Configuration file not specified, "
396 "it is needed for aisimage generation\n",
402 * XIP is not allowed and verify that incompatible
403 * parameters are not sent at the same time
404 * For example, if list is required a data image must not be provided
406 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
407 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
408 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
409 (params
->xflag
) || !(strlen(params
->imagename
));
413 * aisimage parameters
417 "TI Davinci AIS Boot Image support",
420 aisimage_check_params
,
421 aisimage_verify_header
,
422 aisimage_print_header
,
425 aisimage_check_image_types
,