From 4962e38e9a4a053792722918bb11c5408549aebd Mon Sep 17 00:00:00 2001 From: Stefano Babic Date: Mon, 17 Oct 2011 00:07:43 +0000 Subject: [PATCH] mkimage: adding support for Davinci AIS image Some Davinci processors supports the Application Image Script (AIS) boot process. The patch adds the generation of the AIS image inside the mkimage tool to make possible to generate a bootable U-boot without external tools (TI Davinci AIS Generator). Signed-off-by: Stefano Babic CC: Wolfgang Denk --- common/image.c | 9 +- include/image.h | 1 + tools/Makefile | 4 +- tools/aisimage.c | 451 +++++++++++++++++++++++++++++++++++++++++++++++ tools/aisimage.h | 97 ++++++++++ tools/mkimage.c | 2 + tools/mkimage.h | 1 + 7 files changed, 560 insertions(+), 5 deletions(-) create mode 100644 tools/aisimage.c create mode 100644 tools/aisimage.h diff --git a/common/image.c b/common/image.c index 13b4a68802..555d9d9d42 100644 --- a/common/image.c +++ b/common/image.c @@ -131,19 +131,20 @@ static const table_entry_t uimage_os[] = { }; static const table_entry_t uimage_type[] = { + { IH_TYPE_AISIMAGE, "aisimage", "Davinci AIS image",}, { IH_TYPE_FILESYSTEM, "filesystem", "Filesystem Image", }, { IH_TYPE_FIRMWARE, "firmware", "Firmware", }, { IH_TYPE_FLATDT, "flat_dt", "Flat Device Tree", }, { IH_TYPE_KERNEL, "kernel", "Kernel Image", }, + { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",}, + { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",}, + { IH_TYPE_INVALID, NULL, "Invalid Image", }, { IH_TYPE_MULTI, "multi", "Multi-File Image", }, + { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",}, { IH_TYPE_RAMDISK, "ramdisk", "RAMDisk Image", }, { IH_TYPE_SCRIPT, "script", "Script", }, { IH_TYPE_STANDALONE, "standalone", "Standalone Program", }, - { IH_TYPE_IMXIMAGE, "imximage", "Freescale i.MX Boot Image",}, - { IH_TYPE_KWBIMAGE, "kwbimage", "Kirkwood Boot Image",}, - { IH_TYPE_OMAPIMAGE, "omapimage", "TI OMAP SPL With GP CH",}, { IH_TYPE_UBLIMAGE, "ublimage", "Davinci UBL image",}, - { IH_TYPE_INVALID, NULL, "Invalid Image", }, { -1, "", "", }, }; diff --git a/include/image.h b/include/image.h index 56b8b93df0..1ba866e475 100644 --- a/include/image.h +++ b/include/image.h @@ -161,6 +161,7 @@ #define IH_TYPE_IMXIMAGE 10 /* Freescale IMXBoot Image */ #define IH_TYPE_UBLIMAGE 11 /* Davinci UBL Image */ #define IH_TYPE_OMAPIMAGE 12 /* TI OMAP Config Header Image */ +#define IH_TYPE_AISIMAGE 13 /* TI Davinci AIS Image */ /* * Compression Types diff --git a/tools/Makefile b/tools/Makefile index fc741d32d1..df56a250b3 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -86,6 +86,7 @@ NOPED_OBJ_FILES-y += fit_image.o OBJ_FILES-$(CONFIG_CMD_NET) += gen_eth_addr.o OBJ_FILES-$(CONFIG_CMD_LOADS) += img2srec.o OBJ_FILES-$(CONFIG_XWAY_SWAP_BYTES) += xway-swap-bytes.o +NOPED_OBJ_FILES-y += aisimage.o NOPED_OBJ_FILES-y += kwbimage.o NOPED_OBJ_FILES-y += imximage.o NOPED_OBJ_FILES-y += omapimage.o @@ -184,7 +185,8 @@ $(obj)xway-swap-bytes$(SFX): $(obj)xway-swap-bytes.o $(HOSTCC) $(HOSTCFLAGS) $(HOSTLDFLAGS) -o $@ $^ $(HOSTSTRIP) $@ -$(obj)mkimage$(SFX): $(obj)crc32.o \ +$(obj)mkimage$(SFX): $(obj)aisimage.o \ + $(obj)crc32.o \ $(obj)default_image.o \ $(obj)fit_image.o \ $(obj)image.o \ diff --git a/tools/aisimage.c b/tools/aisimage.c new file mode 100644 index 0000000000..6a1011194a --- /dev/null +++ b/tools/aisimage.c @@ -0,0 +1,451 @@ +/* + * (C) Copyright 2011 + * Stefano Babic, DENX Software Engineering, sbabic@denx.de. + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +/* Required to obtain the getline prototype from stdio.h */ +#define _GNU_SOURCE + +#include "mkimage.h" +#include "aisimage.h" +#include + +#define IS_FNC_EXEC(c) (cmd_table[c].AIS_cmd == AIS_CMD_FNLOAD) +#define WORD_ALIGN0 4 +#define WORD_ALIGN(len) (((len)+WORD_ALIGN0-1) & ~(WORD_ALIGN0-1)) +#define MAX_CMD_BUFFER 4096 +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) + +static uint32_t ais_img_size; + +/* + * Supported commands for configuration file + */ +static table_entry_t aisimage_cmds[] = { + {CMD_DATA, "DATA", "Reg Write Data"}, + {CMD_FILL, "FILL", "Fill range with pattern"}, + {CMD_CRCON, "CRCON", "CRC Enable"}, + {CMD_CRCOFF, "CRCOFF", "CRC Disable"}, + {CMD_CRCCHECK, "CRCCHECK", "CRC Validate"}, + {CMD_JMPCLOSE, "JMPCLOSE", "Jump & Close"}, + {CMD_JMP, "JMP", "Jump"}, + {CMD_SEQREAD, "SEQREAD", "Sequential read"}, + {CMD_PLL0, "PLL0", "PLL0"}, + {CMD_PLL1, "PLL1", "PLL1"}, + {CMD_CLK, "CLK", "Clock configuration"}, + {CMD_DDR2, "DDR2", "DDR2 Configuration"}, + {CMD_EMIFA, "EMIFA", "EMIFA"}, + {CMD_EMIFA_ASYNC, "EMIFA_ASYNC", "EMIFA Async"}, + {CMD_PLL, "PLL", "PLL & Clock configuration"}, + {CMD_PSC, "PSC", "PSC setup"}, + {CMD_PINMUX, "PINMUX", "Pinmux setup"}, + {CMD_BOOTTABLE, "BOOT_TABLE", "Boot table command"}, + {-1, "", ""}, +}; + +static struct ais_func_exec { + uint32_t index; + uint32_t argcnt; +} ais_func_table[] = { + [CMD_PLL0] = {0, 2}, + [CMD_PLL1] = {1, 2}, + [CMD_CLK] = {2, 1}, + [CMD_DDR2] = {3, 8}, + [CMD_EMIFA] = {4, 5}, + [CMD_EMIFA_ASYNC] = {5, 5}, + [CMD_PLL] = {6, 3}, + [CMD_PSC] = {7, 1}, + [CMD_PINMUX] = {8, 3} +}; + +static struct cmd_table_t { + uint32_t nargs; + uint32_t AIS_cmd; +} cmd_table[] = { + [CMD_FILL] = { 4, AIS_CMD_FILL}, + [CMD_CRCON] = { 0, AIS_CMD_ENCRC}, + [CMD_CRCOFF] = { 0, AIS_CMD_DISCRC}, + [CMD_CRCCHECK] = { 2, AIS_CMD_ENCRC}, + [CMD_JMPCLOSE] = { 1, AIS_CMD_JMPCLOSE}, + [CMD_JMP] = { 1, AIS_CMD_JMP}, + [CMD_SEQREAD] = { 0, AIS_CMD_SEQREAD}, + [CMD_PLL0] = { 2, AIS_CMD_FNLOAD}, + [CMD_PLL1] = { 2, AIS_CMD_FNLOAD}, + [CMD_CLK] = { 1, AIS_CMD_FNLOAD}, + [CMD_DDR2] = { 8, AIS_CMD_FNLOAD}, + [CMD_EMIFA] = { 5, AIS_CMD_FNLOAD}, + [CMD_EMIFA_ASYNC] = { 5, AIS_CMD_FNLOAD}, + [CMD_PLL] = { 3, AIS_CMD_FNLOAD}, + [CMD_PSC] = { 1, AIS_CMD_FNLOAD}, + [CMD_PINMUX] = { 3, AIS_CMD_FNLOAD}, + [CMD_BOOTTABLE] = { 4, AIS_CMD_BOOTTBL}, +}; + +static uint32_t get_cfg_value(char *token, char *name, int linenr) +{ + char *endptr; + uint32_t value; + + errno = 0; + value = strtoul(token, &endptr, 16); + if (errno || (token == endptr)) { + fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n", + name, linenr, token); + exit(EXIT_FAILURE); + } + return value; +} + +static int get_ais_table_id(uint32_t *ptr) +{ + + int i; + int func_no; + + for (i = 0; i < ARRAY_SIZE(cmd_table); i++) { + if (*ptr == cmd_table[i].AIS_cmd) { + if (cmd_table[i].AIS_cmd != AIS_CMD_FNLOAD) + return i; + + func_no = ((struct ais_cmd_func *)ptr)->func_args + & 0xFFFF; + if (func_no == ais_func_table[i].index) + return i; + } + } + + return -1; +} + +static void aisimage_print_header(const void *hdr) +{ + struct ais_header *ais_hdr = (struct ais_header *)hdr; + uint32_t *ptr; + struct ais_cmd_load *ais_load; + int id; + + if (ais_hdr->magic != AIS_MAGIC_WORD) { + fprintf(stderr, "Error: - AIS Magic Number not found\n"); + return; + } + fprintf(stdout, "Image Type: TI Davinci AIS Boot Image\n"); + fprintf(stdout, "AIS magic : %08x\n", ais_hdr->magic); + ptr = (uint32_t *)&ais_hdr->magic; + ptr++; + + while (*ptr != AIS_CMD_JMPCLOSE) { + /* Check if we find the image */ + if (*ptr == AIS_CMD_LOAD) { + ais_load = (struct ais_cmd_load *)ptr; + fprintf(stdout, "Image at : 0x%08x size 0x%08x\n", + ais_load->addr, + ais_load->size); + ptr = ais_load->data + ais_load->size / sizeof(*ptr); + continue; + } + + id = get_ais_table_id(ptr); + if (id < 0) { + fprintf(stderr, "Error: - AIS Image corrupted\n"); + return; + } + fprintf(stdout, "AIS cmd : %s\n", + get_table_entry_name(aisimage_cmds, NULL, id)); + ptr += cmd_table[id].nargs + IS_FNC_EXEC(id) + 1; + if (((void *)ptr - hdr) > ais_img_size) { + fprintf(stderr, + "AIS Image not terminated by JMPCLOSE\n"); + return; + } + } +} + +static uint32_t *ais_insert_cmd_header(uint32_t cmd, uint32_t nargs, + uint32_t *parms, struct image_type_params *tparams, + uint32_t *ptr, uint32_t size) +{ + int i; + + *ptr++ = cmd_table[cmd].AIS_cmd; + if (IS_FNC_EXEC(cmd)) + *ptr++ = ((nargs & 0xFFFF) << 16) + ais_func_table[cmd].index; + + /* Copy parameters */ + for (i = 0; i < nargs; i++) + *ptr++ = cpu_to_le32(parms[i]); + + return ptr; + +} + +static uint32_t *ais_alloc_buffer(struct mkimage_params *params) +{ + int dfd; + struct stat sbuf; + char *datafile = params->datafile; + uint32_t *ptr; + + dfd = open(datafile, O_RDONLY|O_BINARY); + if (dfd < 0) { + fprintf(stderr, "%s: Can't open %s: %s\n", + params->cmdname, datafile, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (fstat(dfd, &sbuf) < 0) { + fprintf(stderr, "%s: Can't stat %s: %s\n", + params->cmdname, datafile, strerror(errno)); + exit(EXIT_FAILURE); + } + + /* + * Place for header is allocated. The size is taken from + * the size of the datafile, that the ais_image_generate() + * will copy into the header. Copying the datafile + * is not left to the main program, because after the datafile + * the header must be terminated with the Jump & Close command. + */ + ais_img_size = WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER; + ptr = (uint32_t *)malloc(WORD_ALIGN(sbuf.st_size) + MAX_CMD_BUFFER); + if (!ptr) { + fprintf(stderr, "%s: malloc return failure: %s\n", + params->cmdname, strerror(errno)); + exit(EXIT_FAILURE); + } + + close(dfd); + + return ptr; +} + +static uint32_t *ais_copy_image(struct mkimage_params *params, + uint32_t *aisptr) + +{ + int dfd; + struct stat sbuf; + char *datafile = params->datafile; + void *ptr; + + dfd = open(datafile, O_RDONLY|O_BINARY); + if (dfd < 0) { + fprintf(stderr, "%s: Can't open %s: %s\n", + params->cmdname, datafile, strerror(errno)); + exit(EXIT_FAILURE); + } + + if (fstat(dfd, &sbuf) < 0) { + fprintf(stderr, "%s: Can't stat %s: %s\n", + params->cmdname, datafile, strerror(errno)); + exit(EXIT_FAILURE); + } + + ptr = mmap(0, sbuf.st_size, PROT_READ, MAP_SHARED, dfd, 0); + *aisptr++ = AIS_CMD_LOAD; + *aisptr++ = params->ep; + *aisptr++ = sbuf.st_size; + memcpy((void *)aisptr, ptr, sbuf.st_size); + aisptr += WORD_ALIGN(sbuf.st_size) / sizeof(uint32_t); + + (void) munmap((void *)ptr, sbuf.st_size); + (void) close(dfd); + + return aisptr; + +} + +static int aisimage_generate(struct mkimage_params *params, + struct image_type_params *tparams) +{ + FILE *fd = NULL; + char *line = NULL; + char *token, *saveptr1, *saveptr2; + int lineno = 0; + int fld; + size_t len; + int32_t cmd; + uint32_t nargs, cmd_parms[10]; + uint32_t value, size; + char *name = params->imagename; + uint32_t *aishdr, tsize; + + fd = fopen(name, "r"); + if (fd == 0) { + fprintf(stderr, + "Error: %s - Can't open AIS configuration\n", name); + exit(EXIT_FAILURE); + } + + /* + * the size of the header is variable and is computed + * scanning the configuration file. + */ + tparams->header_size = 0; + + /* + * Start allocating a buffer suitable for most command + * The buffer is then reallocated if it is too small + */ + aishdr = ais_alloc_buffer(params); + tparams->hdr = aishdr; + *aishdr++ = AIS_MAGIC_WORD; + + /* Very simple parsing, line starting with # are comments + * and are dropped + */ + while ((getline(&line, &len, fd)) > 0) { + lineno++; + + token = strtok_r(line, "\r\n", &saveptr1); + if (token == NULL) + continue; + + /* Check inside the single line */ + line = token; + fld = CFG_COMMAND; + cmd = CMD_INVALID; + nargs = 0; + while (token != NULL) { + token = strtok_r(line, " \t", &saveptr2); + if (token == NULL) + break; + + /* Drop all text starting with '#' as comments */ + if (token[0] == '#') + break; + + switch (fld) { + case CFG_COMMAND: + cmd = get_table_entry_id(aisimage_cmds, + "aisimage commands", token); + if (cmd < 0) { + fprintf(stderr, + "Error: %s[%d] - Invalid command" + "(%s)\n", name, lineno, token); + + exit(EXIT_FAILURE); + } + break; + case CFG_VALUE: + value = get_cfg_value(token, name, lineno); + cmd_parms[nargs++] = value; + if (nargs > cmd_table[cmd].nargs) { + fprintf(stderr, + "Error: %s[%d] - too much arguments:" + "(%s) for command %s\n", name, + lineno, token, + aisimage_cmds[cmd].sname); + exit(EXIT_FAILURE); + } + break; + } + line = NULL; + fld = CFG_VALUE; + } + if (cmd != CMD_INVALID) { + /* Now insert the command into the header */ + aishdr = ais_insert_cmd_header(cmd, nargs, cmd_parms, + tparams, aishdr, tsize); + } + + } + fclose(fd); + + aishdr = ais_copy_image(params, aishdr); + + /* Add Jmp & Close */ + *aishdr++ = AIS_CMD_JMPCLOSE; + *aishdr++ = params->ep; + + size = (aishdr - (uint32_t *)tparams->hdr) * sizeof(uint32_t); + tparams->header_size = size; + + return 0; +} + +static int aisimage_check_image_types(uint8_t type) +{ + if (type == IH_TYPE_AISIMAGE) + return EXIT_SUCCESS; + else + return EXIT_FAILURE; +} + +static int aisimage_verify_header(unsigned char *ptr, int image_size, + struct mkimage_params *params) +{ + struct ais_header *ais_hdr = (struct ais_header *)ptr; + + if (ais_hdr->magic != AIS_MAGIC_WORD) + return -FDT_ERR_BADSTRUCTURE; + + /* Store the total size to remember in print_hdr */ + ais_img_size = image_size; + + return 0; +} + +static void aisimage_set_header(void *ptr, struct stat *sbuf, int ifd, + struct mkimage_params *params) +{ +} + +int aisimage_check_params(struct mkimage_params *params) +{ + if (!params) + return CFG_INVALID; + if (!strlen(params->imagename)) { + fprintf(stderr, "Error: %s - Configuration file not specified, " + "it is needed for aisimage generation\n", + params->cmdname); + return CFG_INVALID; + } + /* + * Check parameters: + * XIP is not allowed and verify that incompatible + * parameters are not sent at the same time + * For example, if list is required a data image must not be provided + */ + return (params->dflag && (params->fflag || params->lflag)) || + (params->fflag && (params->dflag || params->lflag)) || + (params->lflag && (params->dflag || params->fflag)) || + (params->xflag) || !(strlen(params->imagename)); +} + +/* + * aisimage parameters + */ +static struct image_type_params aisimage_params = { + .name = "TI Davinci AIS Boot Image support", + .header_size = 0, + .hdr = NULL, + .check_image_type = aisimage_check_image_types, + .verify_header = aisimage_verify_header, + .print_header = aisimage_print_header, + .set_header = aisimage_set_header, + .check_params = aisimage_check_params, + .vrec_header = aisimage_generate, +}; + +void init_ais_image_type(void) +{ + mkimage_register(&aisimage_params); +} diff --git a/tools/aisimage.h b/tools/aisimage.h new file mode 100644 index 0000000000..a6372b87e8 --- /dev/null +++ b/tools/aisimage.h @@ -0,0 +1,97 @@ +/* + * (C) Copyright 2011 + * Stefano Babic, DENX Software Engineering, sbabic@denx.de. + * + * See file CREDITS for list of people who contributed to this + * project. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, + * MA 02111-1307 USA + */ + +#ifndef _AISIMAGE_H_ +#define _AISIMAGE_H_ + +/* all values are for little endian systems */ +#define AIS_MAGIC_WORD 0x41504954 +#define AIS_FCN_MAX 8 + +enum { + AIS_CMD_LOAD = 0x58535901, + AIS_CMD_VALCRC = 0x58535902, + AIS_CMD_ENCRC = 0x58535903, + AIS_CMD_DISCRC = 0x58535904, + AIS_CMD_JMP = 0x58535905, + AIS_CMD_JMPCLOSE = 0x58535906, + AIS_CMD_BOOTTBL = 0x58535907, + AIS_CMD_FILL = 0x5853590A, + AIS_CMD_FNLOAD = 0x5853590D, + AIS_CMD_SEQREAD = 0x58535963, +}; + +struct ais_cmd_load { + uint32_t cmd; + uint32_t addr; + uint32_t size; + uint32_t data[1]; +}; + +struct ais_cmd_func { + uint32_t cmd; + uint32_t func_args; + uint32_t parms[AIS_FCN_MAX]; +}; + +struct ais_cmd_jmpclose { + uint32_t cmd; + uint32_t addr; +}; + +#define CMD_DATA_STR "DATA" + +enum ais_file_cmd { + CMD_INVALID, + CMD_FILL, + CMD_CRCON, + CMD_CRCOFF, + CMD_CRCCHECK, + CMD_JMPCLOSE, + CMD_JMP, + CMD_SEQREAD, + CMD_DATA, + CMD_PLL0, + CMD_PLL1, + CMD_CLK, + CMD_DDR2, + CMD_EMIFA, + CMD_EMIFA_ASYNC, + CMD_PLL, + CMD_PSC, + CMD_PINMUX, + CMD_BOOTTABLE +}; + +enum aisimage_fld_types { + CFG_INVALID = -1, + CFG_COMMAND, + CFG_VALUE, +}; + +struct ais_header { + uint32_t magic; + char data[1]; +}; + +#endif /* _AISIMAGE_H_ */ diff --git a/tools/mkimage.c b/tools/mkimage.c index c307a37615..36e28ec923 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -162,6 +162,8 @@ main (int argc, char **argv) init_default_image_type (); /* Init Davinci UBL support */ init_ubl_image_type(); + /* Init Davinci AIS support */ + init_ais_image_type(); params.cmdname = *argv; params.addr = params.ep = 0; diff --git a/tools/mkimage.h b/tools/mkimage.h index 213baf0037..5fe1a48ccf 100644 --- a/tools/mkimage.h +++ b/tools/mkimage.h @@ -147,6 +147,7 @@ void mkimage_register (struct image_type_params *tparams); * * Supported image types init functions */ +void init_ais_image_type(void); void init_kwb_image_type (void); void init_imx_image_type (void); void init_default_image_type (void); -- 2.25.1