Skip to content

Commit 1598297

Browse files
Merge pull request rust-osdev#113 from rust-osdev/make-bootsector-a-staticlib
Make bootsector a staticlib
2 parents c2c1681 + 0630857 commit 1598297

File tree

21 files changed

+198
-100
lines changed

21 files changed

+198
-100
lines changed

.cargo/config

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
[build]
2-
target = "x86_64-real_mode.json"
3-
rustflags = ["-C", "link-arg=-Tlinker.ld"]
1+
[alias]
2+
xbuild = "build -Zbuild-std=core"

.github/workflows/build.yml

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,15 @@ jobs:
4141
4242
- name: "Install Rustup Components"
4343
run: rustup component add rust-src llvm-tools-preview
44-
- name: "Install cargo-xbuild"
45-
run: cargo install cargo-xbuild --debug
4644
- name: "Install cargo-binutils"
4745
run: cargo install cargo-binutils --version 0.1.7 --debug
4846

49-
- run: cargo xbuild
50-
working-directory: test-kernel
51-
name: 'Build Test Kernel'
52-
5347
- name: 'Build Bootloader'
5448
run: cargo xbuild --release
49+
working-directory: src/stage_2
5550

5651
- name: 'Convert Bootloader ELF to Binary'
57-
run: cargo objcopy -- -I elf64-i386 -O binary --binary-architecture=i386:x86-64 target/x86_64-real_mode/release/bootsector target/x86_64-real_mode/release/bootsector.bin
52+
run: cargo objcopy -- -I elf64-i386 -O binary --binary-architecture=i386:x86-64 target/x86_64-real_mode/release/stage_2 target/x86_64-real_mode/release/image.bin
5853

5954
# # install QEMU
6055
# - name: Install QEMU (Linux)
@@ -108,8 +103,6 @@ jobs:
108103
if: runner.os == 'macOS'
109104
- name: "Install Rustup Components"
110105
run: rustup component add rust-src llvm-tools-preview
111-
- name: "Install cargo-xbuild"
112-
run: cargo install cargo-xbuild --debug
113106
- name: 'Build Example Kernel'
114107
run: cargo xbuild
115108
working-directory: example-kernel

Cargo.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ members = [
44
"src/v86",
55
"src/bootsector",
66
"src/stage_2",
7-
# "example-kernel",
8-
# "test-kernel",
7+
"example-kernel",
8+
"test-kernel",
99
]
1010

1111
[profile.release]
1212
opt-level = "z"
13-
panic = "abort"
13+
panic = "abort"
14+
lto = true
15+
16+
[profile.release.package.bootsector]
17+
opt-level = "s"
18+
codegen-units = 1

src/bootsector/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

9+
[lib]
10+
name = "bootsector"
11+
crate-type = ["staticlib"]
12+
913
[dependencies]
1014
shared = { path = "../shared" }
11-
stage_2 = { path = "../stage_2" }

src/bootsector/src/console.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
#[inline(never)]
2-
pub fn println(s: &[u8]) {
1+
#[no_mangle]
2+
pub fn real_mode_println(s: &[u8]) {
33
print(s);
44
print_char(b'\n');
55
}
66

77
pub fn print(s: &[u8]) {
8-
let mut i = 0;
8+
let mut i = 0;
99

10-
while i < s.len() {
11-
print_char(s[i]);
12-
i += 1;
13-
}
10+
while i < s.len() {
11+
print_char(s[i]);
12+
i += 1;
13+
}
1414
}
1515

1616
#[inline(always)]
@@ -19,4 +19,4 @@ pub fn print_char(c: u8) {
1919
unsafe {
2020
llvm_asm!("int 0x10" :: "{ax}"(ax) :: "intel" );
2121
}
22-
}
22+
}

src/bootsector/src/errors.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
use super::console::real_mode_println;
12
use shared::utils;
2-
use super::console::println;
33

44
#[no_mangle]
5-
pub extern "C" fn dap_load_failed() -> ! {
6-
println(b"[!] DAP Load Failed");
5+
extern "C" fn dap_load_failed() -> ! {
6+
real_mode_println(b"[!] DAP Load Failed");
77
loop {
88
utils::hlt()
99
}
1010
}
1111

1212
#[no_mangle]
13-
pub extern "C" fn no_int13h_extensions() -> ! {
14-
println(b"[!] No int13h Extensions");
13+
extern "C" fn no_int13h_extensions() -> ! {
14+
real_mode_println(b"[!] No int13h Extensions");
1515
loop {
1616
utils::hlt()
1717
}
18-
}
18+
}

src/bootsector/src/lib.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![feature(llvm_asm, global_asm)]
2+
#![no_std]
3+
#![allow(dead_code)]
4+
5+
mod console;
6+
mod errors;
7+
8+
use self::console::real_mode_println;
9+
use core::panic::PanicInfo;
10+
use shared::{dap, linker_symbol, utils};
11+
12+
extern "C" {
13+
fn second_stage();
14+
}
15+
global_asm!(include_str!("bootstrap.s"));
16+
17+
#[no_mangle]
18+
extern "C" fn rust_start(disk_number: u16) -> ! {
19+
real_mode_println(b"Stage 1");
20+
21+
check_int13h_extensions(disk_number);
22+
23+
let dap = dap::DiskAddressPacket::new(
24+
linker_symbol!(_rest_of_bootloader_start) as u16,
25+
(linker_symbol!(_rest_of_bootloader_start) - linker_symbol!(_bootloader_start)) as u64,
26+
linker_symbol!(_rest_of_bootloader_end) - linker_symbol!(_rest_of_bootloader_start),
27+
);
28+
29+
unsafe { dap.perform_load(disk_number) };
30+
31+
unsafe { second_stage() };
32+
33+
loop {
34+
utils::hlt();
35+
}
36+
}
37+
38+
fn check_int13h_extensions(disk_number: u16) {
39+
unsafe {
40+
llvm_asm!("
41+
int 0x13
42+
jc no_int13h_extensions
43+
" :: "{ah}"(0x41), "{bx}"(0x55aa), "{dl}"(disk_number) :: "intel", "volatile");
44+
}
45+
}
46+
47+
#[panic_handler]
48+
fn panic(_info: &PanicInfo) -> ! {
49+
real_mode_println(b"[Panic]");
50+
51+
loop {
52+
utils::hlt()
53+
}
54+
}

src/bootsector/src/main.rs

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

src/shared/src/console.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ impl Writer {
3636
llvm_asm!("int 0x10" :: "{ax}"(ax), "{bx}"(0) :: "intel", "volatile");
3737
}
3838
}
39-
}
39+
}

0 commit comments

Comments
 (0)