blob: 6495db6d6a8bf8488ae56e7740eaa30ed6bd38de [file] [log] [blame]
Heiko Schocher7816f2c2011-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020013 * SPDX-License-Identifier: GPL-2.0+
Heiko Schocher7816f2c2011-07-16 00:06:42 +000014 */
15
16/* Required to obtain the getline prototype from stdio.h */
17#define _GNU_SOURCE
18
19#include "mkimage.h"
20#include <image.h>
21#include "ublimage.h"
22
23/*
24 * Supported commands for configuration file
25 */
26static table_entry_t ublimage_cmds[] = {
27 {CMD_BOOT_MODE, "MODE", "UBL special modes", },
28 {CMD_ENTRY, "ENTRY", "Entry point addr for bootloader", },
29 {CMD_PAGE, "PAGES",
30 "number of pages (size of bootloader)", },
31 {CMD_ST_BLOCK, "START_BLOCK",
32 "block number where bootloader is present", },
33 {CMD_ST_PAGE, "START_PAGE",
34 "page number where bootloader is present", },
35 {CMD_LD_ADDR, "LD_ADDR",
36 "load addr", },
37 {-1, "", "", },
38};
39
40/*
41 * Supported Boot options for configuration file
42 * this is needed to set the correct flash offset
43 */
44static table_entry_t ublimage_bootops[] = {
45 {UBL_MAGIC_SAFE, "safe", "Safe boot mode", },
46 {-1, "", "Invalid", },
47};
48
49static struct ubl_header ublimage_header;
50
51static uint32_t get_cfg_value(char *token, char *name, int linenr)
52{
53 char *endptr;
54 uint32_t value;
55
56 errno = 0;
57 value = strtoul(token, &endptr, 16);
58 if (errno || (token == endptr)) {
59 fprintf(stderr, "Error: %s[%d] - Invalid hex data(%s)\n",
60 name, linenr, token);
61 exit(EXIT_FAILURE);
62 }
63 return value;
64}
65
66static void print_hdr(struct ubl_header *ubl_hdr)
67{
68 printf("Image Type : Davinci UBL Boot Image\n");
69 printf("UBL magic : %08x\n", ubl_hdr->magic);
70 printf("Entry Point: %08x\n", ubl_hdr->entry);
71 printf("nr of pages: %08x\n", ubl_hdr->pages);
72 printf("start block: %08x\n", ubl_hdr->block);
73 printf("start page : %08x\n", ubl_hdr->page);
74}
75
76static void parse_cfg_cmd(struct ubl_header *ublhdr, int32_t cmd, char *token,
77 char *name, int lineno, int fld, int dcd_len)
78{
79 static int cmd_ver_first = ~0;
80
81 switch (cmd) {
82 case CMD_BOOT_MODE:
83 ublhdr->magic = get_table_entry_id(ublimage_bootops,
84 "ublimage special boot mode", token);
85 if (ublhdr->magic == -1) {
86 fprintf(stderr, "Error: %s[%d] -Invalid boot mode"
87 "(%s)\n", name, lineno, token);
88 exit(EXIT_FAILURE);
89 }
90 ublhdr->magic += UBL_MAGIC_BASE;
91 if (unlikely(cmd_ver_first != 1))
92 cmd_ver_first = 0;
93 break;
94 case CMD_ENTRY:
95 ublhdr->entry = get_cfg_value(token, name, lineno);
96 break;
97 case CMD_PAGE:
98 ublhdr->pages = get_cfg_value(token, name, lineno);
99 break;
100 case CMD_ST_BLOCK:
101 ublhdr->block = get_cfg_value(token, name, lineno);
102 break;
103 case CMD_ST_PAGE:
104 ublhdr->page = get_cfg_value(token, name, lineno);
105 break;
106 case CMD_LD_ADDR:
107 ublhdr->pll_m = get_cfg_value(token, name, lineno);
108 break;
109 }
110}
111
112static void parse_cfg_fld(struct ubl_header *ublhdr, int32_t *cmd,
113 char *token, char *name, int lineno, int fld, int *dcd_len)
114{
115
116 switch (fld) {
117 case CFG_COMMAND:
118 *cmd = get_table_entry_id(ublimage_cmds,
119 "ublimage commands", token);
120 if (*cmd < 0) {
121 fprintf(stderr, "Error: %s[%d] - Invalid command"
122 "(%s)\n", name, lineno, token);
123 exit(EXIT_FAILURE);
124 }
125 break;
126 case CFG_REG_VALUE:
127 parse_cfg_cmd(ublhdr, *cmd, token, name, lineno, fld, *dcd_len);
128 break;
129 default:
130 break;
131 }
132}
133static uint32_t parse_cfg_file(struct ubl_header *ublhdr, char *name)
134{
135 FILE *fd = NULL;
136 char *line = NULL;
137 char *token, *saveptr1, *saveptr2;
138 int lineno = 0;
139 int i;
140 char *ptr = (char *)ublhdr;
141 int fld;
142 size_t len;
143 int dcd_len = 0;
144 int32_t cmd;
145 int ublhdrlen = sizeof(struct ubl_header);
146
147 fd = fopen(name, "r");
148 if (fd == 0) {
149 fprintf(stderr, "Error: %s - Can't open DCD file\n", name);
150 exit(EXIT_FAILURE);
151 }
152
153 /* Fill header with 0xff */
154 for (i = 0; i < ublhdrlen; i++) {
155 *ptr = 0xff;
156 ptr++;
157 }
158
159 /*
160 * Very simple parsing, line starting with # are comments
161 * and are dropped
162 */
163 while ((getline(&line, &len, fd)) > 0) {
164 lineno++;
165
166 token = strtok_r(line, "\r\n", &saveptr1);
167 if (token == NULL)
168 continue;
169
170 /* Check inside the single line */
171 for (fld = CFG_COMMAND, cmd = CMD_INVALID,
172 line = token; ; line = NULL, fld++) {
173 token = strtok_r(line, " \t", &saveptr2);
174 if (token == NULL)
175 break;
176
177 /* Drop all text starting with '#' as comments */
178 if (token[0] == '#')
179 break;
180
181 parse_cfg_fld(ublhdr, &cmd, token, name,
182 lineno, fld, &dcd_len);
183 }
184 }
185 fclose(fd);
186
187 return dcd_len;
188}
189
190static int ublimage_check_image_types(uint8_t type)
191{
192 if (type == IH_TYPE_UBLIMAGE)
193 return EXIT_SUCCESS;
194 else
195 return EXIT_FAILURE;
196}
197
198static int ublimage_verify_header(unsigned char *ptr, int image_size,
199 struct mkimage_params *params)
200{
Stefano Babic16396792011-09-15 23:50:15 +0000201 struct ubl_header *ubl_hdr = (struct ubl_header *)ptr;
202
203 if ((ubl_hdr->magic & 0xFFFFFF00) != UBL_MAGIC_BASE)
204 return -1;
205
Heiko Schocher7816f2c2011-07-16 00:06:42 +0000206 return 0;
207}
208
209static void ublimage_print_header(const void *ptr)
210{
211 struct ubl_header *ubl_hdr = (struct ubl_header *) ptr;
212
213 print_hdr(ubl_hdr);
214}
215
216static void ublimage_set_header(void *ptr, struct stat *sbuf, int ifd,
217 struct mkimage_params *params)
218{
219 struct ubl_header *ublhdr = (struct ubl_header *)ptr;
220
221 /* Parse configuration file */
222 parse_cfg_file(ublhdr, params->imagename);
223}
224
225int ublimage_check_params(struct mkimage_params *params)
226{
227 if (!params)
228 return CFG_INVALID;
229 if (!strlen(params->imagename)) {
230 fprintf(stderr, "Error: %s - Configuration file not"
231 "specified, it is needed for ublimage generation\n",
232 params->cmdname);
233 return CFG_INVALID;
234 }
235 /*
236 * Check parameters:
237 * XIP is not allowed and verify that incompatible
238 * parameters are not sent at the same time
239 * For example, if list is required a data image must not be provided
240 */
241 return (params->dflag && (params->fflag || params->lflag)) ||
242 (params->fflag && (params->dflag || params->lflag)) ||
243 (params->lflag && (params->dflag || params->fflag)) ||
244 (params->xflag) || !(strlen(params->imagename));
245}
246
247/*
248 * ublimage parameters
249 */
250static struct image_type_params ublimage_params = {
251 .name = "Davinci UBL boot support",
252 .header_size = sizeof(struct ubl_header),
253 .hdr = (void *)&ublimage_header,
254 .check_image_type = ublimage_check_image_types,
255 .verify_header = ublimage_verify_header,
256 .print_header = ublimage_print_header,
257 .set_header = ublimage_set_header,
258 .check_params = ublimage_check_params,
259};
260
261void init_ubl_image_type(void)
262{
263 mkimage_register(&ublimage_params);
264}