/ RNode_Firmware_CE_G2 / partition_hashes
partition_hashes
 1  #!/usr/bin/env python3
 2  
 3  # Copyright (C) 2024, Mark Qvist
 4  
 5  # This program is free software: you can redistribute it and/or modify
 6  # it under the terms of the GNU General Public License as published by
 7  # the Free Software Foundation, either version 3 of the License, or
 8  # (at your option) any later version.
 9  
10  # This program is distributed in the hope that it will be useful,
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  # GNU General Public License for more details.
14  
15  # You should have received a copy of the GNU General Public License
16  # along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  
18  import os
19  import sys
20  import RNS
21  import json
22  import hashlib
23  import subprocess
24  
25  major_version = None
26  minor_version = None
27  target_version = None
28  
29  target_file = os.path.join(sys.argv[1])
30  
31  if sys.argv[1] == "from_device":
32      from_device = True
33  else:
34      from_device = False
35  
36  if not from_device:
37      if not os.path.isfile(target_file):
38          sys.stderr.write(
39              "partition_hashes: firmware binary not found:\n  %s\n"
40              "Build first (e.g. make firmware-heltec32_v3). "
41              "Arduino names the file after the sketch: RNode_Firmware_CE_G2.ino.bin\n" % (target_file,)
42          )
43          sys.exit(1)
44      firmware_data = open(target_file, "rb").read()
45      calc_hash = hashlib.sha256(firmware_data[0:-32]).digest()
46      part_hash = firmware_data[-32:]
47  
48      if calc_hash == part_hash:
49          print(RNS.hexrep(part_hash, delimit=False))
50  
51  else:
52      try:
53          cmdresult = subprocess.run(["rnodeconf", sys.argv[2], "-L"], stdout=subprocess.PIPE).stdout.decode('utf-8')
54          part_hash = cmdresult.split("The actual firmware hash is: ")[1].replace("\n", "")
55          print(part_hash)
56      except Exception as e:
57          print("Could not get partition hash from device: "+str(e))