From 5c8f8defe067127114f488bca5357d7c18b80ec2 Mon Sep 17 00:00:00 2001 From: Maxime Bloch Date: Tue, 28 Jan 2020 23:10:35 +0100 Subject: [PATCH] Refactor to use the print function, at byte formatter, at memdump cmd --- kernel/memory.c | 12 +++-- kernel/shell.c | 119 ++++++++++++++++++++++-------------------- kernel/util/printer.c | 10 ++++ 3 files changed, 81 insertions(+), 60 deletions(-) diff --git a/kernel/memory.c b/kernel/memory.c index 6a57491..f1aaf13 100644 --- a/kernel/memory.c +++ b/kernel/memory.c @@ -68,7 +68,8 @@ void free(void *data) { } void print_memory() { - terminal_write("==== MEM DUMP ====\n", 19); + print("____________________\n"); + print(" === MEM DUMP === \n"); page_tag *curr_page = start; int i = 0; while (curr_page != NULL) { @@ -79,7 +80,7 @@ void print_memory() { void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size; if (empty_start + sizeof(page_tag) < (void *) curr_page->next) { - print("_: empty_page (%d, %d)\n", + print("_: empty page (%d, %d)\n", sizeof(page_tag), (void *) curr_page->next - (empty_start + sizeof(page_tag))); @@ -90,8 +91,13 @@ void print_memory() { curr_page = curr_page->next; i += 1; } - terminal_write("------------------\n\n", 20); + print("____________________\n"); } + +int command_mem_dump(char*string){ + print_memory(); +} + // //void test_allocs() { // void *ptr0 = __alloc(64); diff --git a/kernel/shell.c b/kernel/shell.c index 956754e..f30a1fd 100644 --- a/kernel/shell.c +++ b/kernel/shell.c @@ -8,28 +8,29 @@ #include "terminal.c" #include "inline_asm.c" #include "drivers/keyboard/keyboard.c" +#include "memory.c" char buffer[SHELL_CMD_BUFFER_SIZE]; int buffer_idx = 0; -int echo(char* input) { +int echo(char *input) { terminal_writestring(input); terminal_putchar('\n'); return 0; } -int hello(char* unused) { +int hello(char *unused) { terminal_writestring("Hello, world!\n"); return 0; } -int cls(char* unused) { +int cls(char *unused) { terminal_initialize(); return 0; } -int get_gdt(char* unused) { - gdt_desc desc = {2,2}; +int get_gdt(char *unused) { + gdt_desc desc = {2, 2}; sgdt(&desc); terminal_writestring("limit = "); terminal_writeint(desc.limit, 10); @@ -37,79 +38,77 @@ int get_gdt(char* unused) { terminal_writeint(desc.base, 16); terminal_putchar('\n'); - gdt_entry* entries = (gdt_entry*) desc.base; - int num_entries = (desc.limit+1) / 8; + gdt_entry *entries = (gdt_entry *) desc.base; + int num_entries = (desc.limit + 1) / 8; for (int entry_num = 0; entry_num < num_entries; entry_num++) { gdt_entry entry = entries[entry_num]; uint32_t base = entry.base_lower | entry.base_middle << 16 | entry.base_higher << 24; uint32_t limit = entry.limit_lower | (entry.flags_limit_higher & 0x0f) << 16; uint8_t flags = (entry.flags_limit_higher >> 4); bool is_data = ((entry.access_byte & 0b00001000) >> 3) == 0; - + //terminal_writestring("\nEntry "); //terminal_writeint(entry_num, 10); - terminal_writestring("base = 0x"); - terminal_writeint(base, 16); - terminal_writestring("\nlimit = 0x"); - terminal_writeint(limit, 16); - terminal_writestring("\nflags = 0b"); - terminal_writeint((entry.flags_limit_higher >> 4), 2); + print("base = %x\n", base); + print("limit = %x\n", limit); + print("flags = %b\n", (entry.flags_limit_higher >> 4)); if ((flags & 0b1000) == 0) { - terminal_writestring(" (byte granularity"); + print(" (byte granularity"); } else { - terminal_writestring(" (page granularity"); + print(" (page granularity"); } if ((flags & 0b0100) == 0) { - terminal_writestring(", 16 bit)"); + print(", 16 bit)"); } else { - terminal_writestring(", 32 bit)"); + print(", 32 bit)"); } + print("\n"); - terminal_writestring("\naccess = 0b"); - terminal_writeint(entry.access_byte, 2); - terminal_writestring(" (ring "); - terminal_writeint((entry.access_byte & 0b01100000) >> 5, 10); + print("access = %b (ring %d", + entry.access_byte, + (entry.access_byte & 0b01100000) >> 5 + ); if ((entry.access_byte & 0b00010000) == 0) { - terminal_writestring(", System"); + print(", System"); } if (is_data) { - terminal_writestring(", Data"); + print(", Data"); - if((entry.access_byte & 0b00000100) == 0) { - terminal_writestring(" (growing up, "); + if ((entry.access_byte & 0b00000100) == 0) { + print(" (growing up, "); } else { - terminal_writestring(" (growing down, "); + print(" (growing down, "); } - if((entry.access_byte & 0b00000010) == 0) { - terminal_writestring("r--)"); + if ((entry.access_byte & 0b00000010) == 0) { + print("r--)"); } else { - terminal_writestring("rw-)"); + print("rw-)"); } } else { - terminal_writestring(", Code"); + print(", Code"); - if((entry.access_byte & 0b00000100) == 0) { - terminal_writestring(" (non-conforming, "); + if ((entry.access_byte & 0b00000100) == 0) { + print(" (non-conforming, "); } else { - terminal_writestring(" (conforming, "); + print(" (conforming, "); } - if((entry.access_byte & 0b00000010) == 0) { - terminal_writestring("--x)"); + if ((entry.access_byte & 0b00000010) == 0) { + print("--x)"); } else { - terminal_writestring("r-x)"); + print("r-x)"); } } - terminal_writestring(")\n"); + print(")\n"); } return 0; } -int ree(char* unused) { +int ree(char *unused) { terminal_initialize(); terminal_putchar('R'); for (int i = 1; i < VGA_WIDTH * VGA_HEIGHT; i++) { @@ -118,27 +117,33 @@ int ree(char* unused) { return 0; } +int save_text(char *text) { + +} + // TODO This is ugly, fix this -const char* shell_commands_strings[] = { - "echo", - "hello", - "cls", - "ree", - "getgdt", - NULL +const char *shell_commands_strings[] = { + "echo", + "hello", + "cls", + "ree", + "getgdt", + "memdump", + NULL }; -int (*shell_commands_functions[]) (char*) = { - echo, - hello, - cls, - ree, - get_gdt +int (*shell_commands_functions[])(char *) = { + echo, + hello, + cls, + ree, + get_gdt, + command_mem_dump, }; -int run_command(char* buffer) { +int run_command(char *buffer) { - if(buffer[0] == 0) { + if (buffer[0] == 0) { return 0; } char command[SHELL_CMD_BUFFER_SIZE] = {0}; @@ -150,9 +155,9 @@ int run_command(char* buffer) { int command_idx = 0; - while(shell_commands_strings[command_idx] != NULL) { + while (shell_commands_strings[command_idx] != NULL) { int check_idx = 0; - while(command[check_idx] != 0 && shell_commands_strings[command_idx][check_idx] == command[check_idx]) { + while (command[check_idx] != 0 && shell_commands_strings[command_idx][check_idx] == command[check_idx]) { check_idx++; } if (command[check_idx] == 0 && shell_commands_strings[command_idx][check_idx] == 0) { @@ -176,7 +181,7 @@ void shell_step() { buffer_idx = 0; if (result == -1) { - terminal_writestring("No such command\n"); + print("No such command\n"); } } else if (curr_char == 0x08) { if (buffer_idx != 0) { diff --git a/kernel/util/printer.c b/kernel/util/printer.c index a82599e..4237d3e 100644 --- a/kernel/util/printer.c +++ b/kernel/util/printer.c @@ -14,6 +14,7 @@ * - %c: character * - %d: digit (integer) * - %x: hexadecimal value. Can be used to print pointers to + * - %b: binary value. Digits with base 2 * @param fmt Formatter string * @param ... Variable amount of arguments to be inserted */ @@ -46,10 +47,19 @@ void print(const char *fmt, ...) { terminal_writestring(s); break; case 'x': + terminal_writestring("0x"); + i = va_arg(argp, int); s = itoa(i, fmtbuf, 16); terminal_writestring(s); break; + case 'b': + terminal_writestring("0b"); + + i = va_arg(argp, int); + s = itoa(i, fmtbuf, 2); + terminal_writestring(s); + break; case '%': terminal_putchar('%'); break;