xref: /openbmc/u-boot/tools/aisimage.c (revision 83d290c56fab2d38cd1ab4c4cc7099559c1d5046)
1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
24962e38eSStefano Babic /*
34962e38eSStefano Babic  * (C) Copyright 2011
44962e38eSStefano Babic  * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
54962e38eSStefano Babic  */
64962e38eSStefano Babic 
7f86ed6a8SGuilherme Maciel Ferreira #include "imagetool.h"
84962e38eSStefano Babic #include "aisimage.h"
94962e38eSStefano Babic #include <image.h>
104962e38eSStefano Babic 
114962e38eSStefano Babic #define IS_FNC_EXEC(c)	(cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD)
124962e38eSStefano Babic #define WORD_ALIGN0	4
134962e38eSStefano Babic #define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1))
144962e38eSStefano Babic #define MAX_CMD_BUFFER	4096
154962e38eSStefano Babic 
164962e38eSStefano Babic static uint32_t ais_img_size;
174962e38eSStefano Babic 
184962e38eSStefano Babic /*
194962e38eSStefano Babic  * Supported commands for configuration file
204962e38eSStefano Babic  */
214962e38eSStefano Babic static table_entry_t aisimage_cmds[] = {
224962e38eSStefano Babic 	{CMD_DATA,		"DATA",		"Reg Write Data"},
234962e38eSStefano Babic 	{CMD_FILL,		"FILL",		"Fill range with pattern"},
244962e38eSStefano Babic 	{CMD_CRCON,		"CRCON",	"CRC Enable"},
254962e38eSStefano Babic 	{CMD_CRCOFF,		"CRCOFF",	"CRC Disable"},
264962e38eSStefano Babic 	{CMD_CRCCHECK,		"CRCCHECK",	"CRC Validate"},
274962e38eSStefano Babic 	{CMD_JMPCLOSE,		"JMPCLOSE",	"Jump & Close"},
284962e38eSStefano Babic 	{CMD_JMP,		"JMP",		"Jump"},
294962e38eSStefano Babic 	{CMD_SEQREAD,		"SEQREAD",	"Sequential read"},
304962e38eSStefano Babic 	{CMD_PLL0,		"PLL0",		"PLL0"},
314962e38eSStefano Babic 	{CMD_PLL1,		"PLL1",		"PLL1"},
324962e38eSStefano Babic 	{CMD_CLK,		"CLK",		"Clock configuration"},
334962e38eSStefano Babic 	{CMD_DDR2,		"DDR2",		"DDR2 Configuration"},
344962e38eSStefano Babic 	{CMD_EMIFA,		"EMIFA",	"EMIFA"},
354962e38eSStefano Babic 	{CMD_EMIFA_ASYNC,	"EMIFA_ASYNC",	"EMIFA Async"},
364962e38eSStefano Babic 	{CMD_PLL,		"PLL",		"PLL & Clock configuration"},
374962e38eSStefano Babic 	{CMD_PSC,		"PSC",		"PSC setup"},
384962e38eSStefano Babic 	{CMD_PINMUX,		"PINMUX",	"Pinmux setup"},
394962e38eSStefano Babic 	{CMD_BOOTTABLE,		"BOOT_TABLE",	"Boot table command"},
404962e38eSStefano Babic 	{-1,			"",		""},
414962e38eSStefano Babic };
424962e38eSStefano Babic 
434962e38eSStefano Babic static struct ais_func_exec {
444962e38eSStefano Babic 	uint32_t index;
454962e38eSStefano Babic 	uint32_t argcnt;
464962e38eSStefano Babic } ais_func_table[] = {
474962e38eSStefano Babic 	[CMD_PLL0] = {0, 2},
484962e38eSStefano Babic 	[CMD_PLL1] = {1, 2},
494962e38eSStefano Babic 	[CMD_CLK] = {2, 1},
504962e38eSStefano Babic 	[CMD_DDR2] = {3, 8},
514962e38eSStefano Babic 	[CMD_EMIFA] = {4, 5},
524962e38eSStefano Babic 	[CMD_EMIFA_ASYNC] = {5, 5},
534962e38eSStefano Babic 	[CMD_PLL] = {6, 3},
544962e38eSStefano Babic 	[CMD_PSC] = {7, 1},
554962e38eSStefano Babic 	[CMD_PINMUX] = {8, 3}
564962e38eSStefano Babic };
574962e38eSStefano Babic 
584962e38eSStefano Babic static struct cmd_table_t {
594962e38eSStefano Babic 	uint32_t nargs;
604962e38eSStefano Babic 	uint32_t AIS_cmd;
614962e38eSStefano Babic } cmd_table[] = {
624962e38eSStefano Babic 	[CMD_FILL]	 =	{	4,	AIS_CMD_FILL},
634962e38eSStefano Babic 	[CMD_CRCON]	=	{	0,	AIS_CMD_ENCRC},
644962e38eSStefano Babic 	[CMD_CRCOFF]	=	{	0,	AIS_CMD_DISCRC},
654962e38eSStefano Babic 	[CMD_CRCCHECK]	=	{	2,	AIS_CMD_ENCRC},
664962e38eSStefano Babic 	[CMD_JMPCLOSE]	=	{	1,	AIS_CMD_JMPCLOSE},
674962e38eSStefano Babic 	[CMD_JMP]	=	{	1,	AIS_CMD_JMP},
684962e38eSStefano Babic 	[CMD_SEQREAD]	=	{	0,	AIS_CMD_SEQREAD},
694962e38eSStefano Babic 	[CMD_PLL0]	=	{	2,	AIS_CMD_FNLOAD},
704962e38eSStefano Babic 	[CMD_PLL1]	=	{	2,	AIS_CMD_FNLOAD},
714962e38eSStefano Babic 	[CMD_CLK]	=	{	1,	AIS_CMD_FNLOAD},
724962e38eSStefano Babic 	[CMD_DDR2]	=	{	8,	AIS_CMD_FNLOAD},
734962e38eSStefano Babic 	[CMD_EMIFA]	=	{	5,	AIS_CMD_FNLOAD},
744962e38eSStefano Babic 	[CMD_EMIFA_ASYNC] =	{	5,	AIS_CMD_FNLOAD},
754962e38eSStefano Babic 	[CMD_PLL]	=	{	3,	AIS_CMD_FNLOAD},
764962e38eSStefano Babic 	[CMD_PSC]	=	{	1,	AIS_CMD_FNLOAD},
774962e38eSStefano Babic 	[CMD_PINMUX]	=	{	3,	AIS_CMD_FNLOAD},
784962e38eSStefano Babic 	[CMD_BOOTTABLE]	=	{	4,	AIS_CMD_BOOTTBL},
794962e38eSStefano Babic };
804962e38eSStefano Babic 
get_cfg_value(char * token,char * name,int linenr)814962e38eSStefano Babic static uint32_t get_cfg_value(char *token, char *name,  int linenr)
824962e38eSStefano Babic {
834962e38eSStefano Babic 	char *endptr;
844962e38eSStefano Babic 	uint32_t value;
854962e38eSStefano Babic 
864962e38eSStefano Babic 	errno = 0;
874962e38eSStefano Babic 	value = strtoul(token, &endptr, 16);
884962e38eSStefano Babic 	if (errno || (token == endptr)) {
894962e38eSStefano Babic 		fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
904962e38eSStefano Babic 			name,  linenr, token);
914962e38eSStefano Babic 		exit(EXIT_FAILURE);
924962e38eSStefano Babic 	}
934962e38eSStefano Babic 	return value;
944962e38eSStefano Babic }
954962e38eSStefano Babic 
get_ais_table_id(uint32_t * ptr)964962e38eSStefano Babic static int get_ais_table_id(uint32_t *ptr)
974962e38eSStefano Babic {
984962e38eSStefano Babic 
994962e38eSStefano Babic 	int i;
1004962e38eSStefano Babic 	int func_no;
1014962e38eSStefano Babic 
1024962e38eSStefano Babic 	for (i = 0; i < ARRAY_SIZE(cmd_table); i++) {
1034962e38eSStefano Babic 		if (*ptr == cmd_table[i].AIS_cmd) {
1044962e38eSStefano Babic 			if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD)
1054962e38eSStefano Babic 				return i;
1064962e38eSStefano Babic 
1074962e38eSStefano Babic 			func_no = ((struct ais_cmd_func *)ptr)->func_args
1084962e38eSStefano Babic 				& 0xFFFF;
1094962e38eSStefano Babic 			if (func_no == ais_func_table[i].index)
1104962e38eSStefano Babic 				return i;
1114962e38eSStefano Babic 		}
1124962e38eSStefano Babic 	}
1134962e38eSStefano Babic 
1144962e38eSStefano Babic 	return -1;
1154962e38eSStefano Babic }
1164962e38eSStefano Babic 
aisimage_print_header(const void * hdr)1174962e38eSStefano Babic static void aisimage_print_header(const void *hdr)
1184962e38eSStefano Babic {
1194962e38eSStefano Babic 	struct ais_header *ais_hdr = (struct ais_header *)hdr;
1204962e38eSStefano Babic 	uint32_t *ptr;
1214962e38eSStefano Babic 	struct ais_cmd_load *ais_load;
1224962e38eSStefano Babic 	int id;
1234962e38eSStefano Babic 
1244962e38eSStefano Babic 	if (ais_hdr->magic != AIS_MAGIC_WORD) {
1254962e38eSStefano Babic 		fprintf(stderr, "Error: - AIS Magic Number not found\n");
1264962e38eSStefano Babic 		return;
1274962e38eSStefano Babic 	}
1284962e38eSStefano Babic 	fprintf(stdout, "Image Type:   TI Davinci AIS Boot Image\n");
1294962e38eSStefano Babic 	fprintf(stdout, "AIS magic :   %08x\n", ais_hdr->magic);
1304962e38eSStefano Babic 	ptr = (uint32_t *)&ais_hdr->magic;
1314962e38eSStefano Babic 	ptr++;
1324962e38eSStefano Babic 
1334962e38eSStefano Babic 	while (*ptr != AIS_CMD_JMPCLOSE) {
1344962e38eSStefano Babic 		/* Check if we find the image */
1354962e38eSStefano Babic 		if (*ptr == AIS_CMD_LOAD) {
1364962e38eSStefano Babic 			ais_load = (struct ais_cmd_load *)ptr;
1374962e38eSStefano Babic 			fprintf(stdout, "Image at  :   0x%08x size 0x%08x\n",
1384962e38eSStefano Babic 				ais_load->addr,
1394962e38eSStefano Babic 				ais_load->size);
1404962e38eSStefano Babic 			ptr = ais_load->data + ais_load->size / sizeof(*ptr);
1414962e38eSStefano Babic 			continue;
1424962e38eSStefano Babic 		}
1434962e38eSStefano Babic 
1444962e38eSStefano Babic 		id = get_ais_table_id(ptr);
1454962e38eSStefano Babic 		if (id < 0) {
1464962e38eSStefano Babic 			fprintf(stderr, "Error: -  AIS Image corrupted\n");
1474962e38eSStefano Babic 			return;
1484962e38eSStefano Babic 		}
1494962e38eSStefano Babic 		fprintf(stdout, "AIS cmd   :   %s\n",
1504962e38eSStefano Babic 			get_table_entry_name(aisimage_cmds, NULL, id));
1514962e38eSStefano Babic 		ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1;
1524962e38eSStefano Babic 		if (((void *)ptr - hdr) > ais_img_size) {
1534962e38eSStefano Babic 			fprintf(stderr,
1544962e38eSStefano Babic 				"AIS Image not terminated by JMPCLOSE\n");
1554962e38eSStefano Babic 			return;
1564962e38eSStefano Babic 		}
1574962e38eSStefano Babic 	}
1584962e38eSStefano Babic }
1594962e38eSStefano Babic 
ais_insert_cmd_header(uint32_t cmd,uint32_t nargs,uint32_t * parms,struct image_type_params * tparams,uint32_t * ptr)1604962e38eSStefano Babic static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs,
1614962e38eSStefano Babic 	uint32_t *parms, struct image_type_params *tparams,
16294e9d4c3SSimon Glass 	uint32_t *ptr)
1634962e38eSStefano Babic {
1644962e38eSStefano Babic 	int i;
1654962e38eSStefano Babic 
1664962e38eSStefano Babic 	*ptr++ = cmd_table[cmd].AIS_cmd;
1674962e38eSStefano Babic 	if (IS_FNC_EXEC(cmd))
1684962e38eSStefano Babic 		*ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index;
1694962e38eSStefano Babic 
1704962e38eSStefano Babic 	/* Copy parameters */
1714962e38eSStefano Babic 	for (i = 0; i < nargs; i++)
1724962e38eSStefano Babic 		*ptr++ = cpu_to_le32(parms[i]);
1734962e38eSStefano Babic 
1744962e38eSStefano Babic 	return ptr;
1754962e38eSStefano Babic 
1764962e38eSStefano Babic }
1774962e38eSStefano Babic 
ais_alloc_buffer(struct image_tool_params * params)178f86ed6a8SGuilherme Maciel Ferreira static uint32_t *ais_alloc_buffer(struct image_tool_params *params)
1794962e38eSStefano Babic {
1804962e38eSStefano Babic 	int dfd;
1814962e38eSStefano Babic 	struct stat sbuf;
1824962e38eSStefano Babic 	char *datafile = params->datafile;
1834962e38eSStefano Babic 	uint32_t *ptr;
1844962e38eSStefano Babic 
1854962e38eSStefano Babic 	dfd = open(datafile, O_RDONLY|O_BINARY);
1864962e38eSStefano Babic 	if (dfd < 0) {
1874962e38eSStefano Babic 		fprintf(stderr, "%s: Can't open %s: %s\n",
1884962e38eSStefano Babic 			params->cmdname, datafile, strerror(errno));
1894962e38eSStefano Babic 		exit(EXIT_FAILURE);
1904962e38eSStefano Babic 	}
1914962e38eSStefano Babic 
1924962e38eSStefano Babic 	if (fstat(dfd, &sbuf) < 0) {
1934962e38eSStefano Babic 		fprintf(stderr, "%s: Can't stat %s: %s\n",
1944962e38eSStefano Babic 			params->cmdname, datafile, strerror(errno));
1954962e38eSStefano Babic 		exit(EXIT_FAILURE);
1964962e38eSStefano Babic 	}
1974962e38eSStefano Babic 
1984962e38eSStefano Babic 	/*
1994962e38eSStefano Babic 	 * Place for header is allocated. The size is taken from
2004962e38eSStefano Babic 	 * the size of the datafile, that the ais_image_generate()
2014962e38eSStefano Babic 	 * will copy into the header. Copying the datafile
2024962e38eSStefano Babic 	 * is not left to the main program, because after the datafile
2034962e38eSStefano Babic 	 * the header must be terminated with the Jump & Close command.
2044962e38eSStefano Babic 	 */
2054962e38eSStefano Babic 	ais_img_size = WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER;
2064962e38eSStefano Babic 	ptr = (uint32_t *)malloc(WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER);
2074962e38eSStefano Babic 	if (!ptr) {
2084962e38eSStefano Babic 		fprintf(stderr, "%s: malloc return failure: %s\n",
2094962e38eSStefano Babic 			params->cmdname, strerror(errno));
2104962e38eSStefano Babic 		exit(EXIT_FAILURE);
2114962e38eSStefano Babic 	}
2124962e38eSStefano Babic 
2134962e38eSStefano Babic 	close(dfd);
2144962e38eSStefano Babic 
2154962e38eSStefano Babic 	return ptr;
2164962e38eSStefano Babic }
2174962e38eSStefano Babic 
ais_copy_image(struct image_tool_params * params,uint32_t * aisptr)218f86ed6a8SGuilherme Maciel Ferreira static uint32_t *ais_copy_image(struct image_tool_params *params,
2194962e38eSStefano Babic 	uint32_t *aisptr)
2204962e38eSStefano Babic 
2214962e38eSStefano Babic {
2224962e38eSStefano Babic 	int dfd;
2234962e38eSStefano Babic 	struct stat sbuf;
2244962e38eSStefano Babic 	char *datafile = params->datafile;
2254962e38eSStefano Babic 	void *ptr;
2264962e38eSStefano Babic 
2274962e38eSStefano Babic 	dfd = open(datafile, O_RDONLY|O_BINARY);
2284962e38eSStefano Babic 	if (dfd < 0) {
2294962e38eSStefano Babic 		fprintf(stderr, "%s: Can't open %s: %s\n",
2304962e38eSStefano Babic 			params->cmdname, datafile, strerror(errno));
2314962e38eSStefano Babic 		exit(EXIT_FAILURE);
2324962e38eSStefano Babic 	}
2334962e38eSStefano Babic 
2344962e38eSStefano Babic 	if (fstat(dfd, &sbuf) < 0) {
2354962e38eSStefano Babic 		fprintf(stderr, "%s: Can't stat %s: %s\n",
2364962e38eSStefano Babic 			params->cmdname, datafile, strerror(errno));
2374962e38eSStefano Babic 		exit(EXIT_FAILURE);
2384962e38eSStefano Babic 	}
2394962e38eSStefano Babic 
2404962e38eSStefano Babic 	ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0);
2414962e38eSStefano Babic 	*aisptr++ = AIS_CMD_LOAD;
2424962e38eSStefano Babic 	*aisptr++ = params->ep;
2434962e38eSStefano Babic 	*aisptr++ = sbuf.st_size;
2444962e38eSStefano Babic 	memcpy((void *)aisptr, ptr, sbuf.st_size);
2454962e38eSStefano Babic 	aisptr += WORD_ALIGN(sbuf.st_size) / sizeof(uint32_t);
2464962e38eSStefano Babic 
2474962e38eSStefano Babic 	(void) munmap((void *)ptr, sbuf.st_size);
2484962e38eSStefano Babic 	(void) close(dfd);
2494962e38eSStefano Babic 
2504962e38eSStefano Babic 	return aisptr;
2514962e38eSStefano Babic 
2524962e38eSStefano Babic }
2534962e38eSStefano Babic 
aisimage_generate(struct image_tool_params * params,struct image_type_params * tparams)254f86ed6a8SGuilherme Maciel Ferreira static int aisimage_generate(struct image_tool_params *params,
2554962e38eSStefano Babic 	struct image_type_params *tparams)
2564962e38eSStefano Babic {
2574962e38eSStefano Babic 	FILE *fd = NULL;
2584962e38eSStefano Babic 	char *line = NULL;
2594962e38eSStefano Babic 	char *token, *saveptr1, *saveptr2;
2604962e38eSStefano Babic 	int lineno = 0;
2614962e38eSStefano Babic 	int fld;
2624962e38eSStefano Babic 	size_t len;
2634962e38eSStefano Babic 	int32_t cmd;
2644962e38eSStefano Babic 	uint32_t nargs, cmd_parms[10];
2654962e38eSStefano Babic 	uint32_t value, size;
2664962e38eSStefano Babic 	char *name = params->imagename;
26794e9d4c3SSimon Glass 	uint32_t *aishdr;
2684962e38eSStefano Babic 
2694962e38eSStefano Babic 	fd = fopen(name, "r");
2704962e38eSStefano Babic 	if (fd == 0) {
2714962e38eSStefano Babic 		fprintf(stderr,
2724962e38eSStefano Babic 			"Error: %s - Can't open AIS configuration\n", name);
2734962e38eSStefano Babic 		exit(EXIT_FAILURE);
2744962e38eSStefano Babic 	}
2754962e38eSStefano Babic 
2764962e38eSStefano Babic 	/*
2774962e38eSStefano Babic 	 * the size of the header is variable and is computed
2784962e38eSStefano Babic 	 * scanning the configuration file.
2794962e38eSStefano Babic 	 */
2804962e38eSStefano Babic 	tparams->header_size = 0;
2814962e38eSStefano Babic 
2824962e38eSStefano Babic 	/*
2834962e38eSStefano Babic 	 * Start allocating a buffer suitable for most command
2844962e38eSStefano Babic 	 * The buffer is then reallocated if it is too small
2854962e38eSStefano Babic 	 */
2864962e38eSStefano Babic 	aishdr = ais_alloc_buffer(params);
2874962e38eSStefano Babic 	tparams->hdr = aishdr;
2884962e38eSStefano Babic 	*aishdr++ = AIS_MAGIC_WORD;
2894962e38eSStefano Babic 
2904962e38eSStefano Babic 	/* Very simple parsing, line starting with # are comments
2914962e38eSStefano Babic 	 * and are dropped
2924962e38eSStefano Babic 	 */
2934962e38eSStefano Babic 	while ((getline(&line, &len, fd)) > 0) {
2944962e38eSStefano Babic 		lineno++;
2954962e38eSStefano Babic 
2964962e38eSStefano Babic 		token = strtok_r(line, "\r\n", &saveptr1);
2974962e38eSStefano Babic 		if (token == NULL)
2984962e38eSStefano Babic 			continue;
2994962e38eSStefano Babic 
3004962e38eSStefano Babic 		/* Check inside the single line */
3014962e38eSStefano Babic 		line = token;
3024962e38eSStefano Babic 		fld = CFG_COMMAND;
3034962e38eSStefano Babic 		cmd = CMD_INVALID;
3044962e38eSStefano Babic 		nargs = 0;
3054962e38eSStefano Babic 		while (token != NULL) {
3064962e38eSStefano Babic 			token = strtok_r(line, " \t", &saveptr2);
3074962e38eSStefano Babic 			if (token == NULL)
3084962e38eSStefano Babic 				break;
3094962e38eSStefano Babic 
3104962e38eSStefano Babic 			/* Drop all text starting with '#' as comments */
3114962e38eSStefano Babic 			if (token[0] == '#')
3124962e38eSStefano Babic 				break;
3134962e38eSStefano Babic 
3144962e38eSStefano Babic 			switch (fld) {
3154962e38eSStefano Babic 			case CFG_COMMAND:
3164962e38eSStefano Babic 				cmd = get_table_entry_id(aisimage_cmds,
3174962e38eSStefano Babic 					"aisimage commands", token);
3184962e38eSStefano Babic 				if (cmd < 0) {
3194962e38eSStefano Babic 					fprintf(stderr,
3204962e38eSStefano Babic 					"Error: %s[%d] - Invalid command"
3214962e38eSStefano Babic 					"(%s)\n", name, lineno, token);
3224962e38eSStefano Babic 
3234962e38eSStefano Babic 					exit(EXIT_FAILURE);
3244962e38eSStefano Babic 				}
3254962e38eSStefano Babic 				break;
3264962e38eSStefano Babic 			case CFG_VALUE:
3274962e38eSStefano Babic 				value = get_cfg_value(token, name, lineno);
3284962e38eSStefano Babic 				cmd_parms[nargs++] = value;
3294962e38eSStefano Babic 				if (nargs > cmd_table[cmd].nargs) {
3304962e38eSStefano Babic 					fprintf(stderr,
3314962e38eSStefano Babic 					 "Error: %s[%d] - too much arguments:"
3324962e38eSStefano Babic 						"(%s) for command %s\n", name,
3334962e38eSStefano Babic 						lineno, token,
3344962e38eSStefano Babic 						aisimage_cmds[cmd].sname);
3354962e38eSStefano Babic 					exit(EXIT_FAILURE);
3364962e38eSStefano Babic 				}
3374962e38eSStefano Babic 				break;
3384962e38eSStefano Babic 			}
3394962e38eSStefano Babic 			line = NULL;
3404962e38eSStefano Babic 			fld = CFG_VALUE;
3414962e38eSStefano Babic 		}
3424962e38eSStefano Babic 		if (cmd != CMD_INVALID) {
3434962e38eSStefano Babic 			/* Now insert the command into the header */
3444962e38eSStefano Babic 			aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms,
34594e9d4c3SSimon Glass 				tparams, aishdr);
3464962e38eSStefano Babic 		}
3474962e38eSStefano Babic 
3484962e38eSStefano Babic 	}
3494962e38eSStefano Babic 	fclose(fd);
3504962e38eSStefano Babic 
3514962e38eSStefano Babic 	aishdr = ais_copy_image(params, aishdr);
3524962e38eSStefano Babic 
3534962e38eSStefano Babic 	/* Add Jmp & Close */
3544962e38eSStefano Babic 	*aishdr++ = AIS_CMD_JMPCLOSE;
3554962e38eSStefano Babic 	*aishdr++ = params->ep;
3564962e38eSStefano Babic 
3574962e38eSStefano Babic 	size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t);
3584962e38eSStefano Babic 	tparams->header_size = size;
3594962e38eSStefano Babic 
3604962e38eSStefano Babic 	return 0;
3614962e38eSStefano Babic }
3624962e38eSStefano Babic 
aisimage_check_image_types(uint8_t type)3634962e38eSStefano Babic static int aisimage_check_image_types(uint8_t type)
3644962e38eSStefano Babic {
3654962e38eSStefano Babic 	if (type == IH_TYPE_AISIMAGE)
3664962e38eSStefano Babic 		return EXIT_SUCCESS;
3674962e38eSStefano Babic 	else
3684962e38eSStefano Babic 		return EXIT_FAILURE;
3694962e38eSStefano Babic }
3704962e38eSStefano Babic 
aisimage_verify_header(unsigned char * ptr,int image_size,struct image_tool_params * params)3714962e38eSStefano Babic static int aisimage_verify_header(unsigned char *ptr, int image_size,
372f86ed6a8SGuilherme Maciel Ferreira 			struct image_tool_params *params)
3734962e38eSStefano Babic {
3744962e38eSStefano Babic 	struct ais_header *ais_hdr = (struct ais_header *)ptr;
3754962e38eSStefano Babic 
3764962e38eSStefano Babic 	if (ais_hdr->magic != AIS_MAGIC_WORD)
3774962e38eSStefano Babic 		return -FDT_ERR_BADSTRUCTURE;
3784962e38eSStefano Babic 
3794962e38eSStefano Babic 	/* Store the total size to remember in print_hdr */
3804962e38eSStefano Babic 	ais_img_size = image_size;
3814962e38eSStefano Babic 
3824962e38eSStefano Babic 	return 0;
3834962e38eSStefano Babic }
3844962e38eSStefano Babic 
aisimage_set_header(void * ptr,struct stat * sbuf,int ifd,struct image_tool_params * params)3854962e38eSStefano Babic static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd,
386f86ed6a8SGuilherme Maciel Ferreira 				struct image_tool_params *params)
3874962e38eSStefano Babic {
3884962e38eSStefano Babic }
3894962e38eSStefano Babic 
aisimage_check_params(struct image_tool_params * params)390f86ed6a8SGuilherme Maciel Ferreira int aisimage_check_params(struct image_tool_params *params)
3914962e38eSStefano Babic {
3924962e38eSStefano Babic 	if (!params)
3934962e38eSStefano Babic 		return CFG_INVALID;
3944962e38eSStefano Babic 	if (!strlen(params->imagename)) {
3954962e38eSStefano Babic 		fprintf(stderr, "Error: %s - Configuration file not specified, "
3964962e38eSStefano Babic 			"it is needed for aisimage generation\n",
3974962e38eSStefano Babic 			params->cmdname);
3984962e38eSStefano Babic 		return CFG_INVALID;
3994962e38eSStefano Babic 	}
4004962e38eSStefano Babic 	/*
4014962e38eSStefano Babic 	 * Check parameters:
4024962e38eSStefano Babic 	 * XIP is not allowed and verify that incompatible
4034962e38eSStefano Babic 	 * parameters are not sent at the same time
4044962e38eSStefano Babic 	 * For example, if list is required a data image must not be provided
4054962e38eSStefano Babic 	 */
4064962e38eSStefano Babic 	return	(params->dflag && (params->fflag || params->lflag)) ||
4074962e38eSStefano Babic 		(params->fflag && (params->dflag || params->lflag)) ||
4084962e38eSStefano Babic 		(params->lflag && (params->dflag || params->fflag)) ||
4094962e38eSStefano Babic 		(params->xflag) || !(strlen(params->imagename));
4104962e38eSStefano Babic }
4114962e38eSStefano Babic 
4124962e38eSStefano Babic /*
4134962e38eSStefano Babic  * aisimage parameters
4144962e38eSStefano Babic  */
415a93648d1SGuilherme Maciel Ferreira U_BOOT_IMAGE_TYPE(
416a93648d1SGuilherme Maciel Ferreira 	aisimage,
417a93648d1SGuilherme Maciel Ferreira 	"TI Davinci AIS Boot Image support",
418a93648d1SGuilherme Maciel Ferreira 	0,
419a93648d1SGuilherme Maciel Ferreira 	NULL,
420a93648d1SGuilherme Maciel Ferreira 	aisimage_check_params,
421a93648d1SGuilherme Maciel Ferreira 	aisimage_verify_header,
422a93648d1SGuilherme Maciel Ferreira 	aisimage_print_header,
423a93648d1SGuilherme Maciel Ferreira 	aisimage_set_header,
424a93648d1SGuilherme Maciel Ferreira 	NULL,
425a93648d1SGuilherme Maciel Ferreira 	aisimage_check_image_types,
426a93648d1SGuilherme Maciel Ferreira 	NULL,
427a93648d1SGuilherme Maciel Ferreira 	aisimage_generate
428a93648d1SGuilherme Maciel Ferreira );
429