blob: 99874622d057c887032324ef0e4af2381fe689e5 [file] [log] [blame]
Heiko Schocher58cb84d2011-07-16 00:06:42 +00001/*
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 */
42static 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 */
60static table_entry_t ublimage_bootops[] = {
61 {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
62 {-1, "", "Invalid", },
63};
64
65static struct ubl_header ublimage_header;
66
67static 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
82static 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
92static 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
128static 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}
149static 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
206static 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
214static int ublimage_verify_header(unsigned char *ptr, int image_size,
215 struct mkimage_params *params)
216{
217 return 0;
218}
219
220static void ublimage_print_header(const void *ptr)
221{
222 struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
223
224 print_hdr(ubl_hdr);
225}
226
227static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
228 struct mkimage_params *params)
229{
230 struct ubl_header *ublhdr = (struct ubl_header *)ptr;
231
232 /* Parse configuration file */
233 parse_cfg_file(ublhdr, params->imagename);
234}
235
236int ublimage_check_params(struct mkimage_params *params)
237{
238 if (!params)
239 return CFG_INVALID;
240 if (!strlen(params->imagename)) {
241 fprintf(stderr, "Error: %s - Configuration file not"
242 "specified, it is needed for ublimage generation\n",
243 params->cmdname);
244 return CFG_INVALID;
245 }
246 /*
247 * Check parameters:
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
251 */
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));
256}
257
258/*
259 * ublimage parameters
260 */
261static 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,
270};
271
272void init_ubl_image_type(void)
273{
274 mkimage_register(&ublimage_params);
275}