Skip to content

Commit

Permalink
"Adding User Shell"
Browse files Browse the repository at this point in the history
  • Loading branch information
baponkar committed Nov 3, 2024
1 parent f4e15bb commit 097fa33
Show file tree
Hide file tree
Showing 34 changed files with 487,535 additions and 48,967 deletions.
62 changes: 27 additions & 35 deletions source-code/x86_32/0.12.2/src/usr/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,45 +77,37 @@ void reboot(){



// This C function will print the register values passed via the stack.
void print_registers_c(uint32_t edi, uint32_t esi, uint32_t ebp, uint32_t esp,
uint32_t ebx, uint32_t edx, uint32_t ecx, uint32_t eax) {

// Define a structure to match the order of registers in the assembly function
struct registers {
uint64_t rax, rbx, rcx, rdx;
uint64_t rsi, rdi, rbp;
uint64_t r8, r9, r10, r11;
uint64_t r12, r13, r14, r15;
};

// Implement the print_registers_c function
void print_registers_c(struct registers* regs) {
printf("Register Values:\n");

printf("EAX: ");
print_hex(eax);
printf("\n");

printf("EBX: ");
print_hex(ebx);
printf("\n");

printf("ECX: ");
print_hex(ecx);
printf("\n");

printf("EDX: ");
print_hex(edx);
printf("\n");

printf("ESI: ");
print_hex(esi);
printf("\n");

printf("EDI: ");
print_hex(edi);
printf("\n");

printf("EBP: ");
print_hex(ebp);
printf("\n");

printf("ESP: ");
print_hex(esp);
printf("\n");
printf("RAX: %x\n", regs->rax);
printf("RBX: %x\n", regs->rbx);
printf("RCX: %x\n", regs->rcx);
printf("RDX: %x\n", regs->rdx);
printf("RSI: %x\n", regs->rsi);
printf("RDI: %x\n", regs->rdi);
printf("RBP: %x\n", regs->rbp);
printf("R8 : %x\n", regs->r8);
printf("R9 : %x\n", regs->r9);
printf("R10: %x\n", regs->r10);
printf("R11: %x\n", regs->r11);
printf("R12: %x\n", regs->r12);
printf("R13: %x\n", regs->r13);
printf("R14: %x\n", regs->r14);
printf("R15: %x\n", regs->r15);
}



void print_features(){
printf("Features:\n");
printf("1. GRUB2 Bootloading\n");
Expand Down
Binary file modified source-code/x86_64/0.1/build/image.iso
Binary file not shown.
Binary file modified source-code/x86_64/0.1/build/kernel.bin
Binary file not shown.
44,851 changes: 365 additions & 44,486 deletions source-code/x86_64/0.1/qemu.log

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions source-code/x86_64/0.2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ $(BUILD_DIR)/kernel.o: $(SRC_DIR)/kernel/kernel.c
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/util/util.c -o $(BUILD_DIR)/util.o


$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/driver/vga.c -o $(BUILD_DIR)/vga.o
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/driver/font.c -o $(BUILD_DIR)/font.o
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/driver/vga/vga.c -o $(BUILD_DIR)/vga.o
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/driver/vga/font.c -o $(BUILD_DIR)/font.o
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/driver/ports.c -o $(BUILD_DIR)/ports.o
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/driver/keyboard.c -o $(BUILD_DIR)/keyboard.o

$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/usr/shell.c -o $(BUILD_DIR)/shell.o
$(NASM) $(NASM_FLAG) $(SRC_DIR)/usr/print_reg_values.s -o $(BUILD_DIR)/print_reg_values.o


$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/stdlib/stdlib.c -o $(BUILD_DIR)/stdlib.o
$(GCC) $(GCC_FLAG) -c $(SRC_DIR)/stdlib/stdio.c -o $(BUILD_DIR)/stdio.o
Expand Down Expand Up @@ -75,7 +78,9 @@ $(BUILD_DIR)/kernel.bin: $(BUILD_DIR)/kernel.o \
$(BUILD_DIR)/idt.o \
$(BUILD_DIR)/idt_s.o \
$(BUILD_DIR)/timer.o \
$(BUILD_DIR)/keyboard.o
$(BUILD_DIR)/keyboard.o \
$(BUILD_DIR)/shell.o \
$(BUILD_DIR)/print_reg_values.o



Expand All @@ -94,7 +99,9 @@ $(BUILD_DIR)/kernel.bin: $(BUILD_DIR)/kernel.o \
$(BUILD_DIR)/idt.o \
$(BUILD_DIR)/idt_s.o \
$(BUILD_DIR)/timer.o \
$(BUILD_DIR)/keyboard.o
$(BUILD_DIR)/keyboard.o \
$(BUILD_DIR)/shell.o \
$(BUILD_DIR)/print_reg_values.o


# Creating ISO image
Expand Down
3 changes: 2 additions & 1 deletion source-code/x86_64/0.2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
* Automated Building by Makefile
* Update register_t struct
* Keyboard Driver implementation
* User Shell Implementation

## Screenshot
![Screenshot](../../../image/keblaos_x86_64_v0.1_screenshot.png)
![Screenshot](./image/screenshot_x86_64_v0.2.png)



Expand Down
Binary file modified source-code/x86_64/0.2/build/image.iso
Binary file not shown.
Binary file modified source-code/x86_64/0.2/build/kernel.bin
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
490,454 changes: 486,118 additions & 4,336 deletions source-code/x86_64/0.2/qemu.log

Large diffs are not rendered by default.

227 changes: 213 additions & 14 deletions source-code/x86_64/0.2/src/driver/keyboard.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@

#include "keyboard.h"

#define BUFFER_SIZE 128 // Max Size of Command Buffer
char COMMAND_BUFFER[BUFFER_SIZE]; // Command string Container Buffer
int BUFFER_INDEX = 0; // Current Length of Command String Buffer

uint32_t scanCode; // What key is pressed
bool press; // Press down, or released

bool shift = false; // Shift Key pressed or not
bool capsLock = false; // Caps Lock Key pressed or not




const uint32_t lowercase[128] = {
UNKNOWN,ESC,'1','2','3','4','5','6','7','8',
'9','0','-','=','\b','\t','q','w','e','r',
't','y','u','i','o','p','[',']','\n',CTRL,
'a','s','d','f','g','h','j','k','l',';',
'\'','`',LSHIFT,'\\','z','x','c','v','b','n',
'm',',','.','/',RSHIFT,'*',ALT,' ',CAPS_LOCK,F1,
F2,F3,F4,F5,F6,F7,F8,F9,F10,NUM_LOCK,SCROLL_LOCK,
HOME,UP,PAGE_UP,'-',LEFT,UNKNOWN,RIGHT,'+',END,
DOWN,PAGE_DOWN,INSERT,DELETE,UNKNOWN,UNKNOWN,UNKNOWN,F11,F12,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN
};


const uint32_t uppercase[128] = {
UNKNOWN,ESC,'!','@','#','$','%','^','&','*',
'(',')','_','+','\b','\t','Q','W','E','R',
'T','Y','U','I','O','P','{','}','\n',CTRL,
'A','S','D','F','G','H','J','K','L',':',
'"','~',LSHIFT,'|','Z','X','C','V','B','N',
'M','<','>','?',RSHIFT,'*',ALT,' ',CAPS_LOCK,F1,
F2,F3,F4,F5,F6,F7,F8,F9,F10,NUM_LOCK,SCROLL_LOCK,
HOME,UP,PAGE_UP,'-',LEFT,UNKNOWN,RIGHT,'+',END,
DOWN,PAGE_DOWN,INSERT,DELETE,UNKNOWN,UNKNOWN,UNKNOWN,F11,F12,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN
};


int getScanCode(){
int scancode = inb(0x60) & 0x7F;
print_dec(scancode);
return scancode;
return inb(0x60) & 0x7F;
}


bool getKeyState(){
char key_state = (char) inb(0x60) & 0x80; // Press down return 0x0000000, or released return 0xfffff80

Expand All @@ -19,22 +66,174 @@ bool getKeyState(){
}
}

void keyboard_handler(registers_t *reg){
int scancode = getScanCode();
bool keystate = getKeyState();
print("Scan code: ");
print_dec(scancode);
print("\n");
print("Keystate: ");
print_dec(keystate);
print("\n");

char scanCodeToChar(uint32_t scanCode) {
if(scanCode == 0xE0){
scanCode = (scanCode | 0x100); // Example: Combine with a base to indicate extended
}

if (scanCode >= NUM_1 && scanCode <= NUM_0) {
// Handles NUM_0 to NUM_9, using the shift flag
return shift ? uppercase[scanCode] : lowercase[scanCode];
} else {
// For other keys, check capsLock or other flags
return capsLock ? uppercase[scanCode] : lowercase[scanCode];
}
}

void key_ctrl(uint32_t scanCode, bool keyPress){
switch(scanCode){
case 0x00000060: // Extra scan code returning
break;
case 0x00000608: // Extra scan code returning
break;
case UNKNOWN:
case ESC: // ESC Key
// beep();
break;
case ENTER: // Enter Key Manage
handel_enter_key(keyPress);
break;
case CTRL: // CTRL
break;
case ALT: // ALT
break;
case F1: // F1
break;
case F2: // F2
break;
case F3: // F3
break;
case F4: // F4
break;
case F5: // F5
break;
case F6: // F6
break;
case F7: // F7
break;
case F8: // F8
break;
case F9: // F9
break;
case F10: // F10
break;
case DELETE: // DELETEete key
handel_del_key(keyPress);
case F11: // F11
break;
case F12: // F12
break;
case LSHIFT: // Left shift key
handel_shift_key(keyPress);
break;
case RSHIFT: // Right shift key
handel_shift_key(keyPress);
break;
case CAPS_LOCK: // Caps Lock Key
handel_caps_lock_key(keyPress);
break;
case UP:
// if(press == true){
// move_cursor_up();
// }
break;
case LEFT:
if(keyPress == true){
move_cur_left();
}
break;
case RIGHT:
if(keyPress == true){
move_cur_right(BUFFER_INDEX + 2);
}
break;
case DOWN:
// if(keyPress == true){
// move_cursor_down();
// }
break;
case BACKSPACE:
handel_backspace_key(keyPress);
break;
default:
if(keyPress == true){
putchar(scanCodeToChar(scanCode));
COMMAND_BUFFER[BUFFER_INDEX] = scanCodeToChar(scanCode);
BUFFER_INDEX++;
}
break;
}
}

void init_keyboard(){
void handel_enter_key(bool keyPressed){
if(keyPressed == false){
create_newline();
execute_command(COMMAND_BUFFER);
clear_buffer(COMMAND_BUFFER, BUFFER_SIZE);
BUFFER_INDEX = 0;
}
}

void handel_shift_key(bool keyPressed){
shift = keyPressed;
capsLock = !capsLock;
}

void handel_caps_lock_key(bool keyPressed){
if(keyPressed == true){
capsLock = !capsLock;
}
}

void handel_backspace_key(bool keyPressed){
if(keyPressed == true && BUFFER_INDEX > 0){
backspace_manage();
BUFFER_INDEX--;
COMMAND_BUFFER[BUFFER_INDEX] = '\0';
}
}

void handel_del_key(bool keyPressed){
int cur_pos_col;
if(keyPressed == true){
//del_manage(); // updating screen
cur_pos_col = get_cursor_pos_x() - 2; // 2 for cursor size
for(int i=cur_pos_col; i<BUFFER_INDEX; i++){
COMMAND_BUFFER[i] = COMMAND_BUFFER[i+1]; // Shifting left
}
BUFFER_INDEX--;
}
}

void keyboardHandler(registers_t *regs){
scanCode = getScanCode(); // What key is pressed
press = getKeyState(); // Manage Key Pressed or Released by changing bool variable press
key_ctrl(scanCode, press);
}


void initKeyboard(){
disable_interrupts();
interrupt_install_handler(1, &keyboard_handler);
interrupt_install_handler(1, &keyboardHandler);
enable_interrupts();
}

void disableKeyboard(){
interrupt_uninstall_handler(1);
}




void read_command(char* input) {
BUFFER_INDEX = 0; // Start fresh
while (true) {
// The keyboard handler should populate COMMAND_BUFFER based on key presses
if (COMMAND_BUFFER[BUFFER_INDEX] == '\n') {
// When Enter is pressed, break the loop
input[BUFFER_INDEX] = '\0'; // Null-terminate the input
break;
}
}
}
Loading

0 comments on commit 097fa33

Please sign in to comment.