Skip to content

Commit 9b063d5

Browse files
mpu branch megamerge
1 parent ea397f8 commit 9b063d5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+18795
-18000
lines changed

.gitignore

+1-53
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,2 @@
1-
# .gitignore
2-
3-
# Files generated by the build
4-
interface/**/*.pb.h
5-
interface/**/*.pb.c
6-
7-
# Selected build objects
8-
*.o
9-
.sconsign*
10-
.gradle
11-
12-
13-
# IDE files
14-
.cproject
15-
.project
16-
.metadata/
17-
.idea
18-
19-
# component libraries
20-
/build
21-
/bin
22-
23-
# Misc
24-
tags
25-
26-
# Compiled python scripts
27-
*.pyc
28-
29-
# Emacs temporary files
30-
*~
31-
*\#
32-
33-
# VIM temporary files
34-
*.swo
35-
*.swp
36-
37-
# IAR generated files
38-
*.browse
39-
*.cout
40-
*.cspy.bat
41-
*.dbgdt
42-
*.dep
43-
*.dni
44-
*.jlink
45-
*.lst
46-
*.pbi
47-
*.pbd
48-
*.sfr
49-
*.sim
50-
*.tmp
51-
*.wsdt
52-
*.pbi
53-
*.cout
1+
.DS_Store
542

CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ project(KeepKeyFirmware
66

77
LANGUAGES C CXX ASM)
88

9-
set(BOOTLOADER_MAJOR_VERSION 1)
10-
set(BOOTLOADER_MINOR_VERSION 1)
9+
set(BOOTLOADER_MAJOR_VERSION 2)
10+
set(BOOTLOADER_MINOR_VERSION 0)
1111
set(BOOTLOADER_PATCH_VERSION 0)
1212

1313
option(KK_EMULATOR "Build the emulator" OFF)

cmake/caches/device.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ set(CMAKE_CXX_FLAGS "${ARCH_FLAGS} -std=gnu++11 ${OPT_FLAGS} ${WARN_FLAGS} \
6262

6363
set(CMAKE_ASM_FLAGS "-mcpu=cortex-m3 \
6464
-mthumb \
65+
-x assembler-with-cpp \
6566
-gdwarf-2" CACHE STRING "")
6667

6768
set(CMAKE_EXE_LINKER_FLAGS

include/keepkey/board/bl_mpu.h

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
*
3+
* * Copyright (C) 2018 KeepKey LLC
4+
*
5+
* This library is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public License
16+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef __BL_MPU_H__
20+
#define __BL_MPU_H__
21+
22+
void bl_flash_erase_word(Allocation group);
23+
24+
#endif

include/keepkey/board/check_bootloader.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ extern char bl_hash_v1_0_3_elf_unpatched[32];
3737
extern char bl_hash_v1_0_4_unpatched[32];
3838

3939
extern char bl_hash_v1_1_0[32];
40+
extern char bl_hash_v2_0_0[32];
41+
4042

4143
typedef enum _BootloaderKind {
4244
BLK_UNKNOWN,
@@ -48,6 +50,7 @@ typedef enum _BootloaderKind {
4850
BLK_v1_0_3_elf,
4951
BLK_v1_0_4,
5052
BLK_v1_1_0,
53+
BLK_v2_0_0
5154
} BootloaderKind;
5255

5356
BootloaderKind get_bootloaderKind(void);

include/keepkey/board/keepkey_board.h

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extern uintptr_t __stack_chk_guard;
8585

8686
void board_reset(void);
8787
void board_init(void);
88+
void kk_board_init(void);
8889

8990
void __stack_chk_fail(void) __attribute__((noreturn));
9091
uint32_t calc_crc32(const void *data, int word_len);

include/keepkey/board/keepkey_button.h

+21
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,31 @@ typedef void (*Handler)(void* context);
3232

3333
/* === Functions =========================================================== */
3434

35+
36+
/** kk_keepkey_button_init() - Initialize push botton interrupt registers
37+
* and variables
38+
*
39+
* INPUT
40+
* none
41+
* OUTPUT
42+
* none
43+
**/
44+
void kk_keepkey_button_init(void);
45+
3546
void keepkey_button_init(void);
3647
void keepkey_button_set_on_press_handler( Handler handler, void* context);
3748
void keepkey_button_set_on_release_handler( Handler handler, void* context);
3849
bool keepkey_button_down(void);
3950
bool keepkey_button_up(void);
4051

52+
/**
53+
* buttonisr_usr() - user interrupt service routine for push button external interrupt
54+
*
55+
* INPUT
56+
* none
57+
* OUTPUT
58+
* none
59+
**/
60+
void buttonisr_usr(void);
61+
4162
#endif

include/keepkey/board/keepkey_flash.h

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#ifndef KEEPKEY_FLASH_H
2121
#define KEEPKEY_FLASH_H
2222

23+
24+
#define MODEL_STR_SIZE 32
25+
2326
/* === Includes ============================================================ */
2427

2528
#include <stddef.h>

include/keepkey/board/memory.h

+42-20
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,35 @@
2020
#ifndef MEMORY_H
2121
#define MEMORY_H
2222

23+
//#include <libopencm3/cm3/mpu.h>
2324
#include "trezor/crypto/sha2.h"
2425

2526
#include <stddef.h>
2627
#include <stdbool.h>
2728
#include <inttypes.h>
2829

30+
2931
/*
3032
3133
flash memory layout:
3234
--------------------
33-
name | range | size | function | permissions
34-
-----------+-------------------------+---------+------------------+-------------
35-
Sector 0 | 0x08000000 - 0x08003FFF | 16 KiB | bootstrap code | Read
36-
Sector 1 | 0x08004000 - 0x08007FFF | 16 KiB | storage/config | Read Write
37-
-----------+-------------------------+---------+------------------+-------------
38-
Sector 2 | 0x08008000 - 0x0800BFFF | 16 KiB | storage/config | Read Write
39-
Sector 3 | 0x0800C000 - 0x0800FFFF | 16 KiB | storage/config | Read Write
40-
-----------+-------------------------+---------+------------------+-------------
41-
Sector 4 | 0x08010000 - 0x0801FFFF | 64 KiB | empty | Read Write
42-
Sector 5 | 0x08020000 - 0x0803FFFF | 128 KiB | bootloader code | Read
43-
Sector 6 | 0x08040000 - 0x0805FFFF | 128 KiB | bootloader code | Read
44-
Sector 7 | 0x08060000 - 0x0807FFFF | 128 KiB | application code | Read Write
45-
===========+=========================+============================+=============
46-
Sector 8 | 0x08080000 - 0x0809FFFF | 128 KiB | application code | Read Write
47-
Sector 9 | 0x080A0000 - 0x080BFFFF | 128 KiB | application code | Read Write
48-
Sector 10 | 0x080C0000 - 0x080DFFFF | 128 KiB | application code | Read Write
49-
Sector 11 | 0x080E0000 - 0x080FFFFF | 128 KiB | application code | Read Write
35+
name | range | size | function | MPU Protection
36+
-----------+-------------------------+---------+------------------+----------------------
37+
Sector 0 | 0x08000000 - 0x08003FFF | 16 KiB | bootstrap code | signature dependent
38+
Sector 1 | 0x08004000 - 0x08007FFF | 16 KiB | storage/config | full access
39+
-----------+-------------------------+---------+------------------+----------------------
40+
Sector 2 | 0x08008000 - 0x0800BFFF | 16 KiB | storage/config | full access
41+
Sector 3 | 0x0800C000 - 0x0800FFFF | 16 KiB | storage/config | full access
42+
-----------+-------------------------+---------+------------------+----------------------
43+
Sector 4 | 0x08010000 - 0x0801FFFF | 64 KiB | empty | full access
44+
Sector 5 | 0x08020000 - 0x0803FFFF | 128 KiB | bootloader code | signature dependent
45+
Sector 6 | 0x08040000 - 0x0805FFFF | 128 KiB | bootloader code | signature dependent
46+
Sector 7 | 0x08060000 - 0x0807FFFF | 128 KiB | application code | full access
47+
===========+=========================+============================+======================
48+
Sector 8 | 0x08080000 - 0x0809FFFF | 128 KiB | application code | full access
49+
Sector 9 | 0x080A0000 - 0x080BFFFF | 128 KiB | application code | full access
50+
Sector 10 | 0x080C0000 - 0x080DFFFF | 128 KiB | application code | full access
51+
Sector 11 | 0x080E0000 - 0x080FFFFF | 128 KiB | application code | full access
5052
5153
Application metadata area:
5254
-------------------------
@@ -68,6 +70,10 @@
6870
6971
*/
7072

73+
/* === Defines ============================================================= */
74+
75+
76+
7177
#ifdef EMULATOR
7278
extern uint8_t *emulator_flash_base;
7379
#define FLASH_PTR(x) (emulator_flash_base + (x - FLASH_ORIGIN))
@@ -92,6 +98,10 @@ extern uint8_t *emulator_flash_base;
9298
#define BLDR_FLASH_SECT_LEN 0x20000
9399
#define APP_FLASH_SECT_LEN 0x20000
94100

101+
#define BSTRP_FLASH_SECT_START 0x08000000
102+
#define BLDR_FLASH_SECT_START 0x08020000
103+
104+
95105
/* meta info */
96106
#define META_MAGIC_STR "KPKY"
97107

@@ -130,16 +140,19 @@ extern uint8_t *emulator_flash_base;
130140
#define FLASH_META_SIG2 (FLASH_META_SIG1 + sizeof(((app_meta_td *)NULL)->sig1))
131141
#define FLASH_META_SIG3 (FLASH_META_SIG2 + sizeof(((app_meta_td *)NULL)->sig2))
132142

143+
133144
#define META_MAGIC_SIZE (sizeof(((app_meta_td *)NULL)->magic))
134145

135-
#define FLASH_APP_START (FLASH_META_START + FLASH_META_DESC_LEN) //0x0806_0100 - 0x080F_FFFF
146+
#define FLASH_APP_START (FLASH_META_START + FLASH_META_DESC_LEN) //0x0806_0200 - 0x080F_FFFF
136147
#define FLASH_APP_LEN (FLASH_END - FLASH_APP_START)
137148

138149
#define SIG_FLAG (*( uint8_t const *)FLASH_META_FLAGS)
139150

140151
/* Misc Info. */
141-
#define FLASH_BOOTSTRAP_SECTOR_FIRST 0
142-
#define FLASH_BOOTSTRAP_SECTOR_LAST 0
152+
#define FLASH_BOOTSTRAP_SECTOR 0
153+
154+
#define FLASH_BOOTSTRAP_SECTOR_FIRST 0
155+
#define FLASH_BOOTSTRAP_SECTOR_LAST 0
143156

144157
#define FLASH_STORAGE_SECTOR_FIRST 1
145158
#define FLASH_STORAGE_SECTOR_LAST 3
@@ -209,6 +222,8 @@ static const FlashSector flash_sector_map[] =
209222
{ -1, 0, 0, FLASH_INVALID}
210223
};
211224

225+
void mpu_config(int);
226+
212227
void memory_protect(void);
213228

214229
/// Enable writing. This exercises a bug in the STM32F2 that allows writing to
@@ -228,4 +243,11 @@ const char *memory_firmware_hash_str(char digest[SHA256_DIGEST_STRING_LENGTH]);
228243
int memory_storage_hash(uint8_t *hash, Allocation storage_location);
229244
bool find_active_storage(Allocation *storage_location);
230245

246+
247+
extern void * _timerusr_isr;
248+
extern void * _buttonusr_isr;
249+
extern void * _mmhusr_isr;
250+
251+
252+
231253
#endif

include/keepkey/board/mpudefs.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2018 KeepKey
3+
*
4+
* This library is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with this library. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef MPUDEFS_H
19+
#define MPUDEFS_H
20+
21+
22+
// MPU
23+
#define MPU_RASR_SIZE_512B (0x08UL << MPU_RASR_SIZE_LSB)
24+
#define MPU_RASR_SIZE_1KB (0x09UL << MPU_RASR_SIZE_LSB)
25+
#define MPU_RASR_SIZE_2KB (0x0AUL << MPU_RASR_SIZE_LSB)
26+
#define MPU_RASR_SIZE_4KB (0x0BUL << MPU_RASR_SIZE_LSB)
27+
#define MPU_RASR_SIZE_8KB (0x0CUL << MPU_RASR_SIZE_LSB)
28+
#define MPU_RASR_SIZE_16KB (0x0DUL << MPU_RASR_SIZE_LSB)
29+
#define MPU_RASR_SIZE_32KB (0x0EUL << MPU_RASR_SIZE_LSB)
30+
#define MPU_RASR_SIZE_64KB (0x0FUL << MPU_RASR_SIZE_LSB)
31+
#define MPU_RASR_SIZE_128KB (0x10UL << MPU_RASR_SIZE_LSB)
32+
#define MPU_RASR_SIZE_256KB (0x11UL << MPU_RASR_SIZE_LSB)
33+
#define MPU_RASR_SIZE_512KB (0x12UL << MPU_RASR_SIZE_LSB)
34+
#define MPU_RASR_SIZE_1MB (0x13UL << MPU_RASR_SIZE_LSB)
35+
#define MPU_RASR_SIZE_512MB (0x1CUL << MPU_RASR_SIZE_LSB)
36+
37+
// http://infocenter.arm.com/help/topic/com.arm.doc.dui0552a/BABDJJGF.html
38+
#define MPU_RASR_ATTR_FLASH (MPU_RASR_ATTR_C)
39+
#define MPU_RASR_ATTR_SRAM (MPU_RASR_ATTR_C | MPU_RASR_ATTR_S)
40+
#define MPU_RASR_ATTR_PERIPH (MPU_RASR_ATTR_B | MPU_RASR_ATTR_S)
41+
42+
// subregion disable bits
43+
#define MPU_RASR_DIS_SUB_8 (0b10000000UL << 8)
44+
45+
#define FLASH_BASE (0x08000000U)
46+
#define SRAM_BASE (0x20000000U)
47+
#define BLPROTECT_BASE (0x2001F800U)
48+
49+
50+
#endif

include/keepkey/board/pubkeys.h

+4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
#include <inttypes.h>
2424

2525
#define PUBKEYS 5
26+
#define EXP_PUBKEYS 5
2627
#define PUBKEY_LENGTH 65
2728
#define SIGNATURES 3
2829

2930
#define SIG_OK 0x5A3CA5C3
31+
#define KEY_EXPIRED 0x00000001
3032
#define SIG_FAIL 0x00000000
3133

3234
static const uint8_t pubkey[PUBKEYS][PUBKEY_LENGTH] =
@@ -78,4 +80,6 @@ static const uint8_t pubkey[PUBKEYS][PUBKEY_LENGTH] =
7880
}
7981
};
8082

83+
extern volatile const uint8_t valid_pubkey[PUBKEYS];
84+
8185
#endif

include/keepkey/bootloader/signatures.h include/keepkey/board/signatures.h

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
#ifndef SIGNATURES_H
2121
#define SIGNATURES_H
2222

23+
/// Checks firmware signatures
24+
///
25+
/// \returns SIG_OK if signatures are correct
26+
/// \returns KEY_EXPIRED if an expired signature was detected
27+
/// \returns SIG_FAIL for unrecognized signature
2328
int signatures_ok(void);
2429

2530
#endif

0 commit comments

Comments
 (0)