2026-04-02 22:15:10 +09:00
|
|
|
#include <stdio.h>
|
2026-04-03 16:59:11 +09:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <vector>
|
2026-04-02 22:15:10 +09:00
|
|
|
#include "pico/stdlib.h"
|
|
|
|
|
#include "pico/bootrom.h"
|
2026-04-03 16:59:11 +09:00
|
|
|
#include "msgpackpp.h"
|
|
|
|
|
#include "halfsiphash.h"
|
2026-04-03 17:32:14 +09:00
|
|
|
#include "static_vector.h"
|
2026-04-03 16:59:11 +09:00
|
|
|
|
|
|
|
|
static constexpr uint8_t hash_key[8] = {};
|
|
|
|
|
|
2026-04-03 17:32:14 +09:00
|
|
|
struct ResponseBOOTSEL {
|
2026-04-03 16:59:11 +09:00
|
|
|
static constexpr int8_t ext_id = 1;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
2026-04-03 17:32:14 +09:00
|
|
|
auto as_tuple() { return std::tie(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct RequestBOOTSEL {
|
|
|
|
|
static constexpr int8_t ext_id = 2;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
|
|
|
|
auto as_tuple() { return std::tie(); }
|
2026-04-03 16:59:11 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct Envelope {
|
|
|
|
|
static constexpr int8_t ext_id = 0;
|
|
|
|
|
uint32_t checksum;
|
|
|
|
|
std::vector<uint8_t> payload;
|
|
|
|
|
auto as_tuple() const { return std::tie(checksum, payload); }
|
2026-04-03 17:32:14 +09:00
|
|
|
auto as_tuple() { return std::tie(checksum, payload); }
|
2026-04-03 16:59:11 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static std::vector<uint8_t> pack_envelope(const std::vector<uint8_t> &payload) {
|
|
|
|
|
uint32_t checksum = halfsiphash::hash32(payload.data(), payload.size(), hash_key);
|
|
|
|
|
msgpackpp::packer p;
|
|
|
|
|
p.pack(Envelope{checksum, payload});
|
|
|
|
|
return p.get_payload();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void send_bytes(const std::vector<uint8_t> &data) {
|
|
|
|
|
for (auto b : data) {
|
|
|
|
|
putchar(b);
|
|
|
|
|
}
|
|
|
|
|
stdio_flush();
|
|
|
|
|
}
|
2026-04-02 22:15:10 +09:00
|
|
|
|
2026-04-03 17:32:14 +09:00
|
|
|
template <typename T>
|
|
|
|
|
static void send_message(const T &msg) {
|
|
|
|
|
msgpackpp::packer inner;
|
|
|
|
|
inner.pack(msg);
|
|
|
|
|
auto envelope = pack_envelope(inner.get_payload());
|
|
|
|
|
send_bytes(envelope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int8_t try_decode(const static_vector<uint8_t, 256> &buf) {
|
|
|
|
|
msgpackpp::parser p(buf.data(), static_cast<int>(buf.size()));
|
|
|
|
|
|
|
|
|
|
Envelope env;
|
|
|
|
|
if (!msgpackpp::unpack(p, env)) return -1;
|
|
|
|
|
|
|
|
|
|
uint32_t expected = halfsiphash::hash32(env.payload.data(), env.payload.size(), hash_key);
|
|
|
|
|
if (env.checksum != expected) return -1;
|
|
|
|
|
|
|
|
|
|
msgpackpp::parser inner(env.payload.data(), static_cast<int>(env.payload.size()));
|
|
|
|
|
if (!inner.is_ext()) return -1;
|
|
|
|
|
auto ext = inner.get_ext();
|
|
|
|
|
if (!ext) return -1;
|
|
|
|
|
|
|
|
|
|
return std::get<0>(*ext);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 22:15:10 +09:00
|
|
|
int main() {
|
|
|
|
|
stdio_init_all();
|
|
|
|
|
|
2026-04-03 17:32:14 +09:00
|
|
|
static static_vector<uint8_t, 256> rx_buf;
|
|
|
|
|
|
2026-04-02 22:15:10 +09:00
|
|
|
while (true) {
|
|
|
|
|
int c = getchar_timeout_us(100000);
|
2026-04-03 17:32:14 +09:00
|
|
|
if (c == PICO_ERROR_TIMEOUT) continue;
|
|
|
|
|
|
|
|
|
|
rx_buf.push_back(static_cast<uint8_t>(c));
|
|
|
|
|
|
|
|
|
|
int8_t msg_type = try_decode(rx_buf);
|
|
|
|
|
if (msg_type < 0) {
|
|
|
|
|
if (rx_buf.full()) rx_buf.clear();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rx_buf.clear();
|
|
|
|
|
|
|
|
|
|
switch (msg_type) {
|
|
|
|
|
case RequestBOOTSEL::ext_id:
|
|
|
|
|
send_message(ResponseBOOTSEL{});
|
2026-04-03 16:59:11 +09:00
|
|
|
sleep_ms(100);
|
2026-04-02 22:15:10 +09:00
|
|
|
reset_usb_boot(0, 0);
|
2026-04-03 17:32:14 +09:00
|
|
|
break;
|
2026-04-02 22:15:10 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|