-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathC0_microSD_toolkit.py
More file actions
655 lines (567 loc) · 22.5 KB
/
Copy pathC0_microSD_toolkit.py
File metadata and controls
655 lines (567 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#!/usr/bin/env python3
# Copyright (c) 2024, Signaloid.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
import argparse
import sys
import os
import re
from typing import Optional, Tuple
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent / "src" / "python"))
from signaloid_utilities.c0microsd.interface import C0microSDInterface
from signaloid_utilities.c0microsd.constants import BOOTLOADER_CONSTANTS
from signaloid_utilities.common.bitstream_prefix import (
find_json_object,
)
APP_VERSION = "2.0" # Application version
MAX_FLASH_ATTEMPTS = 5 # Maximum flashing attempts
class C0microSDToolkit(C0microSDInterface):
def __init__(self, target_device: str, force_transactions: bool = False):
super().__init__(target_device, force_transactions)
self.get_status()
btldr_maj_ver = self.configuration_version[0] if self.configuration_version is not None else None
if (btldr_maj_ver not in BOOTLOADER_CONSTANTS):
print("Warning. Bootloader version unrecognised. "
"Falling back to Ver 1 configuration")
btldr_maj_ver = 1
self.BOOTLOADER_SWITCH_CONFIG_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kBootloaderSwitchConfigOffset
self.BOOTLOADER_UNLOCK_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kBootloaderUnlockOffset
self.BOOTLOADER_BITSTREAM_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kBootloaderBitstreamOffset
self.SOC_BITSTREAM_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kSOCBitstreamOffset
self.USER_BITSTREAM_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kUserBitstreamOffset
self.USER_DATA_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kUserDataOffset
self.SERIAL_NUMBER_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kSerialNumberOffset
self.SERIAL_NUMBER_SIZE = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kSerialNumberSize
self.UUID_OFFSET = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kUUIDOffset
self.UUID_SIZE = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kUUIDSize
self.BOOTLOADER_UNLOCK_WORD = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kBootloaderUnlockWord
self.WARMBOOT_TEMPLATE = \
BOOTLOADER_CONSTANTS[btldr_maj_ver].kWamrbootTemplate
def _strip_trailing_bytes(
self, byte_array: bytearray, byte: int
) -> bytearray:
"""
Strip the trailing bytes of a bytearray
:param byte_array (iterable of bytes): Array of bytes to strip.
:param byte (int): The byte to remove.
:return (bytearray): Stripped array of bytes
"""
end = len(byte_array)
while end > 0 and byte_array[end - 1] == byte:
end -= 1
return byte_array[:end]
def switch_boot_config(self, verbose: bool = True) -> None:
"""
Switches the boot configuration of C0-microSD.
"""
if verbose:
self.get_status()
if (self.configuration) == "bootloader":
print(
"Switching device boot mode from "
"Bootloader to Signaloid SoC..."
)
elif (self.configuration) == "soc":
print(
"Switching device boot mode from "
"Signaloid SoC to Bootloader..."
)
elif self.force_transactions:
print("Switching device boot mode...")
self._write(self.BOOTLOADER_SWITCH_CONFIG_OFFSET, bytes([0] * 512))
if verbose:
print(
"Device configured successfully. "
"Power cycle the device to boot in new mode."
)
if (self.configuration == "bootloader"):
print(
"To use the Signaloid C0-microSD in Custom User Bitstream mode"
", power it on without an SD-protocol host present."
)
def unlock_bootloader(self) -> None:
"""
Unlocks the bootloader. Used to flash new bootloader or Signaloid SoC.
"""
self.get_status()
print("Unlocking bootloader...")
self._write(self.BOOTLOADER_UNLOCK_OFFSET, self.BOOTLOADER_UNLOCK_WORD)
def lock_bootloader(self) -> None:
"""
Locks the bootloader and Signaloid SoC sections.
"""
self.get_status()
print("Locking bootloader...")
self._write(self.BOOTLOADER_UNLOCK_OFFSET, bytes([0] * 32))
def flash_and_verify(
self,
file_data: bytes,
flash_offset: int,
max_attempts: int = MAX_FLASH_ATTEMPTS,
unlock_bootloader: bool = False,
verbose: bool = True,
) -> bool:
"""
Flashes data to the C0-microSD and verifies that the flashing
process was successful.
:param file_data: A byte buffer with the data to be written
:param flash_offset: Device offset (in bytes) for the data
to be written
:param max_attempts: Maximum failed attempts before aborting operation
"""
self.get_status()
if self.configuration != "bootloader" and not self.force_transactions:
raise RuntimeError(
"Error: device is not in Bootloader mode. "
"Switch to Bootloader mode and try again"
)
if (unlock_bootloader):
self.unlock_bootloader()
input_file_bytes = len(file_data)
for i in range(1, max_attempts + 1):
if verbose:
print(
f"Attempt {i} of {max_attempts}: Flashing... ",
end="",
flush=True
)
self._write(flash_offset, file_data)
if verbose:
print("Verifying...")
data_to_verify = self._read(flash_offset, input_file_bytes)
if data_to_verify == file_data:
if verbose:
print("Success: The data matches.")
if (unlock_bootloader):
self.lock_bootloader()
return True
else:
if verbose:
print("Error: The data do not match.")
if (unlock_bootloader):
self.lock_bootloader()
return False
def find_json_string(self, data: bytes) -> Optional[dict]:
"""
Attempts to decode the first valid JSON object from a byte stream.
Assumes input is <= 4 KB and encoded in ASCII.
Kept for backwards compatibility; delegates to
:meth:`C0microSDInterface.find_json_string`.
"""
return super().find_json_string(data)
def get_bitstream_prefix(
self,
bitstream_offset: int) -> Tuple[Optional[dict], int, int]:
"""
Reads the prefix section of a bitstream
Kept for backwards compatibility; delegates to
:meth:`C0microSDInterface.get_bitstream_prefix`.
:param bitstream_offset: Offset of bitstream in flash memory
"""
return super().get_bitstream_prefix(bitstream_offset)
def verify_bitstream_crc(
self,
bitstream_offset: int,
bitstream_crc: int,
bitstream_prefix_size: int,
bitstream_size: int
) -> bool:
"""
Verifies a the crc32 checksum of a bitstream
Kept for backwards compatibility; delegates to
:meth:`C0microSDInterface.verify_bitstream_crc`.
:param bitstream_offset: Offset of bitstream in flash memory
:param bitstream_crc: Expected crc of bitstream
:param bitstream_size: Expected size of bitstream in bytes
"""
return super().verify_bitstream_crc(
bitstream_offset,
bitstream_crc,
bitstream_prefix_size,
bitstream_size
)
def print_bitstream_information(self, offset) -> None:
"""
Reads and prints bitstream prefix from a specific offset in the
device. Also runs crc verification if prefix is in json format and
includes `bitstream_crc` and `bitstream_size` attributes
Kept for backwards compatibility; delegates to
:meth:`C0microSDInterface.print_bitstream_information`.
:param offset: Offset of bitstream in flash memory
"""
return super().print_bitstream_information(offset)
def verify_warmboot_section(self, template: Optional[str] = None) -> bool:
warmboot_section = self._read(0, 5*32).hex()
if template is None:
template = self.WARMBOOT_TEMPLATE
return warmboot_section == template
def get_serial_number(self) -> str:
serial_number_section = self._read(
self.SERIAL_NUMBER_OFFSET, self.SERIAL_NUMBER_SIZE
)
serial_number_section = self._strip_trailing_bytes(
serial_number_section, 0xFF
)
serial_number_section = ''.join(
to_printable(byte) for byte in serial_number_section
)
return serial_number_section
def get_uuid(self) -> str:
uuid_section = self._read(
self.UUID_OFFSET, self.UUID_SIZE
)
uuid_section = self._strip_trailing_bytes(
uuid_section, 0xFF
)
uuid_section = ''.join(
to_printable(byte) for byte in uuid_section
)
return uuid_section
def to_printable(byte: bytearray) -> str:
"""
Decode byte to character using UTF-8 encoding.
Decode anything that is not UTF-8 as '.'
"""
return chr(byte) if 32 <= byte <= 126 else '.'
def confirm_action() -> bool:
"""
Prompts the user to accept/reject action
:return: response
"""
while True:
# Prompt the user with the warning message
response = input(
"WARNING: This action may render the device inoperable. "
"Proceed? (y/n): "
).lower()
if response == "y":
return True
elif response == "n":
return False
else:
print("Invalid input. Please enter 'y' for yes or 'n' for no.")
def parse_size(size_str):
"""
Parses a size string with optional suffixes (K, M, G)
and converts it to bytes.
:param size_str: Size string (e.g., '1K', '5M', '3G')
:return: Size in bytes as an integer
"""
match = re.match(r"(\d+)([KMG]?)", size_str.upper())
if not match:
raise ValueError("Invalid padding size format. "
"Use a number or a number with suffix (K, M, G).")
size = int(match.group(1))
suffix = match.group(2)
if suffix == 'K':
return size * 1024
elif suffix == 'M':
return size * (1024 ** 2)
elif suffix == 'G':
return size * (1024 ** 3)
else:
return size
def main(explicit_args: list[str] | None = None):
parser = argparse.ArgumentParser(
description=f"Signaloid C0-microSD-toolkit. Version {APP_VERSION}",
add_help=False
)
parser.add_argument(
'-h', '--help',
action='help',
default=argparse.SUPPRESS,
help='Show this help message and exit.'
)
parser.add_argument(
"-t",
dest="target_device",
required=True,
help="Specify the target device path.",
)
parser.add_argument(
"-b",
dest="input_file",
help=("Specify the input file for flashing "
"(required with -u, -q, or -w)."),
)
parser.add_argument(
"-p",
dest="pad_size",
type=str,
help=("Pad input file with zeros to target size.")
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
"-u",
dest="flash_user_data",
action="store_true",
help="Flash user data."
)
group.add_argument(
"-U",
dest="flash_user_data_auto",
action="store_true",
help="Flash user data with auto switching to and from bootloader mode."
)
group.add_argument(
"-q",
dest="flash_bootloader",
action="store_true",
help="Flash new Bootloader bitstream."
)
group.add_argument(
"-w",
dest="flash_signaloid_soc",
action="store_true",
help="Flash new Signaloid SoC bitstream."
)
group.add_argument(
"-s",
dest="switch_boot_mode",
action="store_true",
help="Switch boot mode."
)
group.add_argument(
"-i",
dest="print_information",
action="store_true",
help="Print target C0-microSD information, and run data verification."
)
group.add_argument(
"-y",
dest="flash_warmboot",
action="store_true",
help="Flash warmboot sector."
)
parser.add_argument(
"-f",
dest="force_flash",
action="store_true",
help="Force flash sequence (do not check for bootloader).",
)
args = parser.parse_args(explicit_args)
# Create a new toolkit instance
try:
# Create a new toolkit object
toolkit = C0microSDToolkit(
args.target_device, force_transactions=args.force_flash
)
# Get status of the C0-microSD, also used to verify that communication
# is correct, and that the C0-microSD is in bootloader mode.
toolkit.get_status()
print(toolkit)
# Print additional information and exit
if args.print_information:
if toolkit.configuration != "bootloader":
print("Device is not in Bootloader mode.")
print(
"To display device Serial Number, device UUID, and verify "
"the bitstream and warmboot sections \nof the "
"non-volatile memory, switch to Bootloader mode and "
"try again."
)
exit(os.EX_CONFIG)
print(f"Device Serial Number: {toolkit.get_serial_number()}")
print(f"Device UUID: {toolkit.get_uuid()}")
print()
print("Reading Bootloader bitstream:")
toolkit.print_bitstream_information(
toolkit.BOOTLOADER_BITSTREAM_OFFSET)
print("Reading Signaloid SoC bitstream:")
toolkit.print_bitstream_information(
toolkit.SOC_BITSTREAM_OFFSET)
toolkit.verify_warmboot_section()
if (toolkit.verify_warmboot_section()):
print("Warmboot section verification: PASS")
else:
print("Warmboot section verification: FAIL")
# Bootloader V2.0 and up supports flashing the warmboot
if (toolkit.configuration_version[0] >= 2):
print("You can attempt to fix the Warmboot "
"section by using the -y argument")
print("Done.")
exit(os.EX_OK)
# Print additional information and exit
if args.flash_warmboot:
if toolkit.configuration != "bootloader":
print("Device is not in Bootloader mode.")
print(
"To display device Serial Number, device UUID, and verify "
"the bitstream and warmboot sections \nof the "
"non-volatile memory, switch to Bootloader mode and "
"try again."
)
exit(os.EX_CONFIG)
if (toolkit.configuration_version[0] < 2):
print(
"Error: Bootloader version "
f"{toolkit.configuration_version[0]}."
f"{toolkit.configuration_version[1]} "
"cannot flash the warmboot section."
)
exit(os.EX_CONFIG)
if not confirm_action():
print("Aborting.")
exit(os.EX_USAGE)
print("Flashing wamrboot section...")
warmboot_bytes = bytes.fromhex(toolkit.WARMBOOT_TEMPLATE)
print(toolkit.WARMBOOT_TEMPLATE)
toolkit.flash_and_verify(warmboot_bytes, 0, 10, True)
print("Done.")
exit(os.EX_OK)
# This is the time to switch boot mode if needed.
if args.switch_boot_mode:
toolkit.switch_boot_config()
print("Done.")
exit(os.EX_OK)
# All commands after this point need an input file
if not args.input_file:
parser.print_help()
print("\nOption -b is required when flashing data.")
sys.exit(os.EX_USAGE)
# Open the input file and store data in memory.
file_data = None
try:
with open(args.input_file, "rb") as src:
file_data = src.read()
except PermissionError:
raise PermissionError(
"Permission denied: You do not have the "
f"necessary permissions to access {args.input_file}."
)
except FileNotFoundError:
raise FileNotFoundError(
f"File not found: The file {args.input_file} does not exist."
)
print("Filename: ", args.input_file)
print("File size: ", len(file_data), "bytes.")
# Parse the pad size if provided
pad_size = None
if args.pad_size is not None:
pad_size = parse_size(args.pad_size)
if pad_size is not None and pad_size > len(file_data):
# Pad the content with zeros
file_data = file_data + (b'\x00' * (pad_size - len(file_data)))
print(f"Input file padded to {pad_size} bytes.")
elif pad_size is not None and pad_size < len(file_data):
print("Warning: The specified padding size is smaller than the "
"input file size. No padding applied.")
if args.flash_bootloader:
# Make sure the user is flashing a bootloader bitstream
bitstream_prefix = find_json_object(file_data[:4096])
if (
(bitstream_prefix is None) or
("type" not in bitstream_prefix) or
(bitstream_prefix["type"] != "bldr")
):
print("Warning: Target bitstream is not a Bootloader.")
print("Please use this option only to flash an official "
"Bootloader bitstream from Signaloid. Visit "
"https://github.com/signaloid/C0-microSD-Hardware "
"to get the latest Bootloader bitstream")
print("Aborting.")
exit(os.EX_USAGE)
if not confirm_action():
print("Aborting.")
exit(os.EX_USAGE)
print("Flashing bootloader bitstream...")
toolkit.flash_and_verify(
file_data, toolkit.BOOTLOADER_BITSTREAM_OFFSET,
MAX_FLASH_ATTEMPTS,
unlock_bootloader=True
)
elif args.flash_signaloid_soc:
# Make sure the user is flashing an soc bitstream
bitstream_prefix = find_json_object(file_data[:4096])
if (
(bitstream_prefix is None) or
("type" not in bitstream_prefix) or
(bitstream_prefix["type"] != "soc")
):
print("Warning: Target bitstream is not a Signaloid SoC.")
print("Please use this option only to flash an official "
"Signaloid SoC bitstream from Signaloid. Visit "
"https://github.com/signaloid/C0-microSD-Hardware "
"to get the latest Signaloid SoC bitstream")
print("Aborting.")
exit(os.EX_USAGE)
if not confirm_action():
print("Aborting.")
exit(os.EX_USAGE)
print("Flashing Signaloid SoC bitstream...")
toolkit.flash_and_verify(
file_data,
toolkit.SOC_BITSTREAM_OFFSET,
MAX_FLASH_ATTEMPTS,
unlock_bootloader=True
)
elif args.flash_user_data:
print("Flashing user data bitstream...")
toolkit.flash_and_verify(
file_data,
toolkit.USER_DATA_OFFSET,
MAX_FLASH_ATTEMPTS,
unlock_bootloader=False
)
elif args.flash_user_data_auto:
toolkit.get_status()
if toolkit.configuration == "soc":
print("Switching to bootloader mode...")
toolkit.switch_boot_config()
input("Please reboot the C0-microSD and press enter.")
print("Flashing user data bitstream...")
toolkit.flash_and_verify(
file_data,
toolkit.USER_DATA_OFFSET,
MAX_FLASH_ATTEMPTS,
unlock_bootloader=False
)
print("Switching to Signaloid SoC mode...")
toolkit.switch_boot_config()
input("Please reboot the C0-microSD and press enter.")
else:
print("Flashing custom user bitstream...")
toolkit.flash_and_verify(
file_data, toolkit.USER_BITSTREAM_OFFSET, MAX_FLASH_ATTEMPTS
)
print("Done.")
except Exception as e:
print(f"{e}\nAn error occurred, aborting.", file=sys.stderr)
if isinstance(e, ValueError):
exit(os.EX_DATAERR)
elif isinstance(e, FileNotFoundError):
exit(os.EX_NOINPUT)
elif isinstance(e, PermissionError):
exit(os.EX_NOPERM)
else:
exit(os.EX_SOFTWARE)
if __name__ == "__main__":
main()