Skip to content

Commit f6d69d1

Browse files
committed
Move towards phil-opp's 'blog-os'
1 parent d050487 commit f6d69d1

File tree

10 files changed

+255
-151
lines changed

10 files changed

+255
-151
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
*.swp
33
*.bin
44
*.o
5+
/target/
6+
**/*.rs.bk

Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "rustboot"
3+
version = "0.1.0"
4+
authors = ["Christian Ternus <[email protected]>"]
5+
6+
[dependencies]
7+
volatile = "0.1.0"
8+
9+
[lib]
10+
crate-type = ["staticlib"]

Makefile

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
1-
LD=i386-elf-ld
2-
RUSTC=rustc
3-
NASM=nasm
4-
QEMU=qemu-system-i386
1+
KNAME := rustboot
2+
BITS := 64
3+
ARCH := x86_64
4+
LD := $(ARCH)-elf-ld
5+
QEMU := qemu-system-$(ARCH)
6+
TRIPLE := $(ARCH)-$(KNAME)
7+
TARGET := target/debug/lib$(KNAME).a
8+
9+
RUSTC := rustc
10+
NASM := nasm
11+
CARGO := xargo
512

613
all: floppy.img
714

815
.SUFFIXES: .o .rs .asm
916

1017
.PHONY: clean run
1118

12-
.rs.o:
13-
$(RUSTC) -O --target i686-unknown-linux-gnu --crate-type lib -o $@ --emit obj $<
19+
$(TARGET):
20+
$(CARGO) build
1421

1522
.asm.o:
16-
$(NASM) -f elf32 -o $@ $<
23+
$(NASM) -f elf$(BITS) -o $@ $<
1724

18-
floppy.img: loader.bin main.bin
25+
floppy.img: loader.bin $(TARGET)
1926
dd if=/dev/zero of=$@ bs=512 count=2 &>/dev/null
2027
cat $^ | dd if=/dev/stdin of=$@ conv=notrunc &>/dev/null
2128

2229
loader.bin: loader.asm
2330
$(NASM) -o $@ -f bin $<
2431

25-
main.bin: linker.ld main.o
26-
$(LD) -m elf_i386 -o $@ -T $^
32+
main.bin: linker.ld main.o *.rs
33+
$(LD) -m elf_$(ARCH) -o $@ -T linker.ld
2734

2835
run: floppy.img
29-
$(QEMU) -fda $<
36+
$(QEMU) -drive format=raw,if=floppy,file=$<
3037

3138
clean:
32-
rm -f *.bin *.o *.img
39+
$(CARGO) clean
40+
rm -f $(TARGET) *.bin *.o *.img

main.rs

Lines changed: 0 additions & 34 deletions
This file was deleted.

mem.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![no_std]
2+
3+
#![feature(asm)]
4+
#![feature(const_fn)]
5+
#![feature(lang_items, start)]
6+
#![feature(unique)]
7+
8+
#[macro_use]
9+
10+
mod mem;
11+
mod stdio;
12+
// mod lib;
13+
14+
extern crate volatile;
15+
16+
// #[no_mangle]
17+
#[inline(never)]
18+
#[start]
19+
pub fn main(_argc: isize, _argv: *const *const u8) -> isize{
20+
unsafe {
21+
stdio::clear_screen(stdio::Color::Cyan);
22+
// stdio::print("hello\nspork", stdio::Color::White, stdio::Color::Black);
23+
// stdio::print_something();
24+
}
25+
1
26+
}
27+
28+
29+
#[lang = "eh_personality"] extern fn eh_personality() {}
30+
#[lang = "panic_fmt"] #[no_mangle] pub extern fn panic_fmt() -> ! {loop{}}

src/mem.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
// use core;
3+
4+
pub type physaddr = u32;
5+
6+
macro_rules! physaddr {
7+
($e:expr) => (($e as physaddr));
8+
}
9+
10+
// macro_rules! physaddr {
11+
// ($e:expr) => (($e as *mut physaddr));
12+
// }
13+
14+
macro_rules! to_ptr {
15+
($e:expr) => (($e as *mut u16));
16+
}
17+
18+
// pub fn poke(dest: *mut physaddr, val: u8) {
19+
// core::ptr::write_volatile(dest, val);
20+
// }
21+
22+
// pub fn memset(dest: *mut physaddr, val: u8, count: isize) {
23+
// for i in 0..count {
24+
// unsafe {
25+
// core::ptr::write_volatile(dest.offset(i), val);
26+
// }
27+
// }
28+
// }
29+
30+
// pub unsafe fn memcpy(dest: *mut physaddr, src: *const physaddr, count: isize) {
31+
// for i in 0..count {
32+
// core::ptr::write_volatile(dest.offset(i as isize), *src.offset(i as isize));
33+
// }
34+
// }

src/stdio.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
2+
// #[macro_use]
3+
4+
use core;
5+
// use mem;
6+
7+
use mem::physaddr;
8+
use core::ptr::Unique;
9+
10+
const VGABASE: physaddr = physaddr!(0xb8000);
11+
12+
const VGA_X: usize = 80;
13+
const VGA_Y: usize = 25;
14+
const VGASIZE: isize = (VGA_X as isize * VGA_Y as isize);
15+
16+
// static mut console_x: u8 = 0;
17+
// static mut console_y: u8 = 0;
18+
19+
#[allow(dead_code)]
20+
#[derive(Debug, Copy, Clone)]
21+
#[repr(u8)]
22+
pub enum Color {
23+
Black = 0,
24+
Blue = 1,
25+
Green = 2,
26+
Cyan = 3,
27+
Red = 4,
28+
Pink = 5,
29+
Brown = 6,
30+
LightGray = 7,
31+
DarkGray = 8,
32+
LightBlue = 9,
33+
LightGreen = 10,
34+
LightCyan = 11,
35+
LightRed = 12,
36+
LightPink = 13,
37+
Yellow = 14,
38+
White = 15,
39+
}
40+
41+
// #[repr(C)]
42+
#[derive(Debug, Clone, Copy)]
43+
struct ColorCode(u8);
44+
45+
impl ColorCode {
46+
const fn new(fg: Color, bg: Color) -> ColorCode {
47+
ColorCode((bg as u8) << 4 | (fg as u8))
48+
}
49+
}
50+
51+
#[repr(C)]
52+
#[derive(Debug, Clone, Copy)]
53+
struct VGAChar {
54+
character: u8,
55+
color_code: ColorCode,
56+
}
57+
58+
struct Buffer {
59+
chars: [[VGAChar; VGA_X]; VGA_Y],
60+
}
61+
62+
pub struct Writer {
63+
col: usize,
64+
color_code: ColorCode,
65+
buffer: Unique<Buffer>
66+
}
67+
68+
impl Writer {
69+
pub fn write_byte(&mut self, byte: u8) {
70+
if self.col >= VGA_X {
71+
self.new_line();
72+
}
73+
74+
let row = VGA_Y - 1;
75+
self.buffer().chars[row][self.col] = VGAChar {
76+
character: byte,
77+
color_code: self.color_code
78+
};
79+
self.col = self.col + 1;
80+
}
81+
fn buffer(&mut self) -> &mut Buffer {
82+
unsafe { self.buffer.as_mut() }
83+
}
84+
fn new_line(&mut self) {
85+
for y in 1..VGA_Y {
86+
for x in 0..VGA_X {
87+
self.buffer().chars[y][x] = self.buffer().chars[y - 1][x];
88+
}
89+
}
90+
}
91+
}
92+
93+
94+
95+
96+
pub fn clear_screen(color: Color) {
97+
unsafe {
98+
for i in 0..(80 * 25) {
99+
core::ptr::write_volatile((VGABASE as *mut u16).offset(i), ((color as u16) << 12))
100+
}
101+
}
102+
}
103+
104+
pub fn to_vga(c: u8, fg: Color, bg: Color) -> u16 {
105+
return ((fg as u16) << 8) | (c as u16);
106+
}
107+
108+
pub fn print_something() {
109+
let mut writer = Writer {
110+
col: 0,
111+
color_code: ColorCode::new(Color::LightGreen, Color::Black),
112+
buffer: unsafe { Unique::new_unchecked(0xb8000 as *mut _) },
113+
};
114+
115+
writer.write_byte(b'H');
116+
}
117+
118+
// pub fn putc(c: u8, fg: Color, bg: Color) {
119+
// unsafe {
120+
// core::ptr::write_volatile(to_ptr!(VGABASE).offset((console_x + (console_y * VGA_X)) as isize),
121+
// to_vga(c, fg, bg)
122+
// )
123+
// }
124+
// }
125+
126+
// pub fn putchar(c: u8, fg: Color, bg: Color) {
127+
// if c != '\n' as u8 {
128+
// putc(c, fg, bg);
129+
// }
130+
// unsafe {
131+
// console_x += 1;
132+
// if console_x >= VGA_X || c == '\n' as u8 {
133+
// console_x = 0;
134+
// console_y += 1;
135+
// if console_y >= VGA_Y {
136+
// console_y = 0;
137+
// }
138+
// }
139+
// }
140+
// }
141+
142+
// pub fn print(s: &str, fg: Color, bg: Color) {
143+
// for c in s.bytes() {
144+
// putchar(c, fg, bg);
145+
// }
146+
// }
147+
148+
// // pub fn printf(s: &str,

0 commit comments

Comments
 (0)