You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
3.1 KiB
114 lines
3.1 KiB
![]()
7 years ago
|
/*
|
||
![]()
7 years ago
|
* This file is part of the TREZOR project, https://trezor.io/
|
||
![]()
7 years ago
|
*
|
||
![]()
7 years ago
|
* 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/>.
|
||
![]()
7 years ago
|
*/
|
||
|
|
||
![]()
7 years ago
|
#include <fcntl.h>
|
||
![]()
6 years ago
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
![]()
7 years ago
|
#include <sys/mman.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/types.h>
|
||
![]()
6 years ago
|
#include <unistd.h>
|
||
![]()
7 years ago
|
|
||
|
#include "common.h"
|
||
![]()
6 years ago
|
#include "profile.h"
|
||
![]()
6 years ago
|
#include "sdcard.h"
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
#ifndef SDCARD_FILE
|
||
![]()
6 years ago
|
#define SDCARD_FILE profile_sdcard_path()
|
||
![]()
7 years ago
|
#endif
|
||
|
|
||
|
#define SDCARD_SIZE (32 * 1024 * 1024)
|
||
|
|
||
|
static uint8_t *sdcard_buffer;
|
||
|
static secbool sdcard_powered;
|
||
|
|
||
![]()
6 years ago
|
static void sdcard_exit(void) {
|
||
|
int r = munmap(sdcard_buffer, SDCARD_SIZE);
|
||
|
ensure(sectrue * (r == 0), "munmap failed");
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
7 years ago
|
void sdcard_init(void) {
|
||
![]()
6 years ago
|
int r;
|
||
|
|
||
|
// check whether the file exists and it has the correct size
|
||
|
struct stat sb;
|
||
|
r = stat(SDCARD_FILE, &sb);
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
// (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);
|
||
![]()
7 years ago
|
ensure(sectrue * (fd >= 0), "open failed");
|
||
![]()
6 years ago
|
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");
|
||
|
}
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
// mmap file
|
||
|
int fd = open(SDCARD_FILE, O_RDWR);
|
||
|
ensure(sectrue * (fd >= 0), "open failed");
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
void *map = mmap(0, SDCARD_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||
|
ensure(sectrue * (map != MAP_FAILED), "mmap failed");
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
sdcard_buffer = (uint8_t *)map;
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
sdcard_powered = secfalse;
|
||
![]()
7 years ago
|
|
||
![]()
6 years ago
|
atexit(sdcard_exit);
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
6 years ago
|
secbool sdcard_is_present(void) { return sectrue; }
|
||
|
|
||
![]()
7 years ago
|
secbool sdcard_power_on(void) {
|
||
![]()
6 years ago
|
sdcard_powered = sectrue;
|
||
|
return sectrue;
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
6 years ago
|
void sdcard_power_off(void) { sdcard_powered = secfalse; }
|
||
![]()
7 years ago
|
|
||
|
uint64_t sdcard_get_capacity_in_bytes(void) {
|
||
![]()
6 years ago
|
return sdcard_powered == sectrue ? SDCARD_SIZE : 0;
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
6 years ago
|
secbool sdcard_read_blocks(uint32_t *dest, uint32_t block_num,
|
||
|
uint32_t num_blocks) {
|
||
|
if (sectrue != sdcard_powered) {
|
||
|
return secfalse;
|
||
|
}
|
||
|
memcpy(dest, sdcard_buffer + block_num * SDCARD_BLOCK_SIZE,
|
||
|
num_blocks * SDCARD_BLOCK_SIZE);
|
||
|
return sectrue;
|
||
![]()
7 years ago
|
}
|
||
|
|
||
![]()
6 years ago
|
secbool sdcard_write_blocks(const uint32_t *src, uint32_t block_num,
|
||
|
uint32_t num_blocks) {
|
||
|
if (sectrue != sdcard_powered) {
|
||
|
return secfalse;
|
||
|
}
|
||
|
memcpy(sdcard_buffer + block_num * SDCARD_BLOCK_SIZE, src,
|
||
|
num_blocks * SDCARD_BLOCK_SIZE);
|
||
|
return sectrue;
|
||
![]()
7 years ago
|
}
|