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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* (C) Copyright 2002-2006
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* (C) Copyright 2002
* Sysgo Real-Time Solutions, GmbH <www.elinos.com>
* Marius Groeger <mgroeger@sysgo.de>
*/
#ifdef CONFIG_DEBUG_INITCALLS
#define DEBUG
#endif
/**
* @file
* @brief Main entry into the C part of barebox
*/
#include <common.h>
#include <shell.h>
#include <init.h>
#include <command.h>
#include <malloc.h>
#include <debug_ll.h>
#include <fs.h>
#include <errno.h>
#include <slice.h>
#include <linux/stat.h>
#include <envfs.h>
#include <magicvar.h>
#include <linux/reboot-mode.h>
#include <asm/sections.h>
#include <uncompress.h>
#include <globalvar.h>
#include <console_countdown.h>
#include <environment.h>
#include <linux/ctype.h>
#include <watchdog.h>
#include <glob.h>
#include <bselftest.h>
extern initcall_t __barebox_initcalls_start[], __barebox_early_initcalls_end[],
__barebox_initcalls_end[];
extern exitcall_t __barebox_exitcalls_start[], __barebox_exitcalls_end[];
#if defined CONFIG_FS_RAMFS && defined CONFIG_FS_DEVFS
static int mount_root(void)
{
mount("none", "ramfs", "/", NULL);
mkdir("/dev", 0);
mkdir("/tmp", 0);
mount("none", "devfs", "/dev", NULL);
if (IS_ENABLED(CONFIG_FS_EFIVARFS)) {
mkdir("/efivars", 0);
mount("none", "efivarfs", "/efivars", NULL);
}
if (IS_ENABLED(CONFIG_FS_PSTORE)) {
mkdir("/pstore", 0);
mount("none", "pstore", "/pstore", NULL);
}
return 0;
}
fs_initcall(mount_root);
#endif
#ifdef CONFIG_ENV_HANDLING
static bool region_overlap(loff_t starta, loff_t lena,
loff_t startb, loff_t lenb)
{
if (starta + lena <= startb)
return 0;
if (startb + lenb <= starta)
return 0;
return 1;
}
static int check_overlap(const char *path)
{
struct cdev *cenv, *cdisk, *cpart;
const char *name;
name = devpath_to_name(path);
if (name == path)
/*
* no /dev/ in front, so *path is some file. No need to
* check further.
*/
return 0;
cenv = cdev_by_name(name);
if (!cenv)
return -EINVAL;
if (cenv->mtd)
return 0;
cdisk = cenv->master;
if (!cdisk)
return 0;
list_for_each_entry(cpart, &cdisk->partitions, partition_entry) {
if (cpart == cenv)
continue;
if (region_overlap(cpart->offset, cpart->size,
cenv->offset, cenv->size))
goto conflict;
}
return 0;
conflict:
pr_err("Environment partition (0x%08llx-0x%08llx) "
"overlaps with partition %s (0x%08llx-0x%08llx), not using it\n",
cenv->offset, cenv->offset + cenv->size - 1,
cpart->name,
cpart->offset, cpart->offset + cpart->size - 1);
return -EINVAL;
}
#else
static int check_overlap(const char *path)
{
return 0;
}
#endif
static int load_environment(void)
{
const char *default_environment_path;
int __maybe_unused ret = 0;
default_environment_path = default_environment_path_get();
if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT))
defaultenv_load("/env", 0);
if (IS_ENABLED(CONFIG_ENV_HANDLING)) {
ret = check_overlap(default_environment_path);
if (ret)
default_environment_path_set(NULL);
else
envfs_load(default_environment_path, "/env", 0);
} else {
if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT))
pr_notice("No support for persistent environment. Using default environment\n");
}
nvvar_load();
return 0;
}
environment_initcall(load_environment);
static int global_autoboot_abort_key;
static const char * const global_autoboot_abort_keys[] = {
"any",
"ctrl-c",
};
static int global_autoboot_timeout = 3;
static const char * const global_autoboot_states[] = {
[AUTOBOOT_COUNTDOWN] = "countdown",
[AUTOBOOT_ABORT] = "abort",
[AUTOBOOT_MENU] = "menu",
[AUTOBOOT_BOOT] = "boot",
};
static int global_autoboot_state = AUTOBOOT_COUNTDOWN;
static bool test_abort(void)
{
bool do_abort = false;
int c, ret;
char key;
while (tstc()) {
c = getchar();
if (tolower(c) == 'q' || c == 3)
do_abort = true;
}
if (!do_abort)
return false;
printf("Abort init sequence? (y/n)\n"
"Will continue with init sequence in:");
ret = console_countdown(5, CONSOLE_COUNTDOWN_EXTERN, "yYnN", &key);
if (!ret)
return false;
if (tolower(key) == 'y')
return true;
return false;
}
#define INITFILE "/env/bin/init"
#define MENUFILE "/env/menu/mainmenu"
/**
* set_autoboot_state - set the autoboot state
* @autoboot: the state to set
*
* This functions sets the autoboot state to the given state. This can be called
* by boards which have its own idea when and how the autoboot shall be aborted.
*/
void set_autoboot_state(enum autoboot_state autoboot)
{
global_autoboot_state = autoboot;
}
/**
* do_autoboot_countdown - print autoboot countdown to console
*
* This prints the autoboot countdown to the console and waits for input. This
* evaluates the global.autoboot_abort_key to determine which keys are allowed
* to interrupt booting and also global.autoboot_timeout to determine the timeout
* for the counter. This function can be called multiple times, it is executed
* only the first time.
*
* Returns an enum autoboot_state value containing the user input.
*/
enum autoboot_state do_autoboot_countdown(void)
{
static enum autoboot_state autoboot_state = AUTOBOOT_UNKNOWN;
unsigned flags = CONSOLE_COUNTDOWN_EXTERN;
int ret;
struct stat s;
bool menu_exists;
char *abortkeys = NULL;
unsigned char outkey;
if (autoboot_state != AUTOBOOT_UNKNOWN)
return autoboot_state;
if (IS_ENABLED(CONFIG_CONSOLE_DISABLE_INPUT)) {
printf("\nNon-interactive console, booting system\n");
return autoboot_state = AUTOBOOT_BOOT;
}
if (global_autoboot_state != AUTOBOOT_COUNTDOWN)
return global_autoboot_state;
menu_exists = stat(MENUFILE, &s) == 0;
if (menu_exists) {
printf("\nHit m for menu or %s to stop autoboot: ",
global_autoboot_abort_keys[global_autoboot_abort_key]);
abortkeys = "m";
} else {
printf("\nHit %s to stop autoboot: ",
global_autoboot_abort_keys[global_autoboot_abort_key]);
}
switch (global_autoboot_abort_key) {
case 0:
flags |= CONSOLE_COUNTDOWN_ANYKEY;
break;
case 1:
flags |= CONSOLE_COUNTDOWN_CTRLC;
break;
default:
break;
}
command_slice_release();
ret = console_countdown(global_autoboot_timeout, flags, abortkeys,
&outkey);
command_slice_acquire();
if (ret == 0)
autoboot_state = AUTOBOOT_BOOT;
else if (menu_exists && outkey == 'm')
autoboot_state = AUTOBOOT_MENU;
else
autoboot_state = AUTOBOOT_ABORT;
return autoboot_state;
}
static int register_autoboot_vars(void)
{
globalvar_add_simple_enum("autoboot_abort_key",
&global_autoboot_abort_key,
global_autoboot_abort_keys,
ARRAY_SIZE(global_autoboot_abort_keys));
globalvar_add_simple_int("autoboot_timeout",
&global_autoboot_timeout, "%u");
globalvar_add_simple_enum("autoboot",
&global_autoboot_state,
global_autoboot_states,
ARRAY_SIZE(global_autoboot_states));
return 0;
}
postcore_initcall(register_autoboot_vars);
static int run_init(void)
{
const char *bmode;
bool env_bin_init_exists;
enum autoboot_state autoboot;
struct stat s;
glob_t g;
int i, ret;
setenv("PATH", "/env/bin");
export("PATH");
/* Run legacy /env/bin/init if it exists */
env_bin_init_exists = stat(INITFILE, &s) == 0;
if (env_bin_init_exists) {
pr_info("running %s...\n", INITFILE);
run_command("source " INITFILE);
return 0;
}
/* Unblank console cursor */
printf("\e[?25h");
if (test_abort()) {
pr_info("Init sequence aborted\n");
return -EINTR;
}
/* Run scripts in /env/init/ */
ret = glob("/env/init/*", 0, NULL, &g);
if (!ret) {
for (i = 0; i < g.gl_pathc; i++) {
const char *path = g.gl_pathv[i];
char *scr;
ret = stat(path, &s);
if (ret)
continue;
if (!S_ISREG(s.st_mode))
continue;
pr_debug("Executing '%s'...\n", path);
scr = basprintf("source %s", path);
run_command(scr);
free(scr);
}
}
globfree(&g);
/* source matching script in /env/bmode/ */
bmode = reboot_mode_get();
if (bmode) {
char *scr, *path;
scr = xasprintf("source /env/bmode/%s", bmode);
path = &scr[strlen("source ")];
if (stat(path, &s) == 0) {
pr_info("Invoking '%s'...\n", path);
run_command(scr);
}
free(scr);
}
autoboot = do_autoboot_countdown();
console_ctrlc_allow();
if (autoboot == AUTOBOOT_BOOT)
run_command("boot");
if (autoboot == AUTOBOOT_MENU)
run_command(MENUFILE);
if (autoboot == AUTOBOOT_ABORT && autoboot == global_autoboot_state)
watchdog_inhibit_all();
run_shell();
run_command(MENUFILE);
return 0;
}
typedef void (*ctor_fn_t)(void);
/* Call all constructor functions linked into the kernel. */
static void do_ctors(void)
{
#ifdef CONFIG_CONSTRUCTORS
ctor_fn_t *fn = (ctor_fn_t *) __ctors_start;
for (; fn < (ctor_fn_t *) __ctors_end; fn++)
(*fn)();
#endif
}
int (*barebox_main)(void);
void __noreturn start_barebox(void)
{
initcall_t *initcall;
int result;
if (!IS_ENABLED(CONFIG_SHELL_NONE) && IS_ENABLED(CONFIG_COMMAND_SUPPORT))
barebox_main = run_init;
do_ctors();
for (initcall = __barebox_initcalls_start;
initcall < __barebox_initcalls_end; initcall++) {
pr_debug("initcall-> %pS\n", *initcall);
result = (*initcall)();
if (result)
pr_err("initcall %pS failed: %s\n", *initcall,
strerror(-result));
}
pr_debug("initcalls done\n");
if (IS_ENABLED(CONFIG_SELFTEST_AUTORUN))
selftests_run();
if (barebox_main)
barebox_main();
if (IS_ENABLED(CONFIG_SHELL_NONE)) {
pr_err("Nothing left to do\n");
hang();
} else {
while (1)
run_shell();
}
}
void __noreturn hang (void)
{
puts ("### ERROR ### Please RESET the board ###\n");
for (;;);
}
/* Everything needed to cleanly shutdown barebox.
* Should be called before starting an OS to get
* the devices into a clean state
*/
void shutdown_barebox(void)
{
exitcall_t *exitcall;
for (exitcall = __barebox_exitcalls_start;
exitcall < __barebox_exitcalls_end; exitcall++) {
pr_debug("exitcall-> %pS\n", *exitcall);
(*exitcall)();
}
console_flush();
}
BAREBOX_MAGICVAR(global.autoboot,
"Autoboot state. Possible values: countdown (default), abort, menu, boot");
BAREBOX_MAGICVAR(global.autoboot_abort_key,
"Which key allows to interrupt autoboot. Possible values: any, ctrl-c");
BAREBOX_MAGICVAR(global.autoboot_timeout,
"Timeout before autoboot starts in seconds");
|