SHA-256 hash verification of flash slots, pico_hash_binary, flash:: namespace, ring_buffer iterator, non-destructive log

This commit is contained in:
Ian Gulliver
2026-04-12 23:47:00 +09:00
parent cc37b711a4
commit 23587c41e2
7 changed files with 210 additions and 64 deletions

View File

@@ -37,15 +37,3 @@ inline void dlog_if_slow(std::string_view label, uint32_t threshold_us, std::fun
dlogf("%.*s %luus", static_cast<int>(label.size()), label.data(), static_cast<unsigned long>(elapsed));
}
inline std::vector<log_entry> dlog_drain() {
std::vector<log_entry> result;
uint16_t n = g_debug_log.used();
result.reserve(n);
for (uint16_t i = 0; i < n; i++) {
log_entry e;
g_debug_log.peek(std::span{&e, 1});
result.push_back(std::move(e));
g_debug_log.consume(1);
}
return result;
}

20
firmware/include/flash.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <tuple>
namespace flash {
constexpr uint32_t FLASH_BASE = 0x10000000;
constexpr uint32_t FLASH_SIZE = 2 * 1024 * 1024;
struct slot {
bool valid;
uint32_t version;
bool hash_ok;
auto as_tuple() const { return std::tie(valid, version, hash_ok); }
auto as_tuple() { return std::tie(valid, version, hash_ok); }
};
slot scan(uint32_t flash_offset);
}

View File

@@ -53,4 +53,15 @@ struct ring_buffer {
uint16_t len = pending < contig ? pending : contig;
return {data.data() + offset, len};
}
struct iterator {
const ring_buffer* rb;
uint16_t index;
const T& operator*() const { return rb->data[(rb->head + index) % N]; }
iterator& operator++() { index++; return *this; }
bool operator!=(const iterator& o) const { return index != o.index; }
};
iterator begin() const { return {this, 0}; }
iterator end() const { return {this, used()}; }
};

View File

@@ -8,6 +8,7 @@
#include "msgpack.h"
#include "halfsiphash.h"
#include "static_vector.h"
#include "flash.h"
struct Envelope {
static constexpr int8_t ext_id = 0;
@@ -128,19 +129,11 @@ struct RequestFlashStatus {
auto as_tuple() { return std::tie(); }
};
struct SlotInfo {
bool valid;
uint32_t version;
bool hash_ok;
auto as_tuple() const { return std::tie(valid, version, hash_ok); }
auto as_tuple() { return std::tie(valid, version, hash_ok); }
};
struct ResponseFlashStatus {
static constexpr int8_t ext_id = 15;
int8_t boot_partition;
SlotInfo slot_a;
SlotInfo slot_b;
flash::slot slot_a;
flash::slot slot_b;
auto as_tuple() const { return std::tie(boot_partition, slot_a, slot_b); }
auto as_tuple() { return std::tie(boot_partition, slot_a, slot_b); }
};