CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
FactoryReset.cpp
Go to the documentation of this file.
2#include "cdc_log.h"
3#include "nvs.h"
4#include "nvs_flash.h"
5#include "esp_system.h"
6
7static const char* TAG = "FactoryReset";
8
9namespace cdc::core {
10
12 uint16_t progressEvery,
13 void (*onRmemProgress)(uint16_t, uint16_t)) {
14 TropicWipeResult result;
15
16 if (!se || !se->isSessionActive()) {
17 LOG_E(TAG, "Cannot wipe TROPIC01: SE session unavailable");
18 return result;
19 }
20 result.sessionReady = true;
21
22 for (uint8_t slot = 0; slot < hal::ISecureElement::ECC_SLOT_COUNT; ++slot) {
23 if (!se->eccSlotUsed(slot)) continue;
24 if (se->eccDelete(slot) == hal::SeResult::OK) {
25 result.eccDeleted++;
26 }
27 }
28
29 const uint16_t total = hal::ISecureElement::RMEM_SLOT_COUNT;
30 for (uint16_t slot = 0; slot < total; ++slot) {
31 if (progressEvery && onRmemProgress && (slot % progressEvery) == 0) {
32 onRmemProgress(slot, total);
33 }
34 if (!se->rmemSlotUsed(slot)) continue;
35 if (se->rmemErase(slot) == hal::SeResult::OK) {
36 result.rmemDeleted++;
37 }
38 }
39 if (progressEvery && onRmemProgress) {
40 onRmemProgress(total, total);
41 }
42
43 return result;
44}
45
46esp_err_t wipeNvs() {
47 esp_err_t err = nvs_flash_erase();
48 if (err != ESP_OK) {
49 LOG_E(TAG, "nvs_flash_erase failed: %s", esp_err_to_name(err));
50 return err;
51 }
52 err = nvs_flash_init();
53 if (err != ESP_OK) {
54 LOG_E(TAG, "nvs_flash_init failed: %s", esp_err_to_name(err));
55 return err;
56 }
57 return ESP_OK;
58}
59
61 nvs_handle_t handle = 0;
62 if (nvs_open(kBootProfileNs, NVS_READWRITE, &handle) == ESP_OK) {
63 nvs_erase_key(handle, kBootProfileKey);
64 nvs_commit(handle);
65 nvs_close(handle);
66 }
67 esp_restart();
68}
69
70} // namespace cdc::core
static const char * TAG
CDC Log: logging over TinyUSB CDC and UART.
#define LOG_E(tag, fmt,...)
Definition cdc_log.h:145
virtual bool rmemSlotUsed(uint16_t slot) const =0
virtual bool eccSlotUsed(uint8_t slot) const =0
static constexpr uint16_t RMEM_SLOT_COUNT
virtual SeResult eccDelete(uint8_t slot)=0
static constexpr uint8_t ECC_SLOT_COUNT
virtual SeResult rmemErase(uint16_t slot)=0
virtual bool isSessionActive() const =0
esp_err_t wipeNvs()
Erases the NVS partition and re-initializes it blank.
TropicWipeResult wipeTropic(hal::ISecureElement *se, uint16_t progressEvery=0, void(*onRmemProgress)(uint16_t current, uint16_t total)=nullptr)
Iterates every TROPIC01 ECC slot (0..ECC_SLOT_COUNT-1) and R-Memory slot (0..RMEM_SLOT_COUNT-1),...
void selfDestruct()
Triggers a full factory wipe on the next boot and restarts.
constexpr const char * kBootProfileNs
constexpr const char * kBootProfileKey