Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import subprocess |
| 5 | import dbus |
| 6 | import string |
| 7 | import os |
| 8 | import fcntl |
| 9 | import glib |
| 10 | import gobject |
| 11 | import dbus.service |
| 12 | import dbus.mainloop.glib |
| 13 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 14 | DBUS_NAME = "org.openbmc.HostIpmi" |
| 15 | OBJ_NAME = "/org/openbmc/HostIpmi/1" |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 16 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 17 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 18 | def header(seq, netfn, lun, cmd): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 19 | return ("seq: 0x%02x\nnetfn: 0x%02x\n\nlun: 0x%02d\ncmd: 0x%02x\n") % ( |
| 20 | seq, |
| 21 | netfn, |
| 22 | lun, |
| 23 | cmd, |
| 24 | ) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 25 | |
| 26 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 27 | def print_request(seq, netfn, lun, cmd, data): |
| 28 | str = header(seq, netfn, lun, cmd) |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 29 | str += "data: [%s]" % ", ".join(["0x%02x" % x for x in data]) |
| 30 | print(str) |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 31 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 32 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 33 | def print_response(seq, netfn, lun, cmd, cc, data): |
| 34 | str = header(seq, netfn, lun, cmd) |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 35 | str += "cc: 0x%02x\ndata: [%s]" % ( |
| 36 | cc, |
| 37 | ", ".join(["0x%02x" % x for x in data]), |
| 38 | ) |
| 39 | print(str) |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 40 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 41 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 42 | class IpmiDebug(dbus.service.Object): |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 43 | def __init__(self, bus, name): |
| 44 | dbus.service.Object.__init__(self, bus, name) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 45 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 46 | @dbus.service.signal(DBUS_NAME, "yyyyay") |
| 47 | def ReceivedMessage(self, seq, netfn, lun, cmd, data): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 48 | print("IPMI packet from host:") |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 49 | print_request(seq, netfn, lun, cmd, data) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 50 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 51 | @dbus.service.method(DBUS_NAME, "yyyyyay", "x") |
| 52 | def sendMessage(self, seq, netfn, lun, cmd, ccode, data): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 53 | print("IPMI packet sent to host:") |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 54 | print_response(seq, netfn, lun, cmd, ccode, data) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 55 | return 0 |
| 56 | |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 57 | @dbus.service.method(DBUS_NAME) |
| 58 | def setAttention(self): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 59 | print("IPMI SMS_ATN set") |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 60 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 61 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 62 | class ConsoleReader(object): |
| 63 | def __init__(self, ipmi_obj): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 64 | self.buffer = "" |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 65 | self.seq = 0 |
| 66 | self.ipmi_obj = ipmi_obj |
| 67 | flags = fcntl.fcntl(sys.stdin.fileno(), fcntl.F_GETFL) |
| 68 | flags |= os.O_NONBLOCK |
| 69 | fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, flags) |
| 70 | glib.io_add_watch(sys.stdin, glib.IO_IN, self.io_callback) |
| 71 | |
| 72 | def io_callback(self, fd, condition): |
| 73 | chunk = fd.read() |
| 74 | for char in chunk: |
| 75 | self.buffer += char |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 76 | if char == "\n": |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 77 | self.line(self.buffer) |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 78 | self.buffer = "" |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 79 | |
| 80 | return True |
| 81 | |
| 82 | def line(self, data): |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 83 | s = data.split(" ") |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 84 | if len(s) < 2: |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 85 | print("Not enough bytes to form a valid IPMI packet") |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 86 | return |
| 87 | try: |
| 88 | data = [int(c, 16) for c in s] |
| 89 | except ValueError: |
| 90 | return |
| 91 | self.seq += 1 |
Jeremy Kerr | e34da63 | 2015-10-27 11:52:08 +0800 | [diff] [blame] | 92 | self.ipmi_obj.ReceivedMessage(self.seq, data[0], 0, data[1], data[2:]) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 93 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 94 | |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 95 | def main(): |
| 96 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Adriana Kobylak | 2cb2775 | 2015-10-19 16:23:14 -0500 | [diff] [blame] | 97 | bus = dbus.SystemBus() |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 98 | obj = IpmiDebug(bus, OBJ_NAME) |
| 99 | mainloop = gobject.MainLoop() |
| 100 | r = ConsoleReader(obj) |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 101 | |
| 102 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 103 | name = dbus.service.BusName(DBUS_NAME, bus) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 104 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 105 | print( |
| 106 | "Enter IPMI packet as hex values. First three bytes will be used" |
| 107 | "as netfn and cmd.\nlun will be zero." |
| 108 | ) |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 109 | mainloop.run() |
| 110 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 111 | |
Patrick Williams | 75fe8cc | 2022-07-22 16:12:12 -0500 | [diff] [blame] | 112 | if __name__ == "__main__": |
Jeremy Kerr | 0ffd4f8 | 2015-09-14 14:00:57 +0800 | [diff] [blame] | 113 | sys.exit(main()) |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 114 | |
| 115 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |