Skip to content

Commit 170f897

Browse files
authored
Merge pull request #23 from gpappasv/dev/improve_doc
Improve documentation of the repository
2 parents 71d8b13 + 980c082 commit 170f897

File tree

5 files changed

+48
-15
lines changed

5 files changed

+48
-15
lines changed

README.md

+14-5
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,17 @@ FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 32K /* 32K of flash is reserved f
6767
/* --- BOOTLOADER CONFIGURATION SPECIFIC INFORMATION END --- */
6868
```
6969

70+
NOTE: While configuring this part of the bootloader linker script you need to carefully consider the flash layout of your MCU.
71+
A good practice would be to always start the primary and secondary applications from the beginning of the desired flash page. Also you need to be careful to not have any overlaps between the two.
72+
Also, since the bootloader cannot update itself, once you flash the bootloader, you cannot modify the flash layout after that, on future application releases.
73+
7074
- Adapt your application:
71-
Similarly to the bootloader, the application must also have that information inside its linker script.
75+
Similarly to the bootloader, the application should also have that information inside its linker script. This is optional though, in case that you need that information inside your application for any reason.
76+
7277
It is important to not mess it up and be careful while copy-pasting that information to your linker script, as the tools that are provided under scripts/build_tools parse the application linker to find the relevant information. More information on how to use those tools can be found in the relevant README.md files.
7378

79+
- Utilize the script **scripts/build_tools/create_dfu_image.py** to make your app binary compatible to the built bootloader. More info on how to use it can be found in **scripts/build_tools/README.md**. The modified binary will be exported to the current working directory.
80+
7481
- You also need to modify the **private_key.pem** and **public_key.pem**, under **projects/**. They are being used to sign the application, and provide the bootloader with the relevant information on authenticating the signature.
7582

7683
# Porting bootloader to other boards
@@ -79,7 +86,7 @@ design the project in a way to be as easily portable as possible. Still there ar
7986
in that area, but you could start by reading the **README.md**, under **projects/bootloader**. There, you can find some useful notes
8087
that can help you port the project faster to your board.
8188

82-
**You can always reach out to me, to guide you through it.**
89+
**You can always reach out to me, to guide you through it. Feel free to push a commit, adding support for your board.**
8390

8491
TODO: In the future, projects/bootloader/boards/<board_name> will contain all the **driver level** code for each specific board, and a simple define will utilize the correct board subfolder to build the bootloader.
8592

@@ -88,10 +95,12 @@ TODO.
8895

8996
# How to use
9097
1. Modify the bootloader (linker script/drivers) as you like based on the information provided before.
91-
2. Use the build.sh script to build the bootloader.bin.
98+
2. Use the build.sh script to build the bootloader.bin. (e.g. build.sh bootloader)
9299
3. Flash the bootloader.bin on flash address 0x08000000.
93-
4. You can develop your application following the example of projects/app. In that case, modify the linker script accordingly, update the private-public key pair and use the build.sh script to build the application.
94-
5. You can always have a binary yourself and utilize the underlying tools (create_dfu_image.py).
100+
4. You can develop your application following the example of projects/app. In that case, modify the linker script accordingly, update the private-public key pair and use the build.sh script to build the application. (e.g. build.sh all OR build.sh app)
101+
5. You can always have a binary yourself and utilize the underlying tools (create_dfu_image.py). With this script you can convert your binary to a compatible application to be used by the bootloader.
102+
103+
IMPORTANT: More information about each script can be found inside the relevant folder that contains it.
95104

96105
# Repository structure
97106
The structure of the repository is as follows:

scripts/build_tools/README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,25 @@ More information for that, can be found inside the python script.
6969
To utilize it for a specific binary, run the following command:
7070

7171
```bash
72-
python create_dfu_image.py </path/to/bin> <path/to/linker_script> <version_major> <version_minor> <patch>
72+
python create_dfu_image.py </path/to/bin> <path/to/linker_script> <version_major> <version_minor> <patch> <path/to/private_key>
7373
```
7474

7575
After the execution of this script, a new binary will be created that will be ready to be flashed to the board, and be compatible with out bootloader.
7676
Also, a yaml file will be produced that will contain information related to the final binary. (e.g. the crc, sha256 hash, version of the binary).
7777

78+
The reason that this script needs the linker script of the bootloader is to find the following information:
79+
- Size of the application (both primary and secondary - which must be the same)
80+
- Size of the footer, to add the relevant information to the end of the binary. (Also location of the footer in flash)
81+
e.g. __flash_app_start__, __flash_app_end__, __header_app_crc_start__, __header_app_fw_version_start__, __header_app_hash_start__.
82+
83+
Also the private key is used to sign the application.
84+
85+
Note: This tool can be used on any application binary.
86+
E.g. Let's say you have an application (binary) of size 120kB.
87+
You configure the bootloader of this repository, to support an application binary of up to 230kB.
88+
You can easily use this script to adapt your binary based on the bootloader standards and make it compatible with it.
89+
Note: Don't forget to update the private-public key pair under **projects**. This is important to build your bootloader based on that pair and use the private key to sign your application. You need to create key pair based on ECDSA (chatgpt is your friend there :) )
90+
7891
# extract public key python script description
7992
This script is being used pre-process a project firmware headerfile and rewrite it in order to add the public key information, in C-array format. The final result of that header file will be:
8093
```bash
@@ -102,4 +115,6 @@ const uint8_t ecdsa_public_key[] = { <public_key info> };
102115
Usage:
103116
```bash
104117
python extract_public_key.py <public_key.pem> <ecdsa_pub_key.h>
105-
```
118+
```
119+
120+
Currently this is automatically being used by the build.sh script while building the bootloader.

scripts/build_tools/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ create_dfu_image() {
5757

5858
echo "Creating DFU image..."
5959
pwd
60-
python create_dfu_image.py "$GIT_ROOT/$binary_path" "$GIT_ROOT/$linker_script" "$version_major" "$version_minor" "$version_patch" "$GIT_ROOT/$private_key_path"
60+
python create_dfu_image.py "$GIT_ROOT/$binary_path" "$GIT_ROOT/$linker_script" "$version_major" "$version_minor" "$version_patch" "$GIT_ROOT/$private_key_path" 1
6161
#if python fails, try with python3
6262
if [ $? -ne 0 ]; then
6363
python3 create_dfu_image.py "$GIT_ROOT/$binary_path" "$GIT_ROOT/$linker_script" "$version_major" "$version_minor" "$version_patch" "$GIT_ROOT/$private_key_path"

scripts/build_tools/build_info.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ projects:
2424
dfu_image:
2525
enabled: true
2626
binary_path: "projects/app/build/app.bin"
27-
linker_script: "projects/app/boards/stm32f401re/STM32F401RETx_FLASH.ld"
27+
linker_script: "projects/bootloader/boards/stm32f401re/STM32F401RETx_FLASH.ld"
2828
version:
2929
major: 0
3030
minor: 1

scripts/build_tools/create_dfu_image.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import hashlib
1919
import os
2020
import yaml
21+
import shutil
2122
from cryptography.hazmat.primitives import hashes, serialization
2223
from cryptography.hazmat.primitives.asymmetric import ec
2324
from cryptography.hazmat.primitives.asymmetric.utils import Prehashed
@@ -182,7 +183,7 @@ def add_signature_to_footer(self):
182183
print(f"\nAdding post processed signature to the footer: {signature.hex()} of len: {len(signature)} bytes")
183184
self.binary_data[footer_start:footer_start + SIGNATURE_SIZE_BYTES] = signature
184185

185-
def commit_to_file(self, file_path):
186+
def commit_to_file(self, file_path, export_bin_to_cwd=False):
186187
"""
187188
Commits the modified binary data to the given file path.
188189
@@ -196,7 +197,12 @@ def commit_to_file(self, file_path):
196197
# Determine the filename of the binary file
197198
binary_filename = os.path.basename(file_path)
198199
# Create the full path to write the binary file inside the update folder
199-
update_file_path = os.path.join(update_folder, binary_filename)
200+
if export_bin_to_cwd is False:
201+
update_file_path = os.path.join(update_folder, binary_filename)
202+
else:
203+
cwd = os.getcwd()
204+
print(f"Exporting binary to current working directory: {cwd}")
205+
update_file_path = os.path.join(os.getcwd(), binary_filename)
200206

201207
# Write the binary data to the specified file path
202208
with open(update_file_path, "wb") as binary_file:
@@ -213,8 +219,8 @@ def commit_to_file(self, file_path):
213219
yaml.dump(yaml_info, yaml_file, default_flow_style=False)
214220

215221
def main():
216-
if len(sys.argv) != 7:
217-
print(f"Usage: {sys.argv[0]} <binary_file> <linker_script> <version_major> <version_minor> <version_patch> <private_key>")
222+
if len(sys.argv) not in (7, 8):
223+
print(f"Usage: {sys.argv[0]} <binary_file> <linker_script> <version_major> <version_minor> <version_patch> <private_key> [export_bin_to_cwd]")
218224
sys.exit(1)
219225

220226
binary_file_path = sys.argv[1]
@@ -223,6 +229,9 @@ def main():
223229
version_minor = int(sys.argv[4])
224230
version_patch = int(sys.argv[5])
225231
private_key = sys.argv[6]
232+
export_bin_to_cwd = True if len(sys.argv) == 7 or sys.argv[7].lower() == 'True' else False
233+
234+
print(f"export_bin_to_cwd: {export_bin_to_cwd}")
226235

227236
try:
228237
with open(binary_file_path, "rb") as binary_file:
@@ -252,11 +261,11 @@ def main():
252261
analyzer.add_signature_to_footer()
253262

254263
# Commit all the information to the binary (actually re-write the binary)
255-
analyzer.commit_to_file(binary_file_path)
264+
analyzer.commit_to_file(binary_file_path, export_bin_to_cwd)
256265

257266
except Exception as e:
258267
print(f"Error: {e}")
259268
sys.exit(1)
260269

261270
if __name__ == "__main__":
262-
main()
271+
main()

0 commit comments

Comments
 (0)