diff --git a/CMakeLists.txt b/CMakeLists.txt index e85b298..e8ee838 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,8 +1,8 @@ cmake_minimum_required(VERSION 3.15) project(new_kernel C ASM) #set(CMAKE_VERBOSE_MAKEFILE ON) -set(CMAKE_C_FLAGS "-g -ffreestanding -Wall -Wextra -fno-exceptions -fno-stack-protector -nostdinc -nostdlib -fno-pie -m32") -set(CMAKE_ASM_NASM_OBJECT_FORMAT "elf") +set(CMAKE_C_FLAGS "-I/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include -g -ffreestanding -Wall -Wextra -fno-exceptions -fno-stack-protector -nostdinc -nostdlib -fno-pie -m32") +#set(CMAKE_ASM_NASM_OBJECT_FORMAT "elf") set(CMAKE_ASM_FLAGS --32) #set(CMAKE_ASM-ATT_FLAGS --32) SET(CMAKE_EXE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/linker.ld -lgcc -ffreestanding -nostdlib") @@ -11,6 +11,7 @@ set(CMAKE_ASM_COMPILER "/usr/bin/as") #enable_language(ASM) include_directories(AFTER kernel boot) +include_directories("/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include") set(CMAKE_C_STANDARD 99) @@ -25,3 +26,4 @@ set_source_files_properties(${kernel_src} PROPERTIES LANGUAGE C COMPILE_FLAGS "" set_target_properties(my-kernel.bin PROPERTIES LINKER_LANGUAGE C PREFIX "" SUFFIX "" LINK_FLAGS "") + diff --git a/cmake/modules/FindGCC_INCLUDES.cmake b/cmake/modules/FindGCC_INCLUDES.cmake new file mode 100644 index 0000000..da40545 --- /dev/null +++ b/cmake/modules/FindGCC_INCLUDES.cmake @@ -0,0 +1,10 @@ +FIND_PATH(GCC_INCLUDES_INCLUDE_DIR cpuid.h + /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include + ) + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set APACHE_FOUND to TRUE if +# all listed variables are TRUE +find_package_handle_standard_args(GCC_INCLUDES DEFAULT_MSG GCC_INCLUDES_INCLUDE_DIR ) + +MARK_AS_ADVANCED(GCC_INCLUDES_INCLUDE_DIR) diff --git a/kernel/cpu/cpuidx.c b/kernel/cpu/cpuidx.c new file mode 100644 index 0000000..3a00b1f --- /dev/null +++ b/kernel/cpu/cpuidx.c @@ -0,0 +1,6 @@ +// +// Created by rick on 12-02-21. +// + +#include "cpuidx.h" + diff --git a/kernel/cpu/cpuidx.h b/kernel/cpu/cpuidx.h new file mode 100644 index 0000000..3a418da --- /dev/null +++ b/kernel/cpu/cpuidx.h @@ -0,0 +1,83 @@ +// +// Created by rick on 12-02-21. +// + +#ifndef NEW_KERNEL_CPUIDX_H +#define NEW_KERNEL_CPUIDX_H + +#include +#include + +enum { + CPUID_FEAT_ECX_SSE3 = 1 << 0, + CPUID_FEAT_ECX_PCLMUL = 1 << 1, + CPUID_FEAT_ECX_DTES64 = 1 << 2, + CPUID_FEAT_ECX_MONITOR = 1 << 3, + CPUID_FEAT_ECX_DS_CPL = 1 << 4, + CPUID_FEAT_ECX_VMX = 1 << 5, + CPUID_FEAT_ECX_SMX = 1 << 6, + CPUID_FEAT_ECX_EST = 1 << 7, + CPUID_FEAT_ECX_TM2 = 1 << 8, + CPUID_FEAT_ECX_SSSE3 = 1 << 9, + CPUID_FEAT_ECX_CID = 1 << 10, + CPUID_FEAT_ECX_FMA = 1 << 12, + CPUID_FEAT_ECX_CX16 = 1 << 13, + CPUID_FEAT_ECX_ETPRD = 1 << 14, + CPUID_FEAT_ECX_PDCM = 1 << 15, + CPUID_FEAT_ECX_PCIDE = 1 << 17, + CPUID_FEAT_ECX_DCA = 1 << 18, + CPUID_FEAT_ECX_SSE4_1 = 1 << 19, + CPUID_FEAT_ECX_SSE4_2 = 1 << 20, + CPUID_FEAT_ECX_x2APIC = 1 << 21, + CPUID_FEAT_ECX_MOVBE = 1 << 22, + CPUID_FEAT_ECX_POPCNT = 1 << 23, + CPUID_FEAT_ECX_AES = 1 << 25, + CPUID_FEAT_ECX_XSAVE = 1 << 26, + CPUID_FEAT_ECX_OSXSAVE = 1 << 27, + CPUID_FEAT_ECX_AVX = 1 << 28, + + CPUID_FEAT_EDX_FPU = 1 << 0, + CPUID_FEAT_EDX_VME = 1 << 1, + CPUID_FEAT_EDX_DE = 1 << 2, + CPUID_FEAT_EDX_PSE = 1 << 3, + CPUID_FEAT_EDX_TSC = 1 << 4, + CPUID_FEAT_EDX_MSR = 1 << 5, + CPUID_FEAT_EDX_PAE = 1 << 6, + CPUID_FEAT_EDX_MCE = 1 << 7, + CPUID_FEAT_EDX_CX8 = 1 << 8, + CPUID_FEAT_EDX_APIC = 1 << 9, + CPUID_FEAT_EDX_SEP = 1 << 11, + CPUID_FEAT_EDX_MTRR = 1 << 12, + CPUID_FEAT_EDX_PGE = 1 << 13, + CPUID_FEAT_EDX_MCA = 1 << 14, + CPUID_FEAT_EDX_CMOV = 1 << 15, + CPUID_FEAT_EDX_PAT = 1 << 16, + CPUID_FEAT_EDX_PSE36 = 1 << 17, + CPUID_FEAT_EDX_PSN = 1 << 18, + CPUID_FEAT_EDX_CLF = 1 << 19, + CPUID_FEAT_EDX_DTES = 1 << 21, + CPUID_FEAT_EDX_ACPI = 1 << 22, + CPUID_FEAT_EDX_MMX = 1 << 23, + CPUID_FEAT_EDX_FXSR = 1 << 24, + CPUID_FEAT_EDX_SSE = 1 << 25, + CPUID_FEAT_EDX_SSE2 = 1 << 26, + CPUID_FEAT_EDX_SS = 1 << 27, + CPUID_FEAT_EDX_HTT = 1 << 28, + CPUID_FEAT_EDX_TM1 = 1 << 29, + CPUID_FEAT_EDX_IA64 = 1 << 30, + CPUID_FEAT_EDX_PBE = 1 << 31 +}; + +static inline void cpuid(int code, uint32_t *a, uint32_t *d) { + asm volatile("cpuid":"=a"(*a), "=d"(*d):"a"(code):"ecx", "ebx"); +} + +/** issue a complete request, storing general registers output as a string + */ +static inline int cpuid_string(int code, uint32_t where[4]) { + asm volatile("cpuid":"=a"(*where), "=b"(*(where + 1)), + "=c"(*(where + 2)), "=d"(*(where + 3)):"a"(code)); + return (int) where[0]; +} + +#endif //NEW_KERNEL_CPUIDX_H diff --git a/kernel/cpu/idt.c b/kernel/cpu/idt.c index a558d1b..a560d74 100644 --- a/kernel/cpu/idt.c +++ b/kernel/cpu/idt.c @@ -7,7 +7,7 @@ idt_gate_t idt[IDT_REGISTERS]; idt_register_t idt_reg; -void set_idt_gate(int n, u32 handler) { +void set_idt_gate(int n, uint32_t handler) { idt[n].low_offset = handler & 0xffff; idt[n].sel = KERNEL_CS; idt[n].always0 = 0; @@ -16,7 +16,7 @@ void set_idt_gate(int n, u32 handler) { } void set_idt() { - idt_reg.base = (u32) &idt; + idt_reg.base = (uint32_t) &idt; idt_reg.limit = IDT_REGISTERS * sizeof(idt_gate_t) - 1; __asm__ __volatile__("lidtl (%0)" : : "r" (&idt_reg)); diff --git a/kernel/cpu/idt.h b/kernel/cpu/idt.h index af39a1f..669e6a0 100644 --- a/kernel/cpu/idt.h +++ b/kernel/cpu/idt.h @@ -9,26 +9,26 @@ /* How every interrupt gate (handler) is defined */ typedef struct { - u16 low_offset; /* Lower 16 bits of handler function address */ - u16 sel; /* Kernel segment selector */ - u8 always0; + uint16_t low_offset; /* Lower 16 bits of handler function address */ + uint16_t sel; /* Kernel segment selector */ + uint8_t always0; /* First byte * Bit 7: "Interrupt is present" * Bits 6-5: Privilege level of caller (0=kernel..3=user) * Bit 4: Set to 0 for interrupt gates * Bits 3-0: bits 1110 = decimal 14 = "32 bit interrupt gate" */ - u8 flags; - u16 high_offset; /* Higher 16 bits of handler function address */ + uint8_t flags; + uint16_t high_offset; /* Higher 16 bits of handler function address */ } __attribute__((packed)) idt_gate_t; typedef struct { - u16 limit; - u32 base; + uint16_t limit; + uint32_t base; } __attribute__((packed)) idt_register_t; #define IDT_REGISTERS 256 -void set_idt_gate(int n, u32 handler); +void set_idt_gate(int n, uint32_t handler); void set_idt(); diff --git a/kernel/cpu/isr.c b/kernel/cpu/isr.c index 3c46659..a5db213 100644 --- a/kernel/cpu/isr.c +++ b/kernel/cpu/isr.c @@ -14,38 +14,38 @@ isr_t interrupt_handlers[256]; void isr_install() { - set_idt_gate(0, (u32) isr0); - set_idt_gate(1, (u32) isr1); - set_idt_gate(2, (u32) isr2); - set_idt_gate(3, (u32) isr3); - set_idt_gate(4, (u32) isr4); - set_idt_gate(5, (u32) isr5); - set_idt_gate(6, (u32) isr6); - set_idt_gate(7, (u32) isr7); - set_idt_gate(8, (u32) isr8); - set_idt_gate(9, (u32) isr9); - set_idt_gate(10, (u32) isr10); - set_idt_gate(11, (u32) isr11); - set_idt_gate(12, (u32) isr12); - set_idt_gate(13, (u32) isr13); - set_idt_gate(14, (u32) isr14); - set_idt_gate(15, (u32) isr15); - set_idt_gate(16, (u32) isr16); - set_idt_gate(17, (u32) isr17); - set_idt_gate(18, (u32) isr18); - set_idt_gate(19, (u32) isr19); - set_idt_gate(20, (u32) isr20); - set_idt_gate(21, (u32) isr21); - set_idt_gate(22, (u32) isr22); - set_idt_gate(23, (u32) isr23); - set_idt_gate(24, (u32) isr24); - set_idt_gate(25, (u32) isr25); - set_idt_gate(26, (u32) isr26); - set_idt_gate(27, (u32) isr27); - set_idt_gate(28, (u32) isr28); - set_idt_gate(29, (u32) isr29); - set_idt_gate(30, (u32) isr30); - set_idt_gate(31, (u32) isr31); + set_idt_gate(0, (uint32_t) isr0); + set_idt_gate(1, (uint32_t) isr1); + set_idt_gate(2, (uint32_t) isr2); + set_idt_gate(3, (uint32_t) isr3); + set_idt_gate(4, (uint32_t) isr4); + set_idt_gate(5, (uint32_t) isr5); + set_idt_gate(6, (uint32_t) isr6); + set_idt_gate(7, (uint32_t) isr7); + set_idt_gate(8, (uint32_t) isr8); + set_idt_gate(9, (uint32_t) isr9); + set_idt_gate(10, (uint32_t) isr10); + set_idt_gate(11, (uint32_t) isr11); + set_idt_gate(12, (uint32_t) isr12); + set_idt_gate(13, (uint32_t) isr13); + set_idt_gate(14, (uint32_t) isr14); + set_idt_gate(15, (uint32_t) isr15); + set_idt_gate(16, (uint32_t) isr16); + set_idt_gate(17, (uint32_t) isr17); + set_idt_gate(18, (uint32_t) isr18); + set_idt_gate(19, (uint32_t) isr19); + set_idt_gate(20, (uint32_t) isr20); + set_idt_gate(21, (uint32_t) isr21); + set_idt_gate(22, (uint32_t) isr22); + set_idt_gate(23, (uint32_t) isr23); + set_idt_gate(24, (uint32_t) isr24); + set_idt_gate(25, (uint32_t) isr25); + set_idt_gate(26, (uint32_t) isr26); + set_idt_gate(27, (uint32_t) isr27); + set_idt_gate(28, (uint32_t) isr28); + set_idt_gate(29, (uint32_t) isr29); + set_idt_gate(30, (uint32_t) isr30); + set_idt_gate(31, (uint32_t) isr31); // Remap the PIC port_byte_out(0x20, 0x11); @@ -60,22 +60,22 @@ void isr_install() { port_byte_out(0xA1, 0x0); // Install the IRQs - set_idt_gate(32, (u32) irq0); - set_idt_gate(33, (u32) irq1); - set_idt_gate(34, (u32) irq2); - set_idt_gate(35, (u32) irq3); - set_idt_gate(36, (u32) irq4); - set_idt_gate(37, (u32) irq5); - set_idt_gate(38, (u32) irq6); - set_idt_gate(39, (u32) irq7); - set_idt_gate(40, (u32) irq8); - set_idt_gate(41, (u32) irq9); - set_idt_gate(42, (u32) irq10); - set_idt_gate(43, (u32) irq11); - set_idt_gate(44, (u32) irq12); - set_idt_gate(45, (u32) irq13); - set_idt_gate(46, (u32) irq14); - set_idt_gate(47, (u32) irq15); + set_idt_gate(32, (uint32_t) irq0); + set_idt_gate(33, (uint32_t) irq1); + set_idt_gate(34, (uint32_t) irq2); + set_idt_gate(35, (uint32_t) irq3); + set_idt_gate(36, (uint32_t) irq4); + set_idt_gate(37, (uint32_t) irq5); + set_idt_gate(38, (uint32_t) irq6); + set_idt_gate(39, (uint32_t) irq7); + set_idt_gate(40, (uint32_t) irq8); + set_idt_gate(41, (uint32_t) irq9); + set_idt_gate(42, (uint32_t) irq10); + set_idt_gate(43, (uint32_t) irq11); + set_idt_gate(44, (uint32_t) irq12); + set_idt_gate(45, (uint32_t) irq13); + set_idt_gate(46, (uint32_t) irq14); + set_idt_gate(47, (uint32_t) irq15); set_idt(); // Load with ASM } @@ -122,7 +122,7 @@ void isr_handler(registers_t r) { printf("Received interrupt: %d - %s\n", r.int_no, exception_messages[r.int_no]); } -void register_interrupt_handler(u8 n, isr_t handler) { +void register_interrupt_handler(uint8_t n, isr_t handler) { interrupt_handlers[n] = handler; } diff --git a/kernel/cpu/isr.h b/kernel/cpu/isr.h index 982534b..22d7837 100644 --- a/kernel/cpu/isr.h +++ b/kernel/cpu/isr.h @@ -123,15 +123,15 @@ extern void irq15(); #define IRQ15 47 typedef struct { - u32 ds; - u32 edi, esi, ebp, esp, ebx, edx, ecx, eax; - u32 int_no, err_code; - u32 eip, cs, eflags, useresp, ss; + uint32_t ds; + uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; + uint32_t int_no, err_code; + uint32_t eip, cs, eflags, useresp, ss; } registers_t; typedef void (*isr_t)(registers_t); -void register_interrupt_handler(u8 n, isr_t handler); +void register_interrupt_handler(uint8_t n, isr_t handler); void isr_install(); diff --git a/kernel/cpu/timer.c b/kernel/cpu/timer.c index 85ffcba..3608ab9 100644 --- a/kernel/cpu/timer.c +++ b/kernel/cpu/timer.c @@ -32,14 +32,14 @@ #define PIT_CHANNEL_2 (0b10 << 6) #define PIT_CHANNEL_READ_BACK (0b11 << 6) -u32 tick = 0; +uint32_t tick = 0; static void timer_callback(registers_t regs) { tick++; } -void sleep(u32 milliseconds) { - u32 done = tick + milliseconds; +void sleep(uint32_t milliseconds) { + uint32_t done = tick + milliseconds; while (tick != done) { k_wait_for_interrupt(); } @@ -53,12 +53,12 @@ void print_current_tick() { kprint("\n"); } -int init_timer(u32 freq) { +int init_timer(uint32_t freq) { register_interrupt_handler(IRQ0, timer_callback); - u32 divisor = 1193180 / freq; - u8 low = (u8) (divisor & 0xFF); - u8 high = (u8) ((divisor >> 8) & 0xFF); + uint32_t divisor = 1193180 / freq; + uint8_t low = (uint8_t) (divisor & 0xFF); + uint8_t high = (uint8_t) ((divisor >> 8) & 0xFF); port_byte_out(PORT_PIT_COMMAND, PIT_MODE_BIN | PIT_MODE_HARDWARE_SQUARE_WAVE_GENERATOR | PIT_ACCESS_MODE_LH | PIT_CHANNEL_0); port_byte_out(PORT_PIT_DATA_0, low); diff --git a/kernel/cpu/timer.h b/kernel/cpu/timer.h index fe629d5..076ffdc 100644 --- a/kernel/cpu/timer.h +++ b/kernel/cpu/timer.h @@ -7,10 +7,10 @@ #include -int init_timer(u32 freq); +int init_timer(uint32_t freq); void print_current_tick(); -void sleep(u32 milliseconds); +void sleep(uint32_t milliseconds); #endif //MY_KERNEL_TIMER_H diff --git a/kernel/drivers/ide.c b/kernel/drivers/ide.c index 2ef8456..5863655 100644 --- a/kernel/drivers/ide.c +++ b/kernel/drivers/ide.c @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include @@ -131,16 +131,16 @@ struct ide_device { } ide_devices[4]; typedef struct { - u8 device_number: 2; - u8 print_error: 1; + uint8_t device_number: 2; + uint8_t print_error: 1; } ide_block_device_info; -u8 ide_read(u8 channel, u8 reg); +uint8_t ide_read(uint8_t channel, uint8_t reg); -void ide_write(u8 channel, u8 reg, u8 data); +void ide_write(uint8_t channel, uint8_t reg, uint8_t data); -u8 ide_read(u8 channel, u8 reg) { - u8 result; +uint8_t ide_read(uint8_t channel, uint8_t reg) { + uint8_t result; if (reg & 0x10) { ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN); result = port_byte_in(channels[channel].base + (reg & 0xF)); @@ -163,7 +163,7 @@ u8 ide_read(u8 channel, u8 reg) { return result; } -void ide_write(u8 channel, u8 reg, u8 data) { +void ide_write(uint8_t channel, uint8_t reg, uint8_t data) { if (reg & 0x10) { ide_write(channel, ATA_REG_CONTROL, 0x80 | channels[channel].nIEN); port_byte_out(channels[channel].base + (reg & 0xF), data); @@ -221,7 +221,7 @@ void ide_read_buffer(unsigned char channel, unsigned char reg, unsigned int *buf // } } -unsigned char ide_polling(unsigned char channel, u8 advanced_check) { +unsigned char ide_polling(unsigned char channel, uint8_t advanced_check) { // (I) Delay 400 nanosecond for BSY to be set: // ------------------------------------------------- @@ -319,9 +319,9 @@ unsigned char ide_print_error(unsigned int drive, unsigned char err) { return err; } -u8 ide_pci_validate(const pci_device *device); +uint8_t ide_pci_validate(const pci_device *device); -u8 ide_pci_initialize(pci_device *device); +uint8_t ide_pci_initialize(pci_device *device); const pci_driver ide_pci_driver = { .name = "pci-ide", @@ -335,7 +335,7 @@ const pci_driver ide_pci_driver = { .initialize = ide_pci_initialize, }; -u8 ide_pci_validate(const pci_device *device) { +uint8_t ide_pci_validate(const pci_device *device) { if (device->class != PCI_CLASS_MASS_STORAGE || device->subclass != PCI_SUB_CLASS_IDE || (device->programInterface != 0x8A && device->programInterface != 0x80)) { @@ -345,7 +345,7 @@ u8 ide_pci_validate(const pci_device *device) { return PCI_VALIDATE_OK; } -void ide_fix_bar(bar_info *bar, u32 default_address, u32 size) { +void ide_fix_bar(bar_info *bar, uint32_t default_address, uint32_t size) { if (bar->address == 0x0) { // no need to actually write ti back bar->address = default_address; @@ -394,9 +394,9 @@ bool ide_pci_init_channels(pci_device *device) { return true; } -u8 ide_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 sectors, void *target) { +uint8_t ide_block_dev_access(const block_device *device, uint8_t direction, uint32_t lba, uint8_t sectors, void *target) { ide_block_device_info *info = device->device_info; - u8 result = ide_access(direction, info->device_number, lba, sectors, target); + uint8_t result = ide_access(direction, info->device_number, lba, sectors, target); if (result != 0) { if (info->print_error) { ide_print_error(info->device_number, result); @@ -431,7 +431,7 @@ void ide_register_block_devices() { } } -u8 ide_pci_initialize(pci_device *device) { +uint8_t ide_pci_initialize(pci_device *device) { if (!ide_pci_init_channels(device)) { return PCI_INIT_FAIL; @@ -541,15 +541,15 @@ void ide_register() { pci_register_driver(&ide_pci_driver); } -u8 ide_read_ata_access(u8 direction, u8 drive, u32 lba, u8 numsects, void *target) { - u8 lba_mode /* 0: CHS, 1:LBA28, 2: LBA48 */, dma /* 0: No DMA, 1: DMA */, cmd; - u8 lba_io[6]; - u8 channel = ide_devices[drive].channel; - u8 slavebit = ide_devices[drive].drive; - u32 bus = channels[channel].base; - u32 words = 256; - u16 cyl, i; - u8 head, sect, err; +uint8_t ide_read_ata_access(uint8_t direction, uint8_t drive, uint32_t lba, uint8_t numsects, void *target) { + uint8_t lba_mode /* 0: CHS, 1:LBA28, 2: LBA48 */, dma /* 0: No DMA, 1: DMA */, cmd; + uint8_t lba_io[6]; + uint8_t channel = ide_devices[drive].channel; + uint8_t slavebit = ide_devices[drive].drive; + uint32_t bus = channels[channel].base; + uint32_t words = 256; + uint16_t cyl, i; + uint8_t head, sect, err; ide_write(channel, ATA_REG_CONTROL, channels[channel].nIEN = (ide_irq_invoked = 0x0) + 0x02); @@ -667,7 +667,7 @@ u8 ide_read_ata_access(u8 direction, u8 drive, u32 lba, u8 numsects, void *targe return 0; } -u8 ide_access(u8 direction, u8 drive, u32 lba, u8 numsects, void *target) { +uint8_t ide_access(uint8_t direction, uint8_t drive, uint32_t lba, uint8_t numsects, void *target) { if (drive > 3 || ide_devices[drive].reserved == 0 || ide_devices[drive].type == IDE_ATAPI) { diff --git a/kernel/drivers/ide.h b/kernel/drivers/ide.h index b462f4b..26e7c01 100644 --- a/kernel/drivers/ide.h +++ b/kernel/drivers/ide.h @@ -9,7 +9,7 @@ void ide_register(); -u8 ide_access(u8 direction, u8 drive, u32 lba, u8 numsects, void *target); +uint8_t ide_access(uint8_t direction, uint8_t drive, uint32_t lba, uint8_t numsects, void *target); void ide_print_devices(); diff --git a/kernel/drivers/keyboard.c b/kernel/drivers/keyboard.c index 6d92d04..f018efa 100644 --- a/kernel/drivers/keyboard.c +++ b/kernel/drivers/keyboard.c @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -26,10 +25,10 @@ const char scancode_map_uppercase[] = { }; struct { - u8 shift: 1; - u8 ctrl: 1; - u8 alt: 1; - u8 extended: 1; + uint8_t shift: 1; + uint8_t ctrl: 1; + uint8_t alt: 1; + uint8_t extended: 1; } keyboard_state; void *keyboard_event_buffer = NULL; diff --git a/kernel/drivers/keyboard.h b/kernel/drivers/keyboard.h index dbf107d..355227d 100644 --- a/kernel/drivers/keyboard.h +++ b/kernel/drivers/keyboard.h @@ -9,12 +9,12 @@ typedef struct KeyEvent_t { // KeyCode key; - u32 scancode; + uint32_t scancode; char ascii_code; - u8 is_release: 1; - u8 shift: 1; - u8 alt: 1; - u8 ctrl: 1; + uint8_t is_release: 1; + uint8_t shift: 1; + uint8_t alt: 1; + uint8_t ctrl: 1; } KeyEvent; char getc(); diff --git a/kernel/drivers/pci.c b/kernel/drivers/pci.c index 9d4225d..87cd8e1 100644 --- a/kernel/drivers/pci.c +++ b/kernel/drivers/pci.c @@ -32,7 +32,7 @@ const pci_driver *pci_drivers[MAX_PCI_DRIVERS]; int last_pci_device_index = 0; pci_device pci_devices[MAX_PCI_DEVICES]; -u32 pci_register_driver(const pci_driver *pci_driver) { +uint32_t pci_register_driver(const pci_driver *pci_driver) { for (int i = 0; i < MAX_PCI_DRIVERS; ++i) { if (pci_drivers[i] != NULL) { continue; @@ -43,55 +43,55 @@ u32 pci_register_driver(const pci_driver *pci_driver) { return PCI_REGISTER_ERR_FULL; } -u32 pci_config_address(u8 bus, u8 slot, u8 func, u8 offset) { +uint32_t pci_config_address(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { return PCI_CONFIG_ENABLE - | ((u32) bus << PCI_CONFIG_SHIFT_BUS_NUMBER) - | ((u32) slot << PCI_CONFIG_SHIFT_DEV_NUMBER) - | ((u32) func << PCI_CONFIG_SHIFT_FUNC_NUMBER) + | ((uint32_t) bus << PCI_CONFIG_SHIFT_BUS_NUMBER) + | ((uint32_t) slot << PCI_CONFIG_SHIFT_DEV_NUMBER) + | ((uint32_t) func << PCI_CONFIG_SHIFT_FUNC_NUMBER) | (offset & 0xFC); } -u32 pci_config_read_double_word(u8 bus, u8 slot, u8 func, u8 offset) { - u32 address = pci_config_address(bus, slot, func, offset); +uint32_t pci_config_read_double_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { + uint32_t address = pci_config_address(bus, slot, func, offset); port_double_word_out(PORT_PCI_CONFIG_ADDRESS, address); return port_double_word_in(PORT_PCI_CONFIG_DATA); } -u16 pci_config_read_word(u8 bus, u8 slot, u8 func, u8 offset) { - u32 address = pci_config_address(bus, slot, func, offset); +uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { + uint32_t address = pci_config_address(bus, slot, func, offset); port_double_word_out(PORT_PCI_CONFIG_ADDRESS, address); return port_double_word_in(PORT_PCI_CONFIG_DATA) >> ((offset & 2) * 8) & 0xFFFF; } -u8 pci_config_read_byte(u8 bus, u8 slot, u8 func, u8 offset) { - u32 address = pci_config_address(bus, slot, func, offset); +uint8_t pci_config_read_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) { + uint32_t address = pci_config_address(bus, slot, func, offset); port_double_word_out(PORT_PCI_CONFIG_ADDRESS, address); return port_double_word_in(PORT_PCI_CONFIG_DATA) >> ((offset & 0b11) * 8) & 0xFF; } -void pci_config_write_double_word(u8 bus, u8 slot, u8 func, u8 offset, u32 value) { - u32 address = pci_config_address(bus, slot, func, offset); +void pci_config_write_double_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t value) { + uint32_t address = pci_config_address(bus, slot, func, offset); port_double_word_out(PORT_PCI_CONFIG_ADDRESS, address); port_double_word_out(PORT_PCI_CONFIG_DATA, value); } -void pci_config_write_word(u8 bus, u8 slot, u8 func, u8 offset, u16 value) { - u32 address = pci_config_address(bus, slot, func, offset); +void pci_config_write_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t value) { + uint32_t address = pci_config_address(bus, slot, func, offset); port_double_word_out(PORT_PCI_CONFIG_ADDRESS, address); port_word_out(PORT_PCI_CONFIG_DATA, value); } -void pci_config_write_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 value) { - u32 address = pci_config_address(bus, slot, func, offset); +void pci_config_write_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t value) { + uint32_t address = pci_config_address(bus, slot, func, offset); port_double_word_out(PORT_PCI_CONFIG_ADDRESS, address); port_byte_out(PORT_PCI_CONFIG_DATA, value); } -u16 pci_get_vendor_id(u8 bus, u8 slot, u8 func) { +uint16_t pci_get_vendor_id(uint8_t bus, uint8_t slot, uint8_t func) { return pci_config_read_word(bus, slot, func, PCI_CONFIG_VENDOR_ID); } -u8 pci_get_header_type(u8 bus, u8 slot, u8 func) { +uint8_t pci_get_header_type(uint8_t bus, uint8_t slot, uint8_t func) { return pci_config_read_byte(bus, slot, func, PCI_CONFIG_HEADER_TYPE); } @@ -113,7 +113,7 @@ void pci_pick_driver(pci_device *device) { } } -void pci_check_function(u8 bus, u8 slot, u8 func) { +void pci_check_function(uint8_t bus, uint8_t slot, uint8_t func) { pci_devices[last_pci_device_index].bus = bus; pci_devices[last_pci_device_index].slot = slot; pci_devices[last_pci_device_index].func = func; @@ -127,12 +127,12 @@ void pci_check_function(u8 bus, u8 slot, u8 func) { // todo do something with the function } -void pci_check_device(u8 bus, u8 device) { - u8 function = 0; - u16 vendor_id = pci_get_vendor_id(bus, device, function); +void pci_check_device(uint8_t bus, uint8_t device) { + uint8_t function = 0; + uint16_t vendor_id = pci_get_vendor_id(bus, device, function); if (vendor_id == 0xFFFF) return; pci_check_function(bus, device, function); - u8 header_type = pci_get_header_type(bus, device, function); + uint8_t header_type = pci_get_header_type(bus, device, function); if (header_type & PCI_HEADER_TYPE_MULTI_FUNC) { for (function = 1; function < 8; ++function) { if (pci_get_vendor_id(bus, device, function) != 0xFFFF) { @@ -142,7 +142,7 @@ void pci_check_device(u8 bus, u8 device) { } } -void pci_check_bus(u8 bus) { +void pci_check_bus(uint8_t bus) { for (int device = 0; device < 32; ++device) { pci_check_device(bus, device); } @@ -156,7 +156,7 @@ void pci_scan() { if (last_pci_device_index != 0) { k_panics("Can only scan once!"); } - u8 header_type = pci_get_header_type(0, 0, 0); + uint8_t header_type = pci_get_header_type(0, 0, 0); if (header_type & PCI_HEADER_TYPE_MULTI_FUNC) { // multiple pci host controllers for (int function = 0; function < 8; ++function) { @@ -193,12 +193,12 @@ void pci_print_info() { } } -void pci_init_bar(pci_device *device, u8 bar_index) { +void pci_init_bar(pci_device *device, uint8_t bar_index) { if (device->headerType != 0x00) { k_panics("Only header 0x00 supported for now"); return; } - u8 offset = 0; + uint8_t offset = 0; bar_info *bar; switch (bar_index) { case 0: @@ -231,9 +231,9 @@ void pci_init_bar(pci_device *device, u8 bar_index) { } - u32 original_address = pci_config_read_double_word(device->bus, device->slot, device->func, offset); + uint32_t original_address = pci_config_read_double_word(device->bus, device->slot, device->func, offset); pci_config_write_double_word(device->bus, device->slot, device->func, offset, 0xFFFFFFFF); - u32 masked_size = pci_config_read_double_word(device->bus, device->slot, device->func, offset); + uint32_t masked_size = pci_config_read_double_word(device->bus, device->slot, device->func, offset); if (original_address & 0x1) { // IO Space diff --git a/kernel/drivers/pci.h b/kernel/drivers/pci.h index e5b5051..d684aa3 100644 --- a/kernel/drivers/pci.h +++ b/kernel/drivers/pci.h @@ -60,60 +60,60 @@ typedef struct pci_driver pci_driver; typedef struct pci_device pci_device; -typedef u8 (*pci_driver_validate)(const pci_device *); +typedef uint8_t (*pci_driver_validate)(const pci_device *); -typedef u8 (*pci_driver_initialize)(pci_device *); +typedef uint8_t (*pci_driver_initialize)(pci_device *); typedef struct pci_driver { const char *name; const char *description; - u8 order; - u8 pci_class; - u8 pci_subclass; + uint8_t order; + uint8_t pci_class; + uint8_t pci_subclass; struct { - u8 pci_use_subclass: 1; + uint8_t pci_use_subclass: 1; }; pci_driver_validate validate; pci_driver_initialize initialize; } pci_driver; typedef struct { - u32 address; - u32 size; - u8 present: 1; - u8 is_io_space: 1; - u8 type: 2; - u8 prefetchable: 1; + uint32_t address; + uint32_t size; + uint8_t present: 1; + uint8_t is_io_space: 1; + uint8_t type: 2; + uint8_t prefetchable: 1; } bar_info; typedef struct pci_device { - u8 bus; - u8 slot; - u8 func; + uint8_t bus; + uint8_t slot; + uint8_t func; union { struct { - u16 vendorId; - u16 deviceId; + uint16_t vendorId; + uint16_t deviceId; }; - u32 config_line_0; + uint32_t config_line_0; }; union { struct { - u8 revisionId; - u8 programInterface; - u8 subclass; - u8 class; + uint8_t revisionId; + uint8_t programInterface; + uint8_t subclass; + uint8_t class; }; - u32 config_line_2; + uint32_t config_line_2; }; union { struct { - u8 cacheLineSize; - u8 latencyTimer; - u8 headerType; - u8 bist; + uint8_t cacheLineSize; + uint8_t latencyTimer; + uint8_t headerType; + uint8_t bist; }; - u32 config_line_3; + uint32_t config_line_3; }; bar_info bar0; bar_info bar1; @@ -123,16 +123,16 @@ typedef struct pci_device { bar_info bar5; const pci_driver *pci_driver; struct { - u8 present: 1; + uint8_t present: 1; } device_state; struct { - u8 initialized: 1; + uint8_t initialized: 1; } driver_state; } pci_device; void pci_print_info(); -u32 pci_register_driver(const pci_driver *pci_driver); +uint32_t pci_register_driver(const pci_driver *pci_driver); void pci_sort_drivers(); @@ -140,18 +140,18 @@ void pci_init_drivers(); void pci_scan(); -u32 pci_config_read_double_word(u8 bus, u8 slot, u8 func, u8 offset); +uint32_t pci_config_read_double_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); -u16 pci_config_read_word(u8 bus, u8 slot, u8 func, u8 offset); +uint16_t pci_config_read_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); -u8 pci_config_read_byte(u8 bus, u8 slot, u8 func, u8 offset); +uint8_t pci_config_read_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset); -void pci_config_write_double_word(u8 bus, u8 slot, u8 func, u8 offset, u32 value); +void pci_config_write_double_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t value); -void pci_config_write_word(u8 bus, u8 slot, u8 func, u8 offset, u16 value); +void pci_config_write_word(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t value); -void pci_config_write_byte(u8 bus, u8 slot, u8 func, u8 offset, u8 value); +void pci_config_write_byte(uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t value); -void pci_init_bar(pci_device *device, u8 bar_index); +void pci_init_bar(pci_device *device, uint8_t bar_index); #endif //NEW_KERNEL_PCI_H diff --git a/kernel/drivers/serial.c b/kernel/drivers/serial.c index 3ad0744..a57a68c 100644 --- a/kernel/drivers/serial.c +++ b/kernel/drivers/serial.c @@ -102,7 +102,7 @@ void write_serial(char a) { } void serial_kprint(const char *msg) { - u32 i = 0; + uint32_t i = 0; while (1) { char c = msg[i]; if (c == 0) { diff --git a/kernel/fs/blockdev.c b/kernel/fs/blockdev.c index 294c3e6..62dbf7a 100644 --- a/kernel/fs/blockdev.c +++ b/kernel/fs/blockdev.c @@ -16,21 +16,21 @@ block_device block_devices[MAX_BLOCK_DEVS]; int last_block_driver = 0; block_dev_driver block_dev_drivers[MAX_BLOCK_DRIVERS]; -u8 block_dev_register_driver(block_dev_driver *driver) { +uint8_t block_dev_register_driver(block_dev_driver *driver) { if (last_block_dev >= MAX_BLOCK_DEVS - 1) { return BLOCK_DEV_REGISTER_DRIVER_FULL; } - memcpy((u8 *) &block_dev_drivers[last_block_driver++], (const u8 *) driver, sizeof(block_dev_drivers)); + memcpy((uint8_t *) &block_dev_drivers[last_block_driver++], (const uint8_t *) driver, sizeof(block_dev_drivers)); return BLOCK_DEV_REGISTER_DRIVER_OK; } -u8 block_dev_register(block_device *device) { +uint8_t block_dev_register(block_device *device) { if (last_block_dev >= MAX_BLOCK_DEVS - 1) { return BLOCK_DEV_REGISTER_FULL; } - memcpy((u8 *) &block_devices[last_block_dev++], (const u8 *) device, sizeof(block_device)); + memcpy((uint8_t *) &block_devices[last_block_dev++], (const uint8_t *) device, sizeof(block_device)); return BLOCK_DEV_REGISTER_OK; } @@ -51,8 +51,8 @@ void block_dev_scan() { int c_last_block_dev = last_block_dev; for (int i = 0; i < c_last_block_dev; ++i) { if (block_devices[i].flags.scanned || !block_devices[i].flags.present) continue; - u8 *lba0 = malloc(block_devices[i].block_size); - u8 read_result = block_devices[i].access(&block_devices[i], BLOCK_DEV_DIRECTION_READ, 0, 1, lba0); + uint8_t *lba0 = malloc(block_devices[i].block_size); + uint8_t read_result = block_devices[i].access(&block_devices[i], BLOCK_DEV_DIRECTION_READ, 0, 1, lba0); if (read_result != BLOCK_DEV_ACCESS_OK) { block_devices[i].flags.unreadable = 1; goto _block_dev_scan_free; @@ -63,7 +63,7 @@ void block_dev_scan() { if (block_dev_drivers[j].flags.root_only && !block_devices[i].flags.root_device) continue; // let the driver test the disk - u8 driver_result = block_dev_drivers[j].check_device(&block_devices[i], lba0); + uint8_t driver_result = block_dev_drivers[j].check_device(&block_devices[i], lba0); if (driver_result == BLOCK_DEV_DRIVER_CHECK_OK) { block_devices[i].driver = &block_dev_drivers[j]; block_devices[i].flags.driver_installed = 1; diff --git a/kernel/fs/blockdev.h b/kernel/fs/blockdev.h index af6829a..85f2718 100644 --- a/kernel/fs/blockdev.h +++ b/kernel/fs/blockdev.h @@ -25,17 +25,17 @@ typedef struct block_device block_device; -typedef u8 (*block_device_driver_check_device)(const block_device *device, u8 *first_sector); +typedef uint8_t (*block_device_driver_check_device)(const block_device *device, uint8_t *first_sector); -typedef u8 (*block_device_driver_free)(const block_device *device); +typedef uint8_t (*block_device_driver_free)(const block_device *device); -typedef u8 (*block_device_access)(const block_device *device, u8 direction, u32 lba, u8 sectors, void *target); +typedef uint8_t (*block_device_access)(const block_device *device, uint8_t direction, uint32_t lba, uint8_t sectors, void *target); typedef struct { char name[16]; struct { - u8 root_only: 1; + uint8_t root_only: 1; } flags; block_device_driver_check_device check_device; block_device_driver_free free_device; @@ -43,24 +43,24 @@ typedef struct { typedef struct block_device { struct { - u8 present: 1; - u8 scanned: 1; - u8 unreadable: 1; - u8 root_device: 1; - u8 driver_installed: 1; + uint8_t present: 1; + uint8_t scanned: 1; + uint8_t unreadable: 1; + uint8_t root_device: 1; + uint8_t driver_installed: 1; } flags; char identifier[16]; - u32 num_lba; - u16 block_size; + uint32_t num_lba; + uint16_t block_size; block_device_access access; block_dev_driver *driver; void *device_info; // pointer to driver defined structure // todo device info } block_device; -u8 block_dev_register_driver(block_dev_driver *driver); +uint8_t block_dev_register_driver(block_dev_driver *driver); -u8 block_dev_register(block_device *device); +uint8_t block_dev_register(block_device *device); void block_dev_free(block_device *device); diff --git a/kernel/fs/fat.c b/kernel/fs/fat.c index 58f0db7..f630e93 100644 --- a/kernel/fs/fat.c +++ b/kernel/fs/fat.c @@ -17,90 +17,90 @@ typedef struct { struct { - u8 jump[3]; + uint8_t jump[3]; char oem_identifier[8]; - u16 bytes_per_sector; - u8 sectors_per_cluster; - u16 reserved_sectors; - u8 fats; - u16 directories; - u16 total_sectors; - u8 media_descriptor; - u16 sectors_per_fat; - u16 sectors_per_track; - u16 heads_sides; - u32 hidden_sectors; - u32 large_total_sectors; + uint16_t bytes_per_sector; + uint8_t sectors_per_cluster; + uint16_t reserved_sectors; + uint8_t fats; + uint16_t directories; + uint16_t total_sectors; + uint8_t media_descriptor; + uint16_t sectors_per_fat; + uint16_t sectors_per_track; + uint16_t heads_sides; + uint32_t hidden_sectors; + uint32_t large_total_sectors; } __attribute((packed)) bpb; union { struct { - u8 drive_number; - u8 flags; - u8 signature; - u32 serial; - u8 label[11]; - u8 system_id[8]; + uint8_t drive_number; + uint8_t flags; + uint8_t signature; + uint32_t serial; + uint8_t label[11]; + uint8_t system_id[8]; } __attribute((packed)) ebr_12_16; struct { - u32 sectors_per_fat; - u16 flags; - u16 fat_version; - u32 root_directory; - u16 fs_info; - u16 backup_boot; - u8 reserved[12]; - u8 drive_number; - u8 flags_nt; - u8 signature; - u32 serial; - u8 label[11]; - u8 system_id[8]; + uint32_t sectors_per_fat; + uint16_t flags; + uint16_t fat_version; + uint32_t root_directory; + uint16_t fs_info; + uint16_t backup_boot; + uint8_t reserved[12]; + uint8_t drive_number; + uint8_t flags_nt; + uint8_t signature; + uint32_t serial; + uint8_t label[11]; + uint8_t system_id[8]; } __attribute((packed)) ebr_32; }; } __attribute((packed)) fat_bpb; typedef struct { - u8 hours: 5; - u8 minutes: 6; - u8 seconds: 5; + uint8_t hours: 5; + uint8_t minutes: 6; + uint8_t seconds: 5; } __attribute((packed)) time_83; typedef struct { - u8 hours: 5; - u8 minutes: 6; - u8 seconds: 5; + uint8_t hours: 5; + uint8_t minutes: 6; + uint8_t seconds: 5; } __attribute((packed)) date_83; typedef struct { union { struct { - u8 name[11]; - u8 attributes; - u8 reserved; - u8 created_seconds; + uint8_t name[11]; + uint8_t attributes; + uint8_t reserved; + uint8_t created_seconds; time_83 created_time; date_83 created_date; date_83 access_date; - u16 high_first_cluster; + uint16_t high_first_cluster; time_83 last_mod_time; date_83 last_mod_date; - u16 low_first_cluster; - u32 file_size; + uint16_t low_first_cluster; + uint32_t file_size; } __attribute((packed)) name_83; struct { - u8 order; - u16 text_1[5]; - u8 attribute; - u8 long_entry_type; - u8 checksum; - u16 text_2[6]; - u16 reserved; - u16 text_3[2]; + uint8_t order; + uint16_t text_1[5]; + uint8_t attribute; + uint8_t long_entry_type; + uint8_t checksum; + uint16_t text_2[6]; + uint16_t reserved; + uint16_t text_3[2]; } __attribute((packed)) long_name; }; } __attribute((packed)) fat_directory_entry; -u8 fat_check_device(const block_device *device, u8 *first_sector); +uint8_t fat_check_device(const block_device *device, uint8_t *first_sector); block_dev_driver fat_driver = { .name = "fat", @@ -119,22 +119,22 @@ void print_chars(char *chars, int amount) { } } -u8 fat_check_device(const block_device *device, u8 *first_sector) { +uint8_t fat_check_device(const block_device *device, uint8_t *first_sector) { fat_bpb bpb; - memcpy((u8 *) &bpb, first_sector, sizeof(fat_bpb)); + memcpy((uint8_t *) &bpb, first_sector, sizeof(fat_bpb)); printf("OEM: "); print_chars(bpb.bpb.oem_identifier, 11); printf("\n"); - u32 total_sectors = bpb.bpb.total_sectors == 0 ? bpb.bpb.large_total_sectors : bpb.bpb.total_sectors; - u32 fat_size = bpb.bpb.sectors_per_fat == 0 ? bpb.ebr_32.sectors_per_fat : bpb.bpb.sectors_per_fat; - u32 root_dir_sectors = ((bpb.bpb.directories * 32) + (bpb.bpb.bytes_per_sector - 1)) / bpb.bpb.bytes_per_sector; - u32 first_data_sector = bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors; - u32 first_fat_sector = bpb.bpb.reserved_sectors; - u32 data_sectors = total_sectors - (bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors); - u32 total_clusters = data_sectors / bpb.bpb.sectors_per_cluster; + uint32_t total_sectors = bpb.bpb.total_sectors == 0 ? bpb.bpb.large_total_sectors : bpb.bpb.total_sectors; + uint32_t fat_size = bpb.bpb.sectors_per_fat == 0 ? bpb.ebr_32.sectors_per_fat : bpb.bpb.sectors_per_fat; + uint32_t root_dir_sectors = ((bpb.bpb.directories * 32) + (bpb.bpb.bytes_per_sector - 1)) / bpb.bpb.bytes_per_sector; + uint32_t first_data_sector = bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors; + uint32_t first_fat_sector = bpb.bpb.reserved_sectors; + uint32_t data_sectors = total_sectors - (bpb.bpb.reserved_sectors + (bpb.bpb.fats * fat_size) + root_dir_sectors); + uint32_t total_clusters = data_sectors / bpb.bpb.sectors_per_cluster; - u8 fat_type; + uint8_t fat_type; if (total_clusters < 4085) { fat_type = FAT_TYPE_12; } else if (total_clusters < 65525) { @@ -147,7 +147,7 @@ u8 fat_check_device(const block_device *device, u8 *first_sector) { // dump dir // fat12/16 only - u32 first_root_dir_sector = first_data_sector - root_dir_sectors; + uint32_t first_root_dir_sector = first_data_sector - root_dir_sectors; // todo fat32 fat_directory_entry *entries = malloc(root_dir_sectors * bpb.bpb.bytes_per_sector); device->access(device, BLOCK_DEV_DIRECTION_READ, first_root_dir_sector, root_dir_sectors, entries); @@ -158,13 +158,13 @@ u8 fat_check_device(const block_device *device, u8 *first_sector) { } typedef struct { - u32 sector; - u32 value; + uint32_t sector; + uint32_t value; } table_result; table_result -get_fat_table_value(u8 fat_type, const u8 *fat_table, u32 active_cluster, u32 first_fat_cluster, u32 sector_size) { - u32 fat_offset; +get_fat_table_value(uint8_t fat_type, const uint8_t *fat_table, uint32_t active_cluster, uint32_t first_fat_cluster, uint32_t sector_size) { + uint32_t fat_offset; table_result result = { .value = 0, .sector = 0, @@ -173,7 +173,7 @@ get_fat_table_value(u8 fat_type, const u8 *fat_table, u32 active_cluster, u32 fi case FAT_TYPE_12: fat_offset = active_cluster * (active_cluster / 2); result.sector = first_fat_cluster + (fat_offset / sector_size); - result.value = *(u16 *) &fat_table[fat_offset % sector_size]; + result.value = *(uint16_t *) &fat_table[fat_offset % sector_size]; if (active_cluster & 0x1) { result.value = result.value >> 4; } else { @@ -183,13 +183,13 @@ get_fat_table_value(u8 fat_type, const u8 *fat_table, u32 active_cluster, u32 fi case FAT_TYPE_16: fat_offset = active_cluster * 2; result.sector = first_fat_cluster + (fat_offset / sector_size); - result.value = *(u16 *) &fat_table[fat_offset % sector_size]; + result.value = *(uint16_t *) &fat_table[fat_offset % sector_size]; return result; case FAT_TYPE_32: case FAT_TYPE_EXFAT: fat_offset = active_cluster * 4; result.sector = first_fat_cluster + (fat_offset / sector_size); - result.value = *(u32 *) &fat_table[fat_offset % sector_size] & 0x0FFFFFFF; + result.value = *(uint32_t *) &fat_table[fat_offset % sector_size] & 0x0FFFFFFF; return result; default: break; diff --git a/kernel/fs/mbr.c b/kernel/fs/mbr.c index e82af3b..6d2a479 100644 --- a/kernel/fs/mbr.c +++ b/kernel/fs/mbr.c @@ -11,31 +11,31 @@ #include typedef struct { - u8 bootable; - u8 starting_head; - u8 starting_sector: 6; - u16 starting_cylinder: 10; - u8 system_id; - u8 ending_head; - u8 ending_sector: 6; - u16 ending_cylinder: 10; - u32 start_lba; - u32 num_lbas; + uint8_t bootable; + uint8_t starting_head; + uint8_t starting_sector: 6; + uint16_t starting_cylinder: 10; + uint8_t system_id; + uint8_t ending_head; + uint8_t ending_sector: 6; + uint16_t ending_cylinder: 10; + uint32_t start_lba; + uint32_t num_lbas; } __attribute((packed)) mbr_partition_table_entry; typedef struct { - u32 unique_id; - u16 reserved; + uint32_t unique_id; + uint16_t reserved; mbr_partition_table_entry entries[4]; - u8 signature[2]; + uint8_t signature[2]; } __attribute((packed)) mbr_table; typedef struct { const block_device *device; - u32 start_lba; + uint32_t start_lba; } mbr_block_driver_info; -u8 mbr_check_device(const block_device *device, u8 *first_sector); +uint8_t mbr_check_device(const block_device *device, uint8_t *first_sector); block_dev_driver mbr_driver = { .name = "mbr", @@ -48,12 +48,12 @@ void mbr_register_block_driver() { block_dev_register_driver(&mbr_driver); } -u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 sectors, void *target) { +uint8_t mbr_block_dev_access(const block_device *device, uint8_t direction, uint32_t lba, uint8_t sectors, void *target) { if (!device->flags.present || lba > device->num_lba) { return BLOCK_DEV_ACCESS_ERR; } const mbr_block_driver_info *info = device->device_info; - const u32 actual_lba = info->start_lba + lba; + const uint32_t actual_lba = info->start_lba + lba; if (actual_lba > info->device->num_lba) { return BLOCK_DEV_ACCESS_ERR; } @@ -62,9 +62,9 @@ u8 mbr_block_dev_access(const block_device *device, u8 direction, u32 lba, u8 se return info->device->access(info->device, direction, actual_lba, sectors, target); } -u8 mbr_check_device(const block_device *device, u8 *first_sector) { +uint8_t mbr_check_device(const block_device *device, uint8_t *first_sector) { mbr_table table; - memcpy((u8 *) &table, first_sector + (device->block_size - sizeof(mbr_table)), sizeof(mbr_table)); + memcpy((uint8_t *) &table, first_sector + (device->block_size - sizeof(mbr_table)), sizeof(mbr_table)); if (table.signature[0] != 0x55 && table.signature[1] != 0xAA) { // AA 55 but in little endian return BLOCK_DEV_DRIVER_CHECK_NO_MATCH; } @@ -104,7 +104,7 @@ u8 mbr_check_device(const block_device *device, u8 *first_sector) { return BLOCK_DEV_DRIVER_CHECK_OK; } -void mbr_read_from_ide(u8 ide_drive) { +void mbr_read_from_ide(uint8_t ide_drive) { char *mbr_data = malloc(0x200); ide_access(0, ide_drive, 0, 1, mbr_data); mbr_partition_table_entry *entry = (mbr_partition_table_entry *) (mbr_data + 0x1BE); diff --git a/kernel/fs/mbr.h b/kernel/fs/mbr.h index d06f006..aeeca4c 100644 --- a/kernel/fs/mbr.h +++ b/kernel/fs/mbr.h @@ -9,6 +9,6 @@ void mbr_register_block_driver(); -void mbr_read_from_ide(u8 ide_drive); +void mbr_read_from_ide(uint8_t ide_drive); #endif //NEW_KERNEL_MBR_H diff --git a/kernel/kernel.c b/kernel/kernel.c index b1bf65f..bc51d1f 100644 --- a/kernel/kernel.c +++ b/kernel/kernel.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -18,6 +17,7 @@ #include #include #include +#include #define BOOTLOADER_NAME_MAX_LENGTH 64 #define CMDLINE_MAX_LENGTH 256 diff --git a/kernel/libc/kprintf.c b/kernel/libc/kprintf.c index d2b39c2..133824a 100644 --- a/kernel/libc/kprintf.c +++ b/kernel/libc/kprintf.c @@ -5,7 +5,7 @@ #include "kprintf.h" #include "libc.h" #include -#include +#include #include #include #include @@ -13,32 +13,32 @@ /* * Integer to string */ -void print_int(u32 value, u32 width, char *buf, u32 *ptr, u8 base) { - // u32 in binary is 32 digits, so never longer than 33 digits +void print_int(uint32_t value, uint32_t width, char *buf, uint32_t *ptr, uint8_t base) { + // uint32_t in binary is 32 digits, so never longer than 33 digits char msg[33]; itoa(value, msg, base); - u32 digits = strlen(msg); + uint32_t digits = strlen(msg); if (digits < width) { - for (u32 i = 0; i < (width - digits); ++i) { + for (uint32_t i = 0; i < (width - digits); ++i) { buf[*ptr] = '0'; *ptr += 1; } } - for (u32 i = 0; i < digits; ++i) { + for (uint32_t i = 0; i < digits; ++i) { buf[*ptr] = msg[i]; *ptr += 1; } } -u32 vasprintf(char *buf, const char *fmt, va_list args) { - u32 ptr = 0; +uint32_t vasprintf(char *buf, const char *fmt, va_list args) { + uint32_t ptr = 0; for (int i = 0; i < strlen(fmt); ++i) { if (fmt[i] != '%') { buf[ptr++] = fmt[i]; continue; } ++i; - u32 arg_width = 0; + uint32_t arg_width = 0; while (fmt[i] >= '0' && fmt[i] <= '9') { arg_width *= 10; arg_width += fmt[i] - '0'; @@ -56,10 +56,10 @@ u32 vasprintf(char *buf, const char *fmt, va_list args) { buf[ptr++] = (char) va_arg(args, int); break; case 'x': - print_int((u32) va_arg(args, u32), arg_width, buf, &ptr, 16); + print_int((uint32_t) va_arg(args, uint32_t), arg_width, buf, &ptr, 16); break; case 'd': - print_int((u32) va_arg(args, u32), arg_width, buf, &ptr, 10); + print_int((uint32_t) va_arg(args, uint32_t), arg_width, buf, &ptr, 10); break; case '%': buf[ptr++] = '%'; diff --git a/kernel/libc/libc.c b/kernel/libc/libc.c index d002ba7..c97cb58 100644 --- a/kernel/libc/libc.c +++ b/kernel/libc/libc.c @@ -8,7 +8,7 @@ #include #include "libc.h" -int memcpy(u8 *dst, const u8 *src, int amount) { +int memcpy(uint8_t *dst, const uint8_t *src, int amount) { for (int i = 0; i < amount; i++) { dst[i] = src[i]; } diff --git a/kernel/libc/libc.h b/kernel/libc/libc.h index 9ecf6e9..a6b3a02 100644 --- a/kernel/libc/libc.h +++ b/kernel/libc/libc.h @@ -10,7 +10,7 @@ #include -int memcpy(u8 *dst, const u8 *src, int amount); +int memcpy(uint8_t *dst, const uint8_t *src, int amount); int memset(char *dst, char data, int amount); diff --git a/kernel/libc/ringqueue.h b/kernel/libc/ringqueue.h index 2264f47..70a9934 100644 --- a/kernel/libc/ringqueue.h +++ b/kernel/libc/ringqueue.h @@ -6,7 +6,7 @@ #define NEW_KERNEL_RINGQUEUE_H #include -#include +#include void *create_buffer(int count, int object_size); diff --git a/kernel/libc/stdbool.c b/kernel/libc/stdbool.c deleted file mode 100644 index 5642a82..0000000 --- a/kernel/libc/stdbool.c +++ /dev/null @@ -1,5 +0,0 @@ -// -// Created by rick on 31-01-21. -// - -#include "stdbool.h" diff --git a/kernel/libc/stdbool.h b/kernel/libc/stdbool.h deleted file mode 100644 index 9e71ef7..0000000 --- a/kernel/libc/stdbool.h +++ /dev/null @@ -1,15 +0,0 @@ -// -// Created by rick on 31-01-21. -// - -#ifndef NEW_KERNEL_STDBOOL_H -#define NEW_KERNEL_STDBOOL_H - -#define TRUE 1 -#define true 1 -#define FALSE 0 -#define false 0 - -typedef unsigned char bool; - -#endif //NEW_KERNEL_STDBOOL_H diff --git a/kernel/libc/va_list.h b/kernel/libc/va_list.h deleted file mode 100644 index cb2782d..0000000 --- a/kernel/libc/va_list.h +++ /dev/null @@ -1,14 +0,0 @@ -// -// Created by rick on 06-02-21. -// - -#ifndef NEW_KERNEL_VA_LIST_H -#define NEW_KERNEL_VA_LIST_H - -typedef __builtin_va_list va_list; -#define va_start(ap, last) __builtin_va_start(ap, last) -#define va_end(ap) __builtin_va_end(ap) -#define va_arg(ap, type) __builtin_va_arg(ap,type) -#define va_copy(dest, src) __builtin_va_copy(dest,src) - -#endif //NEW_KERNEL_VA_LIST_H diff --git a/kernel/mem/mem.c b/kernel/mem/mem.c index 5e127f2..03a7131 100644 --- a/kernel/mem/mem.c +++ b/kernel/mem/mem.c @@ -29,8 +29,8 @@ extern void *kernel_start; typedef struct { void *address; - u32 length; - u32 type; + uint32_t length; + uint32_t type; } __attribute((packed)) mmap_entry; int malloc_entries = 0; @@ -59,7 +59,7 @@ mmap_entry memmap[MEMMAP_ENTRIES] = { typedef struct malloc_map { struct malloc_map *next; struct malloc_map *pref; - u32 size; + uint32_t size; int type; } malloc_map; @@ -96,8 +96,8 @@ void use_mmap_entry(struct multiboot_mmap_entry *entry) { } } -void init_mmap_multiboot(struct multiboot_mmap_entry *entries, u32 count) { - for (u32 i = 0; i < count; ++i) { +void init_mmap_multiboot(struct multiboot_mmap_entry *entries, uint32_t count) { + for (uint32_t i = 0; i < count; ++i) { use_mmap_entry(&entries[i]); } } @@ -114,7 +114,7 @@ void split_malloc_map(malloc_map *entry, unsigned int size) { entry->size = size; } -void *malloc(unsigned int size) { +void *malloc(size_t size) { // todo replace this horrible mess! // this lacks any page alignment and what so ever for (int i = 0; i < MEMMAP_ENTRIES; ++i) { diff --git a/kernel/mem/mem.h b/kernel/mem/mem.h index d7be42f..21af4b8 100644 --- a/kernel/mem/mem.h +++ b/kernel/mem/mem.h @@ -7,13 +7,13 @@ #include -void init_mmap_multiboot(struct multiboot_mmap_entry *entries, u32 count); +void init_mmap_multiboot(struct multiboot_mmap_entry *entries, uint32_t count); void print_malloc_info(); void print_mmap_info(); -void *malloc(unsigned int size); +void *malloc(size_t size); void free(void *mem); diff --git a/kernel/types.h b/kernel/types.h index b09f878..1ee9b5b 100644 --- a/kernel/types.h +++ b/kernel/types.h @@ -8,15 +8,7 @@ #ifndef KERNEL_LIBC_TYPES_H_ #define KERNEL_LIBC_TYPES_H_ -# define NULL (void *)0 - -typedef unsigned long long u64; -typedef long long s64; -typedef unsigned int u32; -typedef int s32; -typedef unsigned short u16; -typedef short s16; -typedef unsigned char u8; -typedef char s8; +#include +#include #endif /* KERNEL_LIBC_TYPES_H_ */