Skip to content

Commit 905a595

Browse files
committed
allow gen_crt_bundle to generate a .S file directly
.. this gets rid of one of the steps of converting it
1 parent 09023ab commit 905a595

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

requirements-dev.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ pyelftools
2626
# for stubs and annotations
2727
adafruit-circuitpython-typing
2828

29+
# for mbedtls certificate store
30+
cryptography
31+
2932
# for web workflow minify
3033
minify_html
3134
jsmin

tools/gen_crt_bundle.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import re
3131
import struct
3232
import sys
33+
import textwrap
3334
from io import open
3435

3536
try:
@@ -217,6 +218,15 @@ def main():
217218
help="Path to CSV-file where the second columns contains the name of the certificates \
218219
that should be included from cacrt_all.pem",
219220
)
221+
parser.add_argument(
222+
"--asm",
223+
"-S",
224+
action="store_true",
225+
default=False,
226+
help="Output an asm file for use with gas, rather than a binary file",
227+
)
228+
parser.add_argument("--symbol", help="The symbol to define", default="x509_crt_bundle")
229+
parser.add_argument("--output", "-o", help="The output file", default=None)
220230

221231
args = parser.parse_args()
222232

@@ -239,8 +249,45 @@ def main():
239249

240250
crt_bundle = bundle.create_bundle()
241251

242-
with open(ca_bundle_bin_file, "wb") as f:
243-
f.write(crt_bundle)
252+
if args.asm:
253+
symbol = args.symbol
254+
filename = args.output or (ca_bundle_bin_file + ".S")
255+
with open(filename, "w") as f:
256+
print(
257+
textwrap.dedent(
258+
f"""\
259+
// Generated from {" ".join(args.input)} with {len(bundle.certificates)} certificates
260+
.data
261+
.section .rodata.embedded
262+
263+
.global {symbol}
264+
.global _binary_{symbol}_start
265+
.global _binary_{symbol}_end
266+
{symbol}:
267+
_binary_{symbol}_start:
268+
"""
269+
),
270+
file=f,
271+
)
272+
for i in range(0, len(crt_bundle), 16):
273+
chunk = crt_bundle[i : i + 16]
274+
formatted = ", ".join(f"0x{byte:02x}" for byte in chunk)
275+
print(f".byte {formatted}", file=f)
276+
print(
277+
textwrap.dedent(
278+
f"""\
279+
_binary_{symbol}_end:
280+
281+
{symbol}_length:
282+
.word {len(crt_bundle)}
283+
"""
284+
),
285+
file=f,
286+
)
287+
else:
288+
filename = args.output or ca_bundle_bin_file
289+
with open(filename, "wb") as f:
290+
f.write(crt_bundle)
244291

245292

246293
if __name__ == "__main__":

0 commit comments

Comments
 (0)