Files
frame/poc/apps/m1_hardware/main/sd_service.cc
T
Lainandlain ef5d27b6c5 feat(m1): storage info command and tf card identity record (#16)
## What

- M1 firmware: new `storage info` console command printing the mounted
TF card identity from
`sdmmc_card_t` (CID: manufacturer/OEM/product name/revision/serial/date;
CSD: capacity in
MiB, sectors, sector size). `storage: no card` when not mounted. CID/CSD
are copied out
under a new mutex that also guards mount/unmount, so the console never
dereferences a
  half-initialized or freed card. No other behavior changed.
- Lab config `_doc/lab/board-c1810c76790e.json`: `tf.tf_card` upgraded
from
`pending-visual-read` to the values read on the real board (`SD16G`,
14910 MiB,
  serial `0x00000024`, mfg 2026-05), citing the in-repo transcript.
- ADR-0003: TF record acceptance checkbox ticked with citation; blockers
list updated.

## Evidence

- `poc/evidence/tf-info-20260907/tf-cid-transcript-20260907.log`:
RTS-reset boot, two
identical `storage info` reads (L188-189, L192-193), `status --json`
sanity (L197,
  `tf":"READY`), serial non-zero.
- Board restored to PoC-A firmware after the read (`poca status`
verified).

Co-authored-by: ania-lain <[email protected]>
2026-09-07 21:03:08 +08:00

268 lines
9.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "sd_service.hh"
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <sys/stat.h>
#include <unistd.h>
#include "board_config.hh"
#include "driver/sdmmc_host.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "esp_vfs_fat.h"
#include "psa/crypto.h"
#include "status_log.hh"
namespace frame::m1 {
namespace {
constexpr char kTag[] = "frame-sd";
constexpr char kHealthDirectory[] = "/frame/.health";
constexpr char kHealthPath[] = "/frame/.health/m1.tmp";
constexpr char kHealthPayload[] = "frame-m1-sd-health-v1\n";
constexpr char kLogDirectory[] = "/frame/logs";
constexpr char kLogPath[] = "/frame/logs/m1-current.jsonl";
constexpr int64_t kLogIntervalUs = 10LL * 1000LL * 1000LL;
} // namespace
sd_service::~sd_service() { unmount(); }
esp_err_t sd_service::start() {
card_lock_ = xSemaphoreCreateMutex();
if (card_lock_ == nullptr) {
return ESP_ERR_NO_MEM;
}
const BaseType_t created =
xTaskCreatePinnedToCore(&sd_service::task_entry, "frame-sd", 6144, this, 7, &task_, 1);
return created == pdPASS ? ESP_OK : ESP_ERR_NO_MEM;
}
void sd_service::request_probe() {
if (task_ != nullptr) {
xTaskNotifyGive(task_);
}
}
void sd_service::task_entry(void* context) { static_cast<sd_service*>(context)->run(); }
esp_err_t sd_service::mount_and_check() {
status_.update([](status_snapshot& value) { value.storage = sd_state::health_checking; });
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
host.max_freq_khz = SDMMC_FREQ_DEFAULT;
sdmmc_slot_config_t slot = SDMMC_SLOT_CONFIG_DEFAULT();
slot.width = 1;
slot.clk = board::kSdClock;
slot.cmd = board::kSdCommand;
slot.d0 = board::kSdData0;
slot.d1 = GPIO_NUM_NC;
slot.d2 = GPIO_NUM_NC;
slot.d3 = GPIO_NUM_NC;
slot.cd = SDMMC_SLOT_NO_CD;
slot.wp = SDMMC_SLOT_NO_WP;
slot.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
esp_vfs_fat_sdmmc_mount_config_t mount{};
mount.format_if_mount_failed = false;
mount.max_files = 8;
mount.allocation_unit_size = 16 * 1024;
// Hold card_lock_ across the mount call: esp_vfs_fat_sdmmc_mount writes card_ via the
// out-param while the card struct is still initializing, and print_card_info must not
// observe that half-built state.
xSemaphoreTake(card_lock_, portMAX_DELAY);
esp_err_t result = esp_vfs_fat_sdmmc_mount(board::kSdMount, &host, &slot, &mount, &card_);
if (result != ESP_OK) {
card_ = nullptr;
}
xSemaphoreGive(card_lock_);
if (result != ESP_OK) {
const bool card_absent = result == ESP_ERR_NOT_FOUND || result == ESP_ERR_TIMEOUT;
status_.update([&](status_snapshot& value) {
value.storage = card_absent ? sd_state::absent : sd_state::failed;
value.storage_error = result;
});
return result;
}
result = health_check();
status_.update([&](status_snapshot& value) {
value.storage = result == ESP_OK ? sd_state::ready : sd_state::failed;
value.storage_error = result;
});
if (result != ESP_OK) {
unmount();
}
return result;
}
esp_err_t sd_service::health_check() {
if (mkdir(kHealthDirectory, 0755) != 0 && errno != EEXIST) {
return ESP_FAIL;
}
if (mkdir(kLogDirectory, 0755) != 0 && errno != EEXIST) {
return ESP_FAIL;
}
FILE* output = std::fopen(kHealthPath, "wb");
if (output == nullptr) {
return ESP_FAIL;
}
const std::size_t written = std::fwrite(kHealthPayload, 1, sizeof(kHealthPayload) - 1U, output);
const int flush_result = std::fflush(output);
const int sync_result = fsync(fileno(output));
const int close_result = std::fclose(output);
if (written != sizeof(kHealthPayload) - 1U || flush_result != 0 || sync_result != 0 ||
close_result != 0) {
unlink(kHealthPath);
return ESP_FAIL;
}
char readback[sizeof(kHealthPayload) - 1U]{};
FILE* input = std::fopen(kHealthPath, "rb");
if (input == nullptr) {
return ESP_FAIL;
}
const std::size_t read = std::fread(readback, 1, sizeof(readback), input);
const int read_close_result = std::fclose(input);
uint8_t expected_hash[32]{};
uint8_t actual_hash[32]{};
std::size_t expected_size = 0;
std::size_t actual_size = 0;
const psa_status_t expected_result =
psa_hash_compute(PSA_ALG_SHA_256, reinterpret_cast<const uint8_t*>(kHealthPayload),
sizeof(readback), expected_hash, sizeof(expected_hash), &expected_size);
const psa_status_t actual_result =
psa_hash_compute(PSA_ALG_SHA_256, reinterpret_cast<const uint8_t*>(readback),
sizeof(readback), actual_hash, sizeof(actual_hash), &actual_size);
unlink(kHealthPath);
return read == sizeof(readback) && read_close_result == 0 && expected_result == PSA_SUCCESS &&
actual_result == PSA_SUCCESS && expected_size == sizeof(expected_hash) &&
actual_size == sizeof(actual_hash) &&
std::memcmp(expected_hash, actual_hash, sizeof(expected_hash)) == 0
? ESP_OK
: ESP_FAIL;
}
void sd_service::unmount() {
if (card_lock_ != nullptr) {
xSemaphoreTake(card_lock_, portMAX_DELAY);
}
if (card_ != nullptr) {
esp_vfs_fat_sdcard_unmount(board::kSdMount, card_);
card_ = nullptr;
}
if (card_lock_ != nullptr) {
xSemaphoreGive(card_lock_);
}
}
void sd_service::print_card_info() const {
if (card_lock_ == nullptr) {
std::printf("storage: no card\n");
return;
}
sdmmc_cid_t cid{};
sdmmc_csd_t csd{};
bool mounted = false;
xSemaphoreTake(card_lock_, portMAX_DELAY);
if (card_ != nullptr) {
cid = card_->cid;
csd = card_->csd;
mounted = true;
}
xSemaphoreGive(card_lock_);
if (!mounted) {
std::printf("storage: no card\n");
return;
}
char name[sizeof(cid.name) + 1]{};
std::memcpy(name, cid.name, sizeof(cid.name));
const unsigned oem_high = static_cast<unsigned>(cid.oem_id >> 8) & 0xFFU;
const unsigned oem_low = static_cast<unsigned>(cid.oem_id) & 0xFFU;
const unsigned serial = static_cast<unsigned>(static_cast<uint32_t>(cid.serial));
const unsigned long long sectors = static_cast<unsigned long long>(csd.capacity);
const unsigned long long capacity_mib =
sectors * static_cast<unsigned long long>(csd.sector_size) / (1024ULL * 1024ULL);
std::printf(
"cid: mfg=0x%02X oem=%c%c name=\"%s\" rev=%u.%u serial=0x%08X date=%u/%u\n",
static_cast<unsigned>(cid.mfg_id) & 0xFFU, oem_high != 0U ? oem_high : '?',
oem_low != 0U ? oem_low : '?', name, (static_cast<unsigned>(cid.revision) >> 4U) & 0xFU,
static_cast<unsigned>(cid.revision) & 0xFU, serial, static_cast<unsigned>(cid.date) & 0xFU,
2000U + ((static_cast<unsigned>(cid.date) >> 4U) & 0xFFU));
std::printf("csd: capacity=%llu MiB (%llu sectors × %u B)\n", capacity_mib, sectors,
static_cast<unsigned>(csd.sector_size));
}
esp_err_t sd_service::append_status_log() {
// Must exceed the ~595-byte payload (measured with boot counters; re-measure before adding
// JSONL fields): format_status_log returns 0 on truncation (= ESP_FAIL).
char line[640];
const status_snapshot snapshot = status_.snapshot();
const std::size_t length =
format_status_log(snapshot, ++log_sequence_, esp_timer_get_time(), line, sizeof(line));
if (length == 0) {
return ESP_FAIL;
}
const std::size_t mirrored = std::fwrite(line, 1, length, stdout);
std::fflush(stdout);
if (mirrored != length) {
return ESP_FAIL;
}
FILE* output = std::fopen(kLogPath, "a");
if (output == nullptr) {
return ESP_FAIL;
}
const std::size_t written = std::fwrite(line, 1, length, output);
const int flush_result = std::fflush(output);
const int sync_result = fsync(fileno(output));
const int close_result = std::fclose(output);
if (written != length || flush_result != 0 || sync_result != 0 || close_result != 0) {
return ESP_FAIL;
}
return ESP_OK;
}
void sd_service::run() {
int64_t next_log_at = 0;
while (true) {
const int64_t now = esp_timer_get_time();
if (card_ == nullptr) {
const esp_err_t result = mount_and_check();
if (result != ESP_OK) {
ESP_LOGW(kTag, "TF probe failed: %s", esp_err_to_name(result));
} else {
next_log_at = now;
}
} else {
const esp_err_t result = sdmmc_get_status(card_);
if (result != ESP_OK) {
ESP_LOGW(kTag, "TF removed or unhealthy: %s", esp_err_to_name(result));
status_.update([&](status_snapshot& value) {
value.storage = sd_state::recovering;
value.storage_error = result;
});
unmount();
status_.update([](status_snapshot& value) {
value.storage = sd_state::absent;
value.tf_logging_ok = false;
});
} else if (now >= next_log_at) {
const esp_err_t log_result = append_status_log();
status_.update([&](status_snapshot& value) {
value.tf_logging_ok = log_result == ESP_OK;
value.logging_error = log_result;
});
next_log_at = now + kLogIntervalUs;
}
}
ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(2000));
}
}
} // namespace frame::m1