2017-11-06 14:46:27 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2017-11-06 14:46:27 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* 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/>.
|
2017-11-06 14:46:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2019-03-29 15:26:02 +00:00
|
|
|
#include <string.h>
|
2017-11-06 14:46:27 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include STM32_HAL_H
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "display.h"
|
|
|
|
#include "flash.h"
|
|
|
|
#include "image.h"
|
|
|
|
#include "rng.h"
|
|
|
|
#include "sbu.h"
|
|
|
|
#include "sdcard.h"
|
|
|
|
#include "secbool.h"
|
|
|
|
#include "touch.h"
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static void progress_callback(int pos, int len) { display_printf("."); }
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static void flash_from_sdcard(uint8_t sector, uint32_t source,
|
|
|
|
uint32_t length) {
|
|
|
|
static uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)];
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(sectrue * (source % SDCARD_BLOCK_SIZE == 0),
|
|
|
|
"source not a multiple of block size");
|
|
|
|
ensure(sectrue * (length % SDCARD_BLOCK_SIZE == 0),
|
|
|
|
"length not a multiple of block size");
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
for (uint32_t i = 0; i < length / SDCARD_BLOCK_SIZE; i++) {
|
|
|
|
display_printf("read %d\n", (unsigned int)(i + source / SDCARD_BLOCK_SIZE));
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(sdcard_read_blocks(buf, i + source / SDCARD_BLOCK_SIZE, 1),
|
|
|
|
"sdcard_read_blocks");
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
for (uint32_t j = 0; j < SDCARD_BLOCK_SIZE / sizeof(uint32_t); j++) {
|
|
|
|
ensure(flash_write_word(
|
|
|
|
sector, i * SDCARD_BLOCK_SIZE + j * sizeof(uint32_t), buf[j]),
|
|
|
|
NULL);
|
2017-11-06 14:46:27 +00:00
|
|
|
}
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2017-11-06 14:46:27 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
int main(void) {
|
|
|
|
sdcard_init();
|
|
|
|
touch_init();
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
display_orientation(0);
|
|
|
|
display_clear();
|
|
|
|
display_backlight(255);
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(sdcard_is_present(), "sdcard_is_present");
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
display_printf("updating boardloader + bootloader\n");
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
static const uint8_t sectors[] = {
|
|
|
|
FLASH_SECTOR_BOARDLOADER_START,
|
|
|
|
1,
|
|
|
|
FLASH_SECTOR_BOARDLOADER_END,
|
|
|
|
FLASH_SECTOR_BOOTLOADER,
|
|
|
|
};
|
|
|
|
display_printf("erasing sectors");
|
|
|
|
ensure(flash_erase_sectors(sectors, sizeof(sectors), progress_callback),
|
|
|
|
"flash_erase_sectors");
|
|
|
|
display_printf("\n");
|
|
|
|
display_printf("erased\n");
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(flash_unlock_write(), NULL);
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
ensure(sdcard_power_on(), NULL);
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
#define BOARDLOADER_CHUNK_SIZE (16 * 1024)
|
|
|
|
#define BOARDLOADER_TOTAL_SIZE (3 * BOARDLOADER_CHUNK_SIZE)
|
|
|
|
#define BOOTLOADER_TOTAL_SIZE (128 * 1024)
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
flash_from_sdcard(FLASH_SECTOR_BOARDLOADER_START, 0 * BOARDLOADER_CHUNK_SIZE,
|
|
|
|
BOARDLOADER_CHUNK_SIZE);
|
|
|
|
flash_from_sdcard(1, 1 * BOARDLOADER_CHUNK_SIZE, BOARDLOADER_CHUNK_SIZE);
|
|
|
|
flash_from_sdcard(FLASH_SECTOR_BOARDLOADER_END, 2 * BOARDLOADER_CHUNK_SIZE,
|
|
|
|
BOARDLOADER_CHUNK_SIZE);
|
|
|
|
flash_from_sdcard(FLASH_SECTOR_BOOTLOADER, BOARDLOADER_TOTAL_SIZE,
|
|
|
|
BOOTLOADER_TOTAL_SIZE);
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
display_printf("done\n");
|
|
|
|
sdcard_power_off();
|
|
|
|
ensure(flash_lock_write(), NULL);
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return 0;
|
2017-11-06 14:46:27 +00:00
|
|
|
}
|