parent
7c630141d4
commit
ba5b44d0c5
@ -0,0 +1,15 @@
|
|||||||
|
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
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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;
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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
|
@ -0,0 +1,114 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 <libopencm3/stm32/flash.h>
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
void flash_lock(void) {}
|
||||||
|
void flash_unlock(void) {}
|
||||||
|
|
||||||
|
void flash_clear_status_flags(void) {}
|
||||||
|
|
||||||
|
void flash_lock_option_bytes(void) {}
|
||||||
|
void flash_unlock_option_bytes(void) {}
|
||||||
|
|
||||||
|
void flash_program_option_bytes(uint32_t data) {
|
||||||
|
(void) data;
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t sector_to_offset(uint8_t sector) {
|
||||||
|
switch (sector) {
|
||||||
|
case 0:
|
||||||
|
return 0x0;
|
||||||
|
case 1:
|
||||||
|
return 0x4000;
|
||||||
|
case 2:
|
||||||
|
return 0x8000;
|
||||||
|
case 3:
|
||||||
|
return 0xC000;
|
||||||
|
case 4:
|
||||||
|
return 0x10000;
|
||||||
|
case 5:
|
||||||
|
return 0x20000;
|
||||||
|
case 6:
|
||||||
|
return 0x40000;
|
||||||
|
case 7:
|
||||||
|
return 0x60000;
|
||||||
|
case 8:
|
||||||
|
return 0x80000;
|
||||||
|
default:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *sector_to_address(uint8_t sector) {
|
||||||
|
ssize_t offset = sector_to_offset(sector);
|
||||||
|
if (offset < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (void *) (FLASH_ORIGIN + offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
static ssize_t sector_to_size(uint8_t sector) {
|
||||||
|
ssize_t start = sector_to_offset(sector);
|
||||||
|
if (start < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t end = sector_to_offset(sector + 1);
|
||||||
|
if (end < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return end - start;
|
||||||
|
}
|
||||||
|
|
||||||
|
void flash_erase_sector(uint8_t sector, uint32_t program_size) {
|
||||||
|
(void) program_size;
|
||||||
|
|
||||||
|
void *address = sector_to_address(sector);
|
||||||
|
if (address == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t size = sector_to_size(sector);
|
||||||
|
if (size < 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(address, 0xFF, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void flash_erase_all_sectors(uint32_t program_size) {
|
||||||
|
(void) program_size;
|
||||||
|
|
||||||
|
memset(emulator_flash_base, 0xFF, FLASH_TOTAL_SIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
void flash_program_word(uint32_t address, uint32_t data) {
|
||||||
|
MMIO32(address) = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void flash_program_byte(uint32_t address, uint8_t data) {
|
||||||
|
MMIO8(address) = data;
|
||||||
|
}
|
@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 "oled.h"
|
||||||
|
|
||||||
|
#if HEADLESS
|
||||||
|
|
||||||
|
void oledInit(void) {}
|
||||||
|
void oledRefresh(void) {}
|
||||||
|
void emulatorPoll(void) {}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
|
||||||
|
static SDL_Renderer *renderer = NULL;
|
||||||
|
static SDL_Texture *texture = NULL;
|
||||||
|
|
||||||
|
void oledInit(void) {
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
|
||||||
|
fprintf(stderr, "Failed to initialize SDL: %s\n", SDL_GetError());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
atexit(SDL_Quit);
|
||||||
|
|
||||||
|
SDL_Window *window = SDL_CreateWindow("TREZOR", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, OLED_WIDTH, OLED_HEIGHT, 0);
|
||||||
|
if (window == NULL) {
|
||||||
|
fprintf(stderr, "Failed to create window: %s\n", SDL_GetError());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||||
|
if (!renderer) {
|
||||||
|
fprintf(stderr, "Failed to create renderer: %s\n", SDL_GetError());
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, OLED_WIDTH, OLED_HEIGHT);
|
||||||
|
|
||||||
|
oledClear();
|
||||||
|
oledRefresh();
|
||||||
|
}
|
||||||
|
|
||||||
|
void oledRefresh(void) {
|
||||||
|
const uint8_t *buffer = oledGetBuffer();
|
||||||
|
|
||||||
|
static uint32_t data[OLED_HEIGHT][OLED_WIDTH];
|
||||||
|
|
||||||
|
for (size_t i = 0; i < OLED_BUFSIZE; i++) {
|
||||||
|
int x = (OLED_BUFSIZE - 1 - i) % OLED_WIDTH;
|
||||||
|
int y = (OLED_BUFSIZE - 1 - i) / OLED_WIDTH * 8 + 7;
|
||||||
|
|
||||||
|
for (uint8_t shift = 0; shift < 8; shift++, y--) {
|
||||||
|
bool set = (buffer[i] >> shift) & 1;
|
||||||
|
data[y][x] = set ? 0xFFFFFFFF : 0xFF000000;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_UpdateTexture(texture, NULL, data, OLED_WIDTH * sizeof(uint32_t));
|
||||||
|
SDL_RenderCopy(renderer, texture, NULL, NULL);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void emulatorPoll(void) {
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
|
if (SDL_PollEvent(&event)) {
|
||||||
|
if (event.type == SDL_QUIT) {
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 "rng.h"
|
||||||
|
|
||||||
|
uint32_t random32(void) {
|
||||||
|
static uint32_t last = 0;
|
||||||
|
uint32_t new;
|
||||||
|
|
||||||
|
do {
|
||||||
|
emulatorRandom(&new, sizeof(new));
|
||||||
|
} while (last == new);
|
||||||
|
|
||||||
|
last = new;
|
||||||
|
return new;
|
||||||
|
}
|
@ -0,0 +1,95 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include <libopencm3/stm32/flash.h>
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
#include "oled.h"
|
||||||
|
#include "rng.h"
|
||||||
|
#include "setup.h"
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
#define EMULATOR_FLASH_FILE "emulator.img"
|
||||||
|
|
||||||
|
void *emulator_flash_base = NULL;
|
||||||
|
|
||||||
|
uint32_t __stack_chk_guard;
|
||||||
|
|
||||||
|
static int urandom = -1;
|
||||||
|
|
||||||
|
static void setup_urandom(void);
|
||||||
|
static void setup_flash(void);
|
||||||
|
|
||||||
|
void setup(void) {
|
||||||
|
setup_urandom();
|
||||||
|
setup_flash();
|
||||||
|
}
|
||||||
|
|
||||||
|
void emulatorRandom(void *buffer, size_t size) {
|
||||||
|
ssize_t n = read(urandom, buffer, size);
|
||||||
|
if (n < 0 || ((size_t) n) != size) {
|
||||||
|
perror("Failed to read /dev/urandom");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setup_urandom(void) {
|
||||||
|
urandom = open("/dev/urandom", O_RDONLY);
|
||||||
|
if (urandom < 0) {
|
||||||
|
perror("Failed to open /dev/urandom");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void setup_flash(void) {
|
||||||
|
int fd = open(EMULATOR_FLASH_FILE, O_RDWR | O_SYNC | O_CREAT, 0644);
|
||||||
|
if (fd < 0) {
|
||||||
|
perror("Failed to open flash emulation file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
off_t length = lseek(fd, 0, SEEK_END);
|
||||||
|
if (length < 0) {
|
||||||
|
perror("Failed to read length of flash emulation file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
emulator_flash_base = mmap(NULL, FLASH_TOTAL_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
|
if (emulator_flash_base == MAP_FAILED) {
|
||||||
|
perror("Failed to map flash emulation file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length < FLASH_TOTAL_SIZE) {
|
||||||
|
if (ftruncate(fd, FLASH_TOTAL_SIZE) != 0) {
|
||||||
|
perror("Failed to initialize flash emulation file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Initialize the flash */
|
||||||
|
flash_erase_all_sectors(FLASH_CR_PROGRAM_X32);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 "strl.h"
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
size_t strlcpy(char *dst, const char *src, size_t size) {
|
||||||
|
if (size == 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < size - 1; i++) {
|
||||||
|
dst[i] = src[i];
|
||||||
|
|
||||||
|
if (src[i] == '\0') {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dst[size - 1] = '\0';
|
||||||
|
return size - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t strlcat(char *dst, const char *src, size_t size) {
|
||||||
|
size_t n = strnlen(dst, size);
|
||||||
|
|
||||||
|
return n + strlcpy(&dst[n], src, size - n);
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 __STRL_H__
|
||||||
|
#define __STRL_H__
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
size_t strlcpy(char *dst, const char *src, size_t size);
|
||||||
|
size_t strlcat(char *dst, const char *src, size_t size);
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 <time.h>
|
||||||
|
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
void timer_init(void) {}
|
||||||
|
|
||||||
|
uint32_t timer_ms(void) {
|
||||||
|
struct timespec t;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &t);
|
||||||
|
|
||||||
|
uint32_t msec = t.tv_sec * 1000 + (t.tv_nsec / 1000000);
|
||||||
|
return msec;
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 <arpa/inet.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#define TREZOR_UDP_PORT 21324
|
||||||
|
|
||||||
|
static int fd = -1;
|
||||||
|
static struct sockaddr_in from;
|
||||||
|
static socklen_t fromlen;
|
||||||
|
|
||||||
|
void emulatorSocketInit(void) {
|
||||||
|
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
|
if (fd < 0) {
|
||||||
|
perror("Failed to create socket");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
fromlen = 0;
|
||||||
|
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(TREZOR_UDP_PORT);
|
||||||
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||||
|
|
||||||
|
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
|
||||||
|
perror("Failed to bind socket");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t emulatorSocketRead(void *buffer, size_t size) {
|
||||||
|
fromlen = sizeof(from);
|
||||||
|
ssize_t n = recvfrom(fd, buffer, size, MSG_DONTWAIT, (struct sockaddr *) &from, &fromlen);
|
||||||
|
|
||||||
|
if (n < 0) {
|
||||||
|
if (errno != EAGAIN && errno != EWOULDBLOCK) {
|
||||||
|
perror("Failed to read socket");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static const char msg_ping[] = { 'P', 'I', 'N', 'G', 'P', 'I', 'N', 'G' };
|
||||||
|
static const char msg_pong[] = { 'P', 'O', 'N', 'G', 'P', 'O', 'N', 'G' };
|
||||||
|
|
||||||
|
if (n == sizeof(msg_ping) && memcmp(buffer, msg_ping, sizeof(msg_ping)) == 0) {
|
||||||
|
emulatorSocketWrite(msg_pong, sizeof(msg_pong));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t emulatorSocketWrite(const void *buffer, size_t size) {
|
||||||
|
if (fromlen > 0) {
|
||||||
|
ssize_t n = sendto(fd, buffer, size, MSG_DONTWAIT, (const struct sockaddr *) &from, fromlen);
|
||||||
|
if (n < 0 || ((size_t) n) != size) {
|
||||||
|
perror("Failed to write socket");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return size;
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the TREZOR project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (C) 2017 Saleem Rashid <trezor@saleemrashid.com>
|
||||||
|
*
|
||||||
|
* 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 <stdint.h>
|
||||||
|
|
||||||
|
#include "usb.h"
|
||||||
|
|
||||||
|
#include "messages.h"
|
||||||
|
#include "timer.h"
|
||||||
|
|
||||||
|
static volatile char tiny = 0;
|
||||||
|
|
||||||
|
void usbInit(void) {
|
||||||
|
emulatorSocketInit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void usbPoll(void) {
|
||||||
|
emulatorPoll();
|
||||||
|
|
||||||
|
static uint8_t buffer[64];
|
||||||
|
if (emulatorSocketRead(buffer, sizeof(buffer)) > 0) {
|
||||||
|
if (!tiny) {
|
||||||
|
msg_read(buffer, sizeof(buffer));
|
||||||
|
} else {
|
||||||
|
msg_read_tiny(buffer, sizeof(buffer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint8_t *data = msg_out_data();
|
||||||
|
|
||||||
|
#if DEBUG_LINK
|
||||||
|
if (data == NULL) {
|
||||||
|
data = msg_debug_out_data();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (data != NULL) {
|
||||||
|
emulatorSocketWrite(data, 64);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char usbTiny(char set) {
|
||||||
|
char old = tiny;
|
||||||
|
tiny = set;
|
||||||
|
return old;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usbSleep(uint32_t millis) {
|
||||||
|
uint32_t start = timer_ms();
|
||||||
|
|
||||||
|
while ((timer_ms() - start) < millis) {
|
||||||
|
usbPoll();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue