Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 2 | |
Gunnar Mills | 1902990 | 2017-10-06 14:47:10 -0500 | [diff] [blame] | 3 | import os |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 4 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 5 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 6 | try: # python 2 |
| 7 | import gobject |
| 8 | except ImportError: # python 3 |
| 9 | from gi.repository import GObject as gobject |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 10 | import dbus |
| 11 | import dbus.service |
| 12 | import dbus.mainloop.glib |
Norman James | 0b73e7d | 2016-01-19 14:03:50 -0600 | [diff] [blame] | 13 | import subprocess |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 14 | from obmc.dbuslib.bindings import get_dbus |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 15 | |
| 16 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 17 | FLASH_DOWNLOAD_PATH = "/tmp" |
| 18 | DBUS_NAME = "org.openbmc.managers.Download" |
| 19 | OBJ_NAME = "/org/openbmc/managers/Download" |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 20 | TFTP_PORT = 69 |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 21 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 22 | |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 23 | class DownloadManagerObject(dbus.service.Object): |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 24 | def __init__(self, bus, name): |
| 25 | dbus.service.Object.__init__(self, bus, name) |
| 26 | bus.add_signal_receiver( |
| 27 | self.DownloadHandler, |
| 28 | dbus_interface="org.openbmc.Flash", |
| 29 | signal_name="Download", |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 30 | path_keyword="path", |
| 31 | ) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 32 | bus.add_signal_receiver( |
| 33 | self.TftpDownloadHandler, |
| 34 | signal_name="TftpDownload", |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 35 | path_keyword="path", |
| 36 | ) |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 37 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 38 | @dbus.service.signal(DBUS_NAME, signature="ss") |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 39 | def DownloadComplete(self, outfile, filename): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 40 | print("Download Complete: " + outfile) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 41 | return outfile |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 42 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 43 | @dbus.service.signal(DBUS_NAME, signature="s") |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 44 | def DownloadError(self, filename): |
| 45 | pass |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 46 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 47 | def TftpDownloadHandler(self, ip, filename, path=None): |
| 48 | try: |
| 49 | filename = str(filename) |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 50 | print("Downloading: " + filename + " from " + ip) |
| 51 | outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 52 | rc = subprocess.call( |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 53 | ["tftp", "-l", outfile, "-r", filename, "-g", ip] |
| 54 | ) |
| 55 | if rc == 0: |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 56 | self.DownloadComplete(outfile, filename) |
| 57 | else: |
| 58 | self.DownloadError(filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 59 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 60 | except Exception as e: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 61 | print("ERROR DownloadManager: " + str(e)) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 62 | self.DownloadError(filename) |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 63 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 64 | # TODO: this needs to be deprecated. |
| 65 | # Shouldn't call flash interface from here |
| 66 | def DownloadHandler(self, url, filename, path=None): |
| 67 | try: |
| 68 | filename = str(filename) |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 69 | print("Downloading: " + filename + " from " + url) |
| 70 | outfile = FLASH_DOWNLOAD_PATH + "/" + os.path.basename(filename) |
| 71 | subprocess.call(["tftp", "-l", outfile, "-r", filename, "-g", url]) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 72 | obj = bus.get_object("org.openbmc.control.Flash", path) |
| 73 | intf = dbus.Interface(obj, "org.openbmc.Flash") |
| 74 | intf.update(outfile) |
Norman James | 8585b21 | 2016-01-29 08:08:30 -0600 | [diff] [blame] | 75 | |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 76 | except Exception as e: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 77 | print("ERROR DownloadManager: " + str(e)) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 78 | obj = bus.get_object("org.openbmc.control.Flash", path) |
| 79 | intf = dbus.Interface(obj, "org.openbmc.Flash") |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 80 | intf.error("Download Error: " + filename) |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 81 | |
| 82 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 83 | if __name__ == "__main__": |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 84 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 85 | bus = get_dbus() |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 86 | obj = DownloadManagerObject(bus, OBJ_NAME) |
| 87 | mainloop = gobject.MainLoop() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 88 | name = dbus.service.BusName(DBUS_NAME, bus) |
Brad Bishop | f5110e1 | 2016-08-30 19:28:45 -0400 | [diff] [blame] | 89 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 90 | print("Running Download Manager") |
Norman James | f066e87 | 2015-10-07 15:29:51 -0500 | [diff] [blame] | 91 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 92 | |
| 93 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |