-
Notifications
You must be signed in to change notification settings - Fork 2
/
OTA_firmware_upgrade.py
68 lines (57 loc) · 2.43 KB
/
OTA_firmware_upgrade.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Slightly edited from GeeekPi original script
# ar - 16-05-2021
import os
import time
import json
import smbus2
import requests
# How to enter into OTA mode:
#
# Method 1) Setting register in terminal: i2cset -y 1 0x17 50 127 b
# (can be done in terminal by running: python3 SetUpgradeMode.py)
#
# Method 2) Remove all power connections and batteries, and then hold the power button, insert the batteries.
# Define device bus and address, and firmware url.
DEVICE_BUS = 1
DEVICE_ADDR = 0x18 # OTA Firmware Upgrade Mode
UPDATE_URL = "https://api.thekoziolfoundation.com/update"
# Instance of bus.
bus = smbus2.SMBus(DEVICE_BUS)
aReceiveBuf = []
for i in range(0xF0, 0xFC):
aReceiveBuf.append(bus.read_byte_data(DEVICE_ADDR, i))
UID0 = "%08X" % (aReceiveBuf[0x03] << 0o30 | aReceiveBuf[0x02] << 0o20 | aReceiveBuf[0x01] << 0o10 | aReceiveBuf[0x00])
UID1 = "%08X" % (aReceiveBuf[0x07] << 0o30 | aReceiveBuf[0x06] << 0o20 | aReceiveBuf[0x05] << 0o10 | aReceiveBuf[0x04])
UID2 = "%08X" % (aReceiveBuf[0x0B] << 0o30 | aReceiveBuf[0x0A] << 0o20 | aReceiveBuf[0x09] << 0o10 | aReceiveBuf[0x08])
r = requests.post(UPDATE_URL, data={"UID0":UID0, "UID1":UID1, "UID2":UID2})
r = json.loads(r.text)
if r['code'] != 0:
print('Could not get the firmware due to:' + r['reason'])
exit(r['code'])
else:
print('Passed authentication, now downloading the latest firmware ...')
req = requests.get(r['url'])
with open("/tmp/firmware.bin", "wb") as f:
f.write(req.content)
print("Firmware downloaded successfully.")
print("Please keep the UPS+ powered, while the firmware is being upgraded.")
print("Disconnecting power during the upgrade will cause unrecoverable failure of the UPS!")
with open("/tmp/firmware.bin", "rb") as f:
while True:
data = f.read(0x10)
for i in range(len(list(data))):
bus.write_byte_data(0x18, i + 1, data[i])
bus.write_byte_data(0x18, 0x32, 0xFA)
time.sleep(0.1)
print('.', end='',flush=True)
if len(list(data)) == 0:
bus.write_byte_data(0X18, 0x32, 0x00)
print('.', flush=True)
print('Firmware upgrade completed.')
print('Please disconnect all power/batteries and reinsert to start using the new firmware.')
os.system("sudo halt")
while True:
time.sleep(10)
#EOF