2017-08-14 09:05:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) Pavol Rusnak, SatoshiLabs
|
|
|
|
*
|
|
|
|
* Licensed under TREZOR License
|
|
|
|
* see LICENSE file for details
|
|
|
|
*/
|
|
|
|
|
2017-12-15 00:00:00 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "common.h"
|
2017-12-14 23:06:09 +00:00
|
|
|
#include "sdcard.h"
|
2017-05-23 22:47:20 +00:00
|
|
|
|
2017-12-15 00:00:00 +00:00
|
|
|
#ifndef SDCARD_FILE
|
|
|
|
#define SDCARD_FILE "/var/tmp/trezor.sdcard"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define SDCARD_SIZE (32 * 1024 * 1024)
|
|
|
|
|
|
|
|
static uint8_t *sdcard_buffer;
|
|
|
|
static secbool sdcard_powered;
|
|
|
|
|
|
|
|
static void sdcard_exit(void)
|
|
|
|
{
|
|
|
|
int r = munmap(sdcard_buffer, SDCARD_SIZE);
|
|
|
|
ensure(sectrue * (r == 0), "munmap failed");
|
|
|
|
}
|
|
|
|
|
2017-10-20 13:25:24 +00:00
|
|
|
void sdcard_init(void) {
|
2017-12-15 00:00:00 +00:00
|
|
|
int r;
|
|
|
|
|
|
|
|
// check whether the file exists and it has the correct size
|
|
|
|
struct stat sb;
|
|
|
|
r = stat(SDCARD_FILE, &sb);
|
|
|
|
|
|
|
|
// (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 = close(fd);
|
|
|
|
ensure(sectrue * (r == 0), "close failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
// mmap file
|
|
|
|
int fd = open(SDCARD_FILE, O_RDWR);
|
|
|
|
ensure(sectrue * (fd >= 0), "open failed");
|
|
|
|
|
|
|
|
void *map = mmap(0, SDCARD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
|
|
|
ensure(sectrue * (map != MAP_FAILED), "mmap failed");
|
|
|
|
|
|
|
|
sdcard_buffer = (uint8_t *)map;
|
|
|
|
|
|
|
|
sdcard_powered = secfalse;
|
|
|
|
|
|
|
|
atexit(sdcard_exit);
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool sdcard_is_present(void) {
|
2017-12-15 00:00:00 +00:00
|
|
|
return sectrue;
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 21:51:39 +00:00
|
|
|
secbool sdcard_power_on(void) {
|
2017-12-15 00:00:00 +00:00
|
|
|
sdcard_powered = sectrue;
|
|
|
|
return sectrue;
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 16:54:04 +00:00
|
|
|
void sdcard_power_off(void) {
|
2017-12-15 00:00:00 +00:00
|
|
|
sdcard_powered = secfalse;
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t sdcard_get_capacity_in_bytes(void) {
|
2017-12-15 00:00:00 +00:00
|
|
|
return sdcard_powered == sectrue ? SDCARD_SIZE : 0;
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 22:26:44 +00:00
|
|
|
secbool sdcard_read_blocks(uint32_t *dest, uint32_t block_num, uint32_t num_blocks) {
|
2017-12-15 00:00:00 +00:00
|
|
|
if (sectrue != sdcard_powered) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memcpy(dest, sdcard_buffer + block_num * SDCARD_BLOCK_SIZE, num_blocks * SDCARD_BLOCK_SIZE);
|
|
|
|
return sectrue;
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-29 22:26:44 +00:00
|
|
|
secbool sdcard_write_blocks(const uint32_t *src, uint32_t block_num, uint32_t num_blocks) {
|
2017-12-15 00:00:00 +00:00
|
|
|
if (sectrue != sdcard_powered) {
|
|
|
|
return secfalse;
|
|
|
|
}
|
|
|
|
memcpy(sdcard_buffer + block_num * SDCARD_BLOCK_SIZE, src, num_blocks * SDCARD_BLOCK_SIZE);
|
|
|
|
return sectrue;
|
2017-05-23 22:47:20 +00:00
|
|
|
}
|