170 lines
3.3 KiB
C
Executable File
170 lines
3.3 KiB
C
Executable File
/*
|
|
* (C) Copyright 2000
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
*/
|
|
|
|
/* #define DEBUG */
|
|
|
|
#include <common.h>
|
|
#include <autoboot.h>
|
|
#include <cli.h>
|
|
#include <console.h>
|
|
#include <version.h>
|
|
|
|
#include <fs.h>
|
|
#include <u-boot/md5.h>
|
|
#include <malloc.h>
|
|
#include <crypt.h>
|
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
/*
|
|
* Board-specific Platform code can reimplement show_boot_progress () if needed
|
|
*/
|
|
__weak void show_boot_progress(int val) {}
|
|
|
|
|
|
#ifdef CONFIG_NM_LOGIN
|
|
/****************************************************************************
|
|
* check if ubootpwd exists in data partition and perform a login,
|
|
* otherwise continue booting
|
|
*/
|
|
int login (void)
|
|
{
|
|
#define PASS_LEN 256
|
|
char stored[PASS_LEN];
|
|
char buf[PASS_LEN], entered[32];
|
|
int res, i, tries;
|
|
int legacy_md5 = 0;
|
|
loff_t actread;
|
|
|
|
puts("\nautoboot has been stopped, press 'e' to enter: ");
|
|
|
|
for (i=0; i<=4096; i++) {
|
|
buf[0] = getc();
|
|
if (buf[0] == 'e' || buf[0] == '\n') {
|
|
puts("e");
|
|
break;
|
|
}
|
|
if (i == 4096) return 0;
|
|
}
|
|
puts("\n");
|
|
|
|
memset(stored, 0x0, sizeof(stored));
|
|
|
|
if (fs_set_blk_dev("mmc", "1:3", FS_TYPE_EXT) != 0) {
|
|
puts("Error, can not set blk devicet");
|
|
return 1;
|
|
}
|
|
|
|
res = fs_read("/root/boot/bootpass", (ulong)stored, 0, sizeof(stored), &actread);
|
|
if ((res!=0) || (actread <= 0)) {
|
|
/* no file or hash found */
|
|
puts("Login succeeded\n\n");
|
|
return 1;
|
|
} else if (actread == 16) {
|
|
legacy_md5 = 1;
|
|
}
|
|
|
|
for (tries = 1; ; tries++) {
|
|
puts("\nEnter password: ");
|
|
|
|
buf[0] = 0;
|
|
for (i=0; i<PASS_LEN; i++) {
|
|
buf[i] = getc();
|
|
if (buf[i] == '\r' || buf[i] == '\n') {
|
|
buf[i] = 0;
|
|
break;
|
|
}
|
|
}
|
|
buf[PASS_LEN-1] = 0;
|
|
|
|
if (strlen(buf) > 0) {
|
|
puts("\n");
|
|
if (legacy_md5) {
|
|
md5((unsigned char*) buf, strlen(buf), (unsigned char *)entered);
|
|
if (memcmp(stored, entered, 16) == 0) {
|
|
break;
|
|
}
|
|
} else {
|
|
char *cp = sha_crypt(buf, stored);
|
|
if (memcmp(cp, stored, actread) == 0) {
|
|
free(cp);
|
|
break;
|
|
}
|
|
free(cp);
|
|
}
|
|
puts("Login incorrect\n");
|
|
if (tries == 3) {
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/* succeeded */
|
|
puts("Login succeeded\n\n");
|
|
return 1;
|
|
}
|
|
|
|
#endif /* CONIFG_NM_LOGIN */
|
|
|
|
/****************************************************************************/
|
|
|
|
static void run_preboot_environment_command(void)
|
|
{
|
|
#ifdef CONFIG_PREBOOT
|
|
char *p;
|
|
|
|
p = getenv("preboot");
|
|
if (p != NULL) {
|
|
# ifdef CONFIG_AUTOBOOT_KEYED
|
|
int prev = disable_ctrlc(1); /* disable Control C checking */
|
|
# endif
|
|
|
|
run_command_list(p, -1, 0);
|
|
|
|
# ifdef CONFIG_AUTOBOOT_KEYED
|
|
disable_ctrlc(prev); /* restore Control C checking */
|
|
# endif
|
|
}
|
|
#endif /* CONFIG_PREBOOT */
|
|
}
|
|
|
|
/* We come here after U-Boot is initialised and ready to process commands */
|
|
void main_loop(void)
|
|
{
|
|
const char *s;
|
|
|
|
bootstage_mark_name(BOOTSTAGE_ID_MAIN_LOOP, "main_loop");
|
|
|
|
#ifdef CONFIG_VERSION_VARIABLE
|
|
setenv("ver", version_string); /* set version variable */
|
|
#endif /* CONFIG_VERSION_VARIABLE */
|
|
|
|
cli_init();
|
|
|
|
run_preboot_environment_command();
|
|
|
|
#if defined(CONFIG_UPDATE_TFTP)
|
|
update_tftp(0UL, NULL, NULL);
|
|
#endif /* CONFIG_UPDATE_TFTP */
|
|
|
|
s = bootdelay_process();
|
|
if (cli_process_fdt(&s))
|
|
cli_secure_boot_cmd(s);
|
|
|
|
autoboot_command(s);
|
|
|
|
#ifdef CONFIG_NM_LOGIN
|
|
if (!login()) {
|
|
puts ("Login failed, resetting...\n");
|
|
do_reset (NULL, 0, 0, NULL);
|
|
}
|
|
#endif
|
|
|
|
cli_loop();
|
|
panic("No CLI available");
|
|
}
|