Skip to content
This repository has been archived by the owner on Apr 16, 2019. It is now read-only.

Emulator support #268

Merged
merged 10 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build/
*.bin
*.elf
*.hex
*.img
*.list
*.srec
*.log
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
[submodule "vendor/nanopb"]
path = vendor/nanopb
url = https://github.com/nanopb/nanopb.git
[submodule "python-trezor"]
path = vendor/python-trezor
url = https://github.com/trezor/python-trezor.git
24 changes: 18 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ env:
- DEBUG_LINK=0 FASTFLASH=1
- DEBUG_LINK=1 FASTFLASH=1

matrix:
include:
- addons:
apt:
packages:
- gcc-multilib
env:
- EMULATOR=1 HEADLESS=1
- DEBUG_LINK=1
before_script:
- sed -i '/hidapi/d' "vendor/python-trezor/requirements.txt"
Copy link
Member

@prusnak prusnak Dec 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python-trezor contains --disable-hidapi option in setup.py, maybe we can try using pip install with --install-option - I remember I was trying to use the same trick for trezor-core, but I failed, though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prusnak I tried that at least 5 different ways and gave up. 😆 pip will not pass provided options to ./setup.py egg_info which is what it uses to gather dependencies.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, glad to know I am the only one who failed on this. Thanks :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@prusnak There is an issue here but I don't imagine it will be fixed pypa/pip#4383

- pip install --user --requirement "vendor/python-trezor/requirements.txt" rlp pytest requests[security]
- pip install --user --no-deps "vendor/python-trezor"
script:
- script/cibuild
- script/test

install:
- curl -LO "https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/protoc-${PROTOBUF_VERSION}-linux-x86_64.zip"
- unzip "protoc-${PROTOBUF_VERSION}-linux-x86_64.zip" -d protoc
- export PATH="$(pwd)/protoc/bin:$PATH"
- pip2 install --user "protobuf==${PROTOBUF_VERSION}"

script:
- make -C vendor/libopencm3 lib/stm32/f2
- make -C vendor/nanopb/generator/proto
- make
- script/cibuild
- make -C bootloader
- make -C fastflash
- make -C firmware/protob
- make -C firmware
- make -C demo

notifications:
Expand Down
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
ifneq ($(EMULATOR),1)
OBJS += startup.o
endif

OBJS += buttons.o
OBJS += layout.o
OBJS += oled.o
OBJS += rng.o
OBJS += serialno.o

ifneq ($(EMULATOR),1)
OBJS += setup.o
endif

OBJS += util.o
OBJS += memory.o

ifneq ($(EMULATOR),1)
OBJS += timer.o
endif

OBJS += gen/bitmaps.o
OBJS += gen/fonts.o

Expand Down
71 changes: 57 additions & 14 deletions Makefile.include
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
TOP_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
TOOLCHAIN_DIR ?= $(TOP_DIR)vendor/libopencm3

ifeq ($(EMULATOR),1)
CC = gcc
LD = gcc
OBJCOPY = objcopy
OBJDUMP = objdump
AR = ar
AS = as

OPTFLAGS ?= -O3
DBGFLAGS ?= -g3
CPUFLAGS ?= -m32
else
PREFIX ?= arm-none-eabi-
CC = $(PREFIX)gcc
LD = $(PREFIX)gcc
Expand All @@ -15,6 +27,7 @@ OPTFLAGS ?= -O3
DBGFLAGS ?= -g -DNDEBUG
CPUFLAGS ?= -mcpu=cortex-m3 -mthumb
FPUFLAGS ?= -msoft-float
endif

CFLAGS += $(OPTFLAGS) \
$(DBGFLAGS) \
Expand Down Expand Up @@ -55,24 +68,36 @@ CFLAGS += $(OPTFLAGS) \
-I$(TOP_DIR)vendor/trezor-crypto/ed25519-donna \
-I$(TOP_DIR)vendor/trezor-qrenc

ifeq ($(EMULATOR),1)
CFLAGS += -DEMULATOR=1

ifeq ($(HEADLESS),1)
CFLAGS += -DHEADLESS=1
else
CFLAGS += -DHEADLESS=0

CFLAGS += $(shell pkg-config --cflags sdl2)
LDLIBS += $(shell pkg-config --libs sdl2)
endif

CFLAGS += -include $(TOP_DIR)emulator/emulator.h
CFLAGS += -include stdio.h

LDFLAGS += -L$(TOP_DIR) \
-L$(TOP_DIR)emulator \
$(CPUFLAGS)

LDLIBS += -ltrezor -lemulator
LIBDEPS += $(TOP_DIR)/libtrezor.a $(TOP_DIR)emulator/libemulator.a
else
ifdef APPVER
CFLAGS += -DAPPVER=$(APPVER)
LDSCRIPT = $(TOP_DIR)/memory_app_$(APPVER).ld
else
LDSCRIPT = $(TOP_DIR)/memory.ld
endif

ifeq ($(MEMORY_PROTECT), 0)
CFLAGS += -DMEMORY_PROTECT=0
else
CFLAGS += -DMEMORY_PROTECT=1
endif

ifeq ($(DEBUG_RNG), 1)
CFLAGS += -DDEBUG_RNG=1
else
CFLAGS += -DDEBUG_RNG=0
endif
CFLAGS += -DEMULATOR=0

LDFLAGS += --static \
-Wl,--start-group \
Expand All @@ -82,13 +107,31 @@ LDFLAGS += --static \
-Wl,--end-group \
-L$(TOP_DIR) \
-L$(TOOLCHAIN_DIR)/lib \
-L$(TOOLCHAIN_DIR)/lib/stm32/f2 \
-T$(LDSCRIPT) \
-nostartfiles \
-Wl,--gc-sections \
$(CPUFLAGS) \
$(FPUFLAGS)

LDLIBS += -ltrezor
LIBDEPS += $(TOP_DIR)/libtrezor.a

LDLIBS += -lopencm3_stm32f2
LIBDEPS += $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f2.a
endif

ifeq ($(MEMORY_PROTECT), 0)
CFLAGS += -DMEMORY_PROTECT=0
else
CFLAGS += -DMEMORY_PROTECT=1
endif

ifeq ($(DEBUG_RNG), 1)
CFLAGS += -DDEBUG_RNG=1
else
CFLAGS += -DDEBUG_RNG=0
endif

all: $(NAME).bin

flash: $(NAME).bin
Expand Down Expand Up @@ -127,8 +170,8 @@ $(NAME).srec: $(NAME).elf
$(NAME).list: $(NAME).elf
$(OBJDUMP) -S $(NAME).elf > $(NAME).list

$(NAME).elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/libopencm3_stm32f2.a $(TOP_DIR)/libtrezor.a
$(LD) -o $(NAME).elf $(OBJS) -ltrezor -lopencm3_stm32f2 $(LDFLAGS)
$(NAME).elf: $(OBJS) $(LDSCRIPT) $(LIBDEPS)
$(LD) -o $(NAME).elf $(OBJS) $(LDLIBS) $(LDFLAGS)

%.o: %.s Makefile
$(AS) $(CPUFLAGS) -o $@ $<
Expand Down
25 changes: 22 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://trezor.io/

## How to build TREZOR firmware?

1. <a href="https://docs.docker.com/engine/installation/">Install Docker</a>
1. [Install Docker](https://docs.docker.com/engine/installation/)
2. `git clone https://github.com/trezor/trezor-mcu.git`
3. `cd trezor-mcu`
4. `./build-firmware.sh TAG` (where TAG is v1.5.0 for example, if left blank the script builds latest commit in master branch)
Expand All @@ -15,7 +15,7 @@ This creates file `build/trezor-TAG.bin` and prints its fingerprint and size at

## How to build TREZOR bootloader?

1. <a href="https://docs.docker.com/engine/installation/">Install Docker</a>
1. [Install Docker](https://docs.docker.com/engine/installation/)
2. `git clone https://github.com/trezor/trezor-mcu.git`
3. `cd trezor-mcu`
4. `./build-bootloader.sh TAG` (where TAG is bl1.3.2 for example, if left blank the script builds latest commit in master branch)
Expand All @@ -34,5 +34,24 @@ Step 3 should produce the same sha256 fingerprint like your local build (for the

**WARNING: This will erase the recovery seed stored on the device! You should never do this on TREZOR that contains coins!**

1. Install python-trezor: `pip install trezor` (<a href="https://github.com/trezor/python-trezor">more info</a>)
1. Install python-trezor: `pip install trezor` ([more info](https://github.com/trezor/python-trezor))
2. `trezorctl firmware_update -f build/trezor-TAG.bin`

## Building for development

If you want to build device firmware, make sure you have the
[GNU ARM Embedded toolchain](https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads) installed.

* If you want to build the emulator instead of the firmware, run `export EMULATOR=1 TREZOR_TRANSPORT_V1=1`
* If you want to build with the debug link, run `export DEBUG_LINK=1`. Use this if you want to run the device tests.
* When you change these variables, use `script/setup` to clean the repository

1. To initialize the repository, run `script/setup`
2. To build the firmware or emulator, run `script/cibuild`

If you are building device firmware, the firmware will be in `firmware/trezor.bin`.

You can launch the emulator using `firmware/trezor.elf`. To use `trezorctl` with the emulator, use
`trezorctl -t udp` (for example, `trezorctl -t udp get_features`).

If `trezorctl -t udp` appears to hang, make sure you have run `export TREZOR_TRANSPORT_V1=1`.
8 changes: 7 additions & 1 deletion buttons.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@

struct buttonState button;

#if !EMULATOR
uint16_t buttonRead(void) {
return gpio_port_read(BTN_PORT);
}
#endif

void buttonUpdate()
{
uint16_t state;
static uint16_t last_state = BTN_PIN_YES | BTN_PIN_NO;

state = gpio_port_read(BTN_PORT);
state = buttonRead();

if ((state & BTN_PIN_YES) == 0) { // Yes button is down
if ((last_state & BTN_PIN_YES) == 0) { // last Yes was down
Expand Down
1 change: 1 addition & 0 deletions buttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct buttonState {

extern struct buttonState button;

uint16_t buttonRead(void);
void buttonUpdate(void);

#ifndef BTN_PORT
Expand Down
17 changes: 17 additions & 0 deletions emulator/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
EMULATOR := 1

OBJS += setup.o

OBJS += buttons.o
OBJS += flash.o
OBJS += oled.o
OBJS += rng.o
OBJS += timer.o
OBJS += udp.o

OBJS += strl.o

libemulator.a: $(OBJS)
$(AR) rcs $@ $(OBJS)

include ../Makefile.include
40 changes: 40 additions & 0 deletions emulator/buttons.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (C) 2017 Saleem Rashid <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "buttons.h"

#if !HEADLESS
#include <SDL.h>
#endif

uint16_t buttonRead(void) {
uint16_t state = 0;

#if !HEADLESS
const uint8_t *scancodes = SDL_GetKeyboardState(NULL);
if (scancodes[SDL_SCANCODE_LEFT]) {
state |= BTN_PIN_NO;
}
if (scancodes[SDL_SCANCODE_RIGHT]) {
state |= BTN_PIN_YES;
}
#endif

return ~state;
}
40 changes: 40 additions & 0 deletions emulator/emulator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This file is part of the TREZOR project, https://trezor.io/
*
* Copyright (C) 2017 Saleem Rashid <[email protected]>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __EMULATOR_H__
#define __EMULATOR_H__

#if EMULATOR

#include "strl.h"

#include <stddef.h>

extern void *emulator_flash_base;

void emulatorPoll(void);
void emulatorRandom(void *buffer, size_t size);

void emulatorSocketInit(void);
size_t emulatorSocketRead(void *buffer, size_t size);
size_t emulatorSocketWrite(const void *buffer, size_t size);

#endif

#endif
Loading