blob: 5f03b96e7e9b518fdef4d12464254aece56e7238 [file] [log] [blame]
Norman James3d6a06f2016-01-30 13:03:45 -06001#!/usr/bin/python -u
2
3import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
Milton Miller0c8c5d42016-06-29 17:41:36 -05007import subprocess
8import tempfile
Norman James3d6a06f2016-01-30 13:03:45 -06009import shutil
10import tarfile
Norman James8a65a542016-02-03 19:04:44 -060011import os
Brad Bishop84e73b52016-05-12 15:57:52 -040012from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Norman James3d6a06f2016-01-30 13:03:45 -060013
14DBUS_NAME = 'org.openbmc.control.BmcFlash'
15OBJ_NAME = '/org/openbmc/control/flash/bmc'
Norman James3d6a06f2016-01-30 13:03:45 -060016DOWNLOAD_INTF = 'org.openbmc.managers.Download'
17
Milton Miller0c8c5d42016-06-29 17:41:36 -050018BMC_DBUS_NAME = 'org.openbmc.control.Bmc'
19BMC_OBJ_NAME = '/org/openbmc/control/bmc0'
20
Norman James3d6a06f2016-01-30 13:03:45 -060021UPDATE_PATH = '/run/initramfs'
22
23
24def doExtract(members,files):
25 for tarinfo in members:
26 if files.has_key(tarinfo.name) == True:
27 yield tarinfo
28
29
Brad Bishop84e73b52016-05-12 15:57:52 -040030class BmcFlashControl(DbusProperties,DbusObjectManager):
Norman James3d6a06f2016-01-30 13:03:45 -060031 def __init__(self,bus,name):
32 self.dbus_objects = { }
Brad Bishop84e73b52016-05-12 15:57:52 -040033 DbusProperties.__init__(self)
34 DbusObjectManager.__init__(self)
Norman James3d6a06f2016-01-30 13:03:45 -060035 dbus.service.Object.__init__(self,bus,name)
36
37 self.Set(DBUS_NAME,"status","Idle")
38 self.Set(DBUS_NAME,"filename","")
Milton Millerc8094102016-06-16 13:11:31 -050039 self.Set(DBUS_NAME,"preserve_network_settings",True)
Norman James3d6a06f2016-01-30 13:03:45 -060040 self.Set(DBUS_NAME,"restore_application_defaults",False)
Norman James8a65a542016-02-03 19:04:44 -060041 self.Set(DBUS_NAME,"update_kernel_and_apps",False)
42 self.Set(DBUS_NAME,"clear_persistent_files",False)
Milton Miller0c8c5d42016-06-29 17:41:36 -050043 self.Set(DBUS_NAME,"auto_apply",False)
44
Norman James3d6a06f2016-01-30 13:03:45 -060045 bus.add_signal_receiver(self.download_error_handler,signal_name = "DownloadError")
46 bus.add_signal_receiver(self.download_complete_handler,signal_name = "DownloadComplete")
47
Milton Miller0c8c5d42016-06-29 17:41:36 -050048 self.update_process = None
49 self.progress_name = None
50
Norman James3d6a06f2016-01-30 13:03:45 -060051 self.InterfacesAdded(name,self.properties)
52
53
54 @dbus.service.method(DBUS_NAME,
55 in_signature='ss', out_signature='')
56 def updateViaTftp(self,ip,filename):
Norman James3d6a06f2016-01-30 13:03:45 -060057 self.Set(DBUS_NAME,"status","Downloading")
Milton Miller0c8c5d42016-06-29 17:41:36 -050058 self.TftpDownload(ip,filename)
59
Chris Austenea5b4012016-05-20 16:41:40 -050060 @dbus.service.method(DBUS_NAME,
61 in_signature='s', out_signature='')
62 def update(self,filename):
63 self.Set(DBUS_NAME,"filename",filename)
64 self.download_complete_handler(filename, filename)
65
Norman James3d6a06f2016-01-30 13:03:45 -060066 @dbus.service.signal(DOWNLOAD_INTF,signature='ss')
67 def TftpDownload(self,ip,filename):
68 self.Set(DBUS_NAME,"filename",filename)
69 pass
70
71
72 ## Signal handler
73 def download_error_handler(self,filename):
74 if (filename == self.Get(DBUS_NAME,"filename")):
75 self.Set(DBUS_NAME,"status","Download Error")
76
77 def download_complete_handler(self,outfile,filename):
78 ## do update
79 if (filename != self.Get(DBUS_NAME,"filename")):
80 return
81
82 print "Download complete. Updating..."
83
84 self.Set(DBUS_NAME,"status","Download Complete")
85 copy_files = {}
86
87 ## determine needed files
Norman James8a65a542016-02-03 19:04:44 -060088 if (self.Get(DBUS_NAME,"update_kernel_and_apps") == False):
Norman James3d6a06f2016-01-30 13:03:45 -060089 copy_files["image-bmc"] = True
90 else:
91 copy_files["image-kernel"] = True
92 copy_files["image-initramfs"] = True
93 copy_files["image-rofs"] = True
94
95 if (self.Get(DBUS_NAME,"restore_application_defaults") == True):
Norman James8a65a542016-02-03 19:04:44 -060096 copy_files["image-rwfs"] = True
Norman James3d6a06f2016-01-30 13:03:45 -060097
98
99 ## make sure files exist in archive
100 try:
101 tar = tarfile.open(outfile,"r")
102 files = {}
103 for f in tar.getnames():
104 files[f] = True
105 tar.close()
106 for f in copy_files.keys():
107 if (files.has_key(f) == False):
108 raise Exception("ERROR: File not found in update archive: "+f)
109
110 except Exception as e:
111 print e
Milton Miller0c8c5d42016-06-29 17:41:36 -0500112 self.Set(DBUS_NAME,"status","Unpack Error")
Norman James3d6a06f2016-01-30 13:03:45 -0600113 return
114
115 try:
116 tar = tarfile.open(outfile,"r")
117 tar.extractall(UPDATE_PATH,members=doExtract(tar,copy_files))
118 tar.close()
Norman James8a65a542016-02-03 19:04:44 -0600119
Adi Gangidi4d27c1b2016-05-11 18:27:43 -0500120 if (self.Get(DBUS_NAME,"clear_persistent_files") == True):
Norman James8a65a542016-02-03 19:04:44 -0600121 print "Removing persistent files"
Milton Millerb6cfc542016-06-17 11:43:40 -0500122 try:
123 os.unlink(UPDATE_PATH+"/whitelist")
124 except OSError as e:
125 if (e.errno == errno.EISDIR):
126 pass
127 elif (e.errno == errno.ENOENT):
128 pass
129 else:
130 raise
131
132 try:
133 wldir = UPDATE_PATH + "/whitelist.d"
134
135 for file in os.listdir(wldir):
136 os.unlink(os.path.join(wldir,file))
137 except OSError as e:
138 if (e.errno == errno.EISDIR):
139 pass
140 else:
141 raise
142
Norman James3d6a06f2016-01-30 13:03:45 -0600143 if (self.Get(DBUS_NAME,"preserve_network_settings") == True):
Norman James8a65a542016-02-03 19:04:44 -0600144 print "Preserving network settings"
Milton Millerc8094102016-06-16 13:11:31 -0500145 shutil.copy2("/run/fw_env",UPDATE_PATH+"/image-u-boot-env")
Norman James3d6a06f2016-01-30 13:03:45 -0600146
147 except Exception as e:
148 print e
Milton Miller0c8c5d42016-06-29 17:41:36 -0500149 self.Set(DBUS_NAME,"status","Unpack Error")
Norman James3d6a06f2016-01-30 13:03:45 -0600150
Milton Miller0c8c5d42016-06-29 17:41:36 -0500151 self.Verify()
152
153 def Verify(self):
154 self.Set(DBUS_NAME,"status","Checking Image")
155 try:
156 subprocess.check_call([
157 "/run/initramfs/update",
158 "--no-flash",
159 "--no-save-files",
160 "--no-restore-files",
161 "--no-clean-saved-files" ])
162
163 self.Set(DBUS_NAME,"status","Image ready to apply.")
164 if (self.Get(DBUS_NAME,"auto_apply")):
165 self.Apply()
166 except:
167 self.Set(DBUS_NAME,"auto_apply",False)
168 try:
169 subprocess.check_output([
170 "/run/initramfs/update",
171 "--no-flash",
172 "--ignore-mount",
173 "--no-save-files",
174 "--no-restore-files",
175 "--no-clean-saved-files" ],
176 stderr = subprocess.STDOUT)
177 self.Set(DBUS_NAME,"status","Deferred for mounted filesystem. reboot BMC to apply.")
178 except subprocess.CalledProcessError as e:
179 self.Set(DBUS_NAME,"status","Verify error: %s"
180 % e.output)
181 except OSError as e:
182 self.Set(DBUS_NAME,"status","Verify error: problem calling update: %s" % e.strerror)
183
184
185 def Cleanup(self):
186 if self.progress_name:
187 try:
188 os.unlink(self.progress_name)
189 self.progress_name = None
190 except oserror as e:
191 if e.errno == EEXIST:
192 pass
193 raise
194 self.update_process = None
195 self.Set(DBUS_NAME,"status","Idle")
196
197 @dbus.service.method(DBUS_NAME,
198 in_signature='', out_signature='')
199 def Abort(self):
200 if self.update_process:
201 try:
202 self.update_process.kill()
203 except:
204 pass
205 for file in os.listdir(UPDATE_PATH):
206 if file.startswith('image-'):
207 os.unlink(os.path.join(UPDATE_PATH,file))
208
209 self.Cleanup();
210
211 @dbus.service.method(DBUS_NAME,
212 in_signature='', out_signature='s')
213 def GetUpdateProgress(self):
214 msg = ""
215
216 if self.update_process and self.update_process.returncode is None:
217 self.update_process.poll()
218
219 if (self.update_process is None):
220 pass
221 elif (self.update_process.returncode > 0):
222 self.Set(DBUS_NAME,"status","Apply failed")
223 elif (self.update_process.returncode is None):
224 pass
225 else: # (self.update_process.returncode == 0)
226 files = ""
227 for file in os.listdir(UPDATE_PATH):
228 if file.startswith('image-'):
229 files = files + file;
230 if files == "":
231 msg = "Apply Complete. Reboot to take effect."
232 else:
233 msg = "Apply Incomplete, Remaining:" + files
234 self.Set(DBUS_NAME,"status", msg)
235
236 msg = self.Get(DBUS_NAME,"status") + "\n";
237 if self.progress_name:
238 try:
239 prog = open(self.progress_name,'r')
240 for line in prog:
241 # strip off initial sets of xxx\r here
242 # ignore crlf at the end
243 # cr will be -1 if no '\r' is found
244 cr = line.rfind("\r", 0, -2)
245 msg = msg + line[cr + 1: ]
246 except OSError as e:
247 if (e.error == EEXIST):
248 pass
249 raise
250 return msg
251
252 @dbus.service.method(DBUS_NAME,
253 in_signature='', out_signature='')
254 def Apply(self):
255 progress = None
256 self.Set(DBUS_NAME,"status","Writing images to flash")
257 try:
258 progress = tempfile.NamedTemporaryFile(
259 delete = False, prefix="progress." )
260 self.progress_name = progress.name
261 self.update_process = subprocess.Popen([
262 "/run/initramfs/update" ],
263 stdout = progress.file,
264 stderr = subprocess.STDOUT )
265 except Exception as e:
266 try:
267 progress.close()
268 os.unlink(progress.name)
269 self.progress_name = None
270 except:
271 pass
272 raise
273
274 try:
275 progress.close()
276 except:
277 pass
278
279 @dbus.service.method(DBUS_NAME,
280 in_signature='', out_signature='')
281 def PrepareForUpdate(self):
282 subprocess.call([
283 "fw_setenv",
284 "openbmconce",
285 "copy-files-to-ram copy-base-filesystem-to-ram"])
286 self.Set(DBUS_NAME,"status","Switch to update mode in progress")
287 o = bus.get_object(BMC_DBUS_NAME, BMC_OBJ_NAME)
288 intf = dbus.Interface(o, BMC_DBUS_NAME)
289 intf.warmReset()
290
Norman James3d6a06f2016-01-30 13:03:45 -0600291
292if __name__ == '__main__':
293 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
294
Brad Bishop84e73b52016-05-12 15:57:52 -0400295 bus = get_dbus()
Norman James3d6a06f2016-01-30 13:03:45 -0600296 obj = BmcFlashControl(bus, OBJ_NAME)
297 mainloop = gobject.MainLoop()
Brad Bishop70852a32016-06-29 22:58:51 -0400298 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James3d6a06f2016-01-30 13:03:45 -0600299
300 print "Running Bmc Flash Control"
301 mainloop.run()
302