3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
8 /* Required to obtain the getline prototype from stdio.h */
15 #define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
17 #define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
18 #define MAX_CMD_BUFFER 4096
20 static uint32_t ais_img_size
;
23 * Supported commands for configuration file
25 static 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"},
47 static struct ais_func_exec
{
50 } ais_func_table
[] = {
56 [CMD_EMIFA_ASYNC
] = {5, 5},
62 static struct cmd_table_t
{
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
},
85 static uint32_t get_cfg_value(char *token
, char *name
, int linenr
)
91 value
= strtoul(token
, &endptr
, 16);
92 if (errno
|| (token
== endptr
)) {
93 fprintf(stderr
, "Error: %s[%d] - Invalid hex data(%s)\n",
100 static int get_ais_table_id(uint32_t *ptr
)
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
)
111 func_no
= ((struct ais_cmd_func
*)ptr
)->func_args
113 if (func_no
== ais_func_table
[i
].index
)
121 static void aisimage_print_header(const void *hdr
)
123 struct ais_header
*ais_hdr
= (struct ais_header
*)hdr
;
125 struct ais_cmd_load
*ais_load
;
128 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
) {
129 fprintf(stderr
, "Error: - AIS Magic Number not found\n");
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
;
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",
144 ptr
= ais_load
->data
+ ais_load
->size
/ sizeof(*ptr
);
148 id
= get_ais_table_id(ptr
);
150 fprintf(stderr
, "Error: - AIS Image corrupted\n");
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
) {
158 "AIS Image not terminated by JMPCLOSE\n");
164 static uint32_t *ais_insert_cmd_header(uint32_t cmd
, uint32_t nargs
,
165 uint32_t *parms
, struct image_type_params
*tparams
,
170 *ptr
++ = cmd_table
[cmd
].AIS_cmd
;
171 if (IS_FNC_EXEC(cmd
))
172 *ptr
++ = ((nargs
& 0xFFFF) << 16) + ais_func_table
[cmd
].index
;
174 /* Copy parameters */
175 for (i
= 0; i
< nargs
; i
++)
176 *ptr
++ = cpu_to_le32(parms
[i
]);
182 static uint32_t *ais_alloc_buffer(struct mkimage_params
*params
)
186 char *datafile
= params
->datafile
;
189 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
191 fprintf(stderr
, "%s: Can't open %s: %s\n",
192 params
->cmdname
, datafile
, strerror(errno
));
196 if (fstat(dfd
, &sbuf
) < 0) {
197 fprintf(stderr
, "%s: Can't stat %s: %s\n",
198 params
->cmdname
, datafile
, strerror(errno
));
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.
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
);
212 fprintf(stderr
, "%s: malloc return failure: %s\n",
213 params
->cmdname
, strerror(errno
));
222 static uint32_t *ais_copy_image(struct mkimage_params
*params
,
228 char *datafile
= params
->datafile
;
231 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
233 fprintf(stderr
, "%s: Can't open %s: %s\n",
234 params
->cmdname
, datafile
, strerror(errno
));
238 if (fstat(dfd
, &sbuf
) < 0) {
239 fprintf(stderr
, "%s: Can't stat %s: %s\n",
240 params
->cmdname
, datafile
, strerror(errno
));
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);
251 (void) munmap((void *)ptr
, sbuf
.st_size
);
258 static int aisimage_generate(struct mkimage_params
*params
,
259 struct image_type_params
*tparams
)
263 char *token
, *saveptr1
, *saveptr2
;
268 uint32_t nargs
, cmd_parms
[10];
269 uint32_t value
, size
;
270 char *name
= params
->imagename
;
273 fd
= fopen(name
, "r");
276 "Error: %s - Can't open AIS configuration\n", name
);
281 * the size of the header is variable and is computed
282 * scanning the configuration file.
284 tparams
->header_size
= 0;
287 * Start allocating a buffer suitable for most command
288 * The buffer is then reallocated if it is too small
290 aishdr
= ais_alloc_buffer(params
);
291 tparams
->hdr
= aishdr
;
292 *aishdr
++ = AIS_MAGIC_WORD
;
294 /* Very simple parsing, line starting with # are comments
297 while ((getline(&line
, &len
, fd
)) > 0) {
300 token
= strtok_r(line
, "\r\n", &saveptr1
);
304 /* Check inside the single line */
309 while (token
!= NULL
) {
310 token
= strtok_r(line
, " \t", &saveptr2
);
314 /* Drop all text starting with '#' as comments */
320 cmd
= get_table_entry_id(aisimage_cmds
,
321 "aisimage commands", token
);
324 "Error: %s[%d] - Invalid command"
325 "(%s)\n", name
, lineno
, token
);
331 value
= get_cfg_value(token
, name
, lineno
);
332 cmd_parms
[nargs
++] = value
;
333 if (nargs
> cmd_table
[cmd
].nargs
) {
335 "Error: %s[%d] - too much arguments:"
336 "(%s) for command %s\n", name
,
338 aisimage_cmds
[cmd
].sname
);
346 if (cmd
!= CMD_INVALID
) {
347 /* Now insert the command into the header */
348 aishdr
= ais_insert_cmd_header(cmd
, nargs
, cmd_parms
,
355 aishdr
= ais_copy_image(params
, aishdr
);
357 /* Add Jmp & Close */
358 *aishdr
++ = AIS_CMD_JMPCLOSE
;
359 *aishdr
++ = params
->ep
;
361 size
= (aishdr
- (uint32_t *)tparams
->hdr
) * sizeof(uint32_t);
362 tparams
->header_size
= size
;
367 static int aisimage_check_image_types(uint8_t type
)
369 if (type
== IH_TYPE_AISIMAGE
)
375 static int aisimage_verify_header(unsigned char *ptr
, int image_size
,
376 struct mkimage_params
*params
)
378 struct ais_header
*ais_hdr
= (struct ais_header
*)ptr
;
380 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
)
381 return -FDT_ERR_BADSTRUCTURE
;
383 /* Store the total size to remember in print_hdr */
384 ais_img_size
= image_size
;
389 static void aisimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
390 struct mkimage_params
*params
)
394 int aisimage_check_params(struct mkimage_params
*params
)
398 if (!strlen(params
->imagename
)) {
399 fprintf(stderr
, "Error: %s - Configuration file not specified, "
400 "it is needed for aisimage generation\n",
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
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
));
417 * aisimage parameters
419 static struct image_type_params aisimage_params
= {
420 .name
= "TI Davinci AIS Boot Image support",
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
,
431 void init_ais_image_type(void)
433 mkimage_register(&aisimage_params
);