1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 23:48:12 +00:00

core/sdcard: make allocating new SD card for emulator fast

This commit is contained in:
matejcik 2020-02-17 16:45:27 +01:00
parent 1e9352b9e0
commit 5bd8d9b5bb
5 changed files with 16 additions and 23 deletions

View File

@ -52,20 +52,18 @@ void sdcard_init(void) {
// check whether the file exists and it has the correct size // check whether the file exists and it has the correct size
struct stat sb; struct stat sb;
int r = stat(SDCARD_FILE, &sb); int r = stat(SDCARD_FILE, &sb);
int should_clear = 0;
// (re)create if non existant or wrong size // (re)create if non existant or wrong size
if (r != 0 || sb.st_size != SDCARD_SIZE) { if (r != 0 || sb.st_size != SDCARD_SIZE) {
int fd = open(SDCARD_FILE, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600); int fd = open(SDCARD_FILE, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
ensure(sectrue * (fd >= 0), "open failed"); ensure(sectrue * (fd >= 0), "open failed");
for (int i = 0; i < SDCARD_SIZE / 16; i++) { r = ftruncate(fd, SDCARD_SIZE);
ssize_t s = write( ensure(sectrue * (r == 0), "truncate failed");
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 = close(fd); r = close(fd);
ensure(sectrue * (r == 0), "close failed"); ensure(sectrue * (r == 0), "close failed");
should_clear = 1;
} }
// mmap file // mmap file
@ -77,6 +75,10 @@ void sdcard_init(void) {
sdcard_buffer = (uint8_t *)map; sdcard_buffer = (uint8_t *)map;
if (should_clear) {
for (int i = 0; i < SDCARD_SIZE; ++i) sdcard_buffer[i] = 0xFF;
}
sdcard_powered = secfalse; sdcard_powered = secfalse;
atexit(sdcard_exit); atexit(sdcard_exit);

View File

@ -1,5 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import gzip
import logging import logging
import os import os
import platform import platform
@ -25,7 +24,6 @@ except Exception:
HERE = Path(__file__).parent.resolve() HERE = Path(__file__).parent.resolve()
MICROPYTHON = HERE / "build" / "unix" / "micropython" MICROPYTHON = HERE / "build" / "unix" / "micropython"
SRC_DIR = HERE / "src" SRC_DIR = HERE / "src"
SD_CARD_GZ = HERE / "trezor.sdcard.gz"
PROFILING_WRAPPER = HERE / "prof" / "prof.py" PROFILING_WRAPPER = HERE / "prof" / "prof.py"
@ -189,9 +187,6 @@ def cli(
elif temporary_profile: elif temporary_profile:
tempdir = tempfile.TemporaryDirectory(prefix="trezor-emulator-") tempdir = tempfile.TemporaryDirectory(prefix="trezor-emulator-")
profile_dir = Path(tempdir.name) 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: elif "TREZOR_PROFILE_DIR" in os.environ:
profile_dir = Path(os.environ["TREZOR_PROFILE_DIR"]) profile_dir = Path(os.environ["TREZOR_PROFILE_DIR"])

Binary file not shown.

View File

@ -14,7 +14,6 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import gzip
import tempfile import tempfile
from collections import defaultdict from collections import defaultdict
from pathlib import Path from pathlib import Path
@ -30,7 +29,6 @@ LOCAL_BUILD_PATHS = {
} }
CORE_SRC_DIR = ROOT / "core" / "src" CORE_SRC_DIR = ROOT / "core" / "src"
SD_CARD_GZ = ROOT / "core" / "trezor.sdcard.gz"
ENV = {"SDL_VIDEODRIVER": "dummy"} ENV = {"SDL_VIDEODRIVER": "dummy"}
@ -88,13 +86,11 @@ class EmulatorWrapper:
executable, self.profile_dir.name, storage=storage, headless=True, executable, self.profile_dir.name, storage=storage, headless=True,
) )
elif gen == "core": elif gen == "core":
with gzip.open(SD_CARD_GZ, "rb") as gz:
self.emulator = CoreEmulator( self.emulator = CoreEmulator(
executable, executable,
self.profile_dir.name, self.profile_dir.name,
storage=storage, storage=storage,
workdir=workdir, workdir=workdir,
sdcard=gz.read(),
headless=True, headless=True,
) )

Binary file not shown.