Take a dropped payload, and stop drowning the console

Double-clicking the emulator printed a usage line to a console nobody sees
and exited. It now opens a window that accepts a dropped .bin, with O for the
system file dialog - GetOpenFileName on Windows, zenity or kdialog on Linux,
since there is no such thing there without dragging in a toolkit. The window
draws a dashed frame and an upload glyph from rectangles and one filled
triangle; no text, because a font would have to be vendored for a screen that
lives a few seconds and the instruction is on the title bar already. It is
created and destroyed before the emulator's own display comes up, so it
cannot interfere with the framebuffer window. A headless or toolkit-less
machine falls back to the old usage message, and dragging a payload onto the
executable still skips all of it - that arrives as argv[1].

Dropping a payload was then unusably slow, and the reason had nothing to do
with the picker. A full hwtest sweep printed 52,452 lines, of which about
51,900 were per-register-access traces: 29,673 [gpio], 16,707 [mmio], 5,430
[sdmmc]. Redirected to a file that is merely wasteful, which is why it went
unnoticed - every scripted run here redirects. On a Windows console it is
not: each line goes through conhost one write at a time. Those traces now
need --trace or RCM_EMU_TRACE=1, which drops default output to 578 lines -
the payload's own UART plus the handful of [emu], [display], [pcie] and
[audio] lines worth seeing. The flag brings all 48,354 back when a
register-level question needs answering.

Also adds RCM_EMU_PICK, which runs the picker's window lifecycle and returns
a path as if it had been dropped, so that path can be tested by a script
instead of only by a human dragging a file.
This commit is contained in:
Baptiste MOINE committed 2026-08-20 10:29:28 +02:00
1 parent 390d64abe6
commit a71ccdf682
9 files changed
+281 -30

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ WIN_CXX = x86_64-w64-mingw32-g++
WIN_OUT = rcm_emu.exe WIN_OUT = rcm_emu.exe
WIN_OBJS = $(SRCS:.cpp=.win.o) WIN_OBJS = $(SRCS:.cpp=.win.o)
WIN_CXXFLAGS = -Wall -g -O2 -std=c++17 -MMD -MP -I. -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I/usr/x86_64-w64-mingw32/include/SDL2 -DSDL_MAIN_HANDLED WIN_CXXFLAGS = -Wall -g -O2 -std=c++17 -MMD -MP -I. -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I/usr/x86_64-w64-mingw32/include/SDL2 -DSDL_MAIN_HANDLED
WIN_LIBS = -lmingw32 -lSDL2main -lSDL2 -lunicorn -lpthread -lwinmm -limm32 -lole32 -loleaut32 -lversion -lsetupapi -lcfgmgr32 -lgdi32 -lrpcrt4 -lws2_32 -luuid -lshell32 -ladvapi32 -luser32 WIN_LIBS = -lmingw32 -lSDL2main -lSDL2 -lunicorn -lpthread -lwinmm -limm32 -lole32 -loleaut32 -lversion -lsetupapi -lcfgmgr32 -lcomdlg32 -lgdi32 -lrpcrt4 -lws2_32 -luuid -lshell32 -ladvapi32 -luser32
WIN_LDFLAGS = -static -static-libgcc -static-libstdc++ WIN_LDFLAGS = -static -static-libgcc -static-libstdc++
.PHONY: all clean windows .PHONY: all clean windows
+7
View File
@@ -119,6 +119,13 @@ green:
![PIN recovery demo](img/pin_recovery_demo.png) ![PIN recovery demo](img/pin_recovery_demo.png)
### Running without arguments
Starting the emulator with no payload opens a small window that takes a
dropped `.bin`, or press **O** for the system file dialog (zenity or kdialog
on Linux). Dragging a payload onto the executable itself still works and
skips the picker - it arrives as `argv[1]`.
### Build (Windows) ### Build (Windows)
Cross-compiled from Linux in a container, because neither dependency has a Cross-compiled from Linux in a container, because neither dependency has a
+25 -1
View File
@@ -22,6 +22,9 @@
#include <unicorn/unicorn.h> #include <unicorn/unicorn.h>
#include "emu_state.h" #include "emu_state.h"
#include <string>
#include "payload_picker.h"
#include "trace.h"
#include "t210/memory_map.h" #include "t210/memory_map.h"
#include "t210/mmio.h" #include "t210/mmio.h"
#include "display/sdl_display.h" #include "display/sdl_display.h"
@@ -273,8 +276,25 @@ static uc_engine *setup_emulation(EmuState *state, uint8_t *payload, size_t payl
// ==================== Main ==================== // ==================== Main ====================
// Defined here, declared in trace.h. Off by default - see the note there.
bool emu_trace_enabled = false;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (getenv("RCM_EMU_TRACE"))
emu_trace_enabled = true;
for (int i = 1; i < argc; i++)
if (strcmp(argv[i], "--trace") == 0)
emu_trace_enabled = true;
// Started with no payload - usually a double-click, where a usage line
// on stderr goes to a console nobody is looking at. Offer the picker
// instead: drop a file on its window, or press O for the system dialog.
// Dragging a .bin onto the executable still bypasses all of this, since
// it arrives as argv[1].
std::string picked;
if (argc < 2) { if (argc < 2) {
picked = payload_picker_run();
if (picked.empty()) {
fprintf(stderr, "Usage: %s <payload.bin> [--sd <sd.img>] [--boot0 <boot0.bin>] [--rawnand <rawnand_prefix>] [--prod-keys <prod.keys>] [--oem erista|mariko] [--bt-radio healthy|faulty|absent] [--wifi-radio healthy|faulty|absent]\n", argv[0]); fprintf(stderr, "Usage: %s <payload.bin> [--sd <sd.img>] [--boot0 <boot0.bin>] [--rawnand <rawnand_prefix>] [--prod-keys <prod.keys>] [--oem erista|mariko] [--bt-radio healthy|faulty|absent] [--wifi-radio healthy|faulty|absent]\n", argv[0]);
fprintf(stderr, "\nControls:\n"); fprintf(stderr, "\nControls:\n");
fprintf(stderr, " Arrow Up/Down = VOL+/VOL- buttons\n"); fprintf(stderr, " Arrow Up/Down = VOL+/VOL- buttons\n");
@@ -283,6 +303,10 @@ int main(int argc, char *argv[]) {
fprintf(stderr, " F8 = Save raw guest FB to fb_dump_NNNN.raw\n"); fprintf(stderr, " F8 = Save raw guest FB to fb_dump_NNNN.raw\n");
return 1; return 1;
} }
}
// Everything below indexes argv, so present the picked file as argv[1].
const char *payload_arg = picked.empty() ? argv[1] : picked.c_str();
printf("=== RCM Payload Emulator ===\n"); printf("=== RCM Payload Emulator ===\n");
printf("Using Unicorn Engine for ARM32 emulation\n\n"); printf("Using Unicorn Engine for ARM32 emulation\n\n");
@@ -298,7 +322,7 @@ int main(int argc, char *argv[]) {
// Load payload binary // Load payload binary
size_t payload_size = 0; size_t payload_size = 0;
uint8_t *payload = load_payload(argv[1], &payload_size); uint8_t *payload = load_payload(payload_arg, &payload_size);
if (!payload) return 1; if (!payload) return 1;
// Parse additional arguments for storage. Configuration state // Parse additional arguments for storage. Configuration state
+192
View File
@@ -0,0 +1,192 @@
// Choosing a payload when the emulator is started without one.
//
// Double-clicking the emulator used to print a usage line to a console
// nobody sees and exit. Instead it now opens a window that takes a dropped
// file, with a key to bring up the system file dialog for people who would
// rather browse. Dragging a .bin onto the executable itself still works and
// still skips all of this - it arrives as argv[1].
//
// The window is deliberately created and destroyed before the emulator's own
// display comes up, so this cannot interfere with the framebuffer window or
// its event handling.
#include "payload_picker.h"
#include <SDL2/SDL.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#ifdef _WIN32
#include <windows.h>
#include <commdlg.h>
#endif
// System file dialog. Windows has one in the OS; on Linux there is no such
// thing without pulling in a toolkit, so shell out to whichever of the two
// usual helpers is installed and fall back to drag-and-drop if neither is.
static std::string native_file_dialog(void) {
#ifdef _WIN32
char path[MAX_PATH] = {0};
OPENFILENAMEA ofn = {};
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = nullptr;
ofn.lpstrFilter = "RCM payloads (*.bin)\0*.bin\0All files\0*.*\0";
ofn.lpstrFile = path;
ofn.nMaxFile = sizeof(path);
ofn.lpstrTitle = "Select an RCM payload";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST | OFN_NOCHANGEDIR;
if (GetOpenFileNameA(&ofn))
return std::string(path);
return std::string();
#else
const char *cmds[] = {
"zenity --file-selection --title='Select an RCM payload' "
"--file-filter='RCM payloads | *.bin' --file-filter='All files | *' 2>/dev/null",
"kdialog --getopenfilename . '*.bin|RCM payloads' 2>/dev/null",
};
for (const char *cmd : cmds) {
FILE *p = popen(cmd, "r");
if (!p)
continue;
char buf[4096] = {0};
char *got = fgets(buf, sizeof(buf), p);
int rc = pclose(p);
if (got && rc == 0) {
std::string s(buf);
while (!s.empty() && (s.back() == '\n' || s.back() == '\r'))
s.pop_back();
if (!s.empty())
return s;
}
}
printf("[picker] no zenity or kdialog found - drag a file onto the window\n");
return std::string();
#endif
}
std::string payload_picker_run(void) {
// Test hook: run the whole window lifecycle, then return this path as if
// it had been dropped. Lets the picker path - which is otherwise only
// reachable by a human dragging a file - be exercised by a script.
const char *forced = getenv("RCM_EMU_PICK");
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "[picker] no video subsystem (%s)\n", SDL_GetError());
return std::string();
}
SDL_Window *win = SDL_CreateWindow(
"RCM Emulator - drop a payload",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 220,
SDL_WINDOW_SHOWN);
if (!win) {
fprintf(stderr, "[picker] no window (%s)\n", SDL_GetError());
return std::string();
}
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
// Drop events are off by default on some platforms.
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
printf("[picker] drop a payload on the window, or press O to browse"
" (Esc quits)\n");
fflush(stdout);
std::string chosen;
bool done = false;
int forced_frames = forced ? 30 : 0; // let the window really come up
while (!done) {
if (forced && --forced_frames <= 0) {
chosen = forced;
done = true;
}
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_QUIT:
done = true;
break;
case SDL_DROPFILE:
if (ev.drop.file) {
chosen = ev.drop.file;
SDL_free(ev.drop.file);
done = true;
}
break;
case SDL_KEYDOWN:
if (ev.key.keysym.sym == SDLK_ESCAPE) {
done = true;
} else if (ev.key.keysym.sym == SDLK_o) {
// Raising the dialog while holding the window is fine on
// both platforms; the dialog is modal to itself only.
chosen = native_file_dialog();
if (!chosen.empty())
done = true;
}
break;
default:
break;
}
}
// Draw a drop target: a dashed frame with an upload glyph inside,
// so the window reads as "put a file here" rather than as a blank
// panel. All of it is rectangles and one filled triangle - a font
// would have to be vendored for a screen that lives a few seconds,
// and the instruction is already on the title bar.
if (ren) {
SDL_SetRenderDrawColor(ren, 0x1B, 0x1B, 0x1B, 0xFF);
SDL_RenderClear(ren);
const int W = 640, H = 220;
const int mx = 24, my = 20; // margin of the frame
SDL_SetRenderDrawColor(ren, 0x00, 0x8C, 0xC8, 0xFF);
// Dashed border: 14 px of line, 10 px of gap.
for (int x = mx; x < W - mx; x += 24) {
SDL_Rect t = {x, my, 14, 2};
SDL_Rect b = {x, H - my - 2, 14, 2};
SDL_RenderFillRect(ren, &t);
SDL_RenderFillRect(ren, &b);
}
for (int y = my; y < H - my; y += 24) {
SDL_Rect l = {mx, y, 2, 14};
SDL_Rect r = {W - mx - 2, y, 2, 14};
SDL_RenderFillRect(ren, &l);
SDL_RenderFillRect(ren, &r);
}
// Upload glyph: arrow out of a tray, centred.
const int cx = W / 2, cy = H / 2 - 12;
SDL_Rect stem = {cx - 6, cy - 6, 12, 40};
SDL_RenderFillRect(ren, &stem);
for (int i = 0; i < 26; i++) { // filled triangle head
SDL_Rect row = {cx - i, cy - 6 - 26 + i, 2 * i + 1, 1};
SDL_RenderFillRect(ren, &row);
}
SDL_Rect tray_l = {cx - 46, cy + 46, 4, 22};
SDL_Rect tray_r = {cx + 42, cy + 46, 4, 22};
SDL_Rect tray_b = {cx - 46, cy + 64, 92, 4};
SDL_RenderFillRect(ren, &tray_l);
SDL_RenderFillRect(ren, &tray_r);
SDL_RenderFillRect(ren, &tray_b);
SDL_RenderPresent(ren);
}
SDL_Delay(16);
}
if (ren)
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
// Pump once so the window is really gone before the emulator's own opens.
SDL_PumpEvents();
if (!chosen.empty()) {
printf("[picker] payload: %s\n", chosen.c_str());
fflush(stdout);
}
return chosen;
}
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include <string>
// Shown when the emulator is started with no payload argument. Returns the
// chosen path, or an empty string if the user closed the window without
// picking one.
std::string payload_picker_run(void);
+2 -1
View File
@@ -1,4 +1,5 @@
#include "i2c3.h" #include "i2c3.h"
#include "trace.h"
#include "../emu_state.h" #include "../emu_state.h"
#include "memory_map.h" #include "memory_map.h"
@@ -289,7 +290,7 @@ void execute(EmuState *state, uint32_t expected_rx_size) {
break; break;
} }
printf("[i2c3] EXEC slave=0x%02X dir=%c cmd=0x%02X tx=%zuB rx=%zuB stack=%zu\n", TRACE("[i2c3] EXEC slave=0x%02X dir=%c cmd=0x%02X tx=%zuB rx=%zuB stack=%zu\n",
slave_addr, dir_read ? 'R' : 'W', cmd, tx_buf.size(), rx_buf.size(), slave_addr, dir_read ? 'R' : 'W', cmd, tx_buf.size(), rx_buf.size(),
ev_stack.size()); ev_stack.size());
} }
+27 -26
View File
@@ -1,4 +1,5 @@
#include "mmio.h" #include "mmio.h"
#include "trace.h"
#include "../emu_state.h" #include "../emu_state.h"
#include "i2c3.h" #include "i2c3.h"
#include "memory_map.h" #include "memory_map.h"
@@ -462,7 +463,7 @@ uint32_t gpio_read(EmuState *state, uint64_t addr) {
val &= ~(1 << 6); // VOL_UP = PX6 val &= ~(1 << 6); // VOL_UP = PX6
if (state->btn_vol_down) if (state->btn_vol_down)
val &= ~(1 << 7); // VOL_DOWN = PX7 val &= ~(1 << 7); // VOL_DOWN = PX7
printf("[gpio] R: Port X IN = 0x%02X\n", val); TRACE("[gpio] R: Port X IN = 0x%02X\n", val);
return val; return val;
} }
@@ -518,7 +519,7 @@ uint32_t gpio_read(EmuState *state, uint64_t addr) {
// question regardless of what is on the board. // question regardless of what is on the board.
if (offset == 0x13C) { if (offset == 0x13C) {
uint32_t v = gpio_h_in(state); uint32_t v = gpio_h_in(state);
printf("[gpio] R: Port H IN = 0x%02X\n", v); TRACE("[gpio] R: Port H IN = 0x%02X\n", v);
return v; return v;
} }
@@ -541,14 +542,14 @@ uint32_t gpio_read(EmuState *state, uint64_t addr) {
if (bank_off < 0x30) { if (bank_off < 0x30) {
// CNF / OE / OUT: hand back the cached write. // CNF / OE / OUT: hand back the cached write.
uint32_t v = mmio_regs.count(addr) ? mmio_regs[addr] : 0; uint32_t v = mmio_regs.count(addr) ? mmio_regs[addr] : 0;
printf("[gpio] R: offset 0x%X (CNF/OE/OUT cache) = 0x%08X\n", offset, v); TRACE("[gpio] R: offset 0x%X (CNF/OE/OUT cache) = 0x%08X\n", offset, v);
return v; return v;
} }
if (bank_off < 0x40) { if (bank_off < 0x40) {
// IN: mirror of the matching OUT one slot earlier. // IN: mirror of the matching OUT one slot earlier.
uint64_t out_addr = addr - 0x10; uint64_t out_addr = addr - 0x10;
uint32_t v = mmio_regs.count(out_addr) ? mmio_regs[out_addr] : 0; uint32_t v = mmio_regs.count(out_addr) ? mmio_regs[out_addr] : 0;
printf("[gpio] R: offset 0x%X (IN, mirror of OUT @ 0x%X) = 0x%08X\n", TRACE("[gpio] R: offset 0x%X (IN, mirror of OUT @ 0x%X) = 0x%08X\n",
offset, (uint32_t)(offset - 0x10), v); offset, (uint32_t)(offset - 0x10), v);
return v; return v;
} }
@@ -556,13 +557,13 @@ uint32_t gpio_read(EmuState *state, uint64_t addr) {
// plain register they write through, 0x80 lower. // plain register they write through, 0x80 lower.
if (bank_off >= 0x80 && bank_off < 0xB0) { if (bank_off >= 0x80 && bank_off < 0xB0) {
uint32_t v = mmio_regs.count(addr - 0x80) ? mmio_regs[addr - 0x80] : 0; uint32_t v = mmio_regs.count(addr - 0x80) ? mmio_regs[addr - 0x80] : 0;
printf("[gpio] R: offset 0x%X (masked alias of 0x%X) = 0x%08X\n", offset, TRACE("[gpio] R: offset 0x%X (masked alias of 0x%X) = 0x%08X\n", offset,
(uint32_t)(offset - 0x80), v); (uint32_t)(offset - 0x80), v);
return v; return v;
} }
} }
printf("[gpio] R: offset 0x%X = 0\n", offset); TRACE("[gpio] R: offset 0x%X = 0\n", offset);
return 0; return 0;
} }
@@ -583,10 +584,10 @@ void gpio_write(EmuState *state, uint64_t addr, uint32_t val) {
uint32_t mask = (val >> 8) & 0xFF; uint32_t mask = (val >> 8) & 0xFF;
uint32_t cur = mmio_regs.count(plain) ? mmio_regs[plain] : 0; uint32_t cur = mmio_regs.count(plain) ? mmio_regs[plain] : 0;
mmio_regs[plain] = (cur & ~mask) | (val & mask); mmio_regs[plain] = (cur & ~mask) | (val & mask);
printf("[gpio] W: offset 0x%X masked -> 0x%X = 0x%08X\n", offset, TRACE("[gpio] W: offset 0x%X masked -> 0x%X = 0x%08X\n", offset,
(uint32_t)(offset - 0x80), mmio_regs[plain]); (uint32_t)(offset - 0x80), mmio_regs[plain]);
} else { } else {
printf("[gpio] W: offset 0x%X = 0x%08X\n", offset, val); TRACE("[gpio] W: offset 0x%X = 0x%08X\n", offset, val);
} }
// Bank 1 holds ports E..H, and port H carries BT_REG_ON, so re-sample the // Bank 1 holds ports E..H, and port H carries BT_REG_ON, so re-sample the
@@ -1751,7 +1752,7 @@ uint32_t misc_read(EmuState *state, uint64_t addr) {
else if (offset == 0xFE) else if (offset == 0xFE)
result = 0x0303; // SDHCI Version 4.0 result = 0x0303; // SDHCI Version 4.0
printf("[sdmmc%c] R: 0x%02lX = 0x%08X (PC=0x%llX)\n", TRACE("[sdmmc%c] R: 0x%02lX = 0x%08X (PC=0x%llX)\n",
(base == SDMMC4_BASE) ? '4' : '1', (unsigned long)offset, (base == SDMMC4_BASE) ? '4' : '1', (unsigned long)offset,
result, (unsigned long long)state->insn_count); result, (unsigned long long)state->insn_count);
return result; return result;
@@ -1773,7 +1774,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
uint32_t base = (addr >= SDMMC4_BASE) ? SDMMC4_BASE : SDMMC1_BASE; uint32_t base = (addr >= SDMMC4_BASE) ? SDMMC4_BASE : SDMMC1_BASE;
uint32_t offset = addr - base; uint32_t offset = addr - base;
printf("[sdmmc%c] W: 0x%02X = 0x%08X (size %d)\n", TRACE("[sdmmc%c] W: 0x%02X = 0x%08X (size %d)\n",
(base == SDMMC4_BASE) ? '4' : '1', offset, val, size); (base == SDMMC4_BASE) ? '4' : '1', offset, val, size);
uint32_t &arg = uint32_t &arg =
@@ -1847,10 +1848,10 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
bool is_read = (trnmod & 0x0010); // Bit 4 of TRNMOD is Read/Write bool is_read = (trnmod & 0x0010); // Bit 4 of TRNMOD is Read/Write
if (base == SDMMC1_BASE) { if (base == SDMMC1_BASE) {
printf("[sdmmc] W: offset 0x%X = 0x%08X (PC=0x%08llX)\n", offset, val, TRACE("[sdmmc] W: offset 0x%X = 0x%08X (PC=0x%08llX)\n", offset, val,
(unsigned long long)state->insn_count); (unsigned long long)state->insn_count);
if (offset == 0x0E || (offset == 0x0C && size == 4)) { if (offset == 0x0E || (offset == 0x0C && size == 4)) {
printf("[sdmmc] CMD%d: arg=0x%08X, blkcnt=%d, trnmod=0x%04X\n", cmd, TRACE("[sdmmc] CMD%d: arg=0x%08X, blkcnt=%d, trnmod=0x%04X\n", cmd,
arg, blkcnt, trnmod); arg, blkcnt, trnmod);
} }
fflush(stdout); fflush(stdout);
@@ -2092,7 +2093,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
uint8_t val = (arg >> 8) & 0xFF; uint8_t val = (arg >> 8) & 0xFF;
if (index == 179) { // PARTITION_CONFIG if (index == 179) { // PARTITION_CONFIG
state->emmc_partition = val & 0x7; state->emmc_partition = val & 0x7;
printf("[sdmmc] eMMC Partition Switch: %u\n", TRACE("[sdmmc] eMMC Partition Switch: %u\n",
state->emmc_partition); state->emmc_partition);
} }
rsp[0] = r1_base | (4 << 9); rsp[0] = r1_base | (4 << 9);
@@ -2130,7 +2131,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
uc_mem_write(uc, dma_addr, scr, 8); uc_mem_write(uc, dma_addr, scr, 8);
norintsts |= 0x0002; norintsts |= 0x0002;
} else { } else {
printf("[sdmmc] ERROR: Storage command %d on base 0x%llX but fd is " TRACE("[sdmmc] ERROR: Storage command %d on base 0x%llX but fd is "
"-1. (Missing --sd or --boot0?)\n", "-1. (Missing --sd or --boot0?)\n",
cmd, (unsigned long long)base); cmd, (unsigned long long)base);
} }
@@ -2162,7 +2163,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
if (bcnt == 0) if (bcnt == 0)
bcnt = 1; bcnt = 1;
if (cmd == 17 || cmd == 18) { if (cmd == 17 || cmd == 18) {
printf("[sdmmc] %s READ CMD%d: Sector = %llu, Count = %u (part=%u)\n", TRACE("[sdmmc] %s READ CMD%d: Sector = %llu, Count = %u (part=%u)\n",
(base == SDMMC1_BASE) ? "SD" : "eMMC", cmd, (base == SDMMC1_BASE) ? "SD" : "eMMC", cmd,
(unsigned long long)sector, bcnt, (unsigned long long)sector, bcnt,
(base == SDMMC1_BASE) ? 0 : state->emmc_partition); (base == SDMMC1_BASE) ? 0 : state->emmc_partition);
@@ -2199,7 +2200,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
part_size = (size_t)st.st_size; part_size = (size_t)st.st_size;
else else
part_size = 4ULL * 1024 * 1024 * 1024; part_size = 4ULL * 1024 * 1024 * 1024;
printf("[sdmmc] GPP part size detected: %zu bytes\n", part_size); TRACE("[sdmmc] GPP part size detected: %zu bytes\n", part_size);
} }
if (!part_size) return; // No GPP files — nothing to read. if (!part_size) return; // No GPP files — nothing to read.
int part_idx = (int)(current_off / part_size); int part_idx = (int)(current_off / part_size);
@@ -2210,7 +2211,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
if (is_read) { if (is_read) {
ssize_t res = pread(real_fd, io_buf.data(), len, part_off); ssize_t res = pread(real_fd, io_buf.data(), len, part_off);
if (res != (ssize_t)len) if (res != (ssize_t)len)
printf("[sdmmc] eMMC GPP READ ERROR: res=%zd, " TRACE("[sdmmc] eMMC GPP READ ERROR: res=%zd, "
"expected=%zu, off=0x%llX\n", "expected=%zu, off=0x%llX\n",
res, len, (unsigned long long)part_off); res, len, (unsigned long long)part_off);
uc_mem_write(uc, dma_addr, io_buf.data(), len); uc_mem_write(uc, dma_addr, io_buf.data(), len);
@@ -2218,7 +2219,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
uc_mem_read(uc, dma_addr, io_buf.data(), len); uc_mem_read(uc, dma_addr, io_buf.data(), len);
ssize_t res = pwrite(real_fd, io_buf.data(), len, part_off); ssize_t res = pwrite(real_fd, io_buf.data(), len, part_off);
if (res != (ssize_t)len) if (res != (ssize_t)len)
printf("[sdmmc] eMMC GPP WRITE ERROR: res=%zd, " TRACE("[sdmmc] eMMC GPP WRITE ERROR: res=%zd, "
"expected=%zu, off=0x%llX\n", "expected=%zu, off=0x%llX\n",
res, len, (unsigned long long)part_off); res, len, (unsigned long long)part_off);
} }
@@ -2229,7 +2230,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
ssize_t res = ssize_t res =
pread(current_fd, io_buf.data(), len, current_off); pread(current_fd, io_buf.data(), len, current_off);
if (res != (ssize_t)len) if (res != (ssize_t)len)
printf("[sdmmc] SD/BOOT READ ERROR: res=%zd, expected=%zu, " TRACE("[sdmmc] SD/BOOT READ ERROR: res=%zd, expected=%zu, "
"off=0x%llX\n", "off=0x%llX\n",
res, len, (unsigned long long)current_off); res, len, (unsigned long long)current_off);
uc_mem_write(uc, dma_addr, io_buf.data(), len); uc_mem_write(uc, dma_addr, io_buf.data(), len);
@@ -2238,7 +2239,7 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value,
ssize_t res = ssize_t res =
pwrite(current_fd, io_buf.data(), len, current_off); pwrite(current_fd, io_buf.data(), len, current_off);
if (res != (ssize_t)len) if (res != (ssize_t)len)
printf("[sdmmc] SD/BOOT WRITE ERROR: res=%zd, expected=%zu, " TRACE("[sdmmc] SD/BOOT WRITE ERROR: res=%zd, expected=%zu, "
"off=0x%llX\n", "off=0x%llX\n",
res, len, (unsigned long long)current_off); res, len, (unsigned long long)current_off);
} }
@@ -3262,7 +3263,7 @@ static void sysreg_write(EmuState *state, uint64_t addr, uint32_t val) {
actmon_write(state, offset - ACTMON_OFF_IN_SYSREG, val); actmon_write(state, offset - ACTMON_OFF_IN_SYSREG, val);
return; return;
} }
printf("[sysreg] W: 0x%08X = 0x%08X\n", offset, val); TRACE("[sysreg] W: 0x%08X = 0x%08X\n", offset, val);
} }
// ==================== SOC_THERM (on-die thermal sensors) ==================== // ==================== SOC_THERM (on-die thermal sensors) ====================
@@ -3444,7 +3445,7 @@ static void hook_mmio_read(uc_engine *uc, uc_mem_type type, uint64_t address,
} else { } else {
result = misc_read(state, address); result = misc_read(state, address);
} }
// printf("[mmio] R: 0x%08llX = 0x%08X (PC=0x%08X)\n", (unsigned long // TRACE("[mmio] R: 0x%08llX = 0x%08X (PC=0x%08X)\n", (unsigned long
// long)address, result, pc); // long)address, result, pc);
} }
@@ -3457,7 +3458,7 @@ static void hook_mmio_write(uc_engine *uc, uc_mem_type type, uint64_t address,
uint32_t val = (uint32_t)value; uint32_t val = (uint32_t)value;
uint32_t pc; uint32_t pc;
uc_reg_read(uc, UC_ARM_REG_PC, &pc); uc_reg_read(uc, UC_ARM_REG_PC, &pc);
// printf("[mmio] W: 0x%08llX = 0x%08X (PC=0x%08X)\n", (unsigned long // TRACE("[mmio] W: 0x%08llX = 0x%08X (PC=0x%08X)\n", (unsigned long
// long)address, val, pc); fflush(stdout); // long)address, val, pc); fflush(stdout);
if (address >= IRAM_BASE && address < IRAM_BASE + 0x40000) { if (address >= IRAM_BASE && address < IRAM_BASE + 0x40000) {
@@ -3620,7 +3621,7 @@ static void hook_mmio_write(uc_engine *uc, uc_mem_type type, uint64_t address,
(address >= I2C5_BASE && address < I2C5_BASE + 0x1000) || (address >= I2C5_BASE && address < I2C5_BASE + 0x1000) ||
(address >= SE_BASE && address < SE_BASE + SE_SIZE) || (address >= SE_BASE && address < SE_BASE + SE_SIZE) ||
(address >= RTC_BASE && address < RTC_BASE + RTC_SIZE))) { (address >= RTC_BASE && address < RTC_BASE + RTC_SIZE))) {
printf("[mmio] W: 0x%08llX = 0x%08X (PC=0x%08X)\n", TRACE("[mmio] W: 0x%08llX = 0x%08X (PC=0x%08X)\n",
(unsigned long long)address, val, pc); (unsigned long long)address, val, pc);
fflush(stdout); fflush(stdout);
} }
@@ -3634,10 +3635,10 @@ static bool hook_unmapped(uc_engine *uc, uc_mem_type type, uint64_t address,
uint32_t pc; uint32_t pc;
uc_reg_read(uc, UC_ARM_REG_PC, &pc); uc_reg_read(uc, UC_ARM_REG_PC, &pc);
if (type == UC_MEM_READ_UNMAPPED) { if (type == UC_MEM_READ_UNMAPPED) {
printf("[mmio] UNMAPPED R: 0x%08lX (PC=0x%08X)\n", (unsigned long)address, TRACE("[mmio] UNMAPPED R: 0x%08lX (PC=0x%08X)\n", (unsigned long)address,
pc); pc);
} else { } else {
printf("[mmio] UNMAPPED W: 0x%08lX = 0x%08llX (PC=0x%08X)\n", TRACE("[mmio] UNMAPPED W: 0x%08lX = 0x%08llX (PC=0x%08X)\n",
(unsigned long)address, (unsigned long long)value, pc); (unsigned long)address, (unsigned long long)value, pc);
} }
uint64_t page = address & ~0xFFFULL; uint64_t page = address & ~0xFFFULL;
+2 -1
View File
@@ -1,4 +1,5 @@
#include "sdmmc.h" #include "sdmmc.h"
#include "trace.h"
#include "memory_map.h" #include "memory_map.h"
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
@@ -102,7 +103,7 @@ static void handle_sd_command(uc_engine *uc, EmuState *state, int id, uint32_t c
uint32_t *norintsts = (id == 1) ? &state->sdmmc_norintsts : &state->sdmmc4_norintsts; uint32_t *norintsts = (id == 1) ? &state->sdmmc_norintsts : &state->sdmmc4_norintsts;
bool *last_55 = (id == 1) ? &state->last_cmd_was_55 : &state->last_cmd4_was_55; bool *last_55 = (id == 1) ? &state->last_cmd_was_55 : &state->last_cmd4_was_55;
printf("[sdmmc%d] CMD%d Arg: 0x%08X\n", id, cmd, *arg); TRACE("[sdmmc%d] CMD%d Arg: 0x%08X\n", id, cmd, *arg);
*norintsts |= 0x01; // Command Complete interrupt *norintsts |= 0x01; // Command Complete interrupt
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include <cstdio>
// Per-access register tracing. Off unless asked for, because it is enormous:
// a full hwtest sweep emits ~52,000 lines, ~51,000 of which are [gpio],
// [mmio] and [sdmmc] traces. Piped to a file that is merely wasteful, but on
// a Windows console every line goes through conhost one write at a time and
// the emulator slows to a crawl - which is exactly what a user sees when
// they double-click the executable instead of running it from a script.
//
// Enable with --trace on the command line or RCM_EMU_TRACE=1.
extern bool emu_trace_enabled;
#define TRACE(...) \
do { \
if (emu_trace_enabled) \
printf(__VA_ARGS__); \
} while (0)