diff --git a/display/config_window.cpp b/display/config_window.cpp index 6898009..bb4aa94 100644 --- a/display/config_window.cpp +++ b/display/config_window.cpp @@ -53,6 +53,21 @@ void reset_to_defaults(EmuState *s) { s->pmic_silicon_rev = 0; s->cpu_pmic_version = 0; s->sd_inserted = true; + s->sd_cid_manfid = 0x03; + s->sd_cid_oemid = 0x5344; // "SD" + s->sd_cid_prod_name = 0x3155445355ULL; // "USDU1" + s->sd_cid_hwrev = 1; + s->sd_cid_fwrev = 0; + s->sd_cid_serial = 0xC0FFEE42; + s->sd_cid_month = 10; + s->sd_cid_year = 2024; + s->emmc_cid_manfid = 0x90; + s->emmc_cid_oemid = 0x01; + s->emmc_cid_prod_name = 0x326134474248ULL; // "HBG4a2" + s->emmc_cid_prv = 0x07; + s->emmc_cid_serial = 0x12345678; + s->emmc_cid_month = 6; + s->emmc_cid_year = 2017; s->fuse_0x100 = 1; s->fuse_0x110 = 0x83; s->fuse_0x1A0 = 0x06; @@ -86,6 +101,51 @@ bool atomic_slider_temp(const char *label, std::atomic &a, float lo_c, float return false; } +// Vendor name + hex tuple. Hex is what gets written; the name is just a UI +// label for known IDs. List mirrors Hekate's gui_info.c switch statements +// for SD (sd_storage.cid.manfid) and eMMC (emmc_storage.cid.manfid). +struct VendorEntry { uint8_t id; const char *name; }; + +static const VendorEntry kSdVendors[] = { + {0x00,"Fake"}, {0x01,"Panasonic"}, {0x02,"Toshiba"}, {0x03,"SanDisk"}, + {0x06,"Ritek"}, {0x09,"ATP"}, {0x13,"Kingmax"}, {0x19,"Dynacard"}, + {0x1A,"Power Quotient"}, {0x1B,"Samsung"}, {0x1D,"AData"}, {0x27,"Phison"}, + {0x28,"Barun Electronics"}, {0x31,"Silicon Power"}, {0x41,"Kingston"}, + {0x51,"STEC"}, {0x5D,"SwissBit"}, {0x61,"Netlist"}, {0x63,"Cactus"}, + {0x73,"Bongiovi"}, {0x74,"Jiaelec"}, {0x76,"Patriot"}, {0x82,"Jiang Tay"}, + {0x83,"Netcom"}, {0x84,"Strontium"}, {0x9C,"Barun / Sony"}, {0x9F,"Taishin"}, + {0xAD,"Longsys"}, +}; +static const VendorEntry kEmmcVendors[] = { + {0x11,"Toshiba"}, {0x15,"Samsung"}, {0x45,"SanDisk"}, + {0x89,"Silicon Motion"}, {0x90,"SK Hynix"}, +}; + +// Combo populated with `entries`; selecting one writes its hex to `target`. +// If the current value doesn't match any entry, the combo's preview shows +// "Custom (0xNN)". The hex is editable via the matching InputScalar above +// or below this widget. +static void vendor_preset_combo(const char *label, + std::atomic &target, + const VendorEntry *entries, size_t n) { + uint8_t cur = target.load(); + char preview[40]; + const char *match = nullptr; + for (size_t i = 0; i < n; i++) if (entries[i].id == cur) { match = entries[i].name; break; } + if (match) snprintf(preview, sizeof(preview), "%s (0x%02X)", match, cur); + else snprintf(preview, sizeof(preview), "Custom (0x%02X)", cur); + if (ImGui::BeginCombo(label, preview)) { + for (size_t i = 0; i < n; i++) { + char item[40]; + snprintf(item, sizeof(item), "%s (0x%02X)", entries[i].name, entries[i].id); + bool sel = (entries[i].id == cur); + if (ImGui::Selectable(item, sel)) target.store(entries[i].id); + if (sel) ImGui::SetItemDefaultFocus(); + } + ImGui::EndCombo(); + } +} + // Helper: hex input that reads/writes a uint32 atomic. bool atomic_hex_input(const char *label, std::atomic &a) { uint32_t v = a.load(); @@ -189,6 +249,102 @@ void build_ui(EmuState *state) { if (ImGui::Checkbox("SD card inserted", &ins)) { state->sd_inserted.store(ins); } + + ImGui::TextDisabled("SD CID (read by Hekate at SD init -- press 'Reboot' to apply):"); + + // manfid: vendor preset + free hex entry + vendor_preset_combo("Vendor preset", state->sd_cid_manfid, + kSdVendors, IM_ARRAYSIZE(kSdVendors)); + { + uint32_t v = state->sd_cid_manfid.load(); + if (ImGui::InputScalar("Manfid (hex)", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", + ImGuiInputTextFlags_CharsHexadecimal)) { + state->sd_cid_manfid.store((uint8_t)(v & 0xFF)); + } + } + // OEM ID (2 ASCII chars) + { + uint16_t raw = state->sd_cid_oemid.load(); + char buf[3] = { (char)(raw >> 8), (char)(raw & 0xFF), 0 }; + if (ImGui::InputText("OEM ID (2 ASCII)", buf, sizeof(buf))) { + uint16_t packed = ((uint16_t)(uint8_t)buf[0] << 8) | (uint16_t)(uint8_t)buf[1]; + state->sd_cid_oemid.store(packed); + } + } + // Product name (5 ASCII chars, packed in low 5 bytes of uint64) + { + uint64_t raw = state->sd_cid_prod_name.load(); + char buf[6]; + for (int i = 0; i < 5; i++) buf[i] = (char)((raw >> (i * 8)) & 0xFF); + buf[5] = 0; + if (ImGui::InputText("Product (5 ASCII)", buf, sizeof(buf))) { + uint64_t packed = 0; + for (int i = 0; i < 5; i++) packed |= (uint64_t)(uint8_t)buf[i] << (i * 8); + state->sd_cid_prod_name.store(packed); + } + } + atomic_slider_int ("HW rev", state->sd_cid_hwrev, 0, 15); + atomic_slider_int ("FW rev", state->sd_cid_fwrev, 0, 15); + // Serial as 8-digit hex + { + uint32_t sn = state->sd_cid_serial.load(); + if (ImGui::InputScalar("Serial", ImGuiDataType_U32, &sn, nullptr, nullptr, "%08X", + ImGuiInputTextFlags_CharsHexadecimal)) { + state->sd_cid_serial.store(sn); + } + } + atomic_slider_int ("Month", state->sd_cid_month, 1, 12); + atomic_slider_int ("Year", state->sd_cid_year, 2000, 2099); + + ImGui::Spacing(); + ImGui::TextDisabled("eMMC CID (SDMMC4 -- press 'Reboot' to apply):"); + + vendor_preset_combo("eMMC vendor preset", state->emmc_cid_manfid, + kEmmcVendors, IM_ARRAYSIZE(kEmmcVendors)); + { + uint32_t v = state->emmc_cid_manfid.load(); + if (ImGui::InputScalar("eMMC Manfid (hex)", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", + ImGuiInputTextFlags_CharsHexadecimal)) { + state->emmc_cid_manfid.store((uint8_t)(v & 0xFF)); + } + } + { + uint32_t v = state->emmc_cid_oemid.load(); + if (ImGui::InputScalar("eMMC OEM ID", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", + ImGuiInputTextFlags_CharsHexadecimal)) { + state->emmc_cid_oemid.store((uint8_t)(v & 0xFF)); + } + } + { + uint64_t raw = state->emmc_cid_prod_name.load(); + char buf[7]; + for (int i = 0; i < 6; i++) buf[i] = (char)((raw >> (i * 8)) & 0xFF); + buf[6] = 0; + if (ImGui::InputText("eMMC Product (6 ASCII)", buf, sizeof(buf))) { + uint64_t packed = 0; + for (int i = 0; i < 6; i++) packed |= (uint64_t)(uint8_t)buf[i] << (i * 8); + state->emmc_cid_prod_name.store(packed); + } + } + { + uint32_t v = state->emmc_cid_prv.load(); + if (ImGui::InputScalar("eMMC Prod Rev", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", + ImGuiInputTextFlags_CharsHexadecimal)) { + state->emmc_cid_prv.store((uint8_t)(v & 0xFF)); + } + } + { + uint32_t sn = state->emmc_cid_serial.load(); + if (ImGui::InputScalar("eMMC Serial", ImGuiDataType_U32, &sn, nullptr, nullptr, "%08X", + ImGuiInputTextFlags_CharsHexadecimal)) { + state->emmc_cid_serial.store(sn); + } + } + atomic_slider_int ("eMMC Month", state->emmc_cid_month, 1, 12); + // 2025 is the upper bound: Hekate adds +16 to the parsed year only + // when it's strictly < 2010, so raw offsets 13..15 (2010..2012) never + // get bumped and eMMC years 2026..2028 are unreachable. + atomic_slider_int ("eMMC Year", state->emmc_cid_year, 2013, 2025); } if (ImGui::CollapsingHeader("USB-PD (BM92T36)", ImGuiTreeNodeFlags_DefaultOpen)) { @@ -225,6 +381,10 @@ void build_ui(EmuState *state) { if (ImGui::Checkbox("Paused", &paused)) { state->paused.store(paused); } + ImGui::SameLine(); + if (ImGui::Button("Reboot")) { + state->reboot_requested.store(true); + } ImGui::Text("emu_usec: %llu", (unsigned long long)state->emu_usec); ImGui::Text("insn_count: %llu", (unsigned long long)state->insn_count); diff --git a/emu_state.h b/emu_state.h index b1737e9..a2213a4 100644 --- a/emu_state.h +++ b/emu_state.h @@ -63,6 +63,11 @@ struct EmuState { // Emulation control. std::atomic running{true}; std::atomic paused{false}; + std::atomic reboot_requested{false}; + + // Payload kept around for soft reboot (re-write to IRAM and reset PC). + uint8_t *payload_ptr = nullptr; + size_t payload_len = 0; // Backlight brightness (0-255). uint32_t backlight = 100; @@ -72,7 +77,8 @@ struct EmuState { uint64_t insn_count = 0; // DRAM pointer (host memory backing the emulated DRAM). - uint8_t *dram_ptr = nullptr; + uint8_t *dram_ptr = nullptr; // High DRAM (0xC0000000+, 1 GB, includes FB) + uint8_t *dram_low_ptr = nullptr; // Low DRAM (0x80000000+, 256 MB, holds Nyx) uint8_t *iram_ptr = nullptr; uint8_t *fb_ptr = nullptr; @@ -143,6 +149,31 @@ struct EmuState { // SD card insertion (GPIO Port Z bit 1 = 0 means inserted). std::atomic sd_inserted{true}; + // SD card identity (returned for SDMMC1 CMD2 ALL_SEND_CID). + // Hekate parses these out of the R2 response per bdk/storage/sdmmc.c + // _sd_storage_parse_cid; the I2C handler builds the 16-byte CID payload + // from these atomics each time CMD2 fires. + std::atomic sd_cid_manfid{0x03}; // 0x03 = SanDisk + std::atomic sd_cid_oemid{0x5344}; // "SD" + std::atomic sd_cid_prod_name{0x3155445355ULL}; // "USDU1" (low byte first) + std::atomic sd_cid_hwrev{1}; // 0-15 + std::atomic sd_cid_fwrev{0}; // 0-15 + std::atomic sd_cid_serial{0xC0FFEE42}; + std::atomic sd_cid_month{10}; // 1-12 + std::atomic sd_cid_year{2024}; // 2000-2255 + + // eMMC identity (SDMMC4 CMD2). Layout per _mmc_storage_parse_cid (MMC v4): + // 8-bit manfid, 8-bit oemid, 6-byte prod_name, 8-bit prv (BCD nibble.nibble), + // 32-bit serial, 4-bit month, 4-bit year offset (year base = 2013 when + // ext_csd.rev >= 5, which is what our EXT_CSD reports). + std::atomic emmc_cid_manfid{0x90}; // 0x90 = SK Hynix + std::atomic emmc_cid_oemid{0x01}; + std::atomic emmc_cid_prod_name{0x326134474248ULL}; // "HBG4a2" (low byte first) + std::atomic emmc_cid_prv{0x07}; // displayed as "0.7" (low.high) + std::atomic emmc_cid_serial{0x12345678}; + std::atomic emmc_cid_month{6}; // 1-12 + std::atomic emmc_cid_year{2017}; // 2013-2025 (Hekate caps at 2025: see config_window.cpp) + // SoC thermal sensor (TMP451, I2C5 @ 0x4C). std::atomic soc_temp_c10{420}; // remote channel (SoC die): °C * 10 std::atomic pcb_temp_c10{350}; // local channel (PCB): °C * 10 diff --git a/main.cpp b/main.cpp index 63634c3..691a9d5 100644 --- a/main.cpp +++ b/main.cpp @@ -166,7 +166,10 @@ static uc_engine *setup_emulation(EmuState *state, uint8_t *payload, size_t payl uc_hook_add(uc, &trace_h, UC_HOOK_CODE, (void*)trace_callback, nullptr, 1, 0); // ---- Map DRAM Low (256MB @ 0x80000000) ---- - err = uc_mem_map(uc, DRAM_BASE, 256 * 1024 * 1024, UC_PROT_ALL); + // Allocated host-side so soft reboot can memset() it (Nyx loads here and + // its file-static SD/eMMC caches would otherwise survive across reboots). + state->dram_low_ptr = (uint8_t *)calloc(1, 256 * 1024 * 1024); + err = uc_mem_map_ptr(uc, DRAM_BASE, 256 * 1024 * 1024, UC_PROT_ALL, state->dram_low_ptr); if (err != UC_ERR_OK) { fprintf(stderr, "[error] Failed to map low DRAM: %s\n", uc_strerror(err)); uc_close(uc); @@ -338,10 +341,13 @@ int main(int argc, char *argv[]) { } } - // Setup ARM emulation + // Setup ARM emulation. Hold on to the payload buffer in EmuState so the + // soft-reboot path (config window "Reboot" button) can re-write it into + // IRAM without re-reading from disk. uc_engine *uc = setup_emulation(&state, payload, payload_size); - free(payload); - if (!uc) return 1; + state.payload_ptr = payload; + state.payload_len = payload_size; + if (!uc) { free(payload); return 1; } // Initialize SDL2 display if (!sdl_display_init()) { @@ -371,6 +377,30 @@ int main(int argc, char *argv[]) { // Process SDL events (keyboard input, window close) if (!sdl_display_poll_events(&state, uc)) break; + // Soft reboot: re-write the payload to IRAM, wipe DRAM (so Nyx and + // the bootloader's file-static caches reset), reset PC/SP/clock and + // re-prime the WDT cookie so Hekate's early boot skips Minerva again. + if (state.reboot_requested.exchange(false)) { + uc_emu_stop(uc); + memset(state.dram_low_ptr, 0, 256 * 1024 * 1024); + memset(state.dram_ptr, 0, 1024 * 1024 * 1024); + uc_mem_write(uc, IPL_LOAD_ADDR, state.payload_ptr, state.payload_len); + uint32_t wdt_magic = 0x544457; + uc_mem_write(uc, 0x4003FF18, &wdt_magic, sizeof(wdt_magic)); + uint32_t reset_pc = IPL_LOAD_ADDR; + uint32_t reset_sp = IPL_STACK_ADDR; + uint32_t reset_cpsr = 0; // ARM mode, all flags clear + uc_reg_write(uc, UC_ARM_REG_PC, &reset_pc); + uc_reg_write(uc, UC_ARM_REG_SP, &reset_sp); + uc_reg_write(uc, UC_ARM_REG_CPSR, &reset_cpsr); + state.fb_addr = FB_BASE; // re-point display at the FB base + state.emu_usec = 0; + state.insn_count = 0; + state.touch_phase = 0; + state.paused = false; + printf("[emu] Soft reboot complete (DRAM wiped)\n"); + } + if (!state.paused) { uint32_t pc, cpsr; uc_reg_read(uc, UC_ARM_REG_PC, &pc); @@ -548,6 +578,8 @@ int main(int argc, char *argv[]) { free(state.iram_ptr); free(state.dram_ptr); + free(state.dram_low_ptr); + free(state.payload_ptr); // fb_ptr points inside dram_ptr; do not free separately. printf("[emu] Done.\n"); diff --git a/t210/mmio.cpp b/t210/mmio.cpp index 96dd2b7..133a520 100644 --- a/t210/mmio.cpp +++ b/t210/mmio.cpp @@ -622,9 +622,14 @@ uint32_t misc_read(EmuState *state, uint64_t addr) { result = (uint32_t)(((base == SDMMC4_BASE) ? state->sdmmc4_adma_addr : state->sdmmc_adma_addr) >> 32); - else if (offset == 0x24) - result = 0x01F70000; // CARD_PRESENT | CD_STABLE | CD_LVL | DAT_LINE_LEVEL - // (Ready) + else if (offset == 0x24) { + // PRESENT_STATE. SDMMC1 honours the SD-insert toggle; SDMMC4 (eMMC) + // is always present. + if (base == SDMMC1_BASE && !state->sd_inserted.load()) + result = 0; // no card present + else + result = 0x01F70000; // CARD_PRESENT | CD_STABLE | CD_LVL | DAT_LINE_LEVEL + } else if (offset == 0x2C) result = 0x0003; // SDHCI_CLOCK_INT_EN | SDHCI_CLOCK_INT_STABLE else if (offset == 0x40) @@ -751,6 +756,16 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value, if (base == SDMMC4_BASE && state->last_cmd4_was_55) r1_base |= 0x0100; + // SDMMC1 with no SD card: signal CMD_TIMEOUT_ERROR for every command. + // Hekate's sd_init_retry sees the timeout, falls back to checking + // gpio_read(PORT_Z, 1), and bails with "Failed to init SD card". + if (base == SDMMC1_BASE && !state->sd_inserted.load()) { + norintsts |= 0x0001; + errintsts |= (1 << 0); + rsp[0] = rsp[1] = rsp[2] = rsp[3] = 0; + return; + } + // Set Command Complete for all commands norintsts |= 0x0001; @@ -781,33 +796,76 @@ void misc_write(uc_engine *uc, EmuState *state, uint64_t addr, int64_t value, ext_csd[215] = (sec_cnt >> 24) & 0xFF; uint64_t dma_addr = 0; - if (trnmod & 0x0001) { // DMA enabled - if ((hostctl & 0x18) == 0x10) { // ADMA2 - uint8_t desc[12]; - if (uc_mem_read(uc, adma_addr, desc, 12) == UC_ERR_OK) { - uint32_t low = *(uint32_t *)(desc + 4); - uint32_t high = *(uint32_t *)(desc + 8); - dma_addr = ((uint64_t)high << 32) | low; - } - } else { // SDMA - dma_addr = sysad; - } - if (dma_addr) uc_mem_write(uc, dma_addr, ext_csd, 512); - } else { - uc_mem_write(uc, sysad, ext_csd, 512); - } + // Tegra's SDMMC uses register 0x58 as the SDMA system-address + // register (Hekate writes the destination buffer pointer directly + // there in sdmmc_driver.c::_sdmmc_dma_init). adma_addr in our state + // captures that write, so it IS the destination, not a descriptor + // pointer. Fall back to sysad (SDHCI standard 0x00) if 0x58 wasn't + // programmed. + dma_addr = adma_addr ? adma_addr : sysad; + if (dma_addr) uc_mem_write(uc, dma_addr, ext_csd, 512); norintsts |= 0x0002; // TRANSFER_COMPLETE } break; case 1: rsp[0] = 0xC0FF8000; break; - case 2: // SEND_CID - rsp[0] = 0x12345678; - rsp[1] = 0xABCDEF01; - rsp[2] = 0x23456789; - rsp[3] = 0xBCDEF012; + case 2: { // SEND_CID + // Build the 16-byte CID from EmuState atomics, then pack into the + // 4 R2 response registers. Hekate's _get_rsp shifts the 4 registers + // left 8 bits (CRC strip) when assembling raw_cid, so we put the + // CID bytes into rspreg pre-shifted right by 8. + uint8_t cid[16] = {0}; + if (base == SDMMC1_BASE) { + // SD CID per _sd_storage_parse_cid (bdk/storage/sdmmc.c). + uint64_t pn = state->sd_cid_prod_name.load(); + cid[0] = state->sd_cid_manfid.load(); + cid[1] = (state->sd_cid_oemid.load() >> 8) & 0xFF; + cid[2] = state->sd_cid_oemid.load() & 0xFF; + cid[3] = pn & 0xFF; + cid[4] = (pn >> 8) & 0xFF; + cid[5] = (pn >> 16) & 0xFF; + cid[6] = (pn >> 24) & 0xFF; + cid[7] = (pn >> 32) & 0xFF; + cid[8] = ((state->sd_cid_hwrev.load() & 0xF) << 4) + | (state->sd_cid_fwrev.load() & 0xF); + uint32_t sn = state->sd_cid_serial.load(); + cid[9] = (sn >> 24) & 0xFF; + cid[10] = (sn >> 16) & 0xFF; + cid[11] = (sn >> 8) & 0xFF; + cid[12] = sn & 0xFF; + uint8_t yr = (uint8_t)(state->sd_cid_year.load() - 2000); + cid[13] = (yr >> 4) & 0x0F; + cid[14] = ((yr & 0x0F) << 4) | (state->sd_cid_month.load() & 0x0F); + } else { + // eMMC CID per _mmc_storage_parse_cid (MMC v4: 8-bit oemid + 6-byte + // prod_name + prv + 32-bit serial + 4-bit month + 4-bit year offset + // from 2013 because we report ext_csd.rev >= 5). + uint64_t pn = state->emmc_cid_prod_name.load(); + cid[0] = state->emmc_cid_manfid.load(); + cid[1] = 0; + cid[2] = state->emmc_cid_oemid.load(); + cid[3] = pn & 0xFF; + cid[4] = (pn >> 8) & 0xFF; + cid[5] = (pn >> 16) & 0xFF; + cid[6] = (pn >> 24) & 0xFF; + cid[7] = (pn >> 32) & 0xFF; + cid[8] = (pn >> 40) & 0xFF; + cid[9] = state->emmc_cid_prv.load(); + uint32_t sn = state->emmc_cid_serial.load(); + cid[10] = (sn >> 24) & 0xFF; + cid[11] = (sn >> 16) & 0xFF; + cid[12] = (sn >> 8) & 0xFF; + cid[13] = sn & 0xFF; + uint8_t yr = (uint8_t)(state->emmc_cid_year.load() - 2013); + cid[14] = ((state->emmc_cid_month.load() & 0x0F) << 4) | (yr & 0x0F); + } + rsp[3] = (cid[0] << 16) | (cid[1] << 8) | cid[2]; + rsp[2] = (cid[3] << 24) | (cid[4] << 16) | (cid[5] << 8) | cid[6]; + rsp[1] = (cid[7] << 24) | (cid[8] << 16) | (cid[9] << 8) | cid[10]; + rsp[0] = (cid[11] << 24) | (cid[12] << 16) | (cid[13] << 8) | cid[14]; break; + } case 3: // SEND_RELATIVE_ADDR if (base == SDMMC1_BASE) { // SD: R6 response — RCA in upper 16 bits, status in lower 16.