From 5bd8d9b5bb36e356d7ceab419a17387e88f56302 Mon Sep 17 00:00:00 2001 From: matejcik Date: Mon, 17 Feb 2020 16:45:27 +0100 Subject: [PATCH] core/sdcard: make allocating new SD card for emulator fast --- core/embed/unix/sdcard.c | 16 +++++++++------- core/emu.py | 5 ----- core/trezor.sdcard.gz | Bin 65333 -> 0 bytes tests/emulators.py | 18 +++++++----------- tests/trezor.sdcard.gz | Bin 65333 -> 0 bytes 5 files changed, 16 insertions(+), 23 deletions(-) delete mode 100644 core/trezor.sdcard.gz delete mode 100644 tests/trezor.sdcard.gz diff --git a/core/embed/unix/sdcard.c b/core/embed/unix/sdcard.c index 74c54fe58e..ff67a82805 100644 --- a/core/embed/unix/sdcard.c +++ b/core/embed/unix/sdcard.c @@ -52,20 +52,18 @@ void sdcard_init(void) { // check whether the file exists and it has the correct size struct stat sb; int r = stat(SDCARD_FILE, &sb); + int should_clear = 0; // (re)create if non existant or wrong size if (r != 0 || sb.st_size != SDCARD_SIZE) { int fd = open(SDCARD_FILE, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600); ensure(sectrue * (fd >= 0), "open failed"); - for (int i = 0; i < SDCARD_SIZE / 16; i++) { - ssize_t s = write( - fd, - "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", - 16); - ensure(sectrue * (s >= 0), "write failed"); - } + r = ftruncate(fd, SDCARD_SIZE); + ensure(sectrue * (r == 0), "truncate failed"); r = close(fd); ensure(sectrue * (r == 0), "close failed"); + + should_clear = 1; } // mmap file @@ -77,6 +75,10 @@ void sdcard_init(void) { sdcard_buffer = (uint8_t *)map; + if (should_clear) { + for (int i = 0; i < SDCARD_SIZE; ++i) sdcard_buffer[i] = 0xFF; + } + sdcard_powered = secfalse; atexit(sdcard_exit); diff --git a/core/emu.py b/core/emu.py index 1a355160d3..236009eec7 100755 --- a/core/emu.py +++ b/core/emu.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -import gzip import logging import os import platform @@ -25,7 +24,6 @@ except Exception: HERE = Path(__file__).parent.resolve() MICROPYTHON = HERE / "build" / "unix" / "micropython" SRC_DIR = HERE / "src" -SD_CARD_GZ = HERE / "trezor.sdcard.gz" PROFILING_WRAPPER = HERE / "prof" / "prof.py" @@ -189,9 +187,6 @@ def cli( elif temporary_profile: tempdir = tempfile.TemporaryDirectory(prefix="trezor-emulator-") profile_dir = Path(tempdir.name) - # unpack empty SD card for faster start-up - with gzip.open(SD_CARD_GZ, "rb") as gz: - (profile_dir / "trezor.sdcard").write_bytes(gz.read()) elif "TREZOR_PROFILE_DIR" in os.environ: profile_dir = Path(os.environ["TREZOR_PROFILE_DIR"]) diff --git a/core/trezor.sdcard.gz b/core/trezor.sdcard.gz deleted file mode 100644 index e9c6efc52f0949b9d8faefacfd1004ad875d1cbf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 65333 zcmeI&OGp$@6b9f?hZGCbN+B{I8zF=uC^SNFRNRD6q69(qP$9CiOh`0QFgLX-gEmFm za8W{8P*6<|N(ovNSw=9?1A0KEp;$Uox^dUKsqZWY&%?&T#wCcx=>4N45lLa$v9koS)KhEuN`?c)H zm3=Rs%#?qx&ia}5t*RoYDtp`gOE*XFbe!*fbExxaUNq}S#1fA1c@I+c^W z@@UokliU9$5INuUzeSdp009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0Rn%aK%}Rn zIDKxfv9A5WT0?*U0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5)eyFlhdq;#$e z@Gv%*Q4{ZJ>`#CI0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5)uKJQ6Nm@Nb3 z#YQSO^B_Qg009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjZ#USQYawBT$PpnH19 zics9A5r6;z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBo5Eh$c)?E(bGy7mWg z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RnL^kU0@4o$CTTj16Yg#C;qA z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+0D;k-^2FIPKq$6j. -import gzip import tempfile from collections import defaultdict from pathlib import Path @@ -30,7 +29,6 @@ LOCAL_BUILD_PATHS = { } CORE_SRC_DIR = ROOT / "core" / "src" -SD_CARD_GZ = ROOT / "core" / "trezor.sdcard.gz" ENV = {"SDL_VIDEODRIVER": "dummy"} @@ -88,15 +86,13 @@ class EmulatorWrapper: executable, self.profile_dir.name, storage=storage, headless=True, ) elif gen == "core": - with gzip.open(SD_CARD_GZ, "rb") as gz: - self.emulator = CoreEmulator( - executable, - self.profile_dir.name, - storage=storage, - workdir=workdir, - sdcard=gz.read(), - headless=True, - ) + self.emulator = CoreEmulator( + executable, + self.profile_dir.name, + storage=storage, + workdir=workdir, + headless=True, + ) def __enter__(self): self.emulator.start() diff --git a/tests/trezor.sdcard.gz b/tests/trezor.sdcard.gz deleted file mode 100644 index e9c6efc52f0949b9d8faefacfd1004ad875d1cbf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 65333 zcmeI&OGp$@6b9f?hZGCbN+B{I8zF=uC^SNFRNRD6q69(qP$9CiOh`0QFgLX-gEmFm za8W{8P*6<|N(ovNSw=9?1A0KEp;$Uox^dUKsqZWY&%?&T#wCcx=>4N45lLa$v9koS)KhEuN`?c)H zm3=Rs%#?qx&ia}5t*RoYDtp`gOE*XFbe!*fbExxaUNq}S#1fA1c@I+c^W z@@UokliU9$5INuUzeSdp009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0Rn%aK%}Rn zIDKxfv9A5WT0?*U0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5)eyFlhdq;#$e z@Gv%*Q4{ZJ>`#CI0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5)uKJQ6Nm@Nb3 z#YQSO^B_Qg009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjZ#USQYawBT$PpnH19 zics9A5r6;z0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBo5Eh$c)?E(bGy7mWg z5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RnL^kU0@4o$CTTj16Yg#C;qA z2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+0D;k-^2FIPKq$6j