1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-15 19:08:07 +00:00

unix: user profiles support for emulator

This commit is contained in:
Milan Rossa 2018-10-10 14:35:40 +02:00 committed by Pavol Rusnak
parent 32fe5d20af
commit fc9bc85ea3
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
7 changed files with 199 additions and 2 deletions

View File

@ -221,6 +221,7 @@ SOURCE_UNIX = [
'vendor/micropython/ports/unix/input.c',
'vendor/micropython/ports/unix/alloc.c',
'embed/unix/common.c',
'embed/unix/profile.c',
'embed/unix/flash.c',
'embed/unix/main.c',
'embed/unix/rng.c',

View File

@ -5,3 +5,23 @@
1. [build](build.md) the emulator
2. run `emu.sh`
3. to use [bridge](https://github.com/trezor/trezord-go) with the emulator support, start it with `trezord -e 21324`
## Profiles
To run emulator with different flash and sdcard files set the environment
variable **TREZOR_PROFILE** like so:
```sh
TREZOR_PROFILE=foobar ./emu.sh
```
This will create a profile directory in your home ``` ~/.trezoremu/foobar```
containing emulator run files.
Alternatively you can set a full path like so:
```sh
TREZOR_PROFILE=/var/tmp/foobar ./emu.sh
```
When the **TREZOR_PROFILE** is not set the default is ```/var/tmp``` .

View File

@ -27,9 +27,10 @@
#include "common.h"
#include "flash.h"
#include "profile.h"
#ifndef FLASH_FILE
#define FLASH_FILE "/var/tmp/trezor.flash"
#define FLASH_FILE profile_flash_path()
#endif
#define SECTOR_COUNT 24

View File

@ -48,6 +48,7 @@
#include "extmod/misc.h"
#include "genhdr/mpversion.h"
#include "input.h"
#include "profile.h"
// Command line options, with their defaults
STATIC bool compile_only = false;
@ -404,6 +405,10 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
MP_NOINLINE int main_(int argc, char **argv);
int main(int argc, char **argv) {
// Through TREZOR_PROFILE you can set the directory for trezor.flash file.
profile_init();
#if MICROPY_PY_THREAD
mp_thread_init();
#endif

128
embed/unix/profile.c Normal file
View File

@ -0,0 +1,128 @@
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <libgen.h>
#include <unistd.h>
#include "profile.h"
#include "common.h"
static int mkpath(const char *path, mode_t mode) {
if (!path) {
errno = EINVAL;
return 1;
}
struct stat sb;
if (!stat(strdup(path), &sb)) {
return 0;
}
// dirname has multiple incompatible implementations.
// Some modify input argument some share output buffer.
char *pathdup = strdup(path);
char *subpath = strdup(dirname(pathdup));
mkpath(subpath, mode);
free(pathdup);
free(subpath);
return mkdir(path, mode);
}
void profile_init(void) {
const char *dir = profile_dir();
if (mkpath(dir, 0755)) {
perror(dir);
printf("!!! Unable to initialize profile directory `%s`. Quitting\n", dir);
exit(1);
}
printf("Profile directory: %s\n", dir);
}
const char *profile_dir(void) {
static const char *_profile_dir;
if (_profile_dir) {
return _profile_dir;
}
char *trezor_profile = getenv("TREZOR_PROFILE");
if (!trezor_profile || strlen(trezor_profile) < 1) {
trezor_profile = PROFILE_DEFAULT;
}
char *path;
if (trezor_profile[0] == '/') {
// TREZOR_PROFILE is a full path to profile directory
path = strdup(trezor_profile);
} else {
// TREZOR_PROFILE is just a profile name and will be put in ~/.trezoremu/
int print_length = asprintf(&path, "%s/" PROFILE_HOMEDOT "/%s", getenv("HOME"), trezor_profile);
if (print_length == -1) {
path = NULL;
}
}
if (!path) { // last resort fallback
path = PROFILE_DEFAULT;
}
_profile_dir = path;
return _profile_dir;
}
const char *profile_flash_path(void) {
static char *_flash_path;
if (_flash_path) {
return _flash_path;
}
if (asprintf(&_flash_path, "%s/trezor.flash", profile_dir()) < 0) {
_flash_path = NULL;
}
if (!_flash_path) { // last resort fallback
_flash_path = PROFILE_DEFAULT "/trezor.flash";
}
return _flash_path;
}
const char *profile_sdcard_path(void) {
static char *_sdcard_path;
if (_sdcard_path) {
return _sdcard_path;
}
if (asprintf(&_sdcard_path, "%s/trezor.sdcard", profile_dir()) < 0) {
_sdcard_path = NULL;
}
if (!_sdcard_path) { // last resort fallback
_sdcard_path = PROFILE_DEFAULT "/trezor.sdcard";
}
return _sdcard_path;
}

41
embed/unix/profile.h Normal file
View File

@ -0,0 +1,41 @@
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (c) SatoshiLabs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __TREZOR_PROFILE_H__
#define __TREZOR_PROFILE_H__
// TREZOR_PROFILE environment variable can be a full path
// or just a name that will result in the path:
// ~/${PROFILE_HOMEDOT}/${TREZOR_PROFILE}
// If the variable is not set the default is ${PROFILE_DEFAULT}
#ifndef PROFILE_DEFAULT
#define PROFILE_DEFAULT "/var/tmp"
#endif
#ifndef PROFILE_HOMEDOT
#define PROFILE_HOMEDOT ".trezoremu"
#endif
void profile_init(void);
const char *profile_dir(void);
const char *profile_flash_path(void);
const char *profile_sdcard_path(void);
#endif // __TREZOR_PROFILE_H__

View File

@ -27,9 +27,10 @@
#include "common.h"
#include "sdcard.h"
#include "profile.h"
#ifndef SDCARD_FILE
#define SDCARD_FILE "/var/tmp/trezor.sdcard"
#define SDCARD_FILE profile_sdcard_path()
#endif
#define SDCARD_SIZE (32 * 1024 * 1024)