Add MyOS: 2011 GRUB/multiboot test kernel
This commit is contained in:
1 parent
d30ec5b5e9
commit
819425f1d0
13 files changed
+363
No files matched your search
@@ -0,0 +1,9 @@
|
||||
default 0
|
||||
timeout 0
|
||||
hiddenmenu
|
||||
|
||||
title MyOS
|
||||
root (fd0)
|
||||
kernel /kernel.bin
|
||||
boot
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/bin/sh
|
||||
|
||||
build=build
|
||||
compiler=i586-elf-gcc
|
||||
linker=i586-elf-ld
|
||||
opt="-Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I./kernel/include -c -fno-strict-aliasing -fno-common -fno-stack-protector"
|
||||
|
||||
echo "Building..."
|
||||
echo "==========="
|
||||
|
||||
# Build C files
|
||||
|
||||
$compiler $opt -o $build/main.o ./kernel/main.c
|
||||
$compiler $opt -o $build/console.o ./kernel/console.c
|
||||
|
||||
# Build ASM files
|
||||
nasm -f elf ./kernel/loader.asm -o $build/loader.o
|
||||
|
||||
# Link
|
||||
$linker -T linker.ld $build/*.o
|
||||
|
||||
# Put on floppy image
|
||||
mount A: /mnt/floppy
|
||||
cp kernel.bin /mnt/floppy/
|
||||
@@ -0,0 +1,110 @@
|
||||
#include <console.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
UPoint ConsoleCursor = {0, 0};
|
||||
UPoint ConsoleSize = {ConsoleWidth, ConsoleHeight};
|
||||
|
||||
uint8* VideoRam = (uint8*)0xb8000;
|
||||
|
||||
void ConsoleClear()
|
||||
{
|
||||
uint32 i;
|
||||
for (i = 0; i < ConsoleSize.X * ConsoleSize.Y * 2; i++)
|
||||
VideoRam[i] = 0;
|
||||
}
|
||||
|
||||
void ConsoleScroll()
|
||||
{
|
||||
uint32 i;
|
||||
|
||||
for (i = 0; i < ConsoleSize.X * (ConsoleSize.Y-1) * 2; i++)
|
||||
VideoRam[i] = VideoRam[i + (ConsoleSize.X * 2)];
|
||||
|
||||
for (i = ConsoleSize.X * (ConsoleSize.Y-1) * 2; i < ConsoleSize.X * ConsoleSize.Y * 2; i++)
|
||||
VideoRam[i] = 0;
|
||||
|
||||
ConsoleCursor.Y--;
|
||||
}
|
||||
|
||||
void ConsoleWriteChar(char c)
|
||||
{
|
||||
ConsoleWriteCharColored(c, ConsoleDefaultColor);
|
||||
}
|
||||
|
||||
void ConsoleWriteCharColored (char c, uint8 color)
|
||||
{
|
||||
uint32 location = ConsoleCursor.X + ConsoleCursor.Y * ConsoleSize.X;
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '\n': ConsoleCursor.X = 0; ConsoleCursor.Y ++; break;
|
||||
case '\r': ConsoleCursor.X = 0; break;
|
||||
case '\t': while (++ConsoleCursor.X % 6 != 0); break;
|
||||
|
||||
default:
|
||||
|
||||
VideoRam[2*location] = c;
|
||||
VideoRam[2*location + 1] = color;
|
||||
|
||||
ConsoleCursor.X++;
|
||||
break;
|
||||
}
|
||||
|
||||
if (ConsoleCursor.X >= ConsoleSize.X) {
|
||||
ConsoleCursor.X = 0;
|
||||
ConsoleCursor.Y ++;
|
||||
}
|
||||
|
||||
if (ConsoleCursor.Y >= ConsoleSize.Y) ConsoleScroll();
|
||||
}
|
||||
|
||||
void ConsoleWriteString (string s)
|
||||
{
|
||||
ConsoleWriteStringColored (s, ConsoleDefaultColor);
|
||||
}
|
||||
|
||||
void ConsoleWriteStringColored (string s, uint8 color)
|
||||
{
|
||||
while (*s != 0)
|
||||
{
|
||||
ConsoleWriteCharColored (*s, color);
|
||||
s = s + 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ConsoleWrite (string format, ...)
|
||||
{
|
||||
if (!format || (*format) == 0) return;
|
||||
|
||||
va_list args;
|
||||
va_start (args, format);
|
||||
uint8 color = ConsoleDefaultColor;
|
||||
|
||||
for (; *format != 0; format = format+1)
|
||||
if (*format != '%') ConsoleWriteCharColored (*format, color);
|
||||
else {
|
||||
format = format+1;
|
||||
switch (*format)
|
||||
{
|
||||
case 'S':
|
||||
case 's':
|
||||
{
|
||||
string s = (string) va_arg(args, string);
|
||||
ConsoleWriteStringColored (s, color);
|
||||
break;
|
||||
}
|
||||
|
||||
case 'C':
|
||||
case 'c':
|
||||
{
|
||||
char c = (char) va_arg(args, char);
|
||||
ConsoleWriteCharColored (c, color);
|
||||
break;
|
||||
}
|
||||
|
||||
case '#': color = (uint8) va_arg(args, uint8); break;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
#ifndef __CONSOLE_H__
|
||||
#define __CONSOLE_H__
|
||||
|
||||
#include <types.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#define ConsoleWidth 80
|
||||
#define ConsoleHeight 25
|
||||
#define ConsoleDefaultColor 0x7
|
||||
|
||||
enum Colors {
|
||||
ColorBlack = 0x0,
|
||||
ColorBlue = 0x1,
|
||||
ColorGreen = 0x2,
|
||||
ColorCyan = 0x3,
|
||||
ColorRed = 0x4,
|
||||
ColorMagenta = 0x5,
|
||||
ColorBrown = 0x6,
|
||||
ColorLightGray = 0x7,
|
||||
ColorDarkGray = 0x8,
|
||||
ColorLightBlue = 0x9,
|
||||
ColorLightGreen = 0xA,
|
||||
ColorLightCyan = 0xB,
|
||||
ColorLightRed = 0xC,
|
||||
ColorLightMagenta = 0xD,
|
||||
ColorYellow = 0xE,
|
||||
ColorWhite = 0xF
|
||||
};
|
||||
|
||||
extern void ConsoleClear();
|
||||
extern void ConsoleWriteChar(char c);
|
||||
extern void ConsoleWriteCharColored (char c, uint8 color);
|
||||
extern void ConsoleWriteString (string s);
|
||||
extern void ConsoleWriteStringColored (string s, uint8 color);
|
||||
extern void ConsoleScroll();
|
||||
|
||||
extern void ConsoleWrite (string format, ...);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef __STDARG__H
|
||||
#define __STDARG__H
|
||||
|
||||
#include <va_list.h>
|
||||
|
||||
/* width of stack == width of int */
|
||||
#define STACKITEM int
|
||||
|
||||
/* round up width of objects pushed on stack. The expression before the
|
||||
& ensures that we get 0 for objects of size 0. */
|
||||
#define VA_SIZE(TYPE) \
|
||||
((sizeof(TYPE) + sizeof(STACKITEM) - 1) \
|
||||
& ~(sizeof(STACKITEM) - 1))
|
||||
|
||||
/* &(LASTARG) points to the LEFTMOST argument of the function call
|
||||
(before the ...) */
|
||||
#define va_start(AP, LASTARG) \
|
||||
(AP=((va_list)&(LASTARG) + VA_SIZE(LASTARG)))
|
||||
|
||||
/* nothing for va_end */
|
||||
#define va_end(AP)
|
||||
|
||||
#define va_arg(AP, TYPE) \
|
||||
(AP += VA_SIZE(TYPE), *((TYPE *)(AP - VA_SIZE(TYPE))))
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef __TYPES_H__
|
||||
#define __TYPES_H__
|
||||
|
||||
typedef signed char int8;
|
||||
typedef unsigned char uint8;
|
||||
|
||||
typedef signed short int16;
|
||||
typedef unsigned short uint16;
|
||||
|
||||
typedef signed int int32;
|
||||
typedef unsigned int uint32;
|
||||
|
||||
typedef signed long long int64;
|
||||
typedef unsigned long long uint64;
|
||||
|
||||
typedef char* string;
|
||||
|
||||
typedef struct {
|
||||
int32 X, Y;
|
||||
} Point;
|
||||
|
||||
typedef struct {
|
||||
uint32 X, Y;
|
||||
} UPoint;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
#ifndef __VA_LIST_H__
|
||||
#define __VA_LIST_H__
|
||||
|
||||
/* va list parameter list */
|
||||
typedef unsigned char *va_list;
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
bits 32
|
||||
|
||||
global loader
|
||||
extern kmain
|
||||
|
||||
; Multiboot header
|
||||
MODULEALIGN equ 1<<0
|
||||
MEMINFO equ 1<<1
|
||||
FLAGS equ MODULEALIGN | MEMINFO
|
||||
MAGIC equ 0x1BADB002
|
||||
CHECKSUM equ -(MAGIC + FLAGS)
|
||||
|
||||
MultibootHeader:
|
||||
dd MAGIC
|
||||
dd FLAGS
|
||||
dd CHECKSUM
|
||||
|
||||
STACKSIZE equ 0x4000 ; 16 KB
|
||||
|
||||
loader:
|
||||
cmp eax, 0x2BADB002 ; verify booted with grub
|
||||
jne .bad
|
||||
|
||||
mov esp, STACKSIZE + stack
|
||||
mov ax, 0x10
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
|
||||
push ebx
|
||||
call kmain
|
||||
|
||||
.bad:
|
||||
cli
|
||||
hlt
|
||||
|
||||
align 4
|
||||
stack:
|
||||
TIMES STACKSIZE db 0
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <console.h>
|
||||
|
||||
void kmain (void* MultibootStructure)
|
||||
{
|
||||
ConsoleClear();
|
||||
ConsoleWrite("Hello world :D %# Hello world\n", ColorLightCyan);
|
||||
ConsoleWrite("Character: '%c' String: '%s'\n", 'p', "Hi there");
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
ENTRY (loader)
|
||||
OUTPUT ("kernel.bin")
|
||||
|
||||
addr = 0x100000;
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
.text addr :
|
||||
ALIGN(0x1000)
|
||||
{
|
||||
*(.text*);
|
||||
*(.rodata*);
|
||||
}
|
||||
|
||||
.data :
|
||||
ALIGN(0x1000)
|
||||
{
|
||||
*(.data*);
|
||||
}
|
||||
|
||||
.bss :
|
||||
ALIGN(0x1000)
|
||||
{
|
||||
*(.bss*);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Executable
+33
@@ -0,0 +1,33 @@
|
||||
#!/bin/sh
|
||||
|
||||
build=build
|
||||
|
||||
clear
|
||||
echo "Building..."
|
||||
echo "==========="
|
||||
|
||||
# Build C files
|
||||
echo "[ 5%] main.c"
|
||||
|
||||
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -nostdinc -fno-builtin -I./kernel/include -c -o $build/main.o ./kernel/main.c -fno-strict-aliasing -fno-common -fno-stack-protector
|
||||
|
||||
# Build ASM files
|
||||
echo "[50%] loader.asm"
|
||||
nasm -f elf ./kernel/loader.asm -o $build/loader.o
|
||||
|
||||
# Link
|
||||
echo "[90%] Linking..."
|
||||
ld -T linker.ld $build/*.o
|
||||
|
||||
# Put on floppy image
|
||||
echo "[99%] Writing to floppy image..."
|
||||
cp Boot/myos.img myos.img
|
||||
mkdir /media/floppy1
|
||||
mount -o loop myos.img /media/floppy1
|
||||
cp kernel.bin /media/floppy1/
|
||||
umount /media/floppy1
|
||||
rm -r /media/floppy1
|
||||
|
||||
# DONE
|
||||
echo "[100] DONE!"
|
||||
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
void kmain (void* MultibootStructure)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -54,3 +54,10 @@ into the kernel (2011).
|
||||
`conio.h` reference), 2010. The files with a `.HT~` extension are 8.3-mangled
|
||||
names (probably `.html~` backups); the CKEditor copy that came with them is not included.
|
||||
The Nov 2010 - Mar 2011 NetBeans project shell is in `ctaos/` above.
|
||||
|
||||
- `MyOS/` (Apr-Aug 2011): small GRUB/multiboot test kernel written while
|
||||
learning to set up an i586-elf cross toolchain, shortly before the luxOS
|
||||
kernel proper: NASM multiboot loader, `kmain`, a VGA text console with
|
||||
printf-style `ConsoleWrite`, `build.sh`, linker script and a GRUB `menu.cfg`.
|
||||
`tar-2011-04/` holds the older April versions of `main.c` and `build.sh` found
|
||||
in `MyOS.tar`. Disk images (`myos.img`), GRUB stages and `kernel.bin` are not included.
|
||||
Reference in new issue
Block a user