Skip to content

Commit da4e50d

Browse files
authored
Add cmake script to update otp.json from privateaes.bin (#536)
1 parent 9021fdd commit da4e50d

File tree

3 files changed

+79
-78
lines changed

3 files changed

+79
-78
lines changed

bootloaders/encrypted/CMakeLists.txt

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ add_executable(enc_bootloader
44
aes.S
55
)
66

7+
# Add command to update otp.json if privateaes.bin changes
8+
add_custom_command(OUTPUT ${CMAKE_CURRENT_LIST_DIR}/otp.json
9+
COMMAND ${CMAKE_COMMAND} -P "${CMAKE_CURRENT_LIST_DIR}/update-key.cmake"
10+
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin)
11+
# Copy that otp.json file to build directory
12+
add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/otp.json
13+
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_LIST_DIR}/otp.json" "${CMAKE_CURRENT_BINARY_DIR}/otp.json"
14+
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/otp.json)
15+
add_custom_target(otp_json DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
16+
add_dependencies(enc_bootloader otp_json)
17+
718
# pull in common dependencies
819
target_link_libraries(enc_bootloader pico_stdlib pico_rand)
920

@@ -39,7 +50,6 @@ endfunction()
3950
add_linker_script(enc_bootloader "0x20070000" "64k")
4051

4152
# configure otp output
42-
configure_file(${CMAKE_CURRENT_LIST_DIR}/otp.json ${CMAKE_CURRENT_BINARY_DIR}/otp.json COPYONLY)
4353
pico_set_otp_key_output_file(enc_bootloader ${CMAKE_CURRENT_BINARY_DIR}/otp.json)
4454

4555
# sign, hash, and clear SRAM

bootloaders/encrypted/otp.json

+45-77
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,46 @@
11
{
2-
"30:0": {
3-
"ecc": true,
4-
"value": [
5-
"0x00",
6-
"0x01",
7-
"0x02",
8-
"0x03",
9-
"0x04",
10-
"0x05",
11-
"0x06",
12-
"0x07",
13-
"0x08",
14-
"0x09",
15-
"0x0a",
16-
"0x0b",
17-
"0x0c",
18-
"0x0d",
19-
"0x0e",
20-
"0x0f",
21-
"0x00",
22-
"0x10",
23-
"0x20",
24-
"0x30",
25-
"0x40",
26-
"0x50",
27-
"0x60",
28-
"0x70",
29-
"0x80",
30-
"0x90",
31-
"0xA0",
32-
"0xB0",
33-
"0xC0",
34-
"0xD0",
35-
"0xE0",
36-
"0xF0"
37-
]
38-
},
39-
"OTP_DATA_KEY1": [
40-
0,
41-
0,
42-
1,
43-
1,
44-
2,
45-
2,
46-
3,
47-
3,
48-
4,
49-
4,
50-
5,
51-
5,
52-
6,
53-
6,
54-
7,
55-
7
56-
],
57-
"OTP_DATA_KEY1_VALID": "0x010101",
58-
"OTP_DATA_KEY2": [
59-
7,
60-
7,
61-
6,
62-
6,
63-
5,
64-
5,
65-
4,
66-
4,
67-
3,
68-
3,
69-
2,
70-
2,
71-
1,
72-
1,
73-
0,
74-
0
75-
],
76-
"OTP_DATA_KEY2_VALID": "0x010101",
77-
"PAGE30_LOCK0": "0x4a4a4a"
78-
}
2+
"30:0" :
3+
{
4+
"ecc" : true,
5+
"value" :
6+
[
7+
"0x00",
8+
"0x01",
9+
"0x02",
10+
"0x03",
11+
"0x04",
12+
"0x05",
13+
"0x06",
14+
"0x07",
15+
"0x08",
16+
"0x09",
17+
"0x0a",
18+
"0x0b",
19+
"0x0c",
20+
"0x0d",
21+
"0x0e",
22+
"0x0f",
23+
"0x00",
24+
"0x10",
25+
"0x20",
26+
"0x30",
27+
"0x40",
28+
"0x50",
29+
"0x60",
30+
"0x70",
31+
"0x80",
32+
"0x90",
33+
"0xa0",
34+
"0xb0",
35+
"0xc0",
36+
"0xd0",
37+
"0xe0",
38+
"0xf0"
39+
]
40+
},
41+
"OTP_DATA_KEY1" : [ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7 ],
42+
"OTP_DATA_KEY1_VALID" : "0x010101",
43+
"OTP_DATA_KEY2" : [ 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 0, 0 ],
44+
"OTP_DATA_KEY2_VALID" : "0x010101",
45+
"PAGE30_LOCK0" : "0x4a4a4a"
46+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
if (CMAKE_VERSION VERSION_LESS 3.19)
2+
# Check if keyfile is not the default, and print warning
3+
file(READ ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin key_file HEX)
4+
if (NOT ${key_file} STREQUAL "000102030405060708090a0b0c0d0e0f00102030405060708090a0b0c0d0e0f0")
5+
message(WARNING
6+
"Encrypted bootloader AES key not updated in otp.json file, as CMake version is < 3.19"
7+
" - you will need to change the key in otp.json manually and re-run the build"
8+
)
9+
endif()
10+
else()
11+
# Read the JSON file.
12+
file(READ ${CMAKE_CURRENT_LIST_DIR}/otp.json json_string)
13+
# Read the key file
14+
file(READ ${CMAKE_CURRENT_LIST_DIR}/privateaes.bin key_file HEX)
15+
16+
# adds '0x' prefix, comma suffix, and quotes for every byte
17+
string(REGEX REPLACE "([0-9a-f][0-9a-f])" "\"0x\\1\", " key_file ${key_file})
18+
set(key_file_json "[${key_file}]")
19+
20+
string(JSON json_string SET ${json_string} "30:0" "value" ${key_file_json})
21+
22+
file(WRITE ${CMAKE_CURRENT_LIST_DIR}/otp.json ${json_string})
23+
endif()

0 commit comments

Comments
 (0)