1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 09:28:13 +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
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);

View File

@ -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"])

Binary file not shown.

View File

@ -14,7 +14,6 @@
# 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>.
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()

Binary file not shown.