Skip to content

Commit 3bc0570

Browse files
committed
Micro Python core updated to the latest release 1.12
Added initial support for OTA update Updated 'uhashlib' module with better HW SHA256 support Improved 'machine.flash_read' function Fixed bugs in 'camera' module Improved 'tft' module Added initial support for OTA updates in 'requests' module Improved SPI Flash driver Disabled 'FIX_CACHE' option in FreeRTOS Improved FreeRTOS SPI driver
1 parent 9d99740 commit 3bc0570

File tree

222 files changed

+22193
-2826
lines changed

Some content is hidden

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

222 files changed

+22193
-2826
lines changed

.cproject

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,14 @@
233233

234234
<buildTargets>
235235

236-
<target name="BUILD" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
236+
<target name="BUILD_Slow" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
237237

238238
<buildCommand>${PWD}/k210-freertos/BUILD.sh</buildCommand>
239239

240+
<buildArguments/>
241+
242+
<buildTarget/>
243+
240244
<stopOnError>true</stopOnError>
241245

242246
<useDefaultCommand>false</useDefaultCommand>
@@ -249,8 +253,6 @@
249253

250254
<buildCommand>${PWD}/k210-freertos/CLEAN.sh</buildCommand>
251255

252-
<buildArguments/>
253-
254256
<stopOnError>true</stopOnError>
255257

256258
<useDefaultCommand>false</useDefaultCommand>
@@ -259,12 +261,14 @@
259261

260262
</target>
261263

262-
<target name="BUILD (verbose)" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
264+
<target name="BUILD_Slow (verbose)" path="" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
263265

264266
<buildCommand>${PWD}/k210-freertos/BUILD.sh</buildCommand>
265267

266268
<buildArguments>-v</buildArguments>
267269

270+
<buildTarget/>
271+
268272
<stopOnError>true</stopOnError>
269273

270274
<useDefaultCommand>false</useDefaultCommand>
@@ -279,8 +283,6 @@
279283

280284
<buildArguments>-j16</buildArguments>
281285

282-
<buildTarget/>
283-
284286
<stopOnError>true</stopOnError>
285287

286288
<useDefaultCommand>false</useDefaultCommand>
@@ -295,8 +297,6 @@
295297

296298
<buildArguments>-v -j16</buildArguments>
297299

298-
<buildTarget/>
299-
300300
<stopOnError>true</stopOnError>
301301

302302
<useDefaultCommand>false</useDefaultCommand>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
eclipse.preferences.version=1
2+
encoding//firmware/kflash.py=utf-8
23
encoding//k210-freertos/MPyTerm.py=utf-8
34
encoding//k210-freertos/kflash.py=utf-8
5+
encoding//k210-freertos/ktool.py=utf-8
46
encoding//micropython/docs/conf.py=utf-8

Kboot/CMakeLists.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# DO NOT MODIFY THIS FILE, IT WILL BE OVERRIDE!!!
2+
3+
# set this will supress some warnings
4+
set(BUILDING_SDK "yes" CACHE INTERNAL "")
5+
6+
# basic config
7+
if (NOT PROJ)
8+
get_filename_component(PROJ ${CMAKE_CURRENT_BINARY_DIR} DIRECTORY)
9+
get_filename_component(PROJ ${PROJ} NAME)
10+
string(REPLACE " " "_" PROJ ${PROJ})
11+
message(STATUS "PROJ not set, use ${PROJ} as PROJ. Also, you can set it manually. e.g. -DPROJ=hello_world")
12+
else()
13+
message("PROJ = ${PROJ}")
14+
endif ()
15+
cmake_minimum_required(VERSION 3.0)
16+
17+
include(./cmake/common.cmake)
18+
project(${PROJ} C ASM)
19+
20+
# config self use headers
21+
include(./cmake/macros.internal.cmake)
22+
header_directories(${SDK_ROOT}/lib)
23+
header_directories(src/${PROJ})
24+
header_directories(kendryte-standalone-demo/${PROJ})
25+
# build library first
26+
add_subdirectory(lib)
27+
28+
# compile project
29+
add_source_files(src/${PROJ}/*.c src/${PROJ}/*.s src/${PROJ}/*.S src/${PROJ}/*.cpp)
30+
add_source_files(kendryte-standalone-demo/${PROJ}/*.c kendryte-standalone-demo/${PROJ}/*.s kendryte-standalone-demo/${PROJ}/*.S kendryte-standalone-demo/${PROJ}/*.cpp)
31+
include(./cmake/executable.cmake)
32+

Kboot/Kboot.md

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
# Kboot
2+
3+
**Kboot** is a small applications (< 8KB) which enables loading and executing applications from any K210 SPI Flash location.
4+
5+
## Features
6+
7+
* Load and execute K210 application stored at any location in SPI Flash.
8+
* Application to be loaded is chosen from the list of available applications in **Kboot configuration sector**.
9+
* Multiple criteria application validity check is performed when selecting the application.
10+
* If no valid application is found **default application** is loaded and executed.
11+
* The **interactive mode** can be enabled in which, based on the specific **Pin** state, the user can choose which application to load and execute from the list of found applications.
12+
<br>
13+
14+
## K210 Boot process when using **kboot**
15+
16+
* After reset, the **1st part** (stage #0) of **Kboot** application is loaded by **K210 ROM** to SRAM address **`0x80000000`** and executed.
17+
* SPI Flash driver is initialized in **XiP** mode.
18+
* The **2nd part** (stage #1) of **Kboot** application is loaded to high SRAM address **`0x805E0000`** and the execution is continued fram that address.
19+
* If allowed by configuration, **Boot Pin** state is checked and saved<br>If Boot Pin is activated (pulled to GND) **interactive mode** is enabled.
20+
* **Configuration sector** is scanned for valid applications.<br>Following actions are performed:
21+
* Configuration entry ID is checked
22+
* Application Flash address is cheched for valid range
23+
* Application size is checked for valid range
24+
* If **Size** flag is set, application size is checked agains the acctual size written in application's _Flash block_.
25+
* Application flags are saved
26+
* Application _Flash block_ **<sup>*</sup>** is checked for valid format
27+
* If **CRC32** flag is set, application's crc32 is calculated and checked
28+
* If **SHA256** flag is set, application's SHA256 hash is calculated and checked
29+
* If no valid application is found, the **backup** configuration sector is scanned.
30+
* If all checks passes, the application is marked as **valid**.
31+
* If **not in interactive mode**, configuration sector scan is terminated as soon as the first valid, flagged as **active** application is found and that application is loaded and executed.
32+
* If in **interactive mode**, all configuration sector entries are scanned and the user is prompted to select which one to load and execute.
33+
* If **no valid application** is found, the **default application** is loaded and executed
34+
* If the **default application** fails the application test, the system is **halted**
35+
36+
> **<sup>*</sup>** each application stored in SPI Flash has the following format:
37+
>
38+
> | Byte offset | Size | Name | Content |
39+
> | ---: | ---: | :---: | :--- |
40+
> | `0` | `1` | AES_FLAG | AES cipher flag, for use with **Kboot** must be `0` |
41+
> | `1` | `4` | APP_SIZE | Application code size |
42+
> | `5` | `APP_SIZE` | APP_CODE | Application code |
43+
> | `APP_SIZE + 5` | `32` | SHA_HASH | Application SHA256 hash |
44+
>
45+
> All applications flashed with **ktool.py** or **kflash.py** have such format.
46+
47+
<br>
48+
49+
## K210 SPI Flash layout
50+
51+
The following SPI Flash layout must be used when using **Kboot*
52+
53+
| From | To | Length | Comment |
54+
| ---: | ---: | ---: | :--- |
55+
| `0x00000000` | `0x00000FFF` | 4K | 1st part of **Kboot** application, **stage #0** code |
56+
| `0x00001000` | `0x00003FFF` | 12K | 2nd part of **Kboot** application, **stage #1** code |
57+
| `0x00004000` | `0x00004FFF` | 4K | **main** boot configuration sector |
58+
| `0x00005000` | `0x00005FFF` | 4K | **backup** boot configuration sector |
59+
| `0x00006000` | `0x0000FFFF` | 40K | reserved, user data etc. |
60+
| `0x00010000` | `DEF_APP_END` | --- | **default application** code |
61+
| >`DEF_APP_END` | `FLASH_END` | --- | user area, application(s) code, file system(s), user data etc. |
62+
63+
<br>
64+
65+
## **Boot configuration** sector
66+
67+
Boot configuration used by **Kboot** is stored in one SPI Flash sector (4KB) at fixed Flash address (`0x00004000`).<br>
68+
One **backup** configuration sector is also used for security reasons, stored in one SPI Flash sector (4KB) at fixed Flash address (`0x00005000`).<br>
69+
70+
The configuration sector consists of **`8`** _application entries_ occupying **`32`** bytes each.<br>
71+
After the last application entry the **config flags** entry is placed, one 32bit value.<br>
72+
73+
Configuration sector layout:
74+
75+
| Offset | To | Length | Comment |
76+
| ---: | ---: | ---: | :--- |
77+
| `0x000` | `01F` | `32` | application entry #0 |
78+
| `0x020` | `03F` | `32` | application entry #1 |
79+
| `0x040` | `05F` | `32` | application entry #2 |
80+
| `0x060` | `07F` | `32` | application entry #3 |
81+
| `0x080` | `09F` | `32` | application entry #4 |
82+
| `0x0A0` | `0BF` | `32` | application entry #5 |
83+
| `0x0C0` | `0DF` | `32` | application entry #6 |
84+
| `0x0E0` | `0FF` | `32` | application entry #7 |
85+
| `0x100` | `103` | `4` | **config flags** |
86+
| `0x104` | `11F` | `4` | reserved |
87+
| `0x120` | `11F` | `4` | not used, user data |
88+
<br>
89+
90+
The format of each _application entry_ in configuration sector is as follows:
91+
92+
| Offset | Length | Comment |
93+
| ---: | ---: | :--- |
94+
| `0` | `4` | Configuration entry **ID** (bits 4-31) + entry **flags** (bits 0-4) |
95+
| `4` | `4` | Application address in SPI Flash |
96+
| `8` | `4` | Application size in SPI Flash. This is the size of the application's **`.bin`** file |
97+
| `12` | `4` | Application's CRC32 value (32bits) |
98+
| `16` | `16` | Null terminated application name or description |
99+
100+
_Notes:_<br>
101+
Configuration entry **ID** must have a value of **`0x5AA5D0C0`** for entry to be recognized as valid.<br>
102+
Application addres must be in range **`0x10000`** ~ **`0x800000`** ( 64KB ~ 8MB ).<br>
103+
Application size must be in range **`0x4000`** ~ **`0x300000`** ( 16KB ~ 3MB ).<br>
104+
Application CRC32 value is used only if **CRC32** flag is set.<br><br>
105+
106+
If **config flags** at offset `0x100` in **configuration sector** is set to configuration entry **ID** (**`0x5AA5D0C0`**), **interractive mode** will be disabled, **boot Pin* will not be checked and nothing will be printed during the boot process.
107+
Configuration entry **flags**:
108+
109+
| Bit | Comment |
110+
| :---: | :--- |
111+
| `0` | **Active** flag, if set the application will be loaded and executed.<br>If multiple entries have **active** flag set, the first one will be loaded ad executed |
112+
| `1` | **CRC32** flag, if set the application's CRC32 value will be calculated and compared with the value in the configuration entry |
113+
| `2` | **AES256** flag, if set the application's AES256 hash value will be calculated and compared with the value stored in flash after the application code (`SHA_HASH`) |
114+
| `3` | **Size** flag, if set the application size specified in configuration entry must match the application size present in application's _Flash block_ |
115+
116+
**_Warning:_** all 32-bit values in the configuration sector entries must be written in **big-endian** format.
117+
118+
<br>
119+
120+
## Default application
121+
122+
If in the boot process **no valid application** was found in the **configuration sector**, the **default application** is loaded and executed.<br>
123+
**_The default application should try to check if loading it was **intended** or it was result of an error condition, in which case it should try to correct the issue._**
124+
125+
<br>
126+
127+
## Building the applications to be used with _Kboot_
128+
129+
Nothing special must be done to build the applications which are going to be used with **Kboot**.<br>
130+
All applications built with Kendryte **Standalone SDK** or **FreeRTOS SDK** should run without issues.<br>
131+
132+
The application can check if the **Kboot** system is used by reading the 2nd SPI Flash sector (at address **`0x1000`**).<br>
133+
At offset `0x09` the `Kboot` **id string** is positioned: **`Kboot_v1.4.1`** (version mumbers may be different).<br>
134+
135+
```
136+
[00001000] 00 B0 1F 00 00 6F 00 40 01 4B 62 6F 6F 74 5F 76 31 2E 34 2E 31 00 00 00 00 19 71 86 FC A2 F8 A6
137+
[00001020] F4 CA F0 CE EC D2 E8 D6 E4 DA E0 5E FC 62 F8 66 F4 6A F0 6E EC F3 27 40 F1 81 27 17 27 00 00 23
138+
```
139+
140+
_See the note about sector data!_
141+
142+
<br><br>
143+
144+
## Usage
145+
146+
Flashing the application(s) to SPI Flash and manipulating the boot **configuration sector** should be performed from the user application.<br>
147+
Accessing SPI flash from K210 application is quite easy and reliable.
148+
149+
150+
151+
### Example
152+
153+
**Application firmware update**
154+
155+
* The condition to update the firmware is detected by the application
156+
* Load the **boot configuration** sector, check its integrity and check the flash address and size of the currently running application
157+
* Flash the new firmware to the **not used** SPI Flash area. The new firmware can be downloaded from remote server using WiFi or GSM, loaded from SD Card, etc ...<br>Use the **application block** format described above.<br>The application can be flashed **without** SHA256 hash, in that case do not set the **`SHA256`** flag.
158+
* Calculate new firmware's CRC32 and/or SHA256 hash if needed.
159+
* Flash the current **main** boot configuration sector to the **backup** boot configuration sector and check it.<br>It will be used by **Kboot** in case the **main** boot configuration sector which we are going to write is corrupted.
160+
* Update (add or change entry) in the boot configuration sector with the new firmware information, set the new firmware as active and the old firmware as inactive and flash the configuration sector.
161+
* Reset (reboot) the system to start the new firmware.
162+
163+
164+
### Important Notes
165+
166+
When reading sectors written with **ktool.p<** or **kflash.py** from SPI Flash in **normal** mode (not using **XiP** mode), the sector data may look corrupted.<br>
167+
The reason is that all sector data are written with **swapped** 32-bit values (32-bit **big endian** format is used).<br>
168+
To correct this, you should **swap endianess** of all 32-bit values in the sector, something like this:.<br>
169+
170+
```
171+
uint8_t buf4[4];
172+
for (int k=0; k<4096; k+=4) {
173+
buf4[0] = sector[k+0];
174+
buf4[1] = sector[k+1];
175+
buf4[2] = sector[k+2];
176+
buf4[3] = sector[k+3];
177+
sector[k+0] = buf4[3];
178+
sector[k+1] = buf4[2];
179+
sector[k+2] = buf4[1];
180+
sector[k+3] = buf4[0];
181+
}
182+
```

0 commit comments

Comments
 (0)