3 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
7 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
10 * Marvell Semiconductor <www.marvell.com>
11 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
13 * SPDX-License-Identifier: GPL-2.0+
16 /* Required to obtain the getline prototype from stdio.h */
24 * Supported commands for configuration file
26 static table_entry_t ublimage_cmds[] = {
27 {CMD_BOOT_MODE, "MODE", "UBL special modes", },
28 {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
30 "number of pages (size of bootloader)", },
31 {CMD_ST_BLOCK, "START_BLOCK",
32 "block number where bootloader is present", },
33 {CMD_ST_PAGE, "START_PAGE",
34 "page number where bootloader is present", },
35 {CMD_LD_ADDR, "LD_ADDR",
41 * Supported Boot options for configuration file
42 * this is needed to set the correct flash offset
44 static table_entry_t ublimage_bootops[] = {
45 {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
46 {-1, "", "Invalid", },
49 static struct ubl_header ublimage_header;
51 static uint32_t get_cfg_value(char *token, char *name, int linenr)
57 value = strtoul(token, &endptr, 16);
58 if (errno || (token == endptr)) {
59 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
66 static void print_hdr(struct ubl_header *ubl_hdr)
68 printf("Image Type : Davinci UBL Boot Image\n");
69 printf("UBL magic : %08x\n", ubl_hdr->magic);
70 printf("Entry Point: %08x\n", ubl_hdr->entry);
71 printf("nr of pages: %08x\n", ubl_hdr->pages);
72 printf("start block: %08x\n", ubl_hdr->block);
73 printf("start page : %08x\n", ubl_hdr->page);
76 static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
77 char *name, int lineno, int fld, int dcd_len)
79 static int cmd_ver_first = ~0;
83 ublhdr->magic = get_table_entry_id(ublimage_bootops,
84 "ublimage special boot mode", token);
85 if (ublhdr->magic == -1) {
86 fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
87 "(%s)\n", name, lineno, token);
90 ublhdr->magic += UBL_MAGIC_BASE;
91 if (unlikely(cmd_ver_first != 1))
95 ublhdr->entry = get_cfg_value(token, name, lineno);
98 ublhdr->pages = get_cfg_value(token, name, lineno);
101 ublhdr->block = get_cfg_value(token, name, lineno);
104 ublhdr->page = get_cfg_value(token, name, lineno);
107 ublhdr->pll_m = get_cfg_value(token, name, lineno);
112 static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
113 char *token, char *name, int lineno, int fld, int *dcd_len)
118 *cmd = get_table_entry_id(ublimage_cmds,
119 "ublimage commands", token);
121 fprintf(stderr, "Error: %s[%d] - Invalid command"
122 "(%s)\n", name, lineno, token);
127 parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
133 static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
137 char *token, *saveptr1, *saveptr2;
140 char *ptr = (char *)ublhdr;
145 int ublhdrlen = sizeof(struct ubl_header);
147 fd = fopen(name, "r");
149 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
153 /* Fill header with 0xff */
154 for (i = 0; i < ublhdrlen; i++) {
160 * Very simple parsing, line starting with # are comments
163 while ((getline(&line, &len, fd)) > 0) {
166 token = strtok_r(line, "\r\n", &saveptr1);
170 /* Check inside the single line */
171 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
172 line = token; ; line = NULL, fld++) {
173 token = strtok_r(line, " \t", &saveptr2);
177 /* Drop all text starting with '#' as comments */
181 parse_cfg_fld(ublhdr, &cmd, token, name,
182 lineno, fld, &dcd_len);
190 static int ublimage_check_image_types(uint8_t type)
192 if (type == IH_TYPE_UBLIMAGE)
198 static int ublimage_verify_header(unsigned char *ptr, int image_size,
199 struct mkimage_params *params)
201 struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
203 if ((ubl_hdr->magic & 0xFFFFFF00) != UBL_MAGIC_BASE)
209 static void ublimage_print_header(const void *ptr)
211 struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
216 static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
217 struct mkimage_params *params)
219 struct ubl_header *ublhdr = (struct ubl_header *)ptr;
221 /* Parse configuration file */
222 parse_cfg_file(ublhdr, params->imagename);
225 int ublimage_check_params(struct mkimage_params *params)
229 if (!strlen(params->imagename)) {
230 fprintf(stderr, "Error: %s - Configuration file not"
231 "specified, it is needed for ublimage generation\n",
237 * XIP is not allowed and verify that incompatible
238 * parameters are not sent at the same time
239 * For example, if list is required a data image must not be provided
241 return (params->dflag && (params->fflag || params->lflag)) ||
242 (params->fflag && (params->dflag || params->lflag)) ||
243 (params->lflag && (params->dflag || params->fflag)) ||
244 (params->xflag) || !(strlen(params->imagename));
248 * ublimage parameters
250 static struct image_type_params ublimage_params = {
251 .name = "Davinci UBL boot support",
252 .header_size = sizeof(struct ubl_header),
253 .hdr = (void *)&ublimage_header,
254 .check_image_type = ublimage_check_image_types,
255 .verify_header = ublimage_verify_header,
256 .print_header = ublimage_print_header,
257 .set_header = ublimage_set_header,
258 .check_params = ublimage_check_params,
261 void init_ubl_image_type(void)
263 mkimage_register(&ublimage_params);