From 4f1112718a254fc270bd2a06e529c48f1ad66063 Mon Sep 17 00:00:00 2001 From: Piotr Dymacz Date: Wed, 31 Aug 2016 22:22:21 +0200 Subject: [PATCH] Move custom button recovery mode to environment script This allows to disable it and change easily as from now whole recovery mode is handled in script, not hard-coded in C. Plus, get rid of some defines, fix support for boot without delay and some other minor changes. --- u-boot/Makefile | 4 - u-boot/common/env_common.c | 5 +- u-boot/common/environment.c | 5 +- u-boot/common/main.c | 155 ++++++------------ u-boot/include/configs/qca9k_common.h | 3 + .../include/{upg_scripts.h => env_scripts.h} | 68 +++++++- 6 files changed, 122 insertions(+), 118 deletions(-) rename u-boot/include/{upg_scripts.h => env_scripts.h} (65%) diff --git a/u-boot/Makefile b/u-boot/Makefile index 869a146..21a0ff9 100644 --- a/u-boot/Makefile +++ b/u-boot/Makefile @@ -361,10 +361,6 @@ unconfig: config_common: @ >include/config.h @$(call include_add,soc/soc_list.h) - @$(call define_add,CONFIG_MAX_BUTTON_PRESSING,10) - @$(call define_add,CONFIG_DELAY_TO_AUTORUN_HTTPD,3) - @$(call define_add,CONFIG_DELAY_TO_AUTORUN_CONSOLE,5) - @$(call define_add,CONFIG_DELAY_TO_AUTORUN_NETCONSOLE,7) ar933x_common: unconfig config_common @$(call define_add,CFG_AG7240_NMACS,2) diff --git a/u-boot/common/env_common.c b/u-boot/common/env_common.c index 8836feb..f9e8de9 100644 --- a/u-boot/common/env_common.c +++ b/u-boot/common/env_common.c @@ -27,7 +27,7 @@ #include #include #include -#include +#include #include #include #include @@ -134,6 +134,9 @@ uchar default_environment[] = { #endif #if defined(CONFIG_ENV_UPG_SCRIPTS_FW) CONFIG_ENV_UPG_SCRIPTS_FW +#endif +#if defined(CONFIG_ENV_BTN_RECOVERY_SCRIPT) + CONFIG_ENV_BTN_RECOVERY_SCRIPT #endif "\0" }; diff --git a/u-boot/common/environment.c b/u-boot/common/environment.c index 899f86e..ac6c8fb 100644 --- a/u-boot/common/environment.c +++ b/u-boot/common/environment.c @@ -33,7 +33,7 @@ #undef __ASSEMBLY__ #include -#include +#include /* * Handle HOSTS that have prepended @@ -196,6 +196,9 @@ env_t environment __PPCENV__ = { #endif #if defined(CONFIG_ENV_UPG_SCRIPTS_FW) CONFIG_ENV_UPG_SCRIPTS_FW +#endif +#if defined(CONFIG_ENV_BTN_RECOVERY_SCRIPT) + CONFIG_ENV_BTN_RECOVERY_SCRIPT #endif "\0" } diff --git a/u-boot/common/main.c b/u-boot/common/main.c index b3f741c..9150820 100644 --- a/u-boot/common/main.c +++ b/u-boot/common/main.c @@ -32,20 +32,9 @@ DECLARE_GLOBAL_DATA_PTR; #endif -extern void all_led_on(void); -extern void all_led_off(void); -extern int NetLoopHttpd(void); - -#define MAX_DELAY_STOP_STR 32 - static char *delete_char(char *buffer, char *p, int *colp, int *np, int plen); static int parse_line(char *, char *[]); -#if defined(CONFIG_BOOTDELAY) &&\ - (CONFIG_BOOTDELAY >= 0) -static int abortboot(int); -#endif - char console_buffer[CFG_CBSIZE]; /* console I/O buffer */ static char erase_seq[] = "\b \b"; /* erase sequence */ static char tab_seq[] = " "; /* used to expand TABs */ @@ -119,9 +108,19 @@ static __inline__ int abortboot(int bootdelay) } #endif /* CONFIG_BOOTDELAY && CONFIG_BOOTDELAY >= 0 */ +/* + * ========= + * Main loop + * ========= + */ void main_loop(void) { - int counter = 0; + char *bootcmd; + +#if defined(CONFIG_BTN_RECOVERY_SCRIPT) + int stop_boot; + char *c; +#endif #ifndef CFG_HUSH_PARSER static char lastcommand[CFG_CBSIZE] = { 0, }; @@ -139,31 +138,20 @@ void main_loop(void) u_boot_hush_start(); #endif -#if defined(CONFIG_BOOTDELAY) &&\ - (CONFIG_BOOTDELAY >= 0) - /* Get boot delay (seconds) */ - s = getenv("bootdelay"); - bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; - /* Get boot command */ - s = getenv("bootcmd"); - - #if !defined(CONFIG_BOOTCOMMAND) - #error "CONFIG_BOOTCOMMAND not defined!" - #endif + bootcmd = getenv("bootcmd"); - if (!s) +#if defined(CONFIG_BOOTCOMMAND) + if (!bootcmd) setenv("bootcmd", CONFIG_BOOTCOMMAND); - s = getenv("bootcmd"); + bootcmd = getenv("bootcmd"); +#endif - /* - * Are we going to run web failsafe mode, - * U-Boot console, U-Boot netconsole - * or just boot command? - */ +/* Recovery mode before normal boot */ +#if defined(CONFIG_BTN_RECOVERY_SCRIPT) if (reset_button_status()) { - #if defined(CONFIG_SILENT_CONSOLE) + #if defined(CONFIG_SILENT_CONSOLE) if (gd->flags & GD_FLG_SILENT) { /* Restore serial console */ console_assign(stdout, "serial"); @@ -172,92 +160,43 @@ void main_loop(void) /* Enable normal console output */ gd->flags &= ~(GD_FLG_SILENT); - #endif - /* Wait 0,5s */ - milisecdelay(500); - - printf("Press reset button for at least:\n" - "- %d sec. to run web failsafe mode\n" - "- %d sec. to run U-Boot console\n" - "- %d sec. to run U-Boot netconsole\n\n", - CONFIG_DELAY_TO_AUTORUN_HTTPD, - CONFIG_DELAY_TO_AUTORUN_CONSOLE, - CONFIG_DELAY_TO_AUTORUN_NETCONSOLE); - - printf("Reset button is pressed for: %2d ", counter); - - while (reset_button_status()) { - /* LED ON and wait 0,15s */ - all_led_on(); - milisecdelay(150); + #endif - /* LED OFF and wait 0,85s */ - all_led_off(); - milisecdelay(850); + run_command("run recovery", 0); - counter++; + /* Should we stop booting after recovery mode? */ + c = getenv("stop_boot"); + stop_boot = c ? (int)simple_strtol(c, NULL, 10) : 0; - /* How long the button is pressed? */ - printf("\b\b\b%2d ", counter); - - if (!reset_button_status()) - break; - - if (counter >= CONFIG_MAX_BUTTON_PRESSING) - break; - } - - all_led_off(); - - if (counter > 0) { - /* Run web failsafe mode */ - if (counter >= CONFIG_DELAY_TO_AUTORUN_HTTPD && - counter < CONFIG_DELAY_TO_AUTORUN_CONSOLE) { - printf("\n\nButton was pressed for %d sec...\n" - "HTTP server is starting for firmware update...\n\n", - counter); - - NetLoopHttpd(); - bootdelay = -1; - } else if (counter >= CONFIG_DELAY_TO_AUTORUN_CONSOLE && - counter < CONFIG_DELAY_TO_AUTORUN_NETCONSOLE) { - printf("\n\nButton was pressed for %d sec...\n" - "Starting U-Boot console...\n\n", - counter); - - bootdelay = -1; - } else if (counter >= CONFIG_DELAY_TO_AUTORUN_NETCONSOLE) { - printf("\n\nButton was pressed for %d sec...\n" - "Starting U-Boot netconsole...\n\n", - counter); - - bootdelay = -1; - run_command("startnc", 0); - } else { - printf("\n\n## Error: button wasn't pressed long enough!\n" - "Continuing normal boot...\n\n"); - } + if (stop_boot) + bootcmd = NULL; + } +#endif /* CONFIG_RECOVERY_MODE */ - } else { - printf("\n\n## Error: button wasn't pressed long enough!\n" - "Continuing normal boot...\n\n"); - } +#if defined(CONFIG_BOOTDELAY) &&\ + (CONFIG_BOOTDELAY >= 0) + /* Get boot delay (seconds) */ + s = getenv("bootdelay"); + bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY; + if (bootdelay >= 0 && bootcmd && !abortboot(bootdelay)) { + /* Try to boot */ + #ifndef CFG_HUSH_PARSER + run_command(bootcmd, 0); + #else + parse_string_outer(bootcmd, FLAG_PARSE_SEMICOLON | + FLAG_EXIT_FROM_LOOP); + #endif } - - if (bootdelay >= 0 && s && !abortboot(bootdelay)) { +#else + if (bootcmd) { /* Try to boot */ #ifndef CFG_HUSH_PARSER - run_command(s, 0); + run_command(bootcmd, 0); #else - parse_string_outer(s, FLAG_PARSE_SEMICOLON | - FLAG_EXIT_FROM_LOOP); + parse_string_outer(bootcmd, FLAG_PARSE_SEMICOLON | + FLAG_EXIT_FROM_LOOP); #endif - /* Something went wrong! */ - printf("\n## Error: failed to execute 'bootcmd'!\n" - "HTTP server is starting for firmware update...\n\n"); - - NetLoopHttpd(); } #endif /* CONFIG_BOOTDELAY && CONFIG_BOOTDELAY >= 0 */ @@ -683,7 +622,7 @@ int do_run(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) if (argc < 2) { print_cmd_help(cmdtp); - return(1); + return 1; } for (i=1; i * * SPDX-License-Identifier: GPL-2.0 */ -#ifndef _UPG_SCRIPTS_H_ -#define _UPG_SCRIPTS_H_ +#ifndef _ENV_SCRIPTS_H_ +#define _ENV_SCRIPTS_H_ /* * U-Boot upgrade @@ -109,4 +109,64 @@ #endif /* CONFIG_UPG_SCRIPTS_FW */ -#endif /* _UPG_SCRIPTS_H_ */ +/* + * Recovery + */ +#if defined(CONFIG_BTN_RECOVERY_SCRIPT) + + #if !defined(CONFIG_CMD_BUTTON) ||\ + !defined(CONFIG_CMD_SLEEP) ||\ + !defined(CONFIG_CMD_LED) ||\ + !defined(CONFIG_CMD_SETEXPR) + #error "Commands setexpr, sleep, button and led{on, off} are required for recovery" + #endif + + #if !defined(CONFIG_GPIO_RESET_BTN) + #error "Reset button definition is required for recovery" + #endif + + #define CONFIG_ENV_BTN_RECOVERY_SCRIPT \ + "recovery=" \ + "if button; then " \ + "sleep 600;" \ + "setenv cnt 0;" \ + "echo Keep button pressed for at least:;" \ + "echo - 3s for web based recovery;" \ + "echo - 5s for U-Boot console;" \ + "echo - 7s for network console;" \ + "echo;" \ + "while button; do " \ + "ledon;" \ + "sleep 300;" \ + "echo . \'\\\\c\';" \ + "sleep 300;" \ + "ledoff;" \ + "sleep 600;" \ + "setexpr cnt $cnt + 1;" \ + "done;" \ + "echo $cnt seconds;" \ + "echo;" \ + "if test $cnt -ge 7; then " \ + "echo Starting network console...;" \ + "setenv stop_boot 1;" \ + "echo;" \ + "startnc;" \ + "elif test $cnt -ge 5; then " \ + "echo Starting U-Boot console...;" \ + "setenv stop_boot 1;" \ + "echo;" \ + "elif test $cnt -ge 3; then " \ + "echo HTTP server is starting for firmware update...;" \ + "setenv stop_boot 1;" \ + "echo;" \ + "httpd;" \ + "elif test $cnt -lt 3; then " \ + "echo \\#\\# Error: button was not pressed long enough!;" \ + "echo Continuing normal boot...;" \ + "echo;" \ + "fi;"\ + "fi\0" + +#endif /* CONFIG_BTN_RECOVERY_SCRIPT */ + +#endif /* _ENV_SCRIPTS_H_ */ -- 2.25.1