xref: /openbmc/u-boot/tools/ublimage.c (revision 702e6014)
1 /*
2  * (C) Copyright 2011
3  * Heiko Schocher, DENX Software Engineering, hs@denx.de.
4  *
5  * Based on:
6  * (C) Copyright 2009
7  * Stefano Babic, DENX Software Engineering, sbabic@denx.de.
8  *
9  * (C) Copyright 2008
10  * Marvell Semiconductor <www.marvell.com>
11  * Written-by: Prafulla Wadaskar <prafulla@marvell.com>
12  *
13  * See file CREDITS for list of people who contributed to this
14  * project.
15  *
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.
20  *
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.
25  *
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,
29  * MA 02111-1307 USA
30  */
31 
32 /* Required to obtain the getline prototype from stdio.h */
33 #define _GNU_SOURCE
34 
35 #include "mkimage.h"
36 #include <image.h>
37 #include "ublimage.h"
38 
39 /*
40  * Supported commands for configuration file
41  */
42 static table_entry_t ublimage_cmds[] = {
43 	{CMD_BOOT_MODE,	"MODE",		"UBL special modes", },
44 	{CMD_ENTRY,	"ENTRY",	"Entry point addr for bootloader", },
45 	{CMD_PAGE,	"PAGES",
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",
52 		"load addr", },
53 	{-1,		"",		"", },
54 };
55 
56 /*
57  * Supported Boot options for configuration file
58  * this is needed to set the correct flash offset
59  */
60 static table_entry_t ublimage_bootops[] = {
61 	{UBL_MAGIC_SAFE,	"safe",	"Safe boot mode",	},
62 	{-1,			"",	"Invalid",		},
63 };
64 
65 static struct ubl_header ublimage_header;
66 
67 static uint32_t get_cfg_value(char *token, char *name,  int linenr)
68 {
69 	char *endptr;
70 	uint32_t value;
71 
72 	errno = 0;
73 	value = strtoul(token, &endptr, 16);
74 	if (errno || (token == endptr)) {
75 		fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
76 			name,  linenr, token);
77 		exit(EXIT_FAILURE);
78 	}
79 	return value;
80 }
81 
82 static void print_hdr(struct ubl_header *ubl_hdr)
83 {
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);
90 }
91 
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)
94 {
95 	static int cmd_ver_first = ~0;
96 
97 	switch (cmd) {
98 	case CMD_BOOT_MODE:
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);
104 			exit(EXIT_FAILURE);
105 		}
106 		ublhdr->magic += UBL_MAGIC_BASE;
107 		if (unlikely(cmd_ver_first != 1))
108 			cmd_ver_first = 0;
109 		break;
110 	case CMD_ENTRY:
111 		ublhdr->entry = get_cfg_value(token, name, lineno);
112 		break;
113 	case CMD_PAGE:
114 		ublhdr->pages = get_cfg_value(token, name, lineno);
115 		break;
116 	case CMD_ST_BLOCK:
117 		ublhdr->block = get_cfg_value(token, name, lineno);
118 		break;
119 	case CMD_ST_PAGE:
120 		ublhdr->page = get_cfg_value(token, name, lineno);
121 		break;
122 	case CMD_LD_ADDR:
123 		ublhdr->pll_m = get_cfg_value(token, name, lineno);
124 		break;
125 	}
126 }
127 
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)
130 {
131 
132 	switch (fld) {
133 	case CFG_COMMAND:
134 		*cmd = get_table_entry_id(ublimage_cmds,
135 			"ublimage commands", token);
136 		if (*cmd < 0) {
137 			fprintf(stderr, "Error: %s[%d] - Invalid command"
138 			"(%s)\n", name, lineno, token);
139 			exit(EXIT_FAILURE);
140 		}
141 		break;
142 	case CFG_REG_VALUE:
143 		parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
144 		break;
145 	default:
146 		break;
147 	}
148 }
149 static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
150 {
151 	FILE *fd = NULL;
152 	char *line = NULL;
153 	char *token, *saveptr1, *saveptr2;
154 	int lineno = 0;
155 	int	i;
156 	char *ptr = (char *)ublhdr;
157 	int fld;
158 	size_t len;
159 	int dcd_len = 0;
160 	int32_t cmd;
161 	int ublhdrlen = sizeof(struct ubl_header);
162 
163 	fd = fopen(name, "r");
164 	if (fd == 0) {
165 		fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
166 		exit(EXIT_FAILURE);
167 	}
168 
169 	/* Fill header with 0xff */
170 	for (i = 0; i < ublhdrlen; i++) {
171 		*ptr = 0xff;
172 		ptr++;
173 	}
174 
175 	/*
176 	 * Very simple parsing, line starting with # are comments
177 	 * and are dropped
178 	 */
179 	while ((getline(&line, &len, fd)) > 0) {
180 		lineno++;
181 
182 		token = strtok_r(line, "\r\n", &saveptr1);
183 		if (token == NULL)
184 			continue;
185 
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);
190 			if (token == NULL)
191 				break;
192 
193 			/* Drop all text starting with '#' as comments */
194 			if (token[0] == '#')
195 				break;
196 
197 			parse_cfg_fld(ublhdr, &cmd, token, name,
198 					lineno, fld, &dcd_len);
199 		}
200 	}
201 	fclose(fd);
202 
203 	return dcd_len;
204 }
205 
206 static int ublimage_check_image_types(uint8_t type)
207 {
208 	if (type == IH_TYPE_UBLIMAGE)
209 		return EXIT_SUCCESS;
210 	else
211 		return EXIT_FAILURE;
212 }
213 
214 static int ublimage_verify_header(unsigned char *ptr, int image_size,
215 			struct mkimage_params *params)
216 {
217 	struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
218 
219 	if ((ubl_hdr->magic & 0xFFFFFF00) != UBL_MAGIC_BASE)
220 		return -1;
221 
222 	return 0;
223 }
224 
225 static void ublimage_print_header(const void *ptr)
226 {
227 	struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
228 
229 	print_hdr(ubl_hdr);
230 }
231 
232 static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
233 				struct mkimage_params *params)
234 {
235 	struct ubl_header *ublhdr = (struct ubl_header *)ptr;
236 
237 	/* Parse configuration file */
238 	parse_cfg_file(ublhdr, params->imagename);
239 }
240 
241 int ublimage_check_params(struct mkimage_params *params)
242 {
243 	if (!params)
244 		return CFG_INVALID;
245 	if (!strlen(params->imagename)) {
246 		fprintf(stderr, "Error: %s - Configuration file not"
247 			"specified, it is needed for ublimage generation\n",
248 			params->cmdname);
249 		return CFG_INVALID;
250 	}
251 	/*
252 	 * Check parameters:
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
256 	 */
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));
261 }
262 
263 /*
264  * ublimage parameters
265  */
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,
275 };
276 
277 void init_ubl_image_type(void)
278 {
279 	mkimage_register(&ublimage_params);
280 }
281