mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Compare commits
203 Commits
post-01
...
93d0daa1e0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
93d0daa1e0 | ||
|
|
83e6c0bc00 | ||
|
|
2b11ad8397 | ||
|
|
fa51f3adbf | ||
|
|
1f097bba71 | ||
|
|
99fbbad9b0 | ||
|
|
94d14d8d62 | ||
|
|
6f0be666f3 | ||
|
|
d9fa84edd6 | ||
|
|
97de98df33 | ||
|
|
9684f75819 | ||
|
|
d6cc562907 | ||
|
|
e2a3e76a32 | ||
|
|
16bbb8f972 | ||
|
|
df0c0b5783 | ||
|
|
c27eb1da9c | ||
|
|
2a61f88d8a | ||
|
|
a90a8bea67 | ||
|
|
f52872f3b9 | ||
|
|
688a21e4ed | ||
|
|
07d6c9c84e | ||
|
|
2265ddecad | ||
|
|
3bc023ec2d | ||
|
|
38604da92f | ||
|
|
1a53a115da | ||
|
|
c6990cb52d | ||
|
|
ef9bb1a087 | ||
|
|
2bc4490404 | ||
|
|
7288459bdf | ||
|
|
7959b77ebd | ||
|
|
bebad6d5a9 | ||
|
|
6c8fea8b00 | ||
|
|
b99c3f6d1d | ||
|
|
66c1792034 | ||
|
|
3280a0afc7 | ||
|
|
6393247a3b | ||
|
|
a387a00c85 | ||
|
|
faa8d15cb7 | ||
|
|
4c9cd3ae32 | ||
|
|
26f1f5b896 | ||
|
|
cbead25b43 | ||
|
|
1f49e2e9fe | ||
|
|
2c25d55810 | ||
|
|
021762cf30 | ||
|
|
ec95ac0e5a | ||
|
|
d8f280315f | ||
|
|
e39c7d8ad8 | ||
|
|
066c278277 | ||
|
|
f40717901b | ||
|
|
96844f6519 | ||
|
|
195f4489e6 | ||
|
|
1b86d7fd62 | ||
|
|
fbdcd45d16 | ||
|
|
d658949f5b | ||
|
|
d12842bcd5 | ||
|
|
0e52ccbebb | ||
|
|
ea5aad0e0c | ||
|
|
4e38e7ddf8 | ||
|
|
1afa3af79d | ||
|
|
49dce7ada7 | ||
|
|
552eb97f75 | ||
|
|
c42cee8d89 | ||
|
|
eace075f2c | ||
|
|
2d5150f2a3 | ||
|
|
ebb9f0b8a7 | ||
|
|
3200b7a68d | ||
|
|
cde3c8f955 | ||
|
|
f316bd1a08 | ||
|
|
4a4d704384 | ||
|
|
1ddc27ca88 | ||
|
|
71d8a438f7 | ||
|
|
3a77cbe794 | ||
|
|
406c406d5f | ||
|
|
6f9cfce6f9 | ||
|
|
651adbc8bd | ||
|
|
4bbaae24a4 | ||
|
|
44d7654ef6 | ||
|
|
f135aefae8 | ||
|
|
ffaac76876 | ||
|
|
5ae9f62f4d | ||
|
|
da0ee38f09 | ||
|
|
263e852dcc | ||
|
|
614e5b0e86 | ||
|
|
9ee42e0f5f | ||
|
|
c4672096d4 | ||
|
|
ffe1b6abd0 | ||
|
|
16ffe80fd5 | ||
|
|
07d8a22a53 | ||
|
|
a004e7da9c | ||
|
|
b1bf0873ce | ||
|
|
f23269ed93 | ||
|
|
46264d08ca | ||
|
|
0489c09d05 | ||
|
|
99b139ddff | ||
|
|
fea4fa9c67 | ||
|
|
ddbe317099 | ||
|
|
abd2f4c5e2 | ||
|
|
0cc02f02c5 | ||
|
|
dbb3db2dc7 | ||
|
|
58629ea208 | ||
|
|
748365bf62 | ||
|
|
2b1f72ccd8 | ||
|
|
1c4ca04a2f | ||
|
|
60856aac6b | ||
|
|
679e453509 | ||
|
|
7de138aa04 | ||
|
|
4f90781b3f | ||
|
|
9e40f377db | ||
|
|
515d98645e | ||
|
|
6b23fa3d28 | ||
|
|
a86fe5c2af | ||
|
|
99a7231d52 | ||
|
|
ab10af6f99 | ||
|
|
621364bd1b | ||
|
|
bcafb60069 | ||
|
|
818bfb9646 | ||
|
|
465eb45cfe | ||
|
|
c6d5a4d567 | ||
|
|
fe5e1fd92c | ||
|
|
ffd1bda509 | ||
|
|
a1060f79f0 | ||
|
|
bb8923cf56 | ||
|
|
3765c27020 | ||
|
|
dbe0802c6d | ||
|
|
70b339a4a5 | ||
|
|
9e1e3926fa | ||
|
|
153beec1c4 | ||
|
|
7b00599bf8 | ||
|
|
72aa3a4580 | ||
|
|
5d7c4678de | ||
|
|
bfde53de79 | ||
|
|
7197d8b18a | ||
|
|
298ec5f4b6 | ||
|
|
e86c95bd24 | ||
|
|
ff8afb814e | ||
|
|
47bcca33b3 | ||
|
|
5ed27923e8 | ||
|
|
fcd849f471 | ||
|
|
70c24b76d0 | ||
|
|
b59720f6e1 | ||
|
|
16a314ab8d | ||
|
|
63b97fe1f8 | ||
|
|
528bbd4e9f | ||
|
|
06d1c9ff81 | ||
|
|
c1064ad316 | ||
|
|
f875a933e5 | ||
|
|
3c2a5ef0f6 | ||
|
|
652d91154f | ||
|
|
64e7ea7b1a | ||
|
|
9a879e69c5 | ||
|
|
eee4718115 | ||
|
|
186321623a | ||
|
|
1246fc4df9 | ||
|
|
9237c875f3 | ||
|
|
03da22d8e1 | ||
|
|
62b89cd80f | ||
|
|
e1034aa9ac | ||
|
|
577c6b8cfc | ||
|
|
d98a4d0cbc | ||
|
|
f308628db2 | ||
|
|
93b2134692 | ||
|
|
ab16203ed7 | ||
|
|
4d405d5098 | ||
|
|
63f8f3a6eb | ||
|
|
e3f9cd42b0 | ||
|
|
013e6f2add | ||
|
|
61f524f040 | ||
|
|
d5cb74ac8b | ||
|
|
3bf2c66acc | ||
|
|
3f9072e8b5 | ||
|
|
0e6ffc1a4f | ||
|
|
bac4a3c549 | ||
|
|
57484164ab | ||
|
|
1ca8cec4e6 | ||
|
|
4bfcea2368 | ||
|
|
2d5d6ee850 | ||
|
|
af0b258279 | ||
|
|
c9ce9d5735 | ||
|
|
de2b99f0d2 | ||
|
|
8c1e98752a | ||
|
|
ceb5cf4eed | ||
|
|
f013172dd7 | ||
|
|
858a2f94c6 | ||
|
|
1d5bc372a0 | ||
|
|
2ce4af4a65 | ||
|
|
0874a50544 | ||
|
|
8d730050d3 | ||
|
|
752a7ed84e | ||
|
|
b5c05193e1 | ||
|
|
6c427e7cee | ||
|
|
c43d6191c5 | ||
|
|
c1e3fa57c6 | ||
|
|
3574958d5f | ||
|
|
cbfe6221bd | ||
|
|
d1897af515 | ||
|
|
59f9fe1e69 | ||
|
|
f4fd0e264a | ||
|
|
bbc19e132c | ||
|
|
f9ffee92c7 | ||
|
|
fd08b1a123 | ||
|
|
d71e6a7cb6 | ||
|
|
27f2c4c2ff | ||
|
|
19de9e560d |
9
.cargo/config.toml
Normal file
9
.cargo/config.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[unstable]
|
||||
build-std = ["core", "compiler_builtins"]
|
||||
build-std-features = ["compiler-builtins-mem"]
|
||||
|
||||
[build]
|
||||
target = "x86_64-blog_os.json"
|
||||
|
||||
[target.'cfg(target_os = "none")']
|
||||
runner = "bootimage runner"
|
||||
21
.github/workflows/code.yml
vendored
21
.github/workflows/code.yml
vendored
@@ -56,21 +56,16 @@ jobs:
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: nightly
|
||||
- name: Install bootimage
|
||||
run: cargo install bootimage --debug
|
||||
- name: Checkout Repository
|
||||
uses: actions/checkout@v2
|
||||
- name: Add thumbv7em-none-eabihf Target
|
||||
run: rustup target add thumbv7em-none-eabihf
|
||||
- name: Build for thumbv7em-none-eabihf
|
||||
run: cargo build --target thumbv7em-none-eabihf
|
||||
- name: Build for Linux
|
||||
if: runner.os == 'Linux'
|
||||
run: cargo rustc -- -C link-arg=-nostartfiles
|
||||
- name: Build for macOS
|
||||
if: runner.os == 'macOS'
|
||||
run: cargo rustc -- -C link-args="-e __start -static -nostartfiles"
|
||||
- name: Build for Windows
|
||||
if: runner.os == 'Windows'
|
||||
run: cargo rustc -- -C link-args="/ENTRY:_start /SUBSYSTEM:console"
|
||||
- name: Install Rustup Components
|
||||
run: rustup component add rust-src llvm-tools-preview
|
||||
- name: Run `cargo bootimage`
|
||||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: bootimage
|
||||
|
||||
check_formatting:
|
||||
name: Check Formatting
|
||||
|
||||
41
Cargo.lock
generated
41
Cargo.lock
generated
@@ -1,7 +1,46 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "blog_os"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bootloader",
|
||||
"lazy_static",
|
||||
"spin 0.5.2",
|
||||
"volatile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bootloader"
|
||||
version = "0.9.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7bdfddac270bbdd45903296bc1caf29a7fdce6b326aaf0bbab7f04c5f98b7447"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
dependencies = [
|
||||
"spin 0.9.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.5.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
|
||||
|
||||
[[package]]
|
||||
name = "spin"
|
||||
version = "0.9.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
|
||||
|
||||
[[package]]
|
||||
name = "volatile"
|
||||
version = "0.2.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"
|
||||
|
||||
11
Cargo.toml
11
Cargo.toml
@@ -5,12 +5,13 @@ authors = ["Philipp Oppermann <dev@phil-opp.com>"]
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
bootloader = "0.9"
|
||||
volatile = "0.2.6"
|
||||
spin = "0.5.2"
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
[dependencies.lazy_static]
|
||||
version = "1.0"
|
||||
features = ["spin_no_std"]
|
||||
|
||||
[[bin]]
|
||||
name = "blog_os"
|
||||
|
||||
50
README.md
50
README.md
@@ -1,25 +1,61 @@
|
||||
# Blog OS (A Freestanding Rust Binary)
|
||||
# Blog OS (VGA Text Mode)
|
||||
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-01)
|
||||
[](https://github.com/phil-opp/blog_os/actions?query=workflow%3A%22Code%22+branch%3Apost-03)
|
||||
|
||||
This repository contains the source code for the [A Freestanding Rust Binary][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
This repository contains the source code for the [VGA Text Mode][post] post of the [Writing an OS in Rust](https://os.phil-opp.com) series.
|
||||
|
||||
[post]: https://os.phil-opp.com/freestanding-rust-binary/
|
||||
[post]: https://os.phil-opp.com/vga-text-mode/
|
||||
|
||||
**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
|
||||
|
||||
## Building
|
||||
|
||||
To build the project on Linux, run:
|
||||
This project requires a nightly version of Rust because it uses some unstable features. At least nightly _2020-07-15_ is required for building. You might need to run `rustup update nightly --force` to update to the latest nightly even if some components such as `rustfmt` are missing it.
|
||||
|
||||
You can build the project by running:
|
||||
|
||||
```
|
||||
cargo rustc -- -Clink-arg=-nostartfiles
|
||||
cargo build
|
||||
```
|
||||
|
||||
The entry point and the build command differ slightly on macOS and Windows. See the [post] for more information.
|
||||
To create a bootable disk image from the compiled kernel, you need to install the [`bootimage`] tool:
|
||||
|
||||
[`bootimage`]: https://github.com/rust-osdev/bootimage
|
||||
|
||||
```
|
||||
cargo install bootimage
|
||||
```
|
||||
|
||||
After installing, you can create the bootable disk image by running:
|
||||
|
||||
```
|
||||
cargo bootimage
|
||||
```
|
||||
|
||||
This creates a bootable disk image in the `target/x86_64-blog_os/debug` directory.
|
||||
|
||||
Please file an issue if you have any problems.
|
||||
|
||||
## Running
|
||||
|
||||
You can run the disk image in [QEMU] through:
|
||||
|
||||
[QEMU]: https://www.qemu.org/
|
||||
|
||||
```
|
||||
cargo run
|
||||
```
|
||||
|
||||
[QEMU] and the [`bootimage`] tool need to be installed for this.
|
||||
|
||||
You can also write the image to an USB stick for booting it on a real machine. On Linux, the command for this is:
|
||||
|
||||
```
|
||||
dd if=target/x86_64-blog_os/debug/bootimage-blog_os.bin of=/dev/sdX && sync
|
||||
```
|
||||
|
||||
Where `sdX` is the device name of your USB stick. **Be careful** to choose the correct device name, because everything on that device is overwritten.
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
||||
1
rust-toolchain
Normal file
1
rust-toolchain
Normal file
@@ -0,0 +1 @@
|
||||
nightly
|
||||
@@ -3,13 +3,18 @@
|
||||
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
mod vga_buffer;
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
pub extern "C" fn _start() -> ! {
|
||||
println!("Hello World{}", "!");
|
||||
|
||||
loop {}
|
||||
}
|
||||
|
||||
/// This function is called on panic.
|
||||
#[panic_handler]
|
||||
fn panic(_info: &PanicInfo) -> ! {
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
println!("{}", info);
|
||||
loop {}
|
||||
}
|
||||
|
||||
171
src/vga_buffer.rs
Normal file
171
src/vga_buffer.rs
Normal file
@@ -0,0 +1,171 @@
|
||||
use core::fmt;
|
||||
use lazy_static::lazy_static;
|
||||
use spin::Mutex;
|
||||
use volatile::Volatile;
|
||||
|
||||
lazy_static! {
|
||||
/// A global `Writer` instance that can be used for printing to the VGA text buffer.
|
||||
///
|
||||
/// Used by the `print!` and `println!` macros.
|
||||
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||
column_position: 0,
|
||||
color_code: ColorCode::new(Color::Yellow, Color::Black),
|
||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
||||
});
|
||||
}
|
||||
|
||||
/// The standard color palette in VGA text mode.
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(u8)]
|
||||
pub enum Color {
|
||||
Black = 0,
|
||||
Blue = 1,
|
||||
Green = 2,
|
||||
Cyan = 3,
|
||||
Red = 4,
|
||||
Magenta = 5,
|
||||
Brown = 6,
|
||||
LightGray = 7,
|
||||
DarkGray = 8,
|
||||
LightBlue = 9,
|
||||
LightGreen = 10,
|
||||
LightCyan = 11,
|
||||
LightRed = 12,
|
||||
Pink = 13,
|
||||
Yellow = 14,
|
||||
White = 15,
|
||||
}
|
||||
|
||||
/// A combination of a foreground and a background color.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(transparent)]
|
||||
struct ColorCode(u8);
|
||||
|
||||
impl ColorCode {
|
||||
/// Create a new `ColorCode` with the given foreground and background colors.
|
||||
fn new(foreground: Color, background: Color) -> ColorCode {
|
||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||
}
|
||||
}
|
||||
|
||||
/// A screen character in the VGA text buffer, consisting of an ASCII character and a `ColorCode`.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
struct ScreenChar {
|
||||
ascii_character: u8,
|
||||
color_code: ColorCode,
|
||||
}
|
||||
|
||||
/// The height of the text buffer (normally 25 lines).
|
||||
const BUFFER_HEIGHT: usize = 25;
|
||||
/// The width of the text buffer (normally 80 columns).
|
||||
const BUFFER_WIDTH: usize = 80;
|
||||
|
||||
/// A structure representing the VGA text buffer.
|
||||
#[repr(transparent)]
|
||||
struct Buffer {
|
||||
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||
}
|
||||
|
||||
/// A writer type that allows writing ASCII bytes and strings to an underlying `Buffer`.
|
||||
///
|
||||
/// Wraps lines at `BUFFER_WIDTH`. Supports newline characters and implements the
|
||||
/// `core::fmt::Write` trait.
|
||||
pub struct Writer {
|
||||
column_position: usize,
|
||||
color_code: ColorCode,
|
||||
buffer: &'static mut Buffer,
|
||||
}
|
||||
|
||||
impl Writer {
|
||||
/// Writes an ASCII byte to the buffer.
|
||||
///
|
||||
/// Wraps lines at `BUFFER_WIDTH`. Supports the `\n` newline character.
|
||||
pub fn write_byte(&mut self, byte: u8) {
|
||||
match byte {
|
||||
b'\n' => self.new_line(),
|
||||
byte => {
|
||||
if self.column_position >= BUFFER_WIDTH {
|
||||
self.new_line();
|
||||
}
|
||||
|
||||
let row = BUFFER_HEIGHT - 1;
|
||||
let col = self.column_position;
|
||||
|
||||
let color_code = self.color_code;
|
||||
self.buffer.chars[row][col].write(ScreenChar {
|
||||
ascii_character: byte,
|
||||
color_code,
|
||||
});
|
||||
self.column_position += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the given ASCII string to the buffer.
|
||||
///
|
||||
/// Wraps lines at `BUFFER_WIDTH`. Supports the `\n` newline character. Does **not**
|
||||
/// support strings with non-ASCII characters, since they can't be printed in the VGA text
|
||||
/// mode.
|
||||
fn write_string(&mut self, s: &str) {
|
||||
for byte in s.bytes() {
|
||||
match byte {
|
||||
// printable ASCII byte or newline
|
||||
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
||||
// not part of printable ASCII range
|
||||
_ => self.write_byte(0xfe),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Shifts all lines one line up and clears the last row.
|
||||
fn new_line(&mut self) {
|
||||
for row in 1..BUFFER_HEIGHT {
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
let character = self.buffer.chars[row][col].read();
|
||||
self.buffer.chars[row - 1][col].write(character);
|
||||
}
|
||||
}
|
||||
self.clear_row(BUFFER_HEIGHT - 1);
|
||||
self.column_position = 0;
|
||||
}
|
||||
|
||||
/// Clears a row by overwriting it with blank characters.
|
||||
fn clear_row(&mut self, row: usize) {
|
||||
let blank = ScreenChar {
|
||||
ascii_character: b' ',
|
||||
color_code: self.color_code,
|
||||
};
|
||||
for col in 0..BUFFER_WIDTH {
|
||||
self.buffer.chars[row][col].write(blank);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Write for Writer {
|
||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||
self.write_string(s);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Like the `print!` macro in the standard library, but prints to the VGA text buffer.
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ($crate::vga_buffer::_print(format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
/// Like the `println!` macro in the standard library, but prints to the VGA text buffer.
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
() => ($crate::print!("\n"));
|
||||
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
|
||||
}
|
||||
|
||||
/// Prints the given formatted string to the VGA text buffer through the global `WRITER` instance.
|
||||
#[doc(hidden)]
|
||||
pub fn _print(args: fmt::Arguments) {
|
||||
use core::fmt::Write;
|
||||
WRITER.lock().write_fmt(args).unwrap();
|
||||
}
|
||||
16
x86_64-blog_os.json
Normal file
16
x86_64-blog_os.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"llvm-target": "x86_64-unknown-none",
|
||||
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
|
||||
"arch": "x86_64",
|
||||
"target-endian": "little",
|
||||
"target-pointer-width": 64,
|
||||
"target-c-int-width": 32,
|
||||
"os": "none",
|
||||
"executables": true,
|
||||
"linker-flavor": "ld.lld",
|
||||
"linker": "rust-lld",
|
||||
"panic-strategy": "abort",
|
||||
"disable-redzone": true,
|
||||
"features": "-mmx,-sse,+soft-float",
|
||||
"rustc-abi": "x86-softfloat"
|
||||
}
|
||||
Reference in New Issue
Block a user