forked from HashFast/hashfast-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field_firmware_update.py
executable file
·175 lines (156 loc) · 6.55 KB
/
field_firmware_update.py
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
#! /usr/bin/env python3
# Copyright (c) 2014, HashFast Technologies LLC
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. Neither the name of HashFast Technologies LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL HASHFAST TECHNOLOGIES LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
def parse_args():
parser = argparse.ArgumentParser(description='Update HashFast modules. By default loads the latest release firmware.')
parser.add_argument('-l', '--list', dest='list', action='store_true', help='list firmware')
parser.add_argument('-r', '--release', dest='release', type=str, help='specific release to load')
parser.add_argument('-t', '--bootloader', dest='bootloader', action='store_true', help='installs the latest bootloader')
# debug
parser.add_argument('-d', '--debug', dest='debug', action='store_true', help='load debug firmware')
parser.add_argument('-b', '--build', dest='build', type=str, help='specific build to load')
return parser.parse_args()
if __name__ == '__main__':
# parse args before other imports
args = parse_args()
import sys
from hf.usb import usbctrl
from hf.usb import util
from hf.firmware.update import HF_Updater
def printme(msg):
print(msg)
def main(args):
if args.debug:
print("WARNING - Debug builds are experimental!")
print("HashFast is not responsible for damage resulting from debug builds.")
updater = HF_Updater()
####################
# BOOTLOADER #
####################
if args.bootloader:
# doing a load, wait for a valid HashFast device
dev = util.poll_hf_device()
if dev is util.USB_DEV_DFU:
# found a device that needs a bootloader
print("Bootloader not detected.")
for flash_size in [512, 128]:
try:
dfu = updater.fetch_release_bootloader(flash=flash_size)
updater.load_firmware_dfu(dfu, flash=flash_size)
break
except:
pass
# wait for device to reboot
util.poll_hf_device()
return
elif dev is util.USB_DEV_HF:
# found device in application mode
updater.dev = usbctrl.poll_hf_ctrl_device(printer=printme)
#updater.enumerate_modules()
updater.enter_loader()
# load firmware
updater.dev_loader = usbctrl.poll_hf_ctrl_device_loader(printer=printme)
updater.enumerate_modules_loader()
# get flash size
loader_version = updater.read_version_loader().version
if loader_version >= 4:
flash_size = int(updater.read_flash_size_loader().flash_size / 1024)
else:
flash_size = 512
print("Using flash_size of: {}k".format(flash_size))
# fetch file
hfu = updater.fetch_release_bootloader_update(flash=flash_size)
updater.load_firmware_hfu(hfu, force=False)
updater.enter_app()
####################
# LIST #
####################
elif args.list:
# list releases/builds
if args.debug:
updater.list_debug_builds()
else:
updater.list_release_firmwares()
####################
# FIRMWARE #
####################
else:
# doing a load, wait for a valid HashFast device
dev = util.poll_hf_device()
if dev is util.USB_DEV_DFU:
# found a device that needs a bootloader
print("Bootloader not detected.")
for flash_size in [512, 128]:
try:
# if no release specified, then latest will be loaded
dfu = updater.fetch_release_bootloader(args.release, flash=flash_size)
updater.load_firmware_dfu(dfu, flash=flash_size)
break
except:
pass
# wait for device to reboot
util.poll_hf_device()
elif dev is util.USB_DEV_HF:
# found device in application mode
updater.dev = usbctrl.poll_hf_ctrl_device(printer=printme)
#updater.enumerate_modules()
updater.enter_loader()
# load firmware
if args.debug or args.build:
if args.build:
updater.dev_loader = usbctrl.poll_hf_ctrl_device_loader(printer=printme)
updater.enumerate_modules_loader()
# get flash size
loader_version = updater.read_version_loader().version
if loader_version >= 4:
flash_size = int(updater.read_flash_size_loader().flash_size / 1024)
else:
flash_size = 512
print("Using flash_size of: {}k".format(flash_size))
# fetch file
hfu = updater.fetch_debug_build(args.build, flash=flash_size)
updater.load_firmware_hfu(hfu)
updater.enter_app()
else:
raise HF_UpdateError("You must specify a debug build with --build BUILD")
else:
# if no release specified, then latest will be loaded
updater.dev_loader = usbctrl.poll_hf_ctrl_device_loader(printer=printme)
updater.enumerate_modules_loader()
# get flash size
loader_version = updater.read_version_loader().version
if loader_version >= 4:
flash_size = int(updater.read_flash_size_loader().flash_size / 1024)
else:
flash_size = 512
print("Using flash_size of: {}k".format(flash_size))
# fetch file
# if no release specified, then latest will be loaded
hfu = updater.fetch_release_firmware(args.release, flash=flash_size)
updater.load_firmware_hfu(hfu)
updater.enter_app()
if __name__ == "__main__":
main(args)