From 1b9b5602420a7dfde8c9433481cb10f070aca840 Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 28 Dec 2017 08:05:38 +0000 Subject: [PATCH] firmware: use libbuild_misoc in libdrtioaux. NFC. --- artiq/firmware/Cargo.lock | 32 +++++++++++ artiq/firmware/Cargo.toml | 2 +- artiq/firmware/bootloader/Cargo.toml | 18 ++++++ artiq/firmware/bootloader/Makefile | 18 ++++++ artiq/firmware/bootloader/bootloader.ld | 46 ++++++++++++++++ artiq/firmware/bootloader/build.rs | 6 ++ artiq/firmware/bootloader/main.rs | 73 +++++++++++++++++++++++++ artiq/firmware/libdrtioaux/Cargo.toml | 3 + artiq/firmware/libdrtioaux/build.rs | 14 +---- artiq/gateware/amp/soc.py | 1 + 10 files changed, 200 insertions(+), 13 deletions(-) create mode 100644 artiq/firmware/bootloader/Cargo.toml create mode 100644 artiq/firmware/bootloader/Makefile create mode 100644 artiq/firmware/bootloader/bootloader.ld create mode 100644 artiq/firmware/bootloader/build.rs create mode 100644 artiq/firmware/bootloader/main.rs diff --git a/artiq/firmware/Cargo.lock b/artiq/firmware/Cargo.lock index aa3483f37..dd4bf58f7 100644 --- a/artiq/firmware/Cargo.lock +++ b/artiq/firmware/Cargo.lock @@ -42,6 +42,16 @@ dependencies = [ "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "bootloader" +version = "0.0.0" +dependencies = [ + "board 0.0.0", + "build_artiq 0.0.0", + "crc 1.6.0 (git+git://github.com/whitequark/crc-rs?rev=51cd356)", + "rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "build_artiq" version = "0.0.0" @@ -49,6 +59,11 @@ dependencies = [ "walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "build_const" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "build_misoc" version = "0.0.0" @@ -77,6 +92,14 @@ name = "compiler_builtins" version = "0.1.0" source = "git+https://github.com/m-labs/compiler-builtins?rev=97916b1#97916b17ca542eac0524b8570c7d05913891a0dc" +[[package]] +name = "crc" +version = "1.6.0" +source = "git+git://github.com/whitequark/crc-rs?rev=51cd356#51cd3560aa9d3823061f2bb46797d8c61f4cfa9e" +dependencies = [ + "build_const 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "cslice" version = "0.3.0" @@ -87,6 +110,7 @@ name = "drtioaux" version = "0.0.0" dependencies = [ "board 0.0.0", + "build_misoc 0.0.0", "byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", "std_artiq 0.0.0", @@ -174,6 +198,11 @@ dependencies = [ "std_artiq 0.0.0", ] +[[package]] +name = "rlibc" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "runtime" version = "0.0.0" @@ -261,10 +290,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" +"checksum build_const 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e90dc84f5e62d2ebe7676b83c22d33b6db8bd27340fb6ffbff0a364efa0cb9c9" "checksum byteorder 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "652805b7e73fada9d85e9a6682a4abd490cb52d96aeecc12e33a0de34dfd0d23" "checksum cc 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a9b13a57efd6b30ecd6598ebdb302cca617930b5470647570468a65d12ef9719" "checksum compiler_builtins 0.1.0 (git+https://github.com/m-labs/compiler-builtins?rev=97916b1)" = "" "checksum compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins?rev=631b568)" = "" +"checksum crc 1.6.0 (git+git://github.com/whitequark/crc-rs?rev=51cd356)" = "" "checksum cslice 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0f8cb7306107e4b10e64994de6d3274bd08996a7c1322a27b86482392f96be0a" "checksum fringe 1.1.0 (git+https://github.com/m-labs/libfringe?rev=bd23494)" = "" "checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb" @@ -273,6 +304,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b" "checksum log_buffer 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ec57723b84bbe7bdf76aa93169c9b59e67473317c6de3a83cb2a0f8ccb2aa493" "checksum managed 0.4.0 (git+https://github.com/m-labs/rust-managed.git?rev=629a6786a1cf1692015f464ed16c04eafa5cb8d1)" = "" +"checksum rlibc 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe" "checksum rustc-cfg 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "56a596b5718bf5e059d59a30af12f7f462a152de147aa462b70892849ee18704" "checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7" "checksum smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=507d2fe)" = "" diff --git a/artiq/firmware/Cargo.toml b/artiq/firmware/Cargo.toml index af13ba5ff..0f2007873 100644 --- a/artiq/firmware/Cargo.toml +++ b/artiq/firmware/Cargo.toml @@ -1,2 +1,2 @@ [workspace] -members = ["runtime", "ksupport", "satman"] +members = ["bootloader", "runtime", "ksupport", "satman"] diff --git a/artiq/firmware/bootloader/Cargo.toml b/artiq/firmware/bootloader/Cargo.toml new file mode 100644 index 000000000..cbf8ccef7 --- /dev/null +++ b/artiq/firmware/bootloader/Cargo.toml @@ -0,0 +1,18 @@ +[package] +authors = ["M-Labs"] +name = "bootloader" +version = "0.0.0" +build = "build.rs" + +[lib] +name = "bootloader" +crate-type = ["staticlib"] +path = "main.rs" + +[build-dependencies] +build_artiq = { path = "../libbuild_artiq" } + +[dependencies] +rlibc = "1.0" +crc = { git = "git://github.com/whitequark/crc-rs", rev = "51cd356", default-features = false } +board = { path = "../libboard", features = ["uart_console"] } diff --git a/artiq/firmware/bootloader/Makefile b/artiq/firmware/bootloader/Makefile new file mode 100644 index 000000000..f247c0994 --- /dev/null +++ b/artiq/firmware/bootloader/Makefile @@ -0,0 +1,18 @@ +include ../include/generated/variables.mak +include $(MISOC_DIRECTORY)/software/common.mak + +RUSTFLAGS += -Cpanic=abort + +all:: bootloader.bin + +.PHONY: $(RUSTOUT)/libbootloader.a +$(RUSTOUT)/libbootloader.a: + $(cargo) --manifest-path $(BOOTLOADER_DIRECTORY)/Cargo.toml -- \ + -Clto + +bootloader.elf: $(RUSTOUT)/libbootloader.a + $(link) -T $(BOOTLOADER_DIRECTORY)/bootloader.ld + +%.bin: %.elf + $(objcopy) -O binary + $(MSCIMG) $@ diff --git a/artiq/firmware/bootloader/bootloader.ld b/artiq/firmware/bootloader/bootloader.ld new file mode 100644 index 000000000..8fe809a49 --- /dev/null +++ b/artiq/firmware/bootloader/bootloader.ld @@ -0,0 +1,46 @@ +INCLUDE generated/output_format.ld +INCLUDE generated/regions.ld +ENTRY(_reset_handler) + +SECTIONS +{ + .vectors : + { + _begin = .; + *(.vectors) + } > rom + + .text : + { + *(.text .text.*) + } > rom + + .rodata : + { + *(.rodata.*) + . = ALIGN(4); + _end = .; + } > rom + + .crc ALIGN(4) : + { + _crc = .; + . += 4; + } + + .bss ALIGN(4) : + { + _fbss = .; + *(.bss .bss.*) + . = ALIGN(4); + _ebss = .; + } > sram + + .stack : + { + /* Ensure we have a certain amount of space available for stack. */ + . = ORIGIN(sram) + LENGTH(sram) - 0x800; + . = ORIGIN(sram) + LENGTH(sram) - 4; + _fstack = .; + } > sram +} diff --git a/artiq/firmware/bootloader/build.rs b/artiq/firmware/bootloader/build.rs new file mode 100644 index 000000000..0a7d63df9 --- /dev/null +++ b/artiq/firmware/bootloader/build.rs @@ -0,0 +1,6 @@ +extern crate build_artiq; + +fn main() { + build_artiq::misoc_cfg(); + build_artiq::git_describe(); +} diff --git a/artiq/firmware/bootloader/main.rs b/artiq/firmware/bootloader/main.rs new file mode 100644 index 000000000..ec9f54bbf --- /dev/null +++ b/artiq/firmware/bootloader/main.rs @@ -0,0 +1,73 @@ +#![no_std] +#![feature(lang_items)] + +extern crate rlibc; +extern crate crc; +#[macro_use] +extern crate board; + +use core::slice; + +fn check_integrity() -> bool { + extern { + static _begin: u8; + static _end: u8; + static _crc: u32; + } + + unsafe { + let length = &_end as *const u8 as usize - + &_begin as *const u8 as usize; + let bios = slice::from_raw_parts(&_begin as *const u8, length); + crc::crc32::checksum_ieee(bios) == _crc + } +} + +fn initialize_sdram() -> bool { + unsafe { + board::sdram_phy::initialize(); + + true + } +} + +#[no_mangle] +pub extern fn main() -> i32 { + println!(""); + println!(r" _ ____ _____ ___ ___ "); + println!(r" / \ | _ \_ _|_ _/ _ \ "); + println!(r" / _ \ | |_) || | | | | | |"); + println!(r" / ___ \| _ < | | | | |_| |"); + println!(r"/_/ \_\_| \_\|_| |___\__\_\"); + println!(""); + println!("ARTIQ Bootloader"); + println!("Copyright (c) 2017 M-Labs Limited"); + println!("Version {}", include_str!(concat!(env!("OUT_DIR"), "/git-describe"))); + println!(""); + + if !check_integrity() { + panic!("Bootloader CRC failed"); + } else { + println!("Bootloader CRC passed"); + } + + if !initialize_sdram() { + panic!("SDRAM initialization failed") + } else { + println!("SDRAM initialized"); + } + + loop {} +} + +#[no_mangle] +pub extern fn exception(vect: u32, _regs: *const u32, pc: u32, ea: u32) { + panic!("exception {} at PC {:#08x}, EA {:#08x}", vect, pc, ea) +} + +#[no_mangle] +#[lang = "panic_fmt"] +pub extern fn panic_fmt(args: core::fmt::Arguments, file: &'static str, line: u32) -> ! { + println!("panic at {}:{}: {}", file, line, args); + loop {} +} diff --git a/artiq/firmware/libdrtioaux/Cargo.toml b/artiq/firmware/libdrtioaux/Cargo.toml index 682d97316..def5c328b 100644 --- a/artiq/firmware/libdrtioaux/Cargo.toml +++ b/artiq/firmware/libdrtioaux/Cargo.toml @@ -8,6 +8,9 @@ build = "build.rs" name = "drtioaux" path = "lib.rs" +[build-dependencies] +build_misoc = { path = "../libbuild_misoc" } + [dependencies] log = { version = "0.3", default-features = false } std_artiq = { path = "../libstd_artiq", features = ["alloc"] } diff --git a/artiq/firmware/libdrtioaux/build.rs b/artiq/firmware/libdrtioaux/build.rs index a7b0335e5..3548ea5ff 100644 --- a/artiq/firmware/libdrtioaux/build.rs +++ b/artiq/firmware/libdrtioaux/build.rs @@ -1,15 +1,5 @@ -use std::env; -use std::path::Path; -use std::io::{BufRead, BufReader}; -use std::fs::File; +extern crate build_misoc; fn main() { - let out_dir = env::var("BUILDINC_DIRECTORY").unwrap(); - let cfg_path = Path::new(&out_dir).join("generated").join("rust-cfg"); - println!("cargo:rerun-if-changed={}", cfg_path.to_str().unwrap()); - - let f = BufReader::new(File::open(&cfg_path).unwrap()); - for line in f.lines() { - println!("cargo:rustc-cfg={}", line.unwrap()); - } + build_misoc::cfg(); } diff --git a/artiq/gateware/amp/soc.py b/artiq/gateware/amp/soc.py index d237822eb..16ab97396 100644 --- a/artiq/gateware/amp/soc.py +++ b/artiq/gateware/amp/soc.py @@ -48,6 +48,7 @@ def build_artiq_soc(soc, argdict): builder = Builder(soc, **argdict) builder.add_software_package("libm") builder.add_software_package("libunwind") + builder.add_software_package("bootloader", os.path.join(artiq_dir, "firmware", "bootloader")) builder.add_software_package("ksupport", os.path.join(artiq_dir, "firmware", "ksupport")) builder.add_software_package("runtime", os.path.join(artiq_dir, "firmware", "runtime")) try: