blob: 43a1899c77c69de8802ec623ba58a24b8623322b [file] [log] [blame]
Norman James372b9662015-10-06 07:47:55 -05001#!/usr/bin/env python
2
3import sys
4import gobject
5import dbus
6import dbus.service
7import dbus.mainloop.glib
8import tftpy
9
10
11
12DBUS_NAME = 'org.openbmc.managers.Flash'
Norman James068efb32015-10-06 16:52:28 -050013OBJ_NAME = '/org/openbmc/managers/Flash'
Norman James372b9662015-10-06 07:47:55 -050014TFTP_PORT = 69
15DOWNLOAD_DIR = '/tmp'
16
17class FlashManagerObject(dbus.service.Object):
18 def __init__(self,bus,name):
19 self.dbus_objects = { }
Norman James068efb32015-10-06 16:52:28 -050020 self.status = { 'bios' : 'OK', 'bmc' : 'OK' }
Norman James372b9662015-10-06 07:47:55 -050021 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 James068efb32015-10-06 16:52:28 -050035 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 James372b9662015-10-06 07:47:55 -050044
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 James068efb32015-10-06 16:52:28 -050051 in_signature='', out_signature='a{ss}')
52 def getStatus(self):
53 return self.status
54
55 @dbus.service.method(DBUS_NAME,
Norman James372b9662015-10-06 07:47:55 -050056 in_signature='sss', out_signature='')
57 def updateFromTftp(self,flash,url,filename):
58 if (self.dbus_objects.has_key(flash) == False):
Norman James068efb32015-10-06 16:52:28 -050059 print "ERROR FlashManager: Not a valid flash device: "+flash
Norman James372b9662015-10-06 07:47:55 -050060 return
61 try:
Norman James068efb32015-10-06 16:52:28 -050062 ## need to make download async
63 self.status[flash]="DOWNLOADING"
64 filename = str(filename)
Norman James372b9662015-10-06 07:47:55 -050065 client = tftpy.TftpClient(url, TFTP_PORT)
Norman James068efb32015-10-06 16:52:28 -050066 print "Downloading: "+filename+" from "+url
Norman James372b9662015-10-06 07:47:55 -050067 outfile = DOWNLOAD_DIR+"/"+filename
Norman James068efb32015-10-06 16:52:28 -050068 client.download(filename,outfile)
Norman James372b9662015-10-06 07:47:55 -050069 intf = self.getInterface(flash)
Norman James068efb32015-10-06 16:52:28 -050070 self.status[flash]="FLASHING"
Norman James372b9662015-10-06 07:47:55 -050071 intf.update(outfile)
72
73 except Exception as e:
Norman James068efb32015-10-06 16:52:28 -050074 print "ERROR FlashManager: "+str(e)
75 self.status="ERROR"
Norman James372b9662015-10-06 07:47:55 -050076
77
78
79if __name__ == '__main__':
80 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -050081 bus = Openbmc.getDBus()
Norman James372b9662015-10-06 07:47:55 -050082 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