Compare commits
184 Commits
Author | SHA1 | Date | |
---|---|---|---|
cb6bd3914c | |||
56c3be9207 | |||
c6ae200a54 | |||
6b1de7c9d4 | |||
ebfeca47b5 | |||
43f4a1f4b1 | |||
0f73efbdc3 | |||
a084ab296c | |||
aa28284187 | |||
87c135b010 | |||
3d30025d3b | |||
556b0f0c4d | |||
bffd652c16 | |||
cbd427b14b | |||
fa4ff5facf | |||
db16e69d5d | |||
1d3b0a5621 | |||
d03a34ab3e | |||
494ef6de81 | |||
784009ba6c | |||
9bddf11ee4 | |||
313b86fdc7 | |||
75491dd36b | |||
c8db9fe905 | |||
d0cbaba4d1 | |||
1f22d0c900 | |||
a474368435 | |||
fb88595dec | |||
70b4db9dbb | |||
9fde638b6e | |||
b4b77dd52c | |||
79951bd0f1 | |||
41152cb4ca | |||
dba3d28bbf | |||
610f30fae9 | |||
e22b90b768 | |||
08fc9a5445 | |||
a66df19edc | |||
5bc844649b | |||
0d390465f6 | |||
04d29fdb71 | |||
e9f426894c | |||
c8810e4701 | |||
b202eca0cc | |||
05de14800b | |||
8ae84716d6 | |||
bb495eceec | |||
67b90a482f | |||
85e5b76aa3 | |||
412925a790 | |||
21afa93c4b | |||
d08d54ecbf | |||
06644a02f9 | |||
8f9881889b | |||
a91ea8d620 | |||
a9a2f6dcaf | |||
9befd553eb | |||
6053745ee9 | |||
0adc6e395c | |||
bbf212d5a2 | |||
94ed9078df | |||
e97e76da7b | |||
1f5e3507ba | |||
a9a6f6f4d4 | |||
2d009273a1 | |||
2b936758ff | |||
509009d134 | |||
0aaf5db5ad | |||
89d962e33a | |||
712337c485 | |||
761e3f0483 | |||
98abfc2af0 | |||
677551fba5 | |||
7daa1c5336 | |||
79ca453f66 | |||
0521733e20 | |||
396c5f3452 | |||
9e03bfc8b6 | |||
2b67ff6f49 | |||
daa29212d0 | |||
83a5b032f8 | |||
345ef6c9f3 | |||
e1682a2fe7 | |||
2cefd9441c | |||
da90bcc164 | |||
4689c11cc8 | |||
692cf2fc08 | |||
b0132c866e | |||
14792ef954 | |||
bb8ff09e3f | |||
dfeabc39aa | |||
14148e4d80 | |||
80774c48fe | |||
862b36b37b | |||
4638656aaf | |||
3381041a8f | |||
f09ed66826 | |||
ba5906c7a3 | |||
05183a3949 | |||
d8580a9393 | |||
50285aa33b | |||
ee63a84c0e | |||
76a243d894 | |||
6bad3f2b96 | |||
51948ce081 | |||
b40a68f581 | |||
55b7429d4e | |||
7b805f2495 | |||
2445034e43 | |||
3c531f3b7b | |||
11cc0c87f3 | |||
bad7f79ed1 | |||
78272f6bc3 | |||
396d5015d2 | |||
1bf14ddefa | |||
c0dd3b529e | |||
8a8f56decb | |||
b9c77827bd | |||
cd02211b21 | |||
a50c2a1233 | |||
07e8a0f4cf | |||
6d23c76e84 | |||
11da8cb162 | |||
1063a68a21 | |||
286b5e9ae1 | |||
d4aadbea38 | |||
dfb71cc759 | |||
872d562d6a | |||
0729754659 | |||
f3fe4ef3b8 | |||
c76725eac3 | |||
9a8d2c29cb | |||
b54b8191c4 | |||
fdf6b71a91 | |||
802a07ebfe | |||
b5ba5b63fe | |||
b4aca026d4 | |||
6d941b7b16 | |||
a982312d2c | |||
6b6e19effd | |||
38f009f2ca | |||
6236c341dd | |||
7110307896 | |||
ab576bf18f | |||
302538b8ff | |||
7f886e44e1 | |||
8d0e6de040 | |||
0b9d7fde22 | |||
48f4046e79 | |||
2cd08dee70 | |||
311b8afc83 | |||
21e3943b5e | |||
10fd7102ca | |||
7de4f94423 | |||
0a546f1467 | |||
af6087ecec | |||
c613002307 | |||
76398df125 | |||
e83408ce83 | |||
715bb9341a | |||
e82bf2ecdf | |||
528527a7ed | |||
71aaa7a598 | |||
9070a76b18 | |||
5048a93ff7 | |||
282b856e24 | |||
3bb1e5fffc | |||
5ccfd0fc74 | |||
70d6bb1eb3 | |||
294b4b0e27 | |||
57980293e6 | |||
ba97e8a7f0 | |||
36b025ccb4 | |||
aab5ff3c90 | |||
8bf6e65a95 | |||
5da602b162 | |||
7a0351e123 | |||
3dc0fe5e2b | |||
82da40c9f0 | |||
8d5b28f823 | |||
68fd6cad70 | |||
c3309d4e5d | |||
06d7cbef47 | |||
e8b1abb265 |
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.rscons*
|
||||
/i686-elf-gcc/
|
||||
/build/
|
74
Functions.c
74
Functions.c
@ -1,74 +0,0 @@
|
||||
//Functions.c
|
||||
//05/07/03 Josh Holtrop
|
||||
//for HOS
|
||||
|
||||
|
||||
inline void outportb(unsigned int port, unsigned char value) // Output a byte to a port
|
||||
{
|
||||
asm volatile ("outb %%al,%%dx"::"d" (port), "a" (value));
|
||||
};
|
||||
|
||||
inline void outportw(unsigned int port, unsigned int value) // Output a word to a port
|
||||
{
|
||||
asm volatile ("outw %%ax,%%dx"::"d" (port), "a" (value));
|
||||
};
|
||||
|
||||
inline byte inportb(unsigned short port)
|
||||
{
|
||||
unsigned char ret_val;
|
||||
|
||||
asm volatile("inb %w1,%b0"
|
||||
: "=a"(ret_val)
|
||||
: "d"(port));
|
||||
return ret_val;
|
||||
};
|
||||
|
||||
void enable_ints()
|
||||
{
|
||||
asm("sti");
|
||||
};
|
||||
|
||||
void disable_ints()
|
||||
{
|
||||
asm("cli");
|
||||
};
|
||||
|
||||
void remap_pics(int pic1, int pic2)
|
||||
{
|
||||
byte a1, a2;
|
||||
|
||||
a1 = inportb(PIC1_DATA);
|
||||
a2 = inportb(PIC2_DATA);
|
||||
|
||||
outportb(PIC1_COMMAND, ICW1_INIT+ICW1_ICW4);
|
||||
outportb(PIC2_COMMAND, ICW1_INIT+ICW1_ICW4);
|
||||
outportb(PIC1_DATA, pic1);
|
||||
outportb(PIC2_DATA, pic2);
|
||||
outportb(PIC1_DATA, 4);
|
||||
outportb(PIC2_DATA, 2);
|
||||
outportb(PIC1_DATA, ICW4_8086);
|
||||
outportb(PIC2_DATA, ICW4_8086);
|
||||
|
||||
outportb(PIC1_DATA, a1);
|
||||
outportb(PIC2_DATA, a2);
|
||||
};
|
||||
|
||||
inline void restart()
|
||||
{
|
||||
outportb (0x64, 0xfe);
|
||||
for (;;)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
inline void eoi()
|
||||
{
|
||||
outportb(0x20, 0x20); //EOI
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
166
Rsconscript
Normal file
166
Rsconscript
Normal file
@ -0,0 +1,166 @@
|
||||
path_prepend "i686-elf-gcc/bin"
|
||||
|
||||
configure do
|
||||
rscons "i686-elf-gcc.rb", "-b", "#{build_dir}/i686-elf-gcc"
|
||||
check_c_compiler "i686-elf-gcc"
|
||||
check_program "genext2fs"
|
||||
check_program "grub-mkstandalone"
|
||||
check_program "mformat", on_fail: "Install the mtools package"
|
||||
check_program "xorriso"
|
||||
check_cfg package: "freetype2", on_fail: "Install libfreetype-dev", use: "freetype"
|
||||
end
|
||||
|
||||
require "tmpdir"
|
||||
|
||||
# EFI (w/ GRUB) partition size (MiB)
|
||||
EFI_PART_SIZE = 8
|
||||
# HOS partition size (MiB)
|
||||
HOS_PART_SIZE = 4
|
||||
# Kernel default font size
|
||||
KFONT_SIZE = 15
|
||||
|
||||
class BiosImage < Builder
|
||||
def run(options)
|
||||
unless @cache.up_to_date?(@target, nil, @sources, @env)
|
||||
print_run_message("Generating BIOS boot image #{@target}", nil)
|
||||
Dir.mktmpdir do |tmpdir|
|
||||
# Create iso directory.
|
||||
FileUtils.mkdir_p("#{tmpdir}/iso/boot/grub")
|
||||
File.open("#{tmpdir}/iso/boot/grub/grub.cfg", "wb") do |fh|
|
||||
fh.write(<<EOF)
|
||||
set default="0"
|
||||
set timeout=1
|
||||
menuentry "HOS" {
|
||||
insmod multiboot2
|
||||
multiboot2 /hos.elf
|
||||
}
|
||||
EOF
|
||||
end
|
||||
@sources.each do |source|
|
||||
FileUtils.cp(source, "#{tmpdir}/iso")
|
||||
end
|
||||
# Build bootable GRUB image.
|
||||
system(*%W[grub-mkrescue -o #{@target} #{tmpdir}/iso], err: "#{@env.build_root}/grub-mkrescue.log")
|
||||
end
|
||||
@cache.register_build(@target, nil, @sources, @env)
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
class EfiImage < Builder
|
||||
def run(options)
|
||||
unless @cache.up_to_date?(@target, nil, @sources, @env)
|
||||
print_run_message("Generating EFI boot image #{@target}", nil)
|
||||
Dir.mktmpdir do |tmpdir|
|
||||
# Build a standalone GRUB.
|
||||
File.open("#{tmpdir}/grub.cfg", "wb") do |fh|
|
||||
fh.write(<<EOF)
|
||||
insmod part_gpt
|
||||
configfile (hd0,gpt2)/grub.cfg
|
||||
EOF
|
||||
end
|
||||
system(*%W[grub-mkstandalone -O x86_64-efi -o #{tmpdir}/BOOTX64.EFI boot/grub/grub.cfg=#{tmpdir}/grub.cfg])
|
||||
# Create EFI partition.
|
||||
system(*%W[dd if=/dev/zero of=#{tmpdir}/efi.part bs=1M count=#{EFI_PART_SIZE}], err: "/dev/null")
|
||||
system(*%W[mformat -i #{tmpdir}/efi.part ::])
|
||||
system(*%W[mmd -i #{tmpdir}/efi.part ::/EFI])
|
||||
system(*%W[mmd -i #{tmpdir}/efi.part ::/EFI/BOOT])
|
||||
system(*%W[mcopy -i #{tmpdir}/efi.part #{tmpdir}/BOOTX64.EFI ::/EFI/BOOT])
|
||||
# Create ext2 HOS partition.
|
||||
FileUtils.mkdir_p("#{tmpdir}/ext2")
|
||||
@sources.each do |source|
|
||||
FileUtils.cp(source, "#{tmpdir}/ext2")
|
||||
end
|
||||
File.open("#{tmpdir}/ext2/grub.cfg", "wb") do |fh|
|
||||
fh.write(<<EOF)
|
||||
set default="0"
|
||||
set timeout=1
|
||||
menuentry "HOS" {
|
||||
insmod part_gpt
|
||||
insmod multiboot2
|
||||
set root=(hd0,gpt2)
|
||||
multiboot2 /hos.elf
|
||||
}
|
||||
EOF
|
||||
end
|
||||
system(*%W[genext2fs -b #{HOS_PART_SIZE * 1024} -d #{tmpdir}/ext2 #{tmpdir}/ext2.part])
|
||||
# Create full disk image.
|
||||
system(*%W[dd if=/dev/zero of=#{@target} bs=1M count=#{EFI_PART_SIZE + HOS_PART_SIZE + 2}], err: "/dev/null")
|
||||
system(*%W[parted -s #{@target} mklabel gpt])
|
||||
system(*%W[parted -s #{@target} mkpart efi 1MiB #{EFI_PART_SIZE + 1}MiB])
|
||||
system(*%W[parted -s #{@target} mkpart hos #{EFI_PART_SIZE + 1}MiB #{EFI_PART_SIZE + HOS_PART_SIZE + 1}MiB])
|
||||
system(*%W[dd if=#{tmpdir}/efi.part of=#{@target} bs=1M seek=1 conv=notrunc], err: "/dev/null")
|
||||
system(*%W[dd if=#{tmpdir}/ext2.part of=#{@target} bs=1M seek=#{1 + EFI_PART_SIZE} conv=notrunc], err: "/dev/null")
|
||||
end
|
||||
@cache.register_build(@target, nil, @sources, @env)
|
||||
end
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
class FontGen < Builder
|
||||
def run(options)
|
||||
if @command
|
||||
finalize_command
|
||||
else
|
||||
fontgen = @vars["fontgen"]
|
||||
@sources += [fontgen]
|
||||
command = %W[#{fontgen} #{@sources.first} #{KFONT_SIZE} #{@target}]
|
||||
standard_command("FontGen <target>#{@target}<reset>", command, {})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class Size < Builder
|
||||
def run(options)
|
||||
if @command
|
||||
finalize_command
|
||||
else
|
||||
@vars["_SOURCES"] = @sources
|
||||
@vars["_TARGET"] = @target
|
||||
command = @env.build_command(%w[${SIZE} ${_SOURCES}], @vars)
|
||||
standard_command("Size <target>#{@target}<reset>", command, stdout: @target)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# FontGen Environment
|
||||
fontgen_env = env "fontgen", use: "freetype" do |env|
|
||||
env["CC"] = "gcc"
|
||||
env.Program("^/fontgen.bin", glob("fontgen/**/*.c"))
|
||||
end
|
||||
|
||||
# Kernel Environment
|
||||
kernel_env = env "kernel" do |env|
|
||||
env.add_builder(EfiImage)
|
||||
env.add_builder(BiosImage)
|
||||
env.add_builder(FontGen)
|
||||
env.add_builder(Size)
|
||||
env["OBJDUMP"] = "i686-elf-objdump"
|
||||
env["SIZE"] = "i686-elf-size"
|
||||
env["CCFLAGS"] += %w[-ffreestanding -Wall -O2]
|
||||
env["LDFLAGS"] += %w[-ffreestanding -nostdlib -T src/link.ld]
|
||||
env["LDFLAGS"] += %W[-Wl,-Map,${_TARGET}.map]
|
||||
env["LIBS"] += %w[gcc]
|
||||
env.FontGen("^/kfont/kfont.c", "font/Hack-Regular.ttf",
|
||||
"fontgen" => fontgen_env.expand("^/fontgen.bin"))
|
||||
env.barrier
|
||||
env["CPPPATH"] += ["#{env.build_root}/kfont"]
|
||||
env.Program("^/hos.elf", glob("src/**/*.{S,c}") + ["^/kfont/kfont.c"])
|
||||
env.depends("#{env.build_root}/hos.elf", "src/link.ld")
|
||||
env.Disassemble("^/hos.elf.txt", "^/hos.elf")
|
||||
env.Size("^/hos.elf.size", "^/hos.elf")
|
||||
env.EfiImage("^/hos-efi.img", %w[^/hos.elf])
|
||||
env.BiosImage("^/hos.img", %w[^/hos.elf])
|
||||
end
|
||||
|
||||
task "run", desc: "Run HOS in QEMU" do
|
||||
img = kernel_env.expand("^/hos.img")
|
||||
sh %W[qemu-system-x86_64 -hda #{img}]
|
||||
end
|
||||
|
||||
task "run-efi", desc: "Run HOS EFI in QEMU" do
|
||||
img = kernel_env.expand("^/hos-efi.img")
|
||||
sh %W[qemu-system-x86_64 -bios OVMF.fd -hda #{img}]
|
||||
end
|
26
bootdef.inc
26
bootdef.inc
@ -1,26 +0,0 @@
|
||||
|
||||
%define VERSION "0.1.2" ;HOS version
|
||||
|
||||
%define BOOT_FAT_SEG 0x07E0 ;right after boot sector
|
||||
%define BOOT_ROOT_SEG 0x0900 ;right after FAT
|
||||
%define BOOT_KERNEL_SEG 0x0AC0 ;right after ROOT_DIR
|
||||
%define BOOT_STAGE2_SEG 0x0B00 ;right after KERNEL_SEG
|
||||
%define BOOT_STAGE2_ADD 0xB000 ;address of stage2 to jump to, org at
|
||||
%define BOOT_KERNEL_ADD 0x100000 ;final pmode kernel destination - physical
|
||||
%define BOOT_RD_ADD 0x200000 ;2mb for ram disk
|
||||
|
||||
%define BOOT_DATA_SEG 0x9000 ;data gathered by stage2 loader goes here
|
||||
|
||||
%define BOOT_HASRD 0x0000 ;1
|
||||
%define BOOT_VESA 0x0002 ;2 - 0 for console, otherwise VESA mode
|
||||
%define BOOT_VESA_OEM 0x0004 ;258 - null-terminated OEM identification string
|
||||
%define BOOT_VESA_VBE 0x0106 ;512 - copy of VESA VBEInfoBlock
|
||||
%define BOOT_VESA_INFO 0x0306 ;256 - copy of VESA ModeInfoBlock for selected mode
|
||||
%define BOOT_MEMENTRIES 0x040A ;4 - dword = number of memmap entries
|
||||
%define BOOT_MEMMAP 0x2000 ;? - memory map information
|
||||
|
||||
|
||||
|
||||
%define BOOT_DRIVE 0x7C24 ;1 - boot drive
|
||||
|
||||
|
@ -1,7 +0,0 @@
|
||||
|
||||
typedef unsigned int dword;
|
||||
typedef unsigned short word;
|
||||
typedef unsigned char byte;
|
||||
|
||||
|
||||
|
BIN
font/Hack-Regular.ttf
Normal file
BIN
font/Hack-Regular.ttf
Normal file
Binary file not shown.
224
fontgen/fontgen.c
Normal file
224
fontgen/fontgen.c
Normal file
@ -0,0 +1,224 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <ft2build.h>
|
||||
#include <stdio.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#define N_CHARS 128
|
||||
|
||||
#define round_up_26_6(val) (((val) + 63) >> 6u)
|
||||
|
||||
int max_advance;
|
||||
int max_top = -9999;
|
||||
int min_bottom = 9999;
|
||||
int line_height;
|
||||
int baseline_offset;
|
||||
|
||||
typedef struct {
|
||||
int width;
|
||||
int height;
|
||||
int top;
|
||||
int left;
|
||||
uint8_t * bitmap;
|
||||
} char_info_t;
|
||||
|
||||
static char_info_t char_infos[N_CHARS];
|
||||
|
||||
static void load_char(FT_Face face, int char_code)
|
||||
{
|
||||
if (FT_Load_Char(face, char_code, FT_LOAD_RENDER) != 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
int advance = round_up_26_6(face->glyph->advance.x);
|
||||
if (advance > max_advance)
|
||||
{
|
||||
max_advance = advance;
|
||||
}
|
||||
if ((face->glyph->bitmap.width == 0) ||
|
||||
(face->glyph->bitmap.rows == 0))
|
||||
{
|
||||
return;
|
||||
}
|
||||
char_infos[char_code].width = face->glyph->bitmap.width;
|
||||
char_infos[char_code].height = face->glyph->bitmap.rows;
|
||||
char_infos[char_code].top = face->glyph->bitmap_top;
|
||||
if (char_infos[char_code].top > max_top)
|
||||
{
|
||||
max_top = char_infos[char_code].top;
|
||||
}
|
||||
int bottom = char_infos[char_code].top - char_infos[char_code].height;
|
||||
if (bottom < min_bottom)
|
||||
{
|
||||
min_bottom = bottom;
|
||||
}
|
||||
char_infos[char_code].left = face->glyph->bitmap_left;
|
||||
char_infos[char_code].bitmap = malloc(char_infos[char_code].width * char_infos[char_code].height);
|
||||
memcpy(char_infos[char_code].bitmap,
|
||||
face->glyph->bitmap.buffer,
|
||||
char_infos[char_code].width * char_infos[char_code].height);
|
||||
}
|
||||
|
||||
static const char * bare_header_name(const char * h_file_name)
|
||||
{
|
||||
const char * p = rindex(h_file_name, '/');
|
||||
if (p == NULL)
|
||||
{
|
||||
p = h_file_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
p++;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static char * include_guard_name(const char * h_file_name)
|
||||
{
|
||||
const char * p = bare_header_name(h_file_name);
|
||||
char * guard_name = malloc(strlen(p) + 1);
|
||||
strcpy(guard_name, p);
|
||||
char * m = guard_name;
|
||||
while (*m != '\0')
|
||||
{
|
||||
if ('a' <= *m && *m <= 'z')
|
||||
{
|
||||
*m = toupper(*m);
|
||||
}
|
||||
else if (('0' <= *m && *m <= '9') || ('A' <= *m && *m <= 'Z'))
|
||||
{
|
||||
/* no change */
|
||||
}
|
||||
else
|
||||
{
|
||||
*m = '_';
|
||||
}
|
||||
m++;
|
||||
}
|
||||
return guard_name;
|
||||
}
|
||||
|
||||
static void generate_bytes(FILE * file, const uint8_t * bytes, int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (i % 8 == 0)
|
||||
{
|
||||
fprintf(file, " ");
|
||||
}
|
||||
fprintf(file, "0x%02xu,", bytes[i]);
|
||||
if ((i + 1) % 8 == 0)
|
||||
{
|
||||
fprintf(file, "\n");
|
||||
}
|
||||
else if (i < (count - 1))
|
||||
{
|
||||
fprintf(file, " ");
|
||||
}
|
||||
}
|
||||
if (count % 8 != 0)
|
||||
{
|
||||
fprintf(file, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void generate(const char * c_file_name)
|
||||
{
|
||||
char * h_file_name = malloc(strlen(c_file_name) + 1);
|
||||
strcpy(h_file_name, c_file_name);
|
||||
h_file_name[strlen(h_file_name) - 1] = 'h';
|
||||
char * guard = include_guard_name(h_file_name);
|
||||
|
||||
FILE * h_file = fopen(h_file_name, "wb");
|
||||
fprintf(h_file, "#ifndef %s\n", guard);
|
||||
fprintf(h_file, "#define %s\n\n", guard);
|
||||
fprintf(h_file, "#include <stdint.h>\n");
|
||||
fprintf(h_file, "typedef struct {\n int width;\n int height;\n int top;\n int left;\n const uint8_t * bitmap;\n} fontgen_char_info_t;\n");
|
||||
fprintf(h_file, "typedef struct {\n int line_height;\n int advance;\n int baseline_offset;\n const fontgen_char_info_t ** char_infos;\n} fontgen_font_t;\n");
|
||||
fprintf(h_file, "extern const fontgen_font_t kfont;\n");
|
||||
fprintf(h_file, "#endif\n");
|
||||
fclose(h_file);
|
||||
|
||||
FILE * c_file = fopen(c_file_name, "wb");
|
||||
fprintf(c_file, "#include \"%s\"\n", bare_header_name(h_file_name));
|
||||
fprintf(c_file, "#include <stddef.h>\n");
|
||||
for (int i = 0; i < N_CHARS; i++)
|
||||
{
|
||||
if (char_infos[i].width > 0)
|
||||
{
|
||||
fprintf(c_file, "static const uint8_t char_bitmap_%d[] = {\n", i);
|
||||
generate_bytes(c_file, char_infos[i].bitmap, char_infos[i].width * char_infos[i].height);
|
||||
fprintf(c_file, "};\n");
|
||||
}
|
||||
fprintf(c_file, "static const fontgen_char_info_t char_%d = {\n", i);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].width);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].height);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].top);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].left);
|
||||
if (char_infos[i].width > 0)
|
||||
{
|
||||
fprintf(c_file, " char_bitmap_%d,\n", i);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(c_file, " NULL,\n");
|
||||
}
|
||||
fprintf(c_file, "};\n\n");
|
||||
}
|
||||
fprintf(c_file, "const fontgen_char_info_t * char_infos[] = {\n");
|
||||
for (int i = 0; i < N_CHARS; i++)
|
||||
{
|
||||
fprintf(c_file, " &char_%d,\n", i);
|
||||
}
|
||||
fprintf(c_file, "};\n");
|
||||
fprintf(c_file, "const fontgen_font_t kfont = {\n");
|
||||
fprintf(c_file, " %d,\n", line_height);
|
||||
fprintf(c_file, " %d,\n", max_advance);
|
||||
fprintf(c_file, " %d,\n", baseline_offset);
|
||||
fprintf(c_file, " char_infos,\n");
|
||||
fprintf(c_file, "};\n");
|
||||
fclose(c_file);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
/* Expect: font file, size, out file */
|
||||
if (argc != 4)
|
||||
{
|
||||
fprintf(stderr, "Incorrect arguments\n");
|
||||
return 1;
|
||||
}
|
||||
const char * font_file = argv[1];
|
||||
int size = atoi(argv[2]);
|
||||
const char * out_file = argv[3];
|
||||
|
||||
FT_Library ft_library;
|
||||
if (FT_Init_FreeType(&ft_library) != 0)
|
||||
{
|
||||
fprintf(stderr, "Could not initialize freetype\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
FT_Face face;
|
||||
if (FT_New_Face(ft_library, font_file, 0, &face) != 0)
|
||||
{
|
||||
fprintf(stderr, "Could not open %s\n", font_file);
|
||||
return 3;
|
||||
}
|
||||
|
||||
FT_Set_Pixel_Sizes(face, 0, size);
|
||||
|
||||
for (int i = 0; i < N_CHARS; i++)
|
||||
{
|
||||
load_char(face, i);
|
||||
}
|
||||
line_height = round_up_26_6(face->size->metrics.height);
|
||||
baseline_offset = (line_height - (max_top - min_bottom)) / 2 - min_bottom;
|
||||
|
||||
generate(out_file);
|
||||
|
||||
return 0;
|
||||
}
|
21
functions.h
21
functions.h
@ -1,21 +0,0 @@
|
||||
//functions.h
|
||||
//05/07/03 Josh Holtrop
|
||||
//for HOS
|
||||
|
||||
|
||||
|
||||
inline void outportb(unsigned int port, unsigned char value);
|
||||
inline void outportw(unsigned int port, unsigned int value);
|
||||
inline byte inportb(unsigned short port);
|
||||
void enable_ints();
|
||||
void disable_ints();
|
||||
inline void restart();
|
||||
void remap_pics(int pic1, int pic2);
|
||||
inline void eoi();
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
68
gdt.inc
68
gdt.inc
@ -1,68 +0,0 @@
|
||||
|
||||
|
||||
gdtr:
|
||||
dw gdt_end-gdt-1
|
||||
dd GDT
|
||||
gdt:
|
||||
dd 0
|
||||
dd 0
|
||||
KERNEL_CODE equ $-gdt
|
||||
dw 0xffff ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x00 ;base 23:16
|
||||
db 0x9A ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
KERNEL_DATA equ $-gdt
|
||||
dw 0xffff ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x00 ;base 23:16
|
||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
VESA_CODE equ $-gdt
|
||||
dw 0xffff ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x00 ;base 23:16
|
||||
db 0x9A ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
VESA_DATA equ $-gdt
|
||||
dw 0xffff ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x00 ;base 23:16
|
||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
VIDEO_TEXT equ $-gdt
|
||||
dw 0x7FFF ;limit 15:0
|
||||
dw 0x8000 ;base 15:0
|
||||
db 0x0B ;base 23:16
|
||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
VIDEO_GRAPHICS equ $-gdt
|
||||
dw 0xFFFF ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x0A ;base 23:16
|
||||
db 0x92 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0x40 ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
USER_CODE equ $-gdt
|
||||
dw 0xffff ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x00 ;base 23:16
|
||||
db 0xFA ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
USER_DATA equ $-gdt
|
||||
dw 0xffff ;limit 15:0
|
||||
dw 0x0000 ;base 15:0
|
||||
db 0x00 ;base 23:16
|
||||
db 0xF2 ;access ([P][DPL][1][Executable][Direction/Conforming][Writable/Readable][A])
|
||||
db 0xCF ;flags ([G][D/B][0][0]) / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
gdt_end:
|
||||
|
||||
|
||||
|
75
i686-elf-gcc.rb
Normal file
75
i686-elf-gcc.rb
Normal file
@ -0,0 +1,75 @@
|
||||
binutils_version = "2.35"
|
||||
binutils_checksum = "1b11659fb49e20e18db460d44485f09442c8c56d5df165de9461eb09c8302f85"
|
||||
gcc_version = "10.2.0"
|
||||
gcc_checksum = "b8dd4368bb9c7f0b98188317ee0254dd8cc99d1e3a18d0ff146c855fe16c1d8c"
|
||||
install_path = File.expand_path("i686-elf-gcc")
|
||||
target = "i686-elf"
|
||||
path_prepend "#{install_path}/bin"
|
||||
|
||||
configure do
|
||||
check_c_compiler "gcc"
|
||||
check_program "make"
|
||||
check_program "bison"
|
||||
check_program "flex"
|
||||
check_program "texi2any", on_fail: "Install the texinfo package"
|
||||
check_program "wget"
|
||||
check_lib "gmp", on_fail: "Install the libgmp-dev package"
|
||||
check_lib "mpc", on_fail: "Install the libmpc-dev package"
|
||||
check_lib "mpfr", on_fail: "Install the libmpfr-dev package"
|
||||
end
|
||||
|
||||
default do
|
||||
unless Dir.exist?(install_path)
|
||||
# Download archives.
|
||||
download "https://ftp.gnu.org/gnu/binutils/binutils-#{binutils_version}.tar.xz",
|
||||
"#{build_dir}/binutils-#{binutils_version}.tar.xz",
|
||||
sha256sum: binutils_checksum
|
||||
|
||||
download "https://ftp.gnu.org/gnu/gcc/gcc-#{gcc_version}/gcc-#{gcc_version}.tar.xz",
|
||||
"#{build_dir}/gcc-#{gcc_version}.tar.xz",
|
||||
sha256sum: gcc_checksum
|
||||
|
||||
# Extract archives.
|
||||
sh "tar", "xJf", "binutils-#{binutils_version}.tar.xz",
|
||||
chdir: build_dir
|
||||
|
||||
sh "tar", "xJf", "gcc-#{gcc_version}.tar.xz",
|
||||
chdir: build_dir
|
||||
|
||||
# Build binutils.
|
||||
rm_rf "#{build_dir}/build-binutils"
|
||||
mkdir_p "#{build_dir}/build-binutils"
|
||||
cd "#{build_dir}/build-binutils" do
|
||||
sh %W[../binutils-#{binutils_version}/configure
|
||||
--target=#{target} --prefix=#{install_path} --with-sysroot --disable-nls
|
||||
--disable-werror]
|
||||
sh "make"
|
||||
sh "make install"
|
||||
end
|
||||
|
||||
# Build gcc.
|
||||
rm_rf "#{build_dir}/build-gcc"
|
||||
mkdir_p "#{build_dir}/build-gcc"
|
||||
cd "#{build_dir}/build-gcc" do
|
||||
sh %W[../gcc-#{gcc_version}/configure
|
||||
--target=#{target} --prefix=#{install_path} --disable-nls
|
||||
--enable-languages=c,c++ --without-headers]
|
||||
sh "make all-gcc"
|
||||
sh "make all-target-libgcc"
|
||||
sh "make install-gcc"
|
||||
sh "make install-target-libgcc"
|
||||
end
|
||||
|
||||
# Remove archives and build directories if everything succeeded.
|
||||
rm_f "#{build_dir}/binutils-#{binutils_version}.tar.xz"
|
||||
rm_rf "#{build_dir}/binutils-#{binutils_version}"
|
||||
rm_rf "#{build_dir}/build-binutils"
|
||||
rm_f "#{build_dir}/gcc-#{gcc_version}.tar.xz"
|
||||
rm_rf "#{build_dir}/gcc-#{gcc_version}"
|
||||
rm_rf "#{build_dir}/build-gcc"
|
||||
end
|
||||
end
|
||||
|
||||
distclean do
|
||||
rm_rf install_path
|
||||
end
|
86
idt.inc
86
idt.inc
@ -1,86 +0,0 @@
|
||||
|
||||
idtr:
|
||||
dw 50*8-1 ;size of idt
|
||||
dd IDT ;address of idt
|
||||
|
||||
|
||||
%macro isr_label 1
|
||||
isr_%1:
|
||||
mov eax, %1
|
||||
jmp isr_main
|
||||
%endmacro
|
||||
|
||||
isr_label 0
|
||||
isr_label 1
|
||||
isr_label 2
|
||||
isr_label 3
|
||||
isr_label 4
|
||||
isr_label 5
|
||||
isr_label 6
|
||||
isr_label 7
|
||||
isr_label 8
|
||||
isr_label 9
|
||||
isr_label 10
|
||||
isr_label 11
|
||||
isr_label 12
|
||||
isr_label 13
|
||||
isr_label 14
|
||||
isr_label 15
|
||||
isr_label 16
|
||||
isr_label 17
|
||||
isr_label 18
|
||||
isr_label 19
|
||||
isr_label 20
|
||||
isr_label 21
|
||||
isr_label 22
|
||||
isr_label 23
|
||||
isr_label 24
|
||||
isr_label 25
|
||||
isr_label 26
|
||||
isr_label 27
|
||||
isr_label 28
|
||||
isr_label 29
|
||||
isr_label 30
|
||||
isr_label 31
|
||||
isr_label 32
|
||||
isr_label 33
|
||||
isr_label 34
|
||||
isr_label 35
|
||||
isr_label 36
|
||||
isr_label 37
|
||||
isr_label 38
|
||||
isr_label 39
|
||||
isr_label 40
|
||||
isr_label 41
|
||||
isr_label 42
|
||||
isr_label 43
|
||||
isr_label 44
|
||||
isr_label 45
|
||||
isr_label 46
|
||||
isr_label 47
|
||||
isr_label 48
|
||||
isr_label 49
|
||||
|
||||
isr_main:
|
||||
pusha
|
||||
push ds
|
||||
push es
|
||||
|
||||
push eax
|
||||
|
||||
call _isr
|
||||
|
||||
pop eax
|
||||
|
||||
pop es
|
||||
pop ds
|
||||
popa
|
||||
|
||||
iret
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
530
io.asm
530
io.asm
@ -1,530 +0,0 @@
|
||||
|
||||
%macro jzfar 1
|
||||
jnz %%skip
|
||||
jmp %1
|
||||
%%skip:
|
||||
|
||||
%endmacro
|
||||
|
||||
[global _writeCursorPosition]
|
||||
[global _getCursorPosition]
|
||||
[global _putc]
|
||||
[global _puts]
|
||||
[global _printf]
|
||||
[global _console_scroll]
|
||||
[global _console_cls]
|
||||
[global _putHex]
|
||||
[global _putDec]
|
||||
[global _putDecu]
|
||||
|
||||
|
||||
;
|
||||
;void writeCursorPosition(word pos)
|
||||
;
|
||||
_writeCursorPosition:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
|
||||
push ebx
|
||||
push edx
|
||||
|
||||
mov eax, [ebp+8] ;cursor position in ax
|
||||
|
||||
mov bl, al
|
||||
mov dx, 0x03D4
|
||||
mov al, 0x0E
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
mov al, ah
|
||||
out dx, al
|
||||
|
||||
dec dx
|
||||
mov al, 0x0F
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
mov al, bl
|
||||
out dx, al
|
||||
|
||||
pop edx
|
||||
pop ebx
|
||||
pop ebp
|
||||
|
||||
ret
|
||||
|
||||
|
||||
;
|
||||
;word getCursorPosition()
|
||||
;
|
||||
_getCursorPosition:
|
||||
push ebx
|
||||
push edx
|
||||
|
||||
xor eax, eax
|
||||
mov dx, 0x03D4
|
||||
mov al, 0x0E
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
in al, dx
|
||||
mov bl, al
|
||||
|
||||
dec dx
|
||||
mov al, 0x0F
|
||||
out dx, al
|
||||
|
||||
inc dx
|
||||
in al, dx
|
||||
mov ah, bl
|
||||
|
||||
pop edx
|
||||
pop ebx
|
||||
|
||||
ret
|
||||
|
||||
|
||||
;
|
||||
;int putc(int chr)
|
||||
;
|
||||
_putc:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push ebx
|
||||
push ecx
|
||||
push edx
|
||||
|
||||
call _getCursorPosition
|
||||
mov ebx, eax
|
||||
mov ecx, ebx
|
||||
|
||||
mov eax, [ebp+8] ;al=character
|
||||
cmp al, 10 ;newline
|
||||
jz putc_newline
|
||||
cmp al, 9 ;tab
|
||||
jz putc_tab
|
||||
|
||||
shl ebx, 1
|
||||
add ebx, 0xb8000
|
||||
mov ah, 0x07
|
||||
mov [ebx], ax
|
||||
mov eax, ecx
|
||||
inc eax
|
||||
cmp eax, 2000
|
||||
jnz putc_writeit2
|
||||
call _console_scroll
|
||||
mov eax, 2000-80
|
||||
putc_writeit2:
|
||||
push eax
|
||||
call _writeCursorPosition
|
||||
add esp, 4
|
||||
jmp putc_done
|
||||
|
||||
putc_newline:
|
||||
mov eax, ebx ;eax = cursor position
|
||||
mov ebx, 80
|
||||
xor edx, edx
|
||||
div bx ;ax=dx:ax/bx, dx=remainder
|
||||
mov bx, 80
|
||||
sub bx, dx
|
||||
mov eax, ecx
|
||||
add eax, ebx ;eax = new cursor position
|
||||
cmp eax, 2000
|
||||
jnz putc_newline_writeit2
|
||||
call _console_scroll
|
||||
mov eax, 2000-80 ;beginning of last row
|
||||
putc_newline_writeit2:
|
||||
push eax
|
||||
call _writeCursorPosition
|
||||
add esp, 4
|
||||
jmp putc_done
|
||||
|
||||
putc_tab:
|
||||
mov eax, ebx ;eax = cursor position
|
||||
mov ebx, 8
|
||||
div bl ;al=ax/bl, ah=remainder
|
||||
xor edx, edx
|
||||
mov dl, ah
|
||||
mov bx, 8
|
||||
sub bx, dx
|
||||
mov eax, ecx
|
||||
add eax, ebx ;eax = new cursor position
|
||||
cmp eax, 2000
|
||||
jnz putc_tab_writeit2
|
||||
call _console_scroll
|
||||
mov eax, 2000-80 ;beginning of last row
|
||||
putc_tab_writeit2:
|
||||
push eax
|
||||
call _writeCursorPosition
|
||||
add esp, 4
|
||||
|
||||
putc_done:
|
||||
pop edx
|
||||
pop ecx
|
||||
pop ebx
|
||||
pop ebp
|
||||
|
||||
ret
|
||||
|
||||
|
||||
|
||||
;
|
||||
;void printf(char *fmt, ... )
|
||||
;
|
||||
_printf:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
pusha
|
||||
mov ebx, [ebp+8] ;ebx = in format string
|
||||
mov esi, ebp
|
||||
add esi, 12 ;esi = to next variable arg
|
||||
xor ecx, ecx ;ecx used if we encounter a '%'
|
||||
printf_loop:
|
||||
mov al, [ebx]
|
||||
inc ebx
|
||||
cmp al, 0
|
||||
jzfar printf_done
|
||||
cmp al, '%'
|
||||
jzfar printf_percent
|
||||
cmp ecx, 1
|
||||
jz printf_special
|
||||
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 4
|
||||
jmp printf_loop
|
||||
|
||||
printf_special:
|
||||
xor ecx, ecx
|
||||
cmp al, 'd'
|
||||
jz printf_decimal
|
||||
cmp al, 'u'
|
||||
jz printf_decimalu
|
||||
cmp al, 'x'
|
||||
jz printf_hex
|
||||
cmp al, '%'
|
||||
jz printf_ppercent
|
||||
cmp al, 's'
|
||||
jz printf_string
|
||||
cmp al, 'c'
|
||||
jz printf_char
|
||||
jmp printf_special_done
|
||||
|
||||
printf_decimal:
|
||||
mov eax, [esi]
|
||||
push eax
|
||||
call _putDec
|
||||
add esp, 4
|
||||
jmp printf_special_done
|
||||
|
||||
printf_decimalu:
|
||||
mov eax, [esi]
|
||||
push eax
|
||||
call _putDecu
|
||||
add esp, 4
|
||||
jmp printf_special_done
|
||||
|
||||
printf_hex:
|
||||
mov eax, [esi]
|
||||
push eax
|
||||
call _putHex
|
||||
add esp, 4
|
||||
jmp printf_special_done
|
||||
|
||||
printf_ppercent:
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 4
|
||||
jmp printf_special_done
|
||||
|
||||
printf_string:
|
||||
mov eax, [esi]
|
||||
push eax
|
||||
call _puts
|
||||
add esp, 4
|
||||
jmp printf_special_done
|
||||
|
||||
printf_char:
|
||||
mov eax, [esi]
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 4
|
||||
jmp printf_special_done
|
||||
|
||||
printf_special_done
|
||||
add esi, 4 ;point to next extra argument
|
||||
jmp printf_loop
|
||||
|
||||
printf_percent:
|
||||
mov ecx, 1
|
||||
jmp printf_loop
|
||||
|
||||
|
||||
printf_done:
|
||||
popa
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
|
||||
;
|
||||
;void console_scroll()
|
||||
;
|
||||
_console_scroll:
|
||||
pusha
|
||||
mov esi, 0xb8000+160
|
||||
mov edi, 0xb8000
|
||||
mov ecx, 960 ;(2000-80)/2
|
||||
console_scroll_loop:
|
||||
lodsd
|
||||
stosd
|
||||
loop console_scroll_loop
|
||||
mov ax, 0x0720
|
||||
mov ecx, 80
|
||||
console_scroll_loop2:
|
||||
stosw
|
||||
loop console_scroll_loop2
|
||||
popa
|
||||
ret
|
||||
|
||||
;
|
||||
;void console_cls()
|
||||
;
|
||||
_console_cls:
|
||||
pusha
|
||||
mov edi, 0xb8000
|
||||
mov ax, 0x0720
|
||||
mov ecx, 2000
|
||||
console_cls_loop:
|
||||
stosw
|
||||
loop console_cls_loop
|
||||
push dword 0
|
||||
call _writeCursorPosition
|
||||
add esp, 4
|
||||
popa
|
||||
ret
|
||||
|
||||
;
|
||||
;int putHex(dword number)
|
||||
;
|
||||
_putHex:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
pusha
|
||||
mov eax, [ebp+8] ;eax = number to print
|
||||
xor ebx, ebx ;we have not printed a character yet
|
||||
mov ecx, 8 ;counter for number of characters
|
||||
|
||||
putHex_loop:
|
||||
push eax
|
||||
push ecx
|
||||
|
||||
dec ecx
|
||||
shl ecx, 2 ;edx=counter*4 (amount to shift by)
|
||||
shr eax, cl
|
||||
and eax, 0x0F
|
||||
cmp cl, 0
|
||||
jz putHex_notzero ;if number is 0
|
||||
cmp al, 0
|
||||
jnz putHex_notzero
|
||||
cmp bl, 0
|
||||
jz putHex_loop_end
|
||||
putHex_notzero:
|
||||
mov bl, 1
|
||||
add eax, '0'
|
||||
cmp eax, '9'
|
||||
jbe putHex_dontadjust
|
||||
add eax, 'A'-'9'-1
|
||||
putHex_dontadjust:
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 4
|
||||
|
||||
putHex_loop_end:
|
||||
pop ecx
|
||||
pop eax
|
||||
loop putHex_loop
|
||||
|
||||
popa
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
;
|
||||
;int puts(char *str)
|
||||
;
|
||||
_puts:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
push esi
|
||||
push eax
|
||||
mov esi, [ebp+8] ;esi = to string
|
||||
puts_loop:
|
||||
lodsb
|
||||
cmp al, 0
|
||||
jz puts_done
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 4
|
||||
jmp puts_loop
|
||||
|
||||
puts_done:
|
||||
pop eax
|
||||
pop esi
|
||||
pop ebp
|
||||
ret
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
_putDecu:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
sub esp, 24
|
||||
mov DWORD [ebp-4], 1
|
||||
mov BYTE [ebp-5], 0
|
||||
L2:
|
||||
mov edx, DWORD [ebp+8]
|
||||
mov eax, -858993459
|
||||
mul edx
|
||||
mov eax, edx
|
||||
shr eax, 3
|
||||
cmp eax, DWORD [ebp-4]
|
||||
jae L4
|
||||
jmp L3
|
||||
L4:
|
||||
mov eax, DWORD [ebp-4]
|
||||
mov edx, eax
|
||||
sal edx, 2
|
||||
add edx, eax
|
||||
lea eax, [edx+edx]
|
||||
mov DWORD [ebp-4], eax
|
||||
jmp L2
|
||||
L3:
|
||||
nop
|
||||
L5:
|
||||
cmp DWORD [ebp-4], 1
|
||||
ja L7
|
||||
jmp L6
|
||||
L7:
|
||||
mov edx, DWORD [ebp+8]
|
||||
mov eax, edx
|
||||
mov edx, 0
|
||||
div DWORD [ebp-4]
|
||||
mov DWORD [ebp-12], eax
|
||||
mov al, BYTE [ebp-12]
|
||||
mov BYTE [ebp-5], al
|
||||
mov eax, 0
|
||||
mov al, BYTE [ebp-5]
|
||||
imul eax, DWORD [ebp-4]
|
||||
sub DWORD [ebp+8], eax
|
||||
mov edx, DWORD [ebp-4]
|
||||
mov eax, -858993459
|
||||
mul edx
|
||||
mov eax, edx
|
||||
shr eax, 3
|
||||
mov DWORD [ebp-4], eax
|
||||
lea eax, [ebp-5]
|
||||
add BYTE [eax], 48
|
||||
sub esp, 12
|
||||
mov eax, 0
|
||||
mov al, BYTE [ebp-5]
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 16
|
||||
jmp L5
|
||||
L6:
|
||||
sub esp, 12
|
||||
mov al, BYTE [ebp+8]
|
||||
add eax, 48
|
||||
and eax, 255
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 16
|
||||
leave
|
||||
ret
|
||||
|
||||
|
||||
|
||||
|
||||
_putDec:
|
||||
push ebp
|
||||
mov ebp, esp
|
||||
sub esp, 24
|
||||
cmp DWORD [ebp+8], 0
|
||||
jns L9
|
||||
sub esp, 12
|
||||
push 45
|
||||
call _putc
|
||||
add esp, 16
|
||||
neg DWORD [ebp+8]
|
||||
L9:
|
||||
mov DWORD [ebp-4], 1
|
||||
mov BYTE [ebp-5], 0
|
||||
L10:
|
||||
mov eax, DWORD [ebp+8]
|
||||
cmp eax, DWORD [ebp-4]
|
||||
jae L12
|
||||
jmp L11
|
||||
L12:
|
||||
mov eax, DWORD [ebp-4]
|
||||
mov edx, eax
|
||||
sal edx, 2
|
||||
add edx, eax
|
||||
lea eax, [edx+edx]
|
||||
mov DWORD [ebp-4], eax
|
||||
jmp L10
|
||||
L11:
|
||||
mov edx, DWORD [ebp-4]
|
||||
mov eax, -858993459
|
||||
mul edx
|
||||
mov eax, edx
|
||||
shr eax, 3
|
||||
mov DWORD [ebp-4], eax
|
||||
L13:
|
||||
cmp DWORD [ebp-4], 1
|
||||
ja L15
|
||||
jmp L14
|
||||
L15:
|
||||
mov edx, DWORD [ebp+8]
|
||||
mov eax, edx
|
||||
mov edx, 0
|
||||
div DWORD [ebp-4]
|
||||
mov DWORD [ebp-12], eax
|
||||
mov al, BYTE [ebp-12]
|
||||
mov BYTE [ebp-5], al
|
||||
mov eax, 0
|
||||
mov al, BYTE [ebp-5]
|
||||
imul eax, DWORD [ebp-4]
|
||||
sub DWORD [ebp+8], eax
|
||||
mov edx, DWORD [ebp-4]
|
||||
mov eax, -858993459
|
||||
mul edx
|
||||
mov eax, edx
|
||||
shr eax, 3
|
||||
mov DWORD [ebp-4], eax
|
||||
lea eax, [ebp-5]
|
||||
add BYTE [eax], 48
|
||||
sub esp, 12
|
||||
mov eax, 0
|
||||
mov al, BYTE [ebp-5]
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 16
|
||||
jmp L13
|
||||
L14:
|
||||
sub esp, 12
|
||||
mov al, BYTE [ebp+8]
|
||||
add eax, 48
|
||||
and eax, 255
|
||||
push eax
|
||||
call _putc
|
||||
add esp, 16
|
||||
leave
|
||||
ret
|
||||
|
||||
|
||||
|
||||
|
18
io.h
18
io.h
@ -1,18 +0,0 @@
|
||||
|
||||
|
||||
#ifndef __HIO_H__
|
||||
#define __HIO_H__ __HIO_H__
|
||||
|
||||
void writeCursorPosition(word pos);
|
||||
word getCursorPosition();
|
||||
int putc(byte chr);
|
||||
int printf(char *fmt, ...);
|
||||
int puts(char *str);
|
||||
int putDec(int number);
|
||||
int putDecu(dword number);
|
||||
int putHex(dword number);
|
||||
void console_cls();
|
||||
void console_scroll();
|
||||
|
||||
#endif
|
||||
|
45
k_defines.h
45
k_defines.h
@ -1,45 +0,0 @@
|
||||
//k_defines.h
|
||||
//03/17/03 Josh Holtrop
|
||||
|
||||
#define PIC1 0x20
|
||||
#define PIC2 0xA0
|
||||
#define PIC1_COMMAND PIC1
|
||||
#define PIC1_DATA (PIC1+1)
|
||||
#define PIC2_COMMAND PIC2
|
||||
#define PIC2_DATA (PIC2+1)
|
||||
#define PIC_EOI 0x20
|
||||
|
||||
#define ICW1_ICW4 0x01 /* ICW4 (not) needed */
|
||||
#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
|
||||
#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
|
||||
#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
|
||||
#define ICW1_INIT 0x10 /* Initialization - required! */
|
||||
|
||||
#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
|
||||
#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
|
||||
#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
|
||||
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
|
||||
#define ICW4_SFNM 0x10 /* Special fully nested (not) */
|
||||
|
||||
#define BLUE_TXT 0x01
|
||||
#define RED_TXT 0x04
|
||||
#define WHITE_TXT 0x07
|
||||
#define CYAN_TXT 0x0B
|
||||
#define YELLOW_TXT 0x0E
|
||||
#define BRWHITE_TXT 0x0F
|
||||
|
||||
#define FREERAM_START 0x268000
|
||||
|
||||
typedef unsigned char byte;
|
||||
typedef unsigned short word;
|
||||
typedef unsigned int dword;
|
||||
|
||||
typedef struct {
|
||||
dword lowdword;
|
||||
dword highdword;
|
||||
} __attribute__((packed)) qword;
|
||||
|
||||
|
||||
|
||||
|
||||
|
62
kernel.asm
62
kernel.asm
@ -1,62 +0,0 @@
|
||||
|
||||
%include "bootdef.inc"
|
||||
|
||||
%define GDT 0x140000
|
||||
%define IDT 0x150000
|
||||
|
||||
[global start]
|
||||
[extern _isr]
|
||||
[extern _k_init]
|
||||
|
||||
bits 32
|
||||
|
||||
start:
|
||||
cli ;if they weren't already off
|
||||
mov edi, GDT
|
||||
mov esi, gdt
|
||||
mov ecx, gdt_end-gdt
|
||||
copy_gdt:
|
||||
lodsb
|
||||
stosb
|
||||
loop copy_gdt
|
||||
|
||||
mov edi, IDT ;destination
|
||||
mov esi, isr_0 ;address of isr0
|
||||
mov edx, isr_1-isr_0 ;distance between isr labels
|
||||
mov ecx, 50 ;number of isrlabels
|
||||
fill_idt:
|
||||
mov ebx, esi
|
||||
mov ax, si
|
||||
stosw ;0 offset 15:0
|
||||
mov ax, KERNEL_CODE
|
||||
stosw ;2 selector 15:0
|
||||
mov ax, 0x8E00
|
||||
stosw ;4 [P][DPL][0][TYPE][0][0][0][0][0][0][0][0]
|
||||
shr esi, 16
|
||||
mov ax, si
|
||||
stosw ;6 offset 31:16
|
||||
mov esi, ebx
|
||||
add esi, edx
|
||||
loop fill_idt
|
||||
|
||||
lgdt [gdtr] ;load gdt
|
||||
jmp KERNEL_CODE:newgdtcontinue
|
||||
newgdtcontinue:
|
||||
mov ax, KERNEL_DATA
|
||||
mov es, ax
|
||||
mov ds, ax
|
||||
mov gs, ax
|
||||
mov fs, ax
|
||||
mov ss, ax
|
||||
mov esp, 0x1ffffc ;stack just under 2mb, moves downward
|
||||
lidt [idtr] ;load idt
|
||||
|
||||
call _k_init
|
||||
hlt ;halt processor when k_init is done
|
||||
jmp $ ;shouldn't get here...
|
||||
|
||||
%include "gdt.inc"
|
||||
%include "idt.inc"
|
||||
|
||||
|
||||
|
96
kernel.c
96
kernel.c
@ -1,96 +0,0 @@
|
||||
//kernel.c
|
||||
//08/13/03 Josh Holtrop
|
||||
//Holtrop's Operating System
|
||||
|
||||
#define VXR video_mode.XResolution
|
||||
#define VYR video_mode.YResolution
|
||||
|
||||
#include "k_defines.h"
|
||||
|
||||
#include "lib/string.h"
|
||||
#include "lib/io.h"
|
||||
|
||||
#include "functions.h"
|
||||
#include "video.h"
|
||||
#include "mm.h"
|
||||
#include "keyboard.h"
|
||||
|
||||
void isr(dword num);
|
||||
void k_init();
|
||||
|
||||
#include "keyboard.c"
|
||||
#include "mm.c"
|
||||
#include "functions.c"
|
||||
#include "video.c"
|
||||
|
||||
dword timer = 0;
|
||||
|
||||
void k_init()
|
||||
{
|
||||
remap_pics(0x20, 0x28);
|
||||
//set timer : 2e9c = 100hz
|
||||
outportb(0x43, 0x34);
|
||||
outportb(0x40, 0x9c); //lsb
|
||||
outportb(0x40, 0x2e); //msb
|
||||
video_init((ModeInfoBlock *) 0x90306);
|
||||
mm_init();
|
||||
enable_ints();
|
||||
console_cls();
|
||||
printf("Memory available to OS: %d MB\n (Bytes: %d)", mm_totalmem/0x100000, mm_totalmem);
|
||||
|
||||
for (;;)
|
||||
printf("%c ", kbdWaitKey());
|
||||
/* pageblock *pb = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if (pb->flags == MM_PB_NP)
|
||||
break;
|
||||
printf("\nBase: 0x%x; Limit: 0x%x; Flags: 0x%x, Link: 0x%x", pb->base, pb->length, pb->flags, pb->link);
|
||||
pb = (pageblock *) pb->link;
|
||||
} */
|
||||
}
|
||||
|
||||
void isr(dword num)
|
||||
{
|
||||
switch(num)
|
||||
{
|
||||
case 0x20:
|
||||
timer++;
|
||||
(*(byte *)0xb8000)++;
|
||||
switch(timer)
|
||||
{
|
||||
case 150:
|
||||
video_rectf(VXR*3/11, 0, VXR*4/11, VYR-1, 0x00000088);
|
||||
break;
|
||||
case 200:
|
||||
video_rectf(VXR*7/11, 0, VXR*8/11, VYR-1, 0x00000088);
|
||||
break;
|
||||
case 250:
|
||||
video_rectf(VXR/11, 0, VXR*2/11, VYR*2/5, 0x00000088);
|
||||
break;
|
||||
case 300:
|
||||
video_rectf(VXR/11, VYR*3/5, VXR*2/11, VYR-1, 0x00000088);
|
||||
break;
|
||||
case 350:
|
||||
video_rectf(VXR*5/11, VYR/5, VXR*6/11, VYR*4/5, 0x00000088);
|
||||
break;
|
||||
case 400:
|
||||
video_rectf(VXR*9/11, VYR/5, VXR-1, VYR*2/5, 0x00000088);
|
||||
break;
|
||||
case 450:
|
||||
video_rectf(VXR*8/11, VYR*3/5, VXR*10/11, VYR*4/5, 0x00000088);
|
||||
break;
|
||||
}
|
||||
eoi();
|
||||
break;
|
||||
case 0x21:
|
||||
printf("------------------KEYBOARD ISR--------------------------\n");
|
||||
isr_keyboard();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
249
keyboard.c
249
keyboard.c
@ -1,249 +0,0 @@
|
||||
//Keyboard.c
|
||||
// Created: 04/17/03 Josh Holtrop
|
||||
// Modified: 09/09/03
|
||||
//for HOS
|
||||
|
||||
#define KBD_BUFFER_LENGTH 64
|
||||
|
||||
byte kbdFlags = 0;
|
||||
byte kbdAscii = 0;
|
||||
byte kbdScan = 0;
|
||||
|
||||
dword kbdBuffer[KBD_BUFFER_LENGTH];
|
||||
byte kbdBufferStart = 0;
|
||||
byte kbdBufferLen = 0;
|
||||
byte kbdExt = 0;
|
||||
byte kbdExt2 = 0;
|
||||
byte ackReason = 0; //used to record the reason why we would get an acknowledge byte (0xFA)
|
||||
|
||||
|
||||
//nul esc bksp tab lctl lsft rsft lalt caps F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 numScrlNumPad------- unknown---- F11 F12 unknown....
|
||||
const byte SCAN2ASCII[128] = "\000\0331234567890-=\010\011qwertyuiop[]\n\001asdfghjkl;'`\001\\zxcvbnm,./\001*\001 \001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001-\001\001\001+\001\001\001\001\001\002\002\002\001\001\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002";
|
||||
const byte SCAN2ASCIISHIFT[128] = "\000\033!@#$%^&*()_+\010\011QWERTYUIOP{}\n\001ASDFGHJKL:\"~\001|ZXCVBNM<>?\001*\001 \001\001\001\001\001\001\001\001\001\001\001\001\001789-456+1230.\002\002\002\001\001\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002\002";
|
||||
|
||||
//====FUNCTIONS:
|
||||
|
||||
void isr_keyboard()
|
||||
{
|
||||
kbdScan = inportb(0x60);
|
||||
byte inState = inportb(0x61);
|
||||
outportb(0x61, inState|0x80);
|
||||
outportb(0x61, inState);
|
||||
|
||||
printf("IRQ 1: %x\n", kbdScan);
|
||||
|
||||
if (kbdScan == 0xFA) //250 //ACKnowledge
|
||||
{
|
||||
if (ackReason == 0xED) //reset LEDs
|
||||
{
|
||||
outportb(0x64, (kbdFlags & 0x07));
|
||||
printf("KBD_ACK!");
|
||||
}
|
||||
ackReason = 0;
|
||||
}
|
||||
if (kbdScan == 224) //extended key
|
||||
{
|
||||
kbdExt = 1;
|
||||
eoi();
|
||||
return;
|
||||
}
|
||||
if (kbdScan == 225)
|
||||
{
|
||||
kbdExt2 = 2;
|
||||
eoi();
|
||||
return;
|
||||
}
|
||||
|
||||
//====handle control keys::
|
||||
kbdAscii = 2;
|
||||
switch (kbdScan) //control keys
|
||||
{
|
||||
case KBD_SCAN_LSHIFT:
|
||||
kbdFlags |= KBD_SHIFT;
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
case KBD_SCAN_RSHIFT:
|
||||
kbdFlags |= KBD_SHIFT;
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
case KBD_SCAN_LCTRL:
|
||||
kbdFlags |= KBD_CTRL;
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
case KBD_SCAN_LALT:
|
||||
kbdFlags |= KBD_ALT;
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
|
||||
case KBD_SCAN_LSHIFT + KBD_SCAN_RELEASED:
|
||||
kbdFlags &= (KBD_SHIFT ^ 0xFF);
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
case KBD_SCAN_RSHIFT + KBD_SCAN_RELEASED:
|
||||
kbdFlags &= (KBD_SHIFT ^ 0xFF);
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
case KBD_SCAN_LCTRL + KBD_SCAN_RELEASED:
|
||||
kbdFlags &= (KBD_CTRL ^ 0xFF);
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
case KBD_SCAN_LALT + KBD_SCAN_RELEASED:
|
||||
kbdFlags &= (KBD_ALT ^ 0xFF);
|
||||
kbdAscii = 1;
|
||||
break;
|
||||
|
||||
case KBD_SCAN_CAPS+KBD_SCAN_RELEASED:
|
||||
kbdFlags ^= KBD_CAPS;
|
||||
kbdAscii = 1;
|
||||
kbd_resetLEDs(); //update LEDs
|
||||
break;
|
||||
case KBD_SCAN_SCROLL+KBD_SCAN_RELEASED:
|
||||
kbdFlags ^= KBD_SCROLL;
|
||||
kbdAscii = 1;
|
||||
kbd_resetLEDs(); //update LEDs
|
||||
break;
|
||||
case KBD_SCAN_NUM+KBD_SCAN_RELEASED:
|
||||
kbdFlags ^= KBD_NUM;
|
||||
kbdAscii = 1;
|
||||
kbd_resetLEDs(); //update LEDs
|
||||
break;
|
||||
}
|
||||
if (kbdAscii == 1)
|
||||
{
|
||||
if (kbdExt > 0)
|
||||
kbdExt--;
|
||||
eoi();
|
||||
return;
|
||||
}
|
||||
//====determine ASCII value of key::
|
||||
if (kbdExt > 0) //extended key, kbdScan holds extended key
|
||||
{
|
||||
kbdExt--;
|
||||
kbdAscii = 1;
|
||||
switch (kbdScan)
|
||||
{
|
||||
case KBD_SCANE_ENTER:
|
||||
kbdAscii = '\n'; break;
|
||||
case 53: // '/' character (divide on numpad)
|
||||
kbdAscii = '/'; break;
|
||||
}
|
||||
}
|
||||
else if (kbdExt2 > 0) //extended key 2
|
||||
{
|
||||
kbdExt2--;
|
||||
// if (kbdScan == 69) // (pause|break)
|
||||
// kbdAscii = 2; //flag ascii value of 1 means control character (pausebreak)
|
||||
// else
|
||||
kbdAscii = 2; //flag ascii value of 2 means ignore key (or unknown value)
|
||||
}
|
||||
else //not an extended key
|
||||
{
|
||||
// if letter key
|
||||
if (((kbdScan >= 16) && (kbdScan <= 25)) || ((kbdScan >= 30) && (kbdScan <= 38)) || ((kbdScan >= 44) && (kbdScan <= 50)))
|
||||
{
|
||||
// if caps and shift are different (either one pressed, not both)
|
||||
if (((kbdFlags & KBD_SHIFT) != 0) & ((kbdFlags & KBD_CAPS) != 0))
|
||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
||||
else if (((kbdFlags & KBD_SHIFT) == 0) & ((kbdFlags & KBD_CAPS) == 0))
|
||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
||||
else
|
||||
kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F];
|
||||
}
|
||||
// if numpad key
|
||||
else if ((kbdScan >= 71) && (kbdScan <= 83))
|
||||
{
|
||||
// if numlock on
|
||||
if (kbdFlags & KBD_NUM)
|
||||
kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F];
|
||||
else
|
||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
||||
}
|
||||
// other key
|
||||
else
|
||||
{
|
||||
if ((kbdFlags & KBD_SHIFT) != 0)
|
||||
kbdAscii = SCAN2ASCIISHIFT[kbdScan & 0x7F];
|
||||
else
|
||||
kbdAscii = SCAN2ASCII[kbdScan & 0x7F];
|
||||
}
|
||||
}
|
||||
|
||||
//====do something with key::
|
||||
if ((kbdScan == 83) & ((kbdFlags & KBD_CTRL) != 0) & ((kbdFlags & KBD_ALT) != 0))
|
||||
{
|
||||
printf("Initiating reboot.");
|
||||
restart();
|
||||
}
|
||||
if (kbdAscii == 2) //unknown key / ignore key
|
||||
{
|
||||
eoi();
|
||||
return;
|
||||
}
|
||||
if (kbdScan < KBD_SCAN_RELEASED) //a key was pressed, save it
|
||||
{
|
||||
if (kbdBufferLen >= KBD_BUFFER_LENGTH) //no key slots available
|
||||
{
|
||||
eoi();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
kbdBuffer[(kbdBufferStart+kbdBufferLen)%KBD_BUFFER_LENGTH] = (dword) ((kbdFlags << 16) | (kbdScan << 8) | kbdAscii);
|
||||
kbdBufferLen++;
|
||||
}
|
||||
}
|
||||
|
||||
eoi();
|
||||
|
||||
// if (key == 0xFA) //ACKnowledge
|
||||
// outportb(0x60, 0x3);
|
||||
};
|
||||
|
||||
inline byte switchCase(byte asciiCode)
|
||||
{
|
||||
if ((asciiCode >= 'A') & (asciiCode <= 'Z'))
|
||||
return (asciiCode + ('a' - 'A'));
|
||||
if ((asciiCode >= 'a') & (asciiCode <= 'z'))
|
||||
return (asciiCode - ('a' - 'A'));
|
||||
return asciiCode;
|
||||
}
|
||||
|
||||
dword kbdGetKey()
|
||||
{
|
||||
if (kbdBufferLen == 0) //buffer empty
|
||||
return 0;
|
||||
dword retVal = kbdBuffer[kbdBufferStart];
|
||||
printf("S:%d\tE:%d\tR:%x\n", kbdBufferStart, kbdBufferLen, retVal);
|
||||
kbdBufferStart++;
|
||||
kbdBufferLen--;
|
||||
if (kbdBufferStart == KBD_BUFFER_LENGTH)
|
||||
kbdBufferStart = 0;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
dword kbdWaitKey()
|
||||
{
|
||||
dword retVal = 0;
|
||||
for (;;)
|
||||
{
|
||||
retVal = kbdGetKey();
|
||||
if (retVal != 0)
|
||||
{
|
||||
printf("S:%d\tE:%d\tR:%x\n", kbdBufferStart, kbdBufferLen, retVal);
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void kbd_resetLEDs()
|
||||
{
|
||||
outportb(0x64, 0xED);
|
||||
//outportb(0x64, (kbdFlags & 0x07));
|
||||
ackReason = 0xED;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
47
keyboard.h
47
keyboard.h
@ -1,47 +0,0 @@
|
||||
//Keyboard.h
|
||||
// Created: 04/17/03 Josh Holtrop
|
||||
// Modified: 05/07/03
|
||||
//for HOS
|
||||
|
||||
#define KBD_SCROLL 0x01
|
||||
#define KBD_NUM 0x02
|
||||
#define KBD_CAPS 0x04
|
||||
#define KBD_SHIFT 0x10
|
||||
#define KBD_CTRL 0x20
|
||||
#define KBD_ALT 0x40
|
||||
|
||||
#define KBD_SCAN_RELEASED 128
|
||||
|
||||
#define KBD_SCAN_LCTRL 29
|
||||
#define KBD_SCAN_LSHIFT 42
|
||||
#define KBD_SCAN_RSHIFT 54
|
||||
#define KBD_SCAN_LALT 56
|
||||
#define KBD_SCAN_SCROLL 70
|
||||
#define KBD_SCAN_CAPS 58
|
||||
#define KBD_SCAN_NUM 69
|
||||
|
||||
#define KBD_SCANE_PRINTSCREEN 55
|
||||
#define KBD_SCANE_INS 82
|
||||
#define KBD_SCANE_HOME 71
|
||||
#define KBD_SCANE_PGUP 73
|
||||
#define KBD_SCANE_DEL 83
|
||||
#define KBD_SCANE_END 79
|
||||
#define KBD_SCANE_PGDN 81
|
||||
#define KBD_SCANE_ENTER 28
|
||||
|
||||
#define KBD_SCANE_NULL 42
|
||||
|
||||
|
||||
|
||||
//====PROTOTYPES:
|
||||
|
||||
void isr_keyboard();
|
||||
inline void kbd_resetLEDs();
|
||||
inline byte kbd_ascii(dword keycode);
|
||||
inline byte kbd_scancode(dword keycode);
|
||||
inline byte kbd_flags(dword keycode);
|
||||
inline byte switchCase(byte asciiCode);
|
||||
dword kbdGetKey();
|
||||
dword kbdWaitKey();
|
||||
|
||||
|
22
link.ld
22
link.ld
@ -1,22 +0,0 @@
|
||||
OUTPUT_FORMAT("binary")
|
||||
ENTRY(start)
|
||||
SECTIONS
|
||||
{
|
||||
.text 0x100000 : {
|
||||
code = .; _code = .; __code = .;
|
||||
*(.text)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
.data : {
|
||||
data = .; _data = .; __data = .;
|
||||
*(.data)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
.bss :
|
||||
{
|
||||
bss = .; _bss = .; __bss = .;
|
||||
*(.bss)
|
||||
. = ALIGN(4096);
|
||||
}
|
||||
end = .; _end = .; __end = .;
|
||||
}
|
242
mm.c
242
mm.c
@ -1,242 +0,0 @@
|
||||
// mm.c
|
||||
// 09/01/03 Josh Holtrop
|
||||
|
||||
// 0x368000 is first available byte (this is right after the kernel @ 1mb and the initrd @ 2mb, 1440kb.
|
||||
|
||||
|
||||
pageblock *first_pageblock = (pageblock *) 0;
|
||||
dword mm_totalmem = 0x100000; //assume 1mb
|
||||
|
||||
void mm_init()
|
||||
{
|
||||
dword *memmap_entries = (dword *) 0x9040A;
|
||||
memmap_entry *maps = (memmap_entry *) 0x92000;
|
||||
dword a;
|
||||
for (a=0;a<(*memmap_entries);a++)
|
||||
{
|
||||
if (maps[a].attributes == 1) // (1) mem free to OS
|
||||
{
|
||||
mm_totalmem += maps[a].limit.lowdword;
|
||||
if ((maps[a].base.lowdword + maps[a].limit.lowdword) > (FREERAM_START+8192)) //goes past where we start freeram
|
||||
{
|
||||
if (maps[a].base.lowdword < FREERAM_START)
|
||||
{
|
||||
maps[a].limit.lowdword = maps[a].limit.lowdword - (FREERAM_START - maps[a].base.lowdword);
|
||||
maps[a].base.lowdword = FREERAM_START; //block at least 4kb, starts >= FREERAM_START
|
||||
}
|
||||
if (first_pageblock == 0) //no pageblock page set up yet, so set it up here
|
||||
{
|
||||
first_pageblock = (pageblock *) maps[a].base.lowdword ;
|
||||
maps[a].base.lowdword += 4096;
|
||||
maps[a].limit.lowdword -= 4096;
|
||||
mm_init_pageblockpage(first_pageblock);
|
||||
first_pageblock->base = maps[a].base.lowdword;
|
||||
first_pageblock->length = maps[a].limit.lowdword / 4096;
|
||||
first_pageblock->flags = MM_PB_AVAIL;
|
||||
}
|
||||
else //first_pageblock already set up, add on segment
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if (pb->flags == MM_PB_NP)
|
||||
break;
|
||||
else
|
||||
pb++;
|
||||
}
|
||||
pb->base = maps[a].base.lowdword;
|
||||
pb->length = maps[a].limit.lowdword / 4096;
|
||||
pb->flags = MM_PB_AVAIL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (first_pageblock == 0)
|
||||
{
|
||||
printf("ERROR! NO INITIAL PAGE BLOCK COULD BE CREATED.");
|
||||
asm("cli");
|
||||
asm("hlt");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void mm_init_pageblockpage(pageblock *pbp)
|
||||
{
|
||||
pageblock *pb = pbp;
|
||||
int a;
|
||||
for (a=0; a<512; a++)
|
||||
{
|
||||
pb->base = 0;
|
||||
pb->length = 0;
|
||||
pb->flags = MM_PB_NP;
|
||||
if (a<511)
|
||||
pb->link = (dword)pb + sizeof(pageblock);
|
||||
else
|
||||
pb->link = 0;
|
||||
pb++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void *mm_palloc(dword numpages)
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
dword freeentries = mm_freeentries();
|
||||
if (freeentries < 2)
|
||||
{
|
||||
if(mm_new_pageblock_page() == 0)
|
||||
return 0;
|
||||
}
|
||||
pageblock *newentry = mm_nextpageblockentry();
|
||||
for (;;)
|
||||
{
|
||||
if ((pb->flags == MM_PB_AVAIL) && (pb->length >= numpages))
|
||||
break;
|
||||
if (pb->link == 0)
|
||||
return 0;
|
||||
pb = (pageblock *)pb->link;
|
||||
}
|
||||
if (pb->length == numpages) //dont need a new entry, just mark this one as used :)
|
||||
{
|
||||
pb->flags = MM_PB_USED;
|
||||
return pb;
|
||||
}
|
||||
else //subtract out allocated number of pages from length
|
||||
{
|
||||
newentry->base = pb->base;
|
||||
newentry->length = numpages;
|
||||
newentry->flags = MM_PB_USED;
|
||||
pb->base += (numpages * 4096);
|
||||
pb->length -= numpages;
|
||||
return (void *)newentry->base;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dword mm_freeentries()
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
dword counter = 0;
|
||||
for (;;)
|
||||
{
|
||||
if (pb->flags == MM_PB_NP)
|
||||
counter++;
|
||||
if (pb->link == 0)
|
||||
return counter;
|
||||
pb = (pageblock *) pb->link;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pageblock *mm_new_pageblock_page()
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if ((pb->flags == MM_PB_AVAIL) && (pb->length > 1))
|
||||
{
|
||||
pageblock *retval = (pageblock *)pb->base;
|
||||
pb->base += 4096;
|
||||
pb->length--;
|
||||
mm_init_pageblockpage(retval); //zeros out, links new pageblock page
|
||||
mm_lastpageblockentry()->link = (dword)retval; //set up link to new pageblock page
|
||||
return retval;
|
||||
}
|
||||
pb = (pageblock *) pb->link;
|
||||
if (pb == 0)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int mm_pfree(void *ptr)
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
dword tofree = (dword) ptr;
|
||||
for (;;)
|
||||
{
|
||||
if ((pb->base == tofree) && (pb->flags == MM_PB_USED))
|
||||
{
|
||||
pb->flags = MM_PB_AVAIL; //found block, mark available / coalesce it
|
||||
pageblock *pbc = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if (pbc->flags == MM_PB_AVAIL)
|
||||
{
|
||||
if ((pbc->base + (pbc->length * 4096)) == pb->base) //pbc ends where pb starts
|
||||
{
|
||||
pbc->length += pb->length; //extend pbc's length by pb's length
|
||||
pb->flags = MM_PB_NP; //mark pb as an unused entry
|
||||
return 0;
|
||||
}
|
||||
if ((pb->base + (pb->length * 4096)) == pbc->base) //pb ends where pb starts
|
||||
{
|
||||
pb->length += pbc->length;
|
||||
pbc->flags = MM_PB_NP;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (pbc->link == 0) //no entry found to consolidate...
|
||||
return 0;
|
||||
pbc = (pageblock *) pbc->link; //next entry to test
|
||||
}
|
||||
}
|
||||
if (pb->link == 0)
|
||||
return 1;
|
||||
pb = (pageblock *) pb->link;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pageblock *mm_lastpageblockentry()
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if (pb->link == 0)
|
||||
return pb;
|
||||
pb = (pageblock *)pb->link;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pageblock *mm_nextpageblockentry()
|
||||
{
|
||||
pageblock *pb = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if (pb->flags == MM_PB_NP)
|
||||
return pb;
|
||||
if (pb->link == 0)
|
||||
return 0;
|
||||
pb = (pageblock *)pb->link;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
dword mm_freemem()
|
||||
{
|
||||
dword amount = 0;
|
||||
pageblock *pb = first_pageblock;
|
||||
for (;;)
|
||||
{
|
||||
if (pb->flags == MM_PB_AVAIL)
|
||||
amount += (pb->length)*4096;
|
||||
if (pb->link == 0)
|
||||
return amount;
|
||||
pb = (pageblock *) pb->link;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
36
mm.h
36
mm.h
@ -1,36 +0,0 @@
|
||||
|
||||
|
||||
typedef struct {
|
||||
qword base;
|
||||
qword limit;
|
||||
dword attributes;
|
||||
} __attribute__((packed)) memmap_entry;
|
||||
|
||||
|
||||
typedef struct {
|
||||
dword base;
|
||||
dword length; //in pages
|
||||
dword flags;
|
||||
dword link;
|
||||
} __attribute__ ((packed)) pageblock; //16 byte pageblock entry - 512 entries = 1 page
|
||||
|
||||
|
||||
void mm_init();
|
||||
void mm_init_pageblockpage(pageblock *pbp);
|
||||
void *mm_palloc(dword numpages);
|
||||
int mm_pfree(void *ptr);
|
||||
dword mm_freeentries();
|
||||
pageblock *mm_new_pageblock_page();
|
||||
pageblock *mm_lastpageblockentry();
|
||||
pageblock *mm_nextpageblockentry();
|
||||
dword mm_freemem();
|
||||
|
||||
|
||||
#define MM_PB_FLAGMASK 0x03 //00000011
|
||||
#define MM_PB_NP 0x00 //00000000
|
||||
#define MM_PB_AVAIL 0x01 //00000001
|
||||
#define MM_PB_USED 0x02 //00000010
|
||||
|
||||
|
||||
|
||||
|
89
readme.txt
Normal file
89
readme.txt
Normal file
@ -0,0 +1,89 @@
|
||||
HOS - Holtrop's Operating System
|
||||
--------------------------------
|
||||
|
||||
HOS is (in the process of becoming) a 32-bit, protected mode, graphical, multitasking operating system.
|
||||
It was written by me, Josh Holtrop, with help from a few others along the way.
|
||||
|
||||
|
||||
Goals: (A = accomplished, P = in progress, T = todo)
|
||||
----------------------------------------------------
|
||||
(A) Custom bootloader to load kernel from FAT-formatted boot media, options for video mode/ram disk
|
||||
(A) Multiboot compliance - kernel can be loaded by GRUB
|
||||
(A) 32-bit protected mode environment
|
||||
(A) VESA Support for graphics modes
|
||||
(A) PS/2 keyboard & mouse drivers
|
||||
(A) Utilize x86's paging architecture for virtual memory management
|
||||
|
||||
(P) Console Manager
|
||||
(P) VFS abstraction layer for a single file system
|
||||
(P) ram disk driver
|
||||
(P) devfs file system driver
|
||||
(P) ext2 file system support
|
||||
|
||||
(T) vfat file system support
|
||||
(T) Multitasking support
|
||||
(T) HASH command shell
|
||||
(T) Window Manager
|
||||
(T) Various other utilities/applications
|
||||
(T) Hard Drive (ATA) driver
|
||||
(T) cdrom (ATAPI) driver
|
||||
|
||||
|
||||
Change Log
|
||||
----------
|
||||
0.16
|
||||
12/16/04 - initrd going to be loaded as a gzipped ext2 image (kernel module) - faster loading!
|
||||
|
||||
0.15
|
||||
07/10/04 - Multiboot support added, loadable by GRUB
|
||||
|
||||
0.14
|
||||
05/21/04 - C++ support in kernel, can use classes & templates
|
||||
04/04/04 - video_line function for diagonal lines
|
||||
03/16/04 - new VFS design with support for a loop device
|
||||
03/01/04 - Thanks to Ben Meyer for helping me get a Makefile working and building on linux to work!
|
||||
|
||||
0.13
|
||||
01/26/04 - functions added to read/write CMOS clock date and time
|
||||
01/19/04 - fixed bug GDTR/IDTR pointing to physical rather than linear table base address
|
||||
01/07/04 - fixed bug not reading sectors correctly from floppy
|
||||
12/28/03 - fixed bug not storing eax on interrupt
|
||||
12/25/03 - fixed bug in mm_palloc()
|
||||
12/25/03 - incorporated output functions as regular functions rather than as part of a linked library
|
||||
12/23/03 - re-written physical memory manager using bitmap instead of stack
|
||||
12/22/03 - kernel relocated to 3gb linear / 1mb+24kb physical to allow for app. address space
|
||||
|
||||
0.12
|
||||
12/21/03 - sample bmp loader tested, works (tests loading a kernel of size ~ 932kb)
|
||||
12/20/03 - GDT/IDT now located at 1mb physical, before kernel
|
||||
10/30/03 - turns floppy motor off
|
||||
10/30/03 - keyboard LEDs working
|
||||
10/29/03 - paging functions working
|
||||
10/15/03 - physical memory management page allocators working
|
||||
|
||||
0.11
|
||||
10/09/03 - PS/2 mouse driver
|
||||
|
||||
0.10
|
||||
09/11/03 - Rewritten C and assembly kernel with VESA GUI mode support, keyboard driver
|
||||
|
||||
0.05
|
||||
05/14/03 - HGUI24/HGUI32 commands finished for testing GUI on both 24bpp and 32bpp graphics cards
|
||||
05/14/03 - first web release!
|
||||
|
||||
0.04
|
||||
03/09/03 - added VM shortcut command
|
||||
03/09/03 - press up to fill retrieve last inputted command for Nate Scholten
|
||||
03/08/03 - press clear to clear console input
|
||||
03/07/03 - added "shortcut" commands PC, IC, ? for Nate Scholten
|
||||
03/06/03 - added PROMPTC, INPUTC commands
|
||||
|
||||
0.03
|
||||
12/30/02 - Command Line Interface working, accepting basic commands
|
||||
|
||||
0.02
|
||||
12/11/02 - Assembly bootsector can load stage2 ("console")
|
||||
|
||||
0.01
|
||||
12/01/02 - Real mode assembly bootsector boots from floppy disk successfully
|
||||
|
18
src/boot.S
Normal file
18
src/boot.S
Normal file
@ -0,0 +1,18 @@
|
||||
.global hos_start
|
||||
.type hos_start, @function
|
||||
hos_start:
|
||||
/* Set stack pointer. */
|
||||
mov $_stack_end, %esp
|
||||
|
||||
/* Jump to C. */
|
||||
push $0
|
||||
push $0
|
||||
push $0
|
||||
push %ebx
|
||||
call hos_main
|
||||
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
||||
.size hos_start, . - hos_start
|
116
src/fb.c
Normal file
116
src/fb.c
Normal file
@ -0,0 +1,116 @@
|
||||
#include "fb.h"
|
||||
#include <stddef.h>
|
||||
#include "mem.h"
|
||||
|
||||
static struct {
|
||||
uint32_t * addr;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t pitch;
|
||||
} fb;
|
||||
|
||||
static inline uint32_t build_pixel(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
return (r << 16u) | (g << 8u) | b;
|
||||
}
|
||||
|
||||
static inline void fb_set_pixel(int x, int y, uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
fb.addr[fb.pitch * y + x] = build_pixel(r, g, b);
|
||||
}
|
||||
|
||||
void fb_clear(void)
|
||||
{
|
||||
memset32(fb.addr, 0u, fb.pitch * fb.height);
|
||||
}
|
||||
|
||||
void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch)
|
||||
{
|
||||
fb.addr = addr;
|
||||
fb.width = width;
|
||||
fb.height = height;
|
||||
fb.pitch = pitch / 4u;
|
||||
fb_clear();
|
||||
}
|
||||
|
||||
uint32_t * fb_addr(void)
|
||||
{
|
||||
return fb.addr;
|
||||
}
|
||||
|
||||
bool fb_ready(void)
|
||||
{
|
||||
return fb.addr != NULL;
|
||||
}
|
||||
|
||||
uint32_t fb_width(void)
|
||||
{
|
||||
return fb.width;
|
||||
}
|
||||
|
||||
uint32_t fb_height(void)
|
||||
{
|
||||
return fb.height;
|
||||
}
|
||||
|
||||
void fb_blend_alpha8(const uint8_t * bitmap, int width, int height, int pitch, int x, int y, uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
if (((x + width) <= 0) || (x >= (int)fb.width) || ((y + height) <= 0) || (y >= (int)fb.height))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (x < 0)
|
||||
{
|
||||
width += x;
|
||||
bitmap += (-x);
|
||||
x = 0;
|
||||
}
|
||||
if (y < 0)
|
||||
{
|
||||
height += y;
|
||||
bitmap += ((-y) * pitch);
|
||||
y = 0;
|
||||
}
|
||||
if ((x + width) > (int)fb.width)
|
||||
{
|
||||
width = (int)fb.width - x;
|
||||
}
|
||||
if ((y + height) > (int)fb.height)
|
||||
{
|
||||
height = (int)fb.height - y;
|
||||
}
|
||||
uint32_t * target = &fb.addr[fb.pitch * y + x];
|
||||
for (int row = 0; row < height; row++)
|
||||
{
|
||||
for (int col = 0; col < width; col++)
|
||||
{
|
||||
uint32_t alpha = bitmap[col];
|
||||
uint32_t current_pixel = target[col];
|
||||
uint8_t cr = (current_pixel >> 16u) & 0xFFu;
|
||||
uint8_t cg = (current_pixel >> 8u) & 0xFFu;
|
||||
uint8_t cb = current_pixel & 0xFFu;
|
||||
uint8_t pr = alpha * r / 255u;
|
||||
uint8_t pg = alpha * g / 255u;
|
||||
uint8_t pb = alpha * b / 255u;
|
||||
uint32_t current_alpha = 255u - alpha;
|
||||
uint32_t pixel = build_pixel(
|
||||
pr + current_alpha * cr / 255u,
|
||||
pg + current_alpha * cg / 255u,
|
||||
pb + current_alpha * cb / 255u);
|
||||
target[col] = pixel;
|
||||
}
|
||||
bitmap += pitch;
|
||||
target += fb.pitch;
|
||||
}
|
||||
}
|
||||
|
||||
void fb_fill(int x, int y, int width, int height, uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
uint32_t * target = &fb.addr[fb.pitch * y + x];
|
||||
uint32_t pixel = build_pixel(r, g, b);
|
||||
for (int row = 0; row < height; row++)
|
||||
{
|
||||
memset32(target, pixel, width);
|
||||
target += fb.pitch;
|
||||
}
|
||||
}
|
16
src/fb.h
Normal file
16
src/fb.h
Normal file
@ -0,0 +1,16 @@
|
||||
#ifndef FB_H
|
||||
#define FB_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
void fb_init(uint32_t * addr, uint32_t width, uint32_t height, uint32_t pitch);
|
||||
bool fb_ready(void);
|
||||
uint32_t * fb_addr(void);
|
||||
uint32_t fb_width(void);
|
||||
uint32_t fb_height(void);
|
||||
void fb_blend_alpha8(const uint8_t * bitmap, int width, int height, int pitch, int x, int y, uint8_t r, uint8_t g, uint8_t b);
|
||||
void fb_fill(int x, int y, int width, int height, uint8_t r, uint8_t g, uint8_t b);
|
||||
void fb_clear(void);
|
||||
|
||||
#endif
|
11
src/fb_text.c
Normal file
11
src/fb_text.c
Normal file
@ -0,0 +1,11 @@
|
||||
#include "fb_text.h"
|
||||
#include "kfont.h"
|
||||
#include "fb.h"
|
||||
|
||||
void fb_text_render_char(int c, int x, int y, uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
const fontgen_char_info_t * char_info = kfont.char_infos[c];
|
||||
y += kfont.line_height - kfont.baseline_offset - char_info->top;
|
||||
x += char_info->left;
|
||||
fb_blend_alpha8(char_info->bitmap, char_info->width, char_info->height, char_info->width, x, y, r, g, b);
|
||||
}
|
8
src/fb_text.h
Normal file
8
src/fb_text.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef FB_TEXT_H
|
||||
#define FB_TEXT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void fb_text_render_char(int c, int x, int y, uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
#endif
|
16
src/gdt.S
Normal file
16
src/gdt.S
Normal file
@ -0,0 +1,16 @@
|
||||
.global gdt_set
|
||||
.extern gdtr
|
||||
.type gdt_set, @function
|
||||
gdt_set:
|
||||
lgdt gdtr
|
||||
jmp $0x8, $gdt_set_reload
|
||||
gdt_set_reload:
|
||||
mov $0x10, %ax
|
||||
mov %ax, %ds
|
||||
mov %ax, %es
|
||||
mov %ax, %fs
|
||||
mov %ax, %gs
|
||||
mov %ax, %ss
|
||||
ret
|
||||
|
||||
.size gdt_set, . - gdt_set
|
20
src/gdt.c
Normal file
20
src/gdt.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include "gdt.h"
|
||||
|
||||
static const gdt_entry_t gdt_entries[] = {
|
||||
/* Null descriptor */
|
||||
0u,
|
||||
/* Code segment for kernel */
|
||||
gdt_build_entry(0u, 0xFFFFFu, 1u, 0u, 1u, 1u, 0u, 1u, 0u, 1u, 1u, 0u),
|
||||
/* Data segment for kernel */
|
||||
gdt_build_entry(0u, 0xFFFFFu, 1u, 0u, 1u, 0u, 0u, 1u, 0u, 1u, 1u, 0u),
|
||||
};
|
||||
|
||||
gdtr_t gdtr;
|
||||
|
||||
void gdt_init(void)
|
||||
{
|
||||
gdtr.size = sizeof(gdt_entries);
|
||||
gdtr.offset_lower = (uintptr_t)gdt_entries & 0xFFFFu;
|
||||
gdtr.offset_upper = (uintptr_t)gdt_entries >> 16u;
|
||||
gdt_set();
|
||||
}
|
34
src/gdt.h
Normal file
34
src/gdt.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef GDT_H
|
||||
#define GDT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
uint16_t size;
|
||||
uint16_t offset_lower;
|
||||
uint16_t offset_upper;
|
||||
uint16_t _reserved;
|
||||
} gdtr_t;
|
||||
|
||||
typedef uint64_t gdt_entry_t;
|
||||
#define gdt_build_entry(base, limit, pr, privl, s, ex, dc, rw, ac, gr, sz, l) \
|
||||
(gdt_entry_t)( \
|
||||
(((gdt_entry_t)base << 32) & 0xFF00000000000000ull) | /* Base 0:15 */ \
|
||||
(((gdt_entry_t)gr & 0x1u) << 55) | /* Granularity (0 = bytes, 1 = blocks) */ \
|
||||
(((gdt_entry_t)sz & 0x1u) << 54) | /* Size (0 = 16-bit, 1 = 32-bit) */ \
|
||||
(((gdt_entry_t)l & 0x1u) << 53) | /* L flag (x86_64 code) */ \
|
||||
(((gdt_entry_t)limit << 32) & 0x000F000000000000ull) | /* Limit 16:19 */ \
|
||||
(((gdt_entry_t)pr & 0x1u) << 47) | /* Present flag */ \
|
||||
(((gdt_entry_t)privl & 0x3u) << 45) | /* Privilege (ring level) */ \
|
||||
(((gdt_entry_t)s & 0x1u) << 44) | /* Type (0 = system, 1 = code/data) */ \
|
||||
(((gdt_entry_t)ex & 0x1u) << 43) | /* Executable flag */ \
|
||||
(((gdt_entry_t)dc & 0x1u) << 42) | /* Direction/Conforming */ \
|
||||
(((gdt_entry_t)rw & 0x1u) << 41) | /* Readable/Writable */ \
|
||||
(((gdt_entry_t)ac & 0x1u) << 40) | /* Accessed flag */ \
|
||||
(((gdt_entry_t)base << 16) & 0x000000FFFFFF0000ull) | /* Base 0:23 */ \
|
||||
((gdt_entry_t)limit & 0x000000000000FFFFull)) /* Limit 0:15 */
|
||||
|
||||
void gdt_init(void);
|
||||
void gdt_set(void);
|
||||
|
||||
#endif
|
25
src/hos_main.c
Normal file
25
src/hos_main.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdint.h>
|
||||
#include "fb.h"
|
||||
#include "mbinfo.h"
|
||||
#include "klog.h"
|
||||
#include "gdt.h"
|
||||
#include "mm.h"
|
||||
|
||||
void hos_main(uint32_t mbinfo_addr)
|
||||
{
|
||||
gdt_init();
|
||||
if (!mbinfo_init(mbinfo_addr))
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!fb_ready())
|
||||
{
|
||||
return;
|
||||
}
|
||||
klog_init();
|
||||
klog_printf("Welcome to HOS!\n");
|
||||
mm_init();
|
||||
mbinfo_load();
|
||||
klog_printf("Found %dKB of usable RAM\n", mm_get_total_ram() / 1024u);
|
||||
klog_printf("Kernel is %dKB at 0x%x\n", mm_get_kernel_size() / 1024u, mm_get_kernel_address());
|
||||
}
|
281
src/hos_printf.c
Normal file
281
src/hos_printf.c
Normal file
@ -0,0 +1,281 @@
|
||||
#include "hos_printf.h"
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "string.h"
|
||||
|
||||
static size_t format_dec(char * buffer, int32_t v)
|
||||
{
|
||||
size_t sz = 0u;
|
||||
bool printing = false;
|
||||
if (v < 0)
|
||||
{
|
||||
buffer[sz++] = '-';
|
||||
v = -v;
|
||||
}
|
||||
for (int32_t div = 1000000000; div >= 1; div /= 10)
|
||||
{
|
||||
int32_t digit = v / div;
|
||||
v %= div;
|
||||
if ((digit != 0) || (div == 1))
|
||||
{
|
||||
printing = true;
|
||||
}
|
||||
if (printing)
|
||||
{
|
||||
buffer[sz++] = digit + '0';
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
static size_t format_dec64(char * buffer, int64_t v)
|
||||
{
|
||||
size_t sz = 0u;
|
||||
bool printing = false;
|
||||
if (v < 0)
|
||||
{
|
||||
buffer[sz++] = '-';
|
||||
v = -v;
|
||||
}
|
||||
for (int64_t div = 1000000000000000000; div >= 1; div /= 10)
|
||||
{
|
||||
int64_t digit = v / div;
|
||||
v %= div;
|
||||
if ((digit != 0) || (div == 1))
|
||||
{
|
||||
printing = true;
|
||||
}
|
||||
if (printing)
|
||||
{
|
||||
buffer[sz++] = digit + '0';
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
static size_t format_udec(char * buffer, int32_t v)
|
||||
{
|
||||
size_t sz = 0u;
|
||||
for (int32_t div = 1000000000u; div >= 1u; div /= 10u)
|
||||
{
|
||||
int32_t digit = v / div;
|
||||
v %= div;
|
||||
if ((digit != 0) || (sz > 0u) || (div == 1))
|
||||
{
|
||||
buffer[sz++] = digit + '0';
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
static size_t format_udec64(char * buffer, uint64_t v)
|
||||
{
|
||||
size_t sz = 0u;
|
||||
for (uint64_t div = 10000000000000000000u; div >= 1u; div /= 10u)
|
||||
{
|
||||
uint64_t digit = v / div;
|
||||
v %= div;
|
||||
if ((digit != 0) || (sz > 0u) || (div == 1))
|
||||
{
|
||||
buffer[sz++] = digit + '0';
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
static size_t format_hex(char * buffer, uint32_t v, bool upper)
|
||||
{
|
||||
const char upper_hex[] = "0123456789ABCDEF";
|
||||
const char lower_hex[] = "0123456789abcdef";
|
||||
size_t sz = 0u;
|
||||
for (int i = 28; i >= 0; i -= 4)
|
||||
{
|
||||
uint8_t n = (v >> i) & 0xFu;
|
||||
if ((sz > 0u) || (n != 0u) || (i == 0))
|
||||
{
|
||||
buffer[sz] = upper ? upper_hex[n] : lower_hex[n];
|
||||
sz++;
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
static size_t format_hex64(char * buffer, uint64_t v, bool upper)
|
||||
{
|
||||
const char upper_hex[] = "0123456789ABCDEF";
|
||||
const char lower_hex[] = "0123456789abcdef";
|
||||
size_t sz = 0u;
|
||||
for (int i = 60; i >= 0; i -= 4)
|
||||
{
|
||||
uint8_t n = (v >> i) & 0xFu;
|
||||
if ((sz > 0u) || (n != 0u) || (i == 0))
|
||||
{
|
||||
buffer[sz] = upper ? upper_hex[n] : lower_hex[n];
|
||||
sz++;
|
||||
}
|
||||
}
|
||||
return sz;
|
||||
}
|
||||
|
||||
static void pad_write(const stream_t * stream, const char * data, size_t length, bool leading_zero, bool left_just, size_t width)
|
||||
{
|
||||
if (left_just)
|
||||
{
|
||||
stream->write(data, length);
|
||||
while (length < width)
|
||||
{
|
||||
stream->write1(' ');
|
||||
length++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
char fill = leading_zero ? '0' : ' ';
|
||||
size_t l = length;
|
||||
while (l < width)
|
||||
{
|
||||
stream->write1(fill);
|
||||
l++;
|
||||
}
|
||||
stream->write(data, length);
|
||||
}
|
||||
}
|
||||
|
||||
void hos_vprintf(const stream_t * stream, const char * fmt, va_list va)
|
||||
{
|
||||
bool in_conv = false;
|
||||
char c;
|
||||
char buffer[22];
|
||||
size_t width;
|
||||
bool leading_zero;
|
||||
bool left_just;
|
||||
bool long_flag;
|
||||
size_t length;
|
||||
while ((c = *fmt))
|
||||
{
|
||||
if (in_conv)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '%':
|
||||
stream->write1('%');
|
||||
break;
|
||||
case 'X':
|
||||
{
|
||||
if (long_flag)
|
||||
{
|
||||
uint64_t v = va_arg(va, uint64_t);
|
||||
length = format_hex(buffer, v, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t v = va_arg(va, uint32_t);
|
||||
length = format_hex(buffer, v, true);
|
||||
}
|
||||
pad_write(stream, buffer, length, leading_zero, left_just, width);
|
||||
}
|
||||
break;
|
||||
case 'c':
|
||||
{
|
||||
char ch = va_arg(va, int);
|
||||
pad_write(stream, &ch, 1u, leading_zero, left_just, width);
|
||||
}
|
||||
break;
|
||||
case 'd':
|
||||
{
|
||||
if (long_flag)
|
||||
{
|
||||
int64_t v = va_arg(va, int64_t);
|
||||
length = format_dec64(buffer, v);
|
||||
}
|
||||
else
|
||||
{
|
||||
int32_t v = va_arg(va, int32_t);
|
||||
length = format_dec(buffer, v);
|
||||
}
|
||||
pad_write(stream, buffer, length, leading_zero, left_just, width);
|
||||
}
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
const char * s = va_arg(va, const char *);
|
||||
pad_write(stream, s, strlen(s), leading_zero, left_just, width);
|
||||
}
|
||||
break;
|
||||
case 'u':
|
||||
{
|
||||
if (long_flag)
|
||||
{
|
||||
uint64_t v = va_arg(va, uint64_t);
|
||||
length = format_udec64(buffer, v);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t v = va_arg(va, uint32_t);
|
||||
length = format_udec(buffer, v);
|
||||
}
|
||||
pad_write(stream, buffer, length, leading_zero, left_just, width);
|
||||
}
|
||||
break;
|
||||
case 'x':
|
||||
{
|
||||
if (long_flag)
|
||||
{
|
||||
uint64_t v = va_arg(va, uint64_t);
|
||||
length = format_hex64(buffer, v, false);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t v = va_arg(va, uint32_t);
|
||||
length = format_hex(buffer, v, false);
|
||||
}
|
||||
pad_write(stream, buffer, length, leading_zero, left_just, width);
|
||||
}
|
||||
break;
|
||||
}
|
||||
in_conv = false;
|
||||
}
|
||||
else if (c == '%')
|
||||
{
|
||||
in_conv = true;
|
||||
width = 0u;
|
||||
leading_zero = false;
|
||||
left_just = false;
|
||||
long_flag = false;
|
||||
if (fmt[1] == '-')
|
||||
{
|
||||
left_just = true;
|
||||
fmt++;
|
||||
}
|
||||
if (fmt[1] == '0')
|
||||
{
|
||||
leading_zero = true;
|
||||
fmt++;
|
||||
}
|
||||
while (('0' <= fmt[1]) && (fmt[1] <= '9'))
|
||||
{
|
||||
width *= 10u;
|
||||
width += (fmt[1] - '0');
|
||||
fmt++;
|
||||
}
|
||||
if (fmt[1] == 'l')
|
||||
{
|
||||
long_flag = true;
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stream->write1(c);
|
||||
}
|
||||
fmt++;
|
||||
}
|
||||
}
|
||||
|
||||
void hos_printf(const stream_t * stream, const char * fmt, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
hos_vprintf(stream, fmt, va);
|
||||
va_end(va);
|
||||
}
|
10
src/hos_printf.h
Normal file
10
src/hos_printf.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef HOS_PRINTF_H
|
||||
#define HOS_PRINTF_H
|
||||
|
||||
#include "stream.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
void hos_vprintf(const stream_t * stream, const char * fmt, va_list va);
|
||||
void hos_printf(const stream_t * stream, const char * fmt, ...);
|
||||
|
||||
#endif
|
103
src/klog.c
Normal file
103
src/klog.c
Normal file
@ -0,0 +1,103 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include "klog.h"
|
||||
#include "kfont.h"
|
||||
#include "fb.h"
|
||||
#include "fb_text.h"
|
||||
#include "stream.h"
|
||||
#include "hos_printf.h"
|
||||
#include "mem.h"
|
||||
|
||||
static struct {
|
||||
size_t console_width;
|
||||
size_t console_height;
|
||||
size_t x;
|
||||
size_t y;
|
||||
bool need_shift;
|
||||
} klog;
|
||||
|
||||
static void shift_line(void)
|
||||
{
|
||||
uint32_t * fb = fb_addr();
|
||||
uint32_t w = fb_width();
|
||||
size_t console_fb_line_words = w * kfont.line_height;
|
||||
for (size_t row = 0u; row < klog.console_height; row++)
|
||||
{
|
||||
memcpy32(fb, fb + console_fb_line_words, console_fb_line_words);
|
||||
fb += console_fb_line_words;
|
||||
}
|
||||
fb_fill(0, kfont.line_height * (klog.console_height - 1u), w, kfont.line_height, 0u, 0x2Cu, 0x55u);
|
||||
}
|
||||
|
||||
static void klog_fb_write1(char c)
|
||||
{
|
||||
if (klog.need_shift)
|
||||
{
|
||||
shift_line();
|
||||
klog.need_shift = false;
|
||||
}
|
||||
if (c == '\n')
|
||||
{
|
||||
if (klog.y == (klog.console_height - 1u))
|
||||
{
|
||||
klog.need_shift = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
klog.y++;
|
||||
}
|
||||
klog.x = 0u;
|
||||
}
|
||||
else
|
||||
{
|
||||
int px = klog.x * kfont.advance;
|
||||
int py = klog.y * kfont.line_height;
|
||||
fb_text_render_char(c, px, py, 0xFFu, 0x80u, 0u);
|
||||
klog.x++;
|
||||
if (klog.x == klog.console_width)
|
||||
{
|
||||
if (klog.y == (klog.console_height - 1u))
|
||||
{
|
||||
klog.need_shift = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
klog.y++;
|
||||
}
|
||||
klog.x = 0u;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void klog_fb_write(const char * src, size_t length)
|
||||
{
|
||||
for (size_t i = 0u; i < length; i++)
|
||||
{
|
||||
klog_fb_write1(src[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static const stream_t klog_fb_stream = {
|
||||
klog_fb_write,
|
||||
klog_fb_write1,
|
||||
};
|
||||
|
||||
void klog_init(void)
|
||||
{
|
||||
klog.console_width = fb_width() / kfont.advance;
|
||||
klog.console_height = fb_height() / kfont.line_height;
|
||||
klog.x = 0u;
|
||||
klog.y = 0u;
|
||||
klog.need_shift = false;
|
||||
fb_fill(0, 0, fb_width(), fb_height(), 0u, 0x2Cu, 0x55u);
|
||||
}
|
||||
|
||||
void klog_printf(const char * fmt, ...)
|
||||
{
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
hos_vprintf(&klog_fb_stream, fmt, va);
|
||||
va_end(va);
|
||||
}
|
7
src/klog.h
Normal file
7
src/klog.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef KLOG_H
|
||||
#define KLOG_H
|
||||
|
||||
void klog_init(void);
|
||||
void klog_printf(const char * fmt, ...);
|
||||
|
||||
#endif
|
41
src/link.ld
Normal file
41
src/link.ld
Normal file
@ -0,0 +1,41 @@
|
||||
ENTRY(hos_start)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 1M;
|
||||
_hos_mem_start = .;
|
||||
|
||||
.text BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.multiboot)
|
||||
*(.text)
|
||||
}
|
||||
|
||||
.rodata BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.rodata)
|
||||
}
|
||||
|
||||
.data BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
|
||||
_stack_size = 16K;
|
||||
.stack (NOLOAD) : ALIGN(4K)
|
||||
{
|
||||
_stack_start = .;
|
||||
. = . + _stack_size;
|
||||
_stack_end = .;
|
||||
}
|
||||
|
||||
.bss BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
}
|
||||
|
||||
. = ALIGN(4K);
|
||||
|
||||
_hos_mem_end = .;
|
||||
}
|
100
src/mbinfo.c
Normal file
100
src/mbinfo.c
Normal file
@ -0,0 +1,100 @@
|
||||
#include "multiboot2.h"
|
||||
#include "fb.h"
|
||||
#include "mem.h"
|
||||
#include <stdint.h>
|
||||
#include "klog.h"
|
||||
#include "mm.h"
|
||||
|
||||
static uint32_t mbinfo[2048];
|
||||
|
||||
static void mbinfo_process_tag(const multiboot2_info_tag_t * tag)
|
||||
{
|
||||
switch (tag->type)
|
||||
{
|
||||
case MULTIBOOT2_INFO_BOOT_COMMAND_LINE:
|
||||
{
|
||||
multiboot2_info_boot_command_line_t * cl =
|
||||
(multiboot2_info_boot_command_line_t *)tag;
|
||||
klog_printf("Kernel boot command line: '%s'\n", cl->string);
|
||||
}
|
||||
break;
|
||||
|
||||
case MULTIBOOT2_INFO_BOOT_LOADER_NAME:
|
||||
{
|
||||
multiboot2_info_boot_loader_name_t * bln =
|
||||
(multiboot2_info_boot_loader_name_t *)tag;
|
||||
klog_printf("Boot loader: '%s'\n", bln->string);
|
||||
}
|
||||
break;
|
||||
|
||||
case MULTIBOOT2_INFO_MEMORY_MAP:
|
||||
{
|
||||
multiboot2_info_memory_map_t * mmap_info =
|
||||
(multiboot2_info_memory_map_t *)tag;
|
||||
size_t sz = sizeof(mmap_info->header) +
|
||||
sizeof(mmap_info->entry_size) +
|
||||
sizeof(mmap_info->entry_version);
|
||||
multiboot2_info_memory_map_entry_t * entry = &mmap_info->entries[0];
|
||||
for (;;)
|
||||
{
|
||||
sz += mmap_info->entry_size;
|
||||
if (sz > mmap_info->header.size)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (entry->type == MULTIBOOT2_MEMORY_MAP_TYPE_RAM)
|
||||
{
|
||||
klog_printf("Memory region %16lx : %16lx\n",
|
||||
entry->base_addr,
|
||||
entry->length);
|
||||
mm_register_ram_region(entry->base_addr, entry->length);
|
||||
}
|
||||
entry = (multiboot2_info_memory_map_entry_t *)((uintptr_t)entry + mmap_info->entry_size);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MULTIBOOT2_INFO_FRAMEBUFFER_INFO:
|
||||
{
|
||||
multiboot2_info_framebuffer_info_t * fbinfo =
|
||||
(multiboot2_info_framebuffer_info_t *)tag;
|
||||
fb_init((uint32_t *)(uintptr_t)fbinfo->framebuffer_addr,
|
||||
fbinfo->framebuffer_width,
|
||||
fbinfo->framebuffer_height,
|
||||
fbinfo->framebuffer_pitch);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void process_tags(const multiboot2_info_tag_t * tag, bool init)
|
||||
{
|
||||
while (tag->type != 0u)
|
||||
{
|
||||
if ((init && (tag->type == MULTIBOOT2_INFO_FRAMEBUFFER_INFO)) ||
|
||||
(!init && (tag->type != MULTIBOOT2_INFO_FRAMEBUFFER_INFO)))
|
||||
{
|
||||
mbinfo_process_tag(tag);
|
||||
}
|
||||
tag = multiboot2_info_next_tag(tag);
|
||||
}
|
||||
}
|
||||
|
||||
bool mbinfo_init(uint32_t mbinfo_addr)
|
||||
{
|
||||
multiboot2_info_header_t * mbinfo_header = (multiboot2_info_header_t *)mbinfo_addr;
|
||||
if (mbinfo_header->total_size <= sizeof(mbinfo))
|
||||
{
|
||||
memcpy32(mbinfo, mbinfo_header, mbinfo_header->total_size / 4u);
|
||||
multiboot2_info_tag_t * tag = (multiboot2_info_tag_t *)(mbinfo + sizeof(multiboot2_info_header_t) / 4u);
|
||||
process_tags(tag, true);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void mbinfo_load(void)
|
||||
{
|
||||
multiboot2_info_tag_t * tag = (multiboot2_info_tag_t *)(mbinfo + sizeof(multiboot2_info_header_t) / 4u);
|
||||
process_tags(tag, false);
|
||||
}
|
10
src/mbinfo.h
Normal file
10
src/mbinfo.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef MBINFO_H
|
||||
#define MBINFO_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool mbinfo_init(uint32_t mbinfo_addr);
|
||||
void mbinfo_load(void);
|
||||
|
||||
#endif
|
49
src/mem.h
Normal file
49
src/mem.h
Normal file
@ -0,0 +1,49 @@
|
||||
#ifndef MEM_H
|
||||
#define MEM_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
static inline void memcpy(void * dest, const void * src, size_t n)
|
||||
{
|
||||
uint32_t r0, r1, r2;
|
||||
__asm__ __volatile__ (
|
||||
"cld\n\t"
|
||||
"rep movsb"
|
||||
: "=&c" (r0), "=&S" (r1), "=&D" (r2)
|
||||
: "2" (dest), "1" (src), "0" (n)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void memcpy32(void * dest, const void * src, size_t count)
|
||||
{
|
||||
uint32_t r0, r1, r2;
|
||||
__asm__ __volatile__ (
|
||||
"cld\n\t"
|
||||
"rep movsd"
|
||||
: "=&c" (r0), "=&S" (r1), "=&D" (r2)
|
||||
: "2" (dest), "1" (src), "0" (count)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
static inline void * memmove(void * dest, const void * src, size_t count)
|
||||
{
|
||||
return __builtin_memmove(dest, src, count);
|
||||
}
|
||||
|
||||
static inline void * memset(void * dest, int val, size_t count)
|
||||
{
|
||||
return __builtin_memset(dest, val, count);
|
||||
}
|
||||
|
||||
static inline void memset32(void * dest, uint32_t val, size_t count)
|
||||
{
|
||||
uint32_t r0, r1;
|
||||
__asm__ __volatile__ (
|
||||
"cld\n\t"
|
||||
"rep stosl"
|
||||
: "=&c" (r0), "=&D" (r1)
|
||||
: "a" (val), "1" (dest), "0" (count)
|
||||
: "memory");
|
||||
}
|
||||
|
||||
#endif
|
91
src/mm.c
Normal file
91
src/mm.c
Normal file
@ -0,0 +1,91 @@
|
||||
#include "mm.h"
|
||||
|
||||
typedef struct mm_page_entry_s {
|
||||
size_t count;
|
||||
struct mm_page_entry_s * next;
|
||||
} mm_region_entry_t;
|
||||
|
||||
static mm_region_entry_t * mm_next_free_region;
|
||||
static size_t mm_free_pages;
|
||||
static size_t mm_total_ram;
|
||||
static size_t kernel_start_address;
|
||||
static size_t kernel_size;
|
||||
|
||||
extern uint8_t _hos_mem_start;
|
||||
extern uint8_t _hos_mem_end;
|
||||
|
||||
static void mm_add_ram_region(size_t base, size_t size)
|
||||
{
|
||||
mm_region_entry_t * region = (mm_region_entry_t *)base;
|
||||
size_t pages = size / PAGE_SIZE;
|
||||
region->count = pages;
|
||||
region->next = mm_next_free_region;
|
||||
mm_next_free_region = region;
|
||||
mm_total_ram += size;
|
||||
mm_free_pages = pages;
|
||||
}
|
||||
|
||||
void mm_register_ram_region(uint64_t base, size_t size)
|
||||
{
|
||||
/* Ignore any RAM region above 4GB. */
|
||||
if (base >= 0x100000000ull)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if ((base + size) > 0x100000000ull)
|
||||
{
|
||||
size = (size_t)(0x100000000ull - base);
|
||||
}
|
||||
size_t end_address = mm_page_floor((size_t)base + size);
|
||||
size_t start_address = mm_page_ceil(base);
|
||||
size = end_address - start_address;
|
||||
if (size < PAGE_SIZE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
size_t kernel_end_address = kernel_start_address + kernel_size;
|
||||
/* Add regions before and after kernel RAM. */
|
||||
if (start_address < kernel_start_address)
|
||||
{
|
||||
/* RAM region begins before kernel RAM. */
|
||||
size_t this_sz = size;
|
||||
if ((start_address + this_sz) > kernel_start_address)
|
||||
{
|
||||
this_sz = kernel_start_address - start_address;
|
||||
}
|
||||
mm_add_ram_region(start_address, this_sz);
|
||||
}
|
||||
if ((start_address + size) > kernel_end_address)
|
||||
{
|
||||
/* RAM region ends after kernel RAM. */
|
||||
size_t this_sz = size;
|
||||
if (start_address < kernel_end_address)
|
||||
{
|
||||
this_sz = (start_address + size) - kernel_end_address;
|
||||
start_address = kernel_end_address;
|
||||
}
|
||||
mm_add_ram_region(start_address, this_sz);
|
||||
}
|
||||
}
|
||||
|
||||
void mm_init(void)
|
||||
{
|
||||
kernel_start_address = mm_page_floor((size_t)&_hos_mem_start);
|
||||
kernel_size = mm_page_ceil((size_t)&_hos_mem_end - kernel_start_address);
|
||||
mm_total_ram = kernel_size;
|
||||
}
|
||||
|
||||
size_t mm_get_total_ram(void)
|
||||
{
|
||||
return mm_total_ram;
|
||||
}
|
||||
|
||||
size_t mm_get_kernel_address(void)
|
||||
{
|
||||
return kernel_start_address;
|
||||
}
|
||||
|
||||
size_t mm_get_kernel_size(void)
|
||||
{
|
||||
return kernel_size;
|
||||
}
|
25
src/mm.h
Normal file
25
src/mm.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef MM_H
|
||||
#define MM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define PAGE_SIZE 4096u
|
||||
|
||||
static inline size_t mm_page_floor(size_t bytes)
|
||||
{
|
||||
return bytes & ~(PAGE_SIZE - 1u);
|
||||
}
|
||||
|
||||
static inline size_t mm_page_ceil(size_t bytes)
|
||||
{
|
||||
return (bytes + PAGE_SIZE - 1u) & ~(PAGE_SIZE - 1u);
|
||||
}
|
||||
|
||||
void mm_init(void);
|
||||
void mm_register_ram_region(uint64_t base, size_t size);
|
||||
size_t mm_get_total_ram(void);
|
||||
size_t mm_get_kernel_address(void);
|
||||
size_t mm_get_kernel_size(void);
|
||||
|
||||
#endif
|
127
src/multiboot2.h
Normal file
127
src/multiboot2.h
Normal file
@ -0,0 +1,127 @@
|
||||
#ifndef MULTIBOOT2_H
|
||||
#define MULTIBOOT2_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define MULTIBOOT2_MAGIC 0xE85250D6u
|
||||
#define MULTIBOOT2_ARCHITECTURE_I386 0u
|
||||
|
||||
typedef struct {
|
||||
uint32_t magic;
|
||||
uint32_t architecture;
|
||||
uint32_t header_length;
|
||||
uint32_t checksum;
|
||||
} multiboot2_header_t;
|
||||
#define multiboot2_header(magic, architecture, header_length) \
|
||||
{(magic), (architecture), (header_length), (uint32_t)(0x100000000u - (magic) - (architecture) - (header_length))}
|
||||
#define multiboot2_header_default() \
|
||||
multiboot2_header(MULTIBOOT2_MAGIC, MULTIBOOT2_ARCHITECTURE_I386, sizeof(multiboot2_header_t))
|
||||
|
||||
typedef struct {
|
||||
uint16_t type;
|
||||
uint16_t flags;
|
||||
uint32_t size;
|
||||
} multiboot2_tag_t;
|
||||
#define multiboot2_end_tag() {0u, 0u, 8u}
|
||||
|
||||
typedef struct {
|
||||
multiboot2_tag_t header;
|
||||
uint32_t width;
|
||||
uint32_t height;
|
||||
uint32_t depth;
|
||||
uint32_t _padding;
|
||||
} multiboot2_framebuffer_tag_t;
|
||||
#define multiboot2_framebuffer_tag(width, height, depth) \
|
||||
{{5u, 0u, 20u}, width, height, depth}
|
||||
|
||||
#define MULTIBOOT2_INFO_TAG_ALIGNMENT 8u
|
||||
#define MULTIBOOT2_INFO_BOOT_COMMAND_LINE 1u
|
||||
#define MULTIBOOT2_INFO_BOOT_LOADER_NAME 2u
|
||||
#define MULTIBOOT2_INFO_MODULES 3u
|
||||
#define MULTIBOOT2_INFO_BASIC_MEMORY_INFO 4u
|
||||
#define MULTIBOOT2_INFO_BIOS_BOOT_DEVICE 5u
|
||||
#define MULTIBOOT2_INFO_MEMORY_MAP 6u
|
||||
#define MULTIBOOT2_INFO_VBE_INFO 7u
|
||||
#define MULTIBOOT2_INFO_FRAMEBUFFER_INFO 8u
|
||||
#define MULTIBOOT2_INFO_ELF_SYMBOLS 9u
|
||||
#define MULTIBOOT2_INFO_APM_TABLE 10u
|
||||
#define MULTIBOOT2_INFO_EFI_32BIT_SYSTEM_TABLE_POINTER 11u
|
||||
#define MULTIBOOT2_INFO_EFI_64BIT_SYSTEM_TABLE_POINTER 12u
|
||||
#define MULTIBOOT2_INFO_SMBIOS_TABLES 13u
|
||||
#define MULTIBOOT2_INFO_ACPI_OLD_RSDP 14u
|
||||
#define MULTIBOOT2_INFO_NETWORKING_INFO 16u
|
||||
#define MULTIBOOT2_INFO_EFI_MEMORY_MAP 17u
|
||||
#define MULTIBOOT2_INFO_EFI_BOOT_SERVICES_NOT_TERMINATED 18u
|
||||
#define MULTIBOOT2_INFO_EFI_32BIT_IMAGE_HANDLE_POINTER 19u
|
||||
#define MULTIBOOT2_INFO_EFI_64BIT_IMAGE_HANDLE_POINTER 20u
|
||||
#define MULTIBOOT2_INFO_IMAGE_LOAD_BASE_PHYSICAL_ADDRESS 21u
|
||||
|
||||
typedef struct {
|
||||
uint32_t total_size;
|
||||
uint32_t _reserved;
|
||||
} multiboot2_info_header_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t type;
|
||||
uint32_t size;
|
||||
} multiboot2_info_tag_t;
|
||||
#define multiboot2_info_next_tag(current_tag) \
|
||||
(multiboot2_info_tag_t *)(((uintptr_t)current_tag + current_tag->size + MULTIBOOT2_INFO_TAG_ALIGNMENT - 1u) & ~(MULTIBOOT2_INFO_TAG_ALIGNMENT - 1u))
|
||||
|
||||
typedef struct {
|
||||
multiboot2_info_tag_t header;
|
||||
char string[];
|
||||
} multiboot2_info_boot_command_line_t;
|
||||
|
||||
typedef struct {
|
||||
multiboot2_info_tag_t header;
|
||||
char string[];
|
||||
} multiboot2_info_boot_loader_name_t;
|
||||
|
||||
typedef struct {
|
||||
multiboot2_info_tag_t header;
|
||||
uint32_t mem_lower;
|
||||
uint32_t mem_upper;
|
||||
} multiboot2_info_basic_memory_info_t;
|
||||
|
||||
#define MULTIBOOT2_MEMORY_MAP_TYPE_RAM 1u
|
||||
#define MULTIBOOT2_MEMORY_MAP_TYPE_ACPI 3u
|
||||
#define MULTIBOOT2_MEMORY_MAP_TYPE_PRESERVE 4u
|
||||
#define MULTIBOOT2_MEMORY_MAP_TYPE_DEFECTIVE 5u
|
||||
|
||||
typedef struct {
|
||||
uint64_t base_addr;
|
||||
uint64_t length;
|
||||
uint32_t type;
|
||||
uint32_t _reserved;
|
||||
} multiboot2_info_memory_map_entry_t;
|
||||
|
||||
typedef struct {
|
||||
multiboot2_info_tag_t header;
|
||||
uint32_t entry_size;
|
||||
uint32_t entry_version;
|
||||
multiboot2_info_memory_map_entry_t entries[];
|
||||
} multiboot2_info_memory_map_t;
|
||||
|
||||
typedef struct {
|
||||
multiboot2_info_tag_t header;
|
||||
uint16_t vbe_mode;
|
||||
uint16_t vbe_interface_set;
|
||||
uint16_t vbe_interface_off;
|
||||
uint16_t vbe_interface_len;
|
||||
uint8_t vbe_control_info[512];
|
||||
uint8_t vbe_mode_info[256];
|
||||
} multiboot2_info_vbe_info_t;
|
||||
|
||||
typedef struct {
|
||||
multiboot2_info_tag_t header;
|
||||
uint64_t framebuffer_addr;
|
||||
uint32_t framebuffer_pitch;
|
||||
uint32_t framebuffer_width;
|
||||
uint32_t framebuffer_height;
|
||||
uint8_t framebuffer_bpp;
|
||||
uint8_t framebuffer_type;
|
||||
uint8_t _reserved;
|
||||
} multiboot2_info_framebuffer_info_t;
|
||||
|
||||
#endif
|
12
src/multiboot_header.c
Normal file
12
src/multiboot_header.c
Normal file
@ -0,0 +1,12 @@
|
||||
#include "multiboot2.h"
|
||||
|
||||
/* Multiboot2 Header. */
|
||||
struct {
|
||||
multiboot2_header_t header;
|
||||
multiboot2_framebuffer_tag_t framebuffer_tag;
|
||||
multiboot2_tag_t end_tag;
|
||||
} multiboot_header __attribute__((section(".multiboot"))) = {
|
||||
multiboot2_header_default(),
|
||||
multiboot2_framebuffer_tag(1600u, 900u, 32u),
|
||||
multiboot2_end_tag(),
|
||||
};
|
12
src/stream.h
Normal file
12
src/stream.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef STREAM_H
|
||||
#define STREAM_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct {
|
||||
void (*write)(const char * src, size_t length);
|
||||
void (*write1)(char c);
|
||||
} stream_t;
|
||||
|
||||
#endif
|
26
src/string.h
Normal file
26
src/string.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef STRING_H
|
||||
#define STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
static inline size_t strlen(const char * s)
|
||||
{
|
||||
size_t r = 0u;
|
||||
while (*s++ != (char)0)
|
||||
{
|
||||
r++;
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline char * strcpy(char * dest, const char * src)
|
||||
{
|
||||
return __builtin_strcpy(dest, src);
|
||||
}
|
||||
|
||||
static inline char * strncpy(char * dest, const char * src, size_t n)
|
||||
{
|
||||
return __builtin_strncpy(dest, src, n);
|
||||
}
|
||||
|
||||
#endif
|
228
stage1.asm
228
stage1.asm
@ -1,228 +0,0 @@
|
||||
|
||||
%include "bootdef.inc"
|
||||
|
||||
[bits 16]
|
||||
|
||||
org 0x7c00
|
||||
|
||||
jmp short start
|
||||
|
||||
; --------------------------------------------------
|
||||
; data portion of the "DOS BOOT RECORD"
|
||||
; ----------------------------------------------------------------------
|
||||
brINT13Flag DB 90H ; 0002h - 0EH for INT13 AH=42 READ
|
||||
brOEM DB 'MSDOS5.0' ; 0003h - OEM ID - Windows 95B
|
||||
brBPS DW 512 ; 000Bh - Bytes per sector
|
||||
brSPC DB 1 ; 000Dh - Sector per cluster
|
||||
brSc_b4_fat DW 1 ; 000Eh - Reserved sectors
|
||||
brFATs DB 2 ; 0010h - FAT copies
|
||||
brRootEntries DW 0E0H ; 0011h - Root directory entries
|
||||
brSectorCount DW 2880 ; 0013h - Sectors in volume, < 32MB
|
||||
brMedia DB 240 ; 0015h - Media descriptor
|
||||
brSPF DW 9 ; 0016h - Sectors per FAT
|
||||
brSc_p_trk DW 18 ; 0018h - Sectors per head/track
|
||||
brHPC DW 2 ; 001Ah - Heads per cylinder
|
||||
brSc_b4_prt DD 0 ; 001Ch - Hidden sectors
|
||||
brSectors DD 0 ; 0020h - Total number of sectors
|
||||
brDrive DB 0 ; 0024h - Physical drive no.
|
||||
DB 0 ; 0025h - Reserved (FAT32)
|
||||
DB 29H ; 0026h - Extended boot record sig (FAT32)
|
||||
brSerialNum DD 404418EAH ; 0027h - Volume serial number
|
||||
brLabel DB 'HOS 0.1.1 ' ; 002Bh - Volume label
|
||||
brFSID DB 'FAT12 ' ; 0036h - File System ID
|
||||
;------------------------------------------------------------------------
|
||||
|
||||
start:
|
||||
jmp 0:jmphere ;ensure that cs=0 and ip=0x7c...
|
||||
jmphere:
|
||||
;dl=drive number, save it!
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
mov [brDrive], dl
|
||||
cli
|
||||
mov ss, ax
|
||||
mov sp, 0x7Bfe ;right under boot sector
|
||||
sti
|
||||
|
||||
mov ax, 0xb800
|
||||
mov ds, ax
|
||||
mov es, ax
|
||||
|
||||
xor di, di
|
||||
mov ax, 0x0700
|
||||
mov cx, 2000
|
||||
cls:
|
||||
stosw
|
||||
loop cls
|
||||
|
||||
enable_a20:
|
||||
in al, 0x64
|
||||
test al, 2
|
||||
jnz enable_a20
|
||||
mov al, 0xD1
|
||||
out 0x64, al
|
||||
ea20_2: in al, 0x64
|
||||
and ax, byte 2
|
||||
jnz ea20_2
|
||||
mov al, 0xDF
|
||||
out 0x60, al
|
||||
|
||||
unreal:
|
||||
xor ax, ax
|
||||
mov es, ax
|
||||
mov ds, ax
|
||||
|
||||
lgdt [gdtr] ;load gdt
|
||||
cli
|
||||
push es
|
||||
push ds ;save segment values
|
||||
mov ebx, cr0
|
||||
inc bl
|
||||
mov cr0, ebx ;pmode!
|
||||
mov ax, KERNEL_DATA
|
||||
mov es, ax
|
||||
mov ds, ax ;load segment limits
|
||||
dec bl
|
||||
mov cr0, ebx ;back to real mode!
|
||||
pop ds
|
||||
pop es ;segments back, with 4gb limits!
|
||||
sti
|
||||
|
||||
;now lets read in the FAT and root directory so we can search for the kernel file...
|
||||
mov ax, 0x0209 ;FAT1
|
||||
mov cx, 0x0002
|
||||
xor dh, dh
|
||||
mov dl, [brDrive]
|
||||
mov bx, BOOT_FAT_SEG
|
||||
mov es, bx
|
||||
xor bx, bx
|
||||
int 0x13
|
||||
|
||||
mov ax, 0x020E ;root directory
|
||||
mov cx, 0x0002 ;cyl/sect
|
||||
mov dh, 0x01 ;head
|
||||
mov dl, [brDrive] ;drive
|
||||
mov bx, BOOT_ROOT_SEG
|
||||
mov es, bx
|
||||
xor bx, bx
|
||||
int 0x13
|
||||
|
||||
;k now read root directory
|
||||
mov bx, BOOT_ROOT_SEG
|
||||
mov ds, bx
|
||||
xor si, si ;k now ds:si points to beginning of root directory
|
||||
mov es, si
|
||||
mov cx, 224 ;max root entries
|
||||
loop_compare:
|
||||
mov di, stage2
|
||||
push cx
|
||||
push si ;save pointer to root dir entry
|
||||
mov cx, 11
|
||||
loop_name:
|
||||
cmpsb
|
||||
loopz loop_name
|
||||
jnz goon ;cx didn't get to zero, bad file
|
||||
pop si
|
||||
pop cx
|
||||
jmp found_file ;good file, ds:si points to start of root directory entry
|
||||
goon:
|
||||
pop si
|
||||
pop cx
|
||||
add si, 32
|
||||
loop loop_compare
|
||||
|
||||
error:
|
||||
jmp $ ;halt! no kernel file found!
|
||||
|
||||
found_file: ;ds:si points to root dir entry
|
||||
xor ax, ax
|
||||
mov gs, ax
|
||||
mov ax, BOOT_STAGE2_SEG
|
||||
mov es, ax
|
||||
|
||||
mov ax, [ds:si+26]
|
||||
mov bx, BOOT_FAT_SEG
|
||||
mov ds, bx ;ds points to beginning of FAT
|
||||
xor di, di
|
||||
|
||||
readstage2_loop:
|
||||
cmp ax, 0xff7
|
||||
jg readstage2_done
|
||||
inc di
|
||||
push ax
|
||||
call getCHSfromCluster
|
||||
mov ax, 0x0201
|
||||
mov dl, [gs:BOOT_DRIVE]
|
||||
xor bx, bx
|
||||
int 0x13
|
||||
mov bx, es
|
||||
add bx, 0x0020
|
||||
mov es, bx
|
||||
pop ax ;current logical cluster #
|
||||
|
||||
mov cx, ax ;cx=logical cluster
|
||||
mov dx, 3
|
||||
mul dx
|
||||
shr ax, 1 ;ax=logical cluster * 3 / 2
|
||||
mov si, ax
|
||||
test cl, 1 ;is bit0 set?
|
||||
jnz odd_cluster
|
||||
even_cluster:
|
||||
lodsw
|
||||
and ax, 0x0fff
|
||||
jmp got_cluster
|
||||
odd_cluster:
|
||||
lodsw
|
||||
shr ax, 4
|
||||
got_cluster:
|
||||
jmp readstage2_loop
|
||||
|
||||
readstage2_done:
|
||||
|
||||
jmp 0:BOOT_STAGE2_ADD
|
||||
|
||||
;------------------------------------------------------
|
||||
getCHSfromCluster:
|
||||
;input: ax=lba of sector on floppy (0-2879)
|
||||
add ax, 31 ;convert logical cluster# to lba#
|
||||
xor dx, dx ;lba->chs
|
||||
mov bx, 18
|
||||
div bx
|
||||
inc dx
|
||||
mov cl, dl ;sector# (1-18)
|
||||
xor dx, dx
|
||||
mov bx, 2
|
||||
div bx
|
||||
mov ch, al ;cylinder# (0-79)
|
||||
mov dh, dl ;head# (0-1)
|
||||
ret
|
||||
|
||||
|
||||
;-------------------------------------------------------
|
||||
gdtr:
|
||||
dw gdt_end-gdt-1
|
||||
dd gdt
|
||||
gdt:
|
||||
dd 0
|
||||
dd 0
|
||||
|
||||
KERNEL_DATA equ $-gdt
|
||||
db 0xff ;segment 16 = 4gb data
|
||||
db 0xff
|
||||
db 0x00
|
||||
db 0x00
|
||||
db 0x00
|
||||
db 0x92
|
||||
db 0xcf ;cf
|
||||
db 0x00
|
||||
|
||||
gdt_end:
|
||||
|
||||
|
||||
|
||||
stage2: db "STAGE2 BIN"
|
||||
|
||||
times 510-($-$$) db 0
|
||||
|
||||
db 0x55, 0xaa
|
||||
|
920
stage2.asm
920
stage2.asm
@ -1,920 +0,0 @@
|
||||
|
||||
%include "bootdef.inc"
|
||||
|
||||
%define VESA_MODEINFO_SEG 0x0120
|
||||
%define VESA_MODELIST_SEG 0x0140
|
||||
%define GOOD_MODELIST_SEG 0x0160
|
||||
|
||||
[bits 16]
|
||||
|
||||
org BOOT_STAGE2_ADD
|
||||
|
||||
|
||||
;k now read root directory
|
||||
mov bx, BOOT_ROOT_SEG
|
||||
mov ds, bx
|
||||
xor si, si ;k now ds:si points to beginning of root directory
|
||||
mov es, si
|
||||
mov cx, 224 ;max root entries
|
||||
loop_compare:
|
||||
mov di, kernel
|
||||
push cx
|
||||
push si ;save pointer to root dir entry
|
||||
mov cx, 11
|
||||
loop_name:
|
||||
cmpsb
|
||||
loopz loop_name
|
||||
jnz goon ;cx didn't get to zero, bad file
|
||||
pop si
|
||||
pop cx
|
||||
jmp found_file ;good file, ds:si points to start of root directory entry
|
||||
goon:
|
||||
pop si
|
||||
pop cx
|
||||
loop loop_compare
|
||||
|
||||
error:
|
||||
jmp $ ;halt! no kernel file found!
|
||||
|
||||
found_file: ;ds:si points to root dir entry of kernel file
|
||||
xor ax, ax
|
||||
mov gs, ax
|
||||
mov ax, [ds:si+26]
|
||||
mov bx, BOOT_FAT_SEG
|
||||
mov ds, bx ;ds points to beginning of FAT
|
||||
mov edi, BOOT_KERNEL_ADD
|
||||
|
||||
readkernel_loop:
|
||||
cmp ax, 0xff7
|
||||
jg readkernel_done
|
||||
push ax
|
||||
call getCHSfromCluster
|
||||
mov ax, 0x0201
|
||||
mov dl, [gs:BOOT_DRIVE]
|
||||
mov bx, BOOT_KERNEL_SEG
|
||||
mov es, bx
|
||||
xor bx, bx
|
||||
int 0x13
|
||||
mov cx, 256
|
||||
xor ax, ax
|
||||
mov es, ax
|
||||
mov esi, BOOT_KERNEL_SEG*16
|
||||
copykernel_loop:
|
||||
mov ax, [es:esi]
|
||||
mov [es:edi], ax
|
||||
inc esi
|
||||
inc esi
|
||||
inc edi
|
||||
inc edi
|
||||
loop copykernel_loop
|
||||
|
||||
pop ax ;current logical cluster #
|
||||
|
||||
mov cx, ax ;cx=logical cluster
|
||||
mov dx, 3
|
||||
mul dx
|
||||
shr ax, 1 ;ax=logical cluster * 3 / 2
|
||||
mov si, ax
|
||||
test cl, 1 ;is bit0 set?
|
||||
jnz odd_cluster
|
||||
even_cluster:
|
||||
lodsw
|
||||
and ax, 0x0fff
|
||||
jmp got_cluster
|
||||
odd_cluster:
|
||||
lodsw
|
||||
shr ax, 4
|
||||
got_cluster:
|
||||
jmp readkernel_loop
|
||||
|
||||
;------------------------------------------------------
|
||||
readkernel_done: ;-------------put more real mode init stuff here!
|
||||
;----ask to load RD from floppy
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
xor di, di
|
||||
mov cx, 2000
|
||||
mov ax, 0x0700
|
||||
cls_loop:
|
||||
stosw
|
||||
loop cls_loop
|
||||
|
||||
mov dx, 0x3d4 ;move cursor off screen...
|
||||
mov al, 0x0e
|
||||
out dx, al
|
||||
inc dx
|
||||
mov al, 0xff
|
||||
out dx, al
|
||||
dec dx
|
||||
mov al, 0x0f
|
||||
out dx, al
|
||||
inc dx
|
||||
out dx, al
|
||||
|
||||
xor di, di
|
||||
mov si, txt_welcome
|
||||
mov ah, 0x1f
|
||||
call puts
|
||||
|
||||
mov di, 160
|
||||
mov si, txt_rd1
|
||||
mov ah, 7
|
||||
call puts
|
||||
|
||||
mov si, txt_rd2
|
||||
mov di, 160*2
|
||||
call puts
|
||||
|
||||
mov di, 160*3
|
||||
mov si, txt_input
|
||||
call puts
|
||||
|
||||
get_rd:
|
||||
xor ax, ax
|
||||
int 0x16
|
||||
cmp al, '1'
|
||||
jz got_rd
|
||||
cmp al, '2'
|
||||
jnz get_rd
|
||||
got_rd:
|
||||
stosb
|
||||
sub al, '1'
|
||||
push ds
|
||||
mov bx, BOOT_DATA_SEG ;segment for data to send kernel
|
||||
mov ds, bx
|
||||
mov [ds:BOOT_HASRD], al
|
||||
pop ds ;ds=0
|
||||
cmp al, 0 ;dont load rd
|
||||
jz no_rd
|
||||
|
||||
mov cx, 80
|
||||
mov edi, 0xb8000+160*4
|
||||
filler_loop:
|
||||
mov word [ds:edi], 0x0400+177
|
||||
inc edi
|
||||
inc edi
|
||||
loop filler_loop
|
||||
mov cx, 80 ;80 cylinders to read
|
||||
xor si, si
|
||||
mov edi, BOOT_RD_ADD ;ram disk address
|
||||
read_cylinder:
|
||||
push cx
|
||||
mov bx, 0x0100
|
||||
mov es, bx
|
||||
xor bx, bx
|
||||
mov ax, 0x0224
|
||||
mov cx, si
|
||||
mov ch, cl
|
||||
mov cl, 1
|
||||
xor dx, dx
|
||||
mov dl, [gs:BOOT_DRIVE]
|
||||
int 0x13
|
||||
|
||||
mov ebx, 0xb8000
|
||||
add bx, si
|
||||
shl bl, 1
|
||||
mov word [ds:ebx+160*4], 0x0200+219
|
||||
|
||||
push si
|
||||
mov esi, 0x1000
|
||||
mov cx, 0x2400
|
||||
copydisk_loop:
|
||||
mov ax, [ds:esi]
|
||||
inc esi
|
||||
inc esi
|
||||
mov [ds:edi], ax
|
||||
inc edi
|
||||
inc edi
|
||||
loop copydisk_loop
|
||||
|
||||
pop si ;what cylinder# we are on...
|
||||
inc si
|
||||
pop cx
|
||||
loop read_cylinder
|
||||
|
||||
;------------------------------------------------------
|
||||
no_rd:
|
||||
mov ax, BOOT_DATA_SEG
|
||||
mov es, ax
|
||||
mov di, BOOT_MEMMAP ;store memmap info in es:di for kernel
|
||||
xor edx, edx
|
||||
mov ax, 0x0100
|
||||
mov ds, ax
|
||||
xor ebx, ebx
|
||||
getmemmap_loop:
|
||||
push es
|
||||
push di
|
||||
push edx
|
||||
mov ax, 0x0100 ;get memory map
|
||||
mov es, ax
|
||||
xor di, di
|
||||
mov eax, 0x0000E820
|
||||
mov ecx, 0x00000014
|
||||
;mov ebx, 0x00000000
|
||||
mov edx, 0x534D4150 ;'SMAP'
|
||||
int 0x15
|
||||
jc getmemmap_carry
|
||||
cmp eax, 0x534D4150 ;eax should be 'SMAP' on return...
|
||||
jnz getmemmap_error
|
||||
cmp ebx, 0
|
||||
jz getmemmap_done
|
||||
|
||||
pop edx ;now, copy memmap entry to es:di on the stack
|
||||
pop di
|
||||
pop es
|
||||
xor si, si
|
||||
mov cx, 20
|
||||
getmemmap_copymem_loop:
|
||||
lodsb
|
||||
stosb
|
||||
loop getmemmap_copymem_loop
|
||||
inc edx
|
||||
jmp getmemmap_loop
|
||||
|
||||
getmemmap_error:
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
mov di, 160*24
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
mov si, txt_memerror
|
||||
mov ah, 0x04
|
||||
call puts
|
||||
hlt
|
||||
jmp $
|
||||
|
||||
getmemmap_carry:
|
||||
dec edx
|
||||
getmemmap_done:
|
||||
pop edx
|
||||
pop di
|
||||
pop es
|
||||
|
||||
xor si, si
|
||||
mov cx, 20
|
||||
getmemmap_done_copymem_loop:
|
||||
lodsb
|
||||
stosb
|
||||
loop getmemmap_done_copymem_loop
|
||||
inc edx
|
||||
|
||||
mov di, BOOT_MEMENTRIES
|
||||
mov [es:di], edx ;save # of memmap entries for kernel
|
||||
|
||||
;on to vesa info...
|
||||
xor ax, ax
|
||||
mov gs, ax
|
||||
mov ds, ax
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
mov di, 160
|
||||
mov cx, 2000-80
|
||||
mov ax, 0x0700
|
||||
cls_vesa_loop:
|
||||
stosw
|
||||
loop cls_vesa_loop
|
||||
mov si, txt_vesa
|
||||
mov di, 160*1
|
||||
mov ah, 7
|
||||
call puts
|
||||
|
||||
push di
|
||||
mov ax, 0x0100
|
||||
mov es, ax
|
||||
xor di, di
|
||||
mov dword [es:di], "2EBV"
|
||||
mov ax, 0x4F00
|
||||
|
||||
int 0x10
|
||||
pop di
|
||||
cmp ax, 0x004F
|
||||
jz vesa_good
|
||||
mov si, txt_novesa
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
mov ah, 7
|
||||
call puts
|
||||
mov ax, BOOT_DATA_SEG
|
||||
mov ds, ax
|
||||
mov word [ds:BOOT_VESA], 0
|
||||
jmp vesa_done
|
||||
vesa_good:
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
mov ax, 0x0100
|
||||
mov ds, ax
|
||||
xor si, si
|
||||
mov bx, [4]
|
||||
mov al, bh
|
||||
call puthex
|
||||
mov al, '.'
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
mov al, bl
|
||||
call puthex
|
||||
add di, 4
|
||||
cmp bh, 2
|
||||
jge vesa_good2
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
mov si, txt_vesaold
|
||||
mov ah, 7
|
||||
call puts
|
||||
mov ax, BOOT_DATA_SEG
|
||||
mov ds, ax
|
||||
mov word [ds:BOOT_VESA], 0
|
||||
jmp vesa_done
|
||||
vesa_good2:
|
||||
mov ebx, [6] ;something like 0x00000E60
|
||||
mov edx, [14]
|
||||
mov si, bx
|
||||
shr ebx, 16
|
||||
mov ds, bx ;ds:si points to null-terminated OEM identification string
|
||||
mov ah, 2
|
||||
push si
|
||||
call puts
|
||||
pop si
|
||||
mov ax, BOOT_DATA_SEG
|
||||
mov es, ax
|
||||
mov di, BOOT_VESA_OEM
|
||||
vesa_copyoem:
|
||||
lodsb
|
||||
stosb
|
||||
or al, al
|
||||
jnz vesa_copyoem
|
||||
mov ax, 0x0100
|
||||
mov ds, ax
|
||||
xor si, si
|
||||
mov di, BOOT_VESA_VBE
|
||||
mov cx, 512
|
||||
vesa_copyvbe:
|
||||
lodsb
|
||||
stosb
|
||||
loop vesa_copyvbe
|
||||
|
||||
mov si, dx
|
||||
shr edx, 16
|
||||
mov ds, dx ;ds:si points to video mode list
|
||||
mov ax, VESA_MODELIST_SEG
|
||||
mov es, ax
|
||||
xor di, di
|
||||
vesa_copymodes:
|
||||
lodsw
|
||||
stosw
|
||||
cmp ax, 0xffff
|
||||
jnz vesa_copymodes
|
||||
|
||||
mov ax, GOOD_MODELIST_SEG
|
||||
mov es, ax
|
||||
xor di, di
|
||||
mov cx, 256
|
||||
mov ax, 0xffff
|
||||
clear_good_mode_list_loop:
|
||||
stosw
|
||||
loop clear_good_mode_list_loop
|
||||
|
||||
mov ax, VESA_MODELIST_SEG
|
||||
mov ds, ax
|
||||
xor si, si ;ds:si points to video mode list where we can edit it :)
|
||||
mov ax, GOOD_MODELIST_SEG
|
||||
mov es, ax
|
||||
xor di, di
|
||||
xor dx, dx ;dx=what good mode # we are on
|
||||
vesa_readmodeinfo_loop:
|
||||
lodsw
|
||||
cmp ax, 0xffff
|
||||
jz vesa_endofmodes
|
||||
push ax ;save mode#
|
||||
call checkmode
|
||||
cmp ax, 0
|
||||
jz vesa_readmodeinfo_good
|
||||
pop ax
|
||||
jmp vesa_readmodeinfo_loop
|
||||
vesa_readmodeinfo_good:
|
||||
pop ax ;restore mode#
|
||||
stosw
|
||||
call vesa_showmodeinfo
|
||||
inc dx
|
||||
jmp vesa_readmodeinfo_loop
|
||||
|
||||
vesa_endofmodes: ;here we have a list of good modes at GOOD_MODELIST_SEG:0
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
mov si, txt_consolemode
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
mov di, 160*2
|
||||
mov ah, 7
|
||||
call puts
|
||||
mov di, 160*3
|
||||
mov cx, dx
|
||||
mov al, 'b'
|
||||
vesa_displaylabels:
|
||||
stosb
|
||||
push ax
|
||||
mov al, 7
|
||||
stosb
|
||||
mov al, '.'
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
pop ax
|
||||
inc al
|
||||
add di, 160-4
|
||||
loop vesa_displaylabels ;done drawing screen of VESA choices, now ask for one
|
||||
;valid options are 'a' through (al-1)
|
||||
mov bl, al
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
mov di, 160*24
|
||||
mov si, txt_input
|
||||
mov ah, 14
|
||||
call puts
|
||||
vesa_getchoice:
|
||||
xor ax, ax
|
||||
int 0x16
|
||||
cmp al, 'a'
|
||||
jl vesa_getchoice
|
||||
cmp al, bl
|
||||
jge vesa_getchoice
|
||||
stosb
|
||||
push ax
|
||||
mov al, 14
|
||||
stosb
|
||||
pop ax
|
||||
xor ah, ah
|
||||
sub ax, 'a'
|
||||
cmp ax, 0
|
||||
jz vesa_consolemode_only
|
||||
mov cx, ax ;cx holds good mode# (1=first good vesa mode)
|
||||
dec cx
|
||||
mov ax, GOOD_MODELIST_SEG
|
||||
mov ds, ax
|
||||
shl cx, 1
|
||||
mov si, cx ;ds:si points to word containing selected mode#
|
||||
lodsw
|
||||
mov cx, ax
|
||||
mov dx, ax ;cx and dx hold mode#
|
||||
|
||||
mov ax, VESA_MODEINFO_SEG
|
||||
mov es, ax
|
||||
xor di, di
|
||||
mov ax, 0x4F01
|
||||
int 0x10
|
||||
call checkvesa
|
||||
|
||||
mov ax, VESA_MODEINFO_SEG
|
||||
mov ds, ax
|
||||
xor si, si
|
||||
mov ax, BOOT_DATA_SEG
|
||||
mov es, ax
|
||||
mov di, BOOT_VESA_INFO
|
||||
mov cx, 256
|
||||
vesa_copymodeinfo_loop:
|
||||
lodsb
|
||||
stosb
|
||||
loop vesa_copymodeinfo_loop ;store ModeInfoBlock for current
|
||||
|
||||
mov [es:BOOT_VESA], dx ;store mode# for kernel
|
||||
|
||||
; mov ax, 0xb800
|
||||
; mov es, ax
|
||||
; xor di, di
|
||||
; mov al, dh
|
||||
; call puthex2
|
||||
; mov al, dl
|
||||
; call puthex2
|
||||
|
||||
mov bx, dx
|
||||
or bx, 0x4000 ;set "use LFB" bit of mode#
|
||||
mov ax, 0x4F02
|
||||
int 0x10 ;switch to graphics mode!!!
|
||||
call checkvesa
|
||||
|
||||
jmp vesa_done
|
||||
|
||||
vesa_consolemode_only:
|
||||
mov ax, BOOT_DATA_SEG
|
||||
mov es, ax
|
||||
mov word [es:BOOT_VESA], 0
|
||||
|
||||
jmp vesa_done
|
||||
|
||||
;------------------------------------------------------
|
||||
vesa_showmodeinfo:
|
||||
pusha
|
||||
push es
|
||||
push ds
|
||||
mov ax, VESA_MODEINFO_SEG
|
||||
mov ds, ax
|
||||
xor si, si
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
|
||||
mov cx, dx
|
||||
|
||||
mov ax, 160
|
||||
mul dx ;ax=160*mod#
|
||||
add ax, 160*3+6 ;offset first line of modes and room on left for label
|
||||
mov di, ax
|
||||
|
||||
mov ax, [ds:18]
|
||||
call console_putDec
|
||||
add di, 10
|
||||
mov al, 'x'
|
||||
call console_putChar
|
||||
inc di
|
||||
inc di
|
||||
mov ax, [ds:20]
|
||||
call console_putDec
|
||||
add di, 10
|
||||
mov al, 'x'
|
||||
call console_putChar
|
||||
inc di
|
||||
inc di
|
||||
xor ah, ah
|
||||
mov al, [ds:25]
|
||||
call console_putDec
|
||||
add di, 8
|
||||
mov al, [ds:0]
|
||||
test al, 0x80
|
||||
jz vesa_showmodeinfo_done
|
||||
mov al, 'L'
|
||||
call console_putChar
|
||||
mov al, 'F'
|
||||
call console_putChar
|
||||
mov al, 'B'
|
||||
call console_putChar
|
||||
inc di
|
||||
inc di
|
||||
mov ebx, [ds:40]
|
||||
mov eax, ebx
|
||||
shr eax, 24
|
||||
call puthex2
|
||||
mov eax, ebx
|
||||
shr eax, 16
|
||||
call puthex2
|
||||
mov al, bh
|
||||
call puthex2
|
||||
mov al, bl
|
||||
call puthex2
|
||||
vesa_showmodeinfo_done:
|
||||
pop ds
|
||||
pop es
|
||||
popa
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
checkmode:
|
||||
push bx
|
||||
push cx
|
||||
push dx
|
||||
push es
|
||||
push ds
|
||||
push di
|
||||
push si
|
||||
mov cx, ax ;cx=modenumber
|
||||
mov ax, VESA_MODEINFO_SEG
|
||||
mov es, ax
|
||||
xor di, di
|
||||
mov ax, 0x4F01
|
||||
int 0x10
|
||||
call checkvesa
|
||||
xor di, di ;es:di -> ModeInfoBlock struc
|
||||
mov ax, [es:di] ;ModeAttributes
|
||||
test al, 1 ;mode supported
|
||||
jz vesa_modenogood
|
||||
test al, 8 ;color mode
|
||||
jz vesa_modenogood
|
||||
test al, 0x10 ;graphics mode
|
||||
jz vesa_modenogood
|
||||
test al, 0x80 ;Linear Frame Buffer supported
|
||||
jz vesa_modenogood
|
||||
mov al, [es:di+25] ;BitsPerPixel
|
||||
cmp al, 16
|
||||
jz vesa_bppok
|
||||
cmp al, 24
|
||||
jz vesa_bppok
|
||||
cmp al, 32
|
||||
jnz vesa_modenogood
|
||||
vesa_bppok:
|
||||
mov ax, [es:di+18] ;XResolution
|
||||
mov bx, [es:di+20] ;YResolution
|
||||
cmp ax, 640 ;640x480
|
||||
jnz res_goon1
|
||||
cmp bx, 480
|
||||
jnz vesa_modenogood
|
||||
jmp vesa_modegood
|
||||
res_goon1:
|
||||
cmp ax, 800
|
||||
jnz res_goon2
|
||||
cmp bx, 600
|
||||
jnz vesa_modenogood
|
||||
jmp vesa_modegood
|
||||
res_goon2:
|
||||
cmp ax, 1024
|
||||
jnz res_goon3
|
||||
cmp bx, 768
|
||||
jnz vesa_modenogood
|
||||
jmp vesa_modegood
|
||||
res_goon3:
|
||||
cmp ax, 1280
|
||||
jnz res_goon4
|
||||
cmp bx, 1024
|
||||
jz vesa_modegood
|
||||
cmp bx, 960
|
||||
jz vesa_modegood
|
||||
jmp vesa_modenogood
|
||||
res_goon4:
|
||||
cmp ax, 1600
|
||||
jnz vesa_modenogood
|
||||
cmp bx, 1200
|
||||
jnz vesa_modenogood
|
||||
vesa_modegood:
|
||||
pop si
|
||||
pop di
|
||||
pop ds
|
||||
pop es
|
||||
pop dx
|
||||
pop cx
|
||||
pop bx
|
||||
xor ax, ax
|
||||
ret
|
||||
vesa_modenogood:
|
||||
pop si
|
||||
pop di
|
||||
pop ds
|
||||
pop es
|
||||
pop dx
|
||||
pop cx
|
||||
pop bx
|
||||
mov ax, 0xffff
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
vesa_done:
|
||||
|
||||
; xor ax, ax ;wait for keypress...
|
||||
; int 0x16
|
||||
|
||||
jmp go_pm
|
||||
|
||||
;------------------------------------------------------
|
||||
puts:
|
||||
lodsb
|
||||
or al, al
|
||||
jz puts_done
|
||||
stosb
|
||||
mov al, ah
|
||||
stosb
|
||||
jmp puts
|
||||
puts_done:
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
checkvesa:
|
||||
cmp ax, 0x004F
|
||||
jnz vesaerror
|
||||
ret
|
||||
vesaerror:
|
||||
mov ax, 0xb800
|
||||
mov es, ax
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
mov si, txt_vesaerror
|
||||
mov di, 160*24
|
||||
mov ah, 4
|
||||
call puts
|
||||
cli
|
||||
hlt
|
||||
|
||||
;-------Function console_putDec
|
||||
;input:
|
||||
; AX = number to display
|
||||
;output:
|
||||
; number written in decimal to es:di
|
||||
console_putDec:
|
||||
pusha
|
||||
xor dx, dx
|
||||
xor bh, bh ;no characters written yet
|
||||
mov cx, 10000
|
||||
div cx ;ax=quotiont, dx=remainder
|
||||
add ax, '0'
|
||||
cmp ax, '0'
|
||||
je .goon1
|
||||
call console_putChar
|
||||
mov bh, 1
|
||||
|
||||
.goon1:
|
||||
mov ax, dx ;load remainder to ax
|
||||
xor dx, dx
|
||||
mov cx, 1000
|
||||
div cx ;ax=quotiont, dx=remainder
|
||||
add ax, '0'
|
||||
cmp ax, '0'
|
||||
je .goon11
|
||||
call console_putChar
|
||||
mov bh, 1
|
||||
jmp .goon2
|
||||
.goon11:
|
||||
cmp bh, 0
|
||||
je .goon2
|
||||
call console_putChar
|
||||
|
||||
.goon2:
|
||||
mov ax, dx ;load remainder to ax
|
||||
xor dx, dx
|
||||
mov cx, 100
|
||||
div cx ;ax=quotiont, dx=remainder
|
||||
add ax, '0'
|
||||
cmp ax, '0'
|
||||
je .goon21
|
||||
call console_putChar
|
||||
mov bh, 1
|
||||
jmp .goon3
|
||||
.goon21:
|
||||
cmp bh, 0
|
||||
je .goon3
|
||||
call console_putChar
|
||||
|
||||
.goon3:
|
||||
mov ax, dx ;load remainder to ax
|
||||
xor dx, dx
|
||||
mov cx, 10
|
||||
div cx ;ax=quotiont, dx=remainder
|
||||
add ax, '0'
|
||||
cmp ax, '0'
|
||||
je .goon31
|
||||
call console_putChar
|
||||
mov bh, 1
|
||||
jmp .goon4
|
||||
.goon31:
|
||||
cmp bh, 0
|
||||
je .goon4
|
||||
call console_putChar
|
||||
.goon4: ;here dx contains last remainder
|
||||
mov ax, dx
|
||||
add ax, '0'
|
||||
call console_putChar
|
||||
|
||||
popa
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
console_putChar:
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
puthex:
|
||||
;es:di points to video memory
|
||||
;al holds hex value
|
||||
|
||||
push ax
|
||||
mov ah, al
|
||||
shr ax, 4
|
||||
and al, 0x0F
|
||||
add al, '0'
|
||||
cmp al, '9'
|
||||
jle puthex_goon1
|
||||
add al, 'A'-'9'-1
|
||||
puthex_goon1:
|
||||
cmp al, '0'
|
||||
jz puthex_skipzero
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
puthex_skipzero:
|
||||
pop ax
|
||||
push ax
|
||||
and al, 0x0F
|
||||
add al, '0'
|
||||
cmp al, '9'
|
||||
jle puthex_goon2
|
||||
add al, 'A'-'9'-1
|
||||
puthex_goon2:
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
pop ax
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
puthex2:
|
||||
;es:di points to video memory, always displays 2 characters!
|
||||
;al holds hex value
|
||||
|
||||
push ax
|
||||
mov ah, al
|
||||
shr ax, 4
|
||||
and al, 0x0F
|
||||
add al, '0'
|
||||
cmp al, '9'
|
||||
jle puthex2_goon1
|
||||
add al, 'A'-'9'-1
|
||||
puthex2_goon1:
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
pop ax
|
||||
push ax
|
||||
and al, 0x0F
|
||||
add al, '0'
|
||||
cmp al, '9'
|
||||
jle puthex2_goon2
|
||||
add al, 'A'-'9'-1
|
||||
puthex2_goon2:
|
||||
stosb
|
||||
mov al, 7
|
||||
stosb
|
||||
pop ax
|
||||
ret
|
||||
|
||||
;------------------------------------------------------
|
||||
txt_welcome: db " Welcome to HOS v", VERSION, "! ", 0
|
||||
txt_rd1: db "1. Do not load an initial ram disk", 0
|
||||
txt_rd2: db "2. Load initial ram disk from floppy", 0
|
||||
txt_input: db "Enter your selection: ", 0
|
||||
txt_vesa: db "VESA version: ", 0
|
||||
txt_vesaerror: db "VESA function call error! Halting system!", 0
|
||||
txt_novesa: db "VESA not found. Starting in console mode...", 0
|
||||
txt_vesaold: db "VESA version 2.0 required. Starting in console mode...", 0
|
||||
txt_consolemode: db "a. Console mode only.", 0
|
||||
txt_memerror: db "Extended Memory Map information unavailable! Halting system...", 0
|
||||
|
||||
;------------------------------------------------------
|
||||
getCHSfromCluster:
|
||||
;input: ax=lba of sector on floppy (0-2879)
|
||||
add ax, 31 ;convert logical cluster# to lba#
|
||||
xor dx, dx ;lba->chs
|
||||
mov bx, 18
|
||||
div bx
|
||||
inc dx
|
||||
mov cl, dl ;sector# (1-18)
|
||||
xor dx, dx
|
||||
mov bx, 2
|
||||
div bx
|
||||
mov ch, al ;cylinder# (0-79)
|
||||
mov dh, dl ;head# (0-1)
|
||||
ret
|
||||
|
||||
|
||||
;-------------------------------------------------------
|
||||
gdtr:
|
||||
dw gdt_end-gdt-1
|
||||
dd gdt
|
||||
gdt:
|
||||
dd 0
|
||||
dd 0
|
||||
|
||||
KERNEL_CODE equ $-gdt
|
||||
db 0xff ;limit 7:0
|
||||
db 0xff ;limit 15:8
|
||||
db 0x00 ;base 7:0
|
||||
db 0x00 ;base 15:8
|
||||
db 0x00 ;base 23:16
|
||||
db 0x9a ;access
|
||||
db 0xcf ;flags / limit 19:16
|
||||
db 0x00 ;base 31:24
|
||||
|
||||
KERNEL_DATA equ $-gdt
|
||||
db 0xff ;segment 16 = 4gb data
|
||||
db 0xff
|
||||
db 0x00
|
||||
db 0x00
|
||||
db 0x00
|
||||
db 0x92
|
||||
db 0xcf ;cf
|
||||
db 0x00
|
||||
|
||||
gdt_end:
|
||||
|
||||
;------------------------------------------------------
|
||||
go_pm:
|
||||
xor ax, ax
|
||||
mov ds, ax
|
||||
lgdt [gdtr]
|
||||
cli
|
||||
mov eax, cr0
|
||||
inc eax
|
||||
mov cr0, eax
|
||||
|
||||
jmp KERNEL_CODE:pmode
|
||||
|
||||
bits 32
|
||||
pmode:
|
||||
mov ax, KERNEL_DATA
|
||||
mov es, ax
|
||||
mov ds, ax
|
||||
mov fs, ax
|
||||
mov gs, ax
|
||||
jmp KERNEL_CODE:BOOT_KERNEL_ADD
|
||||
|
||||
kernel: db "KERNEL BIN", 0
|
||||
|
||||
|
13
string.c
13
string.c
@ -1,13 +0,0 @@
|
||||
|
||||
#include "string.h"
|
||||
|
||||
int strlen(char *str)
|
||||
{
|
||||
int retn = 0;
|
||||
while (*str++ != 0)
|
||||
retn++;
|
||||
return retn;
|
||||
}
|
||||
|
||||
|
||||
|
8
string.h
8
string.h
@ -1,8 +0,0 @@
|
||||
|
||||
#ifndef __HSTRING_H__
|
||||
#define __HSTRING_H__ __HSTRING_H__
|
||||
|
||||
int strlen(char *str);
|
||||
|
||||
#endif
|
||||
|
151
video.c
151
video.c
@ -1,151 +0,0 @@
|
||||
//video.c
|
||||
// 08/13/03 Josh Holtrop
|
||||
|
||||
ModeInfoBlock video_mode;
|
||||
byte *vid_ptr24;
|
||||
word *vid_ptr16;
|
||||
dword *vid_ptr32;
|
||||
|
||||
void video_init(ModeInfoBlock *mib)
|
||||
{
|
||||
video_mode = *mib;
|
||||
|
||||
switch(video_mode.BitsPerPixel)
|
||||
{
|
||||
case 16:
|
||||
vid_ptr16 = (word *) video_mode.PhysBasePtr;
|
||||
break;
|
||||
case 24:
|
||||
vid_ptr24 = (byte *) video_mode.PhysBasePtr;
|
||||
break;
|
||||
case 32:
|
||||
vid_ptr32 = (dword *) video_mode.PhysBasePtr;
|
||||
}
|
||||
|
||||
dword tot = ((video_mode.XResolution) * (video_mode.YResolution));
|
||||
int a;
|
||||
for (a = 0; a < tot; a++)
|
||||
{
|
||||
if (a < (tot / 4))
|
||||
video_psetp(a, 0x00FF0000);
|
||||
else if (a < (tot / 2))
|
||||
video_psetp(a, 0x0000FF00);
|
||||
else if (a < ((tot * 3) / 4))
|
||||
video_psetp(a, 0x000000FF);
|
||||
else
|
||||
video_psetp(a, 0x00FFFFFF);
|
||||
}
|
||||
}
|
||||
|
||||
void video_horiz(int y, int x1, int x2, dword color)
|
||||
{
|
||||
checkBoundsy(y);
|
||||
checkBoundsx(x1);
|
||||
checkBoundsx(x2);
|
||||
if (x1 > x2)
|
||||
{
|
||||
int tmp = x2;
|
||||
x2 = x1;
|
||||
x1 = tmp;
|
||||
}
|
||||
int pixel = y*video_mode.XResolution+x1;
|
||||
int a;
|
||||
for (a = 0; a <= (x2-x1); a++)
|
||||
{
|
||||
video_psetp(pixel, color);
|
||||
pixel++;
|
||||
}
|
||||
}
|
||||
|
||||
void video_vert(int x, int y1, int y2, dword color)
|
||||
{
|
||||
checkBoundsx(x);
|
||||
checkBoundsy(y1);
|
||||
checkBoundsy(y2);
|
||||
if (y1 > y2)
|
||||
{
|
||||
int tmp = y2;
|
||||
y2 = y1;
|
||||
y1 = tmp;
|
||||
}
|
||||
int pixel = y1*video_mode.XResolution+x;
|
||||
int a;
|
||||
for (a = 0; a <= (y2-y1); a++)
|
||||
{
|
||||
video_psetp(pixel, color);
|
||||
pixel+=video_mode.XResolution;
|
||||
}
|
||||
}
|
||||
|
||||
void video_rect(int x1, int y1, int x2, int y2, dword color)
|
||||
{
|
||||
checkBoundsx(x1);
|
||||
checkBoundsx(x2);
|
||||
checkBoundsy(y1);
|
||||
checkBoundsy(y2);
|
||||
if (x2 < x1)
|
||||
{
|
||||
int tmp = x2;
|
||||
x2 = x1;
|
||||
x1 = tmp;
|
||||
}
|
||||
if (y2 < y1)
|
||||
{
|
||||
int tmp = y2;
|
||||
y2 = y1;
|
||||
y1 = tmp;
|
||||
}
|
||||
video_horiz(y1, x1, x2, color);
|
||||
video_horiz(y2, x1, x2, color);
|
||||
video_vert(x1, y1, y2, color);
|
||||
video_vert(x2, y1, y2, color);
|
||||
}
|
||||
|
||||
void video_rectf(int x1, int y1, int x2, int y2, dword color)
|
||||
{
|
||||
checkBoundsx(x1);
|
||||
checkBoundsx(x2);
|
||||
checkBoundsy(y1);
|
||||
checkBoundsy(y2);
|
||||
if (x2 < x1)
|
||||
{
|
||||
int tmp = x2;
|
||||
x2 = x1;
|
||||
x1 = tmp;
|
||||
}
|
||||
if (y2 < y1)
|
||||
{
|
||||
int tmp = y2;
|
||||
y2 = y1;
|
||||
y1 = tmp;
|
||||
}
|
||||
int a;
|
||||
for (a = 0; a <= (y2-y1); a++)
|
||||
video_horiz(y1+a, x1, x2, color);
|
||||
}
|
||||
|
||||
inline void video_pset(int x, int y, dword color)
|
||||
{
|
||||
video_psetp(y*video_mode.XResolution+x, color);
|
||||
}
|
||||
|
||||
void video_psetp(int pixel, dword color)
|
||||
{
|
||||
switch(video_mode.BitsPerPixel)
|
||||
{
|
||||
case 16:
|
||||
vid_ptr16[pixel] = ((color&0xFF)>>3) | ((((color>>8)&0xFF)>>2)<<6) | ((((color>>16)&0xFF)>>3)<<11);
|
||||
//vid_ptr16[pixel] = ((color&0xFF)>>3) | ((((color>>8)&0xFF)>>3)<<5) | ((((color>>16)&0xFF)>>3)<<10);
|
||||
break;
|
||||
case 24:
|
||||
vid_ptr24[pixel*3] = color & 0xFF;
|
||||
vid_ptr24[pixel*3+1] = (color>>8) & 0xFF;
|
||||
vid_ptr24[pixel*3+2] = (color>>16) & 0xFF;
|
||||
break;
|
||||
case 32:
|
||||
vid_ptr32[pixel] = color;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
59
video.h
59
video.h
@ -1,59 +0,0 @@
|
||||
//video.h
|
||||
// 08/18/03 Josh Holtrop
|
||||
|
||||
void video_init();
|
||||
void video_horiz(int y, int x1, int x2, dword color);
|
||||
void video_vert(int x, int y1, int y2, dword color);
|
||||
void video_rect(int x1, int y1, int x2, int y2, dword color);
|
||||
void video_rectf(int x1, int y1, int x2, int y2, dword color);
|
||||
inline void video_pset(int x, int y, dword color);
|
||||
void video_psetp(int pixel, dword color);
|
||||
|
||||
|
||||
#define checkBoundsx(x) (x<0 ? x=0 : (x>=video_mode.XResolution ? x=video_mode.XResolution-1 : 0))
|
||||
#define checkBoundsy(x) (x<0 ? x=0 : (x>=video_mode.YResolution ? x=video_mode.YResolution-1 : 0))
|
||||
|
||||
typedef struct{
|
||||
word ModeAttributes;
|
||||
byte WinAAttributes;
|
||||
byte WinBAttributes;
|
||||
word WinGranularity;
|
||||
word WinSize;
|
||||
word WinASegment;
|
||||
word WinBSegment;
|
||||
dword WinFuncPtr;
|
||||
word BytesPerScanLine;
|
||||
|
||||
word XResolution;
|
||||
word YResolution;
|
||||
byte XCharSize;
|
||||
byte YCharSize;
|
||||
byte NumberOfPlanes;
|
||||
byte BitsPerPixel;
|
||||
byte NumberOfBanks;
|
||||
byte MemoryModel;
|
||||
byte BankSize;
|
||||
byte NumberOfImagePages;
|
||||
byte Reserved1;
|
||||
|
||||
byte RedMaskSize;
|
||||
byte RedFieldPosition;
|
||||
byte GreenMaskSize;
|
||||
byte GreenFieldPosition;
|
||||
byte BlueMaskSize;
|
||||
byte BlueFieldPosition;
|
||||
byte RsvdMaskSize;
|
||||
byte RsvdFieldPosition;
|
||||
byte DirectColorModeInfo;
|
||||
|
||||
dword PhysBasePtr;
|
||||
dword OffScreenMemOffset;
|
||||
word OffScreenMemSize;
|
||||
byte Reserved[206];
|
||||
} ModeInfoBlock;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user