diff --git a/buildroot-external/Config.in b/buildroot-external/Config.in index b50466b..81bab98 100644 --- a/buildroot-external/Config.in +++ b/buildroot-external/Config.in @@ -38,6 +38,22 @@ config BR2_RETROSTONE_VERSION release tag (v0.2.0 -> 0.2.0; scripts/ci/build-board.sh). Development builds get "-dev" appended. +config BR2_RETROSTONE_UPDATE_REPO + string "Update repository (GitHub owner/name)" + help + The GitHub repository whose releases Settings > System update + looks in (docs/updates.md). Empty: PaddleStroke/RetroStoneOS. + A fork that publishes its own update packages sets it, with + its own signing key (BR2_RETROSTONE_UPDATE_PUBKEY). + +config BR2_RETROSTONE_UPDATE_PUBKEY + string "Update signing public key" + help + The signify public key the system updater trusts, installed as + /usr/share/rsos/update.pub. Empty: frontend/assets/update.pub + (the RetroStoneOS release key). For a fork's own key, e.g. + $(BR2_EXTERNAL_RETROSTONE_PATH)/../frontend/assets/update-fork.pub. + choice prompt "Shell on the debug UART" default BR2_RETROSTONE_UART_SHELL_OPEN diff --git a/buildroot-external/board/common/rootfs-overlay/etc/init.d/rcS b/buildroot-external/board/common/rootfs-overlay/etc/init.d/rcS index 038f704..bf11d60 100755 --- a/buildroot-external/board/common/rootfs-overlay/etc/init.d/rcS +++ b/buildroot-external/board/common/rootfs-overlay/etc/init.d/rcS @@ -41,6 +41,19 @@ read -r CMDLINE < /proc/cmdline [ -r /usr/libexec/rsos/board.sh ] && . /usr/libexec/rsos/board.sh # for data-partition (it then skips reading the board files again) export RSOS_DISK RSOS_BOARD_DATA_PARTITION RSOS_BOARD_FIRSTBOOT_TRACE_KIB +# /dev/rsos-disk: the disk with U-Boot's environment, for /etc/fw_env.config: +# the one U-Boot started from (rsos.envdev=mmcblk from boot.scr), else the +# system disk. They differ on a RetroStone2 Pro started by its starter card +# (the system on the eMMC, U-Boot and its environment on the microSD). +_rsos_env=$RSOS_DISK +case " $CMDLINE " in +*" rsos.envdev=mmcblk"[0-9]" "*) + _rsos_env=${CMDLINE##*rsos.envdev=} + _rsos_env=/dev/${_rsos_env%% *} + ;; +esac +ln -sf "$_rsos_env" /dev/rsos-disk +unset _rsos_env iost; IO_START=$IO # --- Prefetch (boot round 3): the frontend and the libraries it maps (and @@ -116,6 +129,9 @@ ts "governor" # Only on a board with that PMIC (board.ini: boot_reason = axp209). [ "$RSOS_BOARD_BOOT_REASON" = axp209 ] && [ -x /usr/bin/rsos-bootreason ] && /usr/bin/rsos-bootreason -w /run/rsos/bootreason +# The AXP209's data registers as they are now, before the boot logger clears +# them (the RetroStone2 Pro's eMMC boot breadcrumbs, rsos-storage's report) +[ -x /usr/libexec/rsos/axpstamp ] && /usr/libexec/rsos/axpstamp raw > /run/rsos/axp-data 2> /dev/null ts "bootreason" # --- User data partition (exFAT) on /data. Created on the first boot. diff --git a/buildroot-external/board/common/rootfs-overlay/usr/bin/rsos-boot-ok b/buildroot-external/board/common/rootfs-overlay/usr/bin/rsos-boot-ok index b80d3c9..5b86446 100755 --- a/buildroot-external/board/common/rootfs-overlay/usr/bin/rsos-boot-ok +++ b/buildroot-external/board/common/rootfs-overlay/usr/bin/rsos-boot-ok @@ -146,9 +146,34 @@ refund() { case "$_f" in [1-9]) fw_setenv rsos_fails $((_f - 1)) ;; esac fi [ "$(get rsos_lowvolt)" != trial ] || fw_setenv rsos_lowvolt + # a faster memory trial: try it again at the next start (an orderly + # shutdown is no failure) + [ -x "$AXP" ] && "$AXP" dram-retry 2> /dev/null echo refunded > "$RUN/boot-state" } +# The RetroStone2's faster memory (Settings > Power > Memory speed): U-Boot's +# SPL runs every start at the chosen DRAM clock as a trial (AXP209 REG0D, +# /usr/libexec/rsos/axpstamp). Once the start is stable, a memory test; then +# the trial is confirmed, or marked failed and the console restarts at the +# normal speed (data must not be written with a memory that flips bits). +AXP=/usr/libexec/rsos/axpstamp +dram_trial() { + [ -x "$AXP" ] && [ -x /usr/libexec/rsos/memtest ] || return 0 + set -- $("$AXP" dram-get 2> /dev/null) + [ "$1" = trial ] || return 0 + if /usr/libexec/rsos/memtest 96 2 > "$RUN/memtest.txt" 2>&1; then + "$AXP" dram-ok + msg "memory at $3 MHz: test passed, confirmed" + return 0 + fi + "$AXP" dram-fail + msg "memory at $3 MHz: test FAILED ($(cat "$RUN/memtest.txt")): back to the normal speed, restarting" + sync + reboot + exit 1 +} + case "$1" in now) confirm @@ -193,4 +218,5 @@ if [ -n "$fe" ] && ! kill -0 "$fe" 2> /dev/null; then msg "the frontend restarted, not confirming (its new instance starts over)" exit 1 fi +dram_trial confirm diff --git a/buildroot-external/board/retrostone2/boot.cmd b/buildroot-external/board/retrostone2/boot.cmd index 5c38e4d..a74ece9 100644 --- a/buildroot-external/board/retrostone2/boot.cmd +++ b/buildroot-external/board/retrostone2/boot.cmd @@ -71,12 +71,15 @@ # # Optional settings, also in the U-Boot environment (fw_setenv): # rsos_overlays e.g. "emmc sata": /boot/overlays/.dtbo to apply +# (Settings > Storage writes it through rsos-storage; +# "emmc" is implied when booted from the eMMC) # "cpu-lowvolt" (a test that may freeze the board) is # one-shot: applied with rsos_lowvolt=trial, which # rsos-boot-ok clears when that boot is confirmed (or given # back); a boot that finds "trial" still set skips it and # sets rsos_lowvolt=failed (skipped until # "fw_setenv rsos_lowvolt") +# rsos_model pro|regular, probed at the first start (see below) # rsos_extraargs appended to the kernel command line (e.g. "initcall_debug") # rsos_verbose 1: U-Boot prints on the UART (silent otherwise, see bootcmd) # @@ -92,7 +95,71 @@ bootstage mark boot.scr +# The disk of the system (bootcmd's rsos_sysdev: 0 = microSD, 1 = the Pro's +# eMMC after Settings > Storage > Install to internal storage; the eMMC also +# when U-Boot started from a starter card, a microSD with only the boot +# loader and games). Linux names them the same way (DT aliases: mmcblk0 = +# microSD, mmcblk1 = eMMC). With the system on the eMMC, its overlay is always +# applied: the base DT has mmc2 disabled, and Linux could not find its root +# filesystem without it. U-Boot's environment is on the disk it started from +# (mmc_bootdev): rsos.envdev tells Linux (rcS: /dev/rsos-disk). +l_dev=0 +if test "${rsos_sysdev}" = 1; then + l_dev=1 +elif test "${mmc_bootdev}" = 1; then + l_dev=1 +fi +l_envdev=0 +if test "${mmc_bootdev}" = 1; then + l_envdev=1 +fi + l_save=0 +# The model: the RetroStone2 Pro has the 16 GB eMMC U23 (and the M.2 slot +# J1), the regular RetroStone2 has neither. The first start (rsos_model +# empty) probes U-Boot's mmc 1 (the eMMC) once and saves rsos_model=pro or +# regular. A Pro gets the overlays "emmc" and "sata" (Settings > Storage can +# switch them off), a regular unit loses "emmc" (the menu then hides the +# internal storage). "rsos-storage redetect" (Settings > Storage > Detect the +# model again) clears rsos_model: the next start probes again. +if test -z "${rsos_model}"; then + if test ${l_dev} = 1; then + setenv rsos_model pro + elif mmc dev 1; then + setenv rsos_model pro + else + setenv rsos_model regular + fi + mmc dev ${l_envdev} + l_new= + for l_ov in ${rsos_overlays}; do + if test "${l_ov}" != "emmc" && test "${l_ov}" != "sata"; then + l_new="${l_new} ${l_ov}" + elif test "${l_ov}" = "sata" && test "${rsos_model}" = "regular"; then + l_new="${l_new} ${l_ov}" + fi + done + if test "${rsos_model}" = "pro"; then + l_new="${l_new} emmc sata" + fi + setenv rsos_overlays ${l_new} + echo "rsos: model ${rsos_model}, overlays: ${rsos_overlays}" + l_save=1 +fi + +l_ovs="${rsos_overlays}" +if test ${l_dev} = 1; then + l_hasemmc=0 + for l_ov in ${rsos_overlays}; do + if test "${l_ov}" = "emmc"; then + l_hasemmc=1 + fi + done + if test ${l_hasemmc} = 0; then + l_ovs="emmc ${rsos_overlays}" + fi +fi + # First boot (no saved environment yet): store the defaults, so that # fw_setenv in Linux always finds a valid environment written by this U-Boot # (otherwise it would start from its own built-in default environment, @@ -167,7 +234,7 @@ if test ${l_fallback} = 1; then l_off=1 elif test "${rsos_bad}" = "${l_other}"; then echo "rsos: slot ${rsos_slot} failed too often, slot ${l_other} is marked bad: staying on ${rsos_slot}" - elif test -e mmc 0:${l_opart} /boot/zImage; then + elif test -e mmc ${l_dev}:${l_opart} /boot/zImage; then if test "${rsos_fallback}" != "${l_other}" || test "${rsos_good}" = "${l_other}"; then echo "rsos: slot ${rsos_slot} failed too often, falling back to slot ${l_other}" setenv rsos_fallback ${rsos_slot} @@ -218,8 +285,8 @@ fi # cpu-lowvolt: one boot at a time until a boot with it is confirmed (a # freeze at 1.0 V must not come back after every watchdog reset). l_lowvolt=0 -if test -n "${rsos_overlays}"; then - for l_ov in ${rsos_overlays}; do +if test -n "${l_ovs}"; then + for l_ov in ${l_ovs}; do if test "${l_ov}" = "cpu-lowvolt"; then l_lowvolt=1 fi @@ -271,26 +338,26 @@ fi # tens of ms in total, spread over the initcalls). The release build # (BR2_RETROSTONE_RELEASE, docs/build.md "Release build") removes both words: # post-build.sh deletes " initcall_debug log_buf_len=1M" from this line. -setenv bootargs "console=ttyS0,115200 root=/dev/mmcblk0p${l_part} rootfstype=ext4 rootwait ro quiet panic=10 consoleblank=0 driver_async_probe=dw-apb-uart,musb-sunxi,sun4i-ts,axp20x-i2c initcall_debug log_buf_len=1M rsos.slot=${rsos_slot} rsos.boot=${l_state} ${rsos_extraargs}" +setenv bootargs "console=ttyS0,115200 root=/dev/mmcblk${l_dev}p${l_part} rootfstype=ext4 rootwait ro quiet panic=10 consoleblank=0 driver_async_probe=dw-apb-uart,musb-sunxi,sun4i-ts,axp20x-i2c initcall_debug log_buf_len=1M rsos.slot=${rsos_slot} rsos.boot=${l_state} rsos.envdev=mmcblk${l_envdev} ${rsos_extraargs}" bootstage mark load-zImage -if load mmc 0:${l_part} ${kernel_addr_r} /boot/zImage; then +if load mmc ${l_dev}:${l_part} ${kernel_addr_r} /boot/zImage; then bootstage mark zImage-loaded - if load mmc 0:${l_part} ${fdt_addr_r} /boot/sun7i-a20-retrostone2.dtb; then + if load mmc ${l_dev}:${l_part} ${fdt_addr_r} /boot/sun7i-a20-retrostone2.dtb; then bootstage mark dtb-loaded - if test -n "${rsos_overlays}"; then + if test -n "${l_ovs}"; then fdt addr ${fdt_addr_r} fdt resize 65536 - for l_ov in ${rsos_overlays}; do + for l_ov in ${l_ovs}; do if test "${l_ov}" = "cpu-lowvolt" && test ${l_lowvolt} = 0; then true - elif load mmc 0:${l_part} ${fdtoverlay_addr_r} /boot/overlays/${l_ov}.dtbo; then + elif load mmc ${l_dev}:${l_part} ${fdtoverlay_addr_r} /boot/overlays/${l_ov}.dtbo; then if fdt apply ${fdtoverlay_addr_r}; then echo "rsos: overlay ${l_ov} applied" else # A failed fdt apply leaves the DT in an undefined state echo "rsos: overlay ${l_ov} failed, reloading the base DT" - load mmc 0:${l_part} ${fdt_addr_r} /boot/sun7i-a20-retrostone2.dtb + load mmc ${l_dev}:${l_part} ${fdt_addr_r} /boot/sun7i-a20-retrostone2.dtb fdt addr ${fdt_addr_r} fdt resize 65536 fi @@ -319,7 +386,7 @@ setenv rsos_ok 1 setenv rsos_tries 0 setenv rsos_fails 0 setenv rsos_p -if test ${rsos_rounds} -lt ${l_maxrounds} && test "${rsos_bad}" != "${l_other}" && test -e mmc 0:${l_opart} /boot/zImage; then +if test ${rsos_rounds} -lt ${l_maxrounds} && test "${rsos_bad}" != "${l_other}" && test -e mmc ${l_dev}:${l_opart} /boot/zImage; then echo "rsos: switching to slot ${l_other}" setenv rsos_fallback ${rsos_slot} setenv rsos_slot ${l_other} diff --git a/buildroot-external/board/retrostone2/patches/uboot/0003-sunxi-eMMC-boot-breadcrumbs-in-the-AXP209-data-registers.patch b/buildroot-external/board/retrostone2/patches/uboot/0003-sunxi-eMMC-boot-breadcrumbs-in-the-AXP209-data-registers.patch new file mode 100644 index 0000000..78e77aa --- /dev/null +++ b/buildroot-external/board/retrostone2/patches/uboot/0003-sunxi-eMMC-boot-breadcrumbs-in-the-AXP209-data-registers.patch @@ -0,0 +1,41 @@ +From: RetroStoneOS +Subject: [PATCH] sunxi: eMMC boot breadcrumbs in the AXP209 data registers + +The RetroStone2 Pro starts from its eMMC once RetroStoneOS is installed +there. With no serial console, the SPL and U-Boot proper leave a marker in +the AXP209 data registers REG0E/REG0F when they run from the eMMC, read at +the next start from the microSD (rsos-storage report). +--- +--- a/board/sunxi/board.c 2026-10-20 13:30:49.965329560 -0400 ++++ b/board/sunxi/board.c 2026-10-20 13:30:49.981921451 -0400 +@@ -589,6 +589,18 @@ + defined CONFIG_AXP803_POWER + power_failed = axp_init(); + ++ /* ++ * RetroStone2: started from the eMMC (Settings > Storage > Install to ++ * internal storage): a breadcrumb in the AXP209's last data registers, ++ * which keep it while the battery is in. With no serial console, the ++ * next start from the microSD shows how far it got (rsos-storage's ++ * eMMC report): 5a e1 = this SPL ran, 5a e2 = U-Boot proper ran. ++ */ ++ if (!power_failed && sunxi_get_boot_device() == BOOT_DEVICE_MMC2) { ++ pmic_bus_write(0x0e, 0x5a); ++ pmic_bus_write(0x0f, 0xe1); ++ } ++ + if (IS_ENABLED(CONFIG_AXP_DISABLE_BOOT_ON_POWERON) && !power_failed) { + u8 boot_reason; + +@@ -849,6 +861,11 @@ + env_set("mmc_bootdev", "0"); + } else if (boot == BOOT_DEVICE_MMC2) { + env_set("mmc_bootdev", "1"); ++ /* RetroStone2: the eMMC breadcrumb, U-Boot proper reached */ ++ if (!pmic_bus_init()) { ++ pmic_bus_write(0x0e, 0x5a); ++ pmic_bus_write(0x0f, 0xe2); ++ } + } + + /* Set fdtfile to match the FIT configuration chosen in SPL. */ diff --git a/buildroot-external/board/retrostone2/patches/uboot/0004-sunxi-A20-DRAM-clock-chosen-at-start-with-a-trial.patch b/buildroot-external/board/retrostone2/patches/uboot/0004-sunxi-A20-DRAM-clock-chosen-at-start-with-a-trial.patch new file mode 100644 index 0000000..7a24ddd --- /dev/null +++ b/buildroot-external/board/retrostone2/patches/uboot/0004-sunxi-A20-DRAM-clock-chosen-at-start-with-a-trial.patch @@ -0,0 +1,79 @@ +From: RetroStoneOS +Subject: [PATCH] sunxi: A20 DRAM clock chosen at every start, with a trial + +The RetroStone2 can run its DRAM faster than the Lime defaults (384 MHz): +the SPL reads the wanted level from an AXP209 data register, uses the +matching DDR3-1066F timings, and falls back by itself when Linux did not +confirm the last start at that speed. +--- +--- a/board/sunxi/dram_sun5i_auto.c 2026-10-20 18:12:20.740264918 -0400 ++++ b/board/sunxi/dram_sun5i_auto.c 2026-10-20 18:12:20.792886645 -0400 +@@ -32,7 +32,68 @@ + .dqs_gating_delay = CONFIG_DRAM_DQS_GATING_DELAY, + }; + ++#ifdef CONFIG_MACH_SUN7I ++#include ++ ++/* ++ * RetroStone2: the memory clock is chosen at every start (Settings > Power > ++ * Memory speed in RetroStoneOS), through the AXP209 data register REG0D, ++ * which the PMIC keeps while the battery is in: << 4 | . ++ * Level 1 = 432 MHz, 2 = 480 MHz, each with the DDR3-1066F timings of ++ * dram_timings_sun4i.h for that clock (those of the A20 boards that run ++ * there, Cubietruck, Bananapi). States: A requested, B trial running, C ++ * confirmed by Linux after a stable start and a memory test, F failed. ++ * A and C start the level as a trial (B), so any start that Linux does not ++ * confirm (freeze, crash, power cut) finds B at the next start: the normal ++ * clock and timings are used, and F is stored. Anything else (0 after the ++ * battery was out): the normal clock, untouched. ++ */ ++#define RSOS_DRAM_REG 0x0d ++ ++static const struct { ++ u16 clock; ++ u8 cas; ++ u32 tpr0, tpr1, tpr2, emr2; ++} rsos_dram_levels[3] = { ++ [1] = { 432, 7, 0x2cb16690, 0xa0b0, 0x22e00, 0x8 }, /* DDR3-1066F 7-6-6-17 */ ++ [2] = { 480, 7, 0x32b27790, 0xa0c0, 0x23200, 0x8 }, /* DDR3-1066F 7-7-7-18 */ ++}; ++ ++static void rsos_dram_choose(struct dram_para *para) ++{ ++ u8 v, lvl; ++ ++ if (pmic_bus_read(RSOS_DRAM_REG, &v)) ++ return; ++ lvl = v & 0x0f; ++ if (lvl < 1 || lvl > 2) ++ return; ++ switch (v & 0xf0) { ++ case 0xa0: ++ case 0xc0: ++ if (pmic_bus_write(RSOS_DRAM_REG, 0xb0 | lvl)) ++ return; /* no trial without its marker */ ++ break; ++ case 0xb0: ++ pmic_bus_write(RSOS_DRAM_REG, 0xf0 | lvl); ++ return; ++ default: ++ return; ++ } ++ para->clock = rsos_dram_levels[lvl].clock; ++ para->cas = rsos_dram_levels[lvl].cas; ++ para->tpr0 = rsos_dram_levels[lvl].tpr0; ++ para->tpr1 = rsos_dram_levels[lvl].tpr1; ++ para->tpr2 = rsos_dram_levels[lvl].tpr2; ++ para->emr2 = rsos_dram_levels[lvl].emr2; ++ para->active_windowing = 1; ++} ++#endif ++ + unsigned long sunxi_dram_init(void) + { ++#ifdef CONFIG_MACH_SUN7I ++ rsos_dram_choose(&dram_para); ++#endif + return dramc_init(&dram_para); + } diff --git a/buildroot-external/board/retrostone2/post-build.sh b/buildroot-external/board/retrostone2/post-build.sh index e81f8a1..7945035 100755 --- a/buildroot-external/board/retrostone2/post-build.sh +++ b/buildroot-external/board/retrostone2/post-build.sh @@ -45,6 +45,26 @@ mkdir -p "$TARGET/usr/libexec/rsos" # them to shutdown.txt on the next boot (tools/rsos-axpstamp.c). "$CC" -O2 -s -Wall -Wextra -Werror -o "$TARGET/usr/libexec/rsos/axpstamp" \ "$BOARD_DIR/tools/rsos-axpstamp.c" +# /usr/libexec/rsos/memtest: the quick DRAM check of the faster memory trial +# (Settings > Power > Memory speed; rsos-boot-ok, tools/rsos-memtest.c). +"$CC" -O2 -s -Wall -Wextra -Werror -o "$TARGET/usr/libexec/rsos/memtest" \ + "$BOARD_DIR/tools/rsos-memtest.c" + +# --- The boot loader, for "Install to internal storage" on the Pro +# (rsos-storage install writes it at 8 KiB on the eMMC): the image's own +# U-Boot, built before the root filesystem is finalised. Its sha256 sits next +# to it, so the installer can check its copy and the write. +UBOOT_BIN=$BINARIES_DIR/u-boot-sunxi-with-spl.bin +if [ ! -f "$UBOOT_BIN" ]; then + echo "post-build.sh: $UBOOT_BIN missing (the eMMC installer needs it)" >&2 + exit 1 +fi +# rsos-storage: Settings > Storage on the Pro (eMMC, M.2 SATA, game disks). +# (Windows checkouts lose the executable bit: board/common/post-build.sh.) +chmod 0755 "$TARGET/usr/bin/rsos-storage" +mkdir -p "$TARGET/usr/share/rsos/bootloader" +cp "$UBOOT_BIN" "$TARGET/usr/share/rsos/bootloader/u-boot-sunxi-with-spl.bin" +(cd "$TARGET/usr/share/rsos/bootloader" && sha256sum u-boot-sunxi-with-spl.bin > u-boot-sunxi-with-spl.bin.sha256) # --- WiFi/BT firmware names. # brcmfmac asks for brcm/brcmfmac-sdio..{bin,txt}, diff --git a/buildroot-external/board/retrostone2/rootfs-overlay/etc/fw_env.config b/buildroot-external/board/retrostone2/rootfs-overlay/etc/fw_env.config index d2e22c2..6381e2b 100644 --- a/buildroot-external/board/retrostone2/rootfs-overlay/etc/fw_env.config +++ b/buildroot-external/board/retrostone2/rootfs-overlay/etc/fw_env.config @@ -1,6 +1,9 @@ # U-Boot environment (A/B boot state) for fw_printenv / fw_setenv. # Must match uboot.fragment: redundant, 64 KiB copies at 1 MiB and 1 MiB + 64 KiB -# on the SD card, outside any partition. +# on the system disk, outside any partition. /dev/rsos-disk is the disk the +# system booted from (rcS links it: the microSD, or the Pro's eMMC once +# RetroStoneOS is installed there); U-Boot keeps its environment on the disk +# it started from too. # Device Offset Size -/dev/mmcblk0 0x100000 0x10000 -/dev/mmcblk0 0x110000 0x10000 +/dev/rsos-disk 0x100000 0x10000 +/dev/rsos-disk 0x110000 0x10000 diff --git a/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board-hooks.sh b/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board-hooks.sh index 2414732..548750a 100644 --- a/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board-hooks.sh +++ b/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board-hooks.sh @@ -17,6 +17,11 @@ rsos_board_late_audio() { # rcS, in the background, after the USB storage modules. # SATA ("Pro" units, sata overlay): libata/ahci_sunxi are modules; the # platform device only exists when the overlay enabled the controller. +# Then the game libraries of the other disks (rsos-storage boot: the eMMC +# or the microSD that the system does not run from, the M.2 drive), and the +# first start from the eMMC carries the microSD's overlays over. In the +# background: the SATA link takes a moment to come up. rsos_board_late_storage() { [ -d /sys/bus/platform/devices/1c18000.sata ] && modprobe -q ahci_sunxi + [ -x /usr/bin/rsos-storage ] && /usr/bin/rsos-storage boot < /dev/null > /dev/null 2>&1 & } diff --git a/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board.ini b/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board.ini index 7d28546..7de9c49 100644 --- a/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board.ini +++ b/buildroot-external/board/retrostone2/rootfs-overlay/etc/rsos/board.ini @@ -42,9 +42,11 @@ storage_overlays = emmc sata display_quirks = sun4i-tcon0-clock panel-keep-scanning # --- Init scripts (board/common/rootfs-overlay/usr/libexec/rsos/board.sh) -# The SD card: the root filesystems (A/B) and the data partition (MBR -# entry 1, FAT32 seed turned into exFAT at the first boot) -data_disk = /dev/mmcblk0 +# The system disk: the root filesystems (A/B) and the data partition (MBR +# entry 1, FAT32 seed turned into exFAT at the first boot). auto: the disk of +# root= on the kernel command line, i.e. the microSD (mmcblk0), or the eMMC +# (mmcblk1) once RetroStoneOS is installed there (Settings > Storage). +data_disk = auto data_partition = 1 # data-partition's first-boot trace (64 KiB, KiB from the start of the card): # in the reserved gap between the U-Boot environment (1 MiB + 2 x 64 KiB) diff --git a/buildroot-external/board/retrostone2/rootfs-overlay/usr/bin/rsos-storage b/buildroot-external/board/retrostone2/rootfs-overlay/usr/bin/rsos-storage new file mode 100755 index 0000000..43213ef --- /dev/null +++ b/buildroot-external/board/retrostone2/rootfs-overlay/usr/bin/rsos-storage @@ -0,0 +1,896 @@ +#!/bin/sh +# rsos-storage: the RetroStone2 "Pro" storage (the 16 GB eMMC U23 and the +# M.2 SATA slot J1), for Settings > Storage. +# +# rsos-storage status the state, "key=value" lines (also in +# /run/rsos/storage-status) +# rsos-storage overlay +# rsos_overlays in the U-Boot environment +# (boot.cmd applies them; restart needed) +# rsos-storage redetect clear rsos_model: the next start probes +# the model again (boot.cmd: RetroStone2 or +# RetroStone2 Pro, by its eMMC) +# rsos-storage install [--keep-data] [--games] +# copy RetroStoneOS to the eMMC so that the +# console starts without a microSD +# rsos-storage sd-starter after an install, from the microSD: keep only +# its boot loader and games (a starter card) +# rsos-storage sd-full started by a starter card: make the card a +# full system again (the eMMC's, copied back) +# rsos-storage sd-games booted from the eMMC: turn the inserted +# RetroStoneOS microSD into a games card (its +# boot loader and system partitions go, its +# RETROSTONE partition and games stay) +# rsos-storage format erase that disk: one exFAT RETROSTONE +# partition with the roms/ folders +# rsos-storage refresh mount the game libraries of the disks that +# are not the system disk (/run/rsos/media/ +# ), unmount the ones that left +# rsos-storage boot once per boot, from rcS after the menu is up +# rsos-storage eject +# +# Boot order of the A20 boot ROM: microSD (mmc0), NAND (none), eMMC (mmc2), +# SPI, USB (FEL). A microSD that carries a boot loader (eGON.BT0 at 8 KiB) +# therefore always starts first: a freshly flashed RetroStoneOS card is the +# way back. A games card has none, so the console starts from the eMMC. +# +# The installer writes the same layout as the SD card image (genimage.cfg): +# 8 KiB U-Boot (with SPL) written LAST: until then the eMMC +# cannot start, whatever happens +# 1 MiB U-Boot environment zeroed (U-Boot's defaults, then +# boot.scr saves its own) +# MBR 2 rootfs A, 512 MiB the running slot, verified +# MBR 3 rootfs B, 512 MiB empty until the first update +# MBR 1 data, exFAT RETROSTONE to the end of the eMMC: settings, +# saves, BIOS files, themes (and the +# games with --games) +# --keep-data (the eMMC already holds RetroStoneOS): only the boot loader and +# the system are rewritten, its data partition is left as it is. +# The microSD stays untouched and bootable, so a power cut at any point +# leaves a console that starts from the card; the next install starts over. +# +# Progress, for the menu: " " in /run/rsos/storage-progress +# (steps: prepare system verify data games bootloader done; "error " +# when it stops: notsd noemmc toosmall nospace busy layout write verify +# format mount copy bootloader nosd notemmc notrsos), also on stdout. Every +# step goes to the kernel log too ("rsos-storage: ..."), hence to +# RETROSTONE/rsos/logs/boot/dmesg.txt. + +PATH=/usr/bin:/usr/sbin:/bin:/sbin +export PATH + +[ -r /usr/libexec/rsos/board.sh ] && . /usr/libexec/rsos/board.sh +BOOT_DISK=${RSOS_STORAGE_BOOT_DISK:-${RSOS_DISK:-/dev/mmcblk0}} +# the disk U-Boot started from (its environment; rcS links it): the microSD +# also when the system runs from the eMMC through a starter card +ENV_DISK=${RSOS_STORAGE_ENV_DISK:-$(readlink /dev/rsos-disk 2> /dev/null)} +ENV_DISK=${ENV_DISK:-$BOOT_DISK} +SD=${RSOS_STORAGE_SD:-/dev/mmcblk0} +EMMC=${RSOS_STORAGE_EMMC:-/dev/mmcblk1} +RUN=${RSOS_RUN:-/run/rsos} +DATA=${RSOS_DATA_MNT:-/data} +SHARE=${RSOS_SHARE:-/usr/share/rsos} +MEDIA=$RUN/media +STATUS=$RUN/storage-status +PROGRESS=$RUN/storage-progress +LIBS=$RUN/libraries +LOCK=$RUN/storage.lock +KMSG=${RSOS_KMSG:-/dev/kmsg} +BOOTLOADER=$SHARE/bootloader/u-boot-sunxi-with-spl.bin +LABEL=RETROSTONE + +# The image layout (genimage.cfg), in 512-byte sectors +ROOT_A_START=8192 +ROOT_B_START=1056768 +ROOT_SIZE=1048576 +DATA_START=2105344 +# a data partition of at least 1 GiB +MIN_SECTORS=$((DATA_START + 2097152)) + +mkdir -p "$RUN" "$MEDIA" 2> /dev/null + +msg() { + echo "rsos-storage: $*" >&2 + { echo "<5>rsos-storage: $*" > "$KMSG"; } 2> /dev/null + # kept on the card too (the boot logger copies dmesg only now and then) + if [ -d "$DATA/rsos/logs" ]; then + read -r _up _ < /proc/uptime + echo "$(date '+%F %T' 2> /dev/null) [$_up] $*" >> "$DATA/rsos/logs/storage.log" 2> /dev/null + fi +} + +progress() { # + echo "$1 $2" > "$PROGRESS.tmp" && mv -f "$PROGRESS.tmp" "$PROGRESS" + echo "progress $1 $2" +} + +fail() { # + echo "error $1" > "$PROGRESS.tmp" && mv -f "$PROGRESS.tmp" "$PROGRESS" + msg "failed ($1): $2" + echo "error $1" + exit 1 +} + +# --------------------------------------------------------------- disks +part() { # : /dev/mmcblk1p2, /dev/sda1 + case "$1" in + *[0-9]) echo "$1p$2" ;; + *) echo "$1$2" ;; + esac +} + +sectors() { # : its size in 512-byte sectors (0: absent) + _s=0 + [ -r "/sys/class/block/${1##*/}/size" ] && read -r _s < "/sys/class/block/${1##*/}/size" + echo "${_s:-0}" +} + +present() { # + [ -b "$1" ] && [ "$(sectors "$1")" -gt 0 ] +} + +# The M.2 SATA disk: the first sd* disk behind the A20's AHCI controller (USB +# sticks are sd* disks too, behind a USB host). +m2_disk() { + for _b in /sys/block/sd*; do + [ -e "$_b" ] || continue + case "$(readlink -f "$_b")" in + */1c18000.sata/*|*/ata[0-9]*) echo "/dev/${_b##*/}"; return 0 ;; + esac + done + return 1 +} + +has_bootloader() { # : an Allwinner boot header at 8 KiB (the boot ROM starts it) + [ "$(dd if="$1" bs=1 skip=8196 count=8 2> /dev/null)" = "eGON.BT0" ] +} + +# RetroStoneOS' layout: the boot loader, and the rootfs A entry where the +# image puts it. +is_rsos() { # + has_bootloader "$1" || return 1 + sfdisk --dump "$1" 2> /dev/null | grep -q "^$(part "$1" 2) : start= *$ROOT_A_START, size= *$ROOT_SIZE," +} + +fstype() { # + blkid -o value -s TYPE "$1" 2> /dev/null +} + +# The disk's game partition: entry 1 (RetroStoneOS, a games card made here, +# or most cards formatted on a PC) +data_part() { # + _p=$(part "$1" 1) + [ -b "$_p" ] && echo "$_p" +} + +# What a disk holds, for the menu: +# rsos RetroStoneOS (boots) bootable another system that boots +# games a RETROSTONE-style game disk other something else +# starter the boot loader and a game partition, no system (a starter card) +# empty no partition table +disk_state() { # + if is_rsos "$1"; then + echo rsos + elif has_bootloader "$1"; then + case "$(fstype "$(part "$1" 1)" 2> /dev/null)" in + exfat|vfat) [ ! -b "$(part "$1" 2)" ] && echo starter || echo bootable ;; + *) echo bootable ;; + esac + elif ! sfdisk --dump "$1" > /dev/null 2>&1; then + echo empty + else + _p=$(data_part "$1") + case "$(fstype "$_p" 2> /dev/null)" in + exfat|vfat) echo games ;; + *) echo other ;; + esac + fi +} + +boot_name() { + case "$BOOT_DISK" in + "$SD") echo sd ;; + "$EMMC") echo emmc ;; + *) echo other ;; + esac +} + +overlays() { + fw_printenv -n rsos_overlays 2> /dev/null +} + +model() { # pro, regular ("" before boot.cmd probed it) + fw_printenv -n rsos_model 2> /dev/null +} + +wait_parts() { # ... + _d=$1 + shift + _i=0 + while [ $_i -lt 30 ]; do + _ok=1 + for _n; do + [ -b "$(part "$_d" "$_n")" ] || _ok=0 + done + [ $_ok = 1 ] && return 0 + usleep 100000 2> /dev/null || sleep 1 + _i=$((_i + 1)) + done + return 1 +} + +reread() { # + blockdev --rereadpt "$1" 2> /dev/null || partx --update "$1" 2> /dev/null +} + +mounted_from() { # : mount points of its partitions + awk -v d="$1" 'index($1, d) == 1 { print $2 }' /proc/mounts +} + +drop_caches() { + sync + echo 3 > /proc/sys/vm/drop_caches 2> /dev/null +} + +# ------------------------------------------------------------ libraries +# A library: 's game partition mounted on $MEDIA/, listed in +# $LIBS (" ") when it has a roms/ folder. +lib_mounted() { # + grep -q " $MEDIA/$1 " /proc/mounts +} + +lib_umount() { # + if lib_mounted "$1"; then + sync + umount "$MEDIA/$1" 2> /dev/null || umount -l "$MEDIA/$1" 2> /dev/null + msg "library $1 unmounted" + fi + rmdir "$MEDIA/$1" 2> /dev/null + return 0 +} + +lib_mount() { # + lib_mounted "$1" && return 0 + _t=$(fstype "$2") + mkdir -p "$MEDIA/$1" + case "$_t" in + exfat) + # VolumeDirty (unplugged or powered off while mounted): check first + if [ -n "$(dd if="$2" bs=1 skip=106 count=1 2> /dev/null)" ]; then + msg "library $1: $2 was not unmounted cleanly, checking it" + fsck.exfat -p "$2" > /dev/null 2>&1 + fi + mount -t exfat -o noatime "$2" "$MEDIA/$1" 2> /dev/null || + { fsck.exfat -p "$2" > /dev/null 2>&1 && mount -t exfat -o noatime "$2" "$MEDIA/$1" 2> /dev/null; } + ;; + vfat) + mount -t vfat -o noatime,utf8,shortname=mixed "$2" "$MEDIA/$1" 2> /dev/null + ;; + *) + false + ;; + esac || { rmdir "$MEDIA/$1" 2> /dev/null; return 1; } + msg "library $1: $2 ($_t) mounted on $MEDIA/$1" +} + +write_libs() { + : > "$LIBS.tmp" + for _n in emmc sd m2; do + lib_mounted "$_n" && [ -d "$MEDIA/$_n/roms" ] && echo "$_n $MEDIA/$_n" >> "$LIBS.tmp" + done + if ! cmp -s "$LIBS.tmp" "$LIBS" 2> /dev/null; then + mv -f "$LIBS.tmp" "$LIBS" + else + rm -f "$LIBS.tmp" + fi +} + +refresh() { + for _n in emmc sd m2; do + case $_n in + emmc) _d=$EMMC ;; + sd) _d=$SD ;; + m2) _d=$(m2_disk) || _d= ;; + esac + # the disk left (card pulled): its mount goes too + if [ -z "$_d" ] || [ "$_d" = "$BOOT_DISK" ] || ! present "$_d"; then + lib_umount $_n + continue + fi + _p=$(data_part "$_d") || { lib_umount $_n; continue; } + lib_mount $_n "$_p" + done + write_libs + status > /dev/null +} + +# --------------------------------------------------------------- status +status() { + { + echo "boot=$(boot_name)" + echo "model=$(model)" + # started by a starter card: the system on the eMMC, U-Boot on the SD + [ "$ENV_DISK" != "$BOOT_DISK" ] && [ "$ENV_DISK" = "$SD" ] && echo "starter=1" + [ -e /sys/bus/platform/devices/1c11000.mmc ] && echo "emmc_ctrl=enabled" + if present "$EMMC"; then + echo "emmc=present" + echo "emmc_mb=$(($(sectors "$EMMC") / 2048))" + [ "$EMMC" = "$BOOT_DISK" ] || echo "emmc_state=$(disk_state "$EMMC")" + else + echo "emmc=absent" + fi + if [ "$SD" != "$BOOT_DISK" ]; then + if present "$SD"; then + echo "sd=present" + echo "sd_mb=$(($(sectors "$SD") / 2048))" + echo "sd_state=$(disk_state "$SD")" + else + echo "sd=absent" + fi + fi + if [ -d /sys/bus/platform/devices/1c18000.sata ]; then + echo "sata=enabled" + else + echo "sata=disabled" + fi + if _m=$(m2_disk); then + echo "m2=present" + echo "m2_mb=$(($(sectors "$_m") / 2048))" + echo "m2_state=$(disk_state "$_m")" + else + echo "m2=absent" + fi + echo "overlays=$(overlays)" + for _n in emmc sd m2; do + if lib_mounted $_n && [ -d "$MEDIA/$_n/roms" ]; then + echo "lib_$_n=$MEDIA/$_n" + fi + done + } > "$STATUS.tmp" + # (not "{ ... } && mv": the group's status is its last test's, false + # when no library is mounted) + mv -f "$STATUS.tmp" "$STATUS" + cat "$STATUS" +} + +# ------------------------------------------------------------- redetect +redetect() { + fw_setenv rsos_model || exit 1 + msg "rsos_model cleared: the model is probed again at the next start" + status > /dev/null +} + +# -------------------------------------------------------------- overlay +overlay() { # + case "$1" in emmc|sata) ;; *) echo "rsos-storage: unknown overlay $1" >&2; exit 2 ;; esac + _new= + for _o in $(overlays); do + [ "$_o" = "$1" ] || _new="$_new $_o" + done + [ "$2" = on ] && _new="$_new $1" + _new=${_new# } + if [ -n "$_new" ]; then + fw_setenv rsos_overlays "$_new" || exit 1 + else + fw_setenv rsos_overlays || exit 1 + fi + msg "rsos_overlays=\"$_new\" (applies at the next start)" + status > /dev/null +} + +# ------------------------------------------------------------- installer +make_layout() { # + _sys=$(cat "$SHARE/rom-folders" 2> /dev/null) + [ -n "$_sys" ] || _sys="arcade gb gba gbc gamegear mastersystem megadrive n64 neogeo nes psx snes" + for _s in $_sys; do + mkdir -p "$1/roms/$_s" + done +} + +copy_progress() { # : cp -R with progress (du of the copy) + _total=$(du -sk "$1" 2> /dev/null | cut -f1) + [ -n "$_total" ] && [ "$_total" -gt 0 ] || _total=1 + cp -R "$1/." "$2/" 2> "$RUN/storage-cp.err" & + _pid=$! + while kill -0 $_pid 2> /dev/null; do + sleep 2 + _done=$(du -sk "$2" 2> /dev/null | cut -f1) + _pct=$(($3 + (${_done:-0} * ($4 - $3)) / _total)) + [ $_pct -gt $4 ] && _pct=$4 + progress $_pct "$5" + done + wait $_pid +} + +install() { + _keep=0 + _games=0 + for _a; do + case "$_a" in + --keep-data) _keep=1 ;; + --games) _games=1 ;; + esac + done + progress 0 prepare + msg "install to $EMMC (keep-data=$_keep games=$_games), from $BOOT_DISK" + [ "$BOOT_DISK" = "$SD" ] || fail notsd "the system does not run from the microSD" + present "$EMMC" || fail noemmc "no eMMC ($EMMC): Settings > Storage > Internal eMMC, then restart" + [ "$(sectors "$EMMC")" -ge $MIN_SECTORS ] || fail toosmall "$EMMC is too small" + [ -f "$BOOTLOADER" ] && (cd "${BOOTLOADER%/*}" && sha256sum -c -s "${BOOTLOADER##*/}.sha256") || + fail bootloader "$BOOTLOADER missing or damaged" + + # the running slot: root=/dev/mmcblk0p2 or p3 + read -r _cl < "${RSOS_CMDLINE:-/proc/cmdline}" + _root=${_cl##*root=} + _root=${_root%% *} + case "$_root" in + "$(part "$SD" 2)"|"$(part "$SD" 3)") ;; + *) fail notsd "root=$_root is not a slot of $SD" ;; + esac + [ "$(sectors "$_root")" -eq $ROOT_SIZE ] || fail layout "$_root is not $ROOT_SIZE sectors" + + if [ $_keep = 1 ]; then + is_rsos "$EMMC" || fail layout "--keep-data: $EMMC does not hold RetroStoneOS" + fi + + # nothing of the eMMC may stay mounted (its library) + lib_umount emmc + write_libs + [ -z "$(mounted_from "$EMMC")" ] || fail busy "$(mounted_from "$EMMC" | tr '\n' ' ')is mounted" + + # room for the data (the games with --games) + if [ $_keep = 0 ]; then + _room=$(( ($(sectors "$EMMC") - DATA_START) / 2 * 95 / 100 )) + _need=0 + for _d in rsos bios saves states screenshots themes; do + [ -d "$DATA/$_d" ] && _need=$((_need + $(du -sk "$DATA/$_d" | cut -f1))) + done + [ $_games = 1 ] && _need=$((_need + $(du -sk "$DATA/roms" | cut -f1))) + msg "data to copy: $_need KiB, room: $_room KiB" + [ $_need -lt $_room ] || fail nospace "$_need KiB to copy, $_room KiB on the eMMC" + fi + + # 1. The eMMC can no longer start (boot loader, environment, trace area + # zeroed) before anything else changes on it. + dd if=/dev/zero of="$EMMC" bs=1024 seek=8 count=4088 conv=fsync 2> /dev/null || + fail write "cannot write $EMMC" + if [ $_keep = 0 ]; then + # the last MiB (data-partition's conversion marker) too + _last=$(( $(sectors "$EMMC") / 2048 - 1 )) + dd if=/dev/zero of="$EMMC" bs=1M seek=$_last count=1 conv=fsync 2> /dev/null + _id= + read -r _u < /proc/sys/kernel/random/uuid + _id=0x${_u%%-*} + printf 'label: dos\nlabel-id: %s\nunit: sectors\n\nstart=%s, type=7\nstart=%s, size=%s, type=83, bootable\nstart=%s, size=%s, type=83\n' \ + "$_id" $DATA_START $ROOT_A_START $ROOT_SIZE $ROOT_B_START $ROOT_SIZE | + sfdisk --quiet --no-reread --wipe always --wipe-partitions always "$EMMC" > /dev/null 2>&1 || + fail write "cannot write the partition table of $EMMC" + msg "partition table written (disk id $_id)" + fi + reread "$EMMC" + wait_parts "$EMMC" 1 2 3 || fail layout "the partitions of $EMMC did not appear" + progress 3 prepare + + # 2. The system (the running slot, read-only: a consistent copy) + _dst=$(part "$EMMC" 2) + _chunks=$((ROOT_SIZE / 32768)) # 16 MiB each + _i=0 + while [ $_i -lt $_chunks ]; do + dd if="$_root" of="$_dst" bs=1M count=16 skip=$((_i * 16)) seek=$((_i * 16)) conv=fsync 2> /dev/null || + fail write "cannot write $_dst (chunk $_i)" + _i=$((_i + 1)) + progress $((3 + _i * 47 / _chunks)) system + done + msg "system copied from $_root to $_dst" + # slot B: empty (no file system signature left from before) + dd if=/dev/zero of="$(part "$EMMC" 3)" bs=1M count=4 conv=fsync 2> /dev/null + + # 3. Read it back from the eMMC itself (not the page cache) + drop_caches + _i=0 + while [ $_i -lt $_chunks ]; do + _a=$(dd if="$_root" bs=1M count=16 skip=$((_i * 16)) 2> /dev/null | sha256sum) + _b=$(dd if="$_dst" bs=1M count=16 skip=$((_i * 16)) 2> /dev/null | sha256sum) + [ "$_a" = "$_b" ] || fail verify "$_dst differs from $_root (chunk $_i)" + _i=$((_i + 1)) + progress $((50 + _i * 15 / _chunks)) verify + done + msg "system verified" + + # 4. The data partition + _mnt=$RUN/install-mnt + mkdir -p "$_mnt" + _ov= + for _o in $(overlays); do + [ "$_o" = emmc ] || _ov="$_ov $_o" + done + _ov=${_ov# } + if [ $_keep = 0 ]; then + progress 66 data + mkfs.exfat -L "$LABEL" "$(part "$EMMC" 1)" > /dev/null 2>&1 || fail format "mkfs.exfat failed" + mount -t exfat -o noatime "$(part "$EMMC" 1)" "$_mnt" || fail mount "cannot mount the new data partition" + make_layout "$_mnt" + mkdir -p "$_mnt/bios" "$_mnt/saves" "$_mnt/states" "$_mnt/screenshots" "$_mnt/themes" "$_mnt/rsos" + [ -f "$DATA/README.txt" ] && cp "$DATA/README.txt" "$_mnt/README.txt" + # settings, game database, controller set-up...: not the caches, + # the logs, the conversion stamp of the microSD, and (without the + # games) not the homebrew marker, so the eMMC gets its own copy + for _e in "$DATA"/rsos/* "$DATA"/rsos/.[!.]*; do + [ -e "$_e" ] || continue + case "${_e##*/}" in + cache|logs|.conversion-done) continue ;; + homebrew-seeded) [ $_games = 1 ] || continue ;; + esac + cp -R "$_e" "$_mnt/rsos/" 2>> "$RUN/storage-cp.err" || fail copy "cannot copy $_e" + done + for _d in bios saves states screenshots themes; do + [ -d "$DATA/$_d" ] || continue + cp -R "$DATA/$_d/." "$_mnt/$_d/" 2>> "$RUN/storage-cp.err" || fail copy "cannot copy $DATA/$_d" + done + progress 70 data + if [ $_games = 1 ]; then + copy_progress "$DATA/roms" "$_mnt/roms" 70 95 games || fail copy "cannot copy the games" + msg "games copied" + fi + else + mount -t exfat -o noatime "$(part "$EMMC" 1)" "$_mnt" 2> /dev/null || + fail mount "cannot mount the data partition of $EMMC" + mkdir -p "$_mnt/rsos" + fi + # the overlays of this unit (the M.2 SATA drive), set by "rsos-storage + # boot" at the first start from the eMMC (its environment is new) + if [ -n "$_ov" ]; then + echo "$_ov" > "$_mnt/rsos/pending-overlays" + else + rm -f "$_mnt/rsos/pending-overlays" + fi + sync + umount "$_mnt" || fail mount "cannot unmount the data partition" + rmdir "$_mnt" 2> /dev/null + progress 96 bootloader + + # 5. Last: the boot loader. From now on the eMMC starts (when no microSD + # with a boot loader is in the slot). + dd if="$BOOTLOADER" of="$EMMC" bs=1024 seek=8 conv=fsync 2> /dev/null || fail bootloader "cannot write the boot loader" + drop_caches + _size=$(wc -c < "$BOOTLOADER") + dd if="$EMMC" bs=1024 skip=8 count=$(( (_size + 1023) / 1024 )) 2> /dev/null | head -c "$_size" | + cmp -s - "$BOOTLOADER" || fail bootloader "the boot loader on $EMMC does not read back" + sync + msg "install finished: the console can start from $EMMC" + progress 100 done + refresh > /dev/null +} + +# ------------------------------------------------------ games card / format +sd_games() { + progress 0 prepare + [ "$BOOT_DISK" = "$EMMC" ] || fail notemmc "the system does not run from the eMMC" + present "$SD" || fail nosd "no microSD" + not_env_disk "$SD" + is_rsos "$SD" || fail notrsos "the microSD does not hold RetroStoneOS" + lib_umount sd + write_libs + [ -z "$(mounted_from "$SD")" ] || fail busy "the microSD is in use" + # boot loader, environment, trace: gone (the boot ROM skips the card) + dd if=/dev/zero of="$SD" bs=1024 seek=8 count=4088 conv=fsync 2> /dev/null || fail write "cannot write $SD" + progress 40 prepare + # the two system partitions: their file systems, then their entries + dd if=/dev/zero of="$(part "$SD" 2)" bs=1M count=4 conv=fsync 2> /dev/null + dd if=/dev/zero of="$(part "$SD" 3)" bs=1M count=4 conv=fsync 2> /dev/null + sfdisk --quiet --no-reread --delete "$SD" 2 3 > /dev/null 2>&1 || fail write "cannot change the partition table of $SD" + reread "$SD" + sync + msg "the microSD is a games card now (its RETROSTONE partition is kept)" + progress 100 done + refresh > /dev/null +} + +# The starter card (the Pro whose boot ROM does not start the eMMC, see +# docs/pro-storage.md): the running microSD keeps its boot loader and its +# RETROSTONE partition (games), its two system partitions leave the MBR. At +# the next start its U-Boot finds no boot.scr on the card and starts the +# system on the eMMC (bootcmd), its environment stays on the card. Run right +# after an install, from the microSD: the MBR is rewritten (--no-reread: the +# running system keeps its partitions until the restart), nothing else on the +# card changes, and the boot state is reset for the eMMC's slot A. +# A freshly flashed card brings the full microSD system back. +sd_starter() { + progress 0 prepare + [ "$BOOT_DISK" = "$SD" ] || fail notsd "the system does not run from the microSD" + is_rsos "$SD" || fail notrsos "the microSD does not hold RetroStoneOS" + present "$EMMC" && is_rsos "$EMMC" || fail noemmc "RetroStoneOS is not installed on the eMMC" + _size=$(wc -c < "$BOOTLOADER") + dd if="$EMMC" bs=1024 skip=8 count=$(( (_size + 1023) / 1024 )) 2> /dev/null | head -c "$_size" | + cmp -s - "$BOOTLOADER" || fail notrsos "the eMMC's system is not this one: install again first" + # the eMMC holds one system, in slot A (the installer's) + printf 'rsos_slot a\nrsos_ok 1\nrsos_tries 0\nrsos_fails 0\nrsos_rounds 0\nrsos_bad\nrsos_fallback\n' > "$RUN/starter-env" + fw_setenv -s "$RUN/starter-env" || fail write "cannot write the boot state" + rm -f "$RUN/starter-env" + progress 50 prepare + sync + sfdisk --quiet --no-reread --delete "$SD" 2 3 > /dev/null 2>&1 || fail write "cannot change the partition table of $SD" + sync + msg "the microSD is a starter card now: at the next start its boot loader starts the system on the eMMC" + progress 100 done +} + +# Back from a starter card: the running system (on the eMMC) is copied to the +# card's slot A again, its two system entries go back into its MBR (sd-starter +# only removed them), the settings, saves, BIOS files and themes are copied +# back to the card's RETROSTONE partition, and the boot state is reset for +# slot A: at the next start the card's U-Boot finds its boot.scr again and +# the console runs from the card. The eMMC keeps RetroStoneOS (and becomes a +# game disk again). The card's boot loader is the one already there. +sd_full() { + progress 0 prepare + [ "$BOOT_DISK" = "$EMMC" ] && [ "$ENV_DISK" = "$SD" ] || fail notstarter "not started by a starter card" + [ "$(disk_state "$SD")" = starter ] || fail notstarter "the microSD is not a starter card" + sfdisk --dump "$SD" 2> /dev/null | grep -q "^$(part "$SD" 1) : start= *$DATA_START," || + fail layout "the microSD's RETROSTONE partition is not where the image puts it" + read -r _cl < "${RSOS_CMDLINE:-/proc/cmdline}" + _root=${_cl##*root=} + _root=${_root%% *} + case "$_root" in + "$(part "$EMMC" 2)"|"$(part "$EMMC" 3)") ;; + *) fail notstarter "root=$_root is not a slot of $EMMC" ;; + esac + [ "$(sectors "$_root")" -eq $ROOT_SIZE ] || fail layout "$_root is not $ROOT_SIZE sectors" + + # 1. The two system entries back (the card's RETROSTONE partition stays + # mounted: no re-read, the kernel is told about the two new ones) + printf 'start=%s, size=%s, type=83, bootable\n' $ROOT_A_START $ROOT_SIZE | + sfdisk --quiet --no-reread -N 2 "$SD" > /dev/null 2>&1 || fail write "cannot write the partition table of $SD" + printf 'start=%s, size=%s, type=83\n' $ROOT_B_START $ROOT_SIZE | + sfdisk --quiet --no-reread -N 3 "$SD" > /dev/null 2>&1 || fail write "cannot write the partition table of $SD" + partx --add --nr 2:3 "$SD" 2> /dev/null || partx --update "$SD" 2> /dev/null + wait_parts "$SD" 2 3 || fail layout "the system partitions of $SD did not appear" + msg "the microSD's system partitions are back in its partition table" + progress 3 system + + # 2. The running system to the card's slot A, then read back + _dst=$(part "$SD" 2) + _chunks=$((ROOT_SIZE / 32768)) + _i=0 + while [ $_i -lt $_chunks ]; do + dd if="$_root" of="$_dst" bs=1M count=16 skip=$((_i * 16)) seek=$((_i * 16)) conv=fsync 2> /dev/null || + fail write "cannot write $_dst (chunk $_i)" + _i=$((_i + 1)) + progress $((3 + _i * 47 / _chunks)) system + done + dd if=/dev/zero of="$(part "$SD" 3)" bs=1M count=4 conv=fsync 2> /dev/null + drop_caches + _i=0 + while [ $_i -lt $_chunks ]; do + _a=$(dd if="$_root" bs=1M count=16 skip=$((_i * 16)) 2> /dev/null | sha256sum) + _b=$(dd if="$_dst" bs=1M count=16 skip=$((_i * 16)) 2> /dev/null | sha256sum) + [ "$_a" = "$_b" ] || fail verify "$_dst differs from $_root (chunk $_i)" + _i=$((_i + 1)) + progress $((50 + _i * 30 / _chunks)) verify + done + msg "system copied from $_root to $_dst and verified" + + # 3. Settings, saves, BIOS files, themes: the eMMC's (in use) to the card + progress 82 data + _m=$MEDIA/sd + lib_mounted sd || lib_mount sd "$(part "$SD" 1)" || fail mount "cannot mount the microSD's RETROSTONE partition" + mkdir -p "$_m/rsos" + for _e in "$DATA"/rsos/* "$DATA"/rsos/.[!.]*; do + [ -e "$_e" ] || continue + case "${_e##*/}" in + cache|logs|.conversion-done|pending-overlays) continue ;; + esac + cp -R "$_e" "$_m/rsos/" 2>> "$RUN/storage-cp.err" || fail copy "cannot copy $_e" + done + for _d in bios saves states screenshots themes; do + [ -d "$DATA/$_d" ] || continue + mkdir -p "$_m/$_d" + cp -R "$DATA/$_d/." "$_m/$_d/" 2>> "$RUN/storage-cp.err" || fail copy "cannot copy $DATA/$_d" + done + # the card's scan caches knew another system's lists + rm -rf "$_m/rsos/cache" + sync + + # 4. The card's boot state: its own slot A + printf 'rsos_slot a\nrsos_ok 1\nrsos_tries 0\nrsos_fails 0\nrsos_rounds 0\nrsos_bad\nrsos_fallback\n' > "$RUN/full-env" + fw_setenv -s "$RUN/full-env" || fail write "cannot write the boot state" + rm -f "$RUN/full-env" + sync + msg "the microSD holds a full system again: at the next start the console runs from it" + progress 100 done +} + +# Never erase or strip the card the console starts from (its U-Boot) +not_env_disk() { # + [ "$1" != "$ENV_DISK" ] || fail busy "$1 holds the boot loader the console starts from" +} + +format_disk() { # + progress 0 prepare + case "$1" in + sd) _d=$SD ;; + m2) _d=$(m2_disk) || fail nosd "no M.2 drive" ;; + *) echo "rsos-storage: format sd|m2" >&2; exit 2 ;; + esac + [ "$_d" != "$BOOT_DISK" ] || fail busy "$_d is the system disk" + not_env_disk "$_d" + present "$_d" || fail nosd "no $_d" + lib_umount "$1" + write_libs + [ -z "$(mounted_from "$_d")" ] || fail busy "$_d is in use" + dd if=/dev/zero of="$_d" bs=1M count=4 conv=fsync 2> /dev/null || fail write "cannot write $_d" + printf 'label: dos\nunit: sectors\n\nstart=2048, type=7\n' | + sfdisk --quiet --no-reread --wipe always --wipe-partitions always "$_d" > /dev/null 2>&1 || + fail write "cannot write the partition table of $_d" + reread "$_d" + wait_parts "$_d" 1 || fail layout "the partition of $_d did not appear" + progress 30 data + mkfs.exfat -L "$LABEL" "$(part "$_d" 1)" > /dev/null 2>&1 || fail format "mkfs.exfat failed" + lib_mount "$1" "$(part "$_d" 1)" || fail mount "cannot mount the new partition" + make_layout "$MEDIA/$1" + [ -f "$DATA/README.txt" ] && cp "$DATA/README.txt" "$MEDIA/$1/README.txt" + sync + msg "$_d formatted as a game disk" + progress 100 done + refresh > /dev/null +} + +# --------------------------------------------------------------- report +# rsos/logs/emmc-report.txt on the system disk, at every start while the eMMC +# is visible: what is on the eMMC, whether U-Boot ever ran from it (it saves +# its environment there at its first start), the eMMC registers that decide +# how it answers at power-on, and the logs a start from it left on its data +# partition (copied to rsos/logs/emmc/). Read-only on the eMMC. +EXT_CSD_FIELDS="162:RST_n_FUNCTION 177:BOOT_BUS_CONDITIONS 178:BOOT_CONFIG_PROT 179:PARTITION_CONFIG 183:BUS_WIDTH 185:HS_TIMING 192:EXT_CSD_REV 226:BOOT_SIZE_MULT" + +ext_csd_byte() { # + echo "$1" | cut -c$(($2 * 2 + 1))-$(($2 * 2 + 2)) +} + +report() { + [ "$EMMC" != "$BOOT_DISK" ] && present "$EMMC" || return 0 + [ -d "$DATA/rsos/logs" ] || return 0 + _r=$DATA/rsos/logs/emmc-report.txt + { + echo "eMMC report, $(date '+%F %T' 2> /dev/null), system on $BOOT_DISK" + echo "eMMC $EMMC: $(sectors "$EMMC") sectors, $(cat /sys/block/${EMMC##*/}/device/name 2> /dev/null)," \ + "manfid $(cat /sys/block/${EMMC##*/}/device/manfid 2> /dev/null)," \ + "date $(cat /sys/block/${EMMC##*/}/device/date 2> /dev/null)" + echo "state: $(disk_state "$EMMC")" + if has_bootloader "$EMMC"; then + _size=$(wc -c < "$BOOTLOADER") + if dd if="$EMMC" bs=1024 skip=8 count=$(( (_size + 1023) / 1024 )) 2> /dev/null | head -c "$_size" | + cmp -s - "$BOOTLOADER"; then + echo "boot loader at 8 KiB: yes, the same as this system's" + else + echo "boot loader at 8 KiB: yes, a DIFFERENT one" + fi + else + echo "boot loader at 8 KiB: none" + fi + # the eMMC's hardware boot partitions: a boot ROM that honours + # PARTITION_CONFIG reads them before the user area + for _b in boot0 boot1; do + _d=/dev/${EMMC##*/}$_b + [ -b "$_d" ] || continue + _at= + [ "$(dd if="$_d" bs=1 skip=4 count=8 2> /dev/null)" = "eGON.BT0" ] && _at="$_at 0" + [ "$(dd if="$_d" bs=1 skip=8196 count=8 2> /dev/null)" = "eGON.BT0" ] && _at="$_at 8KiB" + [ "$(dd if="$_d" bs=1 skip=131076 count=8 2> /dev/null)" = "eGON.BT0" ] && _at="$_at 128KiB" + _nz=$(dd if="$_d" bs=4096 count=64 2> /dev/null | od -An -tx1 | tr -d ' \n0' | wc -c) + echo "hardware partition $_b: boot loader at:${_at:- none}; first 256 KiB $([ "$_nz" -gt 0 ] && echo "has data" || echo "all zero")" + [ -n "$_at" ] && dd if="$_d" bs=1 skip=$(case "$_at" in " 0"*) echo 0 ;; *8KiB*) echo 8192 ;; *) echo 131072 ;; esac) count=64 2> /dev/null | od -An -c | head -4 + done + echo "--- partition table" + sfdisk --dump "$EMMC" 2>&1 + for _n in 1 2 3; do + [ -b "$(part "$EMMC" $_n)" ] && echo "$(part "$EMMC" $_n): $(blkid "$(part "$EMMC" $_n)" 2> /dev/null)" + done + echo "--- U-Boot environment on the eMMC (set by U-Boot when it started from it)" + printf '%s 0x100000 0x10000\n%s 0x110000 0x10000\n' "$EMMC" "$EMMC" > "$RUN/fw_env.emmc.config" + fw_printenv -c "$RUN/fw_env.emmc.config" 2>&1 | grep -E "rsos_|mmc_bootdev|Warning|Bad CRC|bootcmd=" | cut -c1-200 + echo "--- EXT_CSD" + grep -q " /sys/kernel/debug " /proc/mounts || mount -t debugfs debugfs /sys/kernel/debug 2> /dev/null + _x= + for _f in /sys/kernel/debug/mmc*/mmc*:*/ext_csd; do + case "$_f" in */mmc1:*) read -r _x < "$_f" 2> /dev/null ;; esac + done + if [ -n "$_x" ]; then + for _fd in $EXT_CSD_FIELDS; do + echo " ${_fd#*:} [${_fd%%:*}] = 0x$(ext_csd_byte "$_x" "${_fd%%:*}")" + done + else + echo " (not readable: $(ls -d /sys/kernel/debug/mmc* 2> /dev/null | tr '\n' ' '))" + fi + echo "--- AXP209 data registers at this start (REG04-0F)" + _a=$(cat "$RUN/axp-data" 2> /dev/null) + echo " ${_a:-(not read)}" + case "$_a" in + *" 5a e2") echo " => the last start from the eMMC reached U-Boot proper (the SPL loaded it)" ;; + *" 5a e1") echo " => the last start from the eMMC ran the SPL, which did NOT start U-Boot proper" ;; + *) echo " => no sign of a start from the eMMC: the boot ROM did not run our SPL (or no attempt since the last shutdown)" ;; + esac + echo "--- logs a start from the eMMC left" + _m= + if lib_mounted emmc; then + _m=$MEDIA/emmc + elif [ "$(fstype "$(part "$EMMC" 1)")" = exfat ]; then + _m=$RUN/emmc-ro + mkdir -p "$_m" + mount -t exfat -o ro "$(part "$EMMC" 1)" "$_m" 2> /dev/null || _m= + fi + if [ -n "$_m" ] && [ -d "$_m/rsos/logs" ]; then + rm -rf "$DATA/rsos/logs/emmc" + mkdir -p "$DATA/rsos/logs/emmc" + cp -R "$_m/rsos/logs/." "$DATA/rsos/logs/emmc/" 2> /dev/null + echo "copied to rsos/logs/emmc/: $(ls "$_m/rsos/logs" | tr '\n' ' ')" + else + echo "none" + fi + [ "$_m" = "$RUN/emmc-ro" ] && { umount "$_m"; rmdir "$_m"; } + } > "$_r.tmp" 2>&1 + mv -f "$_r.tmp" "$_r" + sync +} + +# ----------------------------------------------------------------- boot +boot() { + # first start from the eMMC: the overlays the microSD had (M.2 SATA) + if [ "$BOOT_DISK" = "$EMMC" ] && [ -f "$DATA/rsos/pending-overlays" ]; then + read -r _ov < "$DATA/rsos/pending-overlays" + if [ -n "$_ov" ] && fw_setenv rsos_overlays "$_ov"; then + msg "rsos_overlays=\"$_ov\" carried over from the microSD (applies at the next start)" + fi + rm -f "$DATA/rsos/pending-overlays" + fi + # the SATA link comes up a moment after the driver (rcS loads it) + if [ -d /sys/bus/platform/devices/1c18000.sata ]; then + _i=0 + while ! m2_disk > /dev/null && [ $_i -lt 50 ]; do + usleep 200000 2> /dev/null || sleep 1 + _i=$((_i + 1)) + done + fi + refresh > /dev/null + report +} + +# ----------------------------------------------------------------- main +cmd=$1 +[ $# -gt 0 ] && shift +case "$cmd" in +status) + status + ;; +overlay) + overlay "$@" + ;; +redetect) + redetect + ;; +install|sd-games|sd-starter|sd-full|format|refresh|boot|eject) + # one at a time: the quick ones wait their turn, a task waits a moment + # for one of them (a refresh at boot) but not for another task + exec 9> "$LOCK" + case "$cmd" in + refresh|boot|eject) flock 9 ;; + *) flock -w 30 9 ;; + esac || fail busy "another rsos-storage runs" + case "$cmd" in + install) install "$@" ;; + sd-games) sd_games ;; + sd-starter) sd_starter ;; + sd-full) sd_full ;; + format) format_disk "$@" ;; + refresh) refresh ;; + boot) boot ;; + eject) + case "$1" in emmc|sd|m2) lib_umount "$1"; write_libs; status > /dev/null ;; esac + ;; + esac + ;; +*) + sed -n '2,/^$/s/^# \{0,1\}//p' "$0" >&2 + exit 2 + ;; +esac diff --git a/buildroot-external/board/retrostone2/tests/boot-ab-qemu-test.sh b/buildroot-external/board/retrostone2/tests/boot-ab-qemu-test.sh index 25794c1..1a562ec 100755 --- a/buildroot-external/board/retrostone2/tests/boot-ab-qemu-test.sh +++ b/buildroot-external/board/retrostone2/tests/boot-ab-qemu-test.sh @@ -33,7 +33,11 @@ ulong timer_get_boot_us(void) { return timer_get_us(); } EOT export CROSS_COMPILE=arm-linux-gnueabihf- PATH=$H/bin:$PATH make -s Cubieboard_defconfig + dt=$(grep '^CONFIG_DEFAULT_DEVICE_TREE=' .config) ./scripts/kconfig/merge_config.sh -m .config "$BR/uboot.fragment" > /dev/null + # the fragment's A20 eMMC device tree and second MMC slot are not the + # Cubieboard's: keep its own (QEMU has one MMC: "mmc dev 1" fails) + sed -i "s|^CONFIG_DEFAULT_DEVICE_TREE=.*|$dt|; /^CONFIG_MMC_SUNXI_SLOT_EXTRA=/d" .config PATH=$H/bin:$PATH make -s olddefconfig grep -E 'WATCHDOG_AUTOSTART|CMD_POWEROFF|CMD_FS_GENERIC' .config PATH=$H/bin:$PATH make -s -j16 > ../build.log 2>&1 || { tail -20 ../build.log; exit 1; } @@ -69,11 +73,16 @@ sed 's|^\([[:space:]]*\)bootz \${kernel_addr_r} - \${fdt_addr_r}$|\1setexpr.b rs { echo "setenv silent"; cat boot.cmd.body; } > boot.cmd.test grep -q '=== KERNEL' boot.cmd.test || { echo "could not patch boot.cmd"; exit 1; } "$H/bin/mkimage" -A arm -O linux -T script -C none -n test -d boot.cmd.test boot.scr > /dev/null +# a Pro for the model probe: QEMU's machine has no second MMC, so "mmc dev 1" +# always fails there; this one finds an "eMMC" +sed 's|^\([[:space:]]*\)elif mmc dev 1; then$|\1elif true; then|' boot.cmd.test > boot-pro.cmd +grep -q 'elif true; then' boot-pro.cmd || { echo "could not patch the model probe"; exit 1; } +"$H/bin/mkimage" -A arm -O linux -T script -C none -n test -d boot-pro.cmd boot-pro.scr > /dev/null # make_slot good|bad|none make_slot() { rm -rf "$1"; mkdir -p "$1/boot" - cp boot.scr "$1/boot/" + cp "${SCR:-boot.scr}" "$1/boot/boot.scr" cp "$HOME/rsos/output/images/sun7i-a20-retrostone2.dtb" "$1/boot/" mkdir -p "$1/boot/overlays" cp "$HOME/rsos/output/target/boot/overlays/emmc.dtbo" "$HOME/rsos/output/target/boot/overlays/cpu-lowvolt.dtbo" \ @@ -258,6 +267,29 @@ make_card noscr empty boot check "bootflow scan, then poweroff" "grep -q 'poweroff ...' out.txt && no_kernel" +echo "== 14: the model probe (rsos_model), a regular unit" +make_card good empty +boot +check "first start: rsos_model=regular saved, no overlay" "grep -q 'rsos: model regular' out.txt && [ \"\$(env_get rsos_model)\" = regular ] && [ -z \"\$(env_get rsos_overlays)\" ] && started a pending" +confirm +env_set rsos_model +env_set rsos_overlays "emmc sata foo" +boot +check "probed again: emmc removed, sata and the others kept" "[ \"\$(env_get rsos_model)\" = regular ] && [ \"\$(env_get rsos_overlays)\" = 'sata foo' ] && ! grep -q 'overlay emmc applied' out.txt && started a pending" +confirm +boot +check "probed once only" "! grep -q 'rsos: model' out.txt && [ \"\$(env_get rsos_overlays)\" = 'sata foo' ] && started a pending" + +echo "== 15: the model probe, a Pro (the eMMC answers)" +SCR=boot-pro.scr make_card good empty +boot +check "first start: rsos_model=pro, overlays emmc and sata" "[ \"\$(env_get rsos_model)\" = pro ] && [ \"\$(env_get rsos_overlays)\" = 'emmc sata' ] && grep -q 'overlay emmc applied' out.txt && started a pending" +confirm +env_set rsos_model +env_set rsos_overlays "foo emmc" +boot +check "probed again: the others kept, emmc and sata once each" "[ \"\$(env_get rsos_overlays)\" = 'foo emmc sata' ] && grep -q 'overlay emmc applied' out.txt && started a pending" + echo "PASS=$pass FAIL=$fail" [ $fail -eq 0 ] diff --git a/buildroot-external/board/retrostone2/tools/rsos-axpstamp.c b/buildroot-external/board/retrostone2/tools/rsos-axpstamp.c index df228cf..8e90433 100644 --- a/buildroot-external/board/retrostone2/tools/rsos-axpstamp.c +++ b/buildroot-external/board/retrostone2/tools/rsos-axpstamp.c @@ -1,23 +1,43 @@ /* - * rsos-axpstamp - keep the times of the last shutdown steps across the power - * cut, in the AXP209's 12 data buffer registers (REG04-REG0F). + * rsos-axpstamp - the AXP209's 12 data buffer registers (REG04-REG0F), which + * the PMIC keeps as long as it has a power source (the battery), also while + * the system is off: * - * rcK writes /data/rsos/logs/shutdown.txt before it unmounts /data, so the - * last steps (sync, unmount, poweroff -f) cannot be logged there. The AXP209 - * keeps REG04-0F as long as it has a power source (the battery), also while - * the system is off, and neither U-Boot nor Linux use them. So rcK saves the - * uptimes of those steps there just before "poweroff -f", and the boot logger - * appends them to shutdown.txt on the next boot (and clears them). - * TODO(hw): check that the registers survive a power-off on battery. + * REG04-0C the times of the last shutdown steps (save / show) + * REG0D the memory speed setting and its trial (dram-*), read by + * U-Boot's SPL before it starts the DRAM (patches/uboot/0004) + * REG0E-0F the eMMC boot breadcrumbs of U-Boot (patches/uboot/0003) + * + * Shutdown times: rcK writes /data/rsos/logs/shutdown.txt before it unmounts + * /data, so the last steps (sync, unmount, poweroff -f) cannot be logged + * there. rcK saves their uptimes here just before "poweroff -f", and the boot + * logger appends them to shutdown.txt on the next boot (and clears them). * * rsos-axpstamp save * uptimes in seconds ("12.34", as in /proc/uptime) of "log written", * "sync done" and "/data unmounted"; the current uptime is added as - * the fourth time ("poweroff -f"). Stored as 4 x 24-bit centiseconds. + * the fourth time ("poweroff -f"). Stored in REG04-0C: the first as + * 24-bit centiseconds, the three steps after it as 16-bit differences. * rsos-axpstamp show * prints " " in seconds and - * clears the registers; exit status 1 (nothing printed) if they hold - * no plausible record. + * clears REG04-0C; exit status 1 (nothing printed) if they hold no + * plausible record. + * rsos-axpstamp raw + * REG04-0F in hex, unchanged (rcS, before "show" clears them). + * + * Memory speed (Settings > Power > Memory speed, the RetroStone2): REG0D = + * << 4 | , level 1 = 432 MHz, 2 = 480 MHz (0 or anything else: + * the normal 384 MHz). States: A requested, B trial running (set by the SPL + * when it uses the level), C confirmed (set by rsos-boot-ok after a stable + * start and a memory test; the SPL turns it into B again at every start, so + * any start that is not confirmed falls back), F failed (the SPL found B: + * that start never got confirmed, the normal speed is used). + * rsos-axpstamp dram-get " " + * rsos-axpstamp dram-set N 0 = normal; 1, 2 = try that level at the next start + * rsos-axpstamp dram-ok B -> C (rsos-boot-ok) + * rsos-axpstamp dram-fail B -> F (the memory test failed) + * rsos-axpstamp dram-retry B -> A (an orderly shutdown before the + * confirmation: try again next time) * -b I2C bus number (default 0: the A20's TWI0, where the AXP209 is) * * The AXP209 (0x34) is bound to the axp20x driver, so plain I2C_SLAVE @@ -39,6 +59,8 @@ #define AXP_ADDR 0x34 #define AXP_DATA0 0x04 #define NVALS 4 +#define STAMP_BYTES 9 /* REG04-0C */ +#define REG_DRAM 0x0d #define MAX_CS 0xffffff /* 24 bits of centiseconds: 46 hours */ static int reg_write(int fd, uint8_t reg, uint8_t val) @@ -89,16 +111,90 @@ static long uptime_cs(void) static int usage(void) { fprintf(stderr, "usage: rsos-axpstamp [-b N] save \n" - " rsos-axpstamp [-b N] show\n"); + " rsos-axpstamp [-b N] show | raw\n" + " rsos-axpstamp [-b N] dram-get | dram-set <0|1|2> | dram-ok | dram-fail | dram-retry\n"); return 2; } +static int open_bus(const char *dev) +{ + int fd = open(dev, O_RDWR | O_CLOEXEC); + + if (fd < 0) + fprintf(stderr, "rsos-axpstamp: %s: %s\n", dev, strerror(errno)); + return fd; +} + +/* raw: REG04-0F in hex, unchanged */ +static int show_raw(const char *dev) +{ + uint8_t v; + int fd = open_bus(dev), j; + + if (fd < 0) + return 1; + for (j = 0; j < 12; j++) { + if (reg_read(fd, AXP_DATA0 + j, &v) < 0) { + fprintf(stderr, "rsos-axpstamp: read REG%02X: %s\n", AXP_DATA0 + j, strerror(errno)); + close(fd); + return 1; + } + printf("%s%02x", j ? " " : "", v); + } + printf("\n"); + close(fd); + return 0; +} + +static int dram_cmd(const char *dev, const char *cmd, const char *arg) +{ + static const char *const states[16] = { + [0xa] = "requested", [0xb] = "trial", [0xc] = "on", [0xf] = "failed", + }; + static const int mhz[3] = { 384, 432, 480 }; + uint8_t v, st, lvl; + int fd = open_bus(dev), r = 0; + + if (fd < 0) + return 1; + if (reg_read(fd, REG_DRAM, &v) < 0) { + fprintf(stderr, "rsos-axpstamp: read REG%02X: %s\n", REG_DRAM, strerror(errno)); + close(fd); + return 1; + } + st = v >> 4; + lvl = v & 0x0f; + if (lvl < 1 || lvl > 2 || !states[st]) { + st = 0; + lvl = 0; + } + if (!strcmp(cmd, "dram-get")) { + printf("%s %d %d\n", st ? states[st] : "normal", lvl, mhz[st == 0xa || st == 0xf ? 0 : lvl]); + } else if (!strcmp(cmd, "dram-set") && arg) { + int n = atoi(arg); + + if (n < 0 || n > 2 || arg[0] < '0' || arg[0] > '2' || arg[1]) + r = 2; + else + r = reg_write(fd, REG_DRAM, n ? (uint8_t)(0xa0 | n) : 0) < 0; + } else if (!strcmp(cmd, "dram-ok") || !strcmp(cmd, "dram-fail") || !strcmp(cmd, "dram-retry")) { + uint8_t to = cmd[5] == 'o' ? 0xc0 : cmd[5] == 'f' ? 0xf0 : 0xa0; + + if (st == 0xb) + r = reg_write(fd, REG_DRAM, (uint8_t)(to | lvl)) < 0; + } else { + r = usage(); + } + close(fd); + return r; +} + int main(int argc, char **argv) { char dev[32]; int bus = 0, fd, i, j; long cs[NVALS]; - uint8_t raw[NVALS * 3]; + uint8_t raw[STAMP_BYTES]; if (argc > 2 && strcmp(argv[1], "-b") == 0) { bus = atoi(argv[2]); @@ -109,25 +205,35 @@ int main(int argc, char **argv) return usage(); snprintf(dev, sizeof(dev), "/dev/i2c-%d", bus); + if (!strncmp(argv[1], "dram-", 5)) + return dram_cmd(dev, argv[1], argc > 2 ? argv[2] : NULL); + if (strcmp(argv[1], "save") == 0) { if (argc != 5) return usage(); for (i = 0; i < 3; i++) cs[i] = parse_cs(argv[2 + i]); cs[3] = uptime_cs(); - for (i = 0; i < NVALS; i++) { + for (i = 0; i < NVALS; i++) if (cs[i] < 0) return usage(); - raw[i * 3] = cs[i] >> 16; - raw[i * 3 + 1] = cs[i] >> 8; - raw[i * 3 + 2] = cs[i]; + raw[0] = cs[0] >> 16; + raw[1] = cs[0] >> 8; + raw[2] = cs[0]; + for (i = 1; i < NVALS; i++) { + long d = cs[i] - cs[i - 1]; + + if (d < 0) + d = 0; + if (d > 0xffff) + d = 0xffff; + raw[1 + 2 * i] = d >> 8; + raw[2 + 2 * i] = d; } - fd = open(dev, O_RDWR | O_CLOEXEC); - if (fd < 0) { - fprintf(stderr, "rsos-axpstamp: %s: %s\n", dev, strerror(errno)); + fd = open_bus(dev); + if (fd < 0) return 1; - } - for (j = 0; j < (int)sizeof(raw); j++) { + for (j = 0; j < STAMP_BYTES; j++) { if (reg_write(fd, AXP_DATA0 + j, raw[j]) < 0) { fprintf(stderr, "rsos-axpstamp: write REG%02X: %s\n", AXP_DATA0 + j, strerror(errno)); @@ -139,15 +245,16 @@ int main(int argc, char **argv) return 0; } + if (strcmp(argv[1], "raw") == 0 && argc == 2) + return show_raw(dev); + if (strcmp(argv[1], "show") == 0 && argc == 2) { int ok = 1; - fd = open(dev, O_RDWR | O_CLOEXEC); - if (fd < 0) { - fprintf(stderr, "rsos-axpstamp: %s: %s\n", dev, strerror(errno)); + fd = open_bus(dev); + if (fd < 0) return 1; - } - for (j = 0; j < (int)sizeof(raw); j++) { + for (j = 0; j < STAMP_BYTES; j++) { if (reg_read(fd, AXP_DATA0 + j, &raw[j]) < 0) { fprintf(stderr, "rsos-axpstamp: read REG%02X: %s\n", AXP_DATA0 + j, strerror(errno)); @@ -155,17 +262,14 @@ int main(int argc, char **argv) return 1; } } - for (i = 0; i < NVALS; i++) - cs[i] = (long)raw[i * 3] << 16 | raw[i * 3 + 1] << 8 | raw[i * 3 + 2]; - /* Plausible: non-zero, in order, all within a minute */ - for (i = 0; i < NVALS; i++) { - if (cs[i] == 0 || (i && cs[i] < cs[i - 1])) - ok = 0; - } - if (cs[NVALS - 1] - cs[0] > 6000) + cs[0] = (long)raw[0] << 16 | raw[1] << 8 | raw[2]; + for (i = 1; i < NVALS; i++) + cs[i] = cs[i - 1] + (raw[1 + 2 * i] << 8 | raw[2 + 2 * i]); + /* Plausible: non-zero, all within a minute */ + if (cs[0] == 0 || cs[NVALS - 1] - cs[0] > 6000) ok = 0; - /* Report once: clear the registers */ - for (j = 0; j < (int)sizeof(raw); j++) + /* Report once: clear the registers (never REG0D-0F) */ + for (j = 0; j < STAMP_BYTES; j++) if (raw[j]) reg_write(fd, AXP_DATA0 + j, 0); close(fd); diff --git a/buildroot-external/board/retrostone2/tools/rsos-memtest.c b/buildroot-external/board/retrostone2/tools/rsos-memtest.c new file mode 100644 index 0000000..3c82540 --- /dev/null +++ b/buildroot-external/board/retrostone2/tools/rsos-memtest.c @@ -0,0 +1,77 @@ +/* + * rsos-memtest - a quick DRAM check for the faster memory trial (Settings > + * Power > Memory speed; rsos-boot-ok runs it before it confirms a trial): + * + * rsos-memtest [MiB] [passes] (96 MiB, 2 passes) + * + * Writes, then reads back, over one buffer: every word's own address, its + * inverse, walking ones, and a pseudo-random sequence. A DRAM clocked beyond + * what the board's chips and wiring take shows up as flipped bits. Exit 0 + * when everything read back, 1 on the first error (printed), 2 when the + * buffer cannot be had. + */ +#include +#include +#include +#include + +static uint32_t lfsr(uint32_t x) +{ + return (x >> 1) ^ (-(x & 1u) & 0xd0000001u); +} + +static int check(volatile uint32_t *p, size_t n, int pattern, int pass) +{ + uint32_t r = 0x13579bdfu + (uint32_t)pass; + size_t i; + + for (i = 0; i < n; i++) { + uint32_t v; + + switch (pattern) { + case 0: v = (uint32_t)(uintptr_t)&p[i]; break; + case 1: v = ~(uint32_t)(uintptr_t)&p[i]; break; + case 2: v = 1u << (i & 31); break; + default: r = lfsr(r); v = r; break; + } + p[i] = v; + } + r = 0x13579bdfu + (uint32_t)pass; + for (i = 0; i < n; i++) { + uint32_t v, got = p[i]; + + switch (pattern) { + case 0: v = (uint32_t)(uintptr_t)&p[i]; break; + case 1: v = ~(uint32_t)(uintptr_t)&p[i]; break; + case 2: v = 1u << (i & 31); break; + default: r = lfsr(r); v = r; break; + } + if (got != v) { + printf("rsos-memtest: pass %d pattern %d word %zu: wrote %08x, read %08x\n", + pass, pattern, i, (unsigned)v, (unsigned)got); + return 1; + } + } + return 0; +} + +int main(int argc, char **argv) +{ + size_t mib = argc > 1 ? (size_t)atoi(argv[1]) : 96, n; + int passes = argc > 2 ? atoi(argv[2]) : 2; + volatile uint32_t *p; + + if (mib < 1 || mib > 512 || passes < 1) + return 2; + n = mib * 1024 * 1024 / sizeof(uint32_t); + p = mmap(NULL, n * sizeof(uint32_t), PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (p == MAP_FAILED) + return 2; + for (int pass = 0; pass < passes; pass++) + for (int pattern = 0; pattern < 4; pattern++) + if (check(p, n, pattern, pass)) + return 1; + printf("rsos-memtest: %zu MiB, %d passes: no error\n", mib, passes); + munmap((void *)p, n * sizeof(uint32_t)); + return 0; +} diff --git a/buildroot-external/board/retrostone2/uboot.fragment b/buildroot-external/board/retrostone2/uboot.fragment index 0de62f7..11bb8a3 100644 --- a/buildroot-external/board/retrostone2/uboot.fragment +++ b/buildroot-external/board/retrostone2/uboot.fragment @@ -25,8 +25,27 @@ CONFIG_BOOTDELAY=0 # stays reachable on a development build by pressing a key on the UART # during the (zero) boot delay: the bootcmd does not run then. Release # builds never stop (uboot-release.fragment). +# The boot loader runs from the disk the boot ROM started (board/sunxi/board.c +# sets mmc_bootdev: 0 = microSD, 1 = the Pro's eMMC, U-Boot's mmc1 with +# MMC_SUNXI_SLOT_EXTRA=2 below; 0 when it is unset: FEL). The system +# (rsos_sysdev) is normally on the same disk. Starter card (the Pro, see +# docs/pro-storage.md): a microSD that kept only its boot loader and its game +# partition, because the A20 boot ROM does not start this eMMC 5.0: no +# boot.scr on the card, so the system comes from the eMMC (mmc 1). U-Boot's +# environment stays on the card it started from. CONFIG_USE_BOOTCOMMAND=y -CONFIG_BOOTCOMMAND="if test \"${rsos_verbose}\" = 1; then setenv silent; else setenv silent 1; fi; setenv silent_linux no; if test \"${rsos_slot}\" = b; then load mmc 0:3 ${scriptaddr} /boot/boot.scr && source ${scriptaddr}; fi; load mmc 0:2 ${scriptaddr} /boot/boot.scr && source ${scriptaddr}; setenv silent; load mmc 0:3 ${scriptaddr} /boot/boot.scr && source ${scriptaddr}; bootflow scan -lb; poweroff" +CONFIG_BOOTCOMMAND="if test \"${rsos_verbose}\" = 1; then setenv silent; else setenv silent 1; fi; setenv silent_linux no; if test \"${mmc_bootdev}\" != 1; then setenv mmc_bootdev 0; fi; setenv rsos_sysdev ${mmc_bootdev}; if test ${mmc_bootdev} = 0; then if test -e mmc 0:2 /boot/boot.scr; then true; elif test -e mmc 0:3 /boot/boot.scr; then true; elif test -e mmc 1:2 /boot/boot.scr; then setenv rsos_sysdev 1; fi; fi; if test \"${rsos_slot}\" = b; then load mmc ${rsos_sysdev}:3 ${scriptaddr} /boot/boot.scr && source ${scriptaddr}; fi; load mmc ${rsos_sysdev}:2 ${scriptaddr} /boot/boot.scr && source ${scriptaddr}; setenv silent; load mmc ${rsos_sysdev}:3 ${scriptaddr} /boot/boot.scr && source ${scriptaddr}; bootflow scan -lb; poweroff" + +# Boot from the Pro's eMMC (Settings > Storage > Install to internal storage): +# the A20 boot ROM tries the microSD (mmc0) first, then NAND (none here), then +# mmc2, the eMMC. U-Boot's eMMC support is the one of the A20-OLinuXino-LIME-eMMC +# (same wiring: mmc2 on PC6-PC11, 4-bit, 3.3 V, reset on PC16): its device +# tree is the Lime's plus the enabled mmc2, and MMC_SUNXI_SLOT_EXTRA=2 makes +# the eMMC U-Boot's mmc1 (sunxi-u-boot.dtsi alias). The environment then lives +# on the disk that was booted (mmc_get_env_dev()), at the same offsets. On a +# unit without the eMMC, nothing reads mmc1 unless booted from it. +CONFIG_DEFAULT_DEVICE_TREE="sun7i-a20-olinuxino-lime-emmc" +CONFIG_MMC_SUNXI_SLOT_EXTRA=2 # Boot timing (docs/build.md, "Boot time"): bootstage records U-Boot's steps, # and boot.scr adds its own ("bootstage mark", patches/uboot/0001). The @@ -107,7 +126,7 @@ CONFIG_ENV_MMC_DEVICE_INDEX=0 CONFIG_ENV_MMC_EMMC_HW_PARTITION=0 CONFIG_CMD_SAVEENV=y CONFIG_CMD_SETEXPR=y -# boot.cmd: "test -e mmc 0: /boot/zImage" before falling back to the +# boot.cmd: "test -e mmc : /boot/zImage" before falling back to the # other slot, "poweroff" when no slot can be booted (AXP209 through the DM # PMIC's axp_sysreset; TODO(hw): check that it really cuts the power). CONFIG_CMD_FS_GENERIC=y diff --git a/buildroot-external/package/rsos-frontend/rsos-frontend.mk b/buildroot-external/package/rsos-frontend/rsos-frontend.mk index 8f503ef..51b41cb 100644 --- a/buildroot-external/package/rsos-frontend/rsos-frontend.mk +++ b/buildroot-external/package/rsos-frontend/rsos-frontend.mk @@ -84,6 +84,8 @@ RSOS_FRONTEND_MAKE_OPTS = \ BUILDDIR=$(@D)/build \ UPDATE_TLS=1 \ UPD_BUILD_EPOCH=$(RSOS_FRONTEND_BUILD_TIME) \ + $(if $(call qstrip,$(BR2_RETROSTONE_UPDATE_REPO)),RSOS_UPDATE_REPO=$(call qstrip,$(BR2_RETROSTONE_UPDATE_REPO))) \ + $(if $(call qstrip,$(BR2_RETROSTONE_UPDATE_PUBKEY)),RSOS_UPDATE_PUBKEY=$(call qstrip,$(BR2_RETROSTONE_UPDATE_PUBKEY))) \ $(if $(call qstrip,$(BR2_PACKAGE_RSOS_FRONTEND_SPLASH_BG)),SPLASH_BG=$(call qstrip,$(BR2_PACKAGE_RSOS_FRONTEND_SPLASH_BG))) define RSOS_FRONTEND_BUILD_CMDS diff --git a/docs/pro-storage.md b/docs/pro-storage.md new file mode 100644 index 0000000..896565d --- /dev/null +++ b/docs/pro-storage.md @@ -0,0 +1,140 @@ +# RetroStone2 "Pro" storage, game covers, multi-disc games + +The Pro variant has a 16 GB eMMC (U23, mmc2, 4-bit) and an M.2 M-key slot (J1) for a **SATA** SSD (the A20 +has no PCIe: NVMe drives do not work). This page describes how RetroStoneOS uses them, and two game list +features that came with this work: box covers from the libretro thumbnail server, and grouping multi-disc games. + +## Regular or Pro + +The first start of a card finds out which RetroStone2 it runs on. `boot.cmd` sees no `rsos_model` in the U-Boot +environment, so it probes U-Boot's `mmc 1` (the eMMC U23) once and saves `rsos_model=pro` or `regular`: + +- **Pro** (the eMMC answers): `rsos_overlays` gets `emmc` and `sata`, applied on that same start, so the + internal storage and the M.2 slot work without touching a setting (the toggles can still switch them off). +- **Regular** (no eMMC): `emmc` is removed from `rsos_overlays` (`sata` and the others stay), and Settings > + Storage hides the Internal storage section; the M.2 section stays. + +Settings > Storage shows **This console > Model** (also in System information) and **Detect the model again** +(`rsos-storage redetect` clears `rsos_model`; the next start probes again). A unit that runs from the eMMC or +through a starter card, or whose eMMC answers in Linux, counts as a Pro whatever `rsos_model` says. The probe +costs one failed MMC initialisation (milliseconds) on a regular unit, once. `boot-ab-qemu-test.sh` covers both +cases (cases 14 and 15). + +## What the player sees + +**Settings > Storage**, started from the microSD: + +| Item | What it does | +|---|---| +| Internal eMMC (toggle) | `rsos_overlays` in the U-Boot environment; the eMMC appears after a restart ("not fitted" on a unit without one) | +| Install to internal storage | copies RetroStoneOS to the eMMC, **with or without the games**, then asks: **Use as starter card** / Keep SD as it is | +| Update internal storage | the same when the eMMC already holds RetroStoneOS: keep its games and saves, or erase it | +| Use this SD card as starter card | when the eMMC holds RetroStoneOS (see "Starter card") | +| M.2 SATA drive (toggle) | the SATA controller (overlay `sata`); any unit with the slot fitted | +| Erase it and use it for games | the M.2 drive becomes a game disk (one exFAT partition, `roms//`) | + +Running from the eMMC: the card in the slot is shown with what it holds; a starter card says "Keep it in" +and offers nothing that could erase it. Another RetroStoneOS card can become a games card, any other card can be +erased for games. + +The games of every mounted game disk (the eMMC or microSD the system does not run from, the M.2 drive) are +merged into the game lists. **Import games** gets **Copy games to** (the system disk or a game disk; another disk +receives only the games, BIOS files, saves and themes stay on the system disk). + +**Settings > Game lists**: **Download game covers** and **Group multi-disc games** (below). +**System information > Credits** lists the contributors from the git history +(`scripts/support/update-credits.sh` writes `frontend/assets/credits.txt`). + +## The eMMC and the A20 boot ROM (why a starter card) + +The A20 boot ROM tries microSD, NAND, eMMC (mmc2), SPI NOR, then USB FEL. On the tested unit (Samsung AGND3R, +EXT_CSD_REV 7 = **eMMC 5.0**, PARTITION_CONFIG 0, RST_n_FUNCTION 0) the boot ROM **never runs the SPL from the +eMMC**, although the install is verified (the boot loader at 8 KiB is byte-identical, the layout is the SD's): +U-Boot's own breadcrumbs (below) stay unset and the screen stays dark (FEL). Olimex saw the same with eMMC 5.x on +the A20 and added SPI flash to their boards. So: + +* **Starter card**: after an install, the microSD can keep its boot loader and its RETROSTONE partition (the + games) and drop its two system partitions (`rsos-storage sd-starter`: MBR entries 2 and 3 removed, the boot + state reset for the eMMC's slot A). At power-on its U-Boot finds no `boot.scr` on the card and loads the one on + the eMMC; Linux runs from the eMMC, U-Boot's environment stays on the card (`rsos.envdev=mmcblk0` on the kernel + command line, `/dev/rsos-disk` in rcS). A freshly flashed card always starts first: the way back. +* The eMMC still gets its own boot loader at every install, so a unit whose boot ROM does start its eMMC (another + eMMC part, or a future workaround) boots it without a card, and nothing else changes. +* **Possible hardware route** (not done): the board has test pads on **PC0/PC1/PC2/PC23**, the A20's SPI0. An + 8-pin SPI NOR there would let the boot ROM start U-Boot from SPI (`SPL_SPI_SUNXI`), which then loads the system + from the eMMC: a boot without any card, as on Olimex's later boards. + +## How it works + +* **U-Boot** (`uboot.fragment`): the A20-OLinuXino-LIME-eMMC device tree (the Lime's plus mmc2, wired like the + RetroStone2: PC6-PC11 4-bit, reset PC16) and `MMC_SUNXI_SLOT_EXTRA=2`: the eMMC is U-Boot's `mmc1`. U-Boot sets + `mmc_bootdev` (0 microSD, 1 eMMC) and keeps its environment on the disk it started from. The boot command picks + the system disk `rsos_sysdev`: the boot disk, or the eMMC when started from a card without `boot.scr` (starter). +* **boot.cmd** loads from `mmc ${rsos_sysdev}` (`root=/dev/mmcblkp`; the DT aliases make the eMMC + `mmcblk1` in Linux too), applies the `emmc` overlay when the system is on the eMMC, and passes `rsos.envdev`. +* **Linux**: `board.ini` `data_disk = auto` (the disk of `root=`); `/etc/fw_env.config` uses `/dev/rsos-disk`, the + disk with U-Boot's environment; `rsos-boot-ok`, the updater and `rsos-storage` write there. A/B updates work the + same with the system on the eMMC. +* **`/usr/bin/rsos-storage`** (board overlay) does the disk work; the menu (`frontend/src/ui/storage.c`) runs it as + a job behind a progress page (`/run/rsos/storage-progress`). Every step is in the kernel log and in + `rsos/logs/storage.log` on the card. +* **The install** writes the SD image's layout to the eMMC: the boot area zeroed first, the partition table, the + running root slot in 16 MiB chunks read back from the eMMC (page cache dropped), slot B emptied, a new exFAT + `RETROSTONE` sized to the eMMC with the settings, saves, BIOS files, themes (and the games, after a space check + done before anything is written), then **last** the boot loader (checked with its sha256, read back). The + microSD is never written by the install. +* **Game disks** are mounted on `/run/rsos/media/`; `/run/rsos/libraries` lists those with `roms/`, + and the loader (`loader_set_libraries`, `games_merge`) merges `/roms//` into each list, each disk + with its own scan cache. The menu polls `/sys/block` every 2 s: a card or drive that comes or goes runs + `rsos-storage refresh`. +* **Diagnostics**: at every start from the microSD with the eMMC visible, `rsos/logs/emmc-report.txt` (its boot + loader, partitions, U-Boot environment, EXT_CSD boot registers, the logs a start from it left) and the AXP209 + data registers: U-Boot patch 0003 writes REG0E/0F = `5a e1` (SPL) / `5a e2` (U-Boot proper) when it runs from + the eMMC; rcS reads them (`axpstamp raw`) before the boot logger clears them. + +## Game covers + +`rsos-covers` (frontend/src/update, the updater's HTTPS client) gets box art from +`thumbnails.libretro.com//Named_Boxarts/`, the server RetroArch uses. The menu writes the +games without a picture to a request file; per system the helper reads the server's listing once and matches +each ROM file name: exactly (No-Intro names), else the title without its (tags), the ROM's region first, then +USA, World, Europe, Japan, and finally a title that starts with the file's name ("Donkey Kong Country 2" -> +"... - Diddy's Kong Quest"). Covers are scaled to 400 px and saved as JPEG (~40 KB) in +`roms//media/covers/.jpg`; the game lists show them when `gamelist.xml` gives no image +(`apply_covers` in ui/games.c). Nothing else is written: Skraper's `gamelist.xml` and existing art win. Arcade +systems are skipped (zip names, not titles). The games not found are listed in `rsos/logs/covers-missing.txt`. +Needs WiFi and a correct clock (HTTPS). + +## Multi-disc games + +`.m3u` playlists already make one entry of a multi-disc game (the discs it lists are hidden; Select+X > Disc +changes discs). **Group multi-disc games** writes the missing playlists: for every set of `... (Disc N) ...` +files in a folder without one, `.m3u` listing the discs in order. Files are never moved or renamed. + +## Memory speed (Settings > Power > Memory speed) + +The DRAM runs at 384 MHz with the Lime's vendor timings. **Faster** = 432 MHz (+12.5 %), **Fastest** = 480 MHz +(+25 %), each with U-Boot's DDR3-1066F timings for that clock (the A20 boards that run there: Cubietruck, +Bananapi, Lamobo R1). The clock is chosen by the SPL before it starts the DRAM (U-Boot patch 0004), from the +AXP209 data register REG0D, which the PMIC keeps while the battery is in: + +* the menu writes "requested" (`axpstamp dram-set`); every start at a faster level is a **trial** (the SPL + writes "trial" before using it); +* 30 s after a stable start, `rsos-boot-ok` runs `rsos-memtest` (96 MiB, 4 patterns, 2 passes): passed → + "confirmed"; failed → "failed" and an immediate restart at the normal speed (a memory that flips bits must not + write files); +* a start that is never confirmed (freeze, crash, power cut, forced power-off) is seen by the next SPL: normal + speed, "failed", shown in the menu. An orderly shutdown before the confirmation retries at the next start. +* the battery out (REG0D cleared): the normal speed. + +The shutdown timestamps (`axpstamp save/show`) moved to REG04-0C to leave REG0D-0F alone. + +## Hardware test checklist + +1. Flash the image, boot, Settings > Storage: **Model** says RetroStone2 Pro, and **Internal eMMC** and **M.2 + SATA drive** are already on (on a regular unit: Model RetroStone2, no Internal storage section). +2. **Install to internal storage**, then **Use as starter card**, then **Restart now** (keep the card in). +3. Settings > Storage shows "Started from: internal + SD starter card"; the card's games are in the lists. +4. M.2: **Erase it and use it for games**, import to it with **Copy games to**, play a game from it. +5. Game lists: **Group multi-disc games**, **Download game covers** (WiFi on). +6. Recovery: a freshly flashed RetroStoneOS card starts the console from the card. diff --git a/docs/updates.md b/docs/updates.md index 777b308..d6c127b 100644 --- a/docs/updates.md +++ b/docs/updates.md @@ -144,6 +144,13 @@ cannot type one); protect the file instead. It was made on 2026-09-27 with package signed with *another* key is always refused. - A fork uses its own key: `rsos-mkupdate keygen`, its public key in `frontend/assets/update.pub` (or `RSOS_UPDATE_PUBKEY=` for the frontend build), its secret in its own repository. +- A fork that publishes its own updates also points the updater at its releases. In its defconfig: + `BR2_RETROSTONE_UPDATE_REPO="/RetroStoneOS"` (the GitHub repository; empty: PaddleStroke/RetroStoneOS) + and `BR2_RETROSTONE_UPDATE_PUBKEY=""` (empty: `frontend/assets/update.pub`); for the + frontend alone, `RSOS_UPDATE_REPO=/` and `RSOS_UPDATE_PUBKEY=`. Its images then find and + install only its own signed releases; an image of the original project never installs them (another key). + Versions follow the same order (§4.1): a fork can number its builds `0.1.3.12`, `0.1.3.13`, ... (newer than + 0.1.3, older than the next 0.1.4). Signing by hand (a package built locally, or an unsigned CI package): diff --git a/frontend/Makefile b/frontend/Makefile index f089907..5858b4f 100644 --- a/frontend/Makefile +++ b/frontend/Makefile @@ -897,6 +897,7 @@ install: $(BINS) update-install $(if $(WEB),install -m 0644 $(WEB) $(DESTDIR)$(DATADIR)/web/) install -m 0644 third_party/sdl-gamecontrollerdb/gamecontrollerdb.txt $(DESTDIR)$(DATADIR)/gamecontrollerdb.txt install -m 0644 src/transfer/data-README.txt $(DESTDIR)$(DATADIR)/data-README.txt + install -m 0644 assets/credits.txt $(DESTDIR)$(DATADIR)/credits.txt install -m 0644 $(SPLASH) $(DESTDIR)$(DATADIR)/splash.rle $(if $(CATALOGS),install -m 0644 $(CATALOGS) $(DESTDIR)$(DATADIR)/locale/) printf '%s\n' "$(RSOS_VERSION_STRING)" > $(DESTDIR)$(SYSCONFDIR)/rsos-version diff --git a/frontend/assets/credits.txt b/frontend/assets/credits.txt new file mode 100644 index 0000000..f6d7ba2 --- /dev/null +++ b/frontend/assets/credits.txt @@ -0,0 +1,10 @@ +# RetroStoneOS credits, shown in Settings > System information > Credits. +# Generated by scripts/support/update-credits.sh from the git history: +# do not edit by hand. "## " lines are section titles. +## RetroStoneOS +8BCraft +Jose Manuel Caamaño +KurdtNervanna +PaddleStroke +## RetroStone VC games +8BCraft diff --git a/frontend/src/main.c b/frontend/src/main.c index 14816b0..dfffaa4 100644 --- a/frontend/src/main.c +++ b/frontend/src/main.c @@ -1458,7 +1458,8 @@ static bool tcp_client_on(const char *path, unsigned port) * once a second): a USB import, export or backup while it copies (not while * it waits for the player: a duplicate question, or a summary nobody has * read yet, review), the web share or the SMB share with a client, an OS - * update download/install, the storage being formatted, the game list still + * update download/install, a storage task or game cover download (also in the + * background), the storage being formatted, the game list still * loading, a game whose core cannot save a state (an idle power-off would * lose it) or a benchmark. When the last one ends, the countdown starts * over. The dim and screen-off stages go on meanwhile. @@ -1489,6 +1490,8 @@ static void idle_busy_check(bool force) why = "smb-client"; else if (M.ui && ui_update_busy(M.ui)) why = "os-update"; + else if (M.ui && ui_storage_busy(M.ui)) + why = "storage-or-covers"; else if (M.ui && !M.in_game && !ui_is_loaded(M.ui)) why = "loading"; power_set_busy(why != NULL, why); diff --git a/frontend/src/tools/uipreview.c b/frontend/src/tools/uipreview.c index 242e166..174be39 100644 --- a/frontend/src/tools/uipreview.c +++ b/frontend/src/tools/uipreview.c @@ -11,6 +11,8 @@ * hold: