blob: 2c1329d10c5dbd3cec302502519234625b842be3 [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
7import Openbmc
8import shutil
9import tarfile
Norman James8a65a542016-02-03 19:04:44 -060010import os
Norman James3d6a06f2016-01-30 13:03:45 -060011
12DBUS_NAME = 'org.openbmc.control.BmcFlash'
13OBJ_NAME = '/org/openbmc/control/flash/bmc'
14FLASH_INTF = 'org.openbmc.Flash'
15DOWNLOAD_INTF = 'org.openbmc.managers.Download'
16
17UPDATE_PATH = '/run/initramfs'
18
19
20def doExtract(members,files):
21 for tarinfo in members:
22 if files.has_key(tarinfo.name) == True:
23 yield tarinfo
24
25
26class BmcFlashControl(Openbmc.DbusProperties,Openbmc.DbusObjectManager):
27 def __init__(self,bus,name):
28 self.dbus_objects = { }
29 Openbmc.DbusProperties.__init__(self)
30 Openbmc.DbusObjectManager.__init__(self)
31 dbus.service.Object.__init__(self,bus,name)
32
33 self.Set(DBUS_NAME,"status","Idle")
34 self.Set(DBUS_NAME,"filename","")
35 self.Set(DBUS_NAME,"preserve_network_settings",False)
36 self.Set(DBUS_NAME,"restore_application_defaults",False)
Norman James8a65a542016-02-03 19:04:44 -060037 self.Set(DBUS_NAME,"update_kernel_and_apps",False)
38 self.Set(DBUS_NAME,"clear_persistent_files",False)
Norman James3d6a06f2016-01-30 13:03:45 -060039
40 bus.add_signal_receiver(self.download_error_handler,signal_name = "DownloadError")
41 bus.add_signal_receiver(self.download_complete_handler,signal_name = "DownloadComplete")
42
43 self.InterfacesAdded(name,self.properties)
44
45
46 @dbus.service.method(DBUS_NAME,
47 in_signature='ss', out_signature='')
48 def updateViaTftp(self,ip,filename):
49 self.TftpDownload(ip,filename)
50 self.Set(DBUS_NAME,"status","Downloading")
51
52 @dbus.service.signal(DOWNLOAD_INTF,signature='ss')
53 def TftpDownload(self,ip,filename):
54 self.Set(DBUS_NAME,"filename",filename)
55 pass
56
57
58 ## Signal handler
59 def download_error_handler(self,filename):
60 if (filename == self.Get(DBUS_NAME,"filename")):
61 self.Set(DBUS_NAME,"status","Download Error")
62
63 def download_complete_handler(self,outfile,filename):
64 ## do update
65 if (filename != self.Get(DBUS_NAME,"filename")):
66 return
67
68 print "Download complete. Updating..."
69
70 self.Set(DBUS_NAME,"status","Download Complete")
71 copy_files = {}
72
73 ## determine needed files
Norman James8a65a542016-02-03 19:04:44 -060074 if (self.Get(DBUS_NAME,"update_kernel_and_apps") == False):
Norman James3d6a06f2016-01-30 13:03:45 -060075 copy_files["image-bmc"] = True
76 else:
77 copy_files["image-kernel"] = True
78 copy_files["image-initramfs"] = True
79 copy_files["image-rofs"] = True
80
81 if (self.Get(DBUS_NAME,"restore_application_defaults") == True):
Norman James8a65a542016-02-03 19:04:44 -060082 copy_files["image-rwfs"] = True
Norman James3d6a06f2016-01-30 13:03:45 -060083
84
85 ## make sure files exist in archive
86 try:
87 tar = tarfile.open(outfile,"r")
88 files = {}
89 for f in tar.getnames():
90 files[f] = True
91 tar.close()
92 for f in copy_files.keys():
93 if (files.has_key(f) == False):
94 raise Exception("ERROR: File not found in update archive: "+f)
95
96 except Exception as e:
97 print e
98 self.Set(DBUS_NAME,"status","Update Error")
99 return
100
101 try:
102 tar = tarfile.open(outfile,"r")
103 tar.extractall(UPDATE_PATH,members=doExtract(tar,copy_files))
104 tar.close()
Norman James8a65a542016-02-03 19:04:44 -0600105
106 if (self.Get(DBUS_NAME,"clear_persistent_files") == False):
107 print "Removing persistent files"
108 os.unlink(UPDATE_PATH+"/whitelist")
Norman James3d6a06f2016-01-30 13:03:45 -0600109 if (self.Get(DBUS_NAME,"preserve_network_settings") == True):
Norman James8a65a542016-02-03 19:04:44 -0600110 print "Preserving network settings"
Norman James3d6a06f2016-01-30 13:03:45 -0600111 shutil.copy2("/dev/mtd2",UPDATE_PATH+"image-u-boot-env")
112
113 except Exception as e:
114 print e
115 self.Set(DBUS_NAME,"status","Update Error")
116
117
118
119 self.Set(DBUS_NAME,"status","Update Success. Please reboot.")
120
121
122if __name__ == '__main__':
123 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
124
125 bus = Openbmc.getDBus()
126 name = dbus.service.BusName(DBUS_NAME, bus)
127 obj = BmcFlashControl(bus, OBJ_NAME)
128 mainloop = gobject.MainLoop()
129
130 print "Running Bmc Flash Control"
131 mainloop.run()
132