Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit f5bdddd

Browse files
author
Daniel Campora
committed
esp32: Merge with master up to version 1.17.2.b1.
1 parent d0dc708 commit f5bdddd

Some content is hidden

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

94 files changed

+1754
-1196
lines changed

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ user.props
4242
.project
4343
.pydevproject
4444
.settings
45+
46+
# Key files (for Flash Encryption and Secure Boot)
47+
secure_boot_signing_key.pem
48+
signature_verification_key.bin
49+
secure-bootloader-key.bin
50+
flash_encryption_key.bin
51+
52+
.DS_Store

Jenkinsfile

+6-14
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,13 @@ node {
2121
make all'''
2222
}
2323

24-
stage('IDF-LIBS') {
25-
// build the libs from esp-idf
26-
sh '''export PATH=$PATH:/opt/xtensa-esp32-elf/bin;
27-
export IDF_PATH=${WORKSPACE}/esp-idf;
28-
cd $IDF_PATH/examples/wifi/scan;
29-
make clean && make all'''
30-
}
31-
32-
for (board in boards_to_build) {
33-
stage(board) {
34-
def parallelSteps = [:]
24+
stage('firmware-build') {
25+
def parallelSteps = [:]
26+
for (board in boards_to_build) {
3527
def board_u = board.toUpperCase()
3628
parallelSteps[board] = boardBuild(board)
37-
parallel parallelSteps
3829
}
30+
parallel parallelSteps
3931
}
4032

4133
stash includes: '**/*.bin', name: 'binary'
@@ -89,7 +81,7 @@ def boardBuild(name) {
8981
cd firmware_package;
9082
cp ../bootloader/bootloader.bin .;
9183
mv ../application.elf ''' + release_dir + name + "-" + PYCOM_VERSION + '''-application.elf;
92-
cp ../appimg.bin .;
84+
cp ../''' + app_bin + ''' appimg.bin;
9385
cp ../lib/partitions.bin .;
9486
cp ../../../../boards/''' + name_short + '''/''' + name_u + '''/script .;
9587
cp ../''' + app_bin + ''' .;
@@ -111,7 +103,7 @@ def flashBuild(short_name) {
111103
sh 'python esp32/tools/pypic.py --port ' + device_name +' --enter'
112104
sh 'esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port ' + device_name +' --baud 921600 erase_flash'
113105
sh 'python esp32/tools/pypic.py --port ' + device_name +' --enter'
114-
sh 'esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port ' + device_name +' --baud 921600 --before no_reset --after no_reset write_flash -pz --flash_mode dio --flash_freq 80m --flash_size detect 0x1000 esp32/build/'+ board_name_u +'/release/bootloader/bootloader.bin 0x8000 esp32/build/'+ board_name_u +'/release/lib/partitions.bin 0x10000 esp32/build/'+ board_name_u +'/release/appimg.bin'
106+
sh 'esp-idf/components/esptool_py/esptool/esptool.py --chip esp32 --port ' + device_name +' --baud 921600 --before no_reset --after no_reset write_flash -pz --flash_mode dio --flash_freq 80m --flash_size detect 0x1000 esp32/build/'+ board_name_u +'/release/bootloader/bootloader.bin 0x8000 esp32/build/'+ board_name_u +'/release/lib/partitions.bin 0x10000 esp32/build/'+ board_name_u +'/release/' + board_name_u.toLowerCase() + '.bin'
115107
sh 'python esp32/tools/pypic.py --port ' + device_name +' --exit'
116108
}
117109
}

README.md

+85-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ MicroPython implements the entire Python 3.4 syntax (including exceptions,
1616
The following core datatypes are provided: str (including basic Unicode
1717
support), bytes, bytearray, tuple, list, dict, set, frozenset, array.array,
1818
collections.namedtuple, classes and instances. Builtin modules include sys,
19-
time, and struct, etc. Select ports have support for _thread module
19+
time, and struct, etc. Select ports have support for `_thread` module
2020
(multithreading). Note that only subset of Python 3.4 functionality
2121
implemented for the data types and modules.
2222

@@ -142,5 +142,88 @@ To build and flash a LoPy:
142142
The above also applies to the FiPy and LoPy4
143143

144144
Make sure that your board is placed into programming mode, otherwise flashing will fail.<br>
145-
PyTrack and PySense boards will automatically switch into programming mode (currently supported on MacOS and Linux only!)<br>
145+
PyTrack and PySense boards will automatically switch into programming mode<br>
146+
(currently supported on MacOS and Linux only!)<br><br>
146147
Expansion Board 2.0 users, please connect ``P2`` to ``GND`` and then reset the board.
148+
149+
## Steps for using Secure Boot and Flash Encryption
150+
151+
### Summary
152+
153+
1. Obtain keys (for Secure Boot and Flash Encryption)
154+
2. Flash keys and parameters in efuses
155+
3. Compile bootloader and application with `make SECURE=on`
156+
4. Flash: bootloader-digest at address 0x0 and encrypted; all the others (partitions and application) encrypted, too.
157+
158+
### Prerequisites
159+
160+
$ export $IDF_PATH=<pycom-esp-idf_PATH>
161+
$ cd esp32
162+
163+
Hold valid keys for Flash Encryption and Secure Boot; they can be generated randomly with the following commands:
164+
165+
python $IDF_PATH/components/esptool_py/esptool/espsecure.py generate_flash_encryption_key flash_encryption_key.bin
166+
python $IDF_PATH/components/esptool_py/esptool/espsecure.py generate_signing_key secure_boot_signing_key.pem
167+
168+
The Secure Boot key `secure_boot_signing_key.pem` has to be transformed into `secure-bootloader-key.bin`, to be burnt into efuses. This can be done in 2 ways:
169+
170+
python $IDF_PATH/components/esptool_py/esptool/espefuse.py extract_public_key --keyfile secure_boot_signing_key.pem signature_verification_key.bin
171+
172+
# or, as an artifact of the make build process, on the same directory level as Makefile
173+
make BOARD=GPY SECURE=on TARGET=boot
174+
175+
Flash keys (`flash_encryption_key.bin` and `secure-bootloader-key.bin`) into the efuses (write and read protected):
176+
177+
**_Note: Irreversible operations_**
178+
179+
# Burning Encryption Key
180+
python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port /dev/ttyUSB0 burn_key flash_encryption flash_encryption_key.bin
181+
# Burning Secure Boot Key
182+
python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port /dev/ttyUSB0 burn_key secure_boot secure-bootloader-key.bin
183+
# Enabling Flash Encryption mechanism
184+
python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port /dev/ttyUSB0 burn_efuse FLASH_CRYPT_CNT
185+
# Configuring Flash Encryption to use all address bits togheter with Encryption key (max value 0x0F)
186+
python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port /dev/ttyUSB0 burn_efuse FLASH_CRYPT_CONFIG 0x0F
187+
# Enabling Secure Boot mechanism
188+
python $IDF_PATH/components/esptool_py/esptool/espefuse.py --port /dev/ttyUSB0 burn_efuse ABS_DONE_0
189+
190+
**_If the keys are not written in efuse, before flashing the bootloader, then random keys will be generated by the ESP32, they can never be read nor re-written, so bootloader can never be updated. Even more, the application can be re-flashed (by USB) just 3 more times._**
191+
192+
### Makefile options:
193+
194+
make BOARD=GPY SECURE=on SECURE_KEY=secure_boot_signing_key.pem ENCRYPT_KEY=flash_encryption_key.bin TARGET=[boot|app]
195+
196+
- `SECURE=on` is the main flag; it's not optional
197+
- if `SECURE=on` by default:
198+
- encryption is enabled
199+
- secure_boot_signing_key.pem is the secure boot key, located relatively to Makefile
200+
- flash_encryption_key.bin is the flash encryption key, located relatively to Makefile
201+
202+
For flashing the bootloader digest and the encrypted versions of all binaries:
203+
204+
make BOARD=GPY SECURE=on flash
205+
206+
### Flashing
207+
208+
For flashing the bootloader-reflash-digest.bin has to be written at address 0x0, instead of the bootloader.bin (at address 0x1000).
209+
210+
Build is done using `SECURE=on` option; additionally, all the binaries are pre-encrypted.
211+
212+
make BOARD=GPY clean
213+
make BOARD=GPY SECURE=on TARGET=boot
214+
make BOARD=GPY SECURE=on TARGET=app
215+
make BOARD=GPY SECURE=on flash
216+
217+
Manual flash command:
218+
219+
python $IDF_PATH/components/esptool_py/esptool/esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 921600 --before no_reset --after no_reset write_flash -z --flash_mode dio --flash_freq 80m --flash_size detect 0x0 build/GPY/release/bootloader/bootloader-reflash-digest.bin_enc 0x8000 build/GPY/release/lib/partitions.bin_enc 0x10000 build/GPY/release/gpy.bin_enc_0x10000
220+
221+
### OTA update
222+
223+
The OTA should be done using the pre-encrypted application image.
224+
225+
Because the encryption is done based on the physical flash address, there are 2 application binaries generated:
226+
- gpy.bin_enc_0x10000 which has to be written at default factory address: 0x10000
227+
- gpy.bin_enc_0x1A0000 which has to be written at the ota_0 partition address (0x1A0000)
228+
229+
*__Hint:__ on micropython interface, the method `pycom.ota_slot()` responds with the address of the next OTA partition available (either 0x10000 or 0x1A0000).*

drivers/sx127x/sx1272/sx1272.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Maintainer: Miguel Luis and Gregory Cristian
3333
#include "sx1272.h"
3434
#include "sx1272-board.h"
3535
#include "esp_attr.h"
36+
#include "esp32_mphal.h"
3637

3738
/*
3839
* Local types definition
@@ -1054,8 +1055,6 @@ static IRAM_ATTR void SX1272OnDioIrq (void) {
10541055
}
10551056
}
10561057

1057-
extern uint64_t system_get_rtc_time(void);
1058-
10591058
IRAM_ATTR void SX1272OnDio0Irq( void )
10601059
{
10611060
volatile uint8_t irqFlags = 0;
@@ -1073,7 +1072,7 @@ IRAM_ATTR void SX1272OnDio0Irq( void )
10731072
int8_t snr = 0;
10741073

10751074
// Store the packet timestamp
1076-
SX1272.Settings.LoRaPacketHandler.TimeStamp = system_get_rtc_time();
1075+
SX1272.Settings.LoRaPacketHandler.TimeStamp = mp_hal_ticks_us();
10771076

10781077
// Clear Irq
10791078
SX1272Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_RXDONE );

drivers/sx127x/sx1276/sx1276.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Maintainer: Miguel Luis, Gregory Cristian and Wael Guibene
3333
#include "sx1276.h"
3434
#include "sx1276-board.h"
3535
#include "esp_attr.h"
36+
#include "esp32_mphal.h"
3637

3738
/*
3839
* Local types definition
@@ -1219,8 +1220,6 @@ static IRAM_ATTR void SX1276OnDioIrq (void) {
12191220
}
12201221
}
12211222

1222-
extern uint64_t system_get_rtc_time(void);
1223-
12241223
IRAM_ATTR void SX1276OnDio0Irq( void )
12251224
{
12261225
volatile uint8_t irqFlags = 0;
@@ -1238,7 +1237,7 @@ IRAM_ATTR void SX1276OnDio0Irq( void )
12381237
int8_t snr = 0;
12391238

12401239
// Store the packet timestamp
1241-
SX1276.Settings.LoRaPacketHandler.TimeStamp = system_get_rtc_time();
1240+
SX1276.Settings.LoRaPacketHandler.TimeStamp = mp_hal_ticks_us();
12421241

12431242
// Clear Irq
12441243
SX1276Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_RXDONE );

esp32/Makefile

+30-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,32 @@ BTYPE ?= release
2323

2424
BUILD = build/$(BOARD)/$(BTYPE)
2525

26+
# by default Secure Boot and Flash Encryption are disabled
27+
SECURE ?= off
28+
29+
ifeq ($(SECURE), on)
30+
# filename of the private key, used for 2 things:
31+
# 1. signing partitions.bin and board.bin
32+
# 2. extracting public key, which is embedded into bootloader.bin
33+
# Generation is done using command:
34+
# espsecure.py generate_signing_key secure_boot_signing_key.pem
35+
SECURE_KEY ?= secure_boot_signing_key.pem
36+
37+
# Flash Encryption key is generated using command:
38+
# espsecure.py generate_flash_encryption_key flash_encryption_key.bin
39+
# Writing it is done using:
40+
# espefuse.py --port PORT burn_key flash_encryption flash_encryption_key.bin
41+
ENCRYPT_KEY ?= flash_encryption_key.bin
42+
else
43+
$(info Use make SECURE=on [optionally SECURE_KEY ?= secure_boot_signing_key.pem] to enable Secure Boot and Flash Encryption mechanisms.)
44+
endif # ifeq ($(SECURE), on)
45+
46+
# Default path to the project: we assume the Makefile including this file
47+
# is in the project directory
48+
ifndef PROJECT_PATH
49+
PROJECT_PATH := $(abspath $(dir $(firstword $(MAKEFILE_LIST))))
50+
endif
51+
2652
FROZEN_MPY_DIR = frozen
2753

2854
include ../py/mkenv.mk
@@ -55,7 +81,7 @@ LIBS = -L$(ESP_IDF_COMP_PATH)/esp32/lib -L$(ESP_IDF_COMP_PATH)/esp32/ld -L$(ESP_
5581
$(ESP_IDF_COMP_PATH)/newlib/lib/libm-psram-workaround.a \
5682
$(ESP_IDF_COMP_PATH)/newlib/lib/libc-psram-workaround.a \
5783
-lfreertos -ljson -ljsmn -llwip -lnewlib -lvfs -lopenssl -lmbedtls -lwpa_supplicant \
58-
-lxtensa-debug-module -lbt -lsdmmc -lsoc -lheap -u ld_include_panic_highint_hdl \
84+
-lxtensa-debug-module -lbt -lsdmmc -lsoc -lheap -lbootloader_support -lmicro-ecc -u ld_include_panic_highint_hdl \
5985

6086
ifeq ($(BOARD), $(filter $(BOARD), FIPY))
6187
LIBS += sigfox/modsigfox_fipy.a
@@ -73,6 +99,9 @@ B_LIBS = -Lbootloader/lib -Lbootloader -L$(BUILD)/bootloader -L$(ESP_IDF_COMP_PA
7399
-L$(ESP_IDF_COMP_PATH)/esp32/lib -llog -lcore -lbootloader_support \
74100
-lspi_flash -lsoc -lmicro-ecc -lgcc -lstdc++ -lgcov
75101

102+
# objcopy paramters, to transform a binary file into an object file
103+
OBJCOPY_EMBED_ARGS = --input-target binary --output-target elf32-xtensa-le --binary-architecture xtensa --rename-section .data=.rodata.embedded
104+
76105
# qstr definitions (must come before including py.mk)
77106
QSTR_DEFS = qstrdefsport.h $(BUILD)/pins_qstr.h
78107
# include py core make definitions

0 commit comments

Comments
 (0)