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 * See file CREDITS for list of people who contributed to this
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
32 /* Required to obtain the getline prototype from stdio.h */
40 * Supported commands for configuration file
42 static table_entry_t ublimage_cmds[] = {
43 {CMD_BOOT_MODE, "MODE", "UBL special modes", },
44 {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
46 "number of pages (size of bootloader)", },
47 {CMD_ST_BLOCK, "START_BLOCK",
48 "block number where bootloader is present", },
49 {CMD_ST_PAGE, "START_PAGE",
50 "page number where bootloader is present", },
51 {CMD_LD_ADDR, "LD_ADDR",
57 * Supported Boot options for configuration file
58 * this is needed to set the correct flash offset
60 static table_entry_t ublimage_bootops[] = {
61 {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
62 {-1, "", "Invalid", },
65 static struct ubl_header ublimage_header;
67 static uint32_t get_cfg_value(char *token, char *name, int linenr)
73 value = strtoul(token, &endptr, 16);
74 if (errno || (token == endptr)) {
75 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
82 static void print_hdr(struct ubl_header *ubl_hdr)
84 printf("Image Type : Davinci UBL Boot Image\n");
85 printf("UBL magic : %08x\n", ubl_hdr->magic);
86 printf("Entry Point: %08x\n", ubl_hdr->entry);
87 printf("nr of pages: %08x\n", ubl_hdr->pages);
88 printf("start block: %08x\n", ubl_hdr->block);
89 printf("start page : %08x\n", ubl_hdr->page);
92 static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
93 char *name, int lineno, int fld, int dcd_len)
95 static int cmd_ver_first = ~0;
99 ublhdr->magic = get_table_entry_id(ublimage_bootops,
100 "ublimage special boot mode", token);
101 if (ublhdr->magic == -1) {
102 fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
103 "(%s)\n", name, lineno, token);
106 ublhdr->magic += UBL_MAGIC_BASE;
107 if (unlikely(cmd_ver_first != 1))
111 ublhdr->entry = get_cfg_value(token, name, lineno);
114 ublhdr->pages = get_cfg_value(token, name, lineno);
117 ublhdr->block = get_cfg_value(token, name, lineno);
120 ublhdr->page = get_cfg_value(token, name, lineno);
123 ublhdr->pll_m = get_cfg_value(token, name, lineno);
128 static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
129 char *token, char *name, int lineno, int fld, int *dcd_len)
134 *cmd = get_table_entry_id(ublimage_cmds,
135 "ublimage commands", token);
137 fprintf(stderr, "Error: %s[%d] - Invalid command"
138 "(%s)\n", name, lineno, token);
143 parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
149 static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
153 char *token, *saveptr1, *saveptr2;
156 char *ptr = (char *)ublhdr;
161 int ublhdrlen = sizeof(struct ubl_header);
163 fd = fopen(name, "r");
165 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
169 /* Fill header with 0xff */
170 for (i = 0; i < ublhdrlen; i++) {
176 * Very simple parsing, line starting with # are comments
179 while ((getline(&line, &len, fd)) > 0) {
182 token = strtok_r(line, "\r\n", &saveptr1);
186 /* Check inside the single line */
187 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
188 line = token; ; line = NULL, fld++) {
189 token = strtok_r(line, " \t", &saveptr2);
193 /* Drop all text starting with '#' as comments */
197 parse_cfg_fld(ublhdr, &cmd, token, name,
198 lineno, fld, &dcd_len);
206 static int ublimage_check_image_types(uint8_t type)
208 if (type == IH_TYPE_UBLIMAGE)
214 static int ublimage_verify_header(unsigned char *ptr, int image_size,
215 struct mkimage_params *params)
220 static void ublimage_print_header(const void *ptr)
222 struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
227 static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
228 struct mkimage_params *params)
230 struct ubl_header *ublhdr = (struct ubl_header *)ptr;
232 /* Parse configuration file */
233 parse_cfg_file(ublhdr, params->imagename);
236 int ublimage_check_params(struct mkimage_params *params)
240 if (!strlen(params->imagename)) {
241 fprintf(stderr, "Error: %s - Configuration file not"
242 "specified, it is needed for ublimage generation\n",
248 * XIP is not allowed and verify that incompatible
249 * parameters are not sent at the same time
250 * For example, if list is required a data image must not be provided
252 return (params->dflag && (params->fflag || params->lflag)) ||
253 (params->fflag && (params->dflag || params->lflag)) ||
254 (params->lflag && (params->dflag || params->fflag)) ||
255 (params->xflag) || !(strlen(params->imagename));
259 * ublimage parameters
261 static struct image_type_params ublimage_params = {
262 .name = "Davinci UBL boot support",
263 .header_size = sizeof(struct ubl_header),
264 .hdr = (void *)&ublimage_header,
265 .check_image_type = ublimage_check_image_types,
266 .verify_header = ublimage_verify_header,
267 .print_header = ublimage_print_header,
268 .set_header = ublimage_set_header,
269 .check_params = ublimage_check_params,
272 void init_ubl_image_type(void)
274 mkimage_register(&ublimage_params);