1 // SPDX-License-Identifier: GPL-2.0+
4 * Heiko Schocher, DENX Software Engineering, hs@denx.de.
8 * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
11 * Marvell Semiconductor <www.marvell.com>
12 * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
15 #include "imagetool.h"
20 * Supported commands for configuration file
22 static table_entry_t ublimage_cmds[] = {
23 {CMD_BOOT_MODE, "MODE", "UBL special modes", },
24 {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
26 "number of pages (size of bootloader)", },
27 {CMD_ST_BLOCK, "START_BLOCK",
28 "block number where bootloader is present", },
29 {CMD_ST_PAGE, "START_PAGE",
30 "page number where bootloader is present", },
31 {CMD_LD_ADDR, "LD_ADDR",
37 * Supported Boot options for configuration file
38 * this is needed to set the correct flash offset
40 static table_entry_t ublimage_bootops[] = {
41 {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
42 {-1, "", "Invalid", },
45 static struct ubl_header ublimage_header;
47 static uint32_t get_cfg_value(char *token, char *name, int linenr)
53 value = strtoul(token, &endptr, 16);
54 if (errno || (token == endptr)) {
55 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
62 static void print_hdr(struct ubl_header *ubl_hdr)
64 printf("Image Type : Davinci UBL Boot Image\n");
65 printf("UBL magic : %08x\n", ubl_hdr->magic);
66 printf("Entry Point: %08x\n", ubl_hdr->entry);
67 printf("nr of pages: %08x\n", ubl_hdr->pages);
68 printf("start block: %08x\n", ubl_hdr->block);
69 printf("start page : %08x\n", ubl_hdr->page);
72 static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
73 char *name, int lineno, int fld, int dcd_len)
75 static int cmd_ver_first = ~0;
79 ublhdr->magic = get_table_entry_id(ublimage_bootops,
80 "ublimage special boot mode", token);
81 if (ublhdr->magic == -1) {
82 fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
83 "(%s)\n", name, lineno, token);
86 ublhdr->magic += UBL_MAGIC_BASE;
87 if (unlikely(cmd_ver_first != 1))
91 ublhdr->entry = get_cfg_value(token, name, lineno);
94 ublhdr->pages = get_cfg_value(token, name, lineno);
97 ublhdr->block = get_cfg_value(token, name, lineno);
100 ublhdr->page = get_cfg_value(token, name, lineno);
103 ublhdr->pll_m = get_cfg_value(token, name, lineno);
108 static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
109 char *token, char *name, int lineno, int fld, int *dcd_len)
114 *cmd = get_table_entry_id(ublimage_cmds,
115 "ublimage commands", token);
117 fprintf(stderr, "Error: %s[%d] - Invalid command"
118 "(%s)\n", name, lineno, token);
123 parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
129 static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
133 char *token, *saveptr1, *saveptr2;
136 char *ptr = (char *)ublhdr;
141 int ublhdrlen = sizeof(struct ubl_header);
143 fd = fopen(name, "r");
145 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
149 /* Fill header with 0xff */
150 for (i = 0; i < ublhdrlen; i++) {
156 * Very simple parsing, line starting with # are comments
159 while ((getline(&line, &len, fd)) > 0) {
162 token = strtok_r(line, "\r\n", &saveptr1);
166 /* Check inside the single line */
167 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
168 line = token; ; line = NULL, fld++) {
169 token = strtok_r(line, " \t", &saveptr2);
173 /* Drop all text starting with '#' as comments */
177 parse_cfg_fld(ublhdr, &cmd, token, name,
178 lineno, fld, &dcd_len);
186 static int ublimage_check_image_types(uint8_t type)
188 if (type == IH_TYPE_UBLIMAGE)
194 static int ublimage_verify_header(unsigned char *ptr, int image_size,
195 struct image_tool_params *params)
197 struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
199 if ((ubl_hdr->magic & 0xFFFFFF00) != UBL_MAGIC_BASE)
205 static void ublimage_print_header(const void *ptr)
207 struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
212 static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
213 struct image_tool_params *params)
215 struct ubl_header *ublhdr = (struct ubl_header *)ptr;
217 /* Parse configuration file */
218 parse_cfg_file(ublhdr, params->imagename);
221 int ublimage_check_params(struct image_tool_params *params)
225 if (!strlen(params->imagename)) {
226 fprintf(stderr, "Error: %s - Configuration file not"
227 "specified, it is needed for ublimage generation\n",
233 * XIP is not allowed and verify that incompatible
234 * parameters are not sent at the same time
235 * For example, if list is required a data image must not be provided
237 return (params->dflag && (params->fflag || params->lflag)) ||
238 (params->fflag && (params->dflag || params->lflag)) ||
239 (params->lflag && (params->dflag || params->fflag)) ||
240 (params->xflag) || !(strlen(params->imagename));
244 * ublimage parameters
248 "Davinci UBL boot support",
249 sizeof(struct ubl_header),
250 (void *)&ublimage_header,
251 ublimage_check_params,
252 ublimage_verify_header,
253 ublimage_print_header,
256 ublimage_check_image_types,