-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware_upgrade
executable file
·152 lines (136 loc) · 5.73 KB
/
firmware_upgrade
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
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1+
"""A simple fwupd frontend"""
import gi
import wget
import os
gi.require_version('Fwupd', '2.0')
from gi.repository import Fwupd
"""
Global variables
"""
#lvfsURL = os.system("snap get uefi-fw-tools conf.downloaduri")
#if lvfsURL is None:
# lvfsURL = "https://cdn.fwupd.org/downloads/firmware.xml.gz"
lvfsURL = "https://cdn.fwupd.org/downloads/firmware.xml.gz"
firmwPath = "/var/snap/uefi-fw-tools/current/cache/app-info/xmls"
metadata = firmwPath + "/firmware.xml.gz"
metadataSigFile = firmwPath + "/firmware.xml.gz.jcat"
logFile = os.environ['SNAP_COMMON'] + "/log/firmware_upgrade.log"
logError = os.environ['SNAP_COMMON'] + "/log/fw.err"
"""
Error statement logging function
"""
def logerror(err_msg):
logErr = open(logError,'w',1)
logErr.write(err_msg)
"""
Initialize
"""
if os.path.exists(metadata):
os.remove(metadata)
if os.path.exists(metadataSigFile):
os.remove(metadataSigFile)
if os.path.exists(logError):
os.remove(logError)
#need to create
os.makedirs(os.environ['SNAP_COMMON'] + "/log", exist_ok=True)
log = open(logFile, 'w', 1)
try:
log.write("Checking directories...\n")
if not os.path.exists(firmwPath):
os.makedirs(firmwPath)
log.write("Starting...\n")
"""
Get Metadata
"""
log.write("Downloading metadata...\n")
filename = wget.download(lvfsURL,metadata,None)
if filename != metadata:
log.write("Unable to download firmware matadata file\n")
logerror("Unable to download firmware matadata file\n")
exit(1)
filename = wget.download(lvfsURL + ".jcat", metadataSigFile,None)
if filename != metadataSigFile:
log.write("Unable to download firmware signature file\n")
logerror("Unable to download firmware signature file\n")
exit(1)
log.write("Complated downloading metadata...\n")
log.write("Connection to Fwupd client...\n")
client = Fwupd.Client.new()
if client is None:
log.write("Failed to connect to Fwupd client\n")
logerror("Failed to connect to Fwupd client\n")
exit(1)
## Refresh query into the BIOS update and query once.
results = client.update_metadata("lvfs", metadata, metadataSigFile, None)
results = client.get_devices(None)
## Refresh query into the BIOS
log.write("Updating metadata ...\n")
results = client.update_metadata("lvfs", metadata, metadataSigFile, None)
if results is True:
log.write("Updated the metadata successfully...\n")
else:
log.write("Failed to update metadata...\n")
logerror("Failed to update metadata\n")
exit(1)
log.write("Getting devices...\n")
## This will be a Fwupd.Result object
results = client.get_devices(None)
scheduledFlag = None
for r in results:
log.write("*****************\n")
log.write("\tDevice description: " + str(r.get_description()) + "\n")
log.write("\tDevice Name: " + str(r.get_name()) + "\n")
log.write("\tDevice Id: " + str(r.get_id()) + "\n")
## Filter firmware only
if r.get_name() == "System Firmware":
log.write("Get Releases...\n")
res = client.get_upgrades(r.get_id(), None)
for rel in res:
log.write(rel.to_string())
log.write("\tUpdate URI: %s\n" % rel.get_uri())
## Get update uri from releases
if rel.get_uri() is not None:
downloadFile = firmwPath + "/" + "firmware_" + \
str(r.get_id()) + ".cab"
if os.path.exists(downloadFile):
os.remove(downloadFile)
log.write("\tDownloading " + str(rel.get_uri()) + \
"\n")
filename = wget.download(rel.get_uri(), \
downloadFile, None)
if filename != downloadFile:
log.write("ERROR: Unable to download " + \
str(rel.get_uri()) + "\n")
logerror("Unable to download " + \
str(rel.get_uri()) + "\n")
exit(1)
log.write("\tFinished downloading " + str(filename) + "\n")
log.write("\tAttempting to schedule offline update...\n")
## Don't use Fwupd.InstallFlags.OFFLINE, fwupd will create
## symlink for the systemd-system-update-generator
finalresults = client.install(str(r.get_id()), \
str(downloadFile), \
Fwupd.InstallFlags.NONE, \
None)
if finalresults is True:
log.write("\tSuccessfully scheduled!\n")
log.write("\tUpdate Message: " + \
str(r.get_update_message()) + "\n")
log.write("\tUpdate State: " + \
str(r.get_update_state()) + "\n")
scheduledFlag = True
else:
log.write("\tFailed to schedule firmware update\n")
logerror("Failed to schedule firmware update\n")
exit(1)
if scheduledFlag is None:
log.write("No devices can be updated: Nothing to do\n")
logerror("No devices can be updated: Nothing to do\n")
exit(1)
exit(0)
except Exception as e:
log.write("Exception caught : " + str(e) + "\n")
logerror("Unknown error: cannot check or update firmware\n")
exit(1)