📄 shell.c
字号:
/****************************************************************************
外壳程序
更新手记:
2007.02.28
1.修正了内存显示方法(MemoryDisplay)的数据输出格式
2.扩展了 defset 的功能
****************************************************************************/
#include "../inc/def.h"
#include "../inc/config.h"
#include "../inc/board.h"
#include "../inc/utils.h"
#include "../inc/shell.h"
#include "../inc/44b.h"
/****************************************************************************/
static unsigned int BaudSet[] = {4800, 9600, 19200, 38400, 57600, 115200, 0};
static EnvParams Env;
extern U32 NandFlashSize;
struct Partition *pNandPart = Env.NandPartition;
CMD_STRUC CMD_INNER[] = {
{"help", "show this list", Help},
{"?", "= help", Help},
#ifdef RTC_TIMER_SUPPORT
{"date", "show or set current date", GetDate},
{"time", "show or set current time", GetTime},
{"setweek", "set weekday", SetWeek},
#endif
{"clock", "show system running clock", ShowSysClock},
#ifdef SET_SYSCLK_SUPPORT
{"setmclk", "set system running clock", ChgSysClk},
#endif
#ifdef SERIAL_PORTS_SWITCH
{"chguart", "change uart(0/1)", ChgUartPort},
#endif
{"setbaud", "set baud rate", ChgBaudRate},
#ifdef TFTP_DOWNLOAD_SUPPORT
{"ipcfg", "show or set current IP address and IP gate", SetIpAddr},
{"netload", "download file by net", LoadFromNet},
{"netrun", "download file by net and run", NetLoadRun},
{"g", "download file by net and run", NetLoadRun},
#endif
{"comload", "download file by uart", LoadFromUart},
{"comrun", "download file by uart and run", UartLoadRun},
#ifdef XMODEM_DOWNLOAD_SUPPORT
{"rx", "download file by xmodem", XmodemReceive},
{"rxrun", "download file by xmodem and run", XmodemReceiveRun},
#endif
#ifdef USB_DOWNLOAD_SUPPORT
{"usbload", "download file by usb", UsbDownload},
{"usbrun", "download file by usb and run", UsbDownloadRun},
#endif
#ifdef NOR_FLASH_SUPPORT
{"prog", "program flash", ProgFlash},
{"ap", "download file and program it to flash", AutoLoadAndProgFlash},
{"backup", "move bios to the top of flash", BackupBios},
{"copy", "copy flash from src to dst address", CopyFlash},
{"boot", "boot from flash", BootLoader},
{"fd", "show flash data", FlashDisplay},
#endif
{"run", "run program", RunProgram},
{"move", "move data from addr1 to addr2", MoveMem},
{"mrun", "move data form prog_s_addr to prog_r_addr and run", MoveRun},
{"md", "show memory data", MemoryDisplay},
{"memd", "show 8/16/32bits memory", MemoryShow},
{"mems", "set 8/16/32bits memory", MemorySet},
#ifdef SAVE_ENV_SUPPORT
{"senv", "save enviroment value to flash", SaveEnv},
#endif
{"machine", "show or set machine number", SetMachine},
{"setpa", "set program save(run) address", SetProgAddr},
{"setbp", "set program boot parameters", SetBootParams},
{"bootkey", "set key to autoboot", SetBootKey},
#ifdef NAND_FLASH_SUPPORT
{"nfpart", "set nand flash partitions", NandPart},
{"nferase", "erase nand flash partition", NandErase},
{"nfprog", "program nand flash", NandProg},
{"nfload", "load program from nand flash", NandLoad},
#endif
{"defset", "default setting for board configure or ucLinux", Default_Setting },
{NULL, NULL, NULL}
};
//程序下载地址
U32 download_addr;
//下载文件长度
U32 download_len;
#ifdef RTC_TIMER_SUPPORT
TIME_STRUC SysTime;
char *WeekDay[7] = {"MON", "TUES", "WED", "THURS","FRI", "SAT", "SUN"};
#endif
/*
void cache_flush(void)
{
unsigned int *pLRU, i;
pLRU = (unsigned int *)0x10004000;
for(i=0; i<(128<<2); i+=4)
pLRU[i] = 0;
}*/
void GetBoardKey(void)
{
/* unsigned int key;
void (*fp)(void) =(void (*)(void))BIOS_LOAD;
if((rPDATG&0xf0) != 0xf0)
{
key = rPDATG&0xf0;
Delay(500); //延时若干个100us,消除抖动
if(key == (rPDATG&0xf0))
{
while((rPDATG&0xf0) != 0xf0);
printf("boot from flash...\n");
(*fp)();
}
}*/
LedSet(~GetKeyStatus());
}
/*******************************************************************************************************/
void ShellIn(void)
{
int i, j, key, led;
int x;
int h_i, h_j, h_jj;
char t_command[MAX_CMD_LEN];
char * command;
char H_command[MAX_CMD_HISTORY][MAX_CMD_LEN];
printf("Machine Number is %d\n", Env.machine);
printf("IP address : %u.%u.%u.%u\n", (Env.nic_cfg.ip>>24)&0xff, (Env.nic_cfg.ip>>16)&0xff, (Env.nic_cfg.ip>>8)&0xff, Env.nic_cfg.ip&0xff);
printf("IP MASK : %u.%u.%u.%u\n", (Env.nic_cfg.mask>>24)&0xff, (Env.nic_cfg.mask>>16)&0xff, (Env.nic_cfg.mask>>8)&0xff, Env.nic_cfg.mask&0xff);
printf("IP GATE : %u.%u.%u.%u\n", (Env.nic_cfg.gate>>24)&0xff, (Env.nic_cfg.gate>>16)&0xff, (Env.nic_cfg.gate>>8)&0xff, Env.nic_cfg.gate&0xff);
printf("Serial baud : %d\n", Env.baud);
printf("Program save in %s flash\n", (Env.saved_in&1)?"nand":"nor");
printf("Program save address 0x%x\n", Env.prog_s_addr);
if(Env.saved_in&2)
printf("Initrd save address 0x%x\n", Env.initrd_addr);
printf("Program run address 0x%x\n", Env.prog_r_addr);
printf("Program boot params : %s\n", Env.boot_params);
// for(i=0; i<8; i++)
// printf("%d, %x ,%x\n", i, Env.NandPartition[i].offset, Env.NandPartition[i].size);
ShowSysClock(0, NULL);
#ifdef RTC_TIMER_SUPPORT
ShowDate();//显示当前日期
ShowTime();//显示当前时间 GetTime(0, NULL);
#endif
if((Env.boot_key>>16)==(('k'<<8)|'b'))
{
U8 key_stat, cnt, chk_loops, chk_key;
chk_key = (Env.boot_key>>12)&3;
printf("Set boot key is key%d, check state %s to boot\n",
chk_key+1, (Env.boot_key&0x100)?"high":"low");
chk_loops = 5;
while(chk_loops--)
{
key_stat = (GetKeyStatus()>>chk_key)&1;
for(cnt=30; cnt; cnt--)
{
Delay(1);
if(key_stat!=((GetKeyStatus()>>chk_key)&1))
break;
}
if(!cnt)
{
if(key_stat==((Env.boot_key>>8)&1))
{
if(Env.boot_key&1)
BootLoader(0, NULL);
else
MoveRun(0, NULL);
}
chk_loops = 0;
}
}
}
printf("\\>");
i = 0;
j = 0;
h_i = 0;
h_j = 0;
h_jj = 0;
for(x=0;x<MAX_CMD_HISTORY;x++)
H_command[x][0] = '\0';
command = H_command[0];
led = 0;
//led = 8;
for(;;)
{
//GetBoardKey();
if(WaitEventWithTimeout(kbhit, 1, 500))
{
led ^= 8;
LedSet(led);
//LedSet(led);
//led >>= 1;
//if(led == 1)
// led = 8;
}
else
{
key = getkey();
if(key == ENTER_KEY)
{
//int tmp;
putch('\n');
memcpy(t_command, command, i+1);
if(i)
{
memcpy(H_command[h_i], command, i+1);
h_i++;
h_i %= MAX_CMD_HISTORY;
h_j = h_i;
h_jj = 0;
command = H_command[h_i];
command[0] = '\0';
}
//tmp = ParseCmd(t_command, i);
ParseCmd(t_command, i);
//if(tmp<0)
// printf("Bad command\n");
printf("\\>");
i = 0;
j = 0;
}
else if(key == BACK_KEY && j > 0)
{
if(i == j)
{
i--;
j--;
printf("\b \b");
}
else
{
i--;
j--;
for(x = j; x < i; x++)
{
command[x]=command[x+1];
}
command[x] = ' ';
printf("\b \b");
for(x = j; x < i+1; x++)
{
putch(command[x]);
}
for(x = 0; x < (i+1-j); x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
}
command[i] = '\0';
}
else if(key == 0x1b)
{
key = getch();
if(key != 0x5b) continue;
key = getch();
if(key == RIGHT_KEY)
{
if(j==i) continue;
putch(0x1b);
putch(0x5b);
putch(RIGHT_KEY);
j++;
}
else if(key == LEFT_KEY)
{
if(j==0) continue;
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
j--;
}
else if(key == HOME_KEY)
{
for(x=0;x<j;x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
j = 0;
}
else if(key == END_KEY)
{
for(x=j;x<i;x++)
{
putch(0x1b);
putch(0x5b);
putch(RIGHT_KEY);
}
j = i;
}
else if(key == UP_KEY)
{
int cm_len;
int tmp_hj;
if(h_jj == MAX_CMD_HISTORY - 1)
{
putch(BEEP_KEY);
continue;
}
tmp_hj = h_j;
if(h_j==0)
h_j = MAX_CMD_HISTORY - 1;
else
h_j--;
cm_len = strlen(H_command[h_j]);
if(cm_len == 0)
{
putch(BEEP_KEY);
h_j = tmp_hj;
continue;
}
command = H_command[h_j];
for(x = 0; x < j; x++)
{ //回到行首
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
{
putch(command[x]);
}
for(;x<i;x++)
{
putch(' ');
}
for(;x>cm_len;x--)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
i = j = cm_len;
h_jj++;
}
else if(key == DOWN_KEY)
{
int cm_len;
if(h_jj == 0)
{
putch(BEEP_KEY);
continue;
}
h_j ++;
h_j %= MAX_CMD_HISTORY;
cm_len = strlen(H_command[h_j]);
command = H_command[h_j];
for(x = 0; x < j; x++)//回到行首
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
for(x = 0; x < cm_len; x++)
{
putch(command[x]);
}
for(;x<i;x++)
{
putch(' ');
}
for(; x > cm_len; x--)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
i = j = cm_len;
h_jj--;
}
}
else if(key >= 0x20 && key <= 0x7e && i < (MAX_CMD_LEN-1))
{
if(i==j)
{
command[i++] = key;
putch(key);
j++;
}
else
{
for(x = i; x > j; x--)
{
command[x] = command[x-1];
}
command[j] = key;
i++;
for(x = j; x < i; x++)
{
putch(command[x]);
}
j++;
for(x = 0; x < i-j; x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
}
command[i] = '\0';
}
}
}
}
/*******************************************************************************************************/
void ReadLine(char * data)
{
int x, i, j, key;
char command[MAX_CMD_LEN] = {0x0};
x = 0;
i = 0;
j = 0;
for(;;)
{
key = getkey();
if(key == ENTER_KEY)
{
putch('\n');
memcpy(data, command, i+1);
break;
}
else if(key == BACK_KEY && j > 0)
{
if(i == j)
{
i--;
j--;
printf("\b \b");
}
else
{
i--;
j--;
for(x = j; x < i; x++)
{
command[x]=command[x+1];
}
command[x] = ' ';
printf("\b \b");
for(x = j; x < i+1; x++)
{
putch(command[x]);
}
for(x = 0; x < (i+1-j); x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
}
command[i] = '\0';
}
else if(key >= 0x20 && key <= 0x7e && i < (MAX_CMD_LEN-1))
{
if(i==j)
{
command[i++] = key;
putch(key);
j++;
}
else
{
for(x = i; x > j; x--)
{
command[x] = command[x-1];
}
command[j] = key;
i++;
for(x = j; x < i; x++)
{
putch(command[x]);
}
j++;
for(x = 0; x < i-j; x++)
{
putch(0x1b);
putch(0x5b);
putch(LEFT_KEY);
}
}
command[i] = '\0';
}
}
//return command;
}
/*******************************************************************************************************/
int ParseCmd(char *cmdline, int cmd_len)
{
int argc, num_commands;
char *argv[MAX_ARGS];
ParseArgs(cmdline, &argc, argv);
/* only whitespace */
if(argc == 0)
return 0;
num_commands = GetCmdMatche(argv[0]);
if(num_commands < 0)
{
printf("No '%s' command, please type 'help' or '?' for a command list\n", argv[0]);
return -1;
}
if(NULL != CMD_INNER[num_commands].proc)
CMD_INNER[num_commands].proc(argc, argv);
return 0;
}
/*******************************************************************************************************/
void ParseArgs(char *cmdline, int *argc, char **argv)
{
#define STATE_WHITESPACE 0
#define STATE_WORD 1
char *c;
int state = STATE_WHITESPACE;
int i;
*argc = 0;
if(strlen(cmdline) == 0)
return;
/* convert all tabs into single spaces */
c = cmdline;
while(*c != '\0')
{
if(*c == '\t')
*c = ' ';
c++;
}
c = cmdline;
i = 0;
/* now find all words on the command line */
while(*c != '\0')
{
if(state == STATE_WHITESPACE)
{
if(*c != ' ')
{
argv[i] = c; //将argv[i]指向c
i++;
state = STATE_WORD;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -