Skip to content

Commit

Permalink
Add baremetal target with support for 32bit RPI4
Browse files Browse the repository at this point in the history
  • Loading branch information
alloncm committed May 19, 2023
1 parent b71d03b commit 50ef758
Show file tree
Hide file tree
Showing 27 changed files with 1,819 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ members = [
"gb",
"lib_gb",
"image_inter",
"bcm_host"
"bcm_host",
"baremetal"
]

[workspace.package]
Expand Down
6 changes: 6 additions & 0 deletions baremetal/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
target = "armv7a-none-eabihf"
rustflags = [
"-Clink-arg=--script=./baremetal/link.ld",
"-Ctarget-feature=+virtualization"
]
17 changes: 17 additions & 0 deletions baremetal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "baremetal"
version.workspace = true
authors.workspace = true
edition = "2021"

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

[dependencies]
log = "0.4"
lib_gb = {path = "../lib_gb", features = ["u16pixel"]}
image_inter = {path = "../image_inter"}

[features]
default = ["rpi4"]
rpi4 = []
rpi2 = []
2 changes: 2 additions & 0 deletions baremetal/build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cargo +nightly b -r -Z build-std=core
rust-objcopy ../target/armv7a-none-eabihf/release/baremetal -O binary kernel7.img
5 changes: 5 additions & 0 deletions baremetal/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const LD_SCRIPT_PATH:&str = "link.ld";

fn main(){
println!("cargo:rerun-if-changed={}", LD_SCRIPT_PATH);
}
3 changes: 3 additions & 0 deletions baremetal/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# armv7a-none-eabihf is not supported automacticaly by rust so the nightly toolchain is neccessary to build the core library
cargo +nightly b -r -Z build-std=core
rust-objcopy ../target/armv7a-none-eabihf/release/baremetal -O binary kernel7.img
7 changes: 7 additions & 0 deletions baremetal/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# configuration for the RPI
arm_64bit=0 # boot to 32 bit mode

# fast boot
boot_delay=0
disable_poe_fan=1
disable_splash=1
60 changes: 60 additions & 0 deletions baremetal/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* Place _start procedure at the entry address for RPI */
__rpi_32_phys_binary_load_addr = 0x8000;
__isr_table_addr = 0;
__stack_size = 0x100000; /* 1MB stack */
ENTRY(__rpi_32_phys_binary_load_addr) /* enry point */

SECTIONS
{
.isr_table (NOLOAD) :
{
. = __isr_table_addr;
/* allocate space for the table */
. = . + 0x40;
}
. = __rpi_32_phys_binary_load_addr;
.text :
{
KEEP(*(.text._start)) /*put _start first, `KEEP` disables linker optimizations*/
*(.text*)
}
/*readonly data - readonly global variables*/
.rodata :
{
*(.rodata*)
}
/*global variables*/
.data :
{
*(.data*)
}
.stack (NOLOAD) : ALIGN(16)
{
. = . + __stack_size;
__cpu0_stack_start = .; /* stack grows from high address to low address */
}
/*bss must be at the end of the linker script in order to keep the image small (otherwise objcopy will gap the space between bss and the next section)*/
/*uninitialized global variables*/
.bss (NOLOAD) : ALIGN(16)
{
__bss_start = .;
*(.bss*)
__bss_end = .;
}
/* uncached memory - used by drivers and devices */
/* Since L1 memory map entry is 1MB, make sure it's alligned correctly*/
.uncached (NOLOAD) : ALIGN(0x100000)
{
__uncached_data_start = .;
__cached_data_map_size = __uncached_data_start / 0x100000;
*(.uncached*)
}

/* Remove those sections from the final binary */
/DISCARD/ :
{
*(.ARM.attributes)
*(.ARM.exidx) /* Used for stack unwinding - not relevant for now */
*(.comment) /* comments about the compiler and linker - not intresting */
}
}
18 changes: 18 additions & 0 deletions baremetal/src/boot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use core::arch::{global_asm, asm};

#[no_mangle]
static PERIPHERALS_BASE_ADDRESS:u32 = crate::peripherals::PERIPHERALS_BASE_ADDRESS as u32;

global_asm!(include_str!("boot_armv7a.s"));

extern "C"{
// declared at startup assembly file
pub fn hang_led()->!;
}

pub fn get_cpu_execution_mode()->u32{
let mut mode:u32;
unsafe{asm!("mrs {r}, cpsr", r = out(reg) mode)};
// only the first 5 bits are relevant for the mode
return mode & 0b1_1111;
}
Loading

0 comments on commit 50ef758

Please sign in to comment.