156 lines
3.9 KiB
C
156 lines
3.9 KiB
C
//
|
|
// Created by rick on 23-02-21.
|
|
//
|
|
|
|
#include "command.h"
|
|
#include "libk/kprint.h"
|
|
#include "libk/libk.h"
|
|
#include <types.h>
|
|
#include <libc/kprintf.h>
|
|
#include <stdbool.h>
|
|
#include <libc/string.h>
|
|
#include <mem/mem.h>
|
|
#include <mem/malloc.h>
|
|
#include <cpu/timer.h>
|
|
#include <drivers/pci.h>
|
|
#include <drivers/ide.h>
|
|
#include <fs/blockdev.h>
|
|
#include <fs/mbr.h>
|
|
#include <libc/readline.h>
|
|
#include <libc/libc.h>
|
|
#include <attributes.h>
|
|
#include <util/power.h>
|
|
|
|
#define BOOTLOADER_NAME_MAX_LENGTH 64
|
|
#define CMDLINE_MAX_LENGTH 256
|
|
|
|
char bootloader_name[BOOTLOADER_NAME_MAX_LENGTH];
|
|
char cmdline[CMDLINE_MAX_LENGTH];
|
|
|
|
typedef void (*cmd_handler_func)(const char *);
|
|
|
|
typedef struct {
|
|
char *cmd;
|
|
cmd_handler_func handler;
|
|
} cmd_handler;
|
|
|
|
void print(const char *arg);
|
|
|
|
void ide(const char *arg);
|
|
|
|
void echo(const char *arg);
|
|
|
|
void help(const char *args);
|
|
|
|
void shutdown(const char *args);
|
|
|
|
cmd_handler cmd_handlers[] = {
|
|
{"help\0", help},
|
|
{"echo\0", echo},
|
|
{"print\0", print},
|
|
{"ide\0", ide},
|
|
{"shutdown", shutdown},
|
|
{NULL, NULL},
|
|
};
|
|
|
|
void shutdown(const char *args) {
|
|
power_shutdown();
|
|
}
|
|
|
|
void print_bootinfo() {
|
|
printf("Bootloader name: %s\n"
|
|
"cmdline: %s\n",
|
|
(bootloader_name[0] == 0 ? "NULL" : bootloader_name),
|
|
(cmdline[0] == 0 ? "NULL" : cmdline));
|
|
}
|
|
|
|
|
|
void help(const char *arg) {
|
|
kprint("Available commands:\n");
|
|
int index = 0;
|
|
while (true) {
|
|
if (cmd_handlers[index].cmd == NULL) {
|
|
break;
|
|
}
|
|
printf("%s\n", cmd_handlers[index].cmd);
|
|
index += 1;
|
|
}
|
|
|
|
}
|
|
|
|
void echo(const char *arg) {
|
|
printf("%s\n", arg);
|
|
}
|
|
|
|
void print(const char *arg) {
|
|
if (strcmp(arg, "mmap") == 0) {
|
|
print_mmap_info();
|
|
} else if (strcmp(arg, "malloc") == 0) {
|
|
print_malloc_info();
|
|
} else if (strcmp(arg, "tick") == 0) {
|
|
print_current_tick();
|
|
} else if (strcmp(arg, "bootinfo") == 0) {
|
|
print_bootinfo();
|
|
} else if (strcmp(arg, "pci") == 0) {
|
|
pci_print_info();
|
|
} else if (strcmp(arg, "pci_caps") == 0) {
|
|
pci_dump_caps();
|
|
} else if (strcmp(arg, "ide") == 0) {
|
|
ide_print_devices();
|
|
} else {
|
|
printf("Unknown print %s\n", arg);
|
|
}
|
|
}
|
|
|
|
void ide(const char *arg) {
|
|
if (strcmp(arg, "block_devices") == 0) {
|
|
block_dev_print_info();
|
|
} else if (strcmp(arg, "ide_devices") == 0) {
|
|
ide_print_devices();
|
|
} else if (strcmp(arg, "mbr") == 0) {
|
|
mbr_read_from_ide(0);
|
|
} else {
|
|
printf("Unknown print %s\n", arg);
|
|
}
|
|
}
|
|
|
|
void store_bootloader_info(multiboot_info_t *multiboot_info) {
|
|
// get bootloader and cmdline
|
|
if (multiboot_info->flags & MULTIBOOT_INFO_CMDLINE) {
|
|
int cmdline_length = strlen((const char *) multiboot_info->cmdline);
|
|
if (cmdline_length > CMDLINE_MAX_LENGTH) {
|
|
k_panics("cmdline to long!\n");
|
|
}
|
|
memcpy(cmdline, (char *) multiboot_info->cmdline, cmdline_length);
|
|
}
|
|
if (multiboot_info->flags & MULTIBOOT_INFO_BOOT_LOADER_NAME) {
|
|
int bootloader_length = strlen((const char *) multiboot_info->boot_loader_name);
|
|
if (bootloader_length > BOOTLOADER_NAME_MAX_LENGTH) {
|
|
k_panics("bootloader name to long!\n");
|
|
}
|
|
memcpy(bootloader_name, (char *) multiboot_info->boot_loader_name, bootloader_length);
|
|
}
|
|
}
|
|
|
|
void noreturn main_loop(void *data) {
|
|
while (true) {
|
|
char *msg = readline(NULL);
|
|
char *args = strchr(msg, ' ');
|
|
args[0] = 0;
|
|
args = &args[1];
|
|
int test_index = 0;
|
|
while (true) {
|
|
if (cmd_handlers[test_index].cmd == 0) {
|
|
printf("Unknown command: %s\n", msg);
|
|
break;
|
|
}
|
|
if (strcmp(cmd_handlers[test_index].cmd, msg) == 0) {
|
|
cmd_handlers[test_index].handler(args);
|
|
break;
|
|
}
|
|
test_index++;
|
|
}
|
|
free(msg);
|
|
}
|
|
}
|