Skip to content

Make bootsector a staticlib #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
[build]
target = "x86_64-real_mode.json"
rustflags = ["-C", "link-arg=-Tlinker.ld"]
[alias]
xbuild = "build -Zbuild-std=core"
11 changes: 2 additions & 9 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,15 @@ jobs:

- name: "Install Rustup Components"
run: rustup component add rust-src llvm-tools-preview
- name: "Install cargo-xbuild"
run: cargo install cargo-xbuild --debug
- name: "Install cargo-binutils"
run: cargo install cargo-binutils --version 0.1.7 --debug

- run: cargo xbuild
working-directory: test-kernel
name: 'Build Test Kernel'

- name: 'Build Bootloader'
run: cargo xbuild --release
working-directory: src/stage_2

- name: 'Convert Bootloader ELF to Binary'
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
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

# # install QEMU
# - name: Install QEMU (Linux)
Expand Down Expand Up @@ -108,8 +103,6 @@ jobs:
if: runner.os == 'macOS'
- name: "Install Rustup Components"
run: rustup component add rust-src llvm-tools-preview
- name: "Install cargo-xbuild"
run: cargo install cargo-xbuild --debug
- name: 'Build Example Kernel'
run: cargo xbuild
working-directory: example-kernel
Expand Down
8 changes: 7 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ members = [
"src/v86",
"src/bootsector",
"src/stage_2",
# "example-kernel",
# "test-kernel",
"example-kernel",
"test-kernel",
]

[profile.release]
opt-level = "z"
panic = "abort"
panic = "abort"
lto = true

[profile.release.package.bootsector]
opt-level = "s"
codegen-units = 1
5 changes: 4 additions & 1 deletion src/bootsector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ edition = "2018"

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

[lib]
name = "bootsector"
crate-type = ["staticlib"]

[dependencies]
shared = { path = "../shared" }
stage_2 = { path = "../stage_2" }
16 changes: 8 additions & 8 deletions src/bootsector/src/console.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#[inline(never)]
pub fn println(s: &[u8]) {
#[no_mangle]
pub fn real_mode_println(s: &[u8]) {
print(s);
print_char(b'\n');
}

pub fn print(s: &[u8]) {
let mut i = 0;
let mut i = 0;

while i < s.len() {
print_char(s[i]);
i += 1;
}
while i < s.len() {
print_char(s[i]);
i += 1;
}
}

#[inline(always)]
Expand All @@ -19,4 +19,4 @@ pub fn print_char(c: u8) {
unsafe {
llvm_asm!("int 0x10" :: "{ax}"(ax) :: "intel" );
}
}
}
12 changes: 6 additions & 6 deletions src/bootsector/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use super::console::real_mode_println;
use shared::utils;
use super::console::println;

#[no_mangle]
pub extern "C" fn dap_load_failed() -> ! {
println(b"[!] DAP Load Failed");
extern "C" fn dap_load_failed() -> ! {
real_mode_println(b"[!] DAP Load Failed");
loop {
utils::hlt()
}
}

#[no_mangle]
pub extern "C" fn no_int13h_extensions() -> ! {
println(b"[!] No int13h Extensions");
extern "C" fn no_int13h_extensions() -> ! {
real_mode_println(b"[!] No int13h Extensions");
loop {
utils::hlt()
}
}
}
54 changes: 54 additions & 0 deletions src/bootsector/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#![feature(llvm_asm, global_asm)]
#![no_std]
#![allow(dead_code)]

mod console;
mod errors;

use self::console::real_mode_println;
use core::panic::PanicInfo;
use shared::{dap, linker_symbol, utils};

extern "C" {
fn second_stage();
}
global_asm!(include_str!("bootstrap.s"));

#[no_mangle]
extern "C" fn rust_start(disk_number: u16) -> ! {
real_mode_println(b"Stage 1");

check_int13h_extensions(disk_number);

let dap = dap::DiskAddressPacket::new(
linker_symbol!(_rest_of_bootloader_start) as u16,
(linker_symbol!(_rest_of_bootloader_start) - linker_symbol!(_bootloader_start)) as u64,
linker_symbol!(_rest_of_bootloader_end) - linker_symbol!(_rest_of_bootloader_start),
);

unsafe { dap.perform_load(disk_number) };

unsafe { second_stage() };

loop {
utils::hlt();
}
}

fn check_int13h_extensions(disk_number: u16) {
unsafe {
llvm_asm!("
int 0x13
jc no_int13h_extensions
" :: "{ah}"(0x41), "{bx}"(0x55aa), "{dl}"(disk_number) :: "intel", "volatile");
}
}

#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
real_mode_println(b"[Panic]");

loop {
utils::hlt()
}
}
46 changes: 0 additions & 46 deletions src/bootsector/src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/shared/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ impl Writer {
llvm_asm!("int 0x10" :: "{ax}"(ax), "{bx}"(0) :: "intel", "volatile");
}
}
}
}
3 changes: 2 additions & 1 deletion src/shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
pub mod console;
pub mod dap;
pub mod utils;
#[macro_use] pub mod macros;
#[macro_use]
pub mod macros;
2 changes: 1 addition & 1 deletion src/shared/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ macro_rules! linker_symbol {

symbol_value
}};
}
}
2 changes: 1 addition & 1 deletion src/shared/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pub fn hlt() {
unsafe {
llvm_asm!("hlt" :::: "intel","volatile");
}
}
}
3 changes: 3 additions & 0 deletions src/stage_2/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build]
target = "../../x86_64-real_mode.json"
rustflags = ["-C", "link-arg=-Tlinker.ld"]
5 changes: 4 additions & 1 deletion src/stage_2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ v86 = { path = "../v86" }

[dependencies.lazy_static]
version = "1.0"
features = ["spin_no_std"]
features = ["spin_no_std"]

[build-dependencies]
llvm-tools = "0.1.1"
75 changes: 75 additions & 0 deletions src/stage_2/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use llvm_tools::{exe, LlvmTools};
use std::env;
use std::path::Path;
use std::process::Command;

fn main() {
let out_dir = env::var("OUT_DIR").unwrap();
let llvm_tools = LlvmTools::new().expect("LLVM tools not found");
let objcopy = llvm_tools
.tool(&exe("llvm-objcopy"))
.expect("llvm-objcopy not found");

build_subproject(
Path::new("../bootsector"),
&[
"_start",
"real_mode_println",
"no_int13h_extensions",
"dap_load_failed",
],
"x86_64-real_mode.json",
&out_dir,
&objcopy,
);
}

fn build_subproject(
dir: &Path,
global_symbols: &[&str],
target: &str,
out_dir: &str,
objcopy: &Path,
) {
let dir_name = dir.file_name().unwrap().to_str().unwrap();
let manifest_path = dir.join("Cargo.toml");
let out_path = Path::new(&out_dir);
assert!(
global_symbols.len() > 0,
"must have at least one global symbol"
);

// build
let mut cmd = Command::new("cargo");
cmd.arg("xbuild").arg("--release");
cmd.arg("--verbose");
cmd.arg(format!("--manifest-path={}", manifest_path.display()));
cmd.arg(format!(
"--target={}",
dir.join("../..").join(target).display()
));
cmd.arg("-Z").arg("unstable-options");
cmd.arg("--out-dir").arg(&out_dir);
cmd.arg("--target-dir")
.arg(out_path.join("target").join(dir_name));
cmd.env_remove("RUSTFLAGS");
cmd.env(
"XBUILD_SYSROOT_PATH",
out_path.join("target").join(dir_name).join("sysroot"),
);
let status = cmd.status().unwrap();
assert!(status.success());

// localize symbols
let mut cmd = Command::new(objcopy);
for symbol in global_symbols {
cmd.arg("-G").arg(symbol);
}
cmd.arg(out_path.join(format!("lib{}.a", dir_name)));
let status = cmd.status().unwrap();
assert!(status.success());

// emit linker flags
println!("cargo:rustc-link-search=native={}", out_dir);
println!("cargo:rustc-link-lib=static={}", dir_name);
}
Loading