Skip to content

Commit

Permalink
Merge pull request #8 from Purdue-eCTF-2024/post_boot
Browse files Browse the repository at this point in the history
Post boot
  • Loading branch information
pawnlord authored Mar 1, 2024
2 parents 3c4f56d + d8c3007 commit 76bfd5b
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 31 deletions.
1 change: 1 addition & 0 deletions application_processor/Cargo.lock

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

1 change: 1 addition & 0 deletions application_processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ serde = { version = "1.0", default-features = false }
[build-dependencies]
bcrypt = "0.15.0"
deployment = { path = "../deployment" }
cc = "1.0"

[profile.dev]
panic = "abort"
Expand Down
10 changes: 10 additions & 0 deletions application_processor/build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{convert::Infallible, path::PathBuf};
use std::env;
use std::fs::read_dir;

use bcrypt::bcrypt;
use deployment::{generate_random_bytes, parse_component_id, SecretDb};
Expand Down Expand Up @@ -153,6 +154,15 @@ fn main() {
// not sure how it manages to currently find it since we are not specifying where it currently is
println!("cargo:rustc-link-arg=-Tmemory.x");
}

cc::Build::new()
.target("thumbv7em-none-eabihf")
.compiler("arm-none-eabi-gcc")
.include("./post_boot")
.flag("-w")
.define("gcc", None)
.file("./post_boot/post_boot.c")
.compile("post_boot");
}

struct HashResult {
Expand Down
11 changes: 11 additions & 0 deletions application_processor/post_boot/post_boot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "post_boot.h"

int post_boot(void){
#ifdef POST_BOOT
POST_BOOT
return 1;
#else
return 0;
#endif

}
7 changes: 7 additions & 0 deletions application_processor/post_boot/post_boot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifdef POST_BOOT
#include <stdint.h>
#include <stdio.h>
#endif


int post_boot(void);
39 changes: 23 additions & 16 deletions application_processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use core::panic::PanicInfo;
use core::str::Utf8Error;

use bytemuck::checked::CheckedCastError;
use post_boot_functions::post_boot;
use post_boot_functions::wrapped_post_boot;
use thiserror_no_std::Error;
use serde::{Serialize, Deserialize};
use max78000_hal::prelude::*;
Expand Down Expand Up @@ -57,6 +59,8 @@ pub enum ApError {
InvalidUtf8(#[from] Utf8Error),
#[error("Error encrypting or decrypting a message")]
DecryptError(#[from] ChaChaError),
#[error("Invalid challenge response for component {0}")]
InvalidChallengeResponse(usize),
#[error("Error while serializing or deserializing message: {0}")]
PostcardError(#[from] postcard::Error),
#[error("Component reported en error occured")]
Expand Down Expand Up @@ -136,22 +140,25 @@ fn panic(info: &PanicInfo) -> ! {
// YOUR DESIGN MUST NOT CHANGE THIS FUNCTION
// Boot message is customized through the AP_BOOT_MSG macro
fn boot() {
// TODO: call the post boot c code

loop {
unsafe {
LED_On(LED1);
MXC_Delay(500000);
LED_On(LED2);
MXC_Delay(500000);
LED_On(LED3);
MXC_Delay(500000);
LED_Off(LED1);
MXC_Delay(500000);
LED_Off(LED2);
MXC_Delay(500000);
LED_Off(LED3);
MXC_Delay(500000);

if wrapped_post_boot() {
return;
} else {
loop {
unsafe {
LED_On(LED1);
MXC_Delay(500000);
LED_On(LED2);
MXC_Delay(500000);
LED_On(LED3);
MXC_Delay(500000);
LED_Off(LED1);
MXC_Delay(500000);
LED_Off(LED2);
MXC_Delay(500000);
LED_Off(LED3);
MXC_Delay(500000);
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions application_processor/src/post_boot_functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! These are all functions called by the c code for the post boot functionality
use core::ffi::{c_int, c_void};

// use core::ptr::slice_from_raw_parts;
use max78000_hal::i2c::I2cAddr;
use crate::flash_data::FlashData;
Expand Down Expand Up @@ -61,3 +63,15 @@ pub extern "C" fn get_provisioned_ids(buffer: *const u32) -> u32 {
// flash_data.component_cnt
todo!()
}



extern "C" {
pub fn post_boot() -> c_int;
}

pub fn wrapped_post_boot() -> bool {
unsafe {
post_boot() == 1
}
}
1 change: 1 addition & 0 deletions component/Cargo.lock

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

1 change: 1 addition & 0 deletions component/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ postcard = "1.0.8"
chacha20poly1305 = "0.10.1"
rand = "0.8.5"
deployment = { path = "../deployment" }
cc = "1.0"

[profile.dev]
panic = "abort"
Expand Down
9 changes: 9 additions & 0 deletions component/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ fn main() {
// not sure how it manages to currently find it since we are not specifying where it currently is
println!("cargo:rustc-link-arg=-Tmemory.x");
}

cc::Build::new()
.target("thumbv7em-none-eabihf")
.compiler("arm-none-eabi-gcc")
.include("./post_boot")
.flag("-w")
.define("gcc", None)
.file("./post_boot/post_boot.c")
.compile("post_boot");
}

fn generate_encrypted_rust_const(rust_code: &mut String, name: &str, data: &[u8], key: [u8; 32]) {
Expand Down
11 changes: 11 additions & 0 deletions component/post_boot/post_boot.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "post_boot.h"

int post_boot(void){
#ifdef POST_BOOT
POST_BOOT
return 1;
#else
return 0;
#endif

}
8 changes: 8 additions & 0 deletions component/post_boot/post_boot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifdef POST_BOOT
#include <stdint.h>
#include <stdio.h>
#include "led.h"
#endif


int post_boot(void);
44 changes: 29 additions & 15 deletions component/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![feature(effects)]
#![feature(const_trait_impl)]

use core::ffi::c_int;
use core::panic::PanicInfo;

use cortex_m::interrupt;
Expand Down Expand Up @@ -118,25 +119,28 @@ fn panic(info: &PanicInfo) -> ! {

fn boot() {
// TODO: post boot functionality
if wrapped_post_boot() {

unsafe {
LED_Off(LED1);
LED_Off(LED2);
LED_Off(LED3);

loop {
LED_On(LED1);
MXC_Delay(500000);
LED_On(LED2);
MXC_Delay(500000);
LED_On(LED3);
MXC_Delay(500000);
} else {
unsafe {
LED_Off(LED1);
MXC_Delay(500000);
LED_Off(LED2);
MXC_Delay(500000);
LED_Off(LED3);
MXC_Delay(500000);

loop {
LED_On(LED1);
MXC_Delay(500000);
LED_On(LED2);
MXC_Delay(500000);
LED_On(LED3);
MXC_Delay(500000);
LED_Off(LED1);
MXC_Delay(500000);
LED_Off(LED2);
MXC_Delay(500000);
LED_Off(LED3);
MXC_Delay(500000);
}
}
}
}
Expand Down Expand Up @@ -278,4 +282,14 @@ fn recv_struct<'de, T: Deserialize<'de>>(recv_buf: &'de mut [u8]) -> Result<T, C
Err(ComponentError::ProtocolError)
},
}
}

extern "C" {
pub fn post_boot() -> c_int;
}

pub fn wrapped_post_boot() -> bool {
unsafe {
post_boot() == 1
}
}

0 comments on commit 76bfd5b

Please sign in to comment.