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 #include "imagetool.h"
21 * Supported commands for configuration file
23 static table_entry_t ublimage_cmds[] = {
24 {CMD_BOOT_MODE, "MODE", "UBL special modes", },
25 {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
27 "number of pages (size of bootloader)", },
28 {CMD_ST_BLOCK, "START_BLOCK",
29 "block number where bootloader is present", },
30 {CMD_ST_PAGE, "START_PAGE",
31 "page number where bootloader is present", },
32 {CMD_LD_ADDR, "LD_ADDR",
38 * Supported Boot options for configuration file
39 * this is needed to set the correct flash offset
41 static table_entry_t ublimage_bootops[] = {
42 {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
43 {-1, "", "Invalid", },
46 static struct ubl_header ublimage_header;
48 static uint32_t get_cfg_value(char *token, char *name, int linenr)
54 value = strtoul(token, &endptr, 16);
55 if (errno || (token == endptr)) {
56 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
63 static void print_hdr(struct ubl_header *ubl_hdr)
65 printf("Image Type : Davinci UBL Boot Image\n");
66 printf("UBL magic : %08x\n", ubl_hdr->magic);
67 printf("Entry Point: %08x\n", ubl_hdr->entry);
68 printf("nr of pages: %08x\n", ubl_hdr->pages);
69 printf("start block: %08x\n", ubl_hdr->block);
70 printf("start page : %08x\n", ubl_hdr->page);
73 static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
74 char *name, int lineno, int fld, int dcd_len)
76 static int cmd_ver_first = ~0;
80 ublhdr->magic = get_table_entry_id(ublimage_bootops,
81 "ublimage special boot mode", token);
82 if (ublhdr->magic == -1) {
83 fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
84 "(%s)\n", name, lineno, token);
87 ublhdr->magic += UBL_MAGIC_BASE;
88 if (unlikely(cmd_ver_first != 1))
92 ublhdr->entry = get_cfg_value(token, name, lineno);
95 ublhdr->pages = get_cfg_value(token, name, lineno);
98 ublhdr->block = get_cfg_value(token, name, lineno);
101 ublhdr->page = get_cfg_value(token, name, lineno);
104 ublhdr->pll_m = get_cfg_value(token, name, lineno);
109 static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
110 char *token, char *name, int lineno, int fld, int *dcd_len)
115 *cmd = get_table_entry_id(ublimage_cmds,
116 "ublimage commands", token);
118 fprintf(stderr, "Error: %s[%d] - Invalid command"
119 "(%s)\n", name, lineno, token);
124 parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
130 static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
134 char *token, *saveptr1, *saveptr2;
137 char *ptr = (char *)ublhdr;
142 int ublhdrlen = sizeof(struct ubl_header);
144 fd = fopen(name, "r");
146 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
150 /* Fill header with 0xff */
151 for (i = 0; i < ublhdrlen; i++) {
157 * Very simple parsing, line starting with # are comments
160 while ((getline(&line, &len, fd)) > 0) {
163 token = strtok_r(line, "\r\n", &saveptr1);
167 /* Check inside the single line */
168 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
169 line = token; ; line = NULL, fld++) {
170 token = strtok_r(line, " \t", &saveptr2);
174 /* Drop all text starting with '#' as comments */
178 parse_cfg_fld(ublhdr, &cmd, token, name,
179 lineno, fld, &dcd_len);
187 static int ublimage_check_image_types(uint8_t type)
189 if (type == IH_TYPE_UBLIMAGE)
195 static int ublimage_verify_header(unsigned char *ptr, int image_size,
196 struct image_tool_params *params)
198 struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
200 if ((ubl_hdr->magic & 0xFFFFFF00) != UBL_MAGIC_BASE)
206 static void ublimage_print_header(const void *ptr)
208 struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
213 static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
214 struct image_tool_params *params)
216 struct ubl_header *ublhdr = (struct ubl_header *)ptr;
218 /* Parse configuration file */
219 parse_cfg_file(ublhdr, params->imagename);
222 int ublimage_check_params(struct image_tool_params *params)
226 if (!strlen(params->imagename)) {
227 fprintf(stderr, "Error: %s - Configuration file not"
228 "specified, it is needed for ublimage generation\n",
234 * XIP is not allowed and verify that incompatible
235 * parameters are not sent at the same time
236 * For example, if list is required a data image must not be provided
238 return (params->dflag && (params->fflag || params->lflag)) ||
239 (params->fflag && (params->dflag || params->lflag)) ||
240 (params->lflag && (params->dflag || params->fflag)) ||
241 (params->xflag) || !(strlen(params->imagename));
245 * ublimage parameters
247 static struct image_type_params ublimage_params = {
248 .name = "Davinci UBL boot support",
249 .header_size = sizeof(struct ubl_header),
250 .hdr = (void *)&ublimage_header,
251 .check_image_type = ublimage_check_image_types,
252 .verify_header = ublimage_verify_header,
253 .print_header = ublimage_print_header,
254 .set_header = ublimage_set_header,
255 .check_params = ublimage_check_params,
258 void init_ubl_image_type(void)
260 register_image_type(&ublimage_params);