forked from kubos/uboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_kubos.c
400 lines (343 loc) · 11 KB
/
update_kubos.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright (C) 2019 Kubos Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <common.h>
#include <net.h>
#include <ext4fs.h>
#include <fat.h>
#include <fs.h>
#include <mmc.h>
#include <kubos.h>
#include <dfu.h>
DECLARE_GLOBAL_DATA_PTR;
#define UPDATE_COUNT_ENVAR "kubos_updatecount"
#define DEV_ENVAR "kubos_updatedev"
#define PART_ENVAR "kubos_updatepart"
#define LOAD_ENVAR "kubos_loadaddr"
int update_kubos_count(void)
{
ulong count;
int ret = 0;
char *val = getenv(UPDATE_COUNT_ENVAR);
if (val == NULL)
{
setenv(UPDATE_COUNT_ENVAR, "1");
}
else
{
count = simple_strtoul(val, NULL, 10);
if (count > 1)
{
setenv(KUBOS_UPDATE_FILE, "bad");
setenv(UPDATE_COUNT_ENVAR, "0");
ret = -1;
}
else
{
count++;
setenv_ulong(UPDATE_COUNT_ENVAR, count);
}
}
saveenv();
return ret;
}
int update_mmc(char * file, void * load_addr, char * dev_num)
{
loff_t actlen;
char * fit_image_name;
int ndepth, noffset;
// Get the file header
int ret = ext4_read_file(file, load_addr, 0, sizeof(struct fdt_header), &actlen);
if (ret < 0)
{
printf("ERROR: Couldn't read %s file - %d\n", file, ret);
return -1;
}
// Get the total size of the FIT file (not including data sections)
int fit_size = fdt_totalsize(load_addr);
// Read the entire FIT file (not including data sections)
ret = ext4_read_file(file, load_addr, 0, fit_size, &actlen);
void * fit = load_addr;
// The data section will be after the FIT file, but aligned on a 4-byte
// boundary
int data_offset = (fit_size + 3) & ~3;
void * data_addr = load_addr + data_offset;
// U-Boot reserves memory for itself at the end of RAM, while we're loading
// the update file at the beginning.
// So the largest update entity we could possibly allow is the amount of
// space between the two, minus the room needed for the DFU chunk buffer,
// minus a little bit of wiggle room (1K)
int max_size = gd->start_addr_sp - (unsigned long) data_addr - CONFIG_SYS_DFU_DATA_BUF_SIZE - SZ_1K;
// Validate the file
if (!fit_check_format(fit)) {
printf("ERROR: Bad FIT format of the update file, aborting "
"auto-update\n");
return 1;
}
// Process updates
noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
ndepth = 0;
while (noffset >= 0 && ndepth >= 0) {
noffset = fdt_next_node(fit, noffset, &ndepth);
if (ndepth != 1) {
continue;
}
fit_image_name = (char *)fit_get_name(fit, noffset, NULL);
printf("Processing update '%s' :", fit_image_name);
// Get the update entity's size and offset
int update_offset, update_size;
if (fit_image_get_data_offset(fit, noffset, &update_offset)) {
printf("Error: failed to get data offset, aborting\n");
ret = 1;
continue;
}
if (fit_image_get_data_size(fit, noffset, &update_size)) {
printf("Error: failed to get data size, aborting\n");
ret = 1;
continue;
}
debug("Load addr: %p, Data offset: %d, Update offset: %d, Data size: %d\n",
data_addr, data_offset, update_offset, update_size);
if (update_size > max_size) {
printf("ERROR: Update file too large. %d > %d\n", update_size, max_size);
ret = 1;
continue;
}
// And load it into RAM
ret = ext4_read_file(file, data_addr, data_offset + update_offset,
update_size, &actlen);
if (ret < 0)
{
printf("ERROR: Couldn't read data section - %d\n", ret);
return -1;
}
if (!fit_image_verify_with_data(fit, noffset, data_addr, update_size)) {
printf("ERROR: Invalid update hash, aborting\n");
ret = 1;
continue;
}
printf("\n");
if (fit_image_check_type(fit, noffset,
IH_TYPE_FIRMWARE)) {
ret = dfu_tftp_write(fit_image_name, (unsigned int) data_addr,
update_size, "mmc", dev_num);
if (ret) {
return ret;
}
} else {
printf("ERROR: Invalid image type\n");
}
}
return 0;
}
/*
* update_kubos
*
* U-Boot has a DFU utility which currently allows a board to download a new firmware package and
* distribute the package components to the appropriate end-point locations.
*
* We want to leverage the second half of this utility and distribute a new firmware package that's
* been copied into the upgrade partition, rather than via a USB/TFTP connection.
*
* Input:
* kubosUpdateType upgrade - Indicates whether we're installing a new package as part of an upgrade,
* or part of recovery.
*
* Returns:
* 0 - An upgrade package was successfully installed
* -1 - No upgrade package could be installed (either because of system error or because no package exists)
* -2 - No upgrade package could be installed, but trigger a reboot so that we can retry.
*/
int update_kubos(bool upgrade)
{
struct mmc *mmc;
disk_partition_t part_info = {};
char * file;
char * env_addr;
char * dfu_info;
loff_t actlen;
ulong addr, dev_num, part = 0;
int ret = KUBOS_ERR_NO_REBOOT;
/*
* Get the name of the update file to load
*/
file = getenv(KUBOS_UPDATE_FILE);
if (file == NULL)
{
debug("INFO: %s envar not found\n", KUBOS_UPDATE_FILE);
return KUBOS_ERR_NO_REBOOT;
}
else if (!strcmp(file, "none") || !strcmp(file, "bad"))
{
debug("INFO: No update file specified\n");
return KUBOS_ERR_NO_REBOOT;
}
/*
* Temp SDRAM address to load to
*/
if ((env_addr = getenv(LOAD_ENVAR)) != NULL)
{
addr = simple_strtoul(env_addr, NULL, 16);
}
else
{
addr = KUBOS_UPGRADE_STORAGE;
}
/*
* Get and upgrade device number
*/
if ((env_addr = getenv(DEV_ENVAR)) != NULL)
{
dev_num = simple_strtoul(env_addr, NULL, 16);
}
else
{
dev_num = KUBOS_UPGRADE_DEVICE;
}
/*
* Load the SD card
*/
mmc = find_mmc_device(dev_num);
if (!mmc)
{
printf("ERROR: Could not access SD card\n");
return KUBOS_ERR_NO_REBOOT;
}
ret = mmc_init(mmc);
if (ret)
{
printf("ERROR: Could not init SD card - %d\n", ret);
return KUBOS_ERR_NO_REBOOT;
}
/* Increase the upgrade attempt count */
if (upgrade && update_kubos_count() != 0)
{
printf("ERROR: Number of update attempts exceeded. Abandoning update\n");
return KUBOS_ERR_NO_REBOOT;
}
/*
* Get and mount the upgrade partition
*/
if ((env_addr = getenv(PART_ENVAR)) != NULL)
{
part = simple_strtoul(env_addr, NULL, 16);
}
else
{
part = KUBOS_UPGRADE_PART;
}
if (part_get_info(&mmc->block_dev, part, &part_info))
{
printf("ERROR: Could not mount upgrade partition. No partition table\n");
return KUBOS_ERR_NO_REBOOT;
}
debug("INFO: Checking for new firmware files\n");
ext4fs_set_blk_dev(&mmc->block_dev, &part_info);
ret = ext4fs_mount(0);
if (!ret) {
printf("ERROR: Could not mount upgrade partition. ext4fs mount err - %d\n", ret);
return KUBOS_ERR_NO_REBOOT;
}
ret = ext4fs_exists(file);
/*
* Upgrade file found, call the existing DFU utility
*/
if (ret)
{
debug("INFO: Found file to upgrade - %s\n", file);
if (strstr(file,"nor") != NULL)
{
if ((dfu_info = getenv("dfu_alt_info_nor")) == NULL)
{
printf("ERROR: Can't upgrade nor files, dfu_alt_info_nor not defined\n");
return KUBOS_ERR_REBOOT;
}
setenv("dfu_alt_info", dfu_info);
ret = ext4_read_file(file, (void *)addr, 0, 0, &actlen);
printf("ext4_read_file RC: %d. Size: %lld\n", ret, actlen);
if (ret < 0)
{
printf("ERROR: Couldn't read %s file - %d\n", file, ret);
return KUBOS_ERR_REBOOT;
}
else
{
/* The "0" parameter isn't used for NOR flash, but it has to be non-NULL */
ret = update_tftp(addr, "nor", "0");
}
}
else
{
if ((dfu_info = getenv("dfu_alt_info_mmc")) == NULL)
{
printf("ERROR: Can't upgrade mmc files, dfu_alt_info_mmc not defined\n");
return KUBOS_ERR_REBOOT;
}
setenv("dfu_alt_info", dfu_info);
/*
* For right now, the "0" parameter is useless. If you add a dfu_alt_info entity
* in the future which has an entity type of "raw", this might need to change
* to specify a non-zero mmc device number.
*/
printf("Calling update_tftp\n");
ret = update_mmc(file, (void *) addr, "0");
}
if (ret)
{
printf("ERROR: System upgrade failed - %d\n", ret);
return KUBOS_ERR_REBOOT;
}
else
{
debug("INFO: Upgrade completed successfully\n");
if (upgrade)
{
/*
* Only mark that we're using a new version of KubOS Linux if we're doing
* a regular upgrade (vs upgrading NOR flash files)
*/
if (strstr(file,"nor") == NULL)
{
char *version = getenv(KUBOS_CURR_VERSION);
setenv(KUBOS_PREV_VERSION, version);
setenv(KUBOS_CURR_TRIED, "0");
}
}
else
{
if (getenv_yesno(KUBOS_CURR_TRIED) == 1)
{
setenv(KUBOS_PREV_VERSION, KUBOS_BASE);
}
else
{
setenv(KUBOS_CURR_TRIED, "1");
}
}
setenv(KUBOS_CURR_VERSION, file);
bootcount_store(0);
}
}
else
{
printf("ERROR: Upgrade file not found '%s'\n", file);
return KUBOS_ERR_REBOOT;
}
/* Reset the updatefile name so that we resume usual boot after rebooting */
setenv(KUBOS_UPDATE_FILE, "none");
setenv(UPDATE_COUNT_ENVAR, "0");
saveenv();
return KUBOS_OK_REBOOT;
}