3 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 /* Required to obtain the getline prototype from stdio.h */
31 #define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
33 #define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
34 #define MAX_CMD_BUFFER 4096
36 static uint32_t ais_img_size
;
39 * Supported commands for configuration file
41 static table_entry_t aisimage_cmds
[] = {
42 {CMD_DATA
, "DATA", "Reg Write Data"},
43 {CMD_FILL
, "FILL", "Fill range with pattern"},
44 {CMD_CRCON
, "CRCON", "CRC Enable"},
45 {CMD_CRCOFF
, "CRCOFF", "CRC Disable"},
46 {CMD_CRCCHECK
, "CRCCHECK", "CRC Validate"},
47 {CMD_JMPCLOSE
, "JMPCLOSE", "Jump & Close"},
48 {CMD_JMP
, "JMP", "Jump"},
49 {CMD_SEQREAD
, "SEQREAD", "Sequential read"},
50 {CMD_PLL0
, "PLL0", "PLL0"},
51 {CMD_PLL1
, "PLL1", "PLL1"},
52 {CMD_CLK
, "CLK", "Clock configuration"},
53 {CMD_DDR2
, "DDR2", "DDR2 Configuration"},
54 {CMD_EMIFA
, "EMIFA", "EMIFA"},
55 {CMD_EMIFA_ASYNC
, "EMIFA_ASYNC", "EMIFA Async"},
56 {CMD_PLL
, "PLL", "PLL & Clock configuration"},
57 {CMD_PSC
, "PSC", "PSC setup"},
58 {CMD_PINMUX
, "PINMUX", "Pinmux setup"},
59 {CMD_BOOTTABLE
, "BOOT_TABLE", "Boot table command"},
63 static struct ais_func_exec
{
66 } ais_func_table
[] = {
72 [CMD_EMIFA_ASYNC
] = {5, 5},
78 static struct cmd_table_t
{
82 [CMD_FILL
] = { 4, AIS_CMD_FILL
},
83 [CMD_CRCON
] = { 0, AIS_CMD_ENCRC
},
84 [CMD_CRCOFF
] = { 0, AIS_CMD_DISCRC
},
85 [CMD_CRCCHECK
] = { 2, AIS_CMD_ENCRC
},
86 [CMD_JMPCLOSE
] = { 1, AIS_CMD_JMPCLOSE
},
87 [CMD_JMP
] = { 1, AIS_CMD_JMP
},
88 [CMD_SEQREAD
] = { 0, AIS_CMD_SEQREAD
},
89 [CMD_PLL0
] = { 2, AIS_CMD_FNLOAD
},
90 [CMD_PLL1
] = { 2, AIS_CMD_FNLOAD
},
91 [CMD_CLK
] = { 1, AIS_CMD_FNLOAD
},
92 [CMD_DDR2
] = { 8, AIS_CMD_FNLOAD
},
93 [CMD_EMIFA
] = { 5, AIS_CMD_FNLOAD
},
94 [CMD_EMIFA_ASYNC
] = { 5, AIS_CMD_FNLOAD
},
95 [CMD_PLL
] = { 3, AIS_CMD_FNLOAD
},
96 [CMD_PSC
] = { 1, AIS_CMD_FNLOAD
},
97 [CMD_PINMUX
] = { 3, AIS_CMD_FNLOAD
},
98 [CMD_BOOTTABLE
] = { 4, AIS_CMD_BOOTTBL
},
101 static uint32_t get_cfg_value(char *token
, char *name
, int linenr
)
107 value
= strtoul(token
, &endptr
, 16);
108 if (errno
|| (token
== endptr
)) {
109 fprintf(stderr
, "Error: %s[%d] - Invalid hex data(%s)\n",
110 name
, linenr
, token
);
116 static int get_ais_table_id(uint32_t *ptr
)
122 for (i
= 0; i
< ARRAY_SIZE(cmd_table
); i
++) {
123 if (*ptr
== cmd_table
[i
].AIS_cmd
) {
124 if (cmd_table
[i
].AIS_cmd
!= AIS_CMD_FNLOAD
)
127 func_no
= ((struct ais_cmd_func
*)ptr
)->func_args
129 if (func_no
== ais_func_table
[i
].index
)
137 static void aisimage_print_header(const void *hdr
)
139 struct ais_header
*ais_hdr
= (struct ais_header
*)hdr
;
141 struct ais_cmd_load
*ais_load
;
144 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
) {
145 fprintf(stderr
, "Error: - AIS Magic Number not found\n");
148 fprintf(stdout
, "Image Type: TI Davinci AIS Boot Image\n");
149 fprintf(stdout
, "AIS magic : %08x\n", ais_hdr
->magic
);
150 ptr
= (uint32_t *)&ais_hdr
->magic
;
153 while (*ptr
!= AIS_CMD_JMPCLOSE
) {
154 /* Check if we find the image */
155 if (*ptr
== AIS_CMD_LOAD
) {
156 ais_load
= (struct ais_cmd_load
*)ptr
;
157 fprintf(stdout
, "Image at : 0x%08x size 0x%08x\n",
160 ptr
= ais_load
->data
+ ais_load
->size
/ sizeof(*ptr
);
164 id
= get_ais_table_id(ptr
);
166 fprintf(stderr
, "Error: - AIS Image corrupted\n");
169 fprintf(stdout
, "AIS cmd : %s\n",
170 get_table_entry_name(aisimage_cmds
, NULL
, id
));
171 ptr
+= cmd_table
[id
].nargs
+ IS_FNC_EXEC(id
) + 1;
172 if (((void *)ptr
- hdr
) > ais_img_size
) {
174 "AIS Image not terminated by JMPCLOSE\n");
180 static uint32_t *ais_insert_cmd_header(uint32_t cmd
, uint32_t nargs
,
181 uint32_t *parms
, struct image_type_params
*tparams
,
186 *ptr
++ = cmd_table
[cmd
].AIS_cmd
;
187 if (IS_FNC_EXEC(cmd
))
188 *ptr
++ = ((nargs
& 0xFFFF) << 16) + ais_func_table
[cmd
].index
;
190 /* Copy parameters */
191 for (i
= 0; i
< nargs
; i
++)
192 *ptr
++ = cpu_to_le32(parms
[i
]);
198 static uint32_t *ais_alloc_buffer(struct mkimage_params
*params
)
202 char *datafile
= params
->datafile
;
205 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
207 fprintf(stderr
, "%s: Can't open %s: %s\n",
208 params
->cmdname
, datafile
, strerror(errno
));
212 if (fstat(dfd
, &sbuf
) < 0) {
213 fprintf(stderr
, "%s: Can't stat %s: %s\n",
214 params
->cmdname
, datafile
, strerror(errno
));
219 * Place for header is allocated. The size is taken from
220 * the size of the datafile, that the ais_image_generate()
221 * will copy into the header. Copying the datafile
222 * is not left to the main program, because after the datafile
223 * the header must be terminated with the Jump & Close command.
225 ais_img_size
= WORD_ALIGN(sbuf
.st_size
) + MAX_CMD_BUFFER
;
226 ptr
= (uint32_t *)malloc(WORD_ALIGN(sbuf
.st_size
) + MAX_CMD_BUFFER
);
228 fprintf(stderr
, "%s: malloc return failure: %s\n",
229 params
->cmdname
, strerror(errno
));
238 static uint32_t *ais_copy_image(struct mkimage_params
*params
,
244 char *datafile
= params
->datafile
;
247 dfd
= open(datafile
, O_RDONLY
|O_BINARY
);
249 fprintf(stderr
, "%s: Can't open %s: %s\n",
250 params
->cmdname
, datafile
, strerror(errno
));
254 if (fstat(dfd
, &sbuf
) < 0) {
255 fprintf(stderr
, "%s: Can't stat %s: %s\n",
256 params
->cmdname
, datafile
, strerror(errno
));
260 ptr
= mmap(0, sbuf
.st_size
, PROT_READ
, MAP_SHARED
, dfd
, 0);
261 *aisptr
++ = AIS_CMD_LOAD
;
262 *aisptr
++ = params
->ep
;
263 *aisptr
++ = sbuf
.st_size
;
264 memcpy((void *)aisptr
, ptr
, sbuf
.st_size
);
265 aisptr
+= WORD_ALIGN(sbuf
.st_size
) / sizeof(uint32_t);
267 (void) munmap((void *)ptr
, sbuf
.st_size
);
274 static int aisimage_generate(struct mkimage_params
*params
,
275 struct image_type_params
*tparams
)
279 char *token
, *saveptr1
, *saveptr2
;
284 uint32_t nargs
, cmd_parms
[10];
285 uint32_t value
, size
;
286 char *name
= params
->imagename
;
289 fd
= fopen(name
, "r");
292 "Error: %s - Can't open AIS configuration\n", name
);
297 * the size of the header is variable and is computed
298 * scanning the configuration file.
300 tparams
->header_size
= 0;
303 * Start allocating a buffer suitable for most command
304 * The buffer is then reallocated if it is too small
306 aishdr
= ais_alloc_buffer(params
);
307 tparams
->hdr
= aishdr
;
308 *aishdr
++ = AIS_MAGIC_WORD
;
310 /* Very simple parsing, line starting with # are comments
313 while ((getline(&line
, &len
, fd
)) > 0) {
316 token
= strtok_r(line
, "\r\n", &saveptr1
);
320 /* Check inside the single line */
325 while (token
!= NULL
) {
326 token
= strtok_r(line
, " \t", &saveptr2
);
330 /* Drop all text starting with '#' as comments */
336 cmd
= get_table_entry_id(aisimage_cmds
,
337 "aisimage commands", token
);
340 "Error: %s[%d] - Invalid command"
341 "(%s)\n", name
, lineno
, token
);
347 value
= get_cfg_value(token
, name
, lineno
);
348 cmd_parms
[nargs
++] = value
;
349 if (nargs
> cmd_table
[cmd
].nargs
) {
351 "Error: %s[%d] - too much arguments:"
352 "(%s) for command %s\n", name
,
354 aisimage_cmds
[cmd
].sname
);
362 if (cmd
!= CMD_INVALID
) {
363 /* Now insert the command into the header */
364 aishdr
= ais_insert_cmd_header(cmd
, nargs
, cmd_parms
,
371 aishdr
= ais_copy_image(params
, aishdr
);
373 /* Add Jmp & Close */
374 *aishdr
++ = AIS_CMD_JMPCLOSE
;
375 *aishdr
++ = params
->ep
;
377 size
= (aishdr
- (uint32_t *)tparams
->hdr
) * sizeof(uint32_t);
378 tparams
->header_size
= size
;
383 static int aisimage_check_image_types(uint8_t type
)
385 if (type
== IH_TYPE_AISIMAGE
)
391 static int aisimage_verify_header(unsigned char *ptr
, int image_size
,
392 struct mkimage_params
*params
)
394 struct ais_header
*ais_hdr
= (struct ais_header
*)ptr
;
396 if (ais_hdr
->magic
!= AIS_MAGIC_WORD
)
397 return -FDT_ERR_BADSTRUCTURE
;
399 /* Store the total size to remember in print_hdr */
400 ais_img_size
= image_size
;
405 static void aisimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
406 struct mkimage_params
*params
)
410 int aisimage_check_params(struct mkimage_params
*params
)
414 if (!strlen(params
->imagename
)) {
415 fprintf(stderr
, "Error: %s - Configuration file not specified, "
416 "it is needed for aisimage generation\n",
422 * XIP is not allowed and verify that incompatible
423 * parameters are not sent at the same time
424 * For example, if list is required a data image must not be provided
426 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
427 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
428 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
429 (params
->xflag
) || !(strlen(params
->imagename
));
433 * aisimage parameters
435 static struct image_type_params aisimage_params
= {
436 .name
= "TI Davinci AIS Boot Image support",
439 .check_image_type
= aisimage_check_image_types
,
440 .verify_header
= aisimage_verify_header
,
441 .print_header
= aisimage_print_header
,
442 .set_header
= aisimage_set_header
,
443 .check_params
= aisimage_check_params
,
444 .vrec_header
= aisimage_generate
,
447 void init_ais_image_type(void)
449 mkimage_register(&aisimage_params
);