Initial commit

This commit is contained in:
vanten-s 2024-03-25 23:00:14 +01:00
commit c32493cdf2
Signed by: vanten-s
GPG key ID: DE3060396884D3F2
32 changed files with 2098687 additions and 0 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[unstable]
bindeps = true

1
.envrc Normal file
View file

@ -0,0 +1 @@
use_nix

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1078
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

14
Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[package]
name = "os"
version = "0.1.0"
[build-dependencies]
bootloader = "0.11"
kernel = { path = "kernel", artifact = "bin", target = "x86_64-unknown-none" }
[dependencies]
# used for UEFI booting in QEMU
ovmf-prebuilt = "0.1.0-alpha.1"
[workspace]
members = ["kernel"]

36
OST OS/info.txt Normal file
View file

@ -0,0 +1,36 @@
https://www.cs.bham.ac.uk/~exr/lectures/opsys/10_11/lectures/os-dev.pdf
CURRENT PAGE: 46
Name:
Obsidian Starlight OS
Compile Assembly:
cd "\Users\05wse01\Desktop\OST OS"
C:\Users\05wse01\AppData\Local\bin\NASM\nasm.exe boot_sect.asm -f bin -o boot_sect.bin
NOTE: The boot_sect.bin date wont change in the process... To clear out some confusions
Create VirtualBox disk:
Open admin cmd
cd "\Program Files\Oracle\VirtualBox"
VBoxManage.exe convertfromraw "C:\Users\05wse01\Desktop\OST OS\target" "C:\Users\05wse01\Desktop\OST OS\output.vdi" --format VDI
NOTE: Target has to be larger than 1mb (check file size in explorer)
AND: The file has to be a multiply of 512
AND: You need to set the the file as an available virtual disk in the VirtualBox interface (how?.. IDK)
Qemu emulator:
cd "C:\Program Files\qemu"
qemu-system-x86_64w.exe "C:\Users\05wse01\Desktop\OST OS\boot_sect.bin"
Compile C:
gcc -ffreestanding -c basic.c -o basic.o
ld -o basic.bin -Ttext 0x0 --oformat binary basic.o
ndisasm -b 32 basic.bin > basic.dis
Clean out rows with:
0000 add [eax],al

4
OST OS/kernel/basic.c Normal file
View file

@ -0,0 +1,4 @@
int my_function () {
int my_var = 0xbaba ;
return my_var ;
}

2097160
OST OS/kernel/basic.dis Normal file

File diff suppressed because it is too large Load diff

15
OST OS/old/temp.asm Normal file
View file

@ -0,0 +1,15 @@
[ org 0x7c00 ]
mov ebx, RANDOM_MSG,
call printLn
mov dx, 0x1fb6 ; store the value to print in dx
call print_hex ; call the function
%include "./print/print.asm"
%include "./print/print_hex.asm"
RANDOM_MSG: db 'Will this work?',0
times 510-($-$$) db 0 ; Pad the boot sector out with zeros
dw 0xaa55

0
OST OS/os/basic.dis Normal file
View file

View file

@ -0,0 +1,42 @@
[ org 0x7c00 ] ; Tells the assemble to start inside the boot location
mov [BOOT_DRIVE], dl
mov bp, 0x8000
mov sp, bp
mov ebx, BOOTING_MSG
mov edx, 2
call print
mov bx, 0x9000
mov dh, 5
mov dl, [BOOT_DRIVE]
call disk_load
mov dx, [0x9000]
call print_hex
mov dx, [0x9000 + 512]
call print_hex
jmp $
; Imports print
%include "./x16/print/print.asm"
%include "./x16/print/print_hex.asm"
%include "./x16/disk/disk_load.asm"
BOOT_DRIVE: db 0
BOOTING_MSG: db 'Booting Obsidian Starlight OS (OST OS)',0
RANDOM_MSG: db 'Ya like jazz?',0
times 510-($-$$) db 0 ; Pad the boot sector out with zeros
dw 0xaa55
times 256 dw 0xdada
times 256 dw 0xface
times 2097152 db 0 ; Only for VirtualBox

View file

@ -0,0 +1,33 @@
[org 0x7c00]
mov bp, 0x9000
mov sp, bp
mov bx, MSG_REAL_MODE
call writeLn
call switch_to_pm
jmp $
%include "./x16/print/print.asm"
%include "./x32/init/gdt.asm"
%include "./x32/print/print_string_pm.asm"
%include "./x32/init/switch_to_pm.asm"
[bits 32]
BEGIN_PM:
mov ebx, MSG_PROT_MODE
call print_string_pm
jmp $
MSG_REAL_MODE: db "Starting OST OS in 16-bit Real Mode", 0
MSG_PROT_MODE: db "Successfully landed Obsidian Starlight in 32-bit Protected Mode", 0
times 510-($-$$) db 0
dw 0xaa55
;times 2097152 db 0 ; Only for VirtualBox

View file

@ -0,0 +1,24 @@
disk_load:
push dx
mov ah, 0x02
mov al, dh
mov ch, 0x00
mov dh, 0x00
mov cl, 0x02
int 0x13
jc disk_error
pop dx
cmp dh, al
jne disk_error
ret
disk_error:
mov ebx, DISK_ERROR_MSG
call writeLn
jmp $
DISK_ERROR_MSG: db "Disk read error!", 0

View file

@ -0,0 +1,33 @@
[ org 0x7c00 ]
write:
mov edx, 0
jmp print
writeLn:
mov edx, 1
print:
mov ah, 0x0e
print_loop:
mov al, [ ebx ]
cmp al, 0
je finish
int 0x10
add ebx, 0x01
call print_loop
finish:
cmp edx, 0
jle end
new_line:
mov al, 13
int 0x10
mov al, 10
int 0x10
dec edx
end:
ret

View file

@ -0,0 +1,25 @@
print_hex:
mov cx, 4
mov di, HEX_OUT + 6
convert_loop:
mov ax, dx
and ax, 0x000F
add al, '0'
cmp al, '9'
jbe not_hex_letter
add al, 7
not_hex_letter:
dec di
mov [di], al
shr dx, 4
loop convert_loop
mov bx, HEX_OUT
call writeLn
ret
HEX_OUT: db '0x0000', 0

View file

@ -0,0 +1,34 @@
; GDT
gdt_start:
gdt_null:
dd 0x0
dd 0x0
gdt_code:
dw 0xffff
dw 0x0
db 0x0
db 10011010b
db 11001111b
db 0x0
gdt_data:
dw 0xffff
dw 0x0
db 0x0
db 10010010b
db 11001111b
db 0x0
gdt_end:
gdt_descriptor:
dw gdt_end - gdt_start - 1
dd gdt_start
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

View file

@ -0,0 +1,26 @@
[bits 16]
switch_to_pm:
cli
lgdt [gdt_descriptor]
mov eax, cr0
or eax, 0x1
mov cr0, eax
jmp CODE_SEG:init_pm
[bits 32]
init_pm:
mov ax, DATA_SEG
mov ds, ax
mov ss, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ebp, 0x90000
mov esp, ebp
call BEGIN_PM

View file

@ -0,0 +1,24 @@
[bits 32]
; Define some constants
VIDEO_MEMORY equ 0xb8000
WHITE_ON_BLACK equ 0x0f
; prints a null - terminated string pointed to by EDX
print_string_pm:
pusha
mov edx, VIDEO_MEMORY ; Set edx to the start of vid mem.
print_string_pm_loop:
mov al, [ ebx ] ; Store the char at EBX in AL
mov ah, WHITE_ON_BLACK ; Store the attributes in AH
cmp al, 0 ; if ( al == 0) , at end of string , so
je print_string_pm_done ; jump to done
mov [ edx ], ax ; Store char and attributes at current
; character cell.
add ebx, 1 ; Increment EBX to the next char in string.
add edx, 2 ; Move to next character cell in vid mem.
jmp print_string_pm_loop ; loop around to print the next char.
print_string_pm_done:
popa
ret ; Return from the function

BIN
OST OS/output1.vdi Normal file

Binary file not shown.

BIN
OST OS/output2.vdi Normal file

Binary file not shown.

BIN
OST OS/output3.vdi Normal file

Binary file not shown.

BIN
OST OS/output4.vdi Normal file

Binary file not shown.

BIN
OST OS/output5.vdi Normal file

Binary file not shown.

BIN
OST OS/output6.vdi Normal file

Binary file not shown.

BIN
OST OS/output7.vdi Normal file

Binary file not shown.

21
build.rs Normal file
View file

@ -0,0 +1,21 @@
use std::path::PathBuf;
fn main() {
// set by cargo, build scripts should use this directory for output files
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
// set by cargo's artifact dependency feature, see
// https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies
let kernel = PathBuf::from(std::env::var_os("CARGO_BIN_FILE_KERNEL_kernel").unwrap());
// create an UEFI disk image (optional)
let uefi_path = out_dir.join("uefi.img");
bootloader::UefiBoot::new(&kernel).create_disk_image(&uefi_path).unwrap();
// create a BIOS disk image
let bios_path = out_dir.join("bios.img");
bootloader::BiosBoot::new(&kernel).create_disk_image(&bios_path).unwrap();
// pass the disk image paths as env variables to the `main.rs`
println!("cargo:rustc-env=UEFI_PATH={}", uefi_path.display());
println!("cargo:rustc-env=BIOS_PATH={}", bios_path.display());
}

16
kernel/Cargo.lock generated Normal file
View file

@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "bootloader_api"
version = "0.11.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a35ba5100c2431e20b924c8103c2cf8adb919ed9880f625e8770c3cb9d1b06aa"
[[package]]
name = "geos"
version = "0.1.0"
dependencies = [
"bootloader_api",
]

15
kernel/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "kernel"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bootloader_api = "0.11.7"
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"

26
kernel/src/main.rs Normal file
View file

@ -0,0 +1,26 @@
#![no_std]
#![no_main]
use core::panic::PanicInfo;
static HELLO: &[u8] = b"Hello World!";
bootloader_api::entry_point!(kernel_main);
fn kernel_main(boot_info: &'static mut bootloader_api::BootInfo) -> ! {
let vga_buffer = 0xb8000 as *mut u8;
for (i, &byte) in HELLO.iter().enumerate() {
unsafe {
*vga_buffer.offset(i as isize * 2) = byte;
*vga_buffer.offset(i as isize * 2 + 1) = 0xb;
}
}
loop {}
}
#[panic_handler]
fn handler(_info: &PanicInfo) -> ! {
loop {}
}

1
rust-toolchain Normal file
View file

@ -0,0 +1 @@
nightly

36
shell.nix Normal file
View file

@ -0,0 +1,36 @@
{ pkgs ? import <nixpkgs> {} }:
pkgs.mkShell rec {
buildInputs = with pkgs; [
clang
llvmPackages_17.bintools
rustup
qemu
];
RUSTC_VERSION = pkgs.lib.readFile ./rust-toolchain;
# https://github.com/rust-lang/rust-bindgen#environment-variables
LIBCLANG_PATH = pkgs.lib.makeLibraryPath [ pkgs.llvmPackages_latest.libclang.lib ];
shellHook = ''
rustup component add llvm-tools-preview
rustup component add rust-analyzer
rustup target add x86_64-unknown-none
export PATH=$PATH:''${CARGO_HOME:-~/.cargo}/bin
export PATH=$PATH:''${RUSTUP_HOME:-~/.rustup}/toolchains/$RUSTC_VERSION-x86_64-unknown-linux-gnu/bin/
'';
# Add precompiled library to rustc search path
RUSTFLAGS = (builtins.map (a: ''-L ${a}/lib'') [
# add libraries here (e.g. pkgs.libvmi)
]);
# Add glibc, clang, glib, and other headers to bindgen search path
BINDGEN_EXTRA_CLANG_ARGS =
# Includes normal include path
(builtins.map (a: ''-I"${a}/include"'') [
# add dev libraries here (e.g. pkgs.libvmi.dev)
pkgs.glibc.dev
])
# Includes with special directory paths
++ [
''-I"${pkgs.llvmPackages_latest.libclang.lib}/lib/clang/${pkgs.llvmPackages_latest.libclang.version}/include"''
''-I"${pkgs.glib.dev}/include/glib-2.0"''
''-I${pkgs.glib.out}/lib/glib-2.0/include/''
];
}

20
src/main.rs Normal file
View file

@ -0,0 +1,20 @@
fn main() {
// read env variables that were set in build script
let uefi_path = env!("UEFI_PATH");
let bios_path = env!("BIOS_PATH");
// choose whether to start the UEFI or BIOS image
let uefi = false;
println!("{}", bios_path);
let mut cmd = std::process::Command::new("qemu-system-x86_64");
if uefi {
cmd.arg("-bios").arg(ovmf_prebuilt::ovmf_pure_efi());
cmd.arg("-drive").arg(format!("format=raw,file={uefi_path}"));
} else {
cmd.arg("-drive").arg(format!("format=raw,file={bios_path}"));
}
let mut child = cmd.spawn().unwrap();
child.wait().unwrap();
}