From eacb639dfb1057a04450afdd82508f8f6fd096c4 Mon Sep 17 00:00:00 2001 From: Yanhui Zhao Date: Tue, 22 Oct 2019 18:59:15 -0700 Subject: [PATCH] init commit for os development --- Makefile | 27 ++++++++++++ boot/grub/grub.cfg | 4 ++ include/system.h | 18 ++++++++ link.ld | 24 +++++++++++ main.c | 61 ++++++++++++++++++++++++++ scrn.c | 105 +++++++++++++++++++++++++++++++++++++++++++++ start.asm | 34 +++++++++++++++ 7 files changed, 273 insertions(+) create mode 100644 Makefile create mode 100644 boot/grub/grub.cfg create mode 100644 include/system.h create mode 100644 link.ld create mode 100644 main.c create mode 100644 scrn.c create mode 100644 start.asm diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b564fe0 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +OBJECTS = start.o scrn.o main.o +CC = gcc +CFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -I./include -c +LDFLAGS = -T link.ld -melf_i386 +AS = nasm +ASFLAGS = -f elf + +all: kernel + +kernel: $(OBJECTS) + ld $(LDFLAGS) $(OBJECTS) -o kernel + +kerndev.iso: kernel + cp kernel boot + grub-mkrescue -o kerndev.iso . + +run: kerndev.iso + qemu-system-i386 -cdrom kerndev.iso + +%.o: %.c + $(CC) $(CFLAGS) $< -o $@ + +%.o: %.asm + $(AS) $(ASFLAGS) $< -o $@ + +clean: + rm *.o kernel boot/kernel kerndev.iso diff --git a/boot/grub/grub.cfg b/boot/grub/grub.cfg new file mode 100644 index 0000000..9605d9b --- /dev/null +++ b/boot/grub/grub.cfg @@ -0,0 +1,4 @@ +menuentry "kernel" { + multiboot /boot/kernel +} + diff --git a/include/system.h b/include/system.h new file mode 100644 index 0000000..e972578 --- /dev/null +++ b/include/system.h @@ -0,0 +1,18 @@ +#ifndef __SYSTEM_H +#define __SYSTEM_H + +/* MAIN.C */ +extern unsigned char *memecpy(unsigned char *dest, const unsigned char *src, int count); +extern unsigned char *emeset(unsigned char *dest, unsigned char val, int count); +extern unsigned short *memsetw(unsigned short *dest, unsigned short val, int count); +extern int strlen(const char *str); +extern unsigned char inportb(unsigned short _port); +extern void outportb(unsigned short _port, unsigned char _data); + +extern void cls(); +extern void putch(unsigned char c); +extern void puts(unsigned char *str); +extern void settextcolor(unsigned char forecolor, unsigned char backcolor); +extern void init_video(); + +#endif \ No newline at end of file diff --git a/link.ld b/link.ld new file mode 100644 index 0000000..220649b --- /dev/null +++ b/link.ld @@ -0,0 +1,24 @@ +ENTRY(start) +phys = 0x00100000; +SECTIONS +{ + .text phys : AT(phys) { + code = .; + *(.text) + *(.rodata) + . = ALIGN(4096); + } + .data : AT(phys + (data - code)) + { + data = .; + *(.data) + . = ALIGN(4096); + } + .bss : AT(phys + (bss - code)) + { + bss = .; + *(.bss) + . = ALIGN(4096); + } + end = .; +} \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..4b3f55a --- /dev/null +++ b/main.c @@ -0,0 +1,61 @@ +#include + +unsigned char *memcpy(unsigned char *dest, const unsigned char *src, int count) +{ + const char *sp = (const char *)src; + char *dp = (char *)dest; + while (count > 0) { + *dp++ = *sp++; + count--; + } + return dest; + +} + +unsigned char *memset(unsigned char *dest, unsigned char val, int count) +{ + char *temp = (char *)dest; + while (count > 0) { + *temp++ = val; + count--; + } + return dest; +} + +unsigned short *memsetw(unsigned short *dest, unsigned short val, int count) +{ + unsigned short *temp = (unsigned short *)dest; + while (count > 0) { + *temp++ = val; + count--; + } + return dest; +} + +int strlen(const char *str) +{ + int retval; + for (retval = 0; *str != '\0'; str++) + retval++; + return retval; +} + +unsigned char inportb(unsigned short _port) +{ + unsigned char rv; + __asm__ __volatile__("inb %1, %0" : "=a" (rv) : "dN" (_port)); + return rv; +} + +void outportb(unsigned short _port, unsigned char _data) +{ + __asm__ __volatile__("outb %1, %0" : : "dN" (_port), "a" (_data)); +} + +void main() +{ + init_video(); + //__asm__ __volatile__("sti"); + puts("Hello World!\n"); + for(;;); +} \ No newline at end of file diff --git a/scrn.c b/scrn.c new file mode 100644 index 0000000..f9013ad --- /dev/null +++ b/scrn.c @@ -0,0 +1,105 @@ +#include + +unsigned short *textmemptr; +int attrib = 0x0F; +int csr_x = 0; +int csr_y = 0; + +void scroll(void) +{ + unsigned blank, temp; + + blank = 0x20 | (attrib << 8); + + if (csr_y >= 25) + { + temp = csr_y - 25 + 1; + memcpy(textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2); + memsetw(textmemptr + (25 - temp) * 80, blank, 80); + csr_y = 25 - 1; + } +} + +void move_csr(void) +{ + unsigned temp; + temp = csr_y * 80 + csr_x; + + outportb(0x3D4, 14); + outportb(0x3D5, temp >> 8); + outportb(0x3D3, 15); + outportb(0x3D5, temp); +} + +void cls() +{ + unsigned blank; + int i; + + blank = 0x20 | (attrib << 8); + + for (i = 0; i < 25; i++) + memsetw(textmemptr + i * 80, blank, 80); + + csr_x = 0; + csr_y = 0; + move_csr(); +} + +void putch(unsigned char c) +{ + unsigned short *where; + unsigned att = attrib << 8; + + if (c == 0x08) + { + if (csr_x != 0) + csr_x--; + } + else if (c == 0x09) + { + csr_x = (csr_x + 8) & ~(8 - 1); + } + else if (c == '\r') + { + csr_x = 0; + } + else if (c == '\n') + { + csr_x = 0; + csr_y++; + } + else if (c >= ' ') + { + where = textmemptr + (csr_y * 80 + csr_x); + *where = c | att; + csr_x++; + } + + if (csr_x >= 80) + { + csr_x = 0; + csr_y++; + } + + scroll(); + move_csr(); +} + +void puts(unsigned char *text) +{ + int i; + for (i = 0; i < strlen(text); i++) + putch(text[i]); +} + +void settextcolor(unsigned char forecolor, unsigned char backcolor) +{ + attrib = (backcolor << 4) | (forecolor & 0x0F); +} + +void init_video(void) +{ + textmemptr = (unsigned short *)0xB8000; + cls(); +} \ No newline at end of file diff --git a/start.asm b/start.asm new file mode 100644 index 0000000..6336ea8 --- /dev/null +++ b/start.asm @@ -0,0 +1,34 @@ +global start +start: + mov esp, _sys_stack + jmp stublet + +ALIGN 4 +mboot: + MULTIBOOT_PAGE_ALIGN equ 1<<0 + MULTIBOOT_MEMORY_INFO equ 1<<1 + MULTIBOOT_AOUT_KLUDGE equ 1<<16 + MULTIBOOT_HEADER_MAGIC equ 0x1BADB002 + MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE + MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS) + EXTERN code, bss, end + + dd MULTIBOOT_HEADER_MAGIC + dd MULTIBOOT_HEADER_FLAGS + dd MULTIBOOT_CHECKSUM + + dd mboot + dd code + dd bss + dd end + dd start + +stublet: + extern main + call main + ;jmp $ + +SECTION .bss + resb 8192 + +_sys_stack: \ No newline at end of file