3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
12 #define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
14 #define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
15 #define MAX_CMD_BUFFER 4096
17 static uint32_t ais_img_size
;
20 * Supported commands for configuration file
22 static table_entry_t aisimage_cmds
[] = {
23 {CMD_DATA
, "DATA", "Reg Write Data"},
24 {CMD_FILL
, "FILL", "Fill range with pattern"},
25 {CMD_CRCON
, "CRCON", "CRC Enable"},
26 {CMD_CRCOFF
, "CRCOFF", "CRC Disable"},
27 {CMD_CRCCHECK
, "CRCCHECK", "CRC Validate"},
28 {CMD_JMPCLOSE
, "JMPCLOSE", "Jump & Close"},
29 {CMD_JMP
, "JMP", "Jump"},
30 {CMD_SEQREAD
, "SEQREAD", "Sequential read"},
31 {CMD_PLL0
, "PLL0", "PLL0"},
32 {CMD_PLL1
, "PLL1", "PLL1"},
33 {CMD_CLK
, "CLK", "Clock configuration"},
34 {CMD_DDR2
, "DDR2", "DDR2 Configuration"},
35 {CMD_EMIFA
, "EMIFA", "EMIFA"},
36 {CMD_EMIFA_ASYNC
, "EMIFA_ASYNC", "EMIFA Async"},
37 {CMD_PLL
, "PLL", "PLL & Clock configuration"},
38 {CMD_PSC
, "PSC", "PSC setup"},
39 {CMD_PINMUX
, "PINMUX", "Pinmux setup"},
40 {CMD_BOOTTABLE
, "BOOT_TABLE", "Boot table command"},
44 static struct ais_func_exec
{
47 } ais_func_table
[] = {
53 [CMD_EMIFA_ASYNC
] = {5, 5},
59 static struct cmd_table_t
{
63 [CMD_FILL
] = { 4, AIS_CMD_FILL
},
64 [CMD_CRCON
] = { 0, AIS_CMD_ENCRC
},
65 [CMD_CRCOFF
] = { 0, AIS_CMD_DISCRC
},
66 [CMD_CRCCHECK
] = { 2, AIS_CMD_ENCRC
},
67 [CMD_JMPCLOSE
] = { 1, AIS_CMD_JMPCLOSE
},
68 [CMD_JMP
] = { 1, AIS_CMD_JMP
},
69 [CMD_SEQREAD
] = { 0, AIS_CMD_SEQREAD
},
70 [CMD_PLL0
] = { 2, AIS_CMD_FNLOAD
},
71 [CMD_PLL1
] = { 2, AIS_CMD_FNLOAD
},
72 [CMD_CLK
] = { 1, AIS_CMD_FNLOAD
},
73 [CMD_DDR2
] = { 8, AIS_CMD_FNLOAD
},
74 [CMD_EMIFA
] = { 5, AIS_CMD_FNLOAD
},
75 [CMD_EMIFA_ASYNC
] = { 5, AIS_CMD_FNLOAD
},
76 [CMD_PLL
] = { 3, AIS_CMD_FNLOAD
},
77 [CMD_PSC
] = { 1, AIS_CMD_FNLOAD
},
78 [CMD_PINMUX
] = { 3, AIS_CMD_FNLOAD
},
79 [CMD_BOOTTABLE
] = { 4, AIS_CMD_BOOTTBL
},
82 static uint32_t get_cfg_value(char *token
, char *name
, int linenr
)
88 value
= strtoul(token
, &endptr
, 16);
89 if (errno
|| (token
== endptr
)) {
90 fprintf(stderr
, "Error: %s[%d] - Invalid hex data(%s)\n",
97 static int get_ais_table_id(uint32_t *ptr
)
103 for (i
= 0; i
< ARRAY_SIZE(cmd_table
); i
++) {
104 if (*ptr
== cmd_table
[i
].AIS_cmd
) {
105 if (cmd_table
[i
].AIS_cmd
!= AIS_CMD_FNLOAD
)
108 func_no
= ((struct ais_cmd_func
*)ptr
)->func_args
110 if (func_no
== ais_func_table
[i
].index
)
118 static void aisimage_print_header(const void *hdr
)
120 struct ais_header
*ais_hdr
= (struct ais_header
*)hdr
;
122 struct ais_cmd_load
*ais_load
;
125 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
) {
126 fprintf(stderr
, "Error: - AIS Magic Number not found\n");
129 fprintf(stdout
, "Image Type: TI Davinci AIS Boot Image\n");
130 fprintf(stdout
, "AIS magic : %08x\n", ais_hdr
->magic
);
131 ptr
= (uint32_t *)&ais_hdr
->magic
;
134 while (*ptr
!= AIS_CMD_JMPCLOSE
) {
135 /* Check if we find the image */
136 if (*ptr
== AIS_CMD_LOAD
) {
137 ais_load
= (struct ais_cmd_load
*)ptr
;
138 fprintf(stdout
, "Image at : 0x%08x size 0x%08x\n",
141 ptr
= ais_load
->data
+ ais_load
->size
/ sizeof(*ptr
);
145 id
= get_ais_table_id(ptr
);
147 fprintf(stderr
, "Error: - AIS Image corrupted\n");
150 fprintf(stdout
, "AIS cmd : %s\n",
151 get_table_entry_name(aisimage_cmds
, NULL
, id
));
152 ptr
+= cmd_table
[id
].nargs
+ IS_FNC_EXEC(id
) + 1;
153 if (((void *)ptr
- hdr
) > ais_img_size
) {
155 "AIS Image not terminated by JMPCLOSE\n");
161 static uint32_t *ais_insert_cmd_header(uint32_t cmd
, uint32_t nargs
,
162 uint32_t *parms
, struct image_type_params
*tparams
,
167 *ptr
++ = cmd_table
[cmd
].AIS_cmd
;
168 if (IS_FNC_EXEC(cmd
))
169 *ptr
++ = ((nargs
& 0xFFFF) << 16) + ais_func_table
[cmd
].index
;
171 /* Copy parameters */
172 for (i
= 0; i
< nargs
; i
++)
173 *ptr
++ = cpu_to_le32(parms
[i
]);
179 static uint32_t *ais_alloc_buffer(struct image_tool_params
*params
)
183 char *datafile
= params
->datafile
;
186 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
188 fprintf(stderr
, "%s: Can't open %s: %s\n",
189 params
->cmdname
, datafile
, strerror(errno
));
193 if (fstat(dfd
, &sbuf
) < 0) {
194 fprintf(stderr
, "%s: Can't stat %s: %s\n",
195 params
->cmdname
, datafile
, strerror(errno
));
200 * Place for header is allocated. The size is taken from
201 * the size of the datafile, that the ais_image_generate()
202 * will copy into the header. Copying the datafile
203 * is not left to the main program, because after the datafile
204 * the header must be terminated with the Jump & Close command.
206 ais_img_size
= WORD_ALIGN(sbuf
.st_size
) + MAX_CMD_BUFFER
;
207 ptr
= (uint32_t *)malloc(WORD_ALIGN(sbuf
.st_size
) + MAX_CMD_BUFFER
);
209 fprintf(stderr
, "%s: malloc return failure: %s\n",
210 params
->cmdname
, strerror(errno
));
219 static uint32_t *ais_copy_image(struct image_tool_params
*params
,
225 char *datafile
= params
->datafile
;
228 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
230 fprintf(stderr
, "%s: Can't open %s: %s\n",
231 params
->cmdname
, datafile
, strerror(errno
));
235 if (fstat(dfd
, &sbuf
) < 0) {
236 fprintf(stderr
, "%s: Can't stat %s: %s\n",
237 params
->cmdname
, datafile
, strerror(errno
));
241 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
242 *aisptr
++ = AIS_CMD_LOAD
;
243 *aisptr
++ = params
->ep
;
244 *aisptr
++ = sbuf
.st_size
;
245 memcpy((void *)aisptr
, ptr
, sbuf
.st_size
);
246 aisptr
+= WORD_ALIGN(sbuf
.st_size
) / sizeof(uint32_t);
248 (void) munmap((void *)ptr
, sbuf
.st_size
);
255 static int aisimage_generate(struct image_tool_params
*params
,
256 struct image_type_params
*tparams
)
260 char *token
, *saveptr1
, *saveptr2
;
265 uint32_t nargs
, cmd_parms
[10];
266 uint32_t value
, size
;
267 char *name
= params
->imagename
;
270 fd
= fopen(name
, "r");
273 "Error: %s - Can't open AIS configuration\n", name
);
278 * the size of the header is variable and is computed
279 * scanning the configuration file.
281 tparams
->header_size
= 0;
284 * Start allocating a buffer suitable for most command
285 * The buffer is then reallocated if it is too small
287 aishdr
= ais_alloc_buffer(params
);
288 tparams
->hdr
= aishdr
;
289 *aishdr
++ = AIS_MAGIC_WORD
;
291 /* Very simple parsing, line starting with # are comments
294 while ((getline(&line
, &len
, fd
)) > 0) {
297 token
= strtok_r(line
, "\r\n", &saveptr1
);
301 /* Check inside the single line */
306 while (token
!= NULL
) {
307 token
= strtok_r(line
, " \t", &saveptr2
);
311 /* Drop all text starting with '#' as comments */
317 cmd
= get_table_entry_id(aisimage_cmds
,
318 "aisimage commands", token
);
321 "Error: %s[%d] - Invalid command"
322 "(%s)\n", name
, lineno
, token
);
328 value
= get_cfg_value(token
, name
, lineno
);
329 cmd_parms
[nargs
++] = value
;
330 if (nargs
> cmd_table
[cmd
].nargs
) {
332 "Error: %s[%d] - too much arguments:"
333 "(%s) for command %s\n", name
,
335 aisimage_cmds
[cmd
].sname
);
343 if (cmd
!= CMD_INVALID
) {
344 /* Now insert the command into the header */
345 aishdr
= ais_insert_cmd_header(cmd
, nargs
, cmd_parms
,
352 aishdr
= ais_copy_image(params
, aishdr
);
354 /* Add Jmp & Close */
355 *aishdr
++ = AIS_CMD_JMPCLOSE
;
356 *aishdr
++ = params
->ep
;
358 size
= (aishdr
- (uint32_t *)tparams
->hdr
) * sizeof(uint32_t);
359 tparams
->header_size
= size
;
364 static int aisimage_check_image_types(uint8_t type
)
366 if (type
== IH_TYPE_AISIMAGE
)
372 static int aisimage_verify_header(unsigned char *ptr
, int image_size
,
373 struct image_tool_params
*params
)
375 struct ais_header
*ais_hdr
= (struct ais_header
*)ptr
;
377 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
)
378 return -FDT_ERR_BADSTRUCTURE
;
380 /* Store the total size to remember in print_hdr */
381 ais_img_size
= image_size
;
386 static void aisimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
387 struct image_tool_params
*params
)
391 int aisimage_check_params(struct image_tool_params
*params
)
395 if (!strlen(params
->imagename
)) {
396 fprintf(stderr
, "Error: %s - Configuration file not specified, "
397 "it is needed for aisimage generation\n",
403 * XIP is not allowed and verify that incompatible
404 * parameters are not sent at the same time
405 * For example, if list is required a data image must not be provided
407 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
408 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
409 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
410 (params
->xflag
) || !(strlen(params
->imagename
));
414 * aisimage parameters
418 "TI Davinci AIS Boot Image support",
421 aisimage_check_params
,
422 aisimage_verify_header
,
423 aisimage_print_header
,
426 aisimage_check_image_types
,