Skip to content

Commit e5d2528

Browse files
committed
dump of working version before cleaning int
1 parent fbdcfad commit e5d2528

24 files changed

+673
-581
lines changed

.cargo/config.toml

-8
This file was deleted.

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[workspace]
2+
members = [
3+
"first_stage",
4+
"second_stage"
5+
]
6+

Makefile

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
# Tools
22
OBJCPY = objcopy
3-
CARGO = cargo
3+
CARGO = cargo +nightly
44
QEMU = qemu-system-x86_64
55
OUTPUT = bootloader.img
66

7+
# Stages
8+
FIRST = first_stage
9+
SECOND = second_stage
10+
TEST = test_stage
11+
712
# Targets
813
16BIT = 16bit_target
914
32BIT = 32bit_target
1015

16+
# Manifests
17+
TOML = Cargo.toml
18+
1119
# Default rule
1220
all: build-rust create_img
1321

1422
# Step 1: Build Rust code
1523
build-rust:
1624
@echo "Compiling Rust code..."
17-
$(CARGO) build --release --target $(16BIT).json -Zbuild-std=core --features 16bit
18-
$(CARGO) build --release --target $(32BIT).json -Zbuild-std=core --features 32bit
19-
25+
$(CARGO) build --release --manifest-path ./$(FIRST)/$(TOML) --target ./$(FIRST)/$(16BIT).json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
26+
$(CARGO) build --release --manifest-path ./$(SECOND)/$(TOML) --target ./$(SECOND)/$(32BIT).json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
2027
# Step 3: Link everything
2128
create_img: build-rust build-asm
22-
$(OBJCPY) -I elf32-i386 -O binary target/$(16BIT)/release/RustOS 16bit.img
23-
$(OBJCPY) -I elf32-i386 -O binary target/$(32BIT)/release/RustOS 32bit.img
29+
$(OBJCPY) -I elf32-i386 -O binary target/$(16BIT)/release/$(FIRST) 16bit.img
30+
$(OBJCPY) -I elf32-i386 -O binary target/$(32BIT)/release/$(SECOND) 32bit.img
2431
cat 16bit.img 32bit.img > $(OUTPUT)
25-
# rm 16bit.img 32bit.img
32+
rm 16bit.img 32bit.img
33+
2634
# Step 4: Run in QEMU
2735
run: all
2836
@echo "Running bootloader in QEMU..."
@@ -31,7 +39,17 @@ run: all
3139
# Clean build artifacts
3240
clean:
3341
@echo "Cleaning build files..."
34-
$(CARGO) clean
35-
rm -f $(BOOT_BIN) $(OUTPUT) $(TARGET) *.img
42+
$(CARGO) clean
43+
rm -rf *.img
44+
45+
test:
46+
@echo "Compiling Rust code..."
47+
$(CARGO) build --release --manifest-path ./$(FIRST)/$(TOML) --target ./$(FIRST)/$(16BIT).json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
48+
$(CARGO) build --release --manifest-path ./$(TEST)/$(TOML) --target ./$(TEST)/$(16BIT).json -Zbuild-std=core -Zbuild-std-features=compiler-builtins-mem
49+
$(OBJCPY) -I elf32-i386 -O binary target/$(16BIT)/release/$(FIRST) 16bit.img
50+
$(OBJCPY) -I elf32-i386 -O binary target/$(16BIT)/release/$(TEST) test.img
51+
cat 16bit.img test.img > $(OUTPUT)
52+
rm 16bit.img test.img
53+
$(QEMU) -drive format=raw,file=$(OUTPUT)
3654

3755
.PHONY: all build-rust build-asm link run clean

first_stage/16bit.ld

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
ENTRY(.first_stage_entry)
1+
ENTRY(_start)
22

33
SECTIONS {
44

5-
.first_stage : { *(.first_stage) }
5+
. = 0x7c00;
6+
7+
.boot : { *(.boot) }
8+
9+
.start : { *(.start) }
10+
11+
.first_stage : { *(.first_stage)}
612

7-
. = 446;
13+
. = 0x7c00 + 446;
814

915
/* Make sure code doesn't override partition table */
1016
.partition_table : {
@@ -16,6 +22,6 @@ SECTIONS {
1622
/* Make sector bootable */
1723
.magic_number : { SHORT(0xaa55) }
1824

19-
. = 512;
25+
. = 0x7c00 +512;
2026

2127
}

first_stage/Cargo.toml

-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
8-
[profile.dev]
9-
panic = "abort"
10-
11-
[profile.release]
12-
panic = "abort"

first_stage/asm/boot.s

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
.section .first_stage_entry, "awx"
2-
.global .boot
1+
.section .boot, "awx"
2+
.global _start
33
.code16
44

55
# This stage initializes the stack, enables the A20 line
66

7-
.boot:
7+
_start:
88
# zero segment registers
99
xor ax, ax
1010
mov ds, ax
@@ -13,23 +13,37 @@
1313
mov fs, ax
1414
mov gs, ax
1515

16-
# clear the direction flag
17-
# (e.g. go forward in memory when using instructions like lodsb)
16+
# clear the direction flag (e.g. go forward in memory when using
17+
# instructions like lodsb)
1818
cld
1919

2020
# initialize stack
2121
mov sp, 0x7c00
2222

23-
.enable_a20:
23+
enable_a20:
2424
# enable A20-Line via IO-Port 92, might not work on all motherboards
25-
cli
2625
in al, 0x92
2726
test al, 2
2827
jnz enable_a20_after
2928
or al, 2
3029
and al, 0xFE
3130
out 0x92, al
32-
sti
33-
.enable_a20_after:
34-
.rust:
35-
call first_stage
31+
enable_a20_after:
32+
33+
check_int13h_extensions:
34+
mov ah, 0x41
35+
mov bx, 0x55aa
36+
# dl contains drive number
37+
int 0x13
38+
jnc .int13_pass
39+
.int13_pass:
40+
pop ax # pop error code again
41+
42+
rust:
43+
# push arguments
44+
push dx # disk number
45+
call first_stage
46+
# Fail code if first stage returns
47+
spin:
48+
hlt
49+
jmp spin

first_stage/build.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use std::path::Path;
22

33
fn main() {
44
let local_path = Path::new(env!("CARGO_MANIFEST_DIR"));
5-
5+
66
println!(
77
"cargo:rustc-link-arg-bins=--script={}",
88
local_path.join("16bit.ld").display()
99
)
10-
1110
}

first_stage/src/bios_enums.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ pub enum Color {
4444
Pink = 13,
4545
Yellow = 14,
4646
White = 15,
47-
}
47+
}

first_stage/src/constants.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
use crate::{first_stage, screen::ScreenChar, second_stage, global_descritor_table::GlobalDescriptorTable};
1+
// use crate::global_descritor_table::{GlobalDescriptorTable, GlobalDescriptorTableRegister32};
2+
3+
// #[unsafe(link_section = ".first_stage")]
4+
// pub static GLOBAL_DESCRIPTOR_TABLE: GlobalDescriptorTable = GlobalDescriptorTable::default();
5+
6+
// pub static GDTR: GlobalDescriptorTableRegister32 = GlobalDescriptorTableRegister32 {
7+
// limit: 0x24 - 1,
8+
// base: &GLOBAL_DESCRIPTOR_TABLE
9+
// };
210

3-
first_stage! {
4-
pub static GLOBAL_DESCRIPTOR_TABLE: GlobalDescriptorTable = GlobalDescriptorTable::default();
5-
}
611
pub const FIRST_STAGE_OFFSET: u16 = 0x7c00;
712
pub const MASTER_BOOT_RECORD_OFFSET: u16 = FIRST_STAGE_OFFSET + 446;
813
pub const SECOND_STAGE_OFFSET: u16 = FIRST_STAGE_OFFSET + 512;
9-
10-
second_stage! {
11-
pub static SCREEN_WIDTH: usize = 80;
12-
pub static SCREEN_HEIGHT: usize = 25;
13-
pub static BOOTABLE: &str = "Partition is bootable";
14-
pub static NOT_BOOTABLE: &str = "Partition is NOT bootable";
15-
}
16-
17-
pub const VGA_BUFFER_PTR: *mut ScreenChar = 0xb8000 as *mut ScreenChar;
18-
pub const KERNEL_START: *mut u8 = 0x10000 as *mut u8;

first_stage/src/disk.rs

+38-71
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{bios_enums::PacketSize, first_stage};
1+
use crate::bios_enums::PacketSize;
22
use core::arch::asm;
33

44
#[repr(C, packed)]
@@ -13,86 +13,53 @@ pub struct DiskAddressPacket {
1313
num_of_blocks: u16,
1414

1515
/// Which address in memory to save the data
16-
transfer_buffer: u32,
16+
memory_buffer: u16,
17+
18+
/// Memory segment for the address
19+
segment: u16,
1720

1821
/// The LBA address of the first sector
1922
abs_block_num: u64,
2023
}
2124

22-
#[repr(C, packed)]
23-
pub struct PartitionTableEntry {
24-
/// Boot indicator bit flag: 0 = no, 0x80 = bootable (or "active").
25-
pub bootable: u8,
26-
27-
/// Starting head of the partition.
28-
pub start_head: u8,
29-
30-
/// Bits 0-5 are the starting sector.
31-
/// Bits 6-16 are the starting cylinder.
32-
pub sector_cylider_start: u16,
33-
34-
/// SystemID.
35-
pub system_id: u8,
36-
37-
/// Ending head of the partition.
38-
pub end_head: u8,
39-
40-
/// Bits 0-5 are the ending sector.
41-
/// Bits 6-16 are the ending cylinder.
42-
pub sector_cylinder_head: u16,
43-
44-
/// Relative Sector (to start of partition -- also equals the partition's starting LBA value)
45-
pub relative_sector: u32,
46-
47-
/// Total Sectors in partition
48-
pub total_sectors: u32,
49-
}
50-
51-
pub struct MasterBootRecord {
52-
pub entries: [PartitionTableEntry; 4],
53-
}
54-
5525
impl DiskAddressPacket {
56-
57-
first_stage! {
5826

59-
pub fn new(
60-
packet_size: PacketSize,
61-
num_of_blocks: u16,
62-
transfer_buffer: u32,
63-
abs_block_num: u64,
64-
) -> Self {
65-
Self {
66-
packet_size: packet_size as u8,
67-
zero: 0,
68-
num_of_blocks,
69-
transfer_buffer,
70-
abs_block_num,
71-
}
72-
}
73-
74-
pub fn load(&self) {
75-
let self_address = self as *const Self as u32;
76-
unsafe {
77-
asm!(
78-
"push si", // si register is required for llvm it's content needs to be saved
79-
"mov si, {3:x}",
80-
"mov ah, {0}",
81-
"mov dl, {1}",
82-
"int {2}",
83-
"pop si",
84-
const 0x42u8,
85-
const 0x80u8,
86-
const 0x13u8,
87-
in(reg) self_address,
88-
)
89-
}
27+
#[unsafe(link_section = ".first_stage")]
28+
pub const fn new(
29+
num_of_blocks: u16,
30+
memory_buffer: u16,
31+
segment: u16,
32+
abs_block_num: u64,
33+
) -> Self {
34+
Self {
35+
packet_size: 0x10 as u8,
36+
zero: 0,
37+
num_of_blocks,
38+
memory_buffer,
39+
segment,
40+
abs_block_num,
9041
}
91-
9242
}
9343

94-
pub fn chs_to_lba() {
95-
todo!()
44+
#[unsafe(link_section = ".first_stage")]
45+
pub fn load(&self) {
46+
unsafe {
47+
asm!(
48+
"push si", // si register is required for llvm it's content needs to be saved
49+
"mov si, {3:x}",
50+
"mov ah, {0}",
51+
"mov dl, {1}",
52+
"int {2}",
53+
"pop si",
54+
const 0x42u8,
55+
const 0x80u8,
56+
const 0x13u8,
57+
in(reg) self as *const Self as u32,
58+
)
59+
}
9660
}
61+
}
9762

63+
pub fn chs_to_lba() {
64+
todo!()
9865
}

0 commit comments

Comments
 (0)