Feat: lab 4
This commit is contained in:
146
kernel/lib/shell.c
Normal file
146
kernel/lib/shell.c
Normal file
@@ -0,0 +1,146 @@
|
||||
#include <stddef.h>
|
||||
#include <kmalloc.h>
|
||||
#include <utils.h>
|
||||
#include <string.h>
|
||||
#include <uart.h>
|
||||
#include <mbox.h>
|
||||
#include <shell.h>
|
||||
|
||||
#define INPUT_BUFLEN 1000
|
||||
|
||||
#define PM_PASSWORD 0x5a000000
|
||||
|
||||
#define PM_RSTC 0x3F10001c
|
||||
#define PM_WDOG 0x3F100024
|
||||
|
||||
void help (void)
|
||||
{
|
||||
uart_puts(
|
||||
"help : print this help menu" ENDL
|
||||
"hello : print Hello World!" ENDL
|
||||
"hwinfo : print hardware info" ENDL
|
||||
"memalloc : alloate memory and print" ENDL
|
||||
"ls : list directory contents" ENDL
|
||||
"cat : concatenate files and print" ENDL
|
||||
"reboot : reboot the device" ENDL
|
||||
);
|
||||
}
|
||||
|
||||
void hello (void)
|
||||
{
|
||||
uart_puts("hello, world" ENDL);
|
||||
}
|
||||
|
||||
void hwinfo (void)
|
||||
{
|
||||
uart_puts(
|
||||
"hwinfo: " ENDL
|
||||
"board revision: "
|
||||
);
|
||||
uart_hex(get_board_revision());
|
||||
uart_puts(
|
||||
ENDL
|
||||
"memory base addr: "
|
||||
);
|
||||
uart_hex(get_memory_base_addr());
|
||||
uart_puts(
|
||||
ENDL
|
||||
"memory size: "
|
||||
);
|
||||
uart_hex(get_memory_size());
|
||||
uart_puts(ENDL);
|
||||
}
|
||||
|
||||
void memalloc(size_t size)
|
||||
{
|
||||
void *addr = kmalloc(size);
|
||||
uart_puts("size: ");
|
||||
uart_hex(size);
|
||||
uart_puts(ENDL);
|
||||
|
||||
uart_puts("addr: ");
|
||||
uart_hex((uint64_t)addr);
|
||||
uart_puts(ENDL);
|
||||
}
|
||||
|
||||
void ls_initrd_callback(file_node_t *tr)
|
||||
{
|
||||
uart_puts(tr->filename);
|
||||
uart_puts(ENDL);
|
||||
}
|
||||
|
||||
void ls(file_node_t *root)
|
||||
{
|
||||
initrd_traverse(root, ls_initrd_callback);
|
||||
}
|
||||
|
||||
void cat(file_node_t *root, const char *filename)
|
||||
{
|
||||
file_node_t *tr = initrd_get(root, filename);
|
||||
if (tr) {
|
||||
uart_puts((char *)tr->filecontent);
|
||||
} else {
|
||||
uart_puts("FILE NOT EXIST!" ENDL);
|
||||
}
|
||||
}
|
||||
|
||||
void set(long addr, unsigned int value) {
|
||||
volatile unsigned int* point = (unsigned int*)addr;
|
||||
*point = value;
|
||||
}
|
||||
|
||||
void reset(int tick) { // reboot after watchdog timer expire
|
||||
set(PM_RSTC, PM_PASSWORD | 0x20); // full reset
|
||||
set(PM_WDOG, PM_PASSWORD | tick); // number of watchdog tick
|
||||
}
|
||||
|
||||
void reboot(void)
|
||||
{
|
||||
reset(1 << 16);
|
||||
}
|
||||
|
||||
int shell(file_node_t *initrd_root)
|
||||
{
|
||||
uart_puts("# ");
|
||||
|
||||
char buf[INPUT_BUFLEN], ch;
|
||||
int sz = 0;
|
||||
while ((ch = uart_getc()) != '\n') {
|
||||
buf[sz++] = ch;
|
||||
uart_send(ch);
|
||||
}
|
||||
uart_puts(ENDL);
|
||||
buf[sz] = '\0';
|
||||
|
||||
const char *i = buf;
|
||||
|
||||
char bin[INPUT_BUFLEN], *j;
|
||||
for (j = bin; *i != '\0' && !isspace(*i); ++i, ++j)
|
||||
*j = *i;
|
||||
*j = '\0';
|
||||
|
||||
for (; *i != '\0' && isspace(*i); ++i);
|
||||
|
||||
char param[INPUT_BUFLEN];
|
||||
for (j = param; *i != '\0' && !isspace(*i); ++i, ++j)
|
||||
*j = *i;
|
||||
*j = '\0';
|
||||
|
||||
if (!strcmp(bin, "help")) {
|
||||
help();
|
||||
} else if (!strcmp(bin, "hello")) {
|
||||
hello();
|
||||
} else if (!strcmp(bin, "hwinfo")) {
|
||||
hwinfo();
|
||||
} else if (!strcmp(bin, "memalloc")){
|
||||
memalloc((size_t)atoi32(param));
|
||||
} else if (!strcmp(bin, "ls")) {
|
||||
ls(initrd_root);
|
||||
} else if (!strcmp(bin, "cat")) {
|
||||
cat(initrd_root, param);
|
||||
} else if (!strcmp(bin, "reboot")) {
|
||||
reboot();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user