mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2026-03-09 08:25:19 -07:00
Compare commits
31 Commits
gbowne1-pa
...
gbowne1-ad
| Author | SHA1 | Date | |
|---|---|---|---|
| 8cf7fa7f31 | |||
| 64d69d505f | |||
| f572101d6b | |||
| 3cd2ff6e1e | |||
| aafd4efcb2 | |||
| d6eb5115d5 | |||
| 84705fd225 | |||
| 6c69b5fd6a | |||
| 1037ba4f54 | |||
| 745deeddde | |||
| f9e281a7ae | |||
| 18801a742f | |||
| a08648eff5 | |||
| 5a664c6e31 | |||
| 4c7de228f9 | |||
| af3f20485f | |||
| 09b2b8cd11 | |||
| 4079d18a45 | |||
| b6c158957e | |||
| cca6aafd65 | |||
| 49c1bad935 | |||
|
|
a7b0d1152f | ||
|
|
e38f1aa2ee | ||
|
|
78d5e9a7ab | ||
|
|
daead5ee57 | ||
|
|
4fb81d2e57 | ||
|
|
19ef0a8627 | ||
| c5f7e4e563 | |||
| f516334e0d | |||
| 4047bc3936 | |||
| 7e54f0de66 |
24
.clang-format
Normal file
24
.clang-format
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
BasedOnStyle: Google
|
||||||
|
IndentWidth: 4
|
||||||
|
TabWidth: 4
|
||||||
|
UseTab: Never
|
||||||
|
ColumnLimit: 80
|
||||||
|
|
||||||
|
DerivePointerAlignment: false
|
||||||
|
PointerAlignment: Right
|
||||||
|
ReferenceAlignment: Right
|
||||||
|
|
||||||
|
AlignConsecutiveMacros: Consecutive
|
||||||
|
AlignTrailingComments:
|
||||||
|
Kind: Always
|
||||||
|
OverEmptyLines: 0
|
||||||
|
|
||||||
|
IncludeBlocks: Regroup
|
||||||
|
IncludeCategories:
|
||||||
|
# Std headers
|
||||||
|
- Regex: '<[[:alnum:]_.]+>'
|
||||||
|
Priority: 2
|
||||||
|
|
||||||
|
# Other headers
|
||||||
|
- Regex: '.*'
|
||||||
|
Priority: 1
|
||||||
6
.clangd
Normal file
6
.clangd
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
CompileFlags:
|
||||||
|
CompilationDatabase: build
|
||||||
|
|
||||||
|
Diagnostics:
|
||||||
|
UnusedIncludes: Strict
|
||||||
|
MissingIncludes: Strict
|
||||||
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[Makefile]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 8
|
||||||
|
tab_width = 8
|
||||||
6
Makefile
6
Makefile
@@ -18,9 +18,9 @@ KERNEL_OBJ += $(patsubst kernel/%.asm, $(BUILD_DIR)/asm_%.o, $(KERNEL_ASM_SRC))
|
|||||||
KLIBC_SRC = $(wildcard klibc/src/*.c)
|
KLIBC_SRC = $(wildcard klibc/src/*.c)
|
||||||
KLIBC_OBJ = $(patsubst klibc/src/%.c, $(BUILD_DIR)/klibc/%.o, $(KLIBC_SRC))
|
KLIBC_OBJ = $(patsubst klibc/src/%.c, $(BUILD_DIR)/klibc/%.o, $(KLIBC_SRC))
|
||||||
|
|
||||||
|
.PHONY: all stage1 stage2 kernel compile-commands $(BUILD_DIR)/compile_commands.json run gdb clean clean-cross clean-all
|
||||||
all: $(DISK_IMG)
|
all: $(DISK_IMG)
|
||||||
|
|
||||||
.PHONY: stage1 stage2 kernel run gdb clean
|
|
||||||
stage1: $(BUILD_DIR)
|
stage1: $(BUILD_DIR)
|
||||||
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/$@.o bootloader/$@.asm
|
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/$@.o bootloader/$@.asm
|
||||||
$(LD) -Ttext=0x7c00 -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.o
|
$(LD) -Ttext=0x7c00 -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.o
|
||||||
@@ -57,6 +57,10 @@ $(BUILD_DIR):
|
|||||||
mkdir -p $@
|
mkdir -p $@
|
||||||
mkdir -p $(BUILD_DIR)/klibc
|
mkdir -p $(BUILD_DIR)/klibc
|
||||||
|
|
||||||
|
compile-commands: $(BUILD_DIR)/compile_commands.json
|
||||||
|
$(BUILD_DIR)/compile_commands.json: $(BUILD_DIR)
|
||||||
|
bear --output $@ -- make -B
|
||||||
|
|
||||||
run:
|
run:
|
||||||
qemu-system-i386 -s -S $(DISK_IMG)
|
qemu-system-i386 -s -S $(DISK_IMG)
|
||||||
|
|
||||||
|
|||||||
119
kernel/ata.c
Normal file
119
kernel/ata.c
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#include "ata.h"
|
||||||
|
#include "io.h"
|
||||||
|
#include "print.h"
|
||||||
|
|
||||||
|
#define ATA_TIMEOUT 100000
|
||||||
|
|
||||||
|
static inline void ata_delay(void) {
|
||||||
|
/* 400ns delay by reading alternate status */
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
inb(ATA_PRIMARY_CTRL);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_wait_ready(void) {
|
||||||
|
for (int i = 0; i < ATA_TIMEOUT; i++) {
|
||||||
|
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
|
||||||
|
/* Must NOT be busy AND must be ready */
|
||||||
|
if (!(status & ATA_SR_BSY) && (status & ATA_SR_DRDY))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool ata_wait(uint8_t mask) {
|
||||||
|
for (int i = 0; i < ATA_TIMEOUT; i++) {
|
||||||
|
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
|
||||||
|
/* If ERR is set, stop waiting and return failure */
|
||||||
|
if (status & ATA_SR_ERR) return false;
|
||||||
|
|
||||||
|
if (!(status & ATA_SR_BSY) && (status & mask))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_init(void) {
|
||||||
|
/* Select drive */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, ATA_MASTER);
|
||||||
|
ata_delay();
|
||||||
|
|
||||||
|
/* Check if drive exists */
|
||||||
|
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
|
||||||
|
if (status == 0xFF || status == 0) return false;
|
||||||
|
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
|
||||||
|
ata_delay();
|
||||||
|
|
||||||
|
if (!ata_wait(ATA_SR_DRQ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
uint16_t identify[256];
|
||||||
|
for (int i = 0; i < 256; i++)
|
||||||
|
identify[i] = inw(ATA_PRIMARY_IO);
|
||||||
|
|
||||||
|
print_string("[ATA] Primary master detected\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_read_sector(uint32_t lba, uint8_t* buffer) {
|
||||||
|
if (!buffer) return false;
|
||||||
|
|
||||||
|
/* 1. Wait for drive to be ready for command */
|
||||||
|
if (!ata_wait_ready()) return false;
|
||||||
|
|
||||||
|
/* 2. Setup Task File (LBA28) */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_SECCOUNT0, 1);
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA0, (uint8_t)(lba));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA1, (uint8_t)(lba >> 8));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA2, (uint8_t)(lba >> 16));
|
||||||
|
|
||||||
|
/* 3. Issue Read Command */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
|
||||||
|
|
||||||
|
/* 4. Wait for Data Request (DRQ) */
|
||||||
|
if (!ata_wait(ATA_SR_DRQ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* 5. Transfer data */
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
uint16_t data = inw(ATA_PRIMARY_IO);
|
||||||
|
buffer[i * 2] = data & 0xFF;
|
||||||
|
buffer[i * 2 + 1] = (data >> 8) & 0xFF;
|
||||||
|
}
|
||||||
|
|
||||||
|
ata_delay();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ata_write_sector(uint32_t lba, const uint8_t* buffer) {
|
||||||
|
if (!buffer) return false;
|
||||||
|
|
||||||
|
/* 1. Wait for drive to be ready for command */
|
||||||
|
if (!ata_wait_ready()) return false;
|
||||||
|
|
||||||
|
/* 2. Setup Task File */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_SECCOUNT0, 1);
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA0, (uint8_t)(lba));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA1, (uint8_t)(lba >> 8));
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_LBA2, (uint8_t)(lba >> 16));
|
||||||
|
|
||||||
|
/* 3. Issue Write Command */
|
||||||
|
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
|
||||||
|
|
||||||
|
/* 4. Wait for drive to request data */
|
||||||
|
if (!ata_wait(ATA_SR_DRQ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
/* 5. Transfer data */
|
||||||
|
for (int i = 0; i < 256; i++) {
|
||||||
|
uint16_t word = buffer[i * 2] | (buffer[i * 2 + 1] << 8);
|
||||||
|
outw(ATA_PRIMARY_IO, word);
|
||||||
|
}
|
||||||
|
|
||||||
|
ata_delay();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
44
kernel/ata.h
Normal file
44
kernel/ata.h
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#ifndef ATA_H
|
||||||
|
#define ATA_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
/* ATA I/O ports */
|
||||||
|
#define ATA_PRIMARY_IO 0x1F0
|
||||||
|
#define ATA_PRIMARY_CTRL 0x3F6
|
||||||
|
|
||||||
|
/* ATA registers */
|
||||||
|
#define ATA_REG_DATA 0x00
|
||||||
|
#define ATA_REG_ERROR 0x01
|
||||||
|
#define ATA_REG_FEATURES 0x01
|
||||||
|
#define ATA_REG_SECCOUNT0 0x02
|
||||||
|
#define ATA_REG_LBA0 0x03
|
||||||
|
#define ATA_REG_LBA1 0x04
|
||||||
|
#define ATA_REG_LBA2 0x05
|
||||||
|
#define ATA_REG_HDDEVSEL 0x06
|
||||||
|
#define ATA_REG_COMMAND 0x07
|
||||||
|
#define ATA_REG_STATUS 0x07
|
||||||
|
|
||||||
|
/* ATA commands */
|
||||||
|
#define ATA_CMD_READ_PIO 0x20
|
||||||
|
#define ATA_CMD_WRITE_PIO 0x30
|
||||||
|
#define ATA_CMD_IDENTIFY 0xEC
|
||||||
|
|
||||||
|
/* Status flags */
|
||||||
|
#define ATA_SR_BSY 0x80
|
||||||
|
#define ATA_SR_DRDY 0x40
|
||||||
|
#define ATA_SR_DRQ 0x08
|
||||||
|
#define ATA_SR_ERR 0x01
|
||||||
|
|
||||||
|
/* Drive select */
|
||||||
|
#define ATA_MASTER 0xA0
|
||||||
|
#define ATA_SLAVE 0xB0
|
||||||
|
|
||||||
|
/* Public API */
|
||||||
|
bool ata_init(void);
|
||||||
|
bool ata_read_sector(uint32_t lba, uint8_t* buffer);
|
||||||
|
bool ata_write_sector(uint32_t lba, const uint8_t* buffer);
|
||||||
|
bool ata_wait_ready(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,36 +1,80 @@
|
|||||||
|
#include <string.h>
|
||||||
#include "display.h"
|
#include "display.h"
|
||||||
#include "io.h" // Include your I/O header for port access
|
#include "io.h"
|
||||||
#include "vga.h"
|
#include "vga.h"
|
||||||
|
|
||||||
// Initialize the display
|
// Initialize the display
|
||||||
void init_display(void) {
|
void init_display(void) {
|
||||||
// Initialize VGA settings, if necessary
|
// Initialize the VGA driver. This typically sets up the 80x25 text mode,
|
||||||
// This could involve setting up the VGA mode, etc.
|
// clears the screen, and sets the cursor.
|
||||||
set_display_mode(0x13); // Example: Set to 320x200 256-color mode
|
vga_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enumerate connected displays
|
// Enumerate connected displays
|
||||||
void enumerate_displays(void) {
|
void enumerate_displays(void) {
|
||||||
// This is a simplified example. Actual enumeration may require
|
// This function is often a complex operation in a real driver.
|
||||||
// reading from specific VGA registers or using BIOS interrupts.
|
// In this simplified kernel/VGA text mode environment, we use printf
|
||||||
|
// to output a message and rely on the fact that VGA is present.
|
||||||
|
|
||||||
// For demonstration, we will just print a message
|
// Clear the display before printing a message
|
||||||
// In a real driver, you would check the VGA registers
|
vga_clear(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK));
|
||||||
// to determine connected displays.
|
|
||||||
clear_display();
|
// Output a simplified enumeration message
|
||||||
// Here you would typically read from VGA registers to find connected displays
|
vga_printf("Display: Standard VGA Text Mode (80x25) Detected.\n");
|
||||||
// For example, using inb() to read from VGA ports
|
|
||||||
|
// In a real driver, you would use inb() and outb() with specific VGA ports
|
||||||
|
// to read information (e.g., from the CRTC registers 0x3D4/0x3D5)
|
||||||
|
// to check for display presence or configuration.
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the display mode
|
// Set the display mode
|
||||||
|
// NOTE: Setting arbitrary VGA modes (like 0x13 for 320x200) is very complex
|
||||||
|
// and requires writing hundreds of register values, often done via BIOS in
|
||||||
|
// real mode. Since we are in protected mode and have a simple text driver,
|
||||||
|
// this function is kept simple or treated as a placeholder for full mode changes.
|
||||||
void set_display_mode(uint8_t mode) {
|
void set_display_mode(uint8_t mode) {
|
||||||
// Set the VGA mode by writing to the appropriate registers
|
// Check if the requested mode is a known mode (e.g., VGA Text Mode 3)
|
||||||
outb(VGA_PORT, mode); // Example function to write to a port
|
// For this example, we simply acknowledge the call.
|
||||||
|
// A true mode set would involve complex register sequencing.
|
||||||
|
|
||||||
|
// The provided vga.c is a Text Mode driver, so a graphical mode set
|
||||||
|
// like 0x13 (320x200 256-color) would break the existing vga_printf functionality.
|
||||||
|
|
||||||
|
// A simplified text-mode-specific response:
|
||||||
|
if (mode == 0x03) { // Mode 3 is standard 80x25 text mode
|
||||||
|
vga_printf("Display mode set to 80x25 Text Mode (Mode 0x03).\n");
|
||||||
|
vga_init(); // Re-initialize the text mode
|
||||||
|
} else {
|
||||||
|
// Simple I/O example based on the original structure (Caution: Incomplete for full mode set)
|
||||||
|
outb(VGA_PORT, mode); // Example function to write to a port
|
||||||
|
vga_printf("Attempting to set display mode to 0x%x. (Warning: May break current display)\n", mode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear the display
|
// Clear the display
|
||||||
void clear_display(void) {
|
void clear_display(void) {
|
||||||
// Clear the display by filling it with a color
|
// Use the VGA driver's clear function, typically clearing to black on light grey
|
||||||
// This is a placeholder for actual clearing logic
|
// or black on black. We'll use the black on light grey from vga_init for consistency.
|
||||||
// You would typically write to video memory here
|
vga_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_GREY));
|
||||||
|
// Reset cursor to 0, 0
|
||||||
|
vga_set_cursor_position(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to write a string
|
||||||
|
void display_write_string(const char* str) {
|
||||||
|
// Use the VGA driver's string writing function
|
||||||
|
vga_write_string(str, strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to print a formatted string
|
||||||
|
void display_printf(const char* format, ...) {
|
||||||
|
// Use the VGA driver's printf function
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
|
||||||
|
// The vga_printf function already handles the va_list internally,
|
||||||
|
// so we can just call it directly.
|
||||||
|
vga_printf(format, args);
|
||||||
|
|
||||||
|
va_end(args);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,13 +2,21 @@
|
|||||||
#define DISPLAY_H
|
#define DISPLAY_H
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include "vga.h" // Include VGA functions
|
||||||
|
|
||||||
#define VGA_PORT 0x3C0 // Base port for VGA
|
#define VGA_PORT 0x3C0 // Base port for VGA (Often used for general control, though 0x3D4/0x3D5 are used for cursor)
|
||||||
|
|
||||||
// Function prototypes
|
// Function prototypes
|
||||||
void init_display(void);
|
void init_display(void);
|
||||||
void enumerate_displays(void);
|
void enumerate_displays(void);
|
||||||
void set_display_mode(uint8_t mode);
|
void set_display_mode(uint8_t mode); // In this context, modes are typically BIOS or VESA modes, which are complex.
|
||||||
|
// We'll treat this as a placeholder/simple mode call.
|
||||||
void clear_display(void);
|
void clear_display(void);
|
||||||
|
|
||||||
|
// New function to write a string using the VGA driver
|
||||||
|
void display_write_string(const char* str);
|
||||||
|
|
||||||
|
// New function to print a formatted string using the VGA driver
|
||||||
|
void display_printf(const char* format, ...);
|
||||||
|
|
||||||
#endif // DISPLAY_H
|
#endif // DISPLAY_H
|
||||||
|
|||||||
65
kernel/hid.c
Normal file
65
kernel/hid.c
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#include "hid.h"
|
||||||
|
#include "usb.h"
|
||||||
|
#include "mouse.h"
|
||||||
|
#include "keyboard.h"
|
||||||
|
#include "print.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// Global variables
|
||||||
|
static bool hid_initialized = false;
|
||||||
|
|
||||||
|
void hid_init(void) {
|
||||||
|
if (hid_initialized) return;
|
||||||
|
hid_initialized = true;
|
||||||
|
|
||||||
|
// Initialize keyboard and mouse HID handling
|
||||||
|
keyboard_init();
|
||||||
|
// Assume USB mouse has been initialized and is connected.
|
||||||
|
usb_hid_init(); // Initializes USB HID for both keyboard and mouse
|
||||||
|
}
|
||||||
|
|
||||||
|
void hid_process_report(uint8_t* report, uint8_t length) {
|
||||||
|
// Process the HID report based on its type
|
||||||
|
if (length == 8) { // Assuming a standard 8-byte report for HID keyboard
|
||||||
|
keyboard_hid_report_t* k_report = (keyboard_hid_report_t*) report;
|
||||||
|
hid_process_keyboard_report(k_report);
|
||||||
|
} else if (length == 3) { // Assuming a standard 3-byte report for HID mouse
|
||||||
|
mouse_hid_report_t* m_report = (mouse_hid_report_t*) report;
|
||||||
|
hid_process_mouse_report(m_report);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle HID keyboard report
|
||||||
|
void hid_process_keyboard_report(const keyboard_hid_report_t* report) {
|
||||||
|
// Iterate over the keycodes and process key presses
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
uint8_t keycode = report->keycodes[i];
|
||||||
|
if (keycode != 0) {
|
||||||
|
char key = scancode_map[keycode];
|
||||||
|
if (key) {
|
||||||
|
keyboard_buffer_add(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle HID mouse report
|
||||||
|
void hid_process_mouse_report(const mouse_hid_report_t* report) {
|
||||||
|
// Process mouse movement and button clicks
|
||||||
|
mouse_data.x += report->x;
|
||||||
|
mouse_data.y += report->y;
|
||||||
|
mouse_data.left_button = (report->buttons & 0x01) != 0;
|
||||||
|
mouse_data.right_button = (report->buttons & 0x02) != 0;
|
||||||
|
|
||||||
|
print_hex((uint32_t)mouse_data.x, 1, 1);
|
||||||
|
print_hex((uint32_t)mouse_data.y, 1, 1);
|
||||||
|
print_hex((uint32_t)report->buttons, 1, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the HID descriptor (for parsing USB HID device descriptors)
|
||||||
|
bool hid_parse_descriptor(uint8_t* descriptor, uint32_t length) {
|
||||||
|
// HID descriptors are defined in the USB HID specification, we'll need to parse them here.
|
||||||
|
// For now, just return true assuming we have a valid descriptor.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
46
kernel/hid.h
Normal file
46
kernel/hid.h
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#ifndef HID_H
|
||||||
|
#define HID_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
|
// HID Report types
|
||||||
|
#define HID_REPORT_INPUT 0x01
|
||||||
|
#define HID_REPORT_OUTPUT 0x02
|
||||||
|
#define HID_REPORT_FEATURE 0x03
|
||||||
|
|
||||||
|
// HID usage page constants (USB HID)
|
||||||
|
#define HID_USAGE_PAGE_GENERIC 0x01
|
||||||
|
#define HID_USAGE_KEYBOARD 0x06
|
||||||
|
#define HID_USAGE_MOUSE 0x02
|
||||||
|
|
||||||
|
// HID keyboard and mouse data
|
||||||
|
typedef struct {
|
||||||
|
uint8_t modifier; // Modifier keys (shift, ctrl, alt, etc.)
|
||||||
|
uint8_t reserved; // Reserved byte
|
||||||
|
uint8_t keycodes[6]; // Keycodes for keys pressed
|
||||||
|
} keyboard_hid_report_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t buttons; // Mouse buttons (bitwise: 0x01 = left, 0x02 = right, 0x04 = middle)
|
||||||
|
int8_t x; // X axis movement
|
||||||
|
int8_t y; // Y axis movement
|
||||||
|
int8_t wheel; // Mouse wheel
|
||||||
|
} mouse_hid_report_t;
|
||||||
|
|
||||||
|
// Initialize the HID subsystem
|
||||||
|
void hid_init(void);
|
||||||
|
|
||||||
|
// Process an incoming HID report
|
||||||
|
void hid_process_report(uint8_t* report, uint8_t length);
|
||||||
|
|
||||||
|
// Process HID keyboard report
|
||||||
|
void hid_process_keyboard_report(const keyboard_hid_report_t* report);
|
||||||
|
|
||||||
|
// Process HID mouse report
|
||||||
|
void hid_process_mouse_report(const mouse_hid_report_t* report);
|
||||||
|
|
||||||
|
// USB HID report descriptor parsing
|
||||||
|
bool hid_parse_descriptor(uint8_t* descriptor, uint32_t length);
|
||||||
|
|
||||||
|
#endif // HID_H
|
||||||
@@ -2,64 +2,91 @@
|
|||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "isr.h"
|
#include "isr.h"
|
||||||
#include "terminal.h"
|
#include "terminal.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
#define KEYBOARD_DATA_PORT 0x60
|
#define KEYBOARD_DATA_PORT 0x60
|
||||||
#define KEY_BUFFER_SIZE 256
|
#define KEY_BUFFER_SIZE 256
|
||||||
|
|
||||||
static char key_buffer[KEY_BUFFER_SIZE];
|
// Use volatile so the compiler knows these change inside interrupts
|
||||||
static uint8_t buffer_head = 0; // Write position (interrupt)
|
static volatile char key_buffer[KEY_BUFFER_SIZE];
|
||||||
static uint8_t buffer_tail = 0; // Read position (get_char)
|
static volatile uint8_t buffer_head = 0;
|
||||||
static uint8_t buffer_count = 0;
|
static volatile uint8_t buffer_tail = 0;
|
||||||
static uint8_t buffer_index = 0;
|
static volatile uint8_t buffer_count = 0;
|
||||||
|
|
||||||
// Basic US QWERTY keymap (scancode to ASCII)
|
// Exported map: Removed 'static' so hid.c can reference it if needed
|
||||||
static const char scancode_map[128] = {
|
const char scancode_map[128] = {
|
||||||
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', // 0x00 - 0x09
|
0, 27, '1', '2', '3', '4', '5', '6', '7', '8',
|
||||||
'9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', // 0x0A - 0x13
|
'9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r',
|
||||||
't', 'y', 'z', 'u', 'i', 'o', 'p', '[', ']', '\n', // 0x14 - 0x1D
|
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0,
|
||||||
0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', // 0x1E - 0x27
|
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
|
||||||
';', '\'', '`', 0, '\\', 'x', 'c', 'v', 'b', // 0x28 - 0x31
|
'\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n',
|
||||||
'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0, // 0x32 - 0x3B
|
'm', ',', '.', '/', 0, '*', 0, ' ', 0
|
||||||
// rest can be filled as needed
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Interrupt handler for IRQ1
|
/**
|
||||||
void keyboard_callback(void) {
|
* Shared function used by both PS/2 (callback) and USB (hid.c)
|
||||||
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
|
* This fixes the "undefined reference to keyboard_buffer_add" error.
|
||||||
|
*/
|
||||||
if (scancode & 0x80) return; // Ignore key release
|
void keyboard_buffer_add(char c) {
|
||||||
|
|
||||||
char c = scancode_map[scancode];
|
|
||||||
if (!c) return;
|
if (!c) return;
|
||||||
|
|
||||||
uint8_t next_head = (buffer_head + 1) % KEY_BUFFER_SIZE;
|
uint8_t next_head = (buffer_head + 1) % KEY_BUFFER_SIZE;
|
||||||
|
|
||||||
// Drop key if buffer full
|
// If buffer is full, we must drop the key
|
||||||
if (next_head == buffer_tail) return;
|
if (next_head == buffer_tail) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
key_buffer[buffer_head] = c;
|
key_buffer[buffer_head] = c;
|
||||||
buffer_head = next_head;
|
buffer_head = next_head;
|
||||||
buffer_count++;
|
buffer_count++;
|
||||||
|
|
||||||
|
// Echo to terminal
|
||||||
terminal_putchar(c);
|
terminal_putchar(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
void keyboard_init() {
|
/**
|
||||||
register_interrupt_handler(33, keyboard_callback); // IRQ1 = int 33 (0x21)
|
* Hardware Interrupt Handler for PS/2
|
||||||
|
*/
|
||||||
|
void keyboard_callback(void) {
|
||||||
|
uint8_t scancode = inb(KEYBOARD_DATA_PORT);
|
||||||
|
|
||||||
|
// Ignore break codes (key release)
|
||||||
|
if (scancode & 0x80) return;
|
||||||
|
|
||||||
|
char c = scancode_map[scancode];
|
||||||
|
keyboard_buffer_add(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Blocking read (returns one char)
|
void keyboard_init(void) {
|
||||||
|
buffer_head = 0;
|
||||||
|
buffer_tail = 0;
|
||||||
|
buffer_count = 0;
|
||||||
|
// IRQ1 is usually mapped to IDT entry 33
|
||||||
|
register_interrupt_handler(33, keyboard_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Blocking read with a safe HLT to prevent CPU 100% usage
|
||||||
|
*/
|
||||||
char keyboard_get_char(void) {
|
char keyboard_get_char(void) {
|
||||||
while (buffer_count == 0) {
|
|
||||||
__asm__ __volatile__("hlt"); // Better than busy loop
|
|
||||||
}
|
|
||||||
|
|
||||||
char c;
|
char c;
|
||||||
__asm__ __volatile__("cli");
|
|
||||||
c = key_buffer[buffer_tail];
|
|
||||||
buffer_tail = (buffer_tail + 1) % KEY_BUFFER_SIZE;
|
|
||||||
buffer_count--;
|
|
||||||
__asm__ __volatile__("sti");
|
|
||||||
|
|
||||||
return c;
|
while (1) {
|
||||||
|
__asm__ __volatile__("cli"); // Disable interrupts to check buffer_count safely
|
||||||
|
|
||||||
|
if (buffer_count > 0) {
|
||||||
|
c = key_buffer[buffer_tail];
|
||||||
|
buffer_tail = (buffer_tail + 1) % KEY_BUFFER_SIZE;
|
||||||
|
buffer_count--;
|
||||||
|
__asm__ __volatile__("sti"); // Re-enable interrupts after reading
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* * IMPORTANT: 'sti' followed by 'hlt' is guaranteed by x86
|
||||||
|
* to execute 'hlt' BEFORE the next interrupt can trigger.
|
||||||
|
* This prevents the race condition hang.
|
||||||
|
*/
|
||||||
|
__asm__ __volatile__("sti; hlt");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
#ifndef KEYBOARD_H
|
#ifndef KEYBOARD_H
|
||||||
#define KEYBOARD_H
|
#define KEYBOARD_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
void keyboard_init(void);
|
void keyboard_init(void);
|
||||||
char keyboard_get_char(void); // Blocking read from buffer
|
void keyboard_buffer_add(char c);
|
||||||
|
char keyboard_get_char(void);
|
||||||
|
|
||||||
|
extern const char scancode_map[128];
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
// Mouse buffer
|
// Mouse buffer
|
||||||
static mouse_data_t mouse_data;
|
mouse_data_t mouse_data;
|
||||||
|
|
||||||
// Read USB mouse data
|
// Read USB mouse data
|
||||||
mouse_data_t usb_read_mouse(void) {
|
mouse_data_t usb_read_mouse(void) {
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ typedef struct {
|
|||||||
bool right_button;
|
bool right_button;
|
||||||
} mouse_data_t;
|
} mouse_data_t;
|
||||||
|
|
||||||
|
extern mouse_data_t mouse_data;
|
||||||
|
|
||||||
// Function declarations for USB 1.x HID mouse support
|
// Function declarations for USB 1.x HID mouse support
|
||||||
bool usb_mouse_init(void);
|
bool usb_mouse_init(void);
|
||||||
bool usb_mouse_detected(void);
|
bool usb_mouse_detected(void);
|
||||||
|
|||||||
21
kernel/pmm.h
Normal file
21
kernel/pmm.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef PMM_H
|
||||||
|
#define PMM_H
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include "memmap.h"
|
||||||
|
#include "paging.h" // For PAGE_SIZE
|
||||||
|
|
||||||
|
#define BLOCKS_PER_BYTE 8
|
||||||
|
|
||||||
|
void pmm_init(memory_map_entry_t* mmap, uint32_t mmap_size, uintptr_t bitmap_addr);
|
||||||
|
void pmm_mark_used(uintptr_t addr);
|
||||||
|
void pmm_mark_free(uintptr_t addr);
|
||||||
|
|
||||||
|
void* pmm_alloc_block();
|
||||||
|
void pmm_free_block(void* addr);
|
||||||
|
|
||||||
|
uint32_t pmm_get_used_block_count();
|
||||||
|
uint32_t pmm_get_free_block_count();
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
#include "vga.h"
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "string_utils.h"
|
#include "string_utils.h"
|
||||||
|
#include "vga.h"
|
||||||
|
|
||||||
void outb(uint16_t port, uint8_t value) {
|
void outb(uint16_t port, uint8_t value) {
|
||||||
__asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port));
|
__asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port));
|
||||||
@@ -134,7 +134,7 @@ void vga_printf(const char* format, ...) {
|
|||||||
va_end(args);
|
va_end(args);
|
||||||
|
|
||||||
// Now you can use the buffer with vga_write_string
|
// Now you can use the buffer with vga_write_string
|
||||||
vga_write_string(buffer, my_strlen(buffer)); // Use my_strlen instead of strlen
|
vga_write_string(buffer, strlen(buffer)); // Use my_strlen instead of strlen
|
||||||
}
|
}
|
||||||
|
|
||||||
void vga_init(void) {
|
void vga_init(void) {
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ typedef enum {
|
|||||||
// Function prototypes
|
// Function prototypes
|
||||||
uint8_t vga_entry_color(vga_color fg, vga_color bg);
|
uint8_t vga_entry_color(vga_color fg, vga_color bg);
|
||||||
uint16_t vga_entry(unsigned char uc, uint8_t color);
|
uint16_t vga_entry(unsigned char uc, uint8_t color);
|
||||||
|
void vga_init(void);
|
||||||
|
|
||||||
void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y);
|
void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y);
|
||||||
void vga_clear(uint8_t color);
|
void vga_clear(uint8_t color);
|
||||||
@@ -50,4 +51,4 @@ void vga_set_cursor_blink_rate(uint8_t rate);
|
|||||||
|
|
||||||
void vga_printf(const char* format, ...);
|
void vga_printf(const char* format, ...);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
102
pmm.c
Normal file
102
pmm.c
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
#include "pmm.h"
|
||||||
|
#include "memory.h" // For memset
|
||||||
|
|
||||||
|
static uint32_t* pmm_bitmap = NULL;
|
||||||
|
static uint32_t max_blocks = 0;
|
||||||
|
static uint32_t used_blocks = 0;
|
||||||
|
|
||||||
|
// Internal bitmap helpers
|
||||||
|
static inline void bitmap_set(uint32_t bit) {
|
||||||
|
pmm_bitmap[bit / 32] |= (1 << (bit % 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_unset(uint32_t bit) {
|
||||||
|
pmm_bitmap[bit / 32] &= ~(1 << (bit % 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_test(uint32_t bit) {
|
||||||
|
return pmm_bitmap[bit / 32] & (1 << (bit % 32));
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm_init(memory_map_entry_t* mmap, uint32_t mmap_size, uintptr_t bitmap_addr) {
|
||||||
|
// 1. Calculate total memory from mmap to find max_blocks
|
||||||
|
uint64_t total_mem = 0;
|
||||||
|
for (uint32_t i = 0; i < mmap_size; i++) {
|
||||||
|
if (mmap[i].type == 1) { // Available RAM
|
||||||
|
total_mem = mmap[i].base_addr + mmap[i].length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
max_blocks = (uint32_t)(total_mem / PAGE_SIZE);
|
||||||
|
used_blocks = max_blocks;
|
||||||
|
pmm_bitmap = (uint32_t*)bitmap_addr;
|
||||||
|
|
||||||
|
// 2. Default all memory to "Reserved" (1s)
|
||||||
|
memset(pmm_bitmap, 0xFF, max_blocks / BLOCKS_PER_BYTE);
|
||||||
|
|
||||||
|
// 3. Mark only the regions reported as Type 1 (Available) as free (0s)
|
||||||
|
for (uint32_t i = 0; i < mmap_size; i++) {
|
||||||
|
if (mmap[i].type == 1) {
|
||||||
|
uint32_t start_block = (uint32_t)(mmap[i].base_addr / PAGE_SIZE);
|
||||||
|
uint32_t block_count = (uint32_t)(mmap[i].length / PAGE_SIZE);
|
||||||
|
|
||||||
|
for (uint32_t j = 0; j < block_count; j++) {
|
||||||
|
bitmap_unset(start_block + j);
|
||||||
|
used_blocks--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Critical: Re-protect the first 1MB (BIOS/VGA/Real Mode stuff)
|
||||||
|
for (uint32_t i = 0; i < (1024 * 1024) / PAGE_SIZE; i++) {
|
||||||
|
pmm_mark_used(i * PAGE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Critical: Re-protect the Kernel + Page Tables
|
||||||
|
// Since your paging tables are at 0x200000 and linker at 1MB,
|
||||||
|
// mark everything from 0x100000 to roughly 0x400000 as used for safety.
|
||||||
|
for (uint32_t i = 0x100000 / PAGE_SIZE; i < 0x400000 / PAGE_SIZE; i++) {
|
||||||
|
pmm_mark_used(i * PAGE_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. Protect the bitmap itself
|
||||||
|
uint32_t bitmap_size_blocks = (max_blocks / BLOCKS_PER_BYTE) / PAGE_SIZE + 1;
|
||||||
|
for(uint32_t i = 0; i < bitmap_size_blocks; i++) {
|
||||||
|
pmm_mark_used(bitmap_addr + (i * PAGE_SIZE));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm_mark_used(uintptr_t addr) {
|
||||||
|
uint32_t block = addr / PAGE_SIZE;
|
||||||
|
if (!bitmap_test(block)) {
|
||||||
|
bitmap_set(block);
|
||||||
|
used_blocks++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm_mark_free(uintptr_t addr) {
|
||||||
|
uint32_t block = addr / PAGE_SIZE;
|
||||||
|
if (bitmap_test(block)) {
|
||||||
|
bitmap_unset(block);
|
||||||
|
used_blocks--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void* pmm_alloc_block() {
|
||||||
|
for (uint32_t i = 0; i < max_blocks / 32; i++) {
|
||||||
|
if (pmm_bitmap[i] != 0xFFFFFFFF) {
|
||||||
|
for (int j = 0; j < 32; j++) {
|
||||||
|
if (!bitmap_test(i * 32 + j)) {
|
||||||
|
uint32_t addr = (i * 32 + j) * PAGE_SIZE;
|
||||||
|
pmm_mark_used(addr);
|
||||||
|
return (void*)addr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL; // OOM
|
||||||
|
}
|
||||||
|
|
||||||
|
void pmm_free_block(void* addr) {
|
||||||
|
pmm_mark_free((uintptr_t)addr);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user