forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.c
185 lines (162 loc) · 5.03 KB
/
library.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
/* library.c
*
* Copyright (C) 2022 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#if 1 /* for desktop testing */
#define HAVE_UNISTD_H
#define PRINTF_ENABLED
#else /* restricted build */
#define NO_FILESYSTEM
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#define exit _exit
#else
#define exit(x) while(1);
#endif
#include "image.h"
#include "printf.h"
/* HAL Stubs */
void hal_init(void)
{
return;
}
int hal_flash_write(uint32_t address, const uint8_t *data, int len)
{
(void)address;
(void)data;
(void)len;
return 0;
}
int hal_flash_erase(uint32_t address, int len)
{
(void)address;
(void)len;
return 0;
}
void hal_flash_unlock(void)
{
return;
}
void hal_flash_lock(void)
{
return;
}
void hal_prepare_boot(void)
{
return;
}
int do_boot(uint32_t* v)
{
wolfBoot_printf("booting %p"
#ifdef HAVE_UNISTD_H
"(actually exiting)"
#else
"(actually spin loop)"
#endif
"\n", v);
exit(0);
}
static uintptr_t gImage;
#ifdef NO_FILESYSTEM
static const uint8_t test_img[] = {
0x57, 0x4F, 0x4C, 0x46, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x00, 0x01,
0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0x02, 0x00, 0x08, 0x00, 0x1E, 0xBC,
0x0E, 0x62, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x02, 0x00, 0x01, 0x01, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x00, 0x20, 0x00, 0x08, 0xF9, 0x46, 0x2E,
0x0F, 0x70, 0x33, 0x38, 0xAC, 0x19, 0xFF, 0x82, 0xC8, 0xAC, 0xD6, 0x9A, 0xF9,
0xB2, 0x1F, 0xED, 0x60, 0x3F, 0x68, 0x7B, 0x85, 0xDB, 0x46, 0x8B, 0x3A, 0x7E,
0x65, 0xE0, 0x10, 0x00, 0x20, 0x00, 0x02, 0x45, 0x14, 0xB0, 0x5A, 0x37, 0x95,
0x3E, 0x17, 0x49, 0xAD, 0x75, 0xE7, 0x71, 0xD5, 0x65, 0xBB, 0x78, 0x7F, 0xFA,
0xF6, 0x31, 0x4F, 0x63, 0xF9, 0x20, 0x3D, 0xA1, 0x56, 0xB2, 0x71, 0x7C, 0x20,
0x00, 0x40, 0x00, 0xC6, 0x7A, 0xEB, 0x04, 0xB1, 0xB8, 0x82, 0xE7, 0x97, 0xD8,
0x00, 0x80, 0x1D, 0x93, 0xA9, 0x80, 0x37, 0xE0, 0x63, 0x7F, 0x78, 0x15, 0xD8,
0xD1, 0x22, 0xD6, 0x75, 0x0B, 0x04, 0xE9, 0x71, 0x12, 0xB7, 0x09, 0x32, 0xBC,
0xB7, 0xFC, 0xA1, 0x9D, 0x32, 0xC0, 0x7D, 0xDB, 0x63, 0xE2, 0x12, 0xF2, 0xE2,
0x41, 0xF4, 0x15, 0x7A, 0x38, 0xB5, 0xCD, 0xAA, 0x01, 0xB3, 0x5E, 0xF2, 0xCC,
0xD9, 0x02, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
#endif
int wolfBoot_start(void)
{
struct wolfBoot_image os_image;
int ret = 0;
memset(&os_image, 0, sizeof(os_image));
os_image.hdr = (uint8_t*)gImage;
if ((ret = wolfBoot_open_image_address(&os_image, (uint8_t*)gImage)) < 0) {
goto exit;
}
if ((ret = wolfBoot_verify_integrity(&os_image)) < 0) {
goto exit;
}
if ((ret = wolfBoot_verify_authenticity(&os_image)) < 0) {
goto exit;
}
wolfBoot_printf("Firmware Valid\n");
do_boot((uint32_t*)os_image.fw_base);
exit:
if (ret < 0) {
wolfBoot_printf("Failure %d: Hdr %d, Hash %d, Sig %d\n", ret,
os_image.hdr_ok, os_image.sha_ok, os_image.signature_ok);
}
return 0;
}
int main(int argc, const char* argv[])
{
int ret = 0;
#ifdef NO_FILESYSTEM
gImage = (uintptr_t)test_img;
#else
if (argc > 1) {
size_t sz = 0;
FILE* img = fopen(argv[1], "rb");
fseek(img, 0, SEEK_END);
sz = ftell(img);
fseek(img, 0, SEEK_SET);
gImage = (uintptr_t)malloc(sz);
if (((void*)gImage) == NULL) {
wolfBoot_printf("failed to malloc %zu bytes for image\n", sz);
ret = -1;
}
size_t bread = fread((void*)gImage, 1, sz, img);
if (bread != sz) {
ret = -2;
wolfBoot_printf("read %zu of %zu bytes from %s\n", bread, sz, argv[1]);
}
fclose(img);
} else {
wolfBoot_printf("usage: %s image_file.bin\n", argv[0]);
return 255;
}
#endif
ret = wolfBoot_start();
#ifndef NO_FILESYSTEM
free((void*)gImage);
#endif
return ret;
}