# Update and Recovery Flow

## Shared Paths

`/etc/init.d/shared` defines the main update and persistence paths:

| Variable | Path |
| --- | --- |
| `GAMES_PATH` | `/games` |
| `SPIKE_MENU_PATH` | `/games/spike3/spike_menu` |
| `OS_UPDATE_PATH` | `/connectivity/os_update` |
| `DOWNLOADED_SPK_PATH` | `/connectivity/update` |
| `MACHINE_REBOOTS_PATH` | `/connectivity/machine_reboots` |
| `CORE_FILE_PATH` | `/connectivity/dump/cores` |
| `GAME_RESTARTS_PATH` | `/tmp/game_restarts` |
| `DOWNLOADED_SPK_AVAILABLE` | `/connectivity/update/UPDATE_REQUESTED` |
| `CONNECTIVITY_CACHE_CLEARED` | `/tmp/connectivity_cache_cleared` |

## Kernel / Boot Partition Update

`S95game` checks `/connectivity/os_update` for:

- `boot.img`
- `boot.img.sha1`
- `boot.sig`
- `boot.sig.sha1`
- `config.txt`
- `config.txt.sha1`

If any are present, all must be present. It validates SHA-1 before copying.

Install behavior:

1. Mount `/dev/mmcblk0p1` at `/boot`.
2. Compare current `/boot` target hash to expected hash.
3. Copy file if needed.
4. Sync and re-check installed hash.
5. Unmount `/boot`.
6. Delete staged update files.
7. If anything was updated, sync, remount `/data` and `/connectivity` read-only, call `/sbin/reboot -f`, and hang.

The expected boot partition files are:

- `/boot/boot.img`
- `/boot/boot.sig`
- `/boot/config.txt`

## Post-install Hook

Before normal startup work, `S95game` runs `/connectivity/os_update/post_install_hook.sh` if present, then deletes it and syncs.

## Game Software Update

`/etc/init.d/update` checks two sources for `.spk` update files:

1. Connectivity-downloaded update files under `/connectivity/update` when `/connectivity/update/UPDATE_REQUESTED` exists.
2. USB flash drives mounted from `/dev/sda1` or `/dev/sda` at `/media`.

Accepted filename patterns:

- `*.spk` case-insensitive.
- `*.spk.NNN.000` case-insensitive extension, with three digits before `.000`.

When a valid update is found:

1. Kill `reboot_monitor`, `conagent_monitor`, and `game_monitor`.
2. Kill `conagent` and `game`.
3. Wait until `pidof game` is empty.
4. Start `/etc/init.d/update_monitor`.
5. On success path, display an update-complete message and hang for power cycle.

## Spike Menu Update Monitor

`/etc/init.d/update_monitor` runs the SPIKE menu update application from tmpfs:

1. Deletes `/tmp/spike_menu`.
2. Copies `/games/spike3/spike_menu/game`, `image.bin`, and `assets` to `/tmp/spike_menu`.
3. Runs `/tmp/spike_menu/game`.
4. Re-runs while `/tmp/spike_menu.restart` exists.
5. Deletes `/tmp/spike_menu` on exit.

The tmpfs copy is intentional because an update may overwrite `image.bin` while it is memory-mapped by the update process.

## Connectivity Cache Recovery

`clear_connectivity_cache` in `/etc/init.d/shared` deletes:

- `/connectivity/cache`
- `/connectivity/files`
- `/connectivity/media_overlay`

It then touches `/tmp/connectivity_cache_cleared`.

The comments identify additional intended `/connectivity` content:

- ALSA and Bluetooth persistent state.
- IC metadata and media caches.
- QR code storage.
- Net game save state/playback under `/connectivity/files/gkpd_4`.
- Connectivity software download cache.
- Sentinels such as `UPDATE_REQUESTED`, `UPDATE_COMPLETED`, and `.swdl_tod`.

## Core Dumps

Startup creates `/connectivity/dump/cores`, prunes stale `.7z` files, and keeps either:

- No core files if `/connectivity` is more than 90% full.
- Otherwise, at most 3 newest core files.

Core pattern:

```text
/connectivity/dump/cores/core.%h.%t
```

The export helper `copy_newest_corefile_to_usb.sh` encrypts the newest core with `7za` and a passphrase file, then copies the encrypted archive to a USB drive.
