Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import gobject |
| 5 | import dbus |
| 6 | import dbus.service |
| 7 | import dbus.mainloop.glib |
| 8 | import tftpy |
| 9 | |
| 10 | |
| 11 | |
| 12 | DBUS_NAME = 'org.openbmc.managers.Flash' |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 13 | OBJ_NAME = '/org/openbmc/managers/Flash' |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 14 | TFTP_PORT = 69 |
| 15 | DOWNLOAD_DIR = '/tmp' |
| 16 | |
| 17 | class FlashManagerObject(dbus.service.Object): |
| 18 | def __init__(self,bus,name): |
| 19 | self.dbus_objects = { } |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 20 | self.status = { 'bios' : 'OK', 'bmc' : 'OK' } |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 21 | dbus.service.Object.__init__(self,bus,name) |
| 22 | ## load utilized objects |
| 23 | self.dbus_objects = { |
| 24 | 'bios' : { |
| 25 | 'bus_name' : 'org.openbmc.flash.Bios', |
| 26 | 'object_name' : '/org/openbmc/flash/Bios_0', |
| 27 | 'interface_name' : 'org.openbmc.Flash' |
| 28 | }, |
| 29 | 'bmc' : { |
| 30 | 'bus_name' : 'org.openbmc.flash.Bmc', |
| 31 | 'object_name' : '/org/openbmc/flash/Bmc_0', |
| 32 | 'interface_name' : 'org.openbmc.Flash' |
| 33 | } |
| 34 | } |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 35 | bus.add_signal_receiver(self.UpdatedHandler, |
| 36 | dbus_interface = "org.openbmc.Flash", signal_name = "Updated", path_keyword='path') |
| 37 | |
| 38 | |
| 39 | def UpdatedHandler(self,path = None): |
| 40 | print "Flash update finish: "+path |
| 41 | for flash in self.dbus_objects: |
| 42 | if (path == self.dbus_objects[flash]['object_name']): |
| 43 | self.status[flash] = 'OK' |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 44 | |
| 45 | def getInterface(self,name): |
| 46 | o = self.dbus_objects[name] |
| 47 | obj = bus.get_object(o['bus_name'],o['object_name']) |
| 48 | return dbus.Interface(obj,o['interface_name']) |
| 49 | |
| 50 | @dbus.service.method(DBUS_NAME, |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 51 | in_signature='', out_signature='a{ss}') |
| 52 | def getStatus(self): |
| 53 | return self.status |
| 54 | |
| 55 | @dbus.service.method(DBUS_NAME, |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 56 | in_signature='sss', out_signature='') |
| 57 | def updateFromTftp(self,flash,url,filename): |
| 58 | if (self.dbus_objects.has_key(flash) == False): |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 59 | print "ERROR FlashManager: Not a valid flash device: "+flash |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 60 | return |
| 61 | try: |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 62 | ## need to make download async |
| 63 | self.status[flash]="DOWNLOADING" |
| 64 | filename = str(filename) |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 65 | client = tftpy.TftpClient(url, TFTP_PORT) |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 66 | print "Downloading: "+filename+" from "+url |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 67 | outfile = DOWNLOAD_DIR+"/"+filename |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 68 | client.download(filename,outfile) |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 69 | intf = self.getInterface(flash) |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 70 | self.status[flash]="FLASHING" |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 71 | intf.update(outfile) |
| 72 | |
| 73 | except Exception as e: |
Norman James | 068efb3 | 2015-10-06 16:52:28 -0500 | [diff] [blame] | 74 | print "ERROR FlashManager: "+str(e) |
| 75 | self.status="ERROR" |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 76 | |
| 77 | |
| 78 | |
| 79 | if __name__ == '__main__': |
| 80 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 81 | bus = Openbmc.getDBus() |
Norman James | 372b966 | 2015-10-06 07:47:55 -0500 | [diff] [blame] | 82 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 83 | obj = FlashManagerObject(bus, OBJ_NAME) |
| 84 | mainloop = gobject.MainLoop() |
| 85 | |
| 86 | print "Running Flash Manager" |
| 87 | mainloop.run() |
| 88 | |