The pin crosses libavr's phase-6 close and the guideline sweep behind it. All 37 chips green, 43 tests each, the README size table matching every built image, and all 13602 flash images byte-identical to the previous pin. The bump broke one gate and exposed another as ornamental. `check_unit.cmake` matched the autobaud loader's measured unit by the symbol `unit_E`; libavr's rule-46 sweep renamed the member to `m_unit`, which the mangling spells `6m_unitE`. On the RAM-home chips the check went red and said so. On the GPIOR chips it went green - the branch that asserts the unit is *not* in RAM passes on an empty match, and an empty match is what a stale regex returns for every image. Both branches mean something again. `tools/check.sh` ran the 37-chip loop under `set -e`, so the first red chip ended the gate and the 36 behind it were never built - a stale size canary on attiny13 would have been an alibi for every loader after it. It accumulates now and fails at the end naming every red preset, which is the shape libavr's own check.sh carries and the reason it carries it. The port's own sweep, verified by byte identity: the four TSB tiers' 16-byte info block is `std::to_array` rather than an extent written beside the sixteen elements the compiler can count, the three-member serial and loader configs break one member per line, the turn-around loops are braced, and the test fixture's config pair is a deduced `std::array` (rules 36, 40, 34). Two comments stop narrating how the code came to be and one stops citing a repro at a path it left two phases ago (rules 12, 13). pureboot's identity stamp stays the raw array rule 36 bans, and now says why: its reads must fold to immediates because the bytes are in program memory and a formed address is dereferenced as data space. As a `std::array` the read loop stopped unrolling and emitted exactly that - measured at +8 B and a wrong answer on the wire. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
129 lines
4.2 KiB
C++
129 lines
4.2 KiB
C++
// simavr "device" for the TSB bootloader: load the boot-linked ELF into the
|
|
// ATmega328P boot section, enter it (BOOTRST is not modelled, so we set PC to
|
|
// the boot base, exactly as simavr's own board_simduino does), and expose
|
|
// UART0 as a pty. A host client (Python pyserial, or the real tsbloader) then
|
|
// speaks the TSB protocol over that pty and actually flashes the device.
|
|
//
|
|
// SPM genuinely writes avr->flash on the mega cores, so on exit (or SIGTERM)
|
|
// we dump the flash image to a file for a ground-truth cross-check against
|
|
// what the client read back through the bootloader.
|
|
#include <array>
|
|
#include <csignal>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <print>
|
|
|
|
#include <unistd.h>
|
|
|
|
// The parts headers (uart_pty.h) carry no C++ linkage guards of their own,
|
|
// unlike simavr's core headers - the block covers both harmlessly.
|
|
extern "C" {
|
|
#include "avr_uart.h"
|
|
#include "sim_avr.h"
|
|
#include "sim_elf.h"
|
|
#include "uart_pty.h"
|
|
}
|
|
|
|
namespace {
|
|
|
|
avr_t *avr;
|
|
uart_pty_t uart_pty;
|
|
const char *dump_path;
|
|
|
|
[[noreturn]] void finish(int)
|
|
{
|
|
if (dump_path) {
|
|
std::FILE *f = std::fopen(dump_path, "wb");
|
|
if (f) {
|
|
std::fwrite(avr->flash, 1, avr->flashend + 1, f);
|
|
std::fclose(f);
|
|
}
|
|
}
|
|
uart_pty_stop(&uart_pty);
|
|
_exit(0);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if (argc < 3) {
|
|
std::println(stderr, "usage: {} <tsb.elf> <boot_base_hex> [flash_dump.bin]", argv[0]);
|
|
return 2;
|
|
}
|
|
auto boot_base = static_cast<std::uint32_t>(std::strtoul(argv[2], nullptr, 0));
|
|
dump_path = argc >= 4 ? argv[3] : nullptr;
|
|
|
|
avr = avr_make_mcu_by_name("atmega328p");
|
|
if (!avr) {
|
|
std::println(stderr, "device: no ATmega328P core");
|
|
return 1;
|
|
}
|
|
avr_init(avr);
|
|
avr->frequency = 16000000;
|
|
// Real flash powers up erased (0xff); the app region must look erased
|
|
// before the bootloader programs it.
|
|
std::memset(avr->flash, 0xff, avr->flashend + 1);
|
|
|
|
// simavr's ELF loader flattens the flash base to 0 (it expects an app at
|
|
// 0x0), but it hands back the boot code in fw.flash; place it at the boot
|
|
// section base ourselves and enter there (BOOTRST is not modelled).
|
|
elf_firmware_t fw{};
|
|
if (elf_read_firmware(argv[1], &fw) != 0) {
|
|
std::println(stderr, "device: cannot read {}", argv[1]);
|
|
return 1;
|
|
}
|
|
// An image that runs past flash end cannot execute on hardware, and a
|
|
// naive copy of it would smash the heap beyond avr->flash - after which
|
|
// the simulation misbehaves in ways that point everywhere but here.
|
|
// Refuse it loudly instead.
|
|
if (boot_base + fw.flashsize > avr->flashend + 1) {
|
|
std::println(stderr, "device: {} B at {:#x} runs past flash end {:#x} - image does not fit its slot",
|
|
fw.flashsize, boot_base, avr->flashend);
|
|
return 1;
|
|
}
|
|
std::memcpy(avr->flash + boot_base, fw.flash, fw.flashsize);
|
|
avr->pc = boot_base;
|
|
avr->codeend = avr->flashend;
|
|
|
|
// Optional: seed the config page (one page below the boot section) with a
|
|
// hex byte string, so the password gate and emergency erase can be tested.
|
|
// Layout: [appjump lo][appjump hi][timeout][password...][0xff].
|
|
const char *cfg = std::getenv("TSB_CONFIG");
|
|
if (cfg) {
|
|
std::uint32_t app_end = boot_base - 128; // config page sits directly below the boot code
|
|
for (int i = 0; cfg[i] && cfg[i + 1]; i += 2) {
|
|
const std::array pair{cfg[i], cfg[i + 1], '\0'};
|
|
avr->flash[app_end + i / 2] = static_cast<std::uint8_t>(std::strtoul(pair.data(), nullptr, 16));
|
|
}
|
|
}
|
|
|
|
// POLL_SLEEP makes simavr usleep(1) on every status-register read while the
|
|
// UART is idle - a host-CPU-saving hack that models no hardware and paces a
|
|
// tight-polling loader (one that releases TX between bytes, as one-wire does)
|
|
// in real time, distorting protocol timing. Clear it so the loader runs at
|
|
// true cycle speed.
|
|
std::uint32_t uflags = 0;
|
|
avr_ioctl(avr, AVR_IOCTL_UART_GET_FLAGS('0'), &uflags);
|
|
uflags &= ~AVR_UART_FLAG_POLL_SLEEP;
|
|
avr_ioctl(avr, AVR_IOCTL_UART_SET_FLAGS('0'), &uflags);
|
|
|
|
uart_pty_init(avr, &uart_pty);
|
|
uart_pty_connect(&uart_pty, '0');
|
|
std::println("TSB_PTY {}", uart_pty.pty.slavename);
|
|
std::fflush(stdout);
|
|
|
|
std::signal(SIGTERM, finish);
|
|
std::signal(SIGINT, finish);
|
|
|
|
for (;;) {
|
|
int state = avr_run(avr);
|
|
if (state == cpu_Done || state == cpu_Crashed) {
|
|
break;
|
|
}
|
|
}
|
|
finish(0);
|
|
}
|