Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 1 | #!/usr/bin/python -u |
| 2 | |
| 3 | import gobject |
| 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
| 7 | import Openbmc |
| 8 | import shutil |
| 9 | import tarfile |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 10 | import os |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 11 | |
| 12 | DBUS_NAME = 'org.openbmc.control.BmcFlash' |
| 13 | OBJ_NAME = '/org/openbmc/control/flash/bmc' |
| 14 | FLASH_INTF = 'org.openbmc.Flash' |
| 15 | DOWNLOAD_INTF = 'org.openbmc.managers.Download' |
| 16 | |
| 17 | UPDATE_PATH = '/run/initramfs' |
| 18 | |
| 19 | |
| 20 | def doExtract(members,files): |
| 21 | for tarinfo in members: |
| 22 | if files.has_key(tarinfo.name) == True: |
| 23 | yield tarinfo |
| 24 | |
| 25 | |
| 26 | class 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 James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 37 | self.Set(DBUS_NAME,"update_kernel_and_apps",False) |
| 38 | self.Set(DBUS_NAME,"clear_persistent_files",False) |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 39 | |
| 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 James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 74 | if (self.Get(DBUS_NAME,"update_kernel_and_apps") == False): |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 75 | 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 James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 82 | copy_files["image-rwfs"] = True |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 83 | |
| 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 James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 105 | |
| 106 | if (self.Get(DBUS_NAME,"clear_persistent_files") == False): |
| 107 | print "Removing persistent files" |
| 108 | os.unlink(UPDATE_PATH+"/whitelist") |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 109 | if (self.Get(DBUS_NAME,"preserve_network_settings") == True): |
Norman James | 8a65a54 | 2016-02-03 19:04:44 -0600 | [diff] [blame] | 110 | print "Preserving network settings" |
Norman James | 3d6a06f | 2016-01-30 13:03:45 -0600 | [diff] [blame] | 111 | 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 | |
| 122 | if __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 | |