From f300fc2d4ad6bc62b132617b6225ec8d052504c4 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 20 Sep 2026 03:44:55 +0100 Subject: [PATCH 01/12] fix: report the real error when a flash fails Three diagnostics in the MicroSD flash view reported nothing useful. The "Selected:" log passed the filename as an argument to a message with no placeholder, which raises TypeError inside logging as soon as the level is enabled; the copy step printed its result to stdout instead of the log; and the failure screen showed the literal text "data.stderr" instead of what dd reported. --- src/seedsigner/views/microsd_views.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 85202ede4..62709b8a0 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -112,18 +112,18 @@ def run(self): return Destination(BackStackView) microsd_image = microsd_images[selected_file_num] - logger.info("Selected:", microsd_image) + logger.info("Selected: %s", microsd_image) if platform.uname()[1] == "seedsigner-os": image_path = os.path.join('/mnt/microsd/microsd-images', microsd_image) data = run(['cp', image_path, '/tmp/img.img'], capture_output=True, text=True) - print(data) + logger.info(data) if len(data.stderr) > 1: self.run_screen( WarningScreen, title="Error", status_headline=None, - text="data.stderr", + text=data.stderr, show_back_button=False, ) return Destination(MainMenuView) From 6efaed13cfed229460845d97f94ef0a218844406 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 20 Sep 2026 03:44:56 +0100 Subject: [PATCH 02/12] fix: detect the MicroSD card at the point of use Every MicroSD tool resolved the device node once and then used that path across a prompt asking the user to insert the card to work on. The flash view resolved it before staging the image and before that prompt, so it refused outright whenever the slot was empty at that moment, and the path it kept described whichever card was present before the swap. The verify view never checked the lookup at all and ran dd with if=None, reading a file named "None". Look the device up immediately before each dd instead, and refuse with a screen stating that nothing was written, read or wiped when no card is present. The refusal is a single helper, so the next tool added here cannot quietly leave it out. --- src/seedsigner/views/microsd_views.py | 57 ++++--- tests/test_microsd_device_guard.py | 211 ++++++++++++++++++++++++++ 2 files changed, 248 insertions(+), 20 deletions(-) create mode 100644 tests/test_microsd_device_guard.py diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 62709b8a0..f05e02244 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -29,6 +29,26 @@ def find_sd_card_device(): return None +def refuse_without_card(view: View, text: str) -> Destination: + """Report that nothing was done and return to the main menu. + + find_sd_card_device() returns None when no card is in the slot, and every + caller must check it immediately before running dd: a path looked up any + earlier describes whichever card was inserted then, and None reaches dd as + the literal string "None" (`of=None`, `if=None`), which either fails + obscurely or writes to a file of that name. + """ + view.run_screen( + WarningScreen, + title="Error", + status_headline=None, + text=text, + show_back_button=False, + button_data=[ButtonOption("Continue")] + ) + return Destination(MainMenuView) + + class ToolsMicroSDMenuView(View): FLASH_IMAGE = ButtonOption("Flash Image") VERIFY_IMAGE = ButtonOption("Verify MicroSD") @@ -90,8 +110,6 @@ def run(self): if ret == RET_CODE__BACK_BUTTON: return Destination(BackStackView) - microsd_dev = find_sd_card_device() - if platform.uname()[1] == "seedsigner-os": microsd_images = os.listdir('/mnt/microsd/microsd-images/') else: @@ -140,6 +158,10 @@ def run(self): if ret == RET_CODE__BACK_BUTTON: return Destination(BackStackView) + microsd_dev = find_sd_card_device() + if microsd_dev is None: + return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") + self.loading_screen = LoadingScreenThread(text="Flashing MicroSD\n\n\n\n\n\n") self.loading_screen.start() @@ -203,6 +225,10 @@ def run(self): return Destination(MainMenuView) else: + microsd_dev = find_sd_card_device() + if microsd_dev is None: + return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") + image_path = os.path.join('/boot/microsd-images', microsd_image) run(['cp', image_path, '/tmp/img.img'], check=False) run(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}'], check=False) @@ -246,21 +272,8 @@ def run(self): from subprocess import run microsd_dev = find_sd_card_device() - - # Nothing to read without a card. Say so rather than running dd against "if=None" - # -- which on a desktop without dd/sudo/sha256sum is a hard crash (Windows), and - # on Linux quietly produces an empty checksum and a misleading "unfamiliar - # checksum" warning. if microsd_dev is None: - self.run_screen( - WarningScreen, - title="No MicroSD Card", - status_headline=None, - text="No MicroSD card detected.", - show_back_button=False, - button_data=[ButtonOption("OK")], - ) - return Destination(MainMenuView) + return refuse_without_card(self, "No MicroSD card detected. Nothing was read.") self.loading_screen = LoadingScreenThread(text="Reading MicroSD\n\n\n\n\n\n") self.loading_screen.start() @@ -311,8 +324,6 @@ class ToolsMicroSDWipeZeroView(View): def run(self): from subprocess import run - microsd_dev = find_sd_card_device() - button_data = [self.WIPE_64MB, self.WIPE_256MB, self.WIPE_ALL] wipe_selection = self.run_screen( @@ -346,6 +357,10 @@ def run(self): if ret == RET_CODE__BACK_BUTTON: return Destination(BackStackView) + microsd_dev = find_sd_card_device() + if microsd_dev is None: + return refuse_without_card(self, "No MicroSD card detected. Nothing was wiped.") + self.loading_screen = LoadingScreenThread(text="Wiping MicroSD\n\n\n\n\n\n(This takes a while)") self.loading_screen.start() @@ -413,8 +428,6 @@ class ToolsMicroSDWipeRandomView(View): def run(self): from subprocess import run - microsd_dev = find_sd_card_device() - button_data = [self.WIPE_64MB, self.WIPE_256MB, self.WIPE_ALL] wipe_selection = self.run_screen( @@ -448,6 +461,10 @@ def run(self): if ret == RET_CODE__BACK_BUTTON: return Destination(BackStackView) + microsd_dev = find_sd_card_device() + if microsd_dev is None: + return refuse_without_card(self, "No MicroSD card detected. Nothing was wiped.") + self.loading_screen = LoadingScreenThread(text="Wiping MicroSD\n\n\n\n\n\n(This takes a while)") self.loading_screen.start() diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py new file mode 100644 index 000000000..51af87513 --- /dev/null +++ b/tests/test_microsd_device_guard.py @@ -0,0 +1,211 @@ +# pylint: disable=missing-function-docstring +import os +from types import SimpleNamespace + +import pytest + +# Must import base before any seedsigner modules +from base import BaseTest + +from seedsigner.views import microsd_views +from seedsigner.views.view import MainMenuView + + +SD_DEV = "/dev/mmcblk0" + +# dd reports its progress on stderr; matching in/out counts mean success. +DD_OK = "64+0 records in\n64+0 records out\n" + + +class RecordingScreens: + """Answers every run_screen() call and records the screens a View showed.""" + + def __init__(self): + self.calls = [] + + def __call__(self, screen_cls, **kwargs): + self.calls.append(kwargs) + return 0 # always pick the first button + + @property + def titles(self): + return [call.get("title") for call in self.calls] + + @property + def texts(self): + # A screen's wording can arrive as either kwarg, e.g. the verify view + # passes "Matched Checksum" as status_headline and the checksum as text. + return [ + text + for call in self.calls + for text in (call.get("text"), call.get("status_headline")) + ] + + def showed(self, needle: str) -> bool: + return any(needle in (text or "") for text in self.texts) + + +class DummyLoadingScreenThread: + def __init__(self, *args, **kwargs): + pass + + def start(self): + pass + + def stop(self): + pass + + +def patch_environment(monkeypatch, commands, on_seedsigner_os=True): + """Make a MicroSD View runnable headlessly and record the commands it runs.""" + hostname = "seedsigner-os" if on_seedsigner_os else "testhost" + monkeypatch.setattr( + "platform.uname", lambda: ("Linux", hostname, "", "", "", "") + ) + monkeypatch.setattr(microsd_views, "LoadingScreenThread", DummyLoadingScreenThread) + + real_listdir = os.listdir + + def fake_listdir(path): + if "microsd-images" in str(path): + return ["seedsigner_os.img"] + return real_listdir(path) + + monkeypatch.setattr(os, "listdir", fake_listdir) + + def fake_run(cmd, *args, **kwargs): + commands.append(cmd) + if cmd[0] == "cp": + # `cp` output is checked for errors; anything on stderr aborts a flash. + return SimpleNamespace(stdout="", stderr="", returncode=0) + if cmd[0] == "sha256sum": + return SimpleNamespace(stdout="0" * 64 + " /tmp/img.img\n", stderr="", returncode=0) + return SimpleNamespace(stdout="", stderr=DD_OK, returncode=0) + + monkeypatch.setattr("subprocess.run", fake_run) + + +def build_view(monkeypatch, view_cls): + view = view_cls() + screens = RecordingScreens() + monkeypatch.setattr(view, "run_screen", screens, raising=False) + return view, screens + + +def assert_no_dd(commands): + """Nothing may read or write a device: no dd at all, and never `of=None`.""" + for cmd in commands: + assert "dd" not in cmd, cmd + assert not any("None" in part for part in cmd), cmd + + +def assert_dd_targets(commands, device): + """Every dd must name the device that was detected, and nothing else.""" + dd_cmds = [cmd for cmd in commands if "dd" in cmd] + assert dd_cmds, commands + for cmd in dd_cmds: + assert any(part.endswith(device) for part in cmd), cmd + assert not any("None" in part for part in cmd), cmd + + +class TestNoCardMeansNoCommand(BaseTest): + """No inserted card must mean no dd and no claim of success.""" + + @pytest.mark.parametrize( + "view_cls,success_text", + [ + (microsd_views.ToolsMicroSDWipeZeroView, "MicroSD Wiped"), + (microsd_views.ToolsMicroSDWipeRandomView, "MicroSD Wiped"), + (microsd_views.ToolsMicroSDFlashView, "MicroSD Flashed"), + (microsd_views.ToolsMicroSDVerifyView, "Matched Checksum"), + ], + ) + def test_no_card_is_refused(self, monkeypatch, view_cls, success_text): + commands = [] + patch_environment(monkeypatch, commands) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: None) + + view, screens = build_view(monkeypatch, view_cls) + + destination = view.run() + + # Staging the image in /tmp is allowed; touching a device is not. + assert_no_dd(commands) + assert not screens.showed(success_text) + assert screens.showed("No MicroSD") + assert destination.View_cls is MainMenuView + + +class TestCardInsertedAtThePrompt(BaseTest): + """The card is detected at the point of use, i.e. after the insert prompt.""" + + @pytest.mark.parametrize( + "view_cls", + [ + microsd_views.ToolsMicroSDWipeZeroView, + microsd_views.ToolsMicroSDWipeRandomView, + ], + ) + def test_wipe_finds_a_card_inserted_after_the_prompt(self, monkeypatch, view_cls): + commands = [] + patch_environment(monkeypatch, commands) + + view, screens = build_view(monkeypatch, view_cls) + # The user only inserts the card once the View has asked for it. + monkeypatch.setattr( + microsd_views, + "find_sd_card_device", + lambda: SD_DEV if screens.showed("Insert MicroSD") else None, + ) + + destination = view.run() + + assert_dd_targets(commands, SD_DEV) + assert screens.showed("MicroSD Wiped") + assert destination.View_cls is MainMenuView + + def test_flash_finds_a_card_inserted_after_the_prompt(self, monkeypatch): + commands = [] + patch_environment(monkeypatch, commands) + + view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) + monkeypatch.setattr( + microsd_views, + "find_sd_card_device", + lambda: SD_DEV if screens.showed("Insert MicroSD") else None, + ) + + destination = view.run() + + assert_dd_targets(commands, SD_DEV) + assert screens.showed("MicroSD Flashed") + assert destination.View_cls is microsd_views.ToolsMicroSDVerifyView + + +class TestFlashOnADevHost(BaseTest): + """The non-seedsigner-os flash branch has its own dd, so its own guard.""" + + def test_no_card_is_refused(self, monkeypatch): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=False) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: None) + + view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) + + destination = view.run() + + assert_no_dd(commands) + assert screens.showed("No MicroSD") + assert destination.View_cls is MainMenuView + + def test_a_present_card_is_flashed(self, monkeypatch): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=False) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + + view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) + + destination = view.run() + + assert_dd_targets(commands, SD_DEV) + assert destination.View_cls is MainMenuView From 0ecace76dcd5dcf413f8c60b12d66e8f79da6feb Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 20 Sep 2026 03:44:56 +0100 Subject: [PATCH 03/12] fix: treat a blank MicroSD card as present find_sd_card_device() reported a card only when its disk node had a partition child in sysfs, which the kernel creates only after parsing a partition table. A brand-new card has none, and neither does one the wipe tool just zeroed, so the tools were blind to the very cards they exist to write: wiping the same card twice, or flashing a blank one, looked like an empty slot. Report the disk node itself, still preferring a partitioned card so that a host with several MMC devices resolves as it did before. --- src/seedsigner/views/microsd_views.py | 25 +++++++++++++---- tests/test_microsd_device_guard.py | 40 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index f05e02244..097e82701 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -20,13 +20,26 @@ def find_sd_card_device(): + """Return the device node of the inserted MicroSD card, or None. + + A card counts as present once its disk node exists, whether or not it + carries a partition table. A brand-new card has none, and neither does one + this tool just zero-wiped, so requiring a partition would make the wipe and + flash tools blind to exactly the cards they are meant to work on. A + partitioned card still wins, so a host with more than one MMC device keeps + resolving to the same node as before. + """ import re - for device in os.listdir("/sys/block"): - if device.startswith("mmcblk") and re.fullmatch(r'mmcblk\d+', device): - partitions = os.listdir(f"/sys/block/{device}") - if any(p.startswith(device + "p") for p in partitions): - return f"/dev/{device}" - return None + blank = None + for device in sorted(os.listdir("/sys/block")): + if not re.fullmatch(r'mmcblk\d+', device): + continue + partitions = os.listdir(f"/sys/block/{device}") + if any(p.startswith(device + "p") for p in partitions): + return f"/dev/{device}" + if blank is None: + blank = f"/dev/{device}" + return blank def refuse_without_card(view: View, text: str) -> Destination: diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index 51af87513..0582ed26f 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -209,3 +209,43 @@ def test_a_present_card_is_flashed(self, monkeypatch): assert_dd_targets(commands, SD_DEV) assert destination.View_cls is MainMenuView + + +class TestBlankCardIsStillACard(BaseTest): + """A card with no partition table is in the slot; the tool zeroed it itself.""" + + def fake_sysfs(self, layout): + real_listdir = os.listdir + + def listdir(path): + # Only sysfs is faked: other threads keep reading real directories + # while this test runs, and they must not see this layout. + if path == "/sys/block": + return list(layout) + device = str(path).rsplit("/", 1)[-1] + if str(path).startswith("/sys/block/") and device in layout: + return layout[device] + return real_listdir(path) + + return listdir + + @pytest.mark.parametrize( + "layout,expected", + [ + ({"mmcblk0": ["mmcblk0p1", "size"]}, "/dev/mmcblk0"), + # Freshly zero-wiped or brand-new: present, but no partition table + ({"mmcblk0": ["size", "removable"]}, "/dev/mmcblk0"), + ({}, None), + ({"sda": ["sda1"]}, None), + ], + ) + def test_detection(self, monkeypatch, layout, expected): + monkeypatch.setattr(os, "listdir", self.fake_sysfs(layout)) + + assert microsd_views.find_sd_card_device() == expected + + def test_a_partitioned_card_wins_over_a_blank_one(self, monkeypatch): + layout = {"mmcblk0": ["size"], "mmcblk1": ["mmcblk1p1", "size"]} + monkeypatch.setattr(os, "listdir", self.fake_sysfs(layout)) + + assert microsd_views.find_sd_card_device() == "/dev/mmcblk1" From e9f1fa7e261137c36f34796395ef0e9419f26a3d Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 20 Sep 2026 03:44:56 +0100 Subject: [PATCH 04/12] fix: refuse a checksum when the read failed The verify view ran dd with check=False and then hashed /tmp/img.img regardless. That file still holds whatever the last read or flash put there, so a card that could not be read was reported with the previous image's checksum, up to a confident "Matched Checksum". Inspect dd's exit status and report that the card could not be read. --- src/seedsigner/views/microsd_views.py | 18 +++++++++++++++++- tests/test_microsd_device_guard.py | 25 ++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 097e82701..bb413fb0c 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -294,7 +294,23 @@ def run(self): dd_cmd = ["dd", f"if={microsd_dev}", "of=/tmp/img.img", "bs=1M", "count=26"] if platform.uname()[1] != "seedsigner-os": dd_cmd = ["sudo"] + dd_cmd - run(dd_cmd, check=False) + read = run(dd_cmd, capture_output=True, text=True) + logger.info(read) + + if read.returncode != 0: + # /tmp/img.img still holds whatever the last read or flash left + # there, so hashing it now would report a checksum for that image + # instead of for this card. + self.loading_screen.stop() + self.run_screen( + WarningScreen, + title="Error", + status_headline=None, + text="Could not read the MicroSD card.", + show_back_button=False, + button_data=[ButtonOption("Continue")] + ) + return Destination(MainMenuView) data = run(["sha256sum", "/tmp/img.img"], capture_output=True, text=True) logger.info(data) diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index 0582ed26f..8e479d4df 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -56,7 +56,7 @@ def stop(self): pass -def patch_environment(monkeypatch, commands, on_seedsigner_os=True): +def patch_environment(monkeypatch, commands, on_seedsigner_os=True, dd_returncode=0): """Make a MicroSD View runnable headlessly and record the commands it runs.""" hostname = "seedsigner-os" if on_seedsigner_os else "testhost" monkeypatch.setattr( @@ -80,6 +80,11 @@ def fake_run(cmd, *args, **kwargs): return SimpleNamespace(stdout="", stderr="", returncode=0) if cmd[0] == "sha256sum": return SimpleNamespace(stdout="0" * 64 + " /tmp/img.img\n", stderr="", returncode=0) + if dd_returncode != 0 and "dd" in cmd: + return SimpleNamespace( + stdout="", stderr="dd: /dev/mmcblk0: Input/output error\n", + returncode=dd_returncode, + ) return SimpleNamespace(stdout="", stderr=DD_OK, returncode=0) monkeypatch.setattr("subprocess.run", fake_run) @@ -249,3 +254,21 @@ def test_a_partitioned_card_wins_over_a_blank_one(self, monkeypatch): monkeypatch.setattr(os, "listdir", self.fake_sysfs(layout)) assert microsd_views.find_sd_card_device() == "/dev/mmcblk1" + + +class TestVerifyReportsAFailedRead(BaseTest): + """A checksum is only meaningful if dd actually read the card.""" + + def test_failed_dd_is_not_reported_as_a_checksum(self, monkeypatch): + commands = [] + patch_environment(monkeypatch, commands, dd_returncode=1) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + + view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDVerifyView) + + destination = view.run() + + assert not screens.showed("Matched Checksum") + assert not screens.showed("Unfamilliar Checksum") + assert screens.showed("Could not read") + assert destination.View_cls is MainMenuView From 01abf83e89d2918e225ba2ae0f238f95db01c733 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 20 Sep 2026 04:17:00 +0100 Subject: [PATCH 05/12] test: stop the patches the menu walk starts The MicroSD and GPG before_run helpers start mock.patch objects and store them on the View, which nothing ever stops, so the rest of the pytest session ran with find_sd_card_device() patched to return "/dev/test" and resolve_microsd_images_dir() pointing at a temp dir. Any later test of the real detector passed or failed on the mock, and every temp dir the file created stayed on disk after the run. Register both in the module and undo them in teardown_method, so a helper that is handed a View rather than the test still gets cleaned up. --- tests/test_flows_menu_navigation.py | 42 +++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/tests/test_flows_menu_navigation.py b/tests/test_flows_menu_navigation.py index 29368fc8d..a1e46c689 100644 --- a/tests/test_flows_menu_navigation.py +++ b/tests/test_flows_menu_navigation.py @@ -17,6 +17,23 @@ from seedsigner.views import scan_views, seed_views, tools_views, settings_views +# The before_run helpers below are handed a View, not the test, so anything they +# start is registered here for teardown_method() to undo. A patcher left running +# leaks into every later test in the session -- a stale +# find_sd_card_device() -> "/dev/test" made the real detector untestable -- and a +# temp dir left behind is never cleaned up at all. +_STARTED_PATCHERS = [] +_TEMP_DIRS = [] + + +def _temp_dir(prefix: str) -> Path: + """A temp dir that teardown_method() removes.""" + import tempfile + + path = Path(tempfile.mkdtemp(prefix=prefix)) + _TEMP_DIRS.append(path) + return path + def _patch_scan_view_decoder(view): """Prevent ScanView subclasses from processing mock decoder data. @@ -47,7 +64,7 @@ def _patch_microsd_child(view): ] for p in patchers: p.start() - view._microsd_patchers = patchers + _STARTED_PATCHERS.extend(patchers) # Inject a seed so FlashView's WarningScreen is shown (BACK exits early) if not view.controller.storage.seeds: @@ -64,16 +81,15 @@ def _patch_gpg_verify_file(view): at a temp dir containing one dummy file, so the View reaches its ButtonListScreen where BACK exits cleanly. """ - import tempfile - - tmpdir = Path(tempfile.mkdtemp(prefix="gpg_verify_test_")) + tmpdir = _temp_dir("gpg_verify_test_") (tmpdir / "dummy.bin").write_bytes(b"test") - view._gpg_verify_patchers = [ + patchers = [ patch("seedsigner.views.gpg_views.resolve_microsd_images_dir", return_value=tmpdir), ] - for p in view._gpg_verify_patchers: + for p in patchers: p.start() + _STARTED_PATCHERS.extend(patchers) view.controller.gpg_keys_imported = True @@ -219,6 +235,10 @@ def teardown_method(self): p = getattr(self, attr, None) if p is not None: p.stop() + while _STARTED_PATCHERS: + _STARTED_PATCHERS.pop().stop() + while _TEMP_DIRS: + shutil.rmtree(_TEMP_DIRS.pop(), ignore_errors=True) super().teardown_method() @@ -794,7 +814,7 @@ def test_smartcard_satodime_claim_finalises_with_matching_microsd_backup(self, m connector = _patch_satodime_connector(monkeypatch) connector.setup_done = False # unclaimed card -> plain claim flow - microsd_dir = use_microsd(monkeypatch, Path(tempfile.mkdtemp(prefix="satodime_backup_test_"))) + microsd_dir = use_microsd(monkeypatch, _temp_dir("satodime_backup_test_")) # The mock's card_setup mints unlock_secret=list(range(20)) for this UID. card_id = seedkeeper_utils.satodime_card_id(connector) @@ -829,7 +849,7 @@ def test_smartcard_satodime_reshow_done_with_matching_microsd_backup(self, monke connector = _patch_satodime_connector(monkeypatch) card_id = seedkeeper_utils.satodime_card_id(connector) - microsd_dir = use_microsd(monkeypatch, Path(tempfile.mkdtemp(prefix="satodime_backup_test_"))) + microsd_dir = use_microsd(monkeypatch, _temp_dir("satodime_backup_test_")) payload = seedkeeper_utils.format_satodime_unlock_payload(card_id, list(range(20))) (microsd_dir / seedkeeper_utils.satodime_unlock_backup_filename(card_id)).write_text(payload, encoding="utf-8") @@ -1208,7 +1228,7 @@ def test_diy_install_applet_returns_to_diy_menu(self): ToolsSmartcardMenuView, ToolsSatochipDIYView, ToolsDIYInstallAppletView, ) - cap_dir = Path(tempfile.mkdtemp(prefix="javacard_cap_test_")) / "javacard-cap" + cap_dir = _temp_dir("javacard_cap_test_") / "javacard-cap" cap_dir.mkdir() (cap_dir / "Satochip.cap").write_bytes(b"test") @@ -1238,9 +1258,9 @@ def test_gpg_import_key_back_leaves_the_gpg_menu(self): if shutil.which("gpg") is None or importlib.util.find_spec("pgpy") is None: pytest.skip("gpg binary and/or pgpy not available") - images_dir = Path(tempfile.mkdtemp(prefix="gpg_import_test_")) + images_dir = _temp_dir("gpg_import_test_") (images_dir / "pubkey.asc").write_text("not a real key") - gnupg_home = Path(tempfile.mkdtemp(prefix="gpg_import_home_")) + gnupg_home = _temp_dir("gpg_import_home_") with ExitStack() as stack: # Keep the real `gpg --import` away from the host's keyring. From 6923eb7809ab7e710752bcc1e6ce1a95aea5b083 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Mon, 21 Sep 2026 09:47:42 +0100 Subject: [PATCH 06/12] fix: list and copy images from one folder Flash Image listed a hard-coded folder and copied from another spelling of it. On a board whose images live anywhere else -- a Luckfox with no card, writing to /userdata; a card on an alternate mount -- it listed images it then could not find, or listed nothing it could flash. It also ran an unguarded os.listdir. A stock card has no images folder, so three taps from the main menu ended on the generic error screen with a FileNotFoundError, and a folder that exists but is empty crashed too, on an IndexError from a button list with no buttons. Resolve the folder once, in flash_images_dir(): the data directory every other file tool uses on SeedSigner OS, the boot partition on a manual Raspberry Pi OS build. List from it and copy from it, and say so on a warning screen when there is nothing there. --- src/seedsigner/views/microsd_views.py | 51 +++++++++++++--- tests/test_microsd_device_guard.py | 4 ++ tests/test_microsd_flash_images_dir.py | 82 ++++++++++++++++++++++++++ tests/test_microsd_os_marker.py | 16 +++++ 4 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 tests/test_microsd_flash_images_dir.py create mode 100644 tests/test_microsd_os_marker.py diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index bb413fb0c..e7a91e674 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -4,6 +4,7 @@ import logging import os import platform +from pathlib import Path from gettext import gettext as _ from seedsigner.gui.screens import ( @@ -12,8 +13,10 @@ LargeIconStatusScreen, WarningScreen, ) +from seedsigner.gui.components import SeedSignerIconConstants from seedsigner.gui.screens.screen import ButtonOption, LoadingScreenThread -from seedsigner.hardware.microsd import MicroSD +from seedsigner.hardware.microsd import MicroSD, resolve_microsd_images_dir +from seedsigner.models.settings import Settings from seedsigner.views.view import View, Destination, BackStackView, MainMenuView logger = logging.getLogger(__name__) @@ -105,6 +108,22 @@ def run(self): return Destination(ToolsMicroSDWipeRandomView) +def flash_images_dir() -> Path: + """ + Where Flash Image finds images: one answer, for listing and for copying. + + It listed one hard-coded folder and copied from another spelling of it, so + a board whose images live anywhere else -- a Luckfox with no card, writing + to /userdata; a card on an alternate mount -- listed images it could not + then find. SeedSigner OS resolves the data directory the way every other + file tool does; a manual Raspberry Pi OS build keeps the boot partition. + """ + if Settings.is_seedsigner_os(): + return resolve_microsd_images_dir() + return Path("/boot/microsd-images") + + + class ToolsMicroSDFlashView(View): def run(self): from subprocess import run @@ -123,10 +142,28 @@ def run(self): if ret == RET_CODE__BACK_BUTTON: return Destination(BackStackView) - if platform.uname()[1] == "seedsigner-os": - microsd_images = os.listdir('/mnt/microsd/microsd-images/') - else: - microsd_images = os.listdir('/boot/microsd-images/') + images_dir = flash_images_dir() + # A stock card has no images folder, and one that exists may be empty. + # Both used to leave the flow through the generic error screen -- a + # FileNotFoundError, then an IndexError from a button list with nothing + # in it -- which made the feature look broken rather than empty. + try: + microsd_images = os.listdir(images_dir) + except OSError as e: + logger.info("No images to flash in %s: %s", images_dir, e) + microsd_images = [] + + if not microsd_images: + self.run_screen( + WarningScreen, + title=_("Flash Image"), + status_icon_name=SeedSignerIconConstants.WARNING, + status_headline=None, + text=_("No images found on the MicroSD card."), + show_back_button=False, + button_data=[ButtonOption(_("OK"))], + ) + return Destination(BackStackView) microsd_images_buttons = [] for file in microsd_images: @@ -146,7 +183,7 @@ def run(self): logger.info("Selected: %s", microsd_image) if platform.uname()[1] == "seedsigner-os": - image_path = os.path.join('/mnt/microsd/microsd-images', microsd_image) + image_path = os.path.join(images_dir, microsd_image) data = run(['cp', image_path, '/tmp/img.img'], capture_output=True, text=True) logger.info(data) if len(data.stderr) > 1: @@ -242,7 +279,7 @@ def run(self): if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") - image_path = os.path.join('/boot/microsd-images', microsd_image) + image_path = os.path.join(images_dir, microsd_image) run(['cp', image_path, '/tmp/img.img'], check=False) run(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}'], check=False) diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index 8e479d4df..e264d5b99 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -1,5 +1,6 @@ # pylint: disable=missing-function-docstring import os +import pathlib from types import SimpleNamespace import pytest @@ -63,6 +64,9 @@ def patch_environment(monkeypatch, commands, on_seedsigner_os=True, dd_returncod "platform.uname", lambda: ("Linux", hostname, "", "", "", "") ) monkeypatch.setattr(microsd_views, "LoadingScreenThread", DummyLoadingScreenThread) + # Flash Image resolves where its images live; keep that off the host's disk. + monkeypatch.setattr(microsd_views, "flash_images_dir", + lambda: pathlib.Path("/test/microsd-images")) real_listdir = os.listdir diff --git a/tests/test_microsd_flash_images_dir.py b/tests/test_microsd_flash_images_dir.py new file mode 100644 index 000000000..15c1f5fdc --- /dev/null +++ b/tests/test_microsd_flash_images_dir.py @@ -0,0 +1,82 @@ +"""Flash Image has to list and copy from the same place, and say when it is empty. + +It listed one hard-coded folder and copied from another spelling of it, so any +board whose images live elsewhere -- a Luckfox writing to /userdata, a card on +an alternate mount -- showed images it then could not find. A stock card has no +images folder at all, and an empty one crashed on a button list with no buttons. +""" +# Must import base before any seedsigner modules +from base import BaseTest + +import subprocess + +from seedsigner.gui.screens import RET_CODE__BACK_BUTTON, WarningScreen +from seedsigner.models.settings import Settings +from seedsigner.views import microsd_views +from seedsigner.views.view import BackStackView, Destination + + +class _Done: + stdout = "" + stderr = "" + returncode = 0 + + +class TestFlashImagesDirectory(BaseTest): + + def _run(self, monkeypatch, images_dir, answers): + monkeypatch.setattr(microsd_views, "flash_images_dir", lambda: images_dir) + commands = [] + monkeypatch.setattr( + subprocess, "run", lambda cmd, *a, **kw: commands.append(cmd) or _Done() + ) + view = microsd_views.ToolsMicroSDFlashView() + screens = [] + + def fake_screen(Screen_cls, **kw): + screens.append(Screen_cls) + return answers.pop(0) + + monkeypatch.setattr(view, "run_screen", fake_screen) + return view.run(), screens, commands + + def test_the_image_is_copied_from_the_folder_it_was_listed_from(self, monkeypatch, tmp_path): + (tmp_path / "release.img").write_bytes(b"\x00" * 16) + monkeypatch.setattr(Settings, "is_seedsigner_os", classmethod(lambda cls: True)) + monkeypatch.setattr("platform.uname", + lambda: ("Linux", "seedsigner-os", "", "", "", "")) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: "/dev/mmcblk0") + + # Pick the image, then back out at "Insert MicroSD to be Flashed". + _, _, commands = self._run(monkeypatch, tmp_path, [0, RET_CODE__BACK_BUTTON]) + + copies = [c for c in commands if c and c[0] == "cp"] + assert copies, commands + assert copies[0][1] == str(tmp_path / "release.img") + + def test_a_missing_images_folder_is_a_message(self, monkeypatch, tmp_path): + destination, screens, _ = self._run(monkeypatch, tmp_path / "absent", [0]) + + assert WarningScreen in screens + assert isinstance(destination, Destination) + assert destination.View_cls is BackStackView + + def test_an_empty_images_folder_is_a_message(self, monkeypatch, tmp_path): + destination, screens, _ = self._run(monkeypatch, tmp_path, [0]) + + assert WarningScreen in screens + assert destination.View_cls is BackStackView + + +class TestWhereImagesLive(BaseTest): + + def test_seedsigner_os_uses_the_resolved_data_directory(self, monkeypatch, tmp_path): + monkeypatch.setattr(Settings, "is_seedsigner_os", classmethod(lambda cls: True)) + monkeypatch.setattr(microsd_views, "resolve_microsd_images_dir", lambda: tmp_path) + + assert microsd_views.flash_images_dir() == tmp_path + + def test_raspberry_pi_os_keeps_the_boot_partition(self, monkeypatch): + monkeypatch.setattr(Settings, "is_seedsigner_os", classmethod(lambda cls: False)) + + assert str(microsd_views.flash_images_dir()) == "/boot/microsd-images" diff --git a/tests/test_microsd_os_marker.py b/tests/test_microsd_os_marker.py new file mode 100644 index 000000000..0934fe42f --- /dev/null +++ b/tests/test_microsd_os_marker.py @@ -0,0 +1,16 @@ +"""SeedSigner OS is recognised by its marker, not by the hostname.""" +# Must import base before any seedsigner modules +from base import BaseTest + +from seedsigner.models.settings import Settings +from seedsigner.views import microsd_views + + +class TestTheOSIsTheMarkerNotTheHostname(BaseTest): + + def test_flash_images_dir_follows_the_os_marker(self, monkeypatch, tmp_path): + monkeypatch.setattr("platform.uname", lambda: ("Linux", "luckfox", "", "", "", "")) + monkeypatch.setattr(Settings, "is_seedsigner_os", classmethod(lambda cls: True)) + monkeypatch.setattr(microsd_views, "resolve_microsd_images_dir", lambda: tmp_path) + + assert microsd_views.flash_images_dir() == tmp_path From f7aa402fed6d268d1b269e5f5ff487c71a75e7e9 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Mon, 21 Sep 2026 14:32:09 +0100 Subject: [PATCH 07/12] fix: never take eMMC for the MicroSD card find_sd_card_device() took the first mmcblk device with a partition table, and the raw-write tools dd straight onto whatever it returns. eMMC is an mmcblk device too, and on a board that has it, it is the board's own storage -- partitioned, so it won over the card in the slot. Accepting a blank card as present, as this branch does, only widened that. The kernel says which is which in device/type: "SD" for a card, "MMC" for eMMC, "SDIO" for anything else on the bus. Only an SD card is a candidate now, and a device that will not say what it is is not one: refusing to write is the answer that cannot brick a board. --- src/seedsigner/views/microsd_views.py | 15 +++++++++ tests/test_microsd_card_is_sd.py | 47 +++++++++++++++++++++++++++ tests/test_microsd_device_guard.py | 3 ++ 3 files changed, 65 insertions(+) create mode 100644 tests/test_microsd_card_is_sd.py diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index e7a91e674..5373a2c76 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -22,6 +22,15 @@ logger = logging.getLogger(__name__) +def _mmc_device_type(device: str) -> str | None: + """The MMC card type the kernel reports: "SD", "MMC" (eMMC) or "SDIO".""" + try: + with open(f"/sys/block/{device}/device/type") as f: + return f.read().strip() + except OSError: + return None + + def find_sd_card_device(): """Return the device node of the inserted MicroSD card, or None. @@ -37,6 +46,12 @@ def find_sd_card_device(): for device in sorted(os.listdir("/sys/block")): if not re.fullmatch(r'mmcblk\d+', device): continue + # eMMC is an mmcblk device too, and on a board that has it, it is the + # board's own storage -- partitioned, so it used to win. Only a device + # the kernel calls an SD card is ever a candidate, and one that will not + # say what it is is not. + if _mmc_device_type(device) != "SD": + continue partitions = os.listdir(f"/sys/block/{device}") if any(p.startswith(device + "p") for p in partitions): return f"/dev/{device}" diff --git a/tests/test_microsd_card_is_sd.py b/tests/test_microsd_card_is_sd.py new file mode 100644 index 000000000..0cb906099 --- /dev/null +++ b/tests/test_microsd_card_is_sd.py @@ -0,0 +1,47 @@ +"""Only an SD card is ever the MicroSD card: eMMC is the board's own storage.""" +# Must import base before any seedsigner modules +from base import BaseTest + +import os + +from seedsigner.views import microsd_views + + +class TestOnlyAnSDCardIsEverChosen(BaseTest): + """ + eMMC is an mmcblk device too, and on a board that has it, it is the board's + own storage -- partitioned, so the old "first partitioned mmcblk" rule chose + it ahead of the card in the slot. The kernel says which is which in + device/type: "SD" for a card, "MMC" for eMMC. + """ + + def _sysfs(self, monkeypatch, layout, types): + real_listdir = os.listdir + + def listdir(path): + if path == "/sys/block": + return list(layout) + device = str(path).rsplit("/", 1)[-1] + if str(path).startswith("/sys/block/") and device in layout: + return layout[device] + return real_listdir(path) + + monkeypatch.setattr(os, "listdir", listdir) + monkeypatch.setattr(microsd_views, "_mmc_device_type", lambda d: types.get(d)) + + def test_the_card_is_chosen_over_a_partitioned_emmc(self, monkeypatch): + self._sysfs(monkeypatch, + {"mmcblk0": ["mmcblk0p1", "mmcblk0p2"], "mmcblk1": ["size"]}, + {"mmcblk0": "MMC", "mmcblk1": "SD"}) + + assert microsd_views.find_sd_card_device() == "/dev/mmcblk1" + + def test_emmc_alone_is_never_a_card(self, monkeypatch): + self._sysfs(monkeypatch, {"mmcblk0": ["mmcblk0p1"]}, {"mmcblk0": "MMC"}) + + assert microsd_views.find_sd_card_device() is None + + def test_a_device_that_will_not_say_what_it_is_is_not_chosen(self, monkeypatch): + self._sysfs(monkeypatch, {"mmcblk0": ["mmcblk0p1"]}, {}) + + assert microsd_views.find_sd_card_device() is None diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index e264d5b99..50fc3aaa9 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -250,12 +250,15 @@ def listdir(path): ) def test_detection(self, monkeypatch, layout, expected): monkeypatch.setattr(os, "listdir", self.fake_sysfs(layout)) + # These are all SD cards; telling one from eMMC is covered separately. + monkeypatch.setattr(microsd_views, "_mmc_device_type", lambda d: "SD") assert microsd_views.find_sd_card_device() == expected def test_a_partitioned_card_wins_over_a_blank_one(self, monkeypatch): layout = {"mmcblk0": ["size"], "mmcblk1": ["mmcblk1p1", "size"]} monkeypatch.setattr(os, "listdir", self.fake_sysfs(layout)) + monkeypatch.setattr(microsd_views, "_mmc_device_type", lambda d: "SD") assert microsd_views.find_sd_card_device() == "/dev/mmcblk1" From fc2fd7158f716154cb1b07ae2e43f120b48e38fc Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Mon, 21 Sep 2026 14:32:09 +0100 Subject: [PATCH 08/12] fix: unmount the card before any raw write Flash and Wipe (Zero) unmounted two hard-coded paths, /mnt/diy and /mnt/microsd, and only when the hostname read "seedsigner-os". A Luckfox mounts its card at /mnt/sdcard and does not carry that hostname, so a flash went straight under a live filesystem. Wipe (Random) unmounted nothing at all. Ask the kernel instead, right before the write, and refuse -- nothing written -- if anything will not go. A mount is on the card when its device number in /proc/self/mountinfo is the card's or a partition's. The name it was mounted by does not say: a root the kernel mounted itself is listed as /dev/root, which is how Raspberry Pi OS without an initramfs shows the card it booted from. A card is held by more than the filesystems mounted from it: SeedSigner OS loop-mounts diy-tools.squashfs from the card at /mnt/diy, an overlay with a layer on the card keeps a copy of that mount of its own, and a mount can sit inside or on top of a card mountpoint. Those come off first, then the card's own mounts, deepest first. Nothing is unmounted and the write is refused if mountinfo or the card's sysfs entries cannot be read, a loop device will not say which file it reads, device-mapper or md is built on the card, or the card holds the root filesystem. The mount table does not show everything that uses the card: swap, an overlay root whose lower layer was mounted before the root was switched, a btrfs root under a device number of its own. So the kernel is also asked outright, by opening for exclusive use, which it refuses while anything has the device: each partition no mount shows, before anything is unmounted, and the whole card, once everything is. The open is a short Python script, run with -c in a child interpreter under the same su prefix as umount, because a dev host runs the app as a user who cannot open the device itself. Each device goes to it with the major:minor sysfs gives it. The kernel honours O_EXCL without O_CREAT only for a block device, so a regular file at the card's path would open without complaint: the script fstat()s what it opened and fails unless it is a block device with that number. Loop mounts come off with umount -d, which frees the loop device in the same command, so its file on the card is closed before the card's own filesystem goes. busybox and util-linux both take -d, and util-linux skips it for an autoclear loop, which is what either mount sets up. losetup -d as a second step would need its own answer for a device autoclear has already freed. On a Raspberry Pi OS host the image is copied before the card is unmounted: it comes from /boot, which can be on the card. The same hostname check decided whether dd needed sudo, so SeedSigner OS is now recognised in this file the way the rest of the codebase recognises it, by Settings.is_seedsigner_os(). --- src/seedsigner/views/microsd_views.py | 325 ++++++++++++-- tests/test_microsd_device_guard.py | 178 +++++++- tests/test_microsd_unmount_before_write.py | 486 +++++++++++++++++++++ 3 files changed, 960 insertions(+), 29 deletions(-) create mode 100644 tests/test_microsd_unmount_before_write.py diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 5373a2c76..f43ecffc8 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -3,9 +3,10 @@ ****************************************************************************""" import logging import os -import platform +import sys from pathlib import Path from gettext import gettext as _ +from typing import NamedTuple from seedsigner.gui.screens import ( RET_CODE__BACK_BUTTON, @@ -21,6 +22,32 @@ logger = logging.getLogger(__name__) +# What the kernel says is mounted, and which block devices those are. +# Tests point these at a made-up tree. +PROC_MOUNTINFO = "/proc/self/mountinfo" +SYS_BLOCK = "/sys/block" + +# Opens each device named on the command line for exclusive use, as mount and +# swapon do. The kernel refuses that with EBUSY while a filesystem, swap, +# device-mapper or md has the device -- or, for a whole disk, any partition of +# it -- whether or not any mount this process can see says so. It does that +# only for a block device, though: O_EXCL without O_CREAT is ignored for +# anything else, so a regular file at the card's path opens without complaint. +# So each device comes with the "major:minor" sysfs gives it, and what was +# opened must be the block device with that number. +EXCLUSIVE_OPEN = ( + "import os, stat, sys\n" + "args = sys.argv[1:]\n" + "for device, devnum in zip(args[::2], args[1::2], strict=True):\n" + " fd = os.open(device, os.O_RDONLY | os.O_EXCL)\n" + " st = os.fstat(fd)\n" + " os.close(fd)\n" + " if not stat.S_ISBLK(st.st_mode):\n" + " sys.exit(f'{device} is not a block device')\n" + " if f'{os.major(st.st_rdev)}:{os.minor(st.st_rdev)}' != devnum:\n" + " sys.exit(f'{device} is not block device {devnum}')\n" +) + def _mmc_device_type(device: str) -> str | None: """The MMC card type the kernel reports: "SD", "MMC" (eMMC) or "SDIO".""" @@ -60,6 +87,255 @@ def find_sd_card_device(): return blank +def _unescape(field: str) -> str: + """Undo the octal escapes mountinfo uses for spaces and the like: \\040""" + import re + return re.sub(r"\\([0-7]{3})", lambda m: chr(int(m.group(1), 8)), field) + + +def _read_sysfs(path: str) -> str: + with open(path) as f: + return f.read().rstrip("\n") + + +def _card_blocks(device: str) -> list[str]: + """The sysfs directories of device and of every partition on it.""" + name = os.path.basename(device) + disk = os.path.join(SYS_BLOCK, name) + return [disk] + [ + os.path.join(disk, entry) + for entry in sorted(os.listdir(disk)) + if entry.startswith(name) + ] + + +def _loop_backing_files() -> dict[str, str]: + """ + The file each bound loop device reads, keyed by its "major:minor". + + Raises OSError when one will not say: its mount may be on the card, and + guessing that it is not would write under it. + """ + loops = {} + for name in os.listdir(SYS_BLOCK): + block = os.path.join(SYS_BLOCK, name) + # Only a loop device that is bound to a file has this directory. + if os.path.isdir(os.path.join(block, "loop")): + devnum = _read_sysfs(os.path.join(block, "dev")) + loops[devnum] = _read_sysfs(os.path.join(block, "loop", "backing_file")) + return loops + + +class _Mount(NamedTuple): + id: str + parent: str + devnum: str + mountpoint: str + fstype: str + source: str + options: str + + +def _parse_mountinfo(mountinfo: str) -> list[_Mount]: + """ + Each line of /proc/self/mountinfo as a _Mount. + + Raises ValueError on a line that is not mountinfo. + """ + mounts = [] + for line in mountinfo.splitlines(): + # ID, parent ID, major:minor, root, mountpoint, options, any optional + # fields, "-", then filesystem type, source and superblock options. + # One space apart each, so an empty source is still a field. + fields = line.split(" ") + separator = fields.index("-", 6) + if len(fields) < separator + 4: + raise ValueError(f"not a mountinfo line: {line!r}") + mounts.append(_Mount( + *fields[:3], + mountpoint=_unescape(fields[4]), + fstype=fields[separator + 1], + source=_unescape(fields[separator + 2]), + options=fields[separator + 3], + )) + return mounts + + +def _overlay_layers(options: str) -> list[str]: + """ + The directories an overlay is made of, from its superblock options: + lowerdir (colon-separated), lowerdir+, datadir+, upperdir and workdir. + """ + layers = [] + for option in options.split(","): + name, _, value = option.partition("=") + if name == "lowerdir": + layers += value.split(":") + elif name in ("lowerdir+", "datadir+", "upperdir", "workdir"): + layers.append(value) + return [_unescape(layer) for layer in layers] + + +def _mounts_holding(device: str, mounts: list[_Mount], card_devnums: set[str], + loops: dict[str, str]) -> list[tuple[str, bool]]: + """ + Every mount that holds device, as (mountpoint, is a loop mount), in + unmount order. + + A mount is on the card when its device number is the card's or one of its + partitions'. The name it was mounted by does not say: a root the kernel + mounted itself is listed as /dev/root, whatever it is on. The name still + counts too, since btrfs gives its mounts a device number of their own. A + loop device reading the card or a partition of it is on the card as well. + + A filesystem on the card is held by more than its own mount. SeedSigner OS + loop-mounts diy-tools.squashfs from the card at /mnt/diy, and while that is + up, /mnt/microsd will not come off. An overlay holds its layers through a + copy of their mounts of its own, so /mnt/microsd comes off under one and + the card stays in use all the same. So anything that sits on a listed + mount is listed too: mounted inside it or on top of it, or a loop device + or an overlay reading a path on it. Each mount is listed after everything + that sits on it, so nested ones come before their parents. + """ + import re + card = re.compile(re.escape(device) + r"(p\d+)?") + + def on_card(mount: _Mount) -> bool: + backing_file = loops.get(mount.devnum) + return bool( + mount.devnum in card_devnums + or card.fullmatch(mount.source) + or (backing_file is not None and card.fullmatch(backing_file)) + ) + + def reads(mount: _Mount) -> list[str]: + """The paths outside its own mountpoint a mount is made from.""" + if mount.devnum in loops: + return [loops[mount.devnum]] + if mount.fstype == "overlay": + return _overlay_layers(mount.options) + return [] + + def covers(mountpoint: str, path: str) -> bool: + return path == mountpoint or path.startswith(mountpoint.rstrip("/") + "/") + + def sits_on(upper: _Mount, lower: _Mount) -> bool: + return upper.parent == lower.id or any( + covers(lower.mountpoint, path) for path in reads(upper) + ) + + order = [] + seen = set() + + def take_off(i: int): + seen.add(i) + for j in range(len(mounts)): + if j not in seen and sits_on(mounts[j], mounts[i]): + take_off(j) + order.append((mounts[i].mountpoint, mounts[i].devnum in loops)) + + for i, mount in enumerate(mounts): + if i not in seen and on_card(mount): + take_off(i) + return order + + +def _unclaimed(devices: dict[str, str]) -> bool: + """ + True when the kernel lets every one of devices be opened exclusively, and + each is the block device with the "major:minor" it maps to. + """ + from subprocess import run + args = [arg for device, devnum in devices.items() for arg in (device, devnum)] + cmd = Settings.SU_COMMAND_PREFIX.split() + [sys.executable, "-c", EXCLUSIVE_OPEN] + args + data = run(cmd, capture_output=True, text=True) + logger.info(data) + return data.returncode == 0 + + +def unmount_card(device: str) -> bool: + """ + Unmount everything that holds device; True only once all of it has gone. + + The raw-write tools used to unmount two hard-coded paths, and only when the + hostname said SeedSigner OS. A Luckfox mounts its card at /mnt/sdcard, so a + flash went straight under a live filesystem, and Wipe (Random) unmounted + nothing at all. The kernel says what is mounted, and from which device, on + every board. When it cannot be asked the answer is False: a failed read is + not the same as nothing mounted. + + The mounts are not the whole answer, though. The kernel can be using the + card with no mount here to show for it: swap, the lower layer of an + overlay root mounted before the root was switched, a btrfs root listed as + /dev/root under a device number of its own, a mount in another namespace. + And a filesystem can outlive the umount of its mount. So the kernel is + asked as well, by opening for exclusive use: before anything comes off, + each partition no mount shows, and once everything has, the whole card. + Each must turn out to be the block device sysfs numbers it, too, or the + answer is about whatever else stands at its path. + """ + from subprocess import run + try: + with open(PROC_MOUNTINFO) as f: + mounts = _parse_mountinfo(f.read()) + blocks = _card_blocks(device) + devnums = { + os.path.basename(block): _read_sysfs(os.path.join(block, "dev")) + for block in blocks + } + built_on = [ + holder + for block in blocks + for holder in os.listdir(os.path.join(block, "holders")) + ] + holding = _mounts_holding(device, mounts, set(devnums.values()), _loop_backing_files()) + except (OSError, ValueError) as e: + logger.info("Cannot tell what holds %s: %s", device, e) + return False + + if built_on: + # device-mapper or md on the card: what is mounted is that device, and + # no umount frees the card underneath it. + logger.info("%s is in use by %s", device, built_on) + return False + + if any(mountpoint == "/" for mountpoint, _ in holding): + # Every mount sits under "/", so this would take the running system's + # /proc, /dev and the rest down with it. + logger.info("%s holds the root filesystem", device) + return False + + # A partition in use that no mount shows -- or the card itself, when it + # has none -- has nothing umount can take off. Asking before anything + # comes off leaves a card the system runs from exactly as it was. + disk = os.path.basename(device) + partitions = [name for name in devnums if name != disk] or [disk] + shown = {mount.devnum for mount in mounts} | {mount.source for mount in mounts} + unseen = { + f"/dev/{name}": devnums[name] for name in partitions + if devnums[name] not in shown and f"/dev/{name}" not in shown + } + if unseen and not _unclaimed(unseen): + logger.info("%s is in use by something no mount shows", device) + return False + + for mountpoint, is_loop in holding: + cmd = Settings.SU_COMMAND_PREFIX.split() + ["umount", mountpoint] + if is_loop: + # Free the loop device as well: while it stays bound it keeps its + # file open, and that file is on the card. + cmd.insert(-1, "-d") + data = run(cmd, capture_output=True, text=True) + logger.info(data) + if data.returncode != 0: + return False + + # umount can succeed and leave the filesystem up: an overlay keeps its own + # copy of each layer's mount, another mount namespace its own copy of the + # tree. Only the kernel can say the card is free. + return _unclaimed({device: devnums[disk]}) + + def refuse_without_card(view: View, text: str) -> Destination: """Report that nothing was done and return to the main menu. @@ -197,7 +473,7 @@ def run(self): microsd_image = microsd_images[selected_file_num] logger.info("Selected: %s", microsd_image) - if platform.uname()[1] == "seedsigner-os": + if Settings.is_seedsigner_os(): image_path = os.path.join(images_dir, microsd_image) data = run(['cp', image_path, '/tmp/img.img'], capture_output=True, text=True) logger.info(data) @@ -227,21 +503,15 @@ def run(self): if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") + if not unmount_card(microsd_dev): + return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was written.") + self.loading_screen = LoadingScreenThread(text="Flashing MicroSD\n\n\n\n\n\n") self.loading_screen.start() - # Unmount everything - if platform.uname()[1] == "seedsigner-os": - data = run(["umount", "/mnt/diy"], capture_output=True, text=True) - logger.info(data) - - if platform.uname()[1] == "seedsigner-os": - data = run(["umount", "/mnt/microsd"], capture_output=True, text=True) - logger.info(data) - # Zero the MicroSD first dd_cmd = ["dd", f"if=/dev/zero", f"of={microsd_dev}", "bs=1M", "count=26"] - if platform.uname()[1] != "seedsigner-os": + if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd data = run(dd_cmd, capture_output=True, text=True) @@ -290,12 +560,18 @@ def run(self): return Destination(MainMenuView) else: + # Copy first: the images are on /boot, and /boot can be on the + # card that is about to be unmounted. + image_path = os.path.join(images_dir, microsd_image) + run(['cp', image_path, '/tmp/img.img'], check=False) + microsd_dev = find_sd_card_device() if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") - image_path = os.path.join(images_dir, microsd_image) - run(['cp', image_path, '/tmp/img.img'], check=False) + if not unmount_card(microsd_dev): + return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was written.") + run(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}'], check=False) return Destination(MainMenuView) @@ -344,7 +620,7 @@ def run(self): self.loading_screen.start() dd_cmd = ["dd", f"if={microsd_dev}", "of=/tmp/img.img", "bs=1M", "count=26"] - if platform.uname()[1] != "seedsigner-os": + if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd read = run(dd_cmd, capture_output=True, text=True) logger.info(read) @@ -442,20 +718,14 @@ def run(self): if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was wiped.") + if not unmount_card(microsd_dev): + return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was wiped.") + self.loading_screen = LoadingScreenThread(text="Wiping MicroSD\n\n\n\n\n\n(This takes a while)") self.loading_screen.start() - # Unmount everything - if platform.uname()[1] == "seedsigner-os": - data = run(["umount", "/mnt/diy"], capture_output=True, text=True) - logger.info(data) - - if platform.uname()[1] == "seedsigner-os": - data = run(["umount", "/mnt/microsd"], capture_output=True, text=True) - logger.info(data) - dd_cmd = ["dd", f"if=/dev/zero", f"of={microsd_dev}", "bs=1M"] + wipesize_cmd_string.split() - if platform.uname()[1] != "seedsigner-os": + if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd data = run(dd_cmd, capture_output=True, text=True) @@ -546,11 +816,14 @@ def run(self): if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was wiped.") + if not unmount_card(microsd_dev): + return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was wiped.") + self.loading_screen = LoadingScreenThread(text="Wiping MicroSD\n\n\n\n\n\n(This takes a while)") self.loading_screen.start() dd_cmd = ["dd", f"if=/dev/urandom", f"of={microsd_dev}", "bs=1M"] + wipesize_cmd_string.split() - if platform.uname()[1] != "seedsigner-os": + if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd data = run(dd_cmd, capture_output=True, text=True) diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index 50fc3aaa9..56c204c80 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -1,6 +1,11 @@ # pylint: disable=missing-function-docstring +import errno +import itertools import os import pathlib +import stat +import subprocess +import sys from types import SimpleNamespace import pytest @@ -8,6 +13,7 @@ # Must import base before any seedsigner modules from base import BaseTest +from seedsigner.models.settings import Settings from seedsigner.views import microsd_views from seedsigner.views.view import MainMenuView @@ -64,6 +70,13 @@ def patch_environment(monkeypatch, commands, on_seedsigner_os=True, dd_returncod "platform.uname", lambda: ("Linux", hostname, "", "", "", "") ) monkeypatch.setattr(microsd_views, "LoadingScreenThread", DummyLoadingScreenThread) + # The views ask the OS marker, not the hostname. + monkeypatch.setattr(Settings, "is_seedsigner_os", + classmethod(lambda cls: on_seedsigner_os)) + # The kernel says nothing about the card unless a test sets it up with + # fake_kernel(), and a card nobody can describe is never written. + monkeypatch.setattr(microsd_views, "PROC_MOUNTINFO", os.devnull) + monkeypatch.setattr(microsd_views, "SYS_BLOCK", os.devnull) # Flash Image resolves where its images live; keep that off the host's disk. monkeypatch.setattr(microsd_views, "flash_images_dir", lambda: pathlib.Path("/test/microsd-images")) @@ -94,6 +107,162 @@ def fake_run(cmd, *args, **kwargs): monkeypatch.setattr("subprocess.run", fake_run) +# The device numbers the fake kernel gives the block devices tests mount by +# name. Any other source is a virtual filesystem, with a number of its own. +DEVNUMS = { + SD_DEV: "179:0", + f"{SD_DEV}p1": "179:1", + f"{SD_DEV}p2": "179:2", + "/dev/mmcblk1p1": "179:33", + "/dev/sda2": "8:2", + "/dev/dm-0": "254:0", + "/dev/loop0": "7:0", + "/dev/loop1": "7:1", + "/dev/loop2": "7:2", +} + +# A regular file standing where a device node should be +FILE = "file" + + +def fake_kernel(monkeypatch, tmp_path, mounts, loops=None, aliases=None, holders=None, + claimed=(), nodes=None): + """ + Point the views at a mountinfo and a /sys/block made up under tmp_path. + + mounts are /proc/mounts lines, oldest first. Each gets the device number + of its source, or of the device aliases names for it: the kernel calls a + root it mounted itself /dev/root, whatever it is on. SD_DEV has two + partitions. loops maps loopN to the file it reads, or to None when sysfs + will not say; holders maps a device on the card to those built on it. + + The kernel holds a device on the card while a mount of it is up, and holds + the devices in claimed whatever comes off: swap, a filesystem no mount + here shows, one an overlay keeps a copy of. Opening a held device for + exclusive use, or the whole card while a partition of it is held, fails + with EBUSY. + + /dev has a node for the card and for each partition, under the number + sysfs gives it. nodes changes what stands at a path: a block device with + another number, FILE, or None for nothing at all. The exclusive-open + check runs as written, against that /dev. + """ + devnums = dict(DEVNUMS) + for alias, device in (aliases or {}).items(): + devnums[alias] = DEVNUMS[device] + + def covers(mountpoint, path): + return path == mountpoint or path.startswith(mountpoint.rstrip("/") + "/") + + lines = [] + for i, mount in enumerate(mounts): + source, mountpoint, fstype, options = mount.split()[:4] + # Made on whichever earlier mount was on top at its path + parent = next( + (j for j in reversed(range(i)) if covers(mounts[j].split()[1], mountpoint)), + -1, + ) + devnum = devnums.get(source, f"0:{40 + i}") + lines.append( + f"{20 + i} {20 + parent} {devnum} / {mountpoint} rw shared:1" + f" - {fstype} {source} {options}\n" + ) + (tmp_path / "mountinfo").write_text("".join(lines)) + + sys_block = tmp_path / "block" + card = SD_DEV.rsplit("/", 1)[-1] + blocks = {SD_DEV: sys_block / card} + for n in (1, 2): + blocks[f"{SD_DEV}p{n}"] = sys_block / card / f"{card}p{n}" + for device, block in blocks.items(): + (block / "holders").mkdir(parents=True) + (block / "dev").write_text(DEVNUMS[device] + "\n") + for holder in (holders or {}).get(device, []): + (block / "holders" / holder).touch() + for loop, backing_file in (loops or {}).items(): + (sys_block / loop / "loop").mkdir(parents=True) + (sys_block / loop / "dev").write_text(DEVNUMS[f"/dev/{loop}"] + "\n") + if backing_file is not None: + (sys_block / loop / "loop" / "backing_file").write_text(backing_file + "\n") + + monkeypatch.setattr(microsd_views, "PROC_MOUNTINFO", str(tmp_path / "mountinfo")) + monkeypatch.setattr(microsd_views, "SYS_BLOCK", str(sys_block)) + + # Every mount that is up, as (mountpoint, the device on the card it holds + # or None), with the top one at each path last. + card_devices = {DEVNUMS[device]: device for device in blocks} + up = [ + (mount.split()[1].replace("\\040", " "), + card_devices.get(devnums.get(mount.split()[0]))) + for mount in mounts + ] + dev = {device: DEVNUMS[device] for device in blocks} + dev.update(nodes or {}) + run = subprocess.run + + def exclusive_open(script, argv): + """Run script the way `python -c script *argv` would, on the fake /dev.""" + held = [device for _, device in up if device] + list(claimed) + opened = {} + fds = itertools.count(1 << 20) + real_open, real_fstat, real_close = os.open, os.fstat, os.close + + def fake_open(path, flags, *args, **kwargs): + if not str(path).startswith(SD_DEV): + return real_open(path, flags, *args, **kwargs) + node = dev.get(path) + if node is None: + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path) + # O_EXCL without O_CREAT claims a block device, and anything else + # opens as if it had not been asked for. + if node != FILE and flags & os.O_EXCL and any( + path == other or SD_DEV in (path, other) for other in held + ): + raise OSError(errno.EBUSY, os.strerror(errno.EBUSY), path) + fd = next(fds) + opened[fd] = node + return fd + + def fake_fstat(fd, *args, **kwargs): + if fd not in opened: + return real_fstat(fd, *args, **kwargs) + if opened[fd] == FILE: + return SimpleNamespace(st_mode=stat.S_IFREG | 0o644, st_rdev=0) + major, minor = map(int, opened[fd].split(":")) + return SimpleNamespace(st_mode=stat.S_IFBLK | 0o660, + st_rdev=os.makedev(major, minor)) + + def fake_close(fd): + if opened.pop(fd, None) is None: + real_close(fd) + + with monkeypatch.context() as patched: + patched.setattr(os, "open", fake_open) + patched.setattr(os, "fstat", fake_fstat) + patched.setattr(os, "close", fake_close) + patched.setattr(sys, "argv", ["-c", *argv]) + try: + exec(script, {"__name__": "__main__"}) # pylint: disable=exec-used + except SystemExit as e: + if e.code not in (None, 0): + return SimpleNamespace(stdout="", stderr=f"{e.code}\n", returncode=1) + except Exception as e: # pylint: disable=broad-except + return SimpleNamespace(stdout="", stderr=f"{type(e).__name__}: {e}\n", + returncode=1) + return SimpleNamespace(stdout="", stderr="", returncode=0) + + def kernel_run(cmd, *args, **kwargs): + result = run(cmd, *args, **kwargs) + if "umount" in cmd: + # umount takes off the top mount at a path + del up[max(i for i, (mountpoint, _) in enumerate(up) if mountpoint == cmd[-1])] + elif "-c" in cmd and "O_EXCL" in cmd[cmd.index("-c") + 1]: + return exclusive_open(cmd[cmd.index("-c") + 1], cmd[cmd.index("-c") + 2:]) + return result + + monkeypatch.setattr("subprocess.run", kernel_run) + + def build_view(monkeypatch, view_cls): view = view_cls() screens = RecordingScreens() @@ -155,9 +324,10 @@ class TestCardInsertedAtThePrompt(BaseTest): microsd_views.ToolsMicroSDWipeRandomView, ], ) - def test_wipe_finds_a_card_inserted_after_the_prompt(self, monkeypatch, view_cls): + def test_wipe_finds_a_card_inserted_after_the_prompt(self, monkeypatch, tmp_path, view_cls): commands = [] patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, mounts=[]) view, screens = build_view(monkeypatch, view_cls) # The user only inserts the card once the View has asked for it. @@ -173,9 +343,10 @@ def test_wipe_finds_a_card_inserted_after_the_prompt(self, monkeypatch, view_cls assert screens.showed("MicroSD Wiped") assert destination.View_cls is MainMenuView - def test_flash_finds_a_card_inserted_after_the_prompt(self, monkeypatch): + def test_flash_finds_a_card_inserted_after_the_prompt(self, monkeypatch, tmp_path): commands = [] patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, mounts=[]) view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) monkeypatch.setattr( @@ -207,9 +378,10 @@ def test_no_card_is_refused(self, monkeypatch): assert screens.showed("No MicroSD") assert destination.View_cls is MainMenuView - def test_a_present_card_is_flashed(self, monkeypatch): + def test_a_present_card_is_flashed(self, monkeypatch, tmp_path): commands = [] patch_environment(monkeypatch, commands, on_seedsigner_os=False) + fake_kernel(monkeypatch, tmp_path, mounts=[]) monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) diff --git a/tests/test_microsd_unmount_before_write.py b/tests/test_microsd_unmount_before_write.py new file mode 100644 index 000000000..ce7525496 --- /dev/null +++ b/tests/test_microsd_unmount_before_write.py @@ -0,0 +1,486 @@ +"""Nothing is written to the card while a filesystem on it is still mounted.""" +# Must import base before any seedsigner modules +from base import BaseTest + +import os +import shutil +import subprocess +import sys + +import pytest + +from seedsigner.views import microsd_views + +from test_microsd_device_guard import ( + DEVNUMS, FILE, SD_DEV, build_view, fake_kernel, patch_environment, +) + + +# What SeedSigner OS has mounted once mdev has seen a card go in: the card +# itself, and diy-tools.squashfs from the card on a loop device. +SEEDSIGNER_OS_MOUNTS = [ + "rootfs / rootfs rw 0 0", + "proc /proc proc rw,relatime 0 0", + f"{SD_DEV}p1 /mnt/microsd vfat rw,sync,relatime 0 0", + "/dev/loop0 /mnt/diy squashfs ro,relatime 0 0", +] +SEEDSIGNER_OS_LOOPS = {"loop0": "/mnt/microsd/diy-tools.squashfs"} + +# Raspberry Pi OS booted with no initramfs: the kernel mounts the root itself +# and lists it as /dev/root, whatever it is on -- here, the card. +DEV_ROOT_MOUNTS = [ + "/dev/root / ext4 rw,noatime 0 0", + "devtmpfs /dev devtmpfs rw 0 0", + "proc /proc proc rw 0 0", + f"{SD_DEV}p1 /boot vfat rw 0 0", +] +DEV_ROOT_ON_THE_CARD = {"/dev/root": f"{SD_DEV}p2"} + +# Raspberry Pi OS with raspi-config's overlay file system, as the overlayroot +# package sets it up: "/" is an overlay whose lower layer is the card's root +# partition, mounted read-only at /media/root-ro. The overlay keeps a copy of +# that mount of its own, so the partition stays in use after an umount of it. +OVERLAYROOT_MOUNTS = [ + "overlayroot / overlay rw,relatime,lowerdir=/media/root-ro," + "upperdir=/media/root-rw/overlay,workdir=/media/root-rw/overlay-workdir 0 0", + "proc /proc proc rw 0 0", + f"{SD_DEV}p2 /media/root-ro ext4 ro 0 0", + "tmpfs-root /media/root-rw tmpfs rw 0 0", + f"{SD_DEV}p1 /boot/firmware vfat rw 0 0", +] + +# The same overlay made by an initramfs script: its lower layer was mounted at +# /lower before the root was switched, so no mount here shows it. +INITRAMFS_OVERLAY_MOUNTS = [ + "overlay / overlay rw,lowerdir=/lower,upperdir=/upper/data,workdir=/upper/work 0 0", + "devtmpfs /dev devtmpfs rw 0 0", + "proc /proc proc rw 0 0", + f"{SD_DEV}p1 /boot/firmware vfat rw 0 0", +] + +# A btrfs root the kernel mounted itself: listed as /dev/root, and under a +# device number btrfs made up rather than the partition's. +BTRFS_ROOT_MOUNTS = [ + "/dev/root / btrfs rw 0 0", + "devtmpfs /dev devtmpfs rw 0 0", + "proc /proc proc rw 0 0", + f"{SD_DEV}p1 /boot/firmware vfat rw 0 0", +] + +# Every way a Raspberry Pi OS host here runs from the card in its slot: the +# mounts, and what else the kernel says about the card. +RUNNING_FROM_THE_CARD = [ + pytest.param(DEV_ROOT_MOUNTS, {"aliases": DEV_ROOT_ON_THE_CARD}, id="dev-root"), + pytest.param(OVERLAYROOT_MOUNTS, {"claimed": {f"{SD_DEV}p2"}}, id="overlayroot"), + pytest.param(INITRAMFS_OVERLAY_MOUNTS, {"claimed": {f"{SD_DEV}p2"}}, id="initramfs-overlay"), + pytest.param(BTRFS_ROOT_MOUNTS, {"claimed": {f"{SD_DEV}p2"}}, id="btrfs-root"), +] + + +def unmounted(commands): + """The mountpoints umount was run on, in order.""" + return [cmd[-1] for cmd in commands if "umount" in cmd] + + +def writes(commands): + """Indexes of the dd commands that write anywhere.""" + return [ + i for i, cmd in enumerate(commands) + if "dd" in cmd and any(part.startswith("of=") for part in cmd) + ] + + +class TestWhatHoldsTheCard(BaseTest): + """ + A card's filesystem is held by more than its own mount. SeedSigner OS + loop-mounts diy-tools.squashfs from the card at /mnt/diy, so unmounting + /mnt/microsd alone either fails busy or leaves a live mount under dd. + """ + + def unmount(self, monkeypatch, tmp_path, mounts, loops=None, **kernel): + commands = [] + patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, mounts, loops, **kernel) + return microsd_views.unmount_card(SD_DEV), commands + + def test_every_filesystem_on_the_card_and_nothing_else(self, monkeypatch, tmp_path): + mounts = [ + "/dev/sda2 / ext4 rw 0 0", + f"{SD_DEV}p1 /mnt/sdcard vfat rw 0 0", + f"{SD_DEV}p2 /mnt/diy ext4 rw 0 0", + "/dev/mmcblk1p1 /mnt/other vfat rw 0 0", + f"{SD_DEV} /mnt/with\\040space vfat rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts) + + assert unmounted(commands) == [ + "/mnt/sdcard", "/mnt/diy", "/mnt/with space", + ], commands + assert ok + + def test_a_card_mounted_under_another_name_is_unmounted(self, monkeypatch, tmp_path): + # A mount is listed under whatever name it was made by; only its device + # number says which device it is. + mounts = [ + "/dev/sda2 / ext4 rw 0 0", + "/dev/disk/by-label/SEEDSIGNER /mnt/microsd vfat rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, + aliases={"/dev/disk/by-label/SEEDSIGNER": f"{SD_DEV}p1"}) + + assert unmounted(commands) == ["/mnt/microsd"], commands + assert ok + + def test_the_diy_tools_loop_mount_goes_before_the_card(self, monkeypatch, tmp_path): + ok, commands = self.unmount(monkeypatch, tmp_path, + SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + + assert unmounted(commands) == ["/mnt/diy", "/mnt/microsd"], commands + assert ok + + def test_the_loop_device_is_released_with_its_mount(self, monkeypatch, tmp_path): + # A loop device left bound keeps the squashfs open, and with it the + # card's filesystem busy. + ok, commands = self.unmount(monkeypatch, tmp_path, + SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + + assert ["umount", "-d", "/mnt/diy"] in [cmd[-3:] for cmd in commands], commands + assert ok + + def test_a_loop_mount_of_a_file_elsewhere_is_left_alone(self, monkeypatch, tmp_path): + mounts = SEEDSIGNER_OS_MOUNTS + ["/dev/loop1 /mnt/old squashfs ro 0 0"] + # Shares a prefix with /mnt/microsd, but is not on the card. + loops = dict(SEEDSIGNER_OS_LOOPS, loop1="/mnt/microsd-old/diy-tools.squashfs") + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, loops) + + assert "/mnt/old" not in unmounted(commands), commands + assert ok + + def test_a_loop_of_the_card_device_itself_is_unmounted(self, monkeypatch, tmp_path): + mounts = SEEDSIGNER_OS_MOUNTS + ["/dev/loop2 /mnt/raw ext4 rw 0 0"] + loops = dict(SEEDSIGNER_OS_LOOPS, loop2=f"{SD_DEV}p2") + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, loops) + + assert "/mnt/raw" in unmounted(commands), commands + assert ok + + def test_nested_mounts_go_first_deepest_first(self, monkeypatch, tmp_path): + mounts = [ + f"{SD_DEV}p1 /mnt/microsd vfat rw 0 0", + f"{SD_DEV}p2 /mnt/microsd/boot vfat rw 0 0", + "tmpfs /mnt/microsd/boot/tmp tmpfs rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts) + + assert unmounted(commands) == [ + "/mnt/microsd/boot/tmp", "/mnt/microsd/boot", "/mnt/microsd", + ], commands + assert ok + + def test_a_mount_on_top_of_the_card_comes_off_first(self, monkeypatch, tmp_path): + # umount takes off only the top mount on a path; once for the tmpfs + # would leave the card mounted underneath. + mounts = [ + f"{SD_DEV}p1 /mnt/microsd vfat rw 0 0", + "tmpfs /mnt/microsd tmpfs rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts) + + assert unmounted(commands) == ["/mnt/microsd", "/mnt/microsd"], commands + assert ok + + def test_a_card_holding_the_root_filesystem_is_refused_untouched(self, monkeypatch, tmp_path): + # Everything is mounted under "/", so "everything on the card" would + # be /proc, /dev and the rest of the running system. + mounts = [ + f"{SD_DEV}p2 / ext4 rw 0 0", + "proc /proc proc rw 0 0", + f"{SD_DEV}p1 /boot/firmware vfat rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts) + + assert unmounted(commands) == [], commands + assert not ok + + @pytest.mark.parametrize("mounts,kernel", RUNNING_FROM_THE_CARD) + def test_a_root_on_the_card_by_any_route_is_refused_untouched(self, monkeypatch, tmp_path, mounts, kernel): + # None of these has a mount of the card at "/" to go by. + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, **kernel) + + assert unmounted(commands) == [], commands + assert not ok + + def test_a_partition_in_use_as_swap_is_refused_untouched(self, monkeypatch, tmp_path): + # Swap is not a mount; only the kernel knows the partition is in use. + mounts = [ + "/dev/sda2 / ext4 rw 0 0", + f"{SD_DEV}p1 /mnt/microsd vfat rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, claimed={f"{SD_DEV}p2"}) + + assert unmounted(commands) == [], commands + assert not ok + + def test_a_filesystem_that_outlives_its_umount_is_a_refusal(self, monkeypatch, tmp_path): + # umount can succeed and leave the filesystem up: an overlay keeps its + # own copy of each layer's mount, a mount namespace its own copy of + # the tree. + ok, commands = self.unmount(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, + SEEDSIGNER_OS_LOOPS, claimed={f"{SD_DEV}p1"}) + + assert unmounted(commands) == ["/mnt/diy", "/mnt/microsd"], commands + assert not ok + + def test_an_overlay_built_on_the_card_goes_before_it(self, monkeypatch, tmp_path): + # The overlay holds the card's filesystem through its own copy of the + # mount: umount of /mnt/microsd succeeds and the card stays in use. + mounts = SEEDSIGNER_OS_MOUNTS + [ + "overlay /mnt/merged overlay rw,lowerdir=/usr/share:/mnt/microsd/layer," + "upperdir=/tmp/upper,workdir=/tmp/work 0 0", + # Shares a prefix with /mnt/microsd, but is not on the card. + "overlay /mnt/old overlay ro,lowerdir=/mnt/microsd-old/layer 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, SEEDSIGNER_OS_LOOPS) + + assert unmounted(commands) == ["/mnt/diy", "/mnt/merged", "/mnt/microsd"], commands + assert ok + + def test_a_card_another_device_is_built_on_is_refused(self, monkeypatch, tmp_path): + # dm-crypt or LVM on a partition: what is mounted is dm-0, and no + # umount frees the card underneath it. + mounts = [ + "/dev/sda2 / ext4 rw 0 0", + "/dev/dm-0 /mnt/vault ext4 rw 0 0", + ] + + ok, commands = self.unmount(monkeypatch, tmp_path, mounts, + holders={f"{SD_DEV}p2": ["dm-0"]}) + + assert unmounted(commands) == [], commands + assert not ok + + def test_an_unreadable_mount_table_is_a_refusal(self, monkeypatch, tmp_path): + commands = [] + patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + monkeypatch.setattr(microsd_views, "PROC_MOUNTINFO", str(tmp_path / "missing")) + + assert microsd_views.unmount_card(SD_DEV) is False + assert unmounted(commands) == [], commands + + def test_a_mount_table_that_does_not_parse_is_a_refusal(self, monkeypatch, tmp_path): + # /proc/mounts where mountinfo was expected: no device numbers to go by. + commands = [] + patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + (tmp_path / "mountinfo").write_text("".join(line + "\n" for line in SEEDSIGNER_OS_MOUNTS)) + + assert microsd_views.unmount_card(SD_DEV) is False + assert unmounted(commands) == [], commands + + def test_a_card_sysfs_does_not_describe_is_a_refusal(self, monkeypatch, tmp_path): + # Pulled after it was detected: without its device numbers there is + # no telling which mounts are on it. + commands = [] + patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + shutil.rmtree(tmp_path / "block" / SD_DEV.rsplit("/", 1)[-1]) + + assert microsd_views.unmount_card(SD_DEV) is False + assert unmounted(commands) == [], commands + + def test_a_loop_that_will_not_say_what_it_reads_is_a_refusal(self, monkeypatch, tmp_path): + # Its mount might be on the card; guessing no would write under it. + ok, commands = self.unmount(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, + loops={"loop0": None}) + + assert unmounted(commands) == [], commands + assert not ok + + def test_a_regular_file_at_the_cards_path_is_refused(self, monkeypatch, tmp_path): + # O_EXCL without O_CREAT claims only a block device, so a file there + # opened for exclusive use without complaint, and dd would have + # written to it. + ok, _ = self.unmount(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, + SEEDSIGNER_OS_LOOPS, nodes={SD_DEV: FILE}) + + assert not ok + + def test_a_regular_file_at_a_partitions_path_is_refused_untouched(self, monkeypatch, tmp_path): + # Opening it says nothing about whether the partition is in use. + ok, commands = self.unmount(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, + SEEDSIGNER_OS_LOOPS, nodes={f"{SD_DEV}p2": FILE}) + + assert unmounted(commands) == [], commands + assert not ok + + def test_another_block_device_at_the_cards_path_is_refused(self, monkeypatch, tmp_path): + # A block device, and free, but not the one sysfs calls the card. + ok, _ = self.unmount(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, + SEEDSIGNER_OS_LOOPS, nodes={SD_DEV: DEVNUMS["/dev/sda2"]}) + + assert not ok + + +class TestTheExclusiveOpenChecksWhatItOpened(BaseTest): + """EXCLUSIVE_OPEN as a real interpreter runs it, on files a test can make.""" + + @staticmethod + def exclusive_open(path): + # Its own number, so that only the kind of file can be what refuses it + st = os.stat(path) + devnum = f"{os.major(st.st_rdev)}:{os.minor(st.st_rdev)}" + return subprocess.run( + [sys.executable, "-c", microsd_views.EXCLUSIVE_OPEN, str(path), devnum], + capture_output=True, text=True, check=False, + ) + + def test_a_regular_file_is_refused(self, tmp_path): + stand_in = tmp_path / "mmcblk0" + stand_in.write_bytes(bytes(512)) + + result = self.exclusive_open(stand_in) + + assert result.returncode != 0 + assert "not a block device" in result.stderr, result.stderr + + def test_a_character_device_is_refused(self): + result = self.exclusive_open(os.devnull) + + assert result.returncode != 0 + assert "not a block device" in result.stderr, result.stderr + + +# Every caller that writes to the card: both branches of Flash, and both +# wipes. The bool is whether it runs on SeedSigner OS. +RAW_WRITERS = [ + (microsd_views.ToolsMicroSDFlashView, True), + (microsd_views.ToolsMicroSDFlashView, False), + (microsd_views.ToolsMicroSDWipeZeroView, True), + (microsd_views.ToolsMicroSDWipeRandomView, True), +] + + +class TestNothingIsWrittenUnderAMountedCard(BaseTest): + """ + The views unmounted two hard-coded paths, and only when the hostname said + SeedSigner OS. A Luckfox mounts its card at /mnt/sdcard, so a flash went + straight under a live filesystem; Wipe (Random) unmounted nothing at all. + """ + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_the_card_is_unmounted_before_dd(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, _ = build_view(monkeypatch, view_cls) + + view.run() + + assert unmounted(commands) == ["/mnt/diy", "/mnt/microsd"], commands + last_umount = max(i for i, cmd in enumerate(commands) if "umount" in cmd) + assert writes(commands) and last_umount < writes(commands)[0], commands + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_an_unreadable_mount_table_writes_nothing(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + monkeypatch.setattr(microsd_views, "PROC_MOUNTINFO", str(tmp_path / "missing")) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert not writes(commands), commands + assert screens.showed("Could not unmount") + + @pytest.mark.parametrize("mounts,kernel", RUNNING_FROM_THE_CARD) + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_card_holding_the_running_system_is_never_written(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os, + mounts, kernel): + # On a Raspberry Pi the card in the slot is the one it booted from. + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, mounts, **kernel) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert unmounted(commands) == [], commands + assert not writes(commands), commands + assert screens.showed("Could not unmount") + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_card_still_in_use_after_its_umount_is_not_written(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS, + claimed={f"{SD_DEV}p1"}) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert unmounted(commands) == ["/mnt/diy", "/mnt/microsd"], commands + assert not writes(commands), commands + assert screens.showed("Could not unmount") + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_card_that_will_not_unmount_is_not_written(self, monkeypatch, view_cls, on_seedsigner_os): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + monkeypatch.setattr(microsd_views, "unmount_card", lambda dev: False) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert not writes(commands), commands + assert screens.showed("Could not unmount") + + @pytest.mark.parametrize("node", [FILE, DEVNUMS["/dev/sda2"]], ids=["file", "other-disk"]) + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_path_that_is_not_the_card_is_not_written(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os, + node): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS, + nodes={SD_DEV: node}) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert not writes(commands), commands + assert screens.showed("Could not unmount") + + @pytest.mark.parametrize("on_seedsigner_os", [True, False]) + def test_the_image_is_copied_before_the_card_is_unmounted(self, monkeypatch, tmp_path, on_seedsigner_os): + # Raspberry Pi OS keeps its images on /boot, which can be a partition + # of the very card being flashed. + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, [ + "/dev/sda2 / ext4 rw 0 0", + f"{SD_DEV}p1 /boot vfat rw 0 0", + ]) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, _ = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) + + view.run() + + copy = next(i for i, cmd in enumerate(commands) if cmd[0] == "cp") + first_umount = next(i for i, cmd in enumerate(commands) if "umount" in cmd) + assert copy < first_umount, commands From a352693098044b6bf653ed85186b4d0543c8aa78 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Mon, 21 Sep 2026 17:41:25 +0100 Subject: [PATCH 09/12] fix: check each step of a flash or wipe A flash or a wipe could report success after a step in it had failed. Flash on a Raspberry Pi OS host ran cp and then dd without looking at either result, and said nothing once dd was done. A copy that failed left /tmp/img.img holding whatever an earlier flash or Verify had put there, and dd wrote that to the card. A dd that failed and one that worked looked the same: straight back to the main menu. Flash on SeedSigner OS zeroes the first 26 MB before it writes the image, and wrote the image whatever the zeroing had done. It and both wipes then went by dd's record counts alone, which do not say: GNU dd reports every record written, and exits 1, when it is closing the card that fails. Every dd that writes to the card now goes through one check: it must exit cleanly and write every record it read. A wipe still counts a full card as done, since it writes until it has written what was asked or the card is full, and dd then fails with "No space left on device". A failed zeroing ends the flash and is what gets reported. The copy is judged by its exit status on both branches, and on a dev host a copy that fails is refused before the card is touched. A dev-host flash reports as the SeedSigner OS branch does: dd's error output on failure, "MicroSD Flashed" on success. Verify is not offered there: the SeedSigner OS branch zeroes the first 26 MB, the span Verify hashes, before it writes, and this one does not. --- src/seedsigner/views/microsd_views.py | 135 +++++++++++--------- tests/test_microsd_device_guard.py | 169 +++++++++++++++++++++++++- 2 files changed, 245 insertions(+), 59 deletions(-) diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index f43ecffc8..5e922f46d 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -356,6 +356,43 @@ def refuse_without_card(view: View, text: str) -> Destination: return Destination(MainMenuView) +def dd_succeeded(data, wipe: bool = False) -> bool: + """ + True when a dd that wrote to the card did everything it was asked to. + + It must exit cleanly: the record counts alone do not say, since dd + reports every record it wrote even when it then fails. + A wipe is the exception. It writes what it was asked to or until the + card is full, and a full card makes dd fail with "No space left on + device" once the job is done. Every record read must also have been + written. + """ + filled = wipe and "No space left on device" in data.stderr + if data.returncode != 0 and not filled: + return False + + records_in, records_out = 1, 0 + for line in data.stderr.split("\n"): + if "records in" in line: + records_in = line.split("+")[0] + elif "records out" in line: + records_out = line.split("+")[0] + # A full card stops dd partway through a record + return filled or records_in == records_out + + +def run_dd(cmd): + """ + Run a dd whose report dd_succeeded() reads, in English whatever the locale. + + GNU dd translates its record counts and errors, and sudo passes the + caller's locale through, so on a host set to any other language a good + write read as a failed one. + """ + from subprocess import run + return run(cmd, capture_output=True, text=True, env={**os.environ, "LC_ALL": "C"}) + + class ToolsMicroSDMenuView(View): FLASH_IMAGE = ButtonOption("Flash Image") VERIFY_IMAGE = ButtonOption("Verify MicroSD") @@ -477,7 +514,7 @@ def run(self): image_path = os.path.join(images_dir, microsd_image) data = run(['cp', image_path, '/tmp/img.img'], capture_output=True, text=True) logger.info(data) - if len(data.stderr) > 1: + if data.returncode != 0 or len(data.stderr) > 1: self.run_screen( WarningScreen, title="Error", @@ -514,28 +551,18 @@ def run(self): if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd - data = run(dd_cmd, capture_output=True, text=True) + data = run_dd(dd_cmd) logger.info(data) - # Then flash the image - data = run(["dd", "if=/tmp/img.img", f"of={microsd_dev}"], capture_output=True, text=True) - logger.info(data) + # Then flash the image, unless zeroing failed: then it is that + # failure that is reported. + if dd_succeeded(data): + data = run_dd(["dd", "if=/tmp/img.img", f"of={microsd_dev}"]) + logger.info(data) self.loading_screen.stop() - data_stderr_split = data.stderr.split('\n') - - inNum = 1 - outNum = 0 - for errorLine in data_stderr_split: - if "records in" in errorLine: - inNum = errorLine.split("+")[0] - continue - elif "records out" in errorLine: - outNum = errorLine.split("+")[0] - continue - - if inNum != outNum: + if not dd_succeeded(data): self.run_screen( WarningScreen, title="Error", @@ -563,7 +590,12 @@ def run(self): # Copy first: the images are on /boot, and /boot can be on the # card that is about to be unmounted. image_path = os.path.join(images_dir, microsd_image) - run(['cp', image_path, '/tmp/img.img'], check=False) + data = run(['cp', image_path, '/tmp/img.img'], capture_output=True, text=True) + logger.info(data) + if data.returncode != 0: + # /tmp/img.img still holds whatever an earlier flash or Verify + # left there, and dd would write that to the card instead. + return refuse_without_card(self, "Could not copy the image. Nothing was written.") microsd_dev = find_sd_card_device() if microsd_dev is None: @@ -572,7 +604,27 @@ def run(self): if not unmount_card(microsd_dev): return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was written.") - run(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}'], check=False) + data = run_dd(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}']) + logger.info(data) + + if not dd_succeeded(data): + self.run_screen( + WarningScreen, + title="Error", + status_headline=None, + text=data.stderr, + show_back_button=False, + button_data=[ButtonOption("Continue")] + ) + else: + self.run_screen( + LargeIconStatusScreen, + title="Success", + status_headline=None, + text="MicroSD Flashed", + show_back_button=False, + button_data=[ButtonOption("Continue")] + ) return Destination(MainMenuView) @@ -622,7 +674,7 @@ def run(self): dd_cmd = ["dd", f"if={microsd_dev}", "of=/tmp/img.img", "bs=1M", "count=26"] if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd - read = run(dd_cmd, capture_output=True, text=True) + read = run_dd(dd_cmd) logger.info(read) if read.returncode != 0: @@ -728,28 +780,12 @@ def run(self): if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd - data = run(dd_cmd, capture_output=True, text=True) + data = run_dd(dd_cmd) logger.info(data) self.loading_screen.stop() - data_stderr_split = data.stderr.split('\n') - - inNum = 1 - outNum = 0 - for errorLine in data_stderr_split: - if "records in" in errorLine: - inNum = errorLine.split("+")[0] - continue - elif "records out" in errorLine: - outNum = errorLine.split("+")[0] - continue - - # The number of in/out records won't match we just keep writing until the disk is full... - if "No space left on device" in data.stderr: - outNum = inNum - - if inNum != outNum: + if not dd_succeeded(data, wipe=True): self.run_screen( WarningScreen, title="Error", @@ -826,29 +862,12 @@ def run(self): if not Settings.is_seedsigner_os(): dd_cmd = ["sudo"] + dd_cmd - data = run(dd_cmd, capture_output=True, text=True) + data = run_dd(dd_cmd) logger.info(data) self.loading_screen.stop() - data_stderr_split = data.stderr.split('\n') - - inNum = 1 - outNum = 0 - for errorLine in data_stderr_split: - if "records in" in errorLine: - inNum = errorLine.split("+")[0] - continue - - if "records out" in errorLine: - outNum = errorLine.split("+")[0] - continue - - # The number of in/out records won't match we just keep writing until the disk is full... - if "No space left on device" in data.stderr: - outNum = inNum - - if inNum != outNum: + if not dd_succeeded(data, wipe=True): self.run_screen( WarningScreen, title="Error", diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index 56c204c80..f1c5302fb 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -63,7 +63,8 @@ def stop(self): pass -def patch_environment(monkeypatch, commands, on_seedsigner_os=True, dd_returncode=0): +def patch_environment(monkeypatch, commands, on_seedsigner_os=True, dd_returncode=0, + cp_returncode=0): """Make a MicroSD View runnable headlessly and record the commands it runs.""" hostname = "seedsigner-os" if on_seedsigner_os else "testhost" monkeypatch.setattr( @@ -94,6 +95,11 @@ def fake_run(cmd, *args, **kwargs): commands.append(cmd) if cmd[0] == "cp": # `cp` output is checked for errors; anything on stderr aborts a flash. + if cp_returncode != 0: + return SimpleNamespace( + stdout="", stderr=f"cp: can't stat '{cmd[1]}': No such file or directory\n", + returncode=cp_returncode, + ) return SimpleNamespace(stdout="", stderr="", returncode=0) if cmd[0] == "sha256sum": return SimpleNamespace(stdout="0" * 64 + " /tmp/img.img\n", stderr="", returncode=0) @@ -389,9 +395,170 @@ def test_a_present_card_is_flashed(self, monkeypatch, tmp_path): destination = view.run() assert_dd_targets(commands, SD_DEV) + assert screens.showed("MicroSD Flashed") + assert destination.View_cls is MainMenuView + + def test_a_failed_flash_is_reported(self, monkeypatch, tmp_path): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=False, dd_returncode=1) + fake_kernel(monkeypatch, tmp_path, mounts=[]) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + + view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) + + destination = view.run() + + assert not screens.showed("MicroSD Flashed") + assert screens.showed("Input/output error") + assert destination.View_cls is MainMenuView + + @pytest.mark.parametrize("on_seedsigner_os", [True, False]) + def test_a_failed_copy_writes_nothing(self, monkeypatch, tmp_path, on_seedsigner_os): + # /tmp/img.img still holds whatever an earlier flash or Verify left + # there, and dd would write that to the card instead. + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os, + cp_returncode=1) + fake_kernel(monkeypatch, tmp_path, mounts=[]) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + + view, screens = build_view(monkeypatch, microsd_views.ToolsMicroSDFlashView) + + destination = view.run() + + assert_no_dd(commands) + assert not screens.showed("MicroSD Flashed") assert destination.View_cls is MainMenuView +def fail_dd(monkeypatch, commands, which, stderr): + """Make each dd which(cmd) picks exit 1 with stderr; the rest run as before.""" + run = subprocess.run + + def failing_run(cmd, *args, **kwargs): + if "dd" in cmd and which(cmd): + commands.append(cmd) + return SimpleNamespace(stdout="", stderr=stderr, returncode=1) + return run(cmd, *args, **kwargs) + + monkeypatch.setattr("subprocess.run", failing_run) + + +# GNU dd when closing the card fails: every record went out, and it failed. +DD_CLOSE_FAILED = ( + f"dd: closing output file '{SD_DEV}': Input/output error\n" + DD_OK +) + + +# GNU dd on a host with a German locale: the same success, in other words. +DD_OK_GERMAN = "64+0 Datensätze ein\n64+0 Datensätze aus\n" + + +def localized_dd(monkeypatch): + """Make every dd report in German unless it runs with LC_ALL=C.""" + run = subprocess.run + + def localized_run(cmd, *args, **kwargs): + data = run(cmd, *args, **kwargs) + if "dd" in cmd and (kwargs.get("env") or {}).get("LC_ALL") != "C": + return SimpleNamespace(stdout=data.stdout, stderr=DD_OK_GERMAN, returncode=data.returncode) + return data + + monkeypatch.setattr("subprocess.run", localized_run) + + +class TestEveryStepIsChecked(BaseTest): + """ + A flash or wipe succeeded only if every dd in it did. The zero pass that + starts a SeedSigner OS flash was never looked at, and the rest went by + dd's record counts alone. + """ + + def run_view(self, monkeypatch, tmp_path, view_cls, which, stderr, on_seedsigner_os=True): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, mounts=[]) + fail_dd(monkeypatch, commands, which, stderr) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + return view.run(), screens, commands + + def test_a_failed_zero_pass_stops_the_flash(self, monkeypatch, tmp_path): + destination, screens, commands = self.run_view( + monkeypatch, tmp_path, microsd_views.ToolsMicroSDFlashView, + which=lambda cmd: "if=/dev/zero" in cmd, + stderr=f"dd: error writing '{SD_DEV}': Input/output error\n3+0 records in\n2+0 records out\n", + ) + + assert not any("if=/tmp/img.img" in cmd for cmd in commands), commands + assert not screens.showed("MicroSD Flashed") + assert screens.showed("Input/output error") + assert destination.View_cls is MainMenuView + + @pytest.mark.parametrize("on_seedsigner_os", [True, False]) + def test_a_flash_whose_dd_failed_is_not_flashed(self, monkeypatch, tmp_path, on_seedsigner_os): + destination, screens, _ = self.run_view( + monkeypatch, tmp_path, microsd_views.ToolsMicroSDFlashView, + which=lambda cmd: "if=/tmp/img.img" in cmd, stderr=DD_CLOSE_FAILED, + on_seedsigner_os=on_seedsigner_os, + ) + + assert not screens.showed("MicroSD Flashed") + assert screens.showed("Input/output error") + assert destination.View_cls is MainMenuView + + @pytest.mark.parametrize("on_seedsigner_os", [True, False]) + @pytest.mark.parametrize( + "view_cls", + [microsd_views.ToolsMicroSDWipeZeroView, microsd_views.ToolsMicroSDWipeRandomView], + ) + def test_a_wipe_whose_dd_failed_is_not_wiped(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os): + destination, screens, _ = self.run_view( + monkeypatch, tmp_path, view_cls, + which=lambda cmd: True, stderr=DD_CLOSE_FAILED, on_seedsigner_os=on_seedsigner_os, + ) + + assert not screens.showed("MicroSD Wiped") + assert screens.showed("Input/output error") + assert destination.View_cls is MainMenuView + + @pytest.mark.parametrize( + "view_cls", + [microsd_views.ToolsMicroSDWipeZeroView, microsd_views.ToolsMicroSDWipeRandomView], + ) + def test_a_wipe_that_fills_the_card_is_wiped(self, monkeypatch, tmp_path, view_cls): + # Writing until the card is full ends the only way it can: dd fails + # with no space left. + _, screens, _ = self.run_view( + monkeypatch, tmp_path, view_cls, which=lambda cmd: True, + stderr=f"dd: error writing '{SD_DEV}': No space left on device\n" + "15193+0 records in\n15192+1 records out\n", + ) + + assert screens.showed("MicroSD Wiped") + + + @pytest.mark.parametrize("view_cls, on_seedsigner_os, done", [ + (microsd_views.ToolsMicroSDFlashView, True, "MicroSD Flashed"), + (microsd_views.ToolsMicroSDFlashView, False, "MicroSD Flashed"), + (microsd_views.ToolsMicroSDWipeZeroView, True, "MicroSD Wiped"), + (microsd_views.ToolsMicroSDWipeRandomView, True, "MicroSD Wiped"), + ]) + def test_dd_is_read_whatever_the_locale(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os, done): + # GNU dd translates its report, and sudo keeps the caller's locale: + # read in any other language, a good write looked like a failed one. + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, mounts=[]) + localized_dd(monkeypatch) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert screens.showed(done) + + class TestBlankCardIsStillACard(BaseTest): """A card with no partition table is in the slot; the tool zeroed it itself.""" From 48ab05d35100bbdcd26ef677d99cf8992db848c0 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Mon, 21 Sep 2026 18:19:40 +0100 Subject: [PATCH 10/12] fix: tell Settings when the card is unmounted Settings learns that the card has gone from mdev, which reports a card pulled out of the slot. Flash and the wipes unmount the card themselves, and nothing reported that, so Persistent Settings stayed on offer with nowhere left to save to: on a board without /userdata, a change made after a wipe was skipped without a word. A save still waiting on its timer could also land after the card was unmounted. Write any waiting save before the first umount, and afterwards tell Settings the card was removed, as mdev would have. That happens even when the unmount is refused part-way, since a card mountpoint may already be gone by then. One thing a card pulled out by hand runs into is left as it was: on a Luckfox the next save still goes to /mnt/sdcard, by then a bare directory on the root filesystem, because SETTINGS_FILENAME is resolved once at start-up. Following the store as it changes is a separate change. --- src/seedsigner/views/microsd_views.py | 28 +++++++++----- tests/test_microsd_device_guard.py | 8 ++++ tests/test_microsd_unmount_before_write.py | 45 ++++++++++++++++++++++ 3 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 5e922f46d..1a704194a 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -319,16 +319,24 @@ def unmount_card(device: str) -> bool: logger.info("%s is in use by something no mount shows", device) return False - for mountpoint, is_loop in holding: - cmd = Settings.SU_COMMAND_PREFIX.split() + ["umount", mountpoint] - if is_loop: - # Free the loop device as well: while it stays bound it keeps its - # file open, and that file is on the card. - cmd.insert(-1, "-d") - data = run(cmd, capture_output=True, text=True) - logger.info(data) - if data.returncode != 0: - return False + # mdev tells Settings when a card is pulled out, and nobody tells it when + # the card is unmounted here. So a save still waiting goes to the card + # first, and Settings then hears what it would have heard from mdev, + # whether or not everything came off. + Settings.get_instance().flush_save() + try: + for mountpoint, is_loop in holding: + cmd = Settings.SU_COMMAND_PREFIX.split() + ["umount", mountpoint] + if is_loop: + # Free the loop device as well: while it stays bound it keeps + # its file open, and that file is on the card. + cmd.insert(-1, "-d") + data = run(cmd, capture_output=True, text=True) + logger.info(data) + if data.returncode != 0: + return False + finally: + Settings.handle_microsd_state_change(MicroSD.ACTION__REMOVED) # umount can succeed and leave the filesystem up: an overlay keeps its own # copy of each layer's mount, another mount namespace its own copy of the diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index f1c5302fb..ddb74e9a1 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -91,6 +91,14 @@ def fake_listdir(path): monkeypatch.setattr(os, "listdir", fake_listdir) + # What the views tell Settings about the card goes on the same list, so a + # test can see when it happened. + monkeypatch.setattr(Settings, "flush_save", + lambda self: commands.append(["Settings.flush_save"])) + monkeypatch.setattr(Settings, "handle_microsd_state_change", staticmethod( + lambda action: commands.append(["Settings.handle_microsd_state_change", action]) + )) + def fake_run(cmd, *args, **kwargs): commands.append(cmd) if cmd[0] == "cp": diff --git a/tests/test_microsd_unmount_before_write.py b/tests/test_microsd_unmount_before_write.py index ce7525496..aea5bb52c 100644 --- a/tests/test_microsd_unmount_before_write.py +++ b/tests/test_microsd_unmount_before_write.py @@ -9,6 +9,7 @@ import pytest +from seedsigner.hardware.microsd import MicroSD from seedsigner.views import microsd_views from test_microsd_device_guard import ( @@ -484,3 +485,47 @@ def test_the_image_is_copied_before_the_card_is_unmounted(self, monkeypatch, tmp copy = next(i for i, cmd in enumerate(commands) if cmd[0] == "cp") first_umount = next(i for i, cmd in enumerate(commands) if "umount" in cmd) assert copy < first_umount, commands + + +class TestSettingsFollowTheCard(BaseTest): + """ + Settings hears from mdev when a card is pulled out, and from nobody when + the app unmounts one itself. Persistent Settings then stayed on offer with + nowhere to save to, and a save still waiting landed after the card had + gone. + """ + + @staticmethod + def told_removed(commands): + return [ + i for i, cmd in enumerate(commands) + if cmd == ["Settings.handle_microsd_state_change", MicroSD.ACTION__REMOVED] + ] + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_settings_are_saved_before_and_told_after(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, _ = build_view(monkeypatch, view_cls) + + view.run() + + umounts = [i for i, cmd in enumerate(commands) if "umount" in cmd] + flushed = commands.index(["Settings.flush_save"]) + assert flushed < umounts[0], commands + assert [i for i in self.told_removed(commands) if i > umounts[-1]], commands + assert max(self.told_removed(commands)) < writes(commands)[0], commands + + def test_settings_are_told_when_the_card_came_off_and_is_still_refused(self, monkeypatch, tmp_path): + # /mnt/microsd is no longer a mountpoint, whatever the kernel still + # holds, so Settings has no card to save to either way. + commands = [] + patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS, + claimed={f"{SD_DEV}p1"}) + + assert microsd_views.unmount_card(SD_DEV) is False + assert unmounted(commands) == ["/mnt/diy", "/mnt/microsd"], commands + assert self.told_removed(commands), commands From 1995bec2207df2f55cfbcd6845f0a8c40c10b0a7 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Mon, 21 Sep 2026 19:52:34 +0100 Subject: [PATCH 11/12] fix: fail a write that did not reach the card dd opens of= with O_CREAT, busybox's as well as GNU's. A card pulled after it was unmounted takes its node with it, and dd then creates a regular file of that name instead, in RAM on devtmpfs, writes it and exits 0: Flash said "MicroSD Flashed" and the wipes "MicroSD Wiped" for a card nothing had reached. devtmpfs never replaces a file it did not make, so the file then stood in for the card, even once the card was back, and every later Flash, Wipe and Verify found it instead. After each dd that writes the card, and after Verify reads it, the path must still be the block device with the number sysfs gives the card. Otherwise the step failed, and a regular file at the path is removed so that the card's node can come back. The card is asked about first, whatever dd said, since a dd that failed can have left a file behind as well, having filled RAM with it. conv=nocreat would stop dd making the file, but busybox dd, as SeedSigner OS builds it, has no such flag and rejects the whole command over it. GNU dd on a dev host does have it; one check that holds for both is kept rather than a second path for one of them. --- src/seedsigner/views/microsd_views.py | 81 ++++++++++++--- tests/test_microsd_device_guard.py | 67 ++++++++++-- tests/test_microsd_write_reaches_card.py | 125 +++++++++++++++++++++++ 3 files changed, 253 insertions(+), 20 deletions(-) create mode 100644 tests/test_microsd_write_reaches_card.py diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 1a704194a..5894143ab 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -3,6 +3,7 @@ ****************************************************************************""" import logging import os +import stat import sys from pathlib import Path from gettext import gettext as _ @@ -401,6 +402,54 @@ def run_dd(cmd): return run(cmd, capture_output=True, text=True, env={**os.environ, "LC_ALL": "C"}) +def is_the_card(device: str) -> bool: + """ + True when device is still the card: the block device sysfs numbers it. + + dd opens of= with O_CREAT, busybox's as well as GNU's, and busybox's has + no conv=nocreat to stop it. A card pulled after it was unmounted takes + its node with it, and dd then writes a regular file of that name instead + -- in RAM, on devtmpfs -- and exits 0. devtmpfs never replaces a file it + did not make, so that file would go on standing in for the card, even + once the card is back, and every later flash, wipe and Verify would find + it instead. So a regular file there is removed, and whatever went to it + or came from it did not reach the card. + """ + from subprocess import run + try: + devnum = _read_sysfs(os.path.join(SYS_BLOCK, os.path.basename(device), "dev")) + node = os.stat(device) + except OSError as e: + logger.info("Cannot tell whether %s is the card: %s", device, e) + else: + if stat.S_ISBLK(node.st_mode) and f"{os.major(node.st_rdev)}:{os.minor(node.st_rdev)}" == devnum: + return True + logger.info("%s is not block device %s", device, devnum) + + try: + stray = stat.S_ISREG(os.lstat(device).st_mode) + except OSError: + stray = False + if stray: + data = run(Settings.SU_COMMAND_PREFIX.split() + ["rm", "-f", device], capture_output=True, text=True) + logger.info(data) + return False + + +def write_failure(data, device: str, wipe: bool = False) -> str | None: + """ + Why a dd that wrote to device did not write the card, or None if it did. + + The card is asked about first, whatever dd said: a dd that failed can + have left a file in the card's place as well, having filled RAM with it. + """ + if not is_the_card(device): + return "The write did not reach the MicroSD card." + if not dd_succeeded(data, wipe): + return data.stderr + return None + + class ToolsMicroSDMenuView(View): FLASH_IMAGE = ButtonOption("Flash Image") VERIFY_IMAGE = ButtonOption("Verify MicroSD") @@ -561,21 +610,23 @@ def run(self): data = run_dd(dd_cmd) logger.info(data) + failure = write_failure(data, microsd_dev) - # Then flash the image, unless zeroing failed: then it is that - # failure that is reported. - if dd_succeeded(data): + # Then flash the image, unless zeroing failed or missed the card: + # then it is that failure that is reported. + if failure is None: data = run_dd(["dd", "if=/tmp/img.img", f"of={microsd_dev}"]) logger.info(data) + failure = write_failure(data, microsd_dev) self.loading_screen.stop() - if not dd_succeeded(data): + if failure is not None: self.run_screen( WarningScreen, title="Error", status_headline=None, - text=data.stderr, + text=failure, show_back_button=False, button_data=[ButtonOption("Continue")] ) @@ -615,12 +666,13 @@ def run(self): data = run_dd(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}']) logger.info(data) - if not dd_succeeded(data): + failure = write_failure(data, microsd_dev) + if failure is not None: self.run_screen( WarningScreen, title="Error", status_headline=None, - text=data.stderr, + text=failure, show_back_button=False, button_data=[ButtonOption("Continue")] ) @@ -685,10 +737,11 @@ def run(self): read = run_dd(dd_cmd) logger.info(read) - if read.returncode != 0: + if not is_the_card(microsd_dev) or read.returncode != 0: # /tmp/img.img still holds whatever the last read or flash left # there, so hashing it now would report a checksum for that image - # instead of for this card. + # instead of for this card. A read of a file standing in for the + # card is no better. self.loading_screen.stop() self.run_screen( WarningScreen, @@ -793,12 +846,13 @@ def run(self): self.loading_screen.stop() - if not dd_succeeded(data, wipe=True): + failure = write_failure(data, microsd_dev, wipe=True) + if failure is not None: self.run_screen( WarningScreen, title="Error", status_headline=None, - text=data.stderr, + text=failure, show_back_button=False, button_data=[ButtonOption("Continue")] ) @@ -875,12 +929,13 @@ def run(self): self.loading_screen.stop() - if not dd_succeeded(data, wipe=True): + failure = write_failure(data, microsd_dev, wipe=True) + if failure is not None: self.run_screen( WarningScreen, title="Error", status_headline=None, - text=data.stderr, + text=failure, show_back_button=False, button_data=[ButtonOption("Continue")] ) diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index ddb74e9a1..ec061a024 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -3,6 +3,7 @@ import itertools import os import pathlib +import shutil import stat import subprocess import sys @@ -140,7 +141,7 @@ def fake_run(cmd, *args, **kwargs): def fake_kernel(monkeypatch, tmp_path, mounts, loops=None, aliases=None, holders=None, - claimed=(), nodes=None): + claimed=(), nodes=None, pull_card=None, put_back=False): """ Point the views at a mountinfo and a /sys/block made up under tmp_path. @@ -159,7 +160,17 @@ def fake_kernel(monkeypatch, tmp_path, mounts, loops=None, aliases=None, holders /dev has a node for the card and for each partition, under the number sysfs gives it. nodes changes what stands at a path: a block device with another number, FILE, or None for nothing at all. The exclusive-open - check runs as written, against that /dev. + check runs as written, against that /dev, and os.stat and os.lstat see + it. dd opens of= with O_CREAT, as busybox and GNU dd both do, so it + makes a regular file at a path with nothing there; rm takes away what is + at a path. + + The card is pulled out just before the first dd pull_card picks: its + nodes go, and its sysfs entry too, unless put_back says the card went + straight back in. Its node does not come back either way: devtmpfs does + not replace the file dd makes in its place. + + Returns that /dev, as a dict. """ devnums = dict(DEVNUMS) for alias, device in (aliases or {}).items(): @@ -214,6 +225,25 @@ def covers(mountpoint, path): dev.update(nodes or {}) run = subprocess.run + def node_stat(node, path): + if node is None: + raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT), path) + if node == FILE: + return SimpleNamespace(st_mode=stat.S_IFREG | 0o644, st_rdev=0) + major, minor = map(int, node.split(":")) + return SimpleNamespace(st_mode=stat.S_IFBLK | 0o660, st_rdev=os.makedev(major, minor)) + + def on_dev(stat_call): + def fake_stat(path, *args, **kwargs): + if isinstance(path, str) and path.startswith(SD_DEV): + return node_stat(dev.get(path), path) + return stat_call(path, *args, **kwargs) + return fake_stat + + # No links in this /dev, so both give the same answer. + monkeypatch.setattr(os, "stat", on_dev(os.stat)) + monkeypatch.setattr(os, "lstat", on_dev(os.lstat)) + def exclusive_open(script, argv): """Run script the way `python -c script *argv` would, on the fake /dev.""" held = [device for _, device in up if device] + list(claimed) @@ -240,11 +270,7 @@ def fake_open(path, flags, *args, **kwargs): def fake_fstat(fd, *args, **kwargs): if fd not in opened: return real_fstat(fd, *args, **kwargs) - if opened[fd] == FILE: - return SimpleNamespace(st_mode=stat.S_IFREG | 0o644, st_rdev=0) - major, minor = map(int, opened[fd].split(":")) - return SimpleNamespace(st_mode=stat.S_IFBLK | 0o660, - st_rdev=os.makedev(major, minor)) + return node_stat(opened[fd], fd) def fake_close(fd): if opened.pop(fd, None) is None: @@ -265,6 +291,28 @@ def fake_close(fd): returncode=1) return SimpleNamespace(stdout="", stderr="", returncode=0) + pulled = [] + + def dd(cmd, result): + if pull_card is not None and not pulled and pull_card(cmd): + pulled.append(cmd) + for device in blocks: + dev[device] = None + if not put_back: + shutil.rmtree(blocks[SD_DEV]) + for part in cmd: + name, _, path = part.partition("=") + if not path.startswith(SD_DEV) or dev.get(path) is not None: + continue + if name == "if": + return SimpleNamespace( + stdout="", stderr=f"dd: can't open '{path}': No such file or directory\n", + returncode=1, + ) + if name == "of": + dev[path] = FILE + return result + def kernel_run(cmd, *args, **kwargs): result = run(cmd, *args, **kwargs) if "umount" in cmd: @@ -272,9 +320,14 @@ def kernel_run(cmd, *args, **kwargs): del up[max(i for i, (mountpoint, _) in enumerate(up) if mountpoint == cmd[-1])] elif "-c" in cmd and "O_EXCL" in cmd[cmd.index("-c") + 1]: return exclusive_open(cmd[cmd.index("-c") + 1], cmd[cmd.index("-c") + 2:]) + elif "rm" in cmd: + dev[cmd[-1]] = None + elif "dd" in cmd: + return dd(cmd, result) return result monkeypatch.setattr("subprocess.run", kernel_run) + return dev def build_view(monkeypatch, view_cls): diff --git a/tests/test_microsd_write_reaches_card.py b/tests/test_microsd_write_reaches_card.py new file mode 100644 index 000000000..191357196 --- /dev/null +++ b/tests/test_microsd_write_reaches_card.py @@ -0,0 +1,125 @@ +"""A flash or a wipe has only happened if what dd wrote reached the card.""" +# Must import base before any seedsigner modules +from base import BaseTest + +import pytest + +from seedsigner.views import microsd_views +from seedsigner.views.view import MainMenuView + +from test_microsd_device_guard import ( + FILE, SD_DEV, build_view, fake_kernel, patch_environment, +) +from test_microsd_unmount_before_write import ( + RAW_WRITERS, SEEDSIGNER_OS_LOOPS, SEEDSIGNER_OS_MOUNTS, writes, +) + + +SUCCESS = { + microsd_views.ToolsMicroSDFlashView: "MicroSD Flashed", + microsd_views.ToolsMicroSDWipeZeroView: "MicroSD Wiped", + microsd_views.ToolsMicroSDWipeRandomView: "MicroSD Wiped", +} + + +def removed(commands): + """The paths rm was run on, in order.""" + return [cmd[-1] for cmd in commands if "rm" in cmd] + + +def run_view(monkeypatch, tmp_path, view_cls, on_seedsigner_os=True, dd_returncode=0, **kernel): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os, + dd_returncode=dd_returncode) + dev = fake_kernel(monkeypatch, tmp_path, SEEDSIGNER_OS_MOUNTS, SEEDSIGNER_OS_LOOPS, **kernel) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + destination = view.run() + return destination, screens, commands, dev + + +class TestAWriteMustReachTheCard(BaseTest): + """ + dd opens of= with O_CREAT. A card pulled after it was unmounted takes its + node with it, and dd then writes a regular file of that name instead -- + in RAM, on devtmpfs -- and exits 0, and the view said the card was + flashed or wiped. devtmpfs does not replace a file it did not make, so + that file went on standing in for the card: every later flash, wipe and + Verify found it, and succeeded. + """ + + @pytest.mark.parametrize("put_back", [False, True], ids=["pulled", "put-back"]) + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_write_to_a_pulled_card_is_a_failure(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os, + put_back): + destination, screens, commands, dev = run_view( + monkeypatch, tmp_path, view_cls, on_seedsigner_os, + pull_card=lambda cmd: any(part.startswith("of=") for part in cmd), put_back=put_back, + ) + + assert not screens.showed(SUCCESS[view_cls]) + assert screens.showed("did not reach the MicroSD card") + # Nothing is written after the write that missed: on SeedSigner OS + # that is the zeroing, and the image never goes anywhere. + assert len(writes(commands)) == 1, commands + # The file dd made is gone, so the card's node can come back. + assert dev[SD_DEV] != FILE + assert removed(commands) == [SD_DEV], commands + assert destination.View_cls is MainMenuView + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_file_left_by_a_dd_that_failed_is_removed_too(self, monkeypatch, tmp_path, view_cls, + on_seedsigner_os): + # RAM runs out long before most images do. + destination, screens, commands, dev = run_view( + monkeypatch, tmp_path, view_cls, on_seedsigner_os, dd_returncode=1, + pull_card=lambda cmd: any(part.startswith("of=") for part in cmd), + ) + + assert not screens.showed(SUCCESS[view_cls]) + assert screens.showed("did not reach the MicroSD card") + assert dev[SD_DEV] != FILE + assert removed(commands) == [SD_DEV], commands + assert destination.View_cls is MainMenuView + + def test_a_card_pulled_between_zeroing_and_the_image_is_not_flashed(self, monkeypatch, tmp_path): + destination, screens, commands, dev = run_view( + monkeypatch, tmp_path, microsd_views.ToolsMicroSDFlashView, + pull_card=lambda cmd: "if=/tmp/img.img" in cmd, + ) + + assert not screens.showed("MicroSD Flashed") + assert screens.showed("did not reach the MicroSD card") + assert dev[SD_DEV] != FILE + assert removed(commands) == [SD_DEV], commands + assert destination.View_cls is MainMenuView + + @pytest.mark.parametrize("view_cls,on_seedsigner_os", RAW_WRITERS) + def test_a_write_that_reached_the_card_removes_nothing(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os): + _, screens, commands, dev = run_view(monkeypatch, tmp_path, view_cls, on_seedsigner_os) + + assert screens.showed(SUCCESS[view_cls]) + assert removed(commands) == [], commands + assert dev[SD_DEV] == "179:0" + + +class TestVerifyMustReadTheCard(BaseTest): + """A file standing in for the card hashes as well as the card does.""" + + def test_a_file_at_the_cards_path_is_not_hashed_as_the_card(self, monkeypatch, tmp_path): + destination, screens, commands, dev = run_view( + monkeypatch, tmp_path, microsd_views.ToolsMicroSDVerifyView, nodes={SD_DEV: FILE}, + ) + + assert not screens.showed("Matched Checksum") + assert "Unfamilliar Checksum" not in screens.titles + assert screens.showed("Could not read") + assert dev[SD_DEV] != FILE + assert removed(commands) == [SD_DEV], commands + assert destination.View_cls is MainMenuView + + def test_the_card_itself_is_hashed(self, monkeypatch, tmp_path): + _, screens, commands, _ = run_view(monkeypatch, tmp_path, microsd_views.ToolsMicroSDVerifyView) + + assert "Unfamilliar Checksum" in screens.titles + assert removed(commands) == [], commands From 345f517aca81cc3f2ef41b2bcfa049e176252b48 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Tue, 22 Sep 2026 04:41:10 +0100 Subject: [PATCH 12/12] fix: hold the card for the whole raw write The exclusive-open check proved the card free, then let go of it before dd opened the card, and Flash let go of it again between its zero pass and its image. Anything that took the card in those gaps was written under: a remount, or a card swapped into the slot. A card pulled and another put in even takes the same node and device number, so unmounted it passed every check. Each job now runs in one child that claims the card for exclusive use and runs every dd of the job with of=/proc/self/fd/N, the file it claimed, until the last has finished, so nothing can mount the card meanwhile. The card's CID is read when it is chosen, and the child writes only while the card in the slot still has it: before the first dd, and after each one before its exit status is looked at, since a wipe that fills the card ends in an error that counts as success. A changed card is a failure whatever dd said, and a card that will not give its CID is not written. The child runs under the su prefix, so its dds carry none. --- src/seedsigner/views/microsd_views.py | 123 +++++++++++--- tests/test_microsd_device_guard.py | 181 ++++++++++++++++++++- tests/test_microsd_unmount_before_write.py | 7 +- 3 files changed, 280 insertions(+), 31 deletions(-) diff --git a/src/seedsigner/views/microsd_views.py b/src/seedsigner/views/microsd_views.py index 5894143ab..38adaca05 100644 --- a/src/seedsigner/views/microsd_views.py +++ b/src/seedsigner/views/microsd_views.py @@ -49,6 +49,51 @@ " sys.exit(f'{device} is not block device {devnum}')\n" ) +# Runs the dd commands on its command line in turn, with the card claimed +# the same way for as long as they last: EXCLUSIVE_OPEN lets go before dd +# starts, and anything could take the card in that gap. Each dd writes +# through the claimed file, of=/proc/self/fd/N, so nothing can mount the +# card meanwhile. And it must still be the card that was chosen, by its CID, +# before the first dd and after each one: a card pulled and another put in +# takes the same node and device number, and unmounted it passes every other +# check. +CLAIMED_WRITE = ( + "import json, os, stat, subprocess, sys\n" + "device, devnum, cid_path, cid, commands = sys.argv[1:]\n" + "def same_card():\n" + " try:\n" + " st = os.stat(device)\n" + " with open(cid_path) as f:\n" + " named = f.read().strip()\n" + " except OSError:\n" + " return False\n" + " return (stat.S_ISBLK(st.st_mode) and named == cid\n" + " and f'{os.major(st.st_rdev)}:{os.minor(st.st_rdev)}' == devnum)\n" + "fd = os.open(device, os.O_WRONLY | os.O_EXCL)\n" + "st = os.fstat(fd)\n" + "if not stat.S_ISBLK(st.st_mode):\n" + " sys.exit(f'{device} is not a block device')\n" + "if f'{os.major(st.st_rdev)}:{os.minor(st.st_rdev)}' != devnum:\n" + " sys.exit(f'{device} is not block device {devnum}')\n" + "def changed():\n" + " print('The MicroSD card was changed.', file=sys.stderr)\n" + " sys.exit(75)\n" + "for dd in json.loads(commands):\n" + " if not same_card():\n" + " changed()\n" + " dd = [f'of=/proc/self/fd/{fd}' if arg == f'of={device}' else arg for arg in dd]\n" + " code = subprocess.run(dd, pass_fds=(fd,)).returncode\n" + " # Before the exit code: a wipe that filled the card ends in an error\n" + " # that counts as success, and must not if another card was filled.\n" + " if not same_card():\n" + " changed()\n" + " if code:\n" + " sys.exit(code)\n" +) +# The exit status CLAIMED_WRITE gives when the card in the slot is not the one +# chosen. Whatever dd reported, nothing it wrote counts. +CARD_CHANGED = 75 + def _mmc_device_type(device: str) -> str | None: """The MMC card type the kernel reports: "SD", "MMC" (eMMC) or "SDIO".""" @@ -402,6 +447,35 @@ def run_dd(cmd): return run(cmd, capture_output=True, text=True, env={**os.environ, "LC_ALL": "C"}) +def card_identity(device: str) -> str | None: + """The card's CID, which names this one card, or None if it will not say.""" + try: + return _read_sysfs(os.path.join(SYS_BLOCK, os.path.basename(device), "device", "cid")) + except OSError: + return None + + +def write_card(dd_cmds: list[list[str]], device: str, card: str): + """ + Run dd_cmds, dds that write device, in turn, holding the card for + exclusive use from before the first opens it until the last has + finished, and only while it is still the card whose CID is card. + + They run under the su prefix, so none carries its own. + """ + import json + from subprocess import CompletedProcess + try: + devnum = _read_sysfs(os.path.join(SYS_BLOCK, os.path.basename(device), "dev")) + except OSError as e: + return CompletedProcess(dd_cmds, 1, "", f"Cannot tell which device {device} is: {e}\n") + cid_path = os.path.join(SYS_BLOCK, os.path.basename(device), "device", "cid") + return run_dd( + Settings.SU_COMMAND_PREFIX.split() + + [sys.executable, "-c", CLAIMED_WRITE, device, devnum, cid_path, card, json.dumps(dd_cmds)] + ) + + def is_the_card(device: str) -> bool: """ True when device is still the card: the block device sysfs numbers it. @@ -445,6 +519,8 @@ def write_failure(data, device: str, wipe: bool = False) -> str | None: """ if not is_the_card(device): return "The write did not reach the MicroSD card." + if data.returncode == CARD_CHANGED: + return "The MicroSD card was changed. It was not written in full." if not dd_succeeded(data, wipe): return data.stderr return None @@ -596,6 +672,9 @@ def run(self): microsd_dev = find_sd_card_device() if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") + card = card_identity(microsd_dev) + if card is None: + return refuse_without_card(self, "Could not identify the MicroSD card. Nothing was written.") if not unmount_card(microsd_dev): return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was written.") @@ -603,22 +682,19 @@ def run(self): self.loading_screen = LoadingScreenThread(text="Flashing MicroSD\n\n\n\n\n\n") self.loading_screen.start() - # Zero the MicroSD first - dd_cmd = ["dd", f"if=/dev/zero", f"of={microsd_dev}", "bs=1M", "count=26"] - if not Settings.is_seedsigner_os(): - dd_cmd = ["sudo"] + dd_cmd - - data = run_dd(dd_cmd) + # Zero the MicroSD first, then flash the image, under one claim. + # A zero pass that fails stops there, and it is that failure that + # is reported. + data = write_card( + [ + ["dd", f"if=/dev/zero", f"of={microsd_dev}", "bs=1M", "count=26"], + ["dd", "if=/tmp/img.img", f"of={microsd_dev}"], + ], + microsd_dev, card, + ) logger.info(data) failure = write_failure(data, microsd_dev) - # Then flash the image, unless zeroing failed or missed the card: - # then it is that failure that is reported. - if failure is None: - data = run_dd(["dd", "if=/tmp/img.img", f"of={microsd_dev}"]) - logger.info(data) - failure = write_failure(data, microsd_dev) - self.loading_screen.stop() if failure is not None: @@ -659,11 +735,14 @@ def run(self): microsd_dev = find_sd_card_device() if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was written.") + card = card_identity(microsd_dev) + if card is None: + return refuse_without_card(self, "Could not identify the MicroSD card. Nothing was written.") if not unmount_card(microsd_dev): return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was written.") - data = run_dd(['sudo', 'dd', f'if=/tmp/img.img', f'of={microsd_dev}']) + data = write_card([['dd', f'if=/tmp/img.img', f'of={microsd_dev}']], microsd_dev, card) logger.info(data) failure = write_failure(data, microsd_dev) @@ -830,6 +909,9 @@ def run(self): microsd_dev = find_sd_card_device() if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was wiped.") + card = card_identity(microsd_dev) + if card is None: + return refuse_without_card(self, "Could not identify the MicroSD card. Nothing was wiped.") if not unmount_card(microsd_dev): return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was wiped.") @@ -838,10 +920,7 @@ def run(self): self.loading_screen.start() dd_cmd = ["dd", f"if=/dev/zero", f"of={microsd_dev}", "bs=1M"] + wipesize_cmd_string.split() - if not Settings.is_seedsigner_os(): - dd_cmd = ["sudo"] + dd_cmd - - data = run_dd(dd_cmd) + data = write_card([dd_cmd], microsd_dev, card) logger.info(data) self.loading_screen.stop() @@ -913,6 +992,9 @@ def run(self): microsd_dev = find_sd_card_device() if microsd_dev is None: return refuse_without_card(self, "No MicroSD card detected. Nothing was wiped.") + card = card_identity(microsd_dev) + if card is None: + return refuse_without_card(self, "Could not identify the MicroSD card. Nothing was wiped.") if not unmount_card(microsd_dev): return refuse_without_card(self, "Could not unmount the MicroSD card. Nothing was wiped.") @@ -921,10 +1003,7 @@ def run(self): self.loading_screen.start() dd_cmd = ["dd", f"if=/dev/urandom", f"of={microsd_dev}", "bs=1M"] + wipesize_cmd_string.split() - if not Settings.is_seedsigner_os(): - dd_cmd = ["sudo"] + dd_cmd - - data = run_dd(dd_cmd) + data = write_card([dd_cmd], microsd_dev, card) logger.info(data) self.loading_screen.stop() diff --git a/tests/test_microsd_device_guard.py b/tests/test_microsd_device_guard.py index ec061a024..125dcc6a9 100644 --- a/tests/test_microsd_device_guard.py +++ b/tests/test_microsd_device_guard.py @@ -204,6 +204,9 @@ def covers(mountpoint, path): (block / "dev").write_text(DEVNUMS[device] + "\n") for holder in (holders or {}).get(device, []): (block / "holders" / holder).touch() + # The card's CID register, which names this one card + (blocks[SD_DEV] / "device").mkdir() + (blocks[SD_DEV] / "device" / "cid").write_text("035344534430344780aabbccdd013a00\n") for loop, backing_file in (loops or {}).items(): (sys_block / loop / "loop").mkdir(parents=True) (sys_block / loop / "dev").write_text(DEVNUMS[f"/dev/{loop}"] + "\n") @@ -244,7 +247,7 @@ def fake_stat(path, *args, **kwargs): monkeypatch.setattr(os, "stat", on_dev(os.stat)) monkeypatch.setattr(os, "lstat", on_dev(os.lstat)) - def exclusive_open(script, argv): + def exclusive_open(script, argv, env=None): """Run script the way `python -c script *argv` would, on the fake /dev.""" held = [device for _, device in up if device] + list(claimed) opened = {} @@ -276,20 +279,48 @@ def fake_close(fd): if opened.pop(fd, None) is None: real_close(fd) + # A claimed write runs its dd through the claimed fd: dd writes the + # device that was opened, under the name it was opened by. + paths = {} + inner = [] + + outer_run = subprocess.run + + def claimed_run(cmd, *args, **kwargs): + cmd = [ + f"of={paths[int(part[len('of=/proc/self/fd/'):])]}" if part.startswith("of=/proc/self/fd/") else part + for part in cmd + ] + # A child's children inherit its environment. + inner.append(outer_run(cmd, capture_output=True, text=True, env=env)) + return SimpleNamespace(returncode=inner[-1].returncode) + + def reported(code): + """What the child printed: each dd's report, then its own message.""" + stderr = "".join(result.stderr for result in inner) + if isinstance(code, str): + return SimpleNamespace(stdout="", stderr=stderr + code + "\n", returncode=1) + return SimpleNamespace(stdout="", stderr=stderr, returncode=code or 0) + + def tracking_open(path, flags, *args, **kwargs): + fd = fake_open(path, flags, *args, **kwargs) + paths[fd] = path + return fd + with monkeypatch.context() as patched: - patched.setattr(os, "open", fake_open) + patched.setattr(os, "open", tracking_open) patched.setattr(os, "fstat", fake_fstat) patched.setattr(os, "close", fake_close) + patched.setattr(subprocess, "run", claimed_run) patched.setattr(sys, "argv", ["-c", *argv]) try: exec(script, {"__name__": "__main__"}) # pylint: disable=exec-used except SystemExit as e: if e.code not in (None, 0): - return SimpleNamespace(stdout="", stderr=f"{e.code}\n", returncode=1) + return reported(e.code) except Exception as e: # pylint: disable=broad-except - return SimpleNamespace(stdout="", stderr=f"{type(e).__name__}: {e}\n", - returncode=1) - return SimpleNamespace(stdout="", stderr="", returncode=0) + return reported(f"{type(e).__name__}: {e}") + return reported(0) pulled = [] @@ -319,7 +350,7 @@ def kernel_run(cmd, *args, **kwargs): # umount takes off the top mount at a path del up[max(i for i, (mountpoint, _) in enumerate(up) if mountpoint == cmd[-1])] elif "-c" in cmd and "O_EXCL" in cmd[cmd.index("-c") + 1]: - return exclusive_open(cmd[cmd.index("-c") + 1], cmd[cmd.index("-c") + 2:]) + return exclusive_open(cmd[cmd.index("-c") + 1], cmd[cmd.index("-c") + 2:], kwargs.get("env")) elif "rm" in cmd: dev[cmd[-1]] = None elif "dd" in cmd: @@ -620,6 +651,142 @@ def test_dd_is_read_whatever_the_locale(self, monkeypatch, tmp_path, view_cls, o assert screens.showed(done) +class TestTheCardIsHeldThroughTheWrite(BaseTest): + """ + The kernel's exclusive-open check let go of the card again before dd + began, so whatever took the card in between -- a remount, or a card + swapped into the slot and mounted on insert -- was written under. + """ + + @pytest.mark.parametrize("view_cls, on_seedsigner_os", [ + (microsd_views.ToolsMicroSDFlashView, True), + (microsd_views.ToolsMicroSDFlashView, False), + (microsd_views.ToolsMicroSDWipeZeroView, True), + (microsd_views.ToolsMicroSDWipeRandomView, True), + ]) + def test_a_card_mounted_after_the_checks_is_not_written( + self, monkeypatch, tmp_path, view_cls, on_seedsigner_os + ): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + # Mounted again once every check unmount_card makes had passed + fake_kernel(monkeypatch, tmp_path, mounts=[f"{SD_DEV}p1 /mnt/microsd vfat rw"]) + monkeypatch.setattr(microsd_views, "unmount_card", lambda device: True) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert not [cmd for cmd in commands if cmd[:1] == ["dd"] or cmd[:2] == ["sudo", "dd"]] + assert not screens.showed("MicroSD Flashed") + assert not screens.showed("MicroSD Wiped") + + +class TestTheSameCardFromStartToEnd(BaseTest): + """ + A card is chosen, unmounted, then written, and a card pulled and another + put in between takes the same /dev/mmcblkN and the same device number: + unmounted, it passed every check. Flash also let go of the card between + its zero pass and its image. + """ + + RAW_WRITERS = [ + (microsd_views.ToolsMicroSDFlashView, True), + (microsd_views.ToolsMicroSDFlashView, False), + (microsd_views.ToolsMicroSDWipeZeroView, True), + (microsd_views.ToolsMicroSDWipeRandomView, True), + ] + + def run_view(self, monkeypatch, tmp_path, view_cls, on_seedsigner_os, swap=False, cid=True): + commands = [] + patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) + fake_kernel(monkeypatch, tmp_path, mounts=[]) + cid_file = tmp_path / "block" / SD_DEV.rsplit("/", 1)[-1] / "device" / "cid" + if not cid: + cid_file.unlink() + if swap: + unmount_card = microsd_views.unmount_card + + def swap_after_unmount(device): + # Another card in the slot, under the same node and number + cid_file.write_text("035344534430304780ffffffff013a00\n") + return unmount_card(device) + + monkeypatch.setattr(microsd_views, "unmount_card", swap_after_unmount) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + view.run() + return screens, commands + + @staticmethod + def dd_runs(commands): + return [cmd for cmd in commands if cmd[:1] == ["dd"] or cmd[:2] == ["sudo", "dd"]] + + @pytest.mark.parametrize("view_cls, on_seedsigner_os", RAW_WRITERS) + def test_a_card_swapped_in_after_it_was_chosen_is_not_written( + self, monkeypatch, tmp_path, view_cls, on_seedsigner_os + ): + screens, commands = self.run_view(monkeypatch, tmp_path, view_cls, on_seedsigner_os, swap=True) + + assert self.dd_runs(commands) == [] + assert not screens.showed("MicroSD Flashed") + assert not screens.showed("MicroSD Wiped") + assert screens.showed("The MicroSD card was changed") + + @pytest.mark.parametrize("view_cls, on_seedsigner_os", RAW_WRITERS) + def test_a_card_that_will_not_say_who_it_is_is_not_written( + self, monkeypatch, tmp_path, view_cls, on_seedsigner_os + ): + screens, commands = self.run_view(monkeypatch, tmp_path, view_cls, on_seedsigner_os, cid=False) + + assert self.dd_runs(commands) == [] + assert not screens.showed("MicroSD Flashed") + assert not screens.showed("MicroSD Wiped") + + @pytest.mark.parametrize( + "view_cls", + [microsd_views.ToolsMicroSDWipeZeroView, microsd_views.ToolsMicroSDWipeRandomView], + ) + def test_a_card_swapped_during_a_wipe_that_filled_it_is_not_wiped(self, monkeypatch, tmp_path, view_cls): + # A wipe to the end of the card finishes with "No space left", which + # is success -- unless it was another card that filled up. + commands = [] + patch_environment(monkeypatch, commands) + fake_kernel(monkeypatch, tmp_path, mounts=[]) + cid_file = tmp_path / "block" / SD_DEV.rsplit("/", 1)[-1] / "device" / "cid" + run = subprocess.run + + def fills_another_card(cmd, *args, **kwargs): + if cmd[:1] == ["dd"]: + commands.append(cmd) + cid_file.write_text("035344534430304780ffffffff013a00\n") + return SimpleNamespace( + stdout="", returncode=1, + stderr=f"dd: error writing '{SD_DEV}': No space left on device\n" + "15193+0 records in\n15192+1 records out\n", + ) + return run(cmd, *args, **kwargs) + + monkeypatch.setattr("subprocess.run", fills_another_card) + monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + view, screens = build_view(monkeypatch, view_cls) + + view.run() + + assert not screens.showed("MicroSD Wiped") + assert screens.showed("The MicroSD card was changed") + + def test_flash_holds_the_card_across_both_of_its_writes(self, monkeypatch, tmp_path): + screens, commands = self.run_view( + monkeypatch, tmp_path, microsd_views.ToolsMicroSDFlashView, on_seedsigner_os=True + ) + + claims = [cmd for cmd in commands if "-c" in cmd and microsd_views.CLAIMED_WRITE in cmd] + assert len(claims) == 1, claims + assert [cmd[1] for cmd in self.dd_runs(commands)] == ["if=/dev/zero", "if=/tmp/img.img"] + assert screens.showed("MicroSD Flashed") + + class TestBlankCardIsStillACard(BaseTest): """A card with no partition table is in the slot; the tool zeroed it itself.""" diff --git a/tests/test_microsd_unmount_before_write.py b/tests/test_microsd_unmount_before_write.py index aea5bb52c..556b25d1e 100644 --- a/tests/test_microsd_unmount_before_write.py +++ b/tests/test_microsd_unmount_before_write.py @@ -84,10 +84,12 @@ def unmounted(commands): def writes(commands): - """Indexes of the dd commands that write anywhere.""" + """Indexes of the dd commands that write anywhere: dd itself, not the + claimed write that runs it.""" return [ i for i, cmd in enumerate(commands) - if "dd" in cmd and any(part.startswith("of=") for part in cmd) + if (cmd[:1] == ["dd"] or cmd[:2] == ["sudo", "dd"]) + and any(part.startswith("of=") for part in cmd) ] @@ -443,6 +445,7 @@ def test_a_card_that_will_not_unmount_is_not_written(self, monkeypatch, view_cls commands = [] patch_environment(monkeypatch, commands, on_seedsigner_os=on_seedsigner_os) monkeypatch.setattr(microsd_views, "find_sd_card_device", lambda: SD_DEV) + monkeypatch.setattr(microsd_views, "card_identity", lambda dev: "a card") monkeypatch.setattr(microsd_views, "unmount_card", lambda dev: False) view, screens = build_view(monkeypatch, view_cls)