oxytoxin

unix-like kernel
git clone git://git.evenfri.pw/oxytoxin.git
Log | Files | Refs | README | LICENSE

commit 91787c09bc573ea474243d7866cc566212e2a18e
parent 112af79a793ae23ec9e2b4ed1f72b0173ede4bf3
Author: evenfri <evenfri256@gmail.com>
Date:   Thu,  1 Oct 2026 22:38:39 +0800

upper case

Diffstat:
Mata/ata.c | 85+++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------
Mata/ata.h | 28++++++++++++++++------------
Mboot.asm | 16++++++++--------
Mdat.h | 66+++++++++++++++++++++++++++++++++---------------------------------
Mdisk.img | 0
Midt.c | 22++++++++++++----------
Minit.c | 20+++++---------------
Mio.asm | 2++
Mkeyboard/keyboard.c | 6+++---
Mkeyboard/keyboard.h | 8++++----
Moxytoxin.h | 6+++---
Mpit.c | 8++++----
Mtty.c | 34+++++++++++++++++-----------------
Mvga.c | 26+++++++++++++-------------
14 files changed, 182 insertions(+), 145 deletions(-)

diff --git a/ata/ata.c b/ata/ata.c @@ -3,50 +3,89 @@ extern void ata_entry(void); static void ata_io_wait(void) { - inb(0x1f7); - inb(0x1f7); - inb(0x1f7); - inb(0x1f7); + inb(0x1F7); + inb(0x1F7); + inb(0x1F7); + inb(0x1F7); } static int ata_wait_drq(void) { + uint8_t status; + while (1) { - uint8_t status = inb(0x1f7); - if (status & ata_drive_err) return -1; - if (!(status & ata_drive_busy) && (status & ata_drive_req_ready)) return 0; + status = inb(0x1F7); + if (status & ATA_DRIVE_ERR) return -1; + if (!(status & ATA_DRIVE_BUSY) && (status & ATA_DRIVE_REQ_READY)) return 0; } } void ata_read_sectors(void *trg, unsigned int lba, unsigned char size) { - outb(0x1f6, 0xe0 | ((lba >> 24) & 0x0f)); - outb(0x1f2, size); - outb(0x1f3, (unsigned char) lba); - outb(0x1f4, (unsigned char) (lba >> 8)); - outb(0x1f5, (unsigned char) (lba >> 16)); - outb(0x3f6, 0x02); - outb(0x1f7, ata_cmd_read); + uint16_t *target; + int count; + int i; + int j; + + outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F)); + outb(0x1F2, size); + outb(0x1F3, (unsigned char) lba); + outb(0x1F4, (unsigned char) (lba >> 8)); + outb(0x1F5, (unsigned char) (lba >> 16)); + outb(0x3F6, 0x02); + outb(0x1F7, ATA_CMD_READ); ata_io_wait(); - uint16_t *target = (uint16_t *) trg; - int count = (size == 0) ? 256 : size; + target = (uint16_t *) trg; + count = (size == 0) ? 256 : size; - for (int i = 0; i < count; i++) { + for (i = 0; i < count; i++) { if (ata_wait_drq() != 0) return; /* read error */ - for (int j = 0; j < 256; j++) { - target[j] = inw(0x1f0); + for (j = 0; j < 256; j++) { + target[j] = inw(0x1F0); } target += 256; } } +void ata_write_sectors(const void *src, unsigned int lba, unsigned char size) { + const uint16_t *source; + int count; + int i; + int j; + + outb(0x1F6, 0xE0 | ((lba >> 24) & 0x0F)); + outb(0x1F2, size); + outb(0x1F3, (unsigned char) lba); + outb(0x1F4, (unsigned char) (lba >> 8)); + outb(0x1F5, (unsigned char) (lba >> 16)); + outb(0x3F6, 0x02); + outb(0x1F7, ATA_CMD_WRITE); + + ata_io_wait(); + + source = (const uint16_t *) src; + count = (size == 0) ? 256 : size; + + for (i = 0; i < count; i++) { + if (ata_wait_drq() != 0) return; /* write error */ + + for (j = 0; j < 256; j++) { + outw(0x1F0, source[j]); + } + source += 256; + } + + outb(0x1F7, ATA_CMD_CACHE_FLUSH); + ata_io_wait(); +} + void ata_handler(void) { - inb(0x1f7); /* reset IRQ */ - outb(0x20, 0x20); /* EOI для master pic1 */ - outb(0xa0, 0x20); /* EOI для slave pic2 */ + inb(0x1F7); /* reset IRQ */ + outb(0x20, 0x20); /* eoi for master pic1 */ + outb(0xA0, 0x20); /* eoi for slave pic2 */ } void ata_init(void) { - idt_register(0x2e, ata_entry); + idt_register(0x2E, ata_entry); } diff --git a/ata/ata.h b/ata/ata.h @@ -1,18 +1,22 @@ -#ifndef ata_h -#define ata_h +#ifndef ATA_H +#define ATA_H -#define ata_base 1 -#define ata_master 0xa0 -#define ata_slave 0xb0 -#define ata_drive_busy 0x80 -#define ata_drive_ready 0x40 -#define ata_drive_req_ready 0x08 -#define ata_drive_write_fault 0x20 -#define ata_drive_err 0x01 -#define ata_cmd_read 0x20 +#define ATA_BASE 1 +#define ATA_MASTER 0XA0 +#define ATA_SLAVE 0XB0 +#define ATA_DRIVE_BUSY 0X80 +#define ATA_DRIVE_READY 0X40 +#define ATA_DRIVE_REQ_READY 0X08 +#define ATA_DRIVE_WRITE_FAULT 0x20 +#define ATA_DRIVE_ERR 0x01 + +#define ATA_CMD_READ 0x20 +#define ATA_CMD_WRITE 0x30 +#define ATA_CMD_CACHE_FLUSH 0XE7 void ata_init(void); void ata_read_sectors(void *trg, unsigned int lba, unsigned char size); +void ata_write_sectors(const void *src, unsigned int lba, unsigned char size); -#endif /* ata_h */ +#endif /* ATA_H */ diff --git a/boot.asm b/boot.asm @@ -1,16 +1,16 @@ bits 32 -mb_align equ 1 << 0 -mb_meminfo equ 1 << 1 -mb_flags equ mb_align | mb_meminfo -mb_magic equ 0x1badb002 ; multiboot magic -mb_checksum equ -(mb_magic + mb_flags) +MB_ALIGN equ 1 << 0 +MB_MEMINFO equ 1 << 1 +MB_FLAGS equ MB_ALIGN | MB_MEMINFO +MB_MAGIC equ 0x1BADB002 ; multiboot magic +MB_CHECKSUM equ -(MB_MAGIC + MB_FLAGS) section .multiboot align 4 - dd mb_magic - dd mb_flags - dd mb_checksum + dd MB_MAGIC + dd MB_FLAGS + dd MB_CHECKSUM section .bss align 16 diff --git a/dat.h b/dat.h @@ -4,29 +4,29 @@ #include "oxytoxin.h" /* vga */ -#define vga_width 80 -#define vga_height 25 -#define vga_start 0xb8000 -#define vga_ctrl 0x3d4 -#define vga_data 0x3d5 +#define VGA_WIDTH 80 +#define VGA_HEIGHT 25 +#define VGA_START 0xB8000 +#define VGA_CTRL 0x3D4 +#define VGA_DATA 0x3D5 enum vga_color { - vga_color_black = 0, - vga_color_blue = 1, - vga_color_green = 2, - vga_color_cyan = 3, - vga_color_red = 4, - vga_color_magenta = 5, - vga_color_brown = 6, - vga_color_light_grey = 7, - vga_color_dark_grey = 8, - vga_color_light_blue = 9, - vga_color_light_green = 10, - vga_color_light_cyan = 11, - vga_color_light_red = 12, - vga_color_light_magenta = 13, - vga_color_light_brown = 14, - vga_color_white = 15, + VGA_COLOR_BLACK = 0, + VGA_COLOR_BLUE = 1, + VGA_COLOR_GREEN = 2, + VGA_COLOR_CYAN = 3, + VGA_COLOR_RED = 4, + VGA_COLOR_MAGENTA = 5, + VGA_COLOR_BROWN = 6, + VGA_COLOR_LIGHT_GREY = 7, + VGA_COLOR_DARK_GREY = 8, + VGA_COLOR_LIGHT_BLUE = 9, + VGA_COLOR_LIGHT_GREEN = 10, + VGA_COLOR_LIGHT_CYAN = 11, + VGA_COLOR_LIGHT_RED = 12, + VGA_COLOR_LIGHT_MAGENTA = 13, + VGA_COLOR_LIGHT_BROWN = 14, + VGA_COLOR_WHITE = 15, }; /* idt */ @@ -45,21 +45,21 @@ typedef struct { typedef void (*int_handler)(); -#define icw_1 0x11 +#define ICW_1 0x11 -#define pic_1 0x20 -#define pic_2 0xa0 -#define pic_1_comm pic_1 -#define pic_1_data (pic_1+1) -#define pic_2_comm pic_2 -#define pic_2_data (pic_2+1) +#define PIC_1 0x20 +#define PIC_2 0xa0 +#define PIC_1_COMM PIC_1 +#define PIC_1_DATA (PIC_1+1) +#define PIC_2_COMM PIC_2 +#define PIC_2_DATA (PIC_2+1) -#define irq_0 0x20 -#define irq_8 0x28 +#define IRQ_0 0x20 +#define IRQ_8 0x28 /* pit */ -#define pit_chan0 0x40 -#define pit_comm 0x43 -#define pit_base_freq 1193182 +#define PIT_CHAN0 0x40 +#define PIT_COMM 0x43 +#define PIT_BASE_FREQ 1193182 #endif /* dat_h */ diff --git a/disk.img b/disk.img Binary files differ. diff --git a/idt.c b/idt.c @@ -11,17 +11,17 @@ void excp_handler() { void pic_remap() { asm volatile ("cli"); - outb(pic_1_comm, icw_1); io_wait(); - outb(pic_2_comm, icw_1); io_wait(); + outb(PIC_1_COMM, ICW_1); io_wait(); + outb(PIC_2_COMM, ICW_1); io_wait(); - outb(pic_1_data, irq_0); io_wait(); - outb(pic_2_data, irq_8); io_wait(); + outb(PIC_1_DATA, IRQ_0); io_wait(); + outb(PIC_2_DATA, IRQ_8); io_wait(); - outb(pic_1_data, 0x01); io_wait(); - outb(pic_2_data, 0x01); io_wait(); + outb(PIC_1_DATA, 0x01); io_wait(); + outb(PIC_2_DATA, 0x01); io_wait(); - outb(pic_1_data, 0b11111101); io_wait(); - outb(pic_2_data, 0xff); io_wait(); + outb(PIC_1_DATA, 0b11111101); io_wait(); + outb(PIC_2_DATA, 0xff); io_wait(); } void idt_load() { @@ -37,7 +37,7 @@ void idt_register(unsigned char vec, int_handler handler) { unsigned int addr; addr = (unsigned int) handler; - idt[vec].isr_low = addr & 0xffffffff; + idt[vec].isr_low = addr & 0xffff; idt[vec].kernel_cs = 0x08; idt[vec].reserved = 0; idt[vec].attr = 0x8e; @@ -45,8 +45,10 @@ void idt_register(unsigned char vec, int_handler handler) { } void idt_init(void) { + int i; + asm volatile ("cli"); - for (int i = 0; i < 256; i++) { + for (i = 0; i < 256; i++) { idt_register(i, excp_handler); } diff --git a/init.c b/init.c @@ -3,8 +3,9 @@ size_t read(void *buf, size_t size) { size_t bytes_read; size_t i; + char *ptr; - char *ptr = (char *)buf; + ptr = (char *)buf; bytes_read = 0; for (i = 0; i < size; i++) { @@ -15,6 +16,9 @@ size_t read(void *buf, size_t size) { } void start_kernel(uint32_t magic, multiboot_info_t *mbi) { + char c; + size_t bytes; + (void)magic; (void)mbi; @@ -27,21 +31,7 @@ void start_kernel(uint32_t magic, multiboot_info_t *mbi) { asm volatile ("sti"); - uint8_t ata_buf[512] = {}; - ata_read_sectors(ata_buf, 0, 1); - - for (int i = 0; i < 512; i++) { - char c = ata_buf[i]; - if ((c > 32 && c < 127) || - c == ' ') { - tty_write(&c, 1); - } - } - while (1) { - char c; - size_t bytes; - bytes = read(&c, 1); if (bytes > 0) { diff --git a/io.asm b/io.asm @@ -1,3 +1,5 @@ +bits 32 + global outb global outw global inb diff --git a/keyboard/keyboard.c b/keyboard/keyboard.c @@ -3,7 +3,7 @@ unsigned int shift_state = 0; -unsigned char buf[buf_size]; +unsigned char buf[BUF_SIZE]; unsigned int buf_head = 0; unsigned int buf_tail = 0; @@ -30,7 +30,7 @@ char keyboard_read(void) { asm volatile ("cli"); if (buf_head != buf_tail) { ch = buf[buf_head]; - buf_head = (buf_head + 1) % buf_size; + buf_head = (buf_head + 1) % BUF_SIZE; asm volatile ("sti"); return ch; } @@ -64,7 +64,7 @@ void keyboard_handler(void) { } if (ascii_char != 0) { - next_tail = (buf_tail + 1) % buf_size; + next_tail = (buf_tail + 1) % BUF_SIZE; if (next_tail != buf_head) { buf[buf_tail] = ascii_char; buf_tail = next_tail; diff --git a/keyboard/keyboard.h b/keyboard/keyboard.h @@ -1,10 +1,10 @@ -#ifndef keyboard_h -#define keyboard_h +#ifndef KEYBOARD_H +#define KEYBOARD_H -#define buf_size 16 +#define BUF_SIZE 16 char keyboard_read(void); void keyboard_init(void); -#endif /* keyboard_h */ +#endif /* KEYBOARD_H */ diff --git a/oxytoxin.h b/oxytoxin.h @@ -1,5 +1,5 @@ -#ifndef oxytoxin_h -#define oxytoxin_h +#ifndef OXYTOXIN_H +#define OXYTOXIN_H #include <stddef.h> #include <stdint.h> @@ -11,4 +11,4 @@ #include "keyboard/keyboard.h" #include "ata/ata.h" -#endif /* oxytoxin_h */ +#endif /* OXYTOXIN_H */ diff --git a/pit.c b/pit.c @@ -11,18 +11,18 @@ void pit_handler(void) { void pit_init(uint32_t freq) { /* calc divisor */ - uint32_t divisor = pit_base_freq / freq; + uint32_t divisor = PIT_BASE_FREQ / freq; /* limit */ if (divisor > 0xffff) divisor = 0xffff; if (divisor < 1) divisor = 1; /* command byte */ - outb(pit_comm, 0x36); + outb(PIT_COMM, 0x36); /* lsb */ - outb(pit_chan0, (uint8_t)(divisor & 0xff)); + outb(PIT_CHAN0, (uint8_t)(divisor & 0xff)); /* msb */ - outb(pit_chan0, (uint8_t)((divisor >> 8) & 0xff)); + outb(PIT_CHAN0, (uint8_t)((divisor >> 8) & 0xff)); idt_register(0x20, irq0_stub); } diff --git a/tty.c b/tty.c @@ -3,7 +3,7 @@ size_t tty_row; size_t tty_column; uint8_t tty_color; -uint16_t* tty_buffer = (uint16_t*)vga_start; +uint16_t* tty_buffer = (uint16_t*)VGA_START; void tty_set_color(uint8_t color) { tty_color = color; @@ -12,7 +12,7 @@ void tty_set_color(uint8_t color) { void tty_putentryat(char c, uint8_t color, size_t x, size_t y) { size_t index; - index = y * vga_width + x; + index = y * VGA_WIDTH + x; tty_buffer[index] = vga_entry(c, color); } @@ -24,9 +24,9 @@ void tty_clear(void) { tty_row = 0; tty_column = 0; - for (y = 0; y < vga_height; y++) { - for (x = 0; x < vga_width; x++) { - index = y * vga_width + x; + for (y = 0; y < VGA_HEIGHT; y++) { + for (x = 0; x < VGA_WIDTH; x++) { + index = y * VGA_WIDTH + x; tty_buffer[index] = vga_entry(' ', tty_color); } } @@ -35,7 +35,7 @@ void tty_clear(void) { void tty_init(void) { vga_cursor_enable(14, 15); - tty_color = vga_entry_color(vga_color_light_grey, vga_color_black); + tty_color = vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK); tty_clear(); } @@ -48,17 +48,17 @@ void tty_scroll(void) { size_t index; /* move lines to line-1 */ - for (row = 1; row < vga_height; row++) { - for (col = 0; col < vga_width; col++) { - src_index = row * vga_width + col; - dst_index = (row - 1) * vga_width + col; + for (row = 1; row < VGA_HEIGHT; row++) { + for (col = 0; col < VGA_WIDTH; col++) { + src_index = row * VGA_WIDTH + col; + dst_index = (row - 1) * VGA_WIDTH + col; tty_buffer[dst_index] = tty_buffer[src_index]; } } /* clear last line*/ blank = ' ' | (tty_color << 8); - for (col = 0; col < vga_width; col++) { - index = (vga_height - 1) * vga_width + col; + for (col = 0; col < VGA_WIDTH; col++) { + index = (VGA_HEIGHT - 1) * VGA_WIDTH + col; tty_buffer[index] = blank; } } @@ -68,9 +68,9 @@ void tty_putchar(char c) { tty_row++; tty_column = 0; - if (tty_row >= vga_height) { + if (tty_row >= VGA_HEIGHT) { tty_scroll(); - tty_row = vga_height - 1; + tty_row = VGA_HEIGHT - 1; } } else if(c == '\b') { if (tty_column > 0) { @@ -79,11 +79,11 @@ void tty_putchar(char c) { } } else { tty_putentryat(c, tty_color, tty_column, tty_row); - if (++tty_column == vga_width) { + if (++tty_column == VGA_WIDTH) { tty_column = 0; - if (++tty_row == vga_height) { + if (++tty_row == VGA_HEIGHT) { tty_scroll(); - tty_row = vga_height - 1; + tty_row = VGA_HEIGHT - 1; } } } diff --git a/vga.c b/vga.c @@ -1,37 +1,37 @@ #include "oxytoxin.h" void vga_cursor_enable(uint8_t s, uint8_t e) { - outb(vga_ctrl, 0x0a); - outb(vga_data, (inb(vga_data) & 0xc0) | s); + outb(VGA_CTRL, 0x0a); + outb(VGA_DATA, (inb(VGA_DATA) & 0xc0) | s); - outb(vga_ctrl, 0x0b); - outb(vga_data, (inb(vga_data) & 0xe0) | e); + outb(VGA_CTRL, 0x0b); + outb(VGA_DATA, (inb(VGA_DATA) & 0xe0) | e); } void vga_cursor_disable(void) { - outb(vga_ctrl, 0x0a); - outb(vga_data, 0x20); + outb(VGA_CTRL, 0x0a); + outb(VGA_DATA, 0x20); } void vga_cursor_update(int x, int y) { uint16_t pos; - pos = y * vga_width + x; - outb(vga_ctrl, 0x0f); - outb(vga_data, (uint8_t) (pos & 0xff)); + pos = y * VGA_WIDTH + x; + outb(VGA_CTRL, 0x0f); + outb(VGA_DATA, (uint8_t) (pos & 0xff)); - outb(vga_ctrl, 0x0e); - outb(vga_data, (uint8_t) ((pos >> 8) & 0xff)); + outb(VGA_CTRL, 0x0e); + outb(VGA_DATA, (uint8_t) ((pos >> 8) & 0xff)); } uint16_t vga_cursor_get_pos(void) { uint16_t pos; pos = 0; - outb(vga_ctrl, 0x0f); + outb(VGA_CTRL, 0x0f); pos |= inb(0x3d5); - outb(vga_ctrl, 0x0e); + outb(VGA_CTRL, 0x0e); pos |= ((uint16_t)inb(0x3d5) << 8); return pos;