AFUERAAAA

This commit is contained in:
2025-01-07 15:11:10 +01:00
parent 27569fd5d8
commit b3687d20ee
17 changed files with 2 additions and 670 deletions

View File

@@ -1,62 +0,0 @@
// ATA PIO driver implementation
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include <stdint.h>
#include "../kernel/io.h"
#include "../libc/stdio.h"
#include "ata.h"
static inline uint16_t inw(uint16_t port) {
uint16_t result;
asm volatile("inw %1, %0" : "=a"(result) : "dN"(port));
return result;
}
static inline void outw(uint16_t port, uint16_t data) {
asm volatile("outw %1, %0" : : "dN"(port), "a"(data));
}
void ata_wait_bsy() {
while (inb(ATA_PRIMARY_IO + ATA_REG_STATUS) & ATA_SR_BSY);
}
void ata_wait_drq() {
while (!(inb(ATA_PRIMARY_IO + ATA_REG_STATUS) & ATA_SR_DRQ));
}
void ata_select_drive(uint8_t drive) {
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | (drive << 4));
}
void ata_read_sector(uint32_t lba, uint8_t* buffer) {
ata_wait_bsy();
ata_select_drive(0);
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));
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
ata_wait_bsy();
ata_wait_drq();
for (int i = 0; i < 256; i++) {
((uint16_t*)buffer)[i] = inw(ATA_PRIMARY_IO + ATA_REG_DATA);
}
}
// Works only w/o paging
void test_read_sector() {
uint8_t buffer[512];
ata_read_sector(0, buffer);
for (int i = 0; i < 512; i++) {
if (i%25==0) puts("\n");
printf("%02x ", buffer[i]);
}
}

View File

@@ -1,35 +0,0 @@
// ATA PIO driver implementation header
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef ATA_H
#define ATA_H
#include <stdint.h>
#define ATA_PRIMARY_IO 0x1F0
#define ATA_PRIMARY_CTRL 0x3F6
#define ATA_CMD_READ_PIO 0x20
#define ATA_CMD_WRITE_PIO 0x30
#define ATA_IDENTIFY 0xEC
#define ATA_REG_DATA 0x00
#define ATA_REG_ERROR 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
#define ATA_SR_BSY 0x80
#define ATA_SR_DRDY 0x40
#define ATA_SR_DRQ 0x08
#define ATA_SR_ERR 0x01
void ata_read_sector(uint32_t lba, uint8_t* buffer);
void test_read_sector();
#endif

View File

@@ -1,158 +0,0 @@
// Keyboard driver
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include "../kernel/io.h"
#include "../libc/stdio.h"
#include "../kernel/system.h"
#include "kb.h"
unsigned char kbdus[128] =
{
0, 27, '1', '2', '3', '4', '5', '6', '7', '8', /* 9 */
'9', '0', '-', '=', '\b', /* Backspace */
'\t', /* Tab */
'q', 'w', 'e', 'r', /* 19 */
't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* Enter key */
0, /* 29 - Control */
'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 39 */
'\'', '`', 0, /* Left shift */
'\\', 'z', 'x', 'c', 'v', 'b', 'n', /* 49 */
'm', ',', '.', '/', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
unsigned char kbdus_shift[128] =
{
0, 27, '!', '@', '#', '$', '%', '^', '&', '*', /* 9 */
'(', ')', '_', '+', '\b', /* Backspace */
'\t', /* Tab */
'Q', 'W', 'E', 'R', /* 19 */
'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', '\n', /* Enter key */
0, /* 29 - Control */
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 39 */
'"', '~', 0, /* Left shift */
'|', 'Z', 'X', 'C', 'V', 'B', 'N', /* 49 */
'M', '<', '>', '?', 0, /* Right shift */
'*',
0, /* Alt */
' ', /* Space bar */
0, /* Caps lock */
0, /* 59 - F1 key ... > */
0, 0, 0, 0, 0, 0, 0, 0,
0, /* < ... F10 */
0, /* 69 - Num lock*/
0, /* Scroll Lock */
0, /* Home key */
0, /* Up Arrow */
0, /* Page Up */
'-',
0, /* Left Arrow */
0,
0, /* Right Arrow */
'+',
0, /* 79 - End key*/
0, /* Down Arrow */
0, /* Page Down */
0, /* Insert Key */
0, /* Delete Key */
0, 0, 0,
0, /* F11 Key */
0, /* F12 Key */
0, /* All other keys are undefined */
};
static char keyboard_buffer[KEYBOARD_BUFFER_SIZE];
static unsigned int keyboard_buffer_start = 0;
static unsigned int keyboard_buffer_end = 0;
static int shift_pressed = 0;
void keyboard_handler()
{
unsigned char scancode;
scancode = inb(0x60);
if (scancode & 0x80)
{
if (scancode == LEFT_SHIFT_RELEASED || scancode == RIGHT_SHIFT_RELEASED) {
shift_pressed = 0;
}
}
else
{
if (scancode == LEFT_SHIFT_PRESSED || scancode == RIGHT_SHIFT_PRESSED) {
shift_pressed = 1;
} else {
char c;
if (shift_pressed) {
c = kbdus_shift[scancode];
} else {
c = kbdus[scancode];
}
if (c)
{
keyboard_buffer[keyboard_buffer_end] = c;
keyboard_buffer_end = (keyboard_buffer_end+1) % KEYBOARD_BUFFER_SIZE;
}
}
}
}
void keyboard_install()
{
irq_install_handler(1, keyboard_handler);
printf("[keyboard] installed irq handler\n");
}
char keyboard_getchar()
{
while (keyboard_buffer_start == keyboard_buffer_end);
char c = keyboard_buffer[keyboard_buffer_start];
keyboard_buffer_start = (keyboard_buffer_start+1) % KEYBOARD_BUFFER_SIZE;
return c;
}
int keyboard_has_input()
{
return keyboard_buffer_start != keyboard_buffer_end;
}
char keyboard_getchar_non_blocking()
{
if (keyboard_has_input())
{
char c = keyboard_buffer[keyboard_buffer_start];
keyboard_buffer_start = (keyboard_buffer_start+1)%KEYBOARD_BUFFER_SIZE;
return c;
}
return 0;
}

View File

@@ -1,20 +0,0 @@
// Keyboard driver header
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef KB_H
#define KB_H
#define KEYBOARD_BUFFER_SIZE 256
#define LEFT_SHIFT_PRESSED 0x2A
#define RIGHT_SHIFT_PRESSED 0x36
#define LEFT_SHIFT_RELEASED 0xAA
#define RIGHT_SHIFT_RELEASED 0xB6
char keyboard_getchar();
int keyboard_has_input();
char keyboard_getchar_non_blocking();
#endif

View File

@@ -1,103 +0,0 @@
// PCI bus driver implementation
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include <stdint.h>
#include "pci.h"
#include "../libc/stdio.h"
static inline void outl(uint16_t port, uint32_t value)
{
__asm__ volatile ("outl %0, %1" : : "a"(value), "Nd"(port));
}
static inline uint32_t inl(uint16_t port)
{
uint32_t ret;
__asm__ volatile ("inl %1, %0" : "=a"(ret) : "Nd"(port));
return ret;
}
void pci_write_config_address(uint32_t address)
{
outl(PCI_CONFIG_ADDRESS, address);
}
uint32_t pci_read_config_data()
{
return inl(PCI_CONFIG_DATA);
}
uint32_t pci_config_address(uint8_t bus, uint8_t device, uint8_t function, uint8_t offset)
{
return (1 << 31) | (bus << 16) | (device << 11) | (function << 8) | (offset & 0xFC);
}
uint32_t pci_read(uint8_t bus, uint8_t device, uint8_t function, uint8_t offset)
{
uint32_t address = pci_config_address(bus, device, function, offset);
pci_write_config_address(address);
return pci_read_config_data();
}
pci_device_t pci_get_device(uint8_t bus, uint8_t device, uint8_t function)
{
pci_device_t dev;
uint32_t reg0 = pci_read(bus, device, function, 0x00); // Vendor ID, Device ID
uint32_t reg2 = pci_read(bus, device, function, 0x08); // Class, Subclass, Prog IF, Revision
dev.vendor_id = reg0 & 0xFFFF;
dev.device_id = (reg0 >> 16) & 0xFFFF;
dev.class_code = (reg2 >> 24) & 0xFF;
dev.subclass = (reg2 >> 16) & 0xFF;
dev.prog_if = (reg2 >> 8) & 0xFF;
dev.revision_id = reg2 & 0xFF;
dev.bus = bus;
dev.device = device;
dev.function = function;
return dev;
}
void scan_pci_bus()
{
for (uint16_t bus = 0; bus < 256; bus++) {
for (uint8_t device = 0; device < 32; device++) {
for (uint8_t function = 0; function < 8; function++) {
pci_device_t dev = pci_get_device(bus, device, function);
if (dev.vendor_id != 0xFFFF) {
// Maybe put that in a database in initrd.tar?
char* vendor_string;
switch(dev.vendor_id)
{
case 0x8086:
vendor_string = "Intel Corporation";
break;
case 0x1234:
vendor_string = "Brain Actuated Technologies";
break;
default:
vendor_string = "Unknown";
break;
}
char* device_string;
switch(dev.device_id)
{
default:
device_string = "Unknown";
break;
}
printf("PCI Device found: Bus %u, Device %u, Function %u, Vendor ID: 0x%x (%s), Device ID: 0x%x (%s), Class: 0x%x\n",
dev.bus, dev.device, dev.function, dev.vendor_id, vendor_string, dev.device_id, device_string, dev.class_code);
}
}
}
}
}

View File

@@ -1,29 +0,0 @@
// PCI bus driver implementation header
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef PCI_H
#define PCI_H
#include <stdint.h>
#define PCI_CONFIG_ADDRESS 0xCF8
#define PCI_CONFIG_DATA 0xCFC
typedef struct
{
uint16_t vendor_id;
uint16_t device_id;
uint8_t class_code;
uint8_t subclass;
uint8_t prog_if;
uint8_t revision_id;
uint8_t bus;
uint8_t device;
uint8_t function;
} pci_device_t;
void scan_pci_bus();
#endif

View File

@@ -1,64 +0,0 @@
// Real-time clock driver implementation for better PRNG
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include <stdint.h>
#include "rtc.h"
#include "../kernel/io.h"
#include "../libc/stdio.h"
uint8_t rtc_read_register(uint8_t reg)
{
outb(0x70, reg);
return inb(0x71);
}
uint8_t bcd_to_bin(uint8_t bcd)
{
return ((bcd/16)*10) + (bcd%16);
}
int rtc_is_updating()
{
outb(0x70, 0x0A);
return (inb(0x71) & 0x80);
}
void rtc_read_time(rtc_time_t *time)
{
while (rtc_is_updating());
time->seconds = rtc_read_register(0x00);
time->minutes = rtc_read_register(0x02);
time->hours = rtc_read_register(0x04);
time->day = rtc_read_register(0x06);
time->month = rtc_read_register(0x07);
time->year = rtc_read_register(0x08);
outb(0x70, 0x0B);
uint8_t registerB = inb(0x71);
if (!(registerB & 0x04))
{
time->seconds = bcd_to_bin(time->seconds);
time->minutes = bcd_to_bin(time->minutes);
time->hours = bcd_to_bin(time->hours);
time->day = bcd_to_bin(time->day);
time->month = bcd_to_bin(time->month);
time->year = bcd_to_bin(time->year);
}
}
void print_time(const rtc_time_t *time)
{
printf("%02d/%02d/%02d %02d:%02d:%02d\n", time->day, time->month, time->year, time->hours, time->minutes, time->seconds);
}
long time_seed()
{
rtc_time_t* time = {0};
rtc_read_time(time);
return time->day + time->month + time->year + time->hours + time->minutes + time->seconds;
}

View File

@@ -1,23 +0,0 @@
// Real-time clock driver implementation header for better PRNG
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef RTC_H
#define RTC_H
typedef struct
{
uint8_t seconds;
uint8_t minutes;
uint8_t hours;
uint8_t day;
uint8_t month;
uint8_t year;
} rtc_time_t;
void rtc_read_time(rtc_time_t *time);
long time_seed();
void print_time(const rtc_time_t *time);
#endif

View File

@@ -25,8 +25,6 @@ extern void irq13();
extern void irq14(); extern void irq14();
extern void irq15(); extern void irq15();
extern void syscall_common_stub();
void *irq_routines[16] = void *irq_routines[16] =
{ {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
@@ -78,8 +76,6 @@ void irq_install()
idt_set_gate(46, (unsigned)irq14, 0x08, 0x8E); idt_set_gate(46, (unsigned)irq14, 0x08, 0x8E);
idt_set_gate(47, (unsigned)irq15, 0x08, 0x8E); idt_set_gate(47, (unsigned)irq15, 0x08, 0x8E);
printf("[kernel] installed irq 0-15\n"); printf("[kernel] installed irq 0-15\n");
idt_set_gate(0x80, (unsigned long)syscall_common_stub, 0x08, 0x8E);
} }
void irq_handler(struct regs *r) void irq_handler(struct regs *r)

View File

@@ -8,14 +8,12 @@
#include "gdt.h" #include "gdt.h"
#include "idt.h" #include "idt.h"
#include "system.h" #include "system.h"
#include "../drivers/ata.h"
#include <stdint.h> #include <stdint.h>
#include "../drivers/framebuffer.h" #include "../drivers/framebuffer.h"
#include "kmain.h" #include "kmain.h"
#include "multiboot2.h" #include "multiboot2.h"
#include "kheap.h" #include "kheap.h"
#include "initrd.h" #include "initrd.h"
#include "../libc/crypto.h"
void kmain(multiboot2_info *mb_info) void kmain(multiboot2_info *mb_info)
{ {
@@ -112,19 +110,8 @@ void kmain(multiboot2_info *mb_info)
__asm__ __volatile__("sti"); __asm__ __volatile__("sti");
init_alloc(); init_alloc();
void* ptr1 = malloc(256);
void* ptr2 = malloc(512);
printf("[debug] malloc test ptr1=0x%x, ptr2=0x%x\n", (unsigned int)ptr1, (unsigned int)ptr2);
free(ptr1); free(ptr2);
void* ptr3 = calloc(1024, 2);
printf("[debug] calloc test ptr3=0x%x\n", (unsigned int)ptr3);
free (ptr3);
// usually the place where i do testing
timer_install(); timer_install();
keyboard_install();
printf("Nothing to do, halting..."); printf("Nothing to do, halting...");
asm("hlt"); asm("hlt");
} }

View File

@@ -210,37 +210,6 @@ irq_common_stub:
add esp, 8 add esp, 8
iret iret
; we'll be placing the syscall_common_stub here.
; push everything, then call syscall_handler (be sure to define it extern)
; then pop back everything and iret
extern syscall_handler
global syscall_common_stub
syscall_common_stub:
pusha
push ds
push es
push fs
push gs
mov eax, ds
push eax ; save ds
mov ax, 0x01 ; kernel segment YES I CHEATED I KNOW THIS SUCKS
mov ds, ax
mov es, ax
call syscall_handler
pop eax
mov ds, eax ; restore ds
pop gs
pop fs
pop es
pop ds
popa
iret
section .bss section .bss
align 4 align 4

View File

@@ -1,33 +0,0 @@
// System calls
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include "../libc/stdio.h"
void handle_syscall(int syscall_number)
{
switch(syscall_number)
{
case 1:
puts("Here's the syscall 1\n");
break;
default:
printf("[error] Invalid syscall number '%d'!\n", syscall_number);
break;
}
}
void syscall_handler()
{
int syscall_number;
void* arg;
// mov eax, syscall_number
// mov ebx, arg
asm volatile("mov %%eax, %0" : "=r"(syscall_number));
asm volatile("mov %%ebx, %0" : "=r"(arg));
printf("[syscall] syscall_number=%d, arg=%p\n", syscall_number, arg);
handle_syscall(syscall_number);
}

View File

@@ -1,16 +0,0 @@
// System information kernel module
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include "../libc/stdio.h"
#include "../libc/string.h"
void cpuid(int code, unsigned int* a, unsigned int* d)
{
asm volatile("cpuid"
: "=a"(*a), "=d"(*d)
: "a"(code)
: "ecx", "ebx");
}

View File

@@ -1,11 +0,0 @@
// System information kernel module header
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef SYSINFO_H
#define SYSINFO_H
void cpuid(int code, unsigned int* a, unsigned int* d);
#endif

View File

@@ -1,48 +0,0 @@
// Cryptography routines for blankos/libc
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#include "crypto.h"
#include <stdint.h>
int lcg(int seed)
{
int x = seed;
// Constants (ZX81 LCG)
int a = 75;
int c = 74;
long m = 65537;
for (int i=0; i<10; i++)
{
x = (a*x + c) % m;
}
return x;
}
int randint(int seed)
{
int x = lcg(seed);
return x;
}
static uint32_t next = 1;
uint32_t rand()
{
next = next * 1103515245 + 12345;
return (next/65536) % 32768;
}
float rand_float()
{
return rand() / 32767.0f;
}
void srand(uint32_t seed)
{
next = seed;
}

View File

@@ -1,19 +0,0 @@
// Cryptography routines for blankos/libc header
// Author: xamidev
// Licensed under the Unlicense. See the repo below.
// https://github.com/xamidev/blankos
#ifndef CRYPTO_H
#define CRYPTO_H
#define RAND_MAX 1024
#include <stdint.h>
int lcg(int seed);
int randint(int seed);
uint32_t rand();
float rand_float();
void srand(uint32_t seed);
#endif

View File

@@ -482,7 +482,7 @@ int* printf_number(int* argp, int length, bool sign, int radix, int width, char
return argp; return argp;
} }
/*
int getch() int getch()
{ {
return keyboard_getchar(); return keyboard_getchar();
@@ -511,6 +511,7 @@ void get_input(char *buffer, int size) {
} }
buffer[index] = '\0'; buffer[index] = '\0';
} }
*/
void colorprintf(uint32_t fg, uint32_t bg, const char* fmt, ...) void colorprintf(uint32_t fg, uint32_t bg, const char* fmt, ...)
{ {