diff --git a/Makefile b/Makefile index 116f681..f28770b 100644 --- a/Makefile +++ b/Makefile @@ -2,12 +2,13 @@ CFLAGS = -g -std=gnu11 -m32 -mgeneral-regs-only -nostdlib -fno-builtin -fno-exce CXXFLAGS = -g -m32 -fno-use-cxa-atexit -mgeneral-regs-only -nostdlib -fno-builtin -fno-rtti -fno-exceptions -fno-leading-underscore -fpermissive -fno-pie -fno-stack-protector -I. LDFLAGS = -DISK_IMG_FILES = build/kernel/kernel.bin hello.txt alpha.txt +DISK_IMG_FILES = build/kernel/kernel.bin build/program/program.bin hello.txt alpha.txt KERNEL_OBJS = build/kernel/entry.o build/kernel/screenstuff.o build/kernel/io.o build/kernel/idt.o build/kernel/keyboard.o \ build/kernel/strings.o build/kernel/atapio.o build/kernel/kmain.o build/kernel/paging.o build/kernel/allocate.o \ build/kernel/gdt.o build/kernel/memory.o build/kernel/process.o build/kernel/datatypes/hash.o \ build/kernel/terminal.o build/kernel/global.o build/kernel/kernel.o -STAGE2_OBS = build/c_code_entry.o build/boot_stage2/io.o build/boot_stage2/atapio.o build/boot_stage2/strings.o build/boot_stage2/screenstuff.o build/boot_stage2/stage2.o build/boot_stage2/paging.o +STAGE2_OBJS = build/c_code_entry.o build/boot_stage2/io.o build/boot_stage2/atapio.o build/boot_stage2/strings.o build/boot_stage2/screenstuff.o build/boot_stage2/stage2.o build/boot_stage2/paging.o +PROGRAM_OBJS = build/program_code_entry.o build/program/program.o run: disk.img qemu-system-x86_64 disk.img @@ -30,6 +31,7 @@ prepare: mkdir -p build/boot_stage2 mkdir -p build/kernel mkdir -p build/kernel/datatypes + mkdir -p build/program mountpoint img.d | grep not || umount img.d clean: @@ -39,15 +41,15 @@ build/boot/boot.bin: src/boot/boot.asm nasm $< -o $@ # Boot Stage 2 -build/boot_stage2/boot.bin: src/boot_stage2/boot_stage2.ld $(STAGE2_OBS) - ld $(LDFLAGS) -T $< $(STAGE2_OBS) +build/boot_stage2/boot.bin: src/boot_stage2/boot_stage2.ld $(STAGE2_OBJS) + ld $(LDFLAGS) -T $< $(STAGE2_OBJS) build/boot_stage2/%.o: src/boot_stage2/%.c gcc $(CFLAGS) -o $@ -c $< # Kernel -build/kernel/kernel.bin: build/kernel/kernel.elf - objcopy -O binary build/kernel/kernel.elf build/kernel/kernel.bin +build/%.bin: build/%.elf + objcopy -O binary $< $@ build/kernel/kernel.elf: src/kernel/kernel.ld $(KERNEL_OBJS) ld $(LDFLAGS) -T $< $(KERNEL_OBJS) @@ -55,15 +57,16 @@ build/kernel/kernel.elf: src/kernel/kernel.ld $(KERNEL_OBJS) build/boot_stage2/stage2.o: src/boot_stage2/main.c gcc $(CFLAGS) -o $@ -c $< -build/kernel/%.o: src/kernel/%.c +build/%.o: src/%.c gcc $(CFLAGS) -o $@ -c $< -build/kernel/%.o: src/kernel/%.cpp +build/%.o: src/%.cpp g++ $(CXXFLAGS) -o $@ -c $< -build/kernel/%.o: src/kernel/%.asm +build/%.o: src/%.asm nasm -felf32 $< -o $@ -# Generic -build/%.o: src/%.asm - nasm -felf32 $< -o $@ \ No newline at end of file +# Program + +build/program/program.bin: src/program/program.ld $(PROGRAM_OBJS) + ld $(LDFLAGS) -T $< $(PROGRAM_OBJS) \ No newline at end of file diff --git a/src/program/program.c b/src/program/program.c new file mode 100644 index 0000000..b9f390b --- /dev/null +++ b/src/program/program.c @@ -0,0 +1,80 @@ +#include "../kernel/types.h" + +void print(char* string) { + asm volatile ("mov $0, %%eax; mov %0, %%esi; int $0x7f" : : "m" (string) : "eax", "esi"); +} + +char getch() { + asm volatile ("mov $1, %%eax; int $0x7f" : : :); +} + +uint8_t getchPS2() { + asm volatile ("mov $2, %%eax; int $0x7f" : : :); +} + +void readfile(char* filename, uint8_t* buffer) { + asm volatile ("mov $3, %%eax; mov %0, %%esi; mov %1, %%edi; int $0x7f" : : "m" (filename), "m" (buffer) : "eax", "esi", "edi"); +} + +void* localalloc(uint32_t size) { + asm volatile ("mov $4, %%eax; mov %0, %%esi; int $0x7f" : : "m" (size) : "esi"); +} + +void localdelete(void* ptr) { + asm volatile ("mov $5, %%eax; mov %0, %%esi; int $0x7f" : : "m" (ptr) : "esi"); +} + +uint32_t filesize(char* filename) { + asm volatile ("mov $6, %%eax; mov %0, %%esi; int $0x7f" : : "m" (filename) : "esi"); +} + +int int_to_decimal(unsigned int number, char* string_buffer) { + for (int i=0; i<11; i++) + string_buffer[i] = 0; + + int index = 9; + unsigned int acc = number; + if (acc == 0) + string_buffer[index--] = '0'; + while (acc != 0) { + string_buffer[index--] = 0x30+(acc%10); + acc /= 10; + } + return (index+1); +} + +char dec_to_hex[16] = "0123456789abcdef"; +int int_to_hex(unsigned int number, char* string_buffer) { + for (int i=0; i<8; i++) + string_buffer[i] = '0'; + string_buffer[8] = 0; + + int index = 7; + unsigned int acc = number; + if (acc == 0) + string_buffer[index--] = '0'; + while (acc != 0) { + string_buffer[index--] = dec_to_hex[acc%0x10]; + acc /= 0x10; + } + return (index+1); +} + +int main() { + print("Testing C code program\n"); + print("My strings are messed up for some reason...\n"); + + uint32_t alpha_size = filesize("HELLO TXT"); + char sizebuf[32]; + uint32_t index = int_to_decimal(alpha_size, sizebuf); + print(sizebuf+index); + print("\n"); + uint8_t* alpha_buffer = (uint8_t*)localalloc(alpha_size + 32); + print("alpha_buffer: "); + index = int_to_hex(alpha_buffer, sizebuf); + print(sizebuf+index); + print("\n"); + readfile("HELLO TXT", alpha_buffer); + print(alpha_buffer); + while (1); +} \ No newline at end of file diff --git a/src/program/program.ld b/src/program/program.ld new file mode 100644 index 0000000..ae32311 --- /dev/null +++ b/src/program/program.ld @@ -0,0 +1,13 @@ +OUTPUT_FORMAT(binary) +OUTPUT_ARCH(i386:i386) + +OUTPUT(build/program/program.bin) + +SECTIONS { + . = 0x8020; + + .text : { + build/program_code_entry.o(.text) + build/program/program.o(.text) + } +} \ No newline at end of file diff --git a/src/program_code_entry.asm b/src/program_code_entry.asm new file mode 100644 index 0000000..7f20292 --- /dev/null +++ b/src/program_code_entry.asm @@ -0,0 +1,7 @@ +[BITS 32] + +_start: + call main + int 0x80 + +extern main \ No newline at end of file