Skip to content

Commit 0a240f5

Browse files
committed
Initial commit
0 parents  commit 0a240f5

File tree

19 files changed

+496
-0
lines changed

19 files changed

+496
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.github/workflows/build.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Build Bootloader & App
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions-rs/toolchain@v1
15+
with:
16+
profile: minimal
17+
toolchain: stable
18+
- name: Build Bootloader
19+
run: cargo build --workspace --package bootloader
20+
- name: Build App
21+
run: cargo build --workspace --package app

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target/
2+
*.rs.bk
3+
*.swp
4+
*.zip

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[workspace]
2+
members = [
3+
"bootloader",
4+
"app",
5+
]

README.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# M2 Bootloader Rust
2+
3+
[![Rust](https://img.shields.io/badge/rust-nightly-orange.svg)](https://www.rust-lang.org/)
4+
5+
## Overview
6+
7+
This repository provides a **Rust-based bootloader** and an example **IoT application** for microcontrollers (STM32F4 series as a reference).
8+
The bootloader is designed to enable:
9+
10+
- **Secure firmware updates** (UART, USB, or OTA-ready)
11+
- **Firmware integrity verification** using CRC or SHA256
12+
- **Fallback mechanism** for invalid firmware
13+
- Minimal dependency on external runtime (`#![no_std]`)
14+
15+
This project serves as a **starting template** for embedded developers looking to build robust IoT bootloaders in Rust.
16+
17+
---
18+
19+
## Features
20+
21+
- Bare-metal Rust bootloader (`#![no_std]`)
22+
- Hardware initialization module (`init.rs`)
23+
- Flash read/write routines (`flash.rs`)
24+
- Firmware verification (`verify.rs`)
25+
- Update handling (`updater.rs`)
26+
- Example IoT application (`app/`)
27+
- Cross-platform scripts for flashing and verification
28+
- Ready-to-use GitHub Actions CI for building bootloader and app
29+
30+
---
31+
32+
## Repository Structure
33+
34+
```
35+
iot-bootloader-rust/
36+
37+
├─ Cargo.toml # Workspace manifest
38+
├─ rust-toolchain.toml # Rust version pinning
39+
├─ README.md
40+
├─ .gitignore
41+
├─ .github/workflows/build.yml # CI/CD workflow
42+
43+
├─ bootloader/ # Bootloader crate
44+
│ ├─ Cargo.toml
45+
│ └─ src/
46+
│ ├─ main.rs
47+
│ ├─ init.rs
48+
│ ├─ flash.rs
49+
│ ├─ updater.rs
50+
│ └─ verify.rs
51+
52+
├─ app/ # IoT Application crate
53+
│ ├─ Cargo.toml
54+
│ └─ src/
55+
│ ├─ main.rs
56+
│ └─ peripherals.rs
57+
58+
├─ scripts/ # Flashing and verification scripts
59+
│ ├─ flash.sh
60+
│ └─ check_firmware.sh
61+
62+
├─ examples/ # Example applications for testing
63+
│ └─ blinky.rs
64+
65+
└─ docs/
66+
└─ memory_map.md # MCU flash layout and memory map
67+
```
68+
69+
---
70+
71+
## Getting Started
72+
73+
### Prerequisites
74+
75+
- **Rust toolchain** (stable recommended)
76+
- `cargo` and `rustup`
77+
- Embedded development tools:
78+
- `probe-rs` CLI for flashing
79+
- STM32 USB/UART programmer
80+
- Optional: VSCode + Rust Analyzer for development
81+
82+
### Build Bootloader
83+
84+
```bash
85+
cd bootloader
86+
cargo build --release --target thumbv7em-none-eabihf
87+
```
88+
89+
### Build Application
90+
91+
```bash
92+
cd app
93+
cargo build --release --target thumbv7em-none-eabihf
94+
```
95+
96+
### Flash Firmware
97+
98+
```bash
99+
./scripts/flash.sh
100+
```
101+
102+
### Verify Firmware
103+
104+
```bash
105+
./scripts/check_firmware.sh
106+
```
107+
108+
---
109+
110+
## Bootloader Workflow
111+
112+
```
113+
+----------------+
114+
| MCU Reset |
115+
+----------------+
116+
|
117+
v
118+
+----------------+
119+
| Bootloader |
120+
| init hardware |
121+
+----------------+
122+
|
123+
v
124+
+----------------+
125+
| Verify firmware |
126+
+----------------+
127+
|
128+
v
129+
+-------------------------+
130+
| Check for update |
131+
| (UART/USB/OTA) |
132+
+-------------------------+
133+
|
134+
v
135+
+-------------------------+
136+
| Apply update if present |
137+
+-------------------------+
138+
|
139+
v
140+
+-------------------------+
141+
| Jump to application |
142+
+-------------------------+
143+
|
144+
v
145+
+-------------------------+
146+
| Fallback if failure |
147+
+-------------------------+
148+
```
149+
150+
---
151+
152+
## Memory Layout
153+
154+
```
155+
Bootloader: 0x08000000 - 0x08003FFF
156+
Application: 0x08004000 - 0x080FFFFF
157+
Backup: 0x08100000 - 0x08103FFF
158+
```
159+
160+
---
161+
162+
## Example Snippets
163+
164+
### Bootloader Main
165+
166+
```rust
167+
#![no_std]
168+
#![no_main]
169+
170+
use cortex_m_rt::entry;
171+
172+
mod init;
173+
mod flash;
174+
mod updater;
175+
mod verify;
176+
177+
#[entry]
178+
fn main() -> ! {
179+
init::init_hardware();
180+
181+
if verify::firmware_valid() {
182+
updater::check_for_update();
183+
jump_to_app();
184+
} else {
185+
loop {}
186+
}
187+
}
188+
189+
fn jump_to_app() -> ! {
190+
const APP_START_ADDRESS: u32 = 0x0800_4000;
191+
let app: extern "C" fn() = unsafe { core::mem::transmute(APP_START_ADDRESS) };
192+
app();
193+
}
194+
```
195+
196+
### Application Main
197+
198+
```rust
199+
#![no_std]
200+
#![no_main]
201+
202+
use cortex_m_rt::entry;
203+
mod peripherals;
204+
205+
#[entry]
206+
fn main() -> ! {
207+
peripherals::init_led();
208+
209+
loop {
210+
peripherals::toggle_led();
211+
}
212+
}
213+
```
214+
215+
### Peripheral Example
216+
217+
```rust
218+
pub fn init_led() {}
219+
pub fn toggle_led() {}
220+
```
221+
222+
---
223+
224+
## Future Development
225+
226+
- Add **OTA over Wi-Fi or BLE**
227+
- Secure boot with **digital signature verification**
228+
- Dual-bank firmware for **rollback support**
229+
- Support additional MCUs (ESP32, nRF52)
230+
- Add **unit tests and CI/CD for embedded targets**
231+
232+
## License
233+
234+
![Apache 2.0 License](https://img.shields.io/badge/License-Apache%202.0-blue?style=flat-square)
235+
![Dual License](https://img.shields.io/badge/License-Dual%20License-green?style=flat-square)
236+
237+
This project is **dual-licensed**:
238+
239+
- **Open-Source / Personal Use:** Apache 2.0
240+
- **Commercial / Closed-Source Use:** Proprietary license required
241+
242+
For commercial licensing inquiries or enterprise use, please contact: [[email protected]](mailto:[email protected])
243+
244+
245+
## Author
246+
247+
**Md Mahbubur Rahman**
248+
[GitHub](https://github.com/m-a-h-b-u-b) | [Website](https://m-a-h-b-u-b.github.io)
249+
250+
---
251+
252+
## Contributing
253+
254+
We welcome contributions!
255+
256+
* Fork the repo and submit pull requests
257+
* Follow Rust coding guidelines and safety best practices
258+
* Report issues or suggest features via GitHub Issues
259+
260+
---
261+
262+
# Further Reading / References
263+
264+
1. [Developing a Cryptographically Secure Bootloader for RISC-V in Rust](https://www.codethink.co.uk/articles/2024/secure_bootloader/)
265+
2. [Low-Power Secure Booting for IoT Edge Devices](https://www.researchgate.net/publication/393623129_Low-Power_Secure_Booting_for_IoT_Edge_Devices)
266+
3. [Secure Boot and Root-of-Trust in Heterogeneous SoCs](https://www.researchgate.net/publication/395172368_A_Comprehensive_Analysis_of_Secure_Boot_and_Root-of-Trust_Implementation_in_Heterogeneous_SoCs_with_a_RISC-V_Secure_Enclave)
267+
4. [Design and Implementation of a Secure Bootloader Using Public Key Cryptography](https://www.researchgate.net/publication/390574601_Design_and_Implementation_of_a_Secure_Bootloader_Using_Public_Key_Cryptography_Research)
268+
5. [Rust for Embedded Systems: Current State and Open Problems](https://arxiv.org/pdf/2311.05063)
269+
6. [Rust for Security and Correctness in the Embedded World](https://www.nccgroup.com/us/research-blog/rust-for-security-and-correctness-in-the-embedded-world/)
270+
7. [Building Safe and Secure Software with Rust on Arm](https://semiengineering.com/building-safe-and-secure-software-with-rust-on-arm/)
271+
8. [Building a Secure Operating System (Redox OS) with Rust](https://changelog.com/podcast/280)
272+
9. [Rust vs C: Language Choices in Embedded Systems and Cryptography](https://www.wolfssl.com/rust-vs-c-navigating-language-choices-in-embedded-systems-and-cryptography/)
273+
10. [Twine: An Embedded Trusted Runtime for WebAssembly](https://arxiv.org/abs/2103.15860)
274+
11. [Ferrocene: Rust Toolchain for Safety-Critical Applications](https://ferrocene.dev/)
275+
12. [Embedded Rust Book](https://docs.rust-embedded.org/book/)
276+
13. [Rust Embedded Working Group](https://github.com/rust-embedded/wg)
277+
14. [Rust Embedded HAL](https://github.com/rust-embedded/embedded-hal)
278+
15. [probe-rs: Debug Probe Library for Rust](https://github.com/probe-rs/probe-rs)

app/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "app"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
cortex-m = "0.7"
8+
cortex-m-rt = "0.7"
9+
stm32f4xx-hal = { version = "0.15", features = ["stm32f411", "rt"] }
10+
panic-halt = "0.2"

app/src/main.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//! M2 Bootloader RUST
2+
//! ------------------
3+
//! License : Dual License
4+
//! - Apache 2.0 for open-source / personal use
5+
//! - Commercial license required for closed-source use
6+
//! Author : Md Mahbubur Rahman
7+
//! URL : <https://m-a-h-b-u-b.github.io>
8+
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-RUST>
9+
10+
11+
#![no_std]
12+
#![no_main]
13+
14+
use cortex_m_rt::entry;
15+
mod peripherals;
16+
17+
#[entry]
18+
fn main() -> ! {
19+
peripherals::init_led();
20+
21+
loop {
22+
peripherals::toggle_led();
23+
}
24+
}

app/src/peripherals.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! M2 Bootloader RUST
2+
//! ------------------
3+
//! License : Dual License
4+
//! - Apache 2.0 for open-source / personal use
5+
//! - Commercial license required for closed-source use
6+
//! Author : Md Mahbubur Rahman
7+
//! URL : <https://m-a-h-b-u-b.github.io>
8+
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-RUST>
9+
10+
pub fn init_led() {}
11+
pub fn toggle_led() {}

bootloader/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "bootloader"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
cortex-m = "0.7"
8+
cortex-m-rt = "0.7"
9+
embedded-hal = "1.0.0"
10+
stm32f4xx-hal = { version = "0.15", features = ["stm32f411", "rt"] }
11+
sha2 = "0.10"
12+
crc-any = "2.0"
13+
defmt = "0.4"
14+
panic-halt = "0.2"

bootloader/src/flash.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! M2 Bootloader RUST
2+
//! ------------------
3+
//! License : Dual License
4+
//! - Apache 2.0 for open-source / personal use
5+
//! - Commercial license required for closed-source use
6+
//! Author : Md Mahbubur Rahman
7+
//! URL : <https://m-a-h-b-u-b.github.io>
8+
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-RUST>
9+
10+
pub fn read_flash(addr: u32, buf: &mut [u8]) {}
11+
pub fn write_flash(addr: u32, data: &[u8]) {}

0 commit comments

Comments
 (0)