2014-04-29 12:26:51 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the TREZOR project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Pavol Rusnak <stick@satoshilabs.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/>.
|
|
|
|
*/
|
|
|
|
|
2014-06-18 22:12:31 +00:00
|
|
|
#include <libopencm3/stm32/flash.h>
|
2014-04-29 12:26:51 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "memory.h"
|
|
|
|
#include "sha2.h"
|
|
|
|
|
|
|
|
#define OPTION_BYTES_1 ((uint64_t *)0x1FFFC000)
|
|
|
|
#define OPTION_BYTES_2 ((uint64_t *)0x1FFFC008)
|
|
|
|
|
|
|
|
void memory_protect(void)
|
|
|
|
{
|
2017-01-26 15:07:45 +00:00
|
|
|
// Reference STM32F205 Flash programming manual revision 5 http://www.st.com/resource/en/programming_manual/cd00233952.pdf
|
|
|
|
// Section 2.6 Option bytes
|
2014-04-29 12:26:51 +00:00
|
|
|
// set RDP level 2 WRP for sectors 0 and 1
|
2017-01-26 15:07:45 +00:00
|
|
|
if ((((*OPTION_BYTES_1) & 0xFFEC) == 0xCCEC) && (((*OPTION_BYTES_2) & 0xFFF) == 0xFFC)) {
|
2014-04-29 12:26:51 +00:00
|
|
|
return; // already set up correctly - bail out
|
|
|
|
}
|
|
|
|
flash_unlock_option_bytes();
|
2017-01-26 15:07:45 +00:00
|
|
|
// Section 2.8.6 Flash option control register (FLASH_OPTCR)
|
|
|
|
// Bits 31:28 Reserved, must be kept cleared.
|
|
|
|
// Bits 27:16 nWRP: Not write protect: write protect bootloader code in flash main memory sectors 0 and 1 (Section 2.3; table 2)
|
|
|
|
// Bits 15:8 RDP: Read protect: level 2 chip read protection active
|
|
|
|
// Bits 7:5 USER: User option bytes: no reset on standby, no reset on stop, software watchdog
|
|
|
|
// Bit 4 Reserved, must be kept cleared.
|
|
|
|
// Bits 3:2 BOR_LEV: BOR reset Level: BOR off
|
|
|
|
// Bit 1 OPTSTRT: Option start: ignored by flash_program_option_bytes
|
|
|
|
// Bit 0 OPTLOCK: Option lock: ignored by flash_program_option_bytes
|
|
|
|
flash_program_option_bytes(0x0FFCCCEC);
|
2014-04-29 12:26:51 +00:00
|
|
|
flash_lock_option_bytes();
|
|
|
|
}
|
|
|
|
|
|
|
|
int memory_bootloader_hash(uint8_t *hash)
|
|
|
|
{
|
|
|
|
sha256_Raw((const uint8_t *)FLASH_BOOT_START, FLASH_BOOT_LEN, hash);
|
|
|
|
sha256_Raw(hash, 32, hash);
|
|
|
|
return 32;
|
|
|
|
}
|