Skip to content

Commit d720d60

Browse files
authored
Merge pull request #2466 from particle-iot/feature/p2-support
P2 support
2 parents 05f5c24 + 2fadda5 commit d720d60

File tree

628 files changed

+58376
-3111
lines changed

Some content is hidden

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

628 files changed

+58376
-3111
lines changed

.buildpackrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ export BUILDPACK_VARIATION=manifest
1515
export RELEASE_PLATFORMS=( )
1616

1717
# Platforms for which this firmware is considered experimental
18-
export PRERELEASE_PLATFORMS=( argon boron bsom b5som tracker esomx )
18+
export PRERELEASE_PLATFORMS=( argon boron bsom b5som tracker esomx p2 )
1919
# Note: a single platform should be only in release or prerelease list. If
2020
# added to both it will be considered a prerelease
2121

2222
# Platforms which require modules to be prebuilt
23-
export MODULAR_PLATFORMS=( argon boron bsom b5som tracker esomx)
23+
export MODULAR_PLATFORMS=( argon boron bsom b5som tracker esomx p2 )
2424

.circleci/config.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ orbs:
1414

1515
aliases:
1616
- &tasks ["compile:all clean:all", "compile:user clean:user", "compile:debug clean:debug"]
17-
- &platforms ["argon", "boron", "esomx", "bsom", "b5som", "tracker"]
17+
- &platforms ["argon", "boron", "esomx", "bsom", "b5som", "tracker", "p2"]
1818

1919
# Define a job to be invoked later in a workflow.
2020
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
@@ -472,7 +472,7 @@ workflows:
472472
- particle-ci-private
473473
matrix:
474474
parameters:
475-
platform: ["argon", "boron", "esomx", "bsom", "b5som", "tracker", "unit-test newhal gcc"]
475+
platform: ["argon", "boron", "esomx", "bsom", "b5som", "tracker", "p2", "unit-test newhal gcc"]
476476
- combine-binaries:
477477
requires:
478478
- build-and-test

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@
4848
[submodule "third_party/fakeit/fakeit"]
4949
path = third_party/fakeit/fakeit
5050
url = https://github.com/particle-iot/Fakeit.git
51+
[submodule "third_party/ambd_sdk/ambd_sdk"]
52+
path = third_party/ambd_sdk/ambd_sdk
53+
url = https://github.com/particle-iot/realtek_ambd_sdk_public.git

.workbench/manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "1.0.0",
33
"toolchains": [
44
{
5-
"platforms": [12, 13, 15, 23, 25, 26],
5+
"platforms": [12, 13, 15, 23, 25, 26, 32],
66
"firmware": "deviceOS@source",
77
"compilers": "gcc-arm@10.2.1",
88
"tools": "buildtools@1.1.1",

CHANGELOG.md

+17
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@
116116
- Makes system_version.h compliant with C; Coalesce FreeRTOS task list internals into single location [#2399](https://github.com/particle-iot/device-os/pull/2399)
117117
- Improves reliability of a few tests [#2406](https://github.com/particle-iot/device-os/pull/2406)
118118

119+
## 3.2.1-p2.4
120+
121+
### BUGFIXES
122+
- Fix overlapping OTA/system-part1/user app sections [#385](https://github.com/particle-iot/firmware-private/pull/385)
123+
- Fix exflash_hal for bootloader usage; remove reentrant libc functions from ROM imports to avoid interference with newlib [#383](https://github.com/particle-iot/firmware-private/pull/383)
124+
- Connecting to unsecured wifi would cause bus fault [#377](https://github.com/particle-iot/firmware-private/pull/377)
125+
- [test] minor fix for P2 [#375](https://github.com/particle-iot/firmware-private/pull/375)
126+
- [rtl872x] hal: i2c thread safe [#361](https://github.com/particle-iot/firmware-private/pull/361)
127+
128+
### ENHANCEMENTS
129+
- [rtl872x] hal: support accessing the OTP built in the external flash [#379](https://github.com/particle-iot/firmware-private/pull/379)
130+
- [rtl872x] hal: add ADC calibration. [#380](https://github.com/particle-iot/firmware-private/pull/380)
131+
- [rtl872x] hal: supports Serial3. [#362](https://github.com/particle-iot/firmware-private/pull/362)
132+
133+
### INTERNAL
134+
- Add new openocd version [#366](https://github.com/particle-iot/firmware-private/pull/366)
135+
119136
## 3.2.0
120137

121138
### BUGFIXES

bootloader/build.mk

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ ASFLAGS += -I$(COMMON_BUILD)/arm/startup
66
# Linker flags
77
LDFLAGS += -Wl,-Map,$(TARGET_BASE).map
88

9-
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/module_start.ld
10-
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/module_end.ld
11-
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/module_info.ld
9+
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/linker_module_start.ld
10+
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/linker_module_end.ld
11+
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/linker_module_info.ld
1212

1313
ifeq (,$(BOOTLOADER_MODULE_DEPENDENCY))
1414
BOOTLOADER_MODULE_DEPENDENCY=0,0,0

bootloader/makefile

+7-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ BOOTLOADER_MODULE_PATH=.
33
PLATFORM_DFU=0x8000000
44
BUILD_PATH_EXT = $(BOOTLOADER_BUILD_PATH_EXT)
55

6+
ifeq ("$(PLATFORM)","p2")
7+
RTL_BINARY_FLASH_START = 0x4000
8+
endif
9+
610
DEPENDENCIES = platform hal services system dynalib
711
MAKE_DEPENDENCIES = platform services
812
# dependent on HAL headers, but not the library
913
LIBS += $(MAKE_DEPENDENCIES)
1014
LIB_DEPS += $(PLATFORM_LIB_DEP) $(SERVICES_LIB_DEP)
1115
LIB_DIRS += $(dir $(LIB_DEPS))
1216

13-
export COMPILE_LTO=y
17+
BOOTLOADER_USE_LTO?=y
18+
19+
export COMPILE_LTO=BOOTLOADER_USE_LTO
1420

1521
export BOOTLOADER_MODULE=1
1622

bootloader/prebootloader/makefile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
PROJECT_ROOT ?= ../..
2+
COMMON_BUILD=$(PROJECT_ROOT)/build
3+
4+
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))
5+
current_dir := $(patsubst %/,%,$(dir $(mkfile_path)))
6+
7+
include $(PROJECT_ROOT)/build/verbose.mk
8+
include $(PROJECT_ROOT)/build/platform-id.mk
9+
include $(PROJECT_ROOT)/build/macros.mk
10+
include $(PROJECT_ROOT)/build/module-defaults.mk
11+
include $(PROJECT_ROOT)/build/arm-tools.mk
12+
13+
projects = $(sort $(wildcard $(current_dir)/src/$(PLATFORM_DYNALIB_MODULES)/*))
14+
makefiles = $(foreach module,$(projects),$(and $(strip $(subst $(MODULE_EXCLUDE),,$(notdir $(module)))),$(wildcard $(module)/makefile)))
15+
16+
SUBDIR_GOALS := $(MAKECMDGOALS)
17+
18+
all: $(makefiles)
19+
20+
$(makefiles):
21+
$(call,echo,'Making module $@')
22+
$(VERBOSE)$(MAKE) -C $(dir $@) $(SUBDIR_GOALS) $(MAKE_ARGS) $(MAKEOVERRIDES)
23+
24+
25+
.PHONY: clean all $(makefiles)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* Copyright (c) 2021 Particle Industries, Inc. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation, either
7+
* version 3 of the License, or (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 GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "stdbool.h"
19+
#include "rtl8721d.h"
20+
#include "check.h"
21+
#include "flash_hal.h"
22+
#include "boot_info.h"
23+
#include "flash_mal.h"
24+
#include "flash_common.h"
25+
26+
extern FLASH_InitTypeDef flash_init_para;
27+
28+
extern uintptr_t link_system_part1_flash_start;
29+
extern uintptr_t link_user_part_flash_end;
30+
extern uintptr_t link_km0_mbr_flash_end;
31+
extern uintptr_t link_platform_flash_end;
32+
extern uintptr_t link_part1_module_info_flash_start;
33+
34+
static void flash_init(void) {
35+
RCC_PeriphClockCmd(APBPeriph_FLASH, APBPeriph_FLASH_CLOCK_XTAL, ENABLE);
36+
37+
uint32_t Temp = HAL_READ32(SYSTEM_CTRL_BASE_LP, REG_LP_CLK_CTRL0);
38+
Temp &= ~(BIT_MASK_FLASH_CLK_SEL << BIT_SHIFT_FLASH_CLK_SEL);
39+
Temp |= BIT_SHIFT_FLASH_CLK_XTAL;
40+
HAL_WRITE32(SYSTEM_CTRL_BASE_LP, REG_LP_CLK_CTRL0, Temp);
41+
42+
FLASH_StructInit(&flash_init_para);
43+
FLASH_Init(SpicOneBitMode);
44+
45+
uint8_t flashId[3];
46+
FLASH_RxCmd(flash_init_para.FLASH_cmd_rd_id, 3, flashId);
47+
if (flashId[0] == 0x20) {
48+
flash_init_para.FLASH_cmd_chip_e = 0xC7;
49+
}
50+
}
51+
52+
static bool flash_copy(uintptr_t src_addr, uintptr_t dest_addr, size_t size) {
53+
const uint32_t COPY_BLOCK_SIZE = 256;
54+
uint8_t buf[COPY_BLOCK_SIZE];
55+
const uintptr_t src_end_addr = src_addr + size;
56+
57+
uint32_t sectorNum = (size / 4096) + (((size % 4096) > 0) ? 1 : 0);
58+
if (hal_flash_erase_sector(dest_addr, sectorNum) != 0) {
59+
return false;
60+
}
61+
62+
while (src_addr < src_end_addr) {
63+
size_t n = src_end_addr - src_addr;
64+
if (n > sizeof(buf)) {
65+
n = sizeof(buf);
66+
}
67+
if (hal_flash_read(src_addr, buf, n) != 0) {
68+
return false;
69+
}
70+
if (hal_flash_write(dest_addr, buf, n) != 0) {
71+
return false;
72+
}
73+
if (memcmp((uint8_t*)src_addr, (uint8_t*)dest_addr, n)) {
74+
return false;
75+
}
76+
src_addr += n;
77+
dest_addr += n;
78+
}
79+
return true;
80+
}
81+
82+
bool bootloaderUpdateIfPending(void) {
83+
flash_init();
84+
85+
flash_update_info_t info = {};
86+
87+
uint32_t infoAddr = BOOT_INFO_FLASH_XIP_START_ADDR + KM0_BOOTLOADER_UPDATE_INFO_OFFSET;
88+
memcpy(&info, (void*)infoAddr, sizeof(info));
89+
90+
// Validate flash_update_info_t integrity
91+
if (info.magic_num != KM0_BOOTLOADER_UPDATE_MAGIC_NUMBER
92+
|| Compute_CRC32((const uint8_t*)&info, sizeof(flash_update_info_t) - sizeof(info.crc32), NULL) != info.crc32) {
93+
goto invalidate;
94+
}
95+
96+
// flash_update_info_t looks valid
97+
// Validate source location, source should be within a potential dynamic OTA location region
98+
const uintptr_t ota_region_start = (uintptr_t)&link_system_part1_flash_start;
99+
const uintptr_t ota_region_end = (uintptr_t)&link_user_part_flash_end;
100+
if (!(info.src_addr >= ota_region_start && info.src_addr < ota_region_end
101+
&& info.size > 0 && info.src_addr + info.size <= ota_region_end)) {
102+
goto invalidate;
103+
}
104+
105+
// Check destination location, it should be within flash excluding MBR
106+
if (!(info.dest_addr >= (uintptr_t)&link_km0_mbr_flash_end && info.dest_addr < (uintptr_t)&link_platform_flash_end
107+
&& info.dest_addr + info.size < (uintptr_t)&link_platform_flash_end)) {
108+
goto invalidate;
109+
}
110+
111+
// Destination-specific checks.
112+
// When updating KM0 part1, if the image is not encrypted and we have encryption enabled, then invalidate the update.
113+
// IE dont allow unencrypted updates once we enable encryption.
114+
uint8_t userEfuse0 = 0xFF;
115+
EFUSE_PMAP_READ8(0, USER_KEY_0_EFUSE_ADDRESS, &userEfuse0, L25EOUTVOLTAGE);
116+
bool part1_encryption_enabled = !(userEfuse0 & PART1_ENCRYPTED_BIT);
117+
bool pending_image_encrypted = (info.flags & MODULE_ENCRYPTED);
118+
if (info.dest_addr == (uintptr_t)&link_part1_module_info_flash_start && (!pending_image_encrypted && part1_encryption_enabled)) {
119+
goto invalidate;
120+
}
121+
122+
bool enableRsip = false;
123+
if ((HAL_READ32(SYSTEM_CTRL_BASE_LP, REG_SYS_EFUSE_SYSCFG3) & BIT_SYS_FLASH_ENCRYPT_EN) != 0) {
124+
// Temporarily disable RSIP for memory copying, should leave the image in the OTA region as-is
125+
uint32_t km0_system_control = HAL_READ32(SYSTEM_CTRL_BASE_LP, REG_LP_KM0_CTRL);
126+
HAL_WRITE32(SYSTEM_CTRL_BASE_LP, REG_LP_KM0_CTRL, (km0_system_control & (~BIT_LSYS_PLFM_FLASH_SCE)));
127+
enableRsip = true;
128+
}
129+
130+
bool ret = flash_copy(info.src_addr, info.dest_addr, info.size);
131+
if (enableRsip) {
132+
uint32_t km0_system_control = HAL_READ32(SYSTEM_CTRL_BASE_LP, REG_LP_KM0_CTRL);
133+
HAL_WRITE32(SYSTEM_CTRL_BASE_LP, REG_LP_KM0_CTRL, (km0_system_control | BIT_LSYS_PLFM_FLASH_SCE));
134+
}
135+
136+
if (!ret) {
137+
// Flash copy failed. Try rebooting
138+
return false;
139+
}
140+
141+
// Flash copy succeeded. If this is an encrypted part1 image, and encryption isnt enabled, enable it in pysical efuse
142+
if (info.dest_addr == (uintptr_t)&link_part1_module_info_flash_start && pending_image_encrypted && !part1_encryption_enabled) {
143+
userEfuse0 &= ~(PART1_ENCRYPTED_BIT);
144+
bool success = (EFUSE_PMAP_WRITE8(0, USER_KEY_0_EFUSE_ADDRESS, userEfuse0, L25EOUTVOLTAGE) != 0);
145+
if (!success) {
146+
return false;
147+
}
148+
}
149+
150+
invalidate:
151+
memset(&info, 0x00, sizeof(info));
152+
if (hal_flash_write(infoAddr, (const uint8_t*)&info, sizeof(info)) != 0) {
153+
hal_flash_erase_sector(BOOT_INFO_FLASH_XIP_START_ADDR, 1);
154+
}
155+
156+
return true;
157+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2021 Particle Industries, Inc. All rights reserved.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation, either
7+
* version 3 of the License, or (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 GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
#ifndef BOOTLOADER_UPDATE_H
19+
#define BOOTLOADER_UPDATE_H
20+
21+
#include <stdint.h>
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
bool bootloaderUpdateIfPending(void);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif // BOOTLOADER_UPDATE_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
ifeq ($(MODULE),prebootloader-mbr)
2+
3+
ASRC += $(COMMON_BUILD)/arm/startup/startup_$(MCU_DEVICE_LC).S
4+
ASFLAGS += -I$(COMMON_BUILD)/arm/startup
5+
6+
# Linker flags
7+
LDFLAGS += -Wl,-Map,$(TARGET_BASE).map
8+
9+
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/linker_module_start.ld
10+
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/linker_module_end.ld
11+
LINKER_DEPS += $(COMMON_BUILD)/arm/linker/linker_module_info.ld
12+
13+
ifeq (,$(PREBOOTLOADER_MBR_MODULE_DEPENDENCY))
14+
PREBOOTLOADER_MBR_MODULE_DEPENDENCY=0,0,0
15+
endif
16+
17+
ifeq (,$(PREBOOTLOADER_MBR_MODULE_DEPENDENCY2))
18+
PREBOOTLOADER_MBR_MODULE_DEPENDENCY2=0,0,0
19+
endif
20+
21+
ifeq (,$(PREBOOTLOADER_MBR_MODULE_INDEX))
22+
PREBOOTLOADER_MBR_MODULE_INDEX=1
23+
endif
24+
25+
GLOBAL_DEFINES += PREBOOTLOADER_MBR_VERSION=$(PREBOOTLOADER_MBR_VERSION)
26+
GLOBAL_DEFINES += MODULE_VERSION=$(PREBOOTLOADER_MBR_VERSION)
27+
GLOBAL_DEFINES += MODULE_FUNCTION=$(MODULE_FUNCTION_BOOTLOADER)
28+
GLOBAL_DEFINES += MODULE_DEPENDENCY=$(PREBOOTLOADER_MBR_MODULE_DEPENDENCY)
29+
GLOBAL_DEFINES += MODULE_DEPENDENCY2=$(PREBOOTLOADER_MBR_MODULE_DEPENDENCY2)
30+
GLOBAL_DEFINES += MODULE_INDEX=$(PREBOOTLOADER_MBR_MODULE_INDEX)
31+
32+
# select sources from platform
33+
34+
# import the sources from the platform
35+
include $(call rwildcard,$(PREBOOTLOADER_MBR_MODULE_PATH)/,sources.mk)
36+
37+
endif

0 commit comments

Comments
 (0)