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
)
217 struct ubl_header
*ubl_hdr
= (struct ubl_header
*)ptr
;
219 if ((ubl_hdr
->magic
& 0xFFFFFF00) != UBL_MAGIC_BASE
)
225 static void ublimage_print_header(const void *ptr
)
227 struct ubl_header
*ubl_hdr
= (struct ubl_header
*) ptr
;
232 static void ublimage_set_header(void *ptr
, struct stat
*sbuf
, int ifd
,
233 struct mkimage_params
*params
)
235 struct ubl_header
*ublhdr
= (struct ubl_header
*)ptr
;
237 /* Parse configuration file */
238 parse_cfg_file(ublhdr
, params
->imagename
);
241 int ublimage_check_params(struct mkimage_params
*params
)
245 if (!strlen(params
->imagename
)) {
246 fprintf(stderr
, "Error: %s - Configuration file not"
247 "specified, it is needed for ublimage generation\n",
253 * XIP is not allowed and verify that incompatible
254 * parameters are not sent at the same time
255 * For example, if list is required a data image must not be provided
257 return (params
->dflag
&& (params
->fflag
|| params
->lflag
)) ||
258 (params
->fflag
&& (params
->dflag
|| params
->lflag
)) ||
259 (params
->lflag
&& (params
->dflag
|| params
->fflag
)) ||
260 (params
->xflag
) || !(strlen(params
->imagename
));
264 * ublimage parameters
266 static struct image_type_params ublimage_params
= {
267 .name
= "Davinci UBL boot support",
268 .header_size
= sizeof(struct ubl_header
),
269 .hdr
= (void *)&ublimage_header
,
270 .check_image_type
= ublimage_check_image_types
,
271 .verify_header
= ublimage_verify_header
,
272 .print_header
= ublimage_print_header
,
273 .set_header
= ublimage_set_header
,
274 .check_params
= ublimage_check_params
,
277 void init_ubl_image_type(void)
279 mkimage_register(&ublimage_params
);