Skip to content

Commit 7596c3f

Browse files
committed
Add bootloader main entry point
Implement bootloader/src/main.rs with: - no_std and no_main configuration for bare-metal execution - Panic handler that halts the system - main() function coordinating: * hardware initialization * firmware update checking and application * placeholder chunk writing for simulation * jump to application stub - jump_to_application() stub for vector table relocation - Ready for integration with flash, verify, init, and updater modules Provides the top-level execution flow for the M2 bootloader.
1 parent 2bd0e47 commit 7596c3f

File tree

1 file changed

+62
-20
lines changed

1 file changed

+62
-20
lines changed

bootloader/src/main.rs

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,78 @@
1-
//! M2 Bootloader RUST
1+
//! M2 Bootloader RUST
22
//! ------------------
33
//! License : Dual License
44
//! - Apache 2.0 for open-source / personal use
55
//! - Commercial license required for closed-source use
66
//! Author : Md Mahbubur Rahman
77
//! URL : <https://m-a-h-b-u-b.github.io>
8-
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-RUST>
8+
//! GitHub : <https://github.com/m-a-h-b-u-b/M2-Bootloader-Rust>
9+
10+
//! Main entry point for the bootloader.
11+
//!
12+
//! This module coordinates hardware initialization, firmware
13+
//! verification, and update handling. It is the top-level
14+
//! execution point for the bootloader firmware.
915
1016
#![no_std]
1117
#![no_main]
1218

13-
use cortex_m_rt::entry;
19+
use core::panic::PanicInfo;
20+
use crate::init::init_hardware;
21+
use crate::updater::{FirmwareUpdater, UpdateMetadata, UpdateError};
22+
use crate::flash::{read_flash, write_flash, FlashError};
23+
use crate::verify::verify_crc;
24+
25+
#[panic_handler]
26+
fn panic(info: &PanicInfo) -> ! {
27+
// TODO: implement platform-specific panic behavior (LED blink, reset, etc.)
28+
loop {}
29+
}
1430

15-
mod init;
16-
mod flash;
17-
mod updater;
18-
mod verify;
31+
/// Bootloader main function.
32+
///
33+
/// # Safety
34+
/// Should be called once at reset, after MCU startup.
35+
#[no_mangle]
36+
pub extern "C" fn main() -> ! {
37+
// Initialize hardware.
38+
let hw = match init_hardware() {
39+
Ok(hw) => hw,
40+
Err(_e) => loop {}, // Initialization failed: halt or reset
41+
};
1942

20-
#[entry]
21-
fn main() -> ! {
22-
init::init_hardware();
43+
// Example: check if new firmware is present and valid.
44+
let update_meta = UpdateMetadata {
45+
target_addr: 0x0800_0000, // Adjust to actual firmware location
46+
image_size: 64 * 1024, // Example size
47+
expected_crc: 0xDEADBEEF, // Example CRC, replace with actual
48+
};
2349

24-
if verify::firmware_valid() {
25-
updater::check_for_update();
26-
jump_to_app();
27-
} else {
28-
loop {}
50+
// Attempt firmware update (stub for demonstration).
51+
let mut updater_flash = unsafe { &mut crate::flash::BOOT_INTERNAL_FLASH as &mut dyn crate::flash::Flash };
52+
match FirmwareUpdater::begin_update(updater_flash, update_meta) {
53+
Ok(mut updater) => {
54+
// In real implementation, fetch data chunks from communication interface
55+
// Here we just simulate writing empty data.
56+
let data = [0xFFu8; 1024];
57+
let mut offset = 0;
58+
while offset < update_meta.image_size {
59+
let chunk_size = core::cmp::min(data.len(), update_meta.image_size - offset);
60+
updater.write_chunk(offset, &data[..chunk_size]).ok();
61+
offset += chunk_size;
62+
}
63+
let _ = updater.finalize_update();
64+
}
65+
Err(_e) => {
66+
// No update, continue to existing firmware.
67+
}
2968
}
69+
70+
// After update or if no update, jump to application.
71+
jump_to_application();
3072
}
3173

32-
fn jump_to_app() -> ! {
33-
const APP_START_ADDRESS: u32 = 0x0800_4000;
34-
let app: extern "C" fn() = unsafe { core::mem::transmute(APP_START_ADDRESS) };
35-
app();
36-
}
74+
/// Placeholder function to jump to the main application.
75+
fn jump_to_application() -> ! {
76+
// TODO: implement vector table relocation and jump to reset handler
77+
loop {}
78+
}

0 commit comments

Comments
 (0)