📄 main.c
字号:
/* * (C) Copyright 2000 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. * * See file CREDITS for list of people who contributed to this * project. * * This program 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 2 of * the License, or (at your option) any later version. * * This program 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., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */#include <armboot.h>#include <command.h>#include <cmd_nvedit.h>#include <cmd_bootm.h>#include <net.h>static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);static int parse_line (char *, char *[]);#if (CONFIG_BOOTDELAY >= 0)static int abortboot(bd_t *, int);#endif#undef DEBUG_PARSERchar console_buffer[CFG_CBSIZE]; /* console I/O buffer */static char erase_seq[] = "\b \b"; /* erase sequence */static char tab_seq[] = " "; /* used to expand TABs */#ifdef CONFIG_BOOT_RETRY_TIMEstatic uint64_t endtime = 0; /* must be set, default is instant timeout */static int retry_time = -1; /* -1 so can call readline before main_loop */#endif#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())#ifndef CONFIG_BOOT_RETRY_MIN#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME#endif/*************************************************************************** * Watch for 'delay' seconds for autoboot stop or autoboot delay string. * returns: 0 - no key string, allow autoboot * 1 - got key string, abort */#if (CONFIG_BOOTDELAY >= 0)# if defined(CONFIG_AUTOBOOT_KEYED)static __inline__ int abortboot(bd_t *bd, int bootdelay){ int abort = 0; char ch; uint64_t etime = endtick(bootdelay); char* delaykey = getenv (bd, "bootdelaykey"); char* delaymatch; char* stopkey = getenv (bd, "bootstopkey"); char* stopmatch;# ifdef CONFIG_AUTOBOOT_PROMPT printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);# endif# ifdef CONFIG_AUTOBOOT_DELAY_STR if (delaykey == NULL) delaykey = CONFIG_AUTOBOOT_DELAY_STR;# endif# ifdef CONFIG_AUTOBOOT_STOP_STR if (stopkey == NULL) stopkey = CONFIG_AUTOBOOT_STOP_STR;# endif# if DEBUG_BOOTKEYS printf("delay key:<%s>\n",delaykey?delaykey:"NULL"); printf("stop key:<%s>\n",stopkey?stopkey:"NULL");# endif delaymatch = delaykey; stopmatch = stopkey; /* In order to keep up with incoming data, check timeout only * when catch up. */ while (!abort && get_ticks() <= etime) { /* abort if matched all of one of the keys */ if (delaymatch && !*delaymatch) {# if DEBUG_BOOTKEYS printf("got delaykey\n");# endif abort = 1; } if (stopmatch && !*stopmatch) {# if DEBUG_BOOTKEYS printf("got stopkey\n");# endif# ifdef CONFIG_BOOT_RETRY_TIME /* don't retry auto boot */ retry_time = -1;# endif abort = 1; } if (tstc()) { ch = getc(); /* get the incoming char */ if (stopmatch) { if (*stopmatch != ch) stopmatch = stopkey; /* start over */ if (*stopmatch == ch) ++stopmatch; /* matched 1 more */ } if (delaymatch) { if (*delaymatch != ch) delaymatch = delaykey; /* start over */ if (*delaymatch == ch) ++delaymatch; /* matched 1 more */ } } }# if DEBUG_BOOTKEYS if (!abort) printf("key timeout\n");# endif return abort;}# else /* !defined(CONFIG_AUTOBOOT_KEYED) */static __inline__ int abortboot(bd_t *bd, int bootdelay){ int abort = 0; printf("\nHit any key to stop autoboot: %2d ", bootdelay);// printf("Hit any key to stop autoboot: %3d ", bootdelay);#ifdef CONFIG_ZERO_BOOTDELAY_CHECK /* * Check if key already pressed * Don't check if bootdelay < 0 */ if (bootdelay >= 0) { if (tstc()) { /* we got a key press */ (void) getc(); /* consume input */ printf ("\b\b\b 0\n"); return 1; /* don't auto boot */ } }#endif while (bootdelay > 0) { int i; --bootdelay; /* delay 100 * 10ms */ for (i=0; !abort && i<100; ++i) { if (tstc()) /* we got a key press */ { abort = 1; /* don't auto boot */ bootdelay = 0; /* no more delay */ (void) getc(); /* consume input */ break; } udelay(10000); }// printf("%3d ", bootdelay); printf("\b\b\b%2d ", bootdelay); udelay(10000); } putc ('\n'); return abort;}# endif /* CONFIG_AUTOBOOT_KEYED */#endif /* CONFIG_BOOTDELAY >= 0 *//****************************************************************************/void main_loop(bd_t *bd){ static char lastcommand[CFG_CBSIZE] = { 0, }; int len; int rc = 1; int flag=0x0;#if (CONFIG_BOOTDELAY >= 0) char *s; int bootdelay;#endif#ifdef CONFIG_PREBOOT char *p;#endif#ifdef CONFIG_PREBOOT if ((p = getenv ("preboot")) != NULL) {# ifdef CONFIG_AUTOBOOT_KEYED int prev = disable_ctrlc(1); /* disable Control C checking */# endif run_command (p, bd, 0);# ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc(prev); /* restore Control C checking */# endif }#endif /* CONFIG_PREBOOT */#if (CONFIG_BOOTDELAY >= 0) s = getenv (bd, "bootdelay"); bootdelay = s ? (int)simple_strtol(s, NULL, 10) : 0; //change 20070914 by hill#if 0//#if 1 printf ("### main_loop entered:\n\n");#endif# ifdef CONFIG_BOOT_RETRY_TIME s = getenv (bd, "bootretry"); if (s != NULL) retry_time = (int)simple_strtoul(s, NULL, 10); else retry_time = CONFIG_BOOT_RETRY_TIME; if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN) retry_time = CONFIG_BOOT_RETRY_MIN;# endif /* CONFIG_BOOT_RETRY_TIME */ s = getenv (bd, "bootcmd"); if (bootdelay >= 0 && s && !abortboot (bd, bootdelay)) {# ifdef CONFIG_AUTOBOOT_KEYED int prev = disable_ctrlc(1); /* disable Control C checking */# endif#ifdef VIADBG run_command (s, bd, 0);#else char *local_args[2]; local_args[0] = "bootm";//argv[0]; //local_args[1] = "80020000"; //change by hill 20070912 local_args[1] = "1020000"; do_bootm (do_bootm, bd, 0, 2, local_args);#endif# ifdef CONFIG_AUTOBOOT_KEYED disable_ctrlc(prev); /* restore Control C checking */# endif }#endif /* CONFIG_BOOTDELAY */ /* * Main Loop for Monitor Command Processing */ for (;;) {#ifdef CONFIG_BOOT_RETRY_TIME if (rc >= 0) { /* Saw enough of a valid command to * restart the timeout. */ reset_cmd_timeout(); }#endif len = readline(CFG_PROMPT); /* assume no special flags for now */ flag = 0; if (len > 0) { strcpy (lastcommand, console_buffer); } else if (len == 0) { // flag |= CMD_FLAG_REPEAT; lastcommand[0] = 0; }#ifdef CONFIG_BOOT_RETRY_TIME else if (len == -2) { /* -2 means timed out, retry autoboot */ printf("\nTimed out waiting for command\n"); return; /* retry autoboot */ }#endif if (len == -1) { printf ("<INTERRUPT>\n"); } else { rc = run_command(lastcommand, bd, flag); } if (rc <= 0) { /* invalid command or not repeatable, forget it */ lastcommand[0] = 0; } }}/*************************************************************************** * reset command line timeout to retry_time seconds */#ifdef CONFIG_BOOT_RETRY_TIMEvoid reset_cmd_timeout(void){ endtime = endtick(retry_time);}#endif/****************************************************************************//* * Prompt for input and read a line. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, * time out when time goes past endtime (timebase time in ticks). * Return: number of read characters * -1 if break * -2 if timed out */int readline (const char *const prompt){ char *p = console_buffer; int n = 0; /* buffer index */ int plen = strlen (prompt); /* prompt length */ int col; /* output column cnt */ char c; /* print prompt */ if (prompt) puts (prompt); col = plen; for (;;) {#ifdef CONFIG_BOOT_RETRY_TIME while (!tstc()) { /* while no incoming data */ if (retry_time >= 0 && get_ticks() > endtime) return (-2); /* timed out */ }#endif c = getc(); /* * Special character handling */ switch (c) { case '\r': /* Enter */ case '\n': *p = '\0'; puts ("\r\n"); return (p - console_buffer); case 0x03: /* ^C - break */ console_buffer[0] = '\0'; /* discard input */ return (-1); case 0x15: /* ^U - erase line */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -