-
Notifications
You must be signed in to change notification settings - Fork 724
Expand file tree
/
Copy pathnxboot_main.c
More file actions
325 lines (287 loc) · 10.3 KB
/
nxboot_main.c
File metadata and controls
325 lines (287 loc) · 10.3 KB
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
/****************************************************************************
* apps/boot/nxboot/nxboot_main.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <syslog.h>
#include <nuttx/ascii.h>
#include <sys/param.h>
#include <nxboot.h>
#include <sys/boardctl.h>
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef CONFIG_NXBOOT_PROGRESS
static bool g_progress_started = false;
#ifdef CONFIG_NXBOOT_PRINTF_PROGRESS_PERCENT
static bool g_progress_percent_started = false;
#endif
#endif
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_NXBOOT_PRINTF_PROGRESS
# ifdef CONFIG_NXBOOT_PRINTF_PROGRESS_PERCENT
static const char backtab[] =
{
ASCII_BS, ASCII_BS, ASCII_BS, ASCII_BS, '\0',
};
# endif
static const char *progress_msgs[] =
{
[startup_msg] = "*** nxboot ***",
[power_reset] = "Power Reset detected, check images only",
[soft_reset] = "Soft reset detected, check for update",
[found_bootable_image] = "Found bootable image, boot from primary",
[no_bootable_image] = "No bootable image found",
[boardioc_image_boot_fail] = "Board failed to boot bootable image",
[ramcopy_started] = "Copying bootable image to RAM",
[recovery_revert] = "Reverting image to recovery",
[recovery_create] = "Creating recovery image",
[update_from_update] = "Updating from update image",
[validate_primary] = "Validating primary image",
[validate_recovery] = "Validating recovery image",
[validate_update] = "Validating update image",
[recovery_created] = "Recovery image created",
[recovery_invalid] = "Recovery image invalid, update stopped",
[update_failed] = "Update failed",
};
#endif /* CONFIG_NXBOOT_PRINTF_PROGRESS */
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxboot_progress
*
* Description:
* If enabled, this function prints progress messages to stdout.
* Messages are handled via integer enums, allowing this function to be
* easily replaced if required with no changes needed to the underlying
* code
*
* Input Parameters:
* type - the progress message type to be printed, as per progress_type_e:
* - nxboot_info: Prefixes arg. string with "INFO:"
* - nxboot_error: Prefixes arg. string with "ERR:"
* - nxboot_progress_start: Prints arg. string with no newline
* to allow a ..... sequence
* or % remaining to follow
* - nxboot_progress_dot: Prints a "." to the ..... sequence
* - nxboot_progress_percent: Displays progress as % remaining
* - nxboot_progress_end, Flags end of a progrees sequence
*
* ... - variadic argument:
* - the enum (int) reference to the message string or
* - the % remaining in the case of type: nxboot_progress_percent
*
* Returned Value:
* None
*
****************************************************************************/
#ifdef CONFIG_NXBOOT_PROGRESS
void nxboot_progress(enum progress_type_e type, ...)
{
#ifdef CONFIG_NXBOOT_PRINTF_PROGRESS
va_list arg;
va_start(arg, type);
switch (type)
{
case nxboot_info:
{
int idx = va_arg(arg, int);
assert(progress_msgs[idx]);
if (strlen(progress_msgs[idx]) == 0)
{
syslog(LOG_ERR, "No progress message for idx: %d\n", idx);
break;
}
dprintf(STDOUT_FILENO, "%s\n", progress_msgs[idx]);
}
break;
case nxboot_error:
{
int idx = va_arg(arg, int);
assert(progress_msgs[idx]);
if (strlen(progress_msgs[idx]) == 0)
{
syslog(LOG_ERR, "No progress message for idx: %d\n", idx);
break;
}
dprintf(STDOUT_FILENO, "ERROR: %s\n", progress_msgs[idx]);
}
break;
case nxboot_progress_start:
{
int idx = va_arg(arg, int);
assert(progress_msgs[idx]);
if (strlen(progress_msgs[idx]) == 0)
{
syslog(LOG_ERR, "No progress message for idx: %d\n", idx);
break;
}
dprintf(STDOUT_FILENO, "%s", progress_msgs[idx]);
g_progress_started = true;
}
break;
case nxboot_progress_dot:
{
assert(g_progress_started);
if (!g_progress_started)
{
syslog(LOG_ERR, "Progress dot requested "
"but no previous progress start\n");
}
else
{
dprintf(STDOUT_FILENO, ".");
}
}
break;
#ifdef CONFIG_NXBOOT_PRINTF_PROGRESS_PERCENT
case nxboot_progress_percent:
{
assert(g_progress_started);
if (!g_progress_started)
{
syslog(LOG_ERR, "Progress percent requested "
"but no previous progress start\n");
}
else
{
int percent = va_arg(arg, int);
if (!g_progress_percent_started)
{
g_progress_percent_started = true;
dprintf(STDOUT_FILENO, ": ");
}
else
{
dprintf(STDOUT_FILENO, "%s", backtab);
}
dprintf(STDOUT_FILENO, "%3d%%", MIN(percent, 100));
}
}
break;
#endif
case nxboot_progress_end:
{
assert(g_progress_started);
if (!g_progress_started)
{
syslog(LOG_ERR, "Progress dot stop requested "
"but no previous progress start\n");
}
else
{
dprintf(STDOUT_FILENO, "\n");
}
g_progress_started = false;
#ifdef CONFIG_NXBOOT_PRINTF_PROGRESS_PERCENT
g_progress_percent_started = false;
#endif
}
break;
default:
{
assert(false);
syslog(LOG_ERR, "Invalid progress message type: %d\n", type);
dprintf(STDOUT_FILENO, "progress: unknown type!\n");
}
break;
}
va_end(arg);
#endif /* CONFIG_NXBOOT_PRINTF_PROGRESS */
}
#endif
/****************************************************************************
* Name: nxboot_main
*
* Description:
* NuttX bootloader entry point.
*
****************************************************************************/
int main(int argc, FAR char *argv[])
{
int ret;
struct boardioc_boot_info_s info;
bool check_only;
#ifdef CONFIG_NXBOOT_SWRESET_ONLY
FAR struct boardioc_reset_cause_s cause;
#endif
#ifdef CONFIG_BOARDCTL_FINALINIT
/* Perform architecture-specific final-initialization (if configured) */
boardctl(BOARDIOC_FINALINIT, 0);
#endif
syslog(LOG_INFO, "*** nxboot ***\n");
nxboot_progress(nxboot_info, startup_msg);
#ifdef CONFIG_NXBOOT_SWRESET_ONLY
check_only = true;
ret = boardctl(BOARDIOC_RESET_CAUSE, (uintptr_t)&cause);
if (ret >= 0)
{
if (cause.cause == BOARDIOC_RESETCAUSE_CPU_SOFT ||
cause.cause == BOARDIOC_RESETCAUSE_CPU_RWDT ||
cause.cause == BOARDIOC_RESETCAUSE_PIN)
{
check_only = false;
nxboot_progress(nxboot_info, soft_reset);
}
else
{
syslog(LOG_INFO, "Power reset detected, performing check only.\n");
nxboot_progress(nxboot_info, power_reset);
}
}
#else
check_only = false;
#endif
if (nxboot_perform_update(check_only) < 0)
{
syslog(LOG_ERR, "Could not find bootable image.\n");
nxboot_progress(nxboot_error, no_bootable_image);
return OK;
}
syslog(LOG_INFO, "Found bootable image, boot from primary.\n");
nxboot_progress(nxboot_info, found_bootable_image);
#ifdef CONFIG_NXBOOT_COPY_TO_RAM
syslog(LOG_INFO, "Copying image to RAM.\n");
nxboot_ramcopy();
#endif
/* Call board specific image boot */
info.path = CONFIG_NXBOOT_PRIMARY_SLOT_PATH;
info.header_size = CONFIG_NXBOOT_HEADER_SIZE;
ret = boardctl(BOARDIOC_BOOT_IMAGE, (uintptr_t)&info);
/* Only get here if the board boot fails */
nxboot_progress(nxboot_error, boardioc_image_boot_fail);
return ret;
}