Skip to content

Commit

Permalink
Merge pull request #42 from pmnxis/dev/i40_fingerprint_elf
Browse files Browse the repository at this point in the history
#40 Generate fingerprint on ELF section, for MPtool
  • Loading branch information
pmnxis committed Oct 31, 2023
2 parents bf08b38 + 0e18ef4 commit 5cad35d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ authors = ["Jinwoo Park <[email protected]>"]
license = "MIT OR Apache-2.0"
description = "application side of billmock hardware, powered by rust-embedded"

# feature name starting with "hw_" is reserved for mass production config generator
[features]
default = ["billmock_default"]
hw_bug_host_inhibit_floating = [] # #19 bug (https://github.com/pmnxis/billmock-app-rs/issues/19)
hotfix_hwbug_host_inhibit_floating = [] # #19 bug (https://github.com/pmnxis/billmock-app-rs/issues/19)
billmock_default = ["hw_mini_0v5"] # To use rust-analzyer utilizing noDefaultFeatures on vscode
eeprom = []
svc_button = [] # SVC button
hw_0v2 = ["hw_bug_host_inhibit_floating"]
hw_0v3 = ["hw_bug_host_inhibit_floating"]
hw_0v2 = ["hotfix_hwbug_host_inhibit_floating"]
hw_0v3 = ["hotfix_hwbug_host_inhibit_floating"]
hw_0v4 = ["eeprom"]
hw_mini_0v4 = ["eeprom"]
hw_mini_0v5 = ["eeprom", "svc_button"]
Expand All @@ -41,7 +42,7 @@ num_enum = { version = "0.7.0", default-features = false } # Application specifi
bit_field = "0.10"
nonmax = { version = "0.5.3", default-features = false, features = [] } # to use common NonMax
static_assertions = "1.1.0"
env_to_array = { version = "0.3.1", features = ["hex"] }
env_to_array = { git = "https://github.com/pmnxis/env-to-array.git", branch = "dynamic_array_patch", features = ["hex"] }
zeroable = "0.2.0"
const-zero = "0.1.1"

Expand All @@ -64,6 +65,8 @@ billmock-otp-dev-info = { git = "https://github.com/pmnxis/billmock-mptool.git"
[build-dependencies]
git2 = "0.18" # Git library for Rust
cargo_metadata = "0.18"
mp-fingerprint-type = { git = "https://github.com/pmnxis/billmock-mptool.git" }
hex = "0.4"

[profile.release]
# or "z"
Expand Down
56 changes: 41 additions & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,10 @@ use std::process::Command;

use cargo_metadata::{Error, MetadataCommand};
use git2::Repository;
use mp_fingerprint_type::{FirmwareFingerprint, MpFingerprint};

const IGNORE_PATH_DEP_INJ: &str = ".cargo/config.toml";

pub fn format_continuous(v: Vec<u8>) -> String {
let mut s = String::new();
for a in v {
s.push_str(format!("{:02X}", a).as_str());
}

s
}

fn main() -> Result<(), Error> {
println!("cargo:rustc-link-arg-bins=--nmagic");
println!("cargo:rustc-link-arg-bins=-Tlink.x");
Expand All @@ -35,14 +27,11 @@ fn main() -> Result<(), Error> {
println!("cargo:rustc-env=PROJECT_NAME={}", project_name);
println!(
"cargo:rustc-env=PROJECT_VERSION={}",
format_continuous(project_version.into())
hex::encode(project_version),
);
} else {
println!("cargo:rustc-env=PROJECT_NAME=unkown");
println!(
"cargo:rustc-env=PROJECT_VERSION={}",
format_continuous(b"?.?.?".into())
);
println!("cargo:rustc-env=PROJECT_VERSION={}", hex::encode(b"?.?.?"),);
}

// Get the Git commit hash
Expand Down Expand Up @@ -98,9 +87,46 @@ fn main() -> Result<(), Error> {

println!(
"cargo:rustc-env=GIT_COMMIT_SHORT_HASH={}",
format_continuous(format!("{}{}", commit_short_hash, short_dirty_str).into())
hex::encode(format!("{}{}", commit_short_hash, short_dirty_str))
);
println!("cargo:rustc-env=GIT_COMMIT_DATETIME={}", commit_datetime);

// Generate elf header fingerprint
let metadata = MetadataCommand::new().no_deps().exec()?;
let main_package = metadata
.packages
.first()
.expect("Cargo.toml doesn't have metadata");

let hw_feature: Vec<(String, String)> = std::env::vars()
.filter(|(key, value)| key.starts_with("CARGO_FEATURE_HW_") && value == "1")
.collect();

if hw_feature.is_empty() {
panic!("There's no specified hardware target");
} else if hw_feature.len() > 1 {
panic!("Cannot specify multiple hardware");
}

let feature_based_model_ver = hw_feature[0]
.0
.strip_prefix("CARGO_FEATURE_HW_")
.unwrap()
.replace("_", "-");

let fingerprint = MpFingerprint {
firmware_fingerprint: FirmwareFingerprint {
model_name: "BillMock-HW".to_owned(), // this is const value
model_ver: feature_based_model_ver,
firmware_ver: main_package.version.to_string(),
firmware_git_hash: format!("{}{}", commit_hash, dirty_str),
},
};

println!(
"cargo:rustc-env=MP_FINGERPRINT_TOML_HEX={}",
fingerprint.to_hex_string(),
);

Ok(())
}
24 changes: 12 additions & 12 deletions memory.x
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
/* STM32G030C8 */
MEMORY
{
/*
* [Mass Production Reminder]
* It is feasible to consider switching to the STM32G030C6Tx chip
* for the purpose of addressing supply availability and cost reduction.
* However, please be aware that the current debug build exceeds the 32KB
* limit for the flash section.
* As a result, this change would be applicable only when transitioning
* to the release build for mass production.
*
* If you wish to proceed with the change to STM32G030C6Tx,
* please modify the FLASH LENGTH to 32KB.
*/
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 8K
}

/*
* Mass-Production usage ELF section
* ref - https://github.com/pmnxis/billmock-app-rs/issues/40
* ref - https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
*/
SECTIONS {
.mp_fingerprint 0 (OVERLAY) :
{
KEEP(*(.mp_fingerprint))
}
}
6 changes: 3 additions & 3 deletions src/components/host_side_bill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::{AnyPin, Output};

use crate::semi_layer::buffered_opendrain::{buffered_opendrain_spawn, BufferedOpenDrain};
#[cfg(not(feature = "hw_bug_host_inhibit_floating"))]
#[cfg(not(feature = "hotfix_hwbug_host_inhibit_floating"))]
use crate::semi_layer::buffered_wait::buffered_wait_spawn;
use crate::semi_layer::buffered_wait::{BufferedWait, InputEventChannel, RawInputPortKind};
use crate::semi_layer::timing::SharedToggleTiming;
Expand All @@ -19,7 +19,7 @@ use crate::types::input_port::InputPortKind;
use crate::types::player::Player;

pub struct HostSideBill {
#[cfg_attr(feature = "hw_bug_host_inhibit_floating", allow(unused))]
#[cfg_attr(feature = "hotfix_hwbug_host_inhibit_floating", allow(unused))]
in_inhibit: BufferedWait,
pub out_busy: BufferedOpenDrain,
pub out_vend: BufferedOpenDrain,
Expand Down Expand Up @@ -69,7 +69,7 @@ impl HostSideBill {
unwrap!(spawner.spawn(buffered_opendrain_spawn(&self.out_jam)));
unwrap!(spawner.spawn(buffered_opendrain_spawn(&self.out_start)));

#[cfg(not(feature = "hw_bug_host_inhibit_floating"))]
#[cfg(not(feature = "hotfix_hwbug_host_inhibit_floating"))]
unwrap!(spawner.spawn(buffered_wait_spawn(&self.in_inhibit)));
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
mod application;
mod boards;
mod components;
mod mp_fingerprint;
mod semi_layer;
mod types;

Expand Down
23 changes: 23 additions & 0 deletions src/mp_fingerprint/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: © 2023 Jinwoo Park ([email protected])
*
* SPDX-License-Identifier: MIT OR Apache-2.0
*/

//! This code is not reflected on program binrary or actual memory.
//! .mp_fingerprint section is `NOLOAD` and `READONLY` section that
//! virtually existed in ELF header.
//! The contents of .mp_fingerprint would be used for billmock-mptool
//! to determine what kind of board, feature, version, git hash based
//! from ELF binary.

//! DO NOT USE `&str` or any other slice type
//! ```rs
//! #[allow(unused, dead_code)]
//! #[no_mangle]
//! #[link_section = ".mp_fingerprint"]
//! static TEST_FINGER: [u8; 14] = *b"SOME TOML HERE";

use env_to_array::patch_linker_section_from_hex_env;

patch_linker_section_from_hex_env!(".mp_fingerprint", "MP_INFO_TOML", "MP_FINGERPRINT_TOML_HEX");

0 comments on commit 5cad35d

Please sign in to comment.