ivmarkov / rust-esp32-std-demo

Rust on ESP32 STD demo app. A demo STD binary crate for the ESP32[XX] and ESP-IDF, which connects to WiFi, Ethernet, drives a small HTTP server and draws on a LED screen.
Apache License 2.0
784 stars 105 forks source link

Reading efuse values? #72

Closed S3j5b0 closed 2 years ago

S3j5b0 commented 2 years ago

Hi; i have a use case where I need some key material to be hardcoded onto the devices. For that purpose, using the efuse component seemed like the best option.

So I found the esptool, and espfuse tools, that I can use to read the data and write to it through the command line.

But I can't find too much on how to read the efuse from rust. I found [this thing(https://docs.rs/esp32/0.6.0/esp32/efuse/index.html), but the docs are exremely sparse, and there is no examples to the code.

The only thing I really can find about it, is this line, in your sample output on the readme:


Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (4761) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE

Is there any resources that you know of that one can learn to read these keys?

brianmay commented 2 years ago

In my code I have:

let mut mac: [u8; 6] = [0; 6];
unsafe {
    let ptr = &mut mac as *mut u8;
    esp_efuse_mac_get_default(ptr);
 }
let client_id = format!("robotica-remote-rust_{}", hex::encode(mac));

I don't think there is any safe API to access this yet.

S3j5b0 commented 2 years ago

ah thanks a lot! Which crate has this function, and do you know if it has one for reading the key blocks, adn not the mac value?

ivmarkov commented 2 years ago

esp-idf-sys which is raw unsafe bindings to ESP IDF. Therefore check the ESP IDF documentation / API for esp efuse: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/system/efuse.html

S3j5b0 commented 2 years ago

cool thx. But the efuse manager api seems to be c. Is there no way to do this in pure rust?

My thought was that I could use espefuse.py to place some keys on my devices and then read from them in rust, so they can be used in my application. Is this not possilbe?

ivmarkov commented 2 years ago

Please check esp-idf-sys and read on the Bindgen utility and what raw unsafe bindings really mean in Rust - this will help you get started. Essentially I'm telling you that most ESP IDF C APIs you can call from rust already as they are exposed as unsafe Rust APIs in esp-idf-sys.