Skip to content
Open
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ hw0/
*.elf
*.img
target/
kernel*.img
kernel*.img

initramfs.cpio
26 changes: 26 additions & 0 deletions bootloader/Cargo.lock

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

29 changes: 29 additions & 0 deletions bootloader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "bootloader"
version = "0.1.0"
authors = ["HelloHomework"]
edition = "2021"

[[bin]]
name = "bootloader"
path = "src/main.rs"
test = false
bench = false

[lib]
test = false

#[toolchain]
#channel = "nightly"

[features]
default = ["tock-registers"]

[workspace]
resolver = "2"

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

[dependencies]
aarch64-cpu = "9.4.0"
tock-registers = { version = "0.8.x", default-features = false, features = ["register_types"], optional = true }
127 changes: 127 additions & 0 deletions bootloader/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
## SPDX-License-Identifier: MIT OR Apache-2.0
##
## Copyright (c) 2018-2023 Andre Richter <andre.o.richter@gmail.com>
##
## This file is part of the `rust-raspberrypi-OS-tutorials` project.
## https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials/
##
##--------------------------------------------------------------------------------------------------
## Optional, user-provided configuration values
##--------------------------------------------------------------------------------------------------

# BSP ?= rpi3
TARGET = aarch64-unknown-none-softfloat
KERNEL_BIN = bootloader.img
QEMU_BINARY = qemu-system-aarch64
QEMU_MACHINE_TYPE = raspi3
QEMU_RELEASE_ARGS = -display none -d in_asm

# LD_SCRIPT_PATH = src/
# LD_SCRIPT_NAME = linker.ld
LD_SCRIPT_NAME = kernel.ld

# LD_SCRIPT_PATH = $(shell pwd)/src/bsp/raspberrypi
LD_SCRIPT_PATH = src/
KERNEL_LINKER_SCRIPT = $(LD_SCRIPT_PATH)bsp/rpi3/$(LD_SCRIPT_NAME)


RUSTC_MISC_ARGS = -C target-cpu=cortex-a53

# Export for build.rs.
export LD_SCRIPT_PATH



##--------------------------------------------------------------------------------------------------
## Targets and Prerequisites
##--------------------------------------------------------------------------------------------------
KERNEL_MANIFEST = Cargo.toml
# KERNEL_LINKER_SCRIPT = kernel.ld
LAST_BUILD_CONFIG = target/$(BSP).build_config

KERNEL_ELF = target/$(TARGET)/debug/bootloader
# KERNEL_ELF = target/$(TARGET)/release/kernel
# This parses cargo's dep-info file.
# https://doc.rust-lang.org/cargo/guide/build-cache.html#dep-info-files
KERNEL_ELF_DEPS = $(filter-out %: ,$(file < $(KERNEL_ELF).d)) $(KERNEL_MANIFEST) $(LAST_BUILD_CONFIG)



##--------------------------------------------------------------------------------------------------
## Command building blocks
##--------------------------------------------------------------------------------------------------
RUSTFLAGS = $(RUSTC_MISC_ARGS) \
-C link-arg=--library-path=$(LD_SCRIPT_PATH) \
-C link-arg=--script=$(KERNEL_LINKER_SCRIPT)

RUSTFLAGS_PEDANTIC = $(RUSTFLAGS) \
-D warnings \
-D missing_docs

# FEATURES = --features bsp_$(BSP)
COMPILER_ARGS = --target=$(TARGET) \
# --release
# $(FEATURES)\


RUSTC_CMD = cargo rustc $(COMPILER_ARGS)
# RUSTC_CMD = cargo miri $(COMPILER_ARGS)
OBJCOPY_CMD = rust-objcopy \
--strip-all \
-O binary

##--------------------------------------------------------------------------------------------------
## Targets
##--------------------------------------------------------------------------------------------------

all: $(KERNEL_BIN)

##------------------------------------------------------------------------------
## Save the configuration as a file, so make understands if it changed.
##------------------------------------------------------------------------------
$(LAST_BUILD_CONFIG):
@rm -f target/*.build_config
@mkdir -p target
@touch $(LAST_BUILD_CONFIG)

##------------------------------------------------------------------------------
## Compile the kernel ELF
##------------------------------------------------------------------------------
$(KERNEL_ELF): $(KERNEL_ELF_DEPS)
# $(call color_header, "Compiling kernel ELF - $(BSP)")
@RUSTFLAGS="$(RUSTFLAGS)" $(RUSTC_CMD)

##------------------------------------------------------------------------------
## Generate the stripped kernel binary
##------------------------------------------------------------------------------
$(KERNEL_BIN): $(KERNEL_ELF)
$(call color_header, "Generating stripped binary")
@$(OBJCOPY_CMD) $(KERNEL_ELF) $(KERNEL_BIN)
$(call color_progress_prefix, "Name")
@echo $(KERNEL_BIN)
$(call color_progress_prefix, "Size")
$(call disk_usage_KiB, $(KERNEL_BIN))


##------------------------------------------------------------------------------
## Debug
##------------------------------------------------------------------------------
debug:
# qemu-system-aarch64 -M raspi3b -kernel $(KERNEL_BIN) -display none -serial null -serial stdio
# qemu-system-aarch64 -M raspi3b -kernel $(KERNEL_BIN) -display none -serial stdio
qemu-system-aarch64 -M raspi3b -kernel $(KERNEL_BIN) \
-display none -serial null -serial pty \
-dtb ../resources/bcm2710-rpi-3-b-plus.dtb \
-initrd ../resources/initramfs.cpio

# debug:
# qemu-system-aarch64 -M raspi3b -kernel $(KERNEL_BIN) \
# -display none -serial null -serial pty \
# -dtb ../resources/bcm2710-rpi-3-b-plus.dtb \
# -initrd ../resources/initramfs.cpio

##------------------------------------------------------------------------------
## Clean
##------------------------------------------------------------------------------
clean:
rm -rf target $(KERNEL_BIN)
File renamed without changes.
51 changes: 51 additions & 0 deletions bootloader/src/arrsting.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// #[derive(PartialEq)]
const MAXIUM_LEN: usize = 256;
pub struct ArrString {
data: [u8; MAXIUM_LEN], // maxium size MAXIUM_LEN
len: usize,
}

#[allow(dead_code)]
impl ArrString {
pub fn new(s: &str) -> Self {
let mut data: [u8; MAXIUM_LEN] = [0; MAXIUM_LEN];
for (index, c) in s.chars().enumerate() {
data[index] = c as u8;
}
ArrString {
data: data,
len: s.len(),
}
}

pub fn get_len(&self) -> usize {
self.len
}

pub fn get_data(&self) -> [u8 ;MAXIUM_LEN] {
self.data
}

pub fn clean_buf(&mut self) {
for x in self.data.as_mut() {
*x = 0 as u8;
}
self.len = 0;
}

pub fn push_char(&mut self, c: char) {
self.data[self.len] = c as u8;
self.len += 1;
}

fn compare(&self, other: &Self) -> bool {
self.data.starts_with(&other.data)
}
}

#[allow(dead_code)]
impl PartialEq for ArrString {
fn eq(&self, other: &Self) -> bool {
self.compare(other)
}
}
File renamed without changes.
4 changes: 4 additions & 0 deletions bootloader/src/bsp/device_driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod bcm;
mod common;

pub use bcm::*;
10 changes: 10 additions & 0 deletions bootloader/src/bsp/device_driver/bcm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
mod bcm2xxx_gpio;
// mod bcm2xxx_pl011_uart;
mod bcm2xxx_mini_uart;
mod bcm2xxx_mbox;


pub use bcm2xxx_gpio::*;
// pub use bcm2xxx_pl011_uart::*;
pub use bcm2xxx_mini_uart::*;
pub use bcm2xxx_mbox::*;
Loading