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
35 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
37 static uint32_t ais_img_size;
40 * Supported commands for configuration file
42 static table_entry_t aisimage_cmds[] = {
43 {CMD_DATA, "DATA", "Reg Write Data"},
44 {CMD_FILL, "FILL", "Fill range with pattern"},
45 {CMD_CRCON, "CRCON", "CRC Enable"},
46 {CMD_CRCOFF, "CRCOFF", "CRC Disable"},
47 {CMD_CRCCHECK, "CRCCHECK", "CRC Validate"},
48 {CMD_JMPCLOSE, "JMPCLOSE", "Jump & Close"},
49 {CMD_JMP, "JMP", "Jump"},
50 {CMD_SEQREAD, "SEQREAD", "Sequential read"},
51 {CMD_PLL0, "PLL0", "PLL0"},
52 {CMD_PLL1, "PLL1", "PLL1"},
53 {CMD_CLK, "CLK", "Clock configuration"},
54 {CMD_DDR2, "DDR2", "DDR2 Configuration"},
55 {CMD_EMIFA, "EMIFA", "EMIFA"},
56 {CMD_EMIFA_ASYNC, "EMIFA_ASYNC", "EMIFA Async"},
57 {CMD_PLL, "PLL", "PLL & Clock configuration"},
58 {CMD_PSC, "PSC", "PSC setup"},
59 {CMD_PINMUX, "PINMUX", "Pinmux setup"},
60 {CMD_BOOTTABLE, "BOOT_TABLE", "Boot table command"},
64 static struct ais_func_exec {
67 } ais_func_table[] = {
73 [CMD_EMIFA_ASYNC] = {5, 5},
79 static struct cmd_table_t {
83 [CMD_FILL] = { 4, AIS_CMD_FILL},
84 [CMD_CRCON] = { 0, AIS_CMD_ENCRC},
85 [CMD_CRCOFF] = { 0, AIS_CMD_DISCRC},
86 [CMD_CRCCHECK] = { 2, AIS_CMD_ENCRC},
87 [CMD_JMPCLOSE] = { 1, AIS_CMD_JMPCLOSE},
88 [CMD_JMP] = { 1, AIS_CMD_JMP},
89 [CMD_SEQREAD] = { 0, AIS_CMD_SEQREAD},
90 [CMD_PLL0] = { 2, AIS_CMD_FNLOAD},
91 [CMD_PLL1] = { 2, AIS_CMD_FNLOAD},
92 [CMD_CLK] = { 1, AIS_CMD_FNLOAD},
93 [CMD_DDR2] = { 8, AIS_CMD_FNLOAD},
94 [CMD_EMIFA] = { 5, AIS_CMD_FNLOAD},
95 [CMD_EMIFA_ASYNC] = { 5, AIS_CMD_FNLOAD},
96 [CMD_PLL] = { 3, AIS_CMD_FNLOAD},
97 [CMD_PSC] = { 1, AIS_CMD_FNLOAD},
98 [CMD_PINMUX] = { 3, AIS_CMD_FNLOAD},
99 [CMD_BOOTTABLE] = { 4, AIS_CMD_BOOTTBL},
102 static uint32_t get_cfg_value(char *token, char *name, int linenr)
108 value = strtoul(token, &endptr, 16);
109 if (errno || (token == endptr)) {
110 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
111 name, linenr, token);
117 static int get_ais_table_id(uint32_t *ptr)
123 for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
124 if (*ptr == cmd_table[i].AIS_cmd) {
125 if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD)
128 func_no = ((struct ais_cmd_func *)ptr)->func_args
130 if (func_no == ais_func_table[i].index)
138 static void aisimage_print_header(const void *hdr)
140 struct ais_header *ais_hdr = (struct ais_header *)hdr;
142 struct ais_cmd_load *ais_load;
145 if (ais_hdr->magic != AIS_MAGIC_WORD) {
146 fprintf(stderr, "Error: - AIS Magic Number not found\n");
149 fprintf(stdout, "Image Type: TI Davinci AIS Boot Image\n");
150 fprintf(stdout, "AIS magic : %08x\n", ais_hdr->magic);
151 ptr = (uint32_t *)&ais_hdr->magic;
154 while (*ptr != AIS_CMD_JMPCLOSE) {
155 /* Check if we find the image */
156 if (*ptr == AIS_CMD_LOAD) {
157 ais_load = (struct ais_cmd_load *)ptr;
158 fprintf(stdout, "Image at : 0x%08x size 0x%08x\n",
161 ptr = ais_load->data + ais_load->size / sizeof(*ptr);
165 id = get_ais_table_id(ptr);
167 fprintf(stderr, "Error: - AIS Image corrupted\n");
170 fprintf(stdout, "AIS cmd : %s\n",
171 get_table_entry_name(aisimage_cmds, NULL, id));
172 ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1;
173 if (((void *)ptr - hdr) > ais_img_size) {
175 "AIS Image not terminated by JMPCLOSE\n");
181 static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
182 uint32_t *parms, struct image_type_params *tparams,
187 *ptr++ = cmd_table[cmd].AIS_cmd;
188 if (IS_FNC_EXEC(cmd))
189 *ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index;
191 /* Copy parameters */
192 for (i = 0; i < nargs; i++)
193 *ptr++ = cpu_to_le32(parms[i]);
199 static uint32_t *ais_alloc_buffer(struct mkimage_params *params)
203 char *datafile = params->datafile;
206 dfd = open(datafile, O_RDONLY|O_BINARY);
208 fprintf(stderr, "%s: Can't open %s: %s\n",
209 params->cmdname, datafile, strerror(errno));
213 if (fstat(dfd, &sbuf) < 0) {
214 fprintf(stderr, "%s: Can't stat %s: %s\n",
215 params->cmdname, datafile, strerror(errno));
220 * Place for header is allocated. The size is taken from
221 * the size of the datafile, that the ais_image_generate()
222 * will copy into the header. Copying the datafile
223 * is not left to the main program, because after the datafile
224 * the header must be terminated with the Jump & Close command.
226 ais_img_size = WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER;
227 ptr = (uint32_t *)malloc(WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER);
229 fprintf(stderr, "%s: malloc return failure: %s\n",
230 params->cmdname, strerror(errno));
239 static uint32_t *ais_copy_image(struct mkimage_params *params,
245 char *datafile = params->datafile;
248 dfd = open(datafile, O_RDONLY|O_BINARY);
250 fprintf(stderr, "%s: Can't open %s: %s\n",
251 params->cmdname, datafile, strerror(errno));
255 if (fstat(dfd, &sbuf) < 0) {
256 fprintf(stderr, "%s: Can't stat %s: %s\n",
257 params->cmdname, datafile, strerror(errno));
261 ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
262 *aisptr++ = AIS_CMD_LOAD;
263 *aisptr++ = params->ep;
264 *aisptr++ = sbuf.st_size;
265 memcpy((void *)aisptr, ptr, sbuf.st_size);
266 aisptr += WORD_ALIGN(sbuf.st_size) / sizeof(uint32_t);
268 (void) munmap((void *)ptr, sbuf.st_size);
275 static int aisimage_generate(struct mkimage_params *params,
276 struct image_type_params *tparams)
280 char *token, *saveptr1, *saveptr2;
285 uint32_t nargs, cmd_parms[10];
286 uint32_t value, size;
287 char *name = params->imagename;
290 fd = fopen(name, "r");
293 "Error: %s - Can't open AIS configuration\n", name);
298 * the size of the header is variable and is computed
299 * scanning the configuration file.
301 tparams->header_size = 0;
304 * Start allocating a buffer suitable for most command
305 * The buffer is then reallocated if it is too small
307 aishdr = ais_alloc_buffer(params);
308 tparams->hdr = aishdr;
309 *aishdr++ = AIS_MAGIC_WORD;
311 /* Very simple parsing, line starting with # are comments
314 while ((getline(&line, &len, fd)) > 0) {
317 token = strtok_r(line, "\r\n", &saveptr1);
321 /* Check inside the single line */
326 while (token != NULL) {
327 token = strtok_r(line, " \t", &saveptr2);
331 /* Drop all text starting with '#' as comments */
337 cmd = get_table_entry_id(aisimage_cmds,
338 "aisimage commands", token);
341 "Error: %s[%d] - Invalid command"
342 "(%s)\n", name, lineno, token);
348 value = get_cfg_value(token, name, lineno);
349 cmd_parms[nargs++] = value;
350 if (nargs > cmd_table[cmd].nargs) {
352 "Error: %s[%d] - too much arguments:"
353 "(%s) for command %s\n", name,
355 aisimage_cmds[cmd].sname);
363 if (cmd != CMD_INVALID) {
364 /* Now insert the command into the header */
365 aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms,
372 aishdr = ais_copy_image(params, aishdr);
374 /* Add Jmp & Close */
375 *aishdr++ = AIS_CMD_JMPCLOSE;
376 *aishdr++ = params->ep;
378 size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t);
379 tparams->header_size = size;
384 static int aisimage_check_image_types(uint8_t type)
386 if (type == IH_TYPE_AISIMAGE)
392 static int aisimage_verify_header(unsigned char *ptr, int image_size,
393 struct mkimage_params *params)
395 struct ais_header *ais_hdr = (struct ais_header *)ptr;
397 if (ais_hdr->magic != AIS_MAGIC_WORD)
398 return -FDT_ERR_BADSTRUCTURE;
400 /* Store the total size to remember in print_hdr */
401 ais_img_size = image_size;
406 static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
407 struct mkimage_params *params)
411 int aisimage_check_params(struct mkimage_params *params)
415 if (!strlen(params->imagename)) {
416 fprintf(stderr, "Error: %s - Configuration file not specified, "
417 "it is needed for aisimage generation\n",
423 * XIP is not allowed and verify that incompatible
424 * parameters are not sent at the same time
425 * For example, if list is required a data image must not be provided
427 return (params->dflag && (params->fflag || params->lflag)) ||
428 (params->fflag && (params->dflag || params->lflag)) ||
429 (params->lflag && (params->dflag || params->fflag)) ||
430 (params->xflag) || !(strlen(params->imagename));
434 * aisimage parameters
436 static struct image_type_params aisimage_params = {
437 .name = "TI Davinci AIS Boot Image support",
440 .check_image_type = aisimage_check_image_types,
441 .verify_header = aisimage_verify_header,
442 .print_header = aisimage_print_header,
443 .set_header = aisimage_set_header,
444 .check_params = aisimage_check_params,
445 .vrec_header = aisimage_generate,
448 void init_ais_image_type(void)
450 mkimage_register(&aisimage_params);