Refactor to use the print function, add byte formatter, at memdump cmd

This commit is contained in:
Maxime Bloch 2020-01-28 23:10:35 +01:00
parent 831b49d8dd
commit 5e1a12239a
No known key found for this signature in database
GPG Key ID: CE32A7D95B7D6418
3 changed files with 81 additions and 60 deletions

View File

@ -68,7 +68,8 @@ void free(void *data) {
} }
void print_memory() { void print_memory() {
terminal_write("==== MEM DUMP ====\n", 19); print("____________________\n");
print(" === MEM DUMP === \n");
page_tag *curr_page = start; page_tag *curr_page = start;
int i = 0; int i = 0;
while (curr_page != NULL) { while (curr_page != NULL) {
@ -79,7 +80,7 @@ void print_memory() {
void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size; void *empty_start = (void *) curr_page + sizeof(page_tag) + curr_page->size;
if (empty_start + sizeof(page_tag) < (void *) curr_page->next) { 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), sizeof(page_tag),
(void *) curr_page->next - (void *) curr_page->next -
(empty_start + sizeof(page_tag))); (empty_start + sizeof(page_tag)));
@ -90,8 +91,13 @@ void print_memory() {
curr_page = curr_page->next; curr_page = curr_page->next;
i += 1; i += 1;
} }
terminal_write("------------------\n\n", 20); print("____________________\n");
} }
int command_mem_dump(char*string){
print_memory();
}
// //
//void test_allocs() { //void test_allocs() {
// void *ptr0 = __alloc(64); // void *ptr0 = __alloc(64);

View File

@ -8,28 +8,29 @@
#include "terminal.c" #include "terminal.c"
#include "inline_asm.c" #include "inline_asm.c"
#include "drivers/keyboard/keyboard.c" #include "drivers/keyboard/keyboard.c"
#include "memory.c"
char buffer[SHELL_CMD_BUFFER_SIZE]; char buffer[SHELL_CMD_BUFFER_SIZE];
int buffer_idx = 0; int buffer_idx = 0;
int echo(char* input) { int echo(char *input) {
terminal_writestring(input); terminal_writestring(input);
terminal_putchar('\n'); terminal_putchar('\n');
return 0; return 0;
} }
int hello(char* unused) { int hello(char *unused) {
terminal_writestring("Hello, world!\n"); terminal_writestring("Hello, world!\n");
return 0; return 0;
} }
int cls(char* unused) { int cls(char *unused) {
terminal_initialize(); terminal_initialize();
return 0; return 0;
} }
int get_gdt(char* unused) { int get_gdt(char *unused) {
gdt_desc desc = {2,2}; gdt_desc desc = {2, 2};
sgdt(&desc); sgdt(&desc);
terminal_writestring("limit = "); terminal_writestring("limit = ");
terminal_writeint(desc.limit, 10); terminal_writeint(desc.limit, 10);
@ -37,79 +38,77 @@ int get_gdt(char* unused) {
terminal_writeint(desc.base, 16); terminal_writeint(desc.base, 16);
terminal_putchar('\n'); terminal_putchar('\n');
gdt_entry* entries = (gdt_entry*) desc.base; gdt_entry *entries = (gdt_entry *) desc.base;
int num_entries = (desc.limit+1) / 8; int num_entries = (desc.limit + 1) / 8;
for (int entry_num = 0; entry_num < num_entries; entry_num++) { for (int entry_num = 0; entry_num < num_entries; entry_num++) {
gdt_entry entry = 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 base = entry.base_lower | entry.base_middle << 16 | entry.base_higher << 24;
uint32_t limit = entry.limit_lower | (entry.flags_limit_higher & 0x0f) << 16; uint32_t limit = entry.limit_lower | (entry.flags_limit_higher & 0x0f) << 16;
uint8_t flags = (entry.flags_limit_higher >> 4); uint8_t flags = (entry.flags_limit_higher >> 4);
bool is_data = ((entry.access_byte & 0b00001000) >> 3) == 0; bool is_data = ((entry.access_byte & 0b00001000) >> 3) == 0;
//terminal_writestring("\nEntry "); //terminal_writestring("\nEntry ");
//terminal_writeint(entry_num, 10); //terminal_writeint(entry_num, 10);
terminal_writestring("base = 0x"); print("base = %x\n", base);
terminal_writeint(base, 16); print("limit = %x\n", limit);
terminal_writestring("\nlimit = 0x"); print("flags = %b\n", (entry.flags_limit_higher >> 4));
terminal_writeint(limit, 16);
terminal_writestring("\nflags = 0b");
terminal_writeint((entry.flags_limit_higher >> 4), 2);
if ((flags & 0b1000) == 0) { if ((flags & 0b1000) == 0) {
terminal_writestring(" (byte granularity"); print(" (byte granularity");
} else { } else {
terminal_writestring(" (page granularity"); print(" (page granularity");
} }
if ((flags & 0b0100) == 0) { if ((flags & 0b0100) == 0) {
terminal_writestring(", 16 bit)"); print(", 16 bit)");
} else { } else {
terminal_writestring(", 32 bit)"); print(", 32 bit)");
} }
print("\n");
terminal_writestring("\naccess = 0b"); print("access = %b (ring %d",
terminal_writeint(entry.access_byte, 2); entry.access_byte,
terminal_writestring(" (ring "); (entry.access_byte & 0b01100000) >> 5
terminal_writeint((entry.access_byte & 0b01100000) >> 5, 10); );
if ((entry.access_byte & 0b00010000) == 0) { if ((entry.access_byte & 0b00010000) == 0) {
terminal_writestring(", System"); print(", System");
} }
if (is_data) { if (is_data) {
terminal_writestring(", Data"); print(", Data");
if((entry.access_byte & 0b00000100) == 0) { if ((entry.access_byte & 0b00000100) == 0) {
terminal_writestring(" (growing up, "); print(" (growing up, ");
} else { } else {
terminal_writestring(" (growing down, "); print(" (growing down, ");
} }
if((entry.access_byte & 0b00000010) == 0) { if ((entry.access_byte & 0b00000010) == 0) {
terminal_writestring("r--)"); print("r--)");
} else { } else {
terminal_writestring("rw-)"); print("rw-)");
} }
} else { } else {
terminal_writestring(", Code"); print(", Code");
if((entry.access_byte & 0b00000100) == 0) { if ((entry.access_byte & 0b00000100) == 0) {
terminal_writestring(" (non-conforming, "); print(" (non-conforming, ");
} else { } else {
terminal_writestring(" (conforming, "); print(" (conforming, ");
} }
if((entry.access_byte & 0b00000010) == 0) { if ((entry.access_byte & 0b00000010) == 0) {
terminal_writestring("--x)"); print("--x)");
} else { } else {
terminal_writestring("r-x)"); print("r-x)");
} }
} }
terminal_writestring(")\n"); print(")\n");
} }
return 0; return 0;
} }
int ree(char* unused) { int ree(char *unused) {
terminal_initialize(); terminal_initialize();
terminal_putchar('R'); terminal_putchar('R');
for (int i = 1; i < VGA_WIDTH * VGA_HEIGHT; i++) { for (int i = 1; i < VGA_WIDTH * VGA_HEIGHT; i++) {
@ -118,27 +117,33 @@ int ree(char* unused) {
return 0; return 0;
} }
int save_text(char *text) {
}
// TODO This is ugly, fix this // TODO This is ugly, fix this
const char* shell_commands_strings[] = { const char *shell_commands_strings[] = {
"echo", "echo",
"hello", "hello",
"cls", "cls",
"ree", "ree",
"getgdt", "getgdt",
NULL "memdump",
NULL
}; };
int (*shell_commands_functions[]) (char*) = { int (*shell_commands_functions[])(char *) = {
echo, echo,
hello, hello,
cls, cls,
ree, ree,
get_gdt 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; return 0;
} }
char command[SHELL_CMD_BUFFER_SIZE] = {0}; char command[SHELL_CMD_BUFFER_SIZE] = {0};
@ -150,9 +155,9 @@ int run_command(char* buffer) {
int command_idx = 0; int command_idx = 0;
while(shell_commands_strings[command_idx] != NULL) { while (shell_commands_strings[command_idx] != NULL) {
int check_idx = 0; 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++; check_idx++;
} }
if (command[check_idx] == 0 && shell_commands_strings[command_idx][check_idx] == 0) { if (command[check_idx] == 0 && shell_commands_strings[command_idx][check_idx] == 0) {
@ -176,7 +181,7 @@ void shell_step() {
buffer_idx = 0; buffer_idx = 0;
if (result == -1) { if (result == -1) {
terminal_writestring("No such command\n"); print("No such command\n");
} }
} else if (curr_char == 0x08) { } else if (curr_char == 0x08) {
if (buffer_idx != 0) { if (buffer_idx != 0) {

View File

@ -14,6 +14,7 @@
* - %c: character * - %c: character
* - %d: digit (integer) * - %d: digit (integer)
* - %x: hexadecimal value. Can be used to print pointers to * - %x: hexadecimal value. Can be used to print pointers to
* - %b: binary value. Digits with base 2
* @param fmt Formatter string * @param fmt Formatter string
* @param ... Variable amount of arguments to be inserted * @param ... Variable amount of arguments to be inserted
*/ */
@ -46,10 +47,19 @@ void print(const char *fmt, ...) {
terminal_writestring(s); terminal_writestring(s);
break; break;
case 'x': case 'x':
terminal_writestring("0x");
i = va_arg(argp, int); i = va_arg(argp, int);
s = itoa(i, fmtbuf, 16); s = itoa(i, fmtbuf, 16);
terminal_writestring(s); terminal_writestring(s);
break; break;
case 'b':
terminal_writestring("0b");
i = va_arg(argp, int);
s = itoa(i, fmtbuf, 2);
terminal_writestring(s);
break;
case '%': case '%':
terminal_putchar('%'); terminal_putchar('%');
break; break;