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
|
|
|
*/
|
|
|
|
|
2024-11-01 11:55:31 +00:00
|
|
|
#include <trezor_model.h>
|
|
|
|
#include <trezor_rtl.h>
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <gfx/gfx_draw.h>
|
|
|
|
#include <gfx/terminal.h>
|
|
|
|
#include <io/display.h>
|
|
|
|
#include <io/sdcard.h>
|
|
|
|
#include <io/touch.h>
|
|
|
|
#include <sec/rng.h>
|
|
|
|
#include <sys/bootutils.h>
|
|
|
|
#include <sys/system.h>
|
|
|
|
#include <sys/systimer.h>
|
|
|
|
#include <util/flash.h>
|
|
|
|
#include <util/image.h>
|
|
|
|
#include <util/rsod.h>
|
2017-11-06 14:46:27 +00:00
|
|
|
#include "flash.h"
|
|
|
|
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
2024-11-15 09:07:01 +00:00
|
|
|
#include <sec/hash_processor.h>
|
2023-12-15 22:50:33 +00:00
|
|
|
#endif
|
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
static void progress_callback(int pos, int len) { term_printf("."); }
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2024-09-18 07:24:59 +00:00
|
|
|
static void flash_from_sdcard(const flash_area_t *area, uint32_t source,
|
2019-03-29 15:26:02 +00:00
|
|
|
uint32_t length) {
|
|
|
|
static uint32_t buf[SDCARD_BLOCK_SIZE / sizeof(uint32_t)];
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2023-12-26 13:33:11 +00:00
|
|
|
_Static_assert(SDCARD_BLOCK_SIZE % FLASH_BLOCK_SIZE == 0);
|
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++) {
|
2024-01-17 14:29:49 +00:00
|
|
|
term_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
|
|
|
|
2024-04-15 15:47:35 +00:00
|
|
|
for (uint32_t j = 0; j < SDCARD_BLOCK_SIZE / FLASH_BLOCK_SIZE; j++) {
|
2023-12-26 13:33:11 +00:00
|
|
|
ensure(flash_area_write_block(
|
|
|
|
area, i * SDCARD_BLOCK_SIZE + j * FLASH_BLOCK_SIZE,
|
|
|
|
&buf[j * FLASH_BLOCK_WORDS]),
|
2019-03-29 15:26:02 +00:00
|
|
|
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) {
|
2024-09-18 07:24:59 +00:00
|
|
|
system_init(&rsod_panic_handler);
|
2024-07-12 09:35:49 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
sdcard_init();
|
|
|
|
touch_init();
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
|
|
|
hash_processor_init();
|
|
|
|
#endif
|
|
|
|
|
2024-11-15 09:07:01 +00:00
|
|
|
display_set_orientation(0);
|
2024-11-13 10:04:53 +00:00
|
|
|
gfx_clear();
|
2024-11-15 09:07:01 +00:00
|
|
|
display_set_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
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("updating boardloader + bootloader\n");
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("erasing sectors");
|
2023-06-30 10:12:55 +00:00
|
|
|
ensure(flash_area_erase(&BOARDLOADER_AREA, progress_callback),
|
|
|
|
"flash_erase_sectors");
|
|
|
|
ensure(flash_area_erase(&BOOTLOADER_AREA, progress_callback),
|
2019-03-29 15:26:02 +00:00
|
|
|
"flash_erase_sectors");
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("\n");
|
|
|
|
term_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
|
|
|
|
2023-06-30 10:12:55 +00:00
|
|
|
flash_from_sdcard(&BOARDLOADER_AREA, 0, BOARDLOADER_TOTAL_SIZE);
|
|
|
|
flash_from_sdcard(&BOOTLOADER_AREA, BOARDLOADER_TOTAL_SIZE,
|
2019-03-29 15:26:02 +00:00
|
|
|
BOOTLOADER_TOTAL_SIZE);
|
2017-11-06 14:46:27 +00:00
|
|
|
|
2024-01-17 14:29:49 +00:00
|
|
|
term_printf("done\n");
|
2019-03-29 15:26:02 +00:00
|
|
|
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
|
|
|
}
|