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"
|
|
|
|
|
|
|
|
|
|
static constexpr uint8_t hash_key[8] = {};
|
|
|
|
|
|
|
|
|
|
struct RebootingBootsel {
|
|
|
|
|
static constexpr int8_t ext_id = 1;
|
|
|
|
|
auto as_tuple() const { return std::tie(); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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); }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
stdio_init_all();
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
int c = getchar_timeout_us(100000);
|
|
|
|
|
if (c == 'p') {
|
|
|
|
|
printf("p");
|
|
|
|
|
} else if (c == 'b') {
|
2026-04-03 16:59:11 +09:00
|
|
|
msgpackpp::packer inner;
|
|
|
|
|
inner.pack(RebootingBootsel{});
|
|
|
|
|
auto msg = pack_envelope(inner.get_payload());
|
|
|
|
|
send_bytes(msg);
|
|
|
|
|
sleep_ms(100);
|
2026-04-02 22:15:10 +09:00
|
|
|
reset_usb_boot(0, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|