Debian Codegraph @

util/nvidia/cbootimage/src/set.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2012-2014, NVIDIA CORPORATION. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * See file CREDITS for list of people who contributed to this
17 * project.
18 */
19
20 /*
21 * set.c - State setting support for the cbootimage tool
22 */
23
24 #include "set.h"
25 #include "cbootimage.h"
26 #include "crypto.h"
27 #include "data_layout.h"
28
29 /*
30 * Function prototypes
31 *
32 * ParseXXX() parses XXX in the input
33 * SetXXX() sets state based on the parsing results but does not perform
34 * any parsing of its own
35 * A ParseXXX() function may call other parse functions and set functions.
36 * A SetXXX() function may not call any parsing functions.
37 */
38 #define DEFAULT() \
39 default: \
40 printf("Unexpected token %d at line %d\n", \
41 token, __LINE__); \
42 return 1
43
44 int
45 read_from_image(char *filename,
46 uint32_t offset,
47 uint32_t max_size,
48 uint8_t **image,
49 uint32_t *actual_size,
50 file_type f_type)
51 {
52 int result = 0; /* 0 = success, 1 = failure */
53 FILE *fp;
54 struct stat stats;
55
56 fp = fopen(filename, "rb");
57 if (fp == NULL) {
58 result = 1;
59 return result;
60 }
61
62 if (fseek(fp, offset, SEEK_SET) == -1) {
63 printf("Error: Couldn't seek to %s(%d)\n", filename, offset);
64 result = 1;
65 goto cleanup;
66 }
67
68 if (stat(filename, &stats) != 0) {
69 printf("Error: Unable to query info on bootloader path %s\n",
70 filename);
71 result = 1;
72 goto cleanup;
73 }
74
75 if (f_type == file_type_bl) {
76 if ((stats.st_size - offset) > max_size) {
77 printf("Error: Bootloader file %s is too large.\n",
78 filename);
79 result = 1;
80 goto cleanup;
81 }
82 *actual_size = (uint32_t)stats.st_size;
83 } else {
84 if ((stats.st_size - offset) < max_size)
85 *actual_size = stats.st_size - offset;
86 else
87 *actual_size = max_size;
88 }
89
90 *image = malloc(*actual_size);
91 if (*image == NULL) {
92 result = 1;
93 goto cleanup;
94 }
95
96 memset(*image, 0, *actual_size);
97
98 if (fread(*image, 1, (size_t)(*actual_size), fp) !=
99 (size_t)(*actual_size)) {
100 result = 1;
101 free(*image);
102 goto cleanup;
103 }
104
105 cleanup:
106 fclose(fp);
107 return result;
108 }
109
110 /*
111 * Processes commands to set a bootloader.
112 *
113 * @param context The main context pointer
114 * @param filename The file name of bootloader
115 * @param load_addr The load address value for bootloader
116 * @param entry_point The entry point value for bootloader
117 * @return 0 and 1 for success and failure
118 */
119 int
120 set_bootloader(build_image_context *context,
121 char *filename,
122 uint32_t load_addr,
123 uint32_t entry_point)
124 {
125 context->newbl_filename = filename;
126 context->newbl_load_addr = load_addr;
127 context->newbl_entry_point = entry_point;
128 return update_bl(context);
129 }
130
131 /*
132 * Processes commands to set a MTS image.
133 *
134 * @param context The main context pointer
135 * @param filename The file name of MTS image
136 * @param load_addr The load address value for MTS image
137 * @param entry_point The entry point value for MTS image
138 * @return 0 and 1 for success and failure
139 */
140 int
141 set_mts_image(build_image_context *context,
142 char *filename,
143 uint32_t load_addr,
144 uint32_t entry_point)
145 {
146 context->mts_filename = filename;
147 context->mts_load_addr = load_addr;
148 context->mts_entry_point = entry_point;
149 return update_mts_image(context);
150 }
151
152 int
153 set_rsa_param(build_image_context *context, parse_token token,
154 char *filename)
155 {
156 int result;
157 uint8_t *rsa_storage; /* Holds the rsa param after reading */
158 int32_t size; /* Bytes to read */
159 uint32_t actual_size; /* In bytes */
160
161 if ((size = g_soc_config->get_value_size(token)) <= 0) {
162 printf("Error: Unsupported token %d for value size.\n", token);
163 exit(1);
164 }
165
166 /* Read the image into memory. */
167 result = read_from_image(filename,
168 0,
169 (uint32_t)size,
170 &rsa_storage,
171 &actual_size,
172 file_type_bin);
173
174 if (result) {
175 printf("Error reading file %s.\n", filename);
176 exit(1);
177 }
178
179 if (actual_size != size) {
180 printf("Error: invalid size, file %s.\n", filename);
181 exit(1);
182 }
183
184 if (enable_debug)
185 printf("Updating token %d with file %s\n", (int)token, filename);
186
187 /* set to appropriate bct field */
188 result = g_soc_config->set_value(token,
189 rsa_storage, context->bct);
190
191 free(rsa_storage);
192 return result;
193 }
194
195 #define DEFAULT() \
196 default: \
197 printf("Unexpected token %d at line %d\n", \
198 token, __LINE__); \
199 return 1
200
201 /*
202 * General handler for setting values in config files.
203 *
204 * @param context The main context pointer
205 * @param token The parse token value
206 * @param value The pointer of value to set
207 * @return 0 for success
208 */
209 int context_set_value(build_image_context *context,
210 parse_token token,
211 void *value)
212 {
213 assert(context != NULL);
214
215 switch (token) {
216 case token_attribute:
217 context->newbl_attr = *((uint32_t *)value);
218 break;
219
220 case token_block_size:
221 context->block_size = *((uint32_t *)value);
222 context->block_size_log2 = log2(*((uint32_t *)value));
223
224 if (context->memory != NULL) {
225 printf("Error: Too late to change block size.\n");
226 return 1;
227 }
228
229 if (context->block_size != (uint32_t)(1 << context->block_size_log2)) {
230 printf("Error: Block size must be a power of 2.\n");
231 return 1;
232 }
233 context->pages_per_blk= 1 << (context->block_size_log2-
234 context->page_size_log2);
235 g_soc_config->set_value(token_block_size_log2,
236 &(context->block_size_log2), context->bct);
237 break;
238
239 case token_partition_size:
240 if (context->memory != NULL) {
241 printf("Error: Too late to change block size.\n");
242 return 1;
243 }
244
245 context->partition_size= *((uint32_t *)value);
246 g_soc_config->set_value(token_partition_size,
247 value, context->bct);
248 break;
249
250 case token_page_size:
251 context->page_size = *((uint32_t *)value);
252 context->page_size_log2 = log2(*((uint32_t *)value));
253
254 if (context->page_size != (uint32_t)(1 << context->page_size_log2)) {
255 printf("Error: Page size must be a power of 2.\n");
256 return 1;
257 }
258 context->pages_per_blk= 1 << (context->block_size_log2-
259 context->page_size_log2);
260
261 g_soc_config->set_value(token_page_size_log2,
262 &(context->page_size_log2), context->bct);
263 break;
264 case token_redundancy:
265 context->redundancy = *((uint32_t *)value);
266 break;
267
268 case token_version:
269 context->version = *((uint32_t *)value);
270 break;
271
272 case token_bct_copy:
273 context->bct_copy = *((uint32_t *)value);
274 break;
275
276 case token_odm_data:
277 context->odm_data = *((uint32_t *)value);
278 break;
279
280 case token_pre_bct_pad_blocks:
281 if (context->bct_init) {
282 printf("Error: Too late to pre-BCT pad.\n");
283 return 1;
284 }
285 context->pre_bct_pad_blocks = *((uint32_t *)value);
286 break;
287
288 case token_secure_jtag_control:
289 context->secure_jtag_control = *((uint32_t *)value);
290 g_soc_config->set_value(token_secure_jtag_control,
291 value, context->bct);
292 break;
293
294 case token_secure_debug_control:
295 context->secure_debug_control = *((uint32_t *)value);
296 g_soc_config->set_value(token_secure_debug_control,
297 value, context->bct);
298 break;
299
300 case token_unique_chip_id:
301 memcpy(context->unique_chip_id, value, 16);
302 g_soc_config->set_value(token_unique_chip_id,
303 value, context->bct);
304 break;
305
306 DEFAULT();
307 }
308
309 return 0;
310 }