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