Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | import sys |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 4 | import dbus |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 5 | import argparse |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 6 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 7 | from dbus.mainloop.glib import DBusGMainLoop |
| 8 | import gobject |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 9 | import json |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 10 | import os |
| 11 | import signal |
| 12 | import time |
| 13 | from subprocess import Popen |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 14 | |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 15 | import obmc_system_config |
| 16 | import obmc.system |
| 17 | |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 18 | descriptors = { |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 19 | 'power': { |
| 20 | 'bus_name': 'org.openbmc.control.Power', |
| 21 | 'object_name': '/org/openbmc/control/power0', |
| 22 | 'interface_name': 'org.openbmc.control.Power' |
| 23 | }, |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 24 | 'chassison': { |
| 25 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 26 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 27 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 28 | 'property': 'RequestedPowerTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 29 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.On', |
| 30 | 'monitor': 'obmc-chassis-poweron@0.target', |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 31 | }, |
| 32 | 'chassisoff': { |
| 33 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 34 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 35 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 36 | 'property': 'RequestedPowerTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 37 | 'value': 'xyz.openbmc_project.State.Chassis.Transition.Off', |
| 38 | 'monitor': 'obmc-chassis-hard-poweroff@0.target', |
Andrew Geissler | 2cbbb67 | 2017-05-17 10:52:49 -0500 | [diff] [blame] | 39 | }, |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 40 | 'poweron': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 41 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 42 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 43 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 44 | 'property': 'RequestedHostTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 45 | 'value': 'xyz.openbmc_project.State.Host.Transition.On', |
| 46 | 'monitor': 'obmc-host-start@0.target', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 47 | }, |
| 48 | 'poweroff': { |
Andrew Geissler | 25b9a85 | 2016-12-19 16:14:09 -0600 | [diff] [blame] | 49 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 50 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 51 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 52 | 'property': 'RequestedHostTransition', |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 53 | 'value': 'xyz.openbmc_project.State.Host.Transition.Off', |
| 54 | 'monitor': 'obmc-host-stop@0.target', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 55 | }, |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 56 | 'bmcstate': { |
| 57 | 'bus_name': 'xyz.openbmc_project.State.BMC', |
| 58 | 'object_name': '/xyz/openbmc_project/state/bmc0', |
| 59 | 'interface_name': 'xyz.openbmc_project.State.BMC', |
| 60 | 'property': 'CurrentBMCState', |
| 61 | }, |
| 62 | 'chassisstate': { |
| 63 | 'bus_name': 'xyz.openbmc_project.State.Chassis', |
| 64 | 'object_name': '/xyz/openbmc_project/state/chassis0', |
| 65 | 'interface_name': 'xyz.openbmc_project.State.Chassis', |
| 66 | 'property': 'CurrentPowerState', |
| 67 | }, |
| 68 | 'hoststate': { |
| 69 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 70 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 71 | 'interface_name': 'xyz.openbmc_project.State.Host', |
| 72 | 'property': 'CurrentHostState', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 73 | }, |
| 74 | 'bootprogress': { |
George Keishing | 3be0995 | 2017-08-18 09:48:03 -0500 | [diff] [blame] | 75 | 'bus_name': 'xyz.openbmc_project.State.Host', |
| 76 | 'object_name': '/xyz/openbmc_project/state/host0', |
| 77 | 'interface_name': 'xyz.openbmc_project.State.Boot.Progress', |
| 78 | 'property': 'BootProgress', |
Andrew Geissler | cae038e | 2016-12-19 15:29:10 -0600 | [diff] [blame] | 79 | }, |
Joel Stanley | a30ab9d | 2018-01-31 13:34:16 +1030 | [diff] [blame] | 80 | 'state' : ['bmcstate', 'chassisstate', 'hoststate'], |
| 81 | 'status' : ['bmcstate', 'chassisstate', 'hoststate'], |
Norman James | b4914ad | 2015-10-26 07:14:29 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 84 | GPIO_DEFS_FILE = '/etc/default/obmc/gpio/gpio_defs.json' |
| 85 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 86 | def run_set_property(dbus_bus, dbus_iface, descriptor, args): |
| 87 | mainloop = gobject.MainLoop() |
| 88 | |
| 89 | iface = descriptor['interface_name'] |
| 90 | prop = descriptor['property'] |
| 91 | |
| 92 | if 'monitor' not in descriptor: |
| 93 | dbus_iface.Set(iface, prop, descriptor['value']) |
| 94 | return True |
| 95 | |
| 96 | def property_listener(job, path, unit, state): |
| 97 | if descriptor['monitor'] != unit: |
| 98 | return |
| 99 | |
| 100 | property_listener.success = (state == 'done') |
| 101 | mainloop.quit() |
| 102 | |
| 103 | property_listener.success = True |
| 104 | |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 105 | if args.wait and args.verbose: |
| 106 | pid = Popen(["/bin/journalctl", "-f", "--no-pager"]).pid |
| 107 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 108 | if args.wait: |
| 109 | sig_match = dbus_bus.add_signal_receiver(property_listener, "JobRemoved") |
| 110 | |
| 111 | dbus_iface.Set(iface, prop, descriptor['value']) |
| 112 | |
| 113 | if args.wait: |
| 114 | mainloop.run() |
| 115 | sig_match.remove() |
| 116 | |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 117 | if args.wait and args.verbose: |
| 118 | # wait some time for the journal output |
| 119 | time.sleep(args.wait_tune) |
| 120 | os.kill(pid, signal.SIGTERM) |
| 121 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 122 | return property_listener.success |
| 123 | |
Andrew Jeffery | cf6a784 | 2017-10-12 14:47:43 +1030 | [diff] [blame] | 124 | def get_dbus_obj(dbus_bus, bus, obj, args): |
| 125 | if not args.wait: |
| 126 | return dbus_bus.get_object(bus, obj) |
| 127 | |
| 128 | mainloop = gobject.MainLoop() |
| 129 | |
| 130 | def property_listener(job, path, unit, state): |
| 131 | if 'obmc-standby.target' == unit: |
| 132 | mainloop.quit() |
| 133 | |
| 134 | sig_match = dbus_bus.add_signal_receiver(property_listener, "JobRemoved") |
| 135 | try: |
| 136 | return dbus_bus.get_object(bus, obj) |
| 137 | except dbus.exceptions.DBusException as e: |
| 138 | if args.verbose: |
| 139 | pid = Popen(["/bin/journalctl", "-f", "--no-pager"]).pid |
| 140 | |
| 141 | mainloop.run() |
| 142 | |
| 143 | if args.verbose: |
| 144 | os.kill(pid, signal.SIGTERM) |
| 145 | finally: |
| 146 | sig_match.remove() |
| 147 | |
| 148 | return dbus_bus.get_object(bus, obj) |
| 149 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 150 | def run_one_command(dbus_bus, descriptor, args): |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 151 | bus = descriptor['bus_name'] |
| 152 | obj = descriptor['object_name'] |
| 153 | iface = descriptor['interface_name'] |
Andrew Jeffery | cf6a784 | 2017-10-12 14:47:43 +1030 | [diff] [blame] | 154 | dbus_obj = get_dbus_obj(dbus_bus, bus, obj, args) |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 155 | result = None |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 156 | |
Andrew Jeffery | 08e149b | 2017-10-12 01:37:07 +1030 | [diff] [blame] | 157 | if 'property' in descriptor: |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 158 | dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties") |
Andrew Jeffery | 08e149b | 2017-10-12 01:37:07 +1030 | [diff] [blame] | 159 | if 'value' in descriptor: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 160 | result = run_set_property(dbus_bus, dbus_iface, descriptor, args) |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 161 | else: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 162 | prop = descriptor['property'] |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 163 | dbus_prop = dbus_iface.Get(iface, prop) |
| 164 | print '{:<20}: {}'.format(prop, str(dbus_prop)) |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 165 | result = True |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 166 | else: |
| 167 | dbus_iface = dbus.Interface(dbus_obj, "org.freedesktop.DBus.Properties") |
| 168 | props = dbus_iface.GetAll(iface) |
| 169 | for p in props: |
Andrew Jeffery | bba82bd | 2017-10-11 22:20:30 +1030 | [diff] [blame] | 170 | print "{} = {}".format(p, str(props[p])) |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 171 | result = True |
Andrew Jeffery | e4dcaef | 2017-10-11 18:08:01 +1030 | [diff] [blame] | 172 | |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 173 | return result |
| 174 | |
| 175 | def run_all_commands(dbus_bus, recipe, args): |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 176 | if isinstance(recipe, dict): |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 177 | return run_one_command(dbus_bus, recipe, args) |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame] | 178 | |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 179 | assert isinstance(recipe, list) |
| 180 | for command in recipe: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 181 | descriptor = descriptors[command] |
| 182 | if not run_one_command(dbus_bus, descriptor, args): |
| 183 | print "Failed to execute command: {}".format(descriptor) |
| 184 | return False |
| 185 | |
| 186 | return True |
Andrew Jeffery | 7b376e7 | 2017-10-11 18:12:05 +1030 | [diff] [blame] | 187 | |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 188 | def gpio_set_value(gpio_name, active_low, asserted): |
| 189 | gpio_id = obmc.system.convertGpio(gpio_name) |
| 190 | gpio_value_path = "/sys/class/gpio/gpio{}/value".format(gpio_id) |
| 191 | |
| 192 | with open(gpio_value_path, 'w') as gpio: |
| 193 | # Inversion behaviour needs to change with the resolution of |
| 194 | # https://github.com/openbmc/openbmc/issues/2489, where properly |
| 195 | # configuring the kernel will allow it to handle the inversion for us. |
| 196 | gpio.write(str(int(asserted ^ active_low))) |
| 197 | |
| 198 | return True |
| 199 | |
| 200 | def gpio_deassert(gpio_name, active_low, args): |
| 201 | # Deal with silly python2 exception handling as outlined in main |
| 202 | if args.verbose: |
| 203 | return gpio_set_value(gpio_name, active_low, False) |
| 204 | |
| 205 | try: |
| 206 | return gpio_set_value(gpio_name, active_low, False) |
| 207 | except IOError as e: |
| 208 | print >> sys.stderr, "Failed to access GPIO {}: {}".format(gpio_name, e.message) |
| 209 | return False |
| 210 | |
| 211 | def run_chassiskill(args): |
| 212 | # We shouldn't be able to invoke run_chassiskill() unless it's been |
| 213 | # explicitly added as a valid command to argparse in main() |
| 214 | assert can_chassiskill() |
| 215 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 216 | data = {} |
| 217 | with open(GPIO_DEFS_FILE, 'r') as json_input: |
| 218 | data = json.load(json_input) |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 219 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 220 | gpios = data['gpio_configs']['power_config']['power_up_outs'] |
| 221 | defs = data['gpio_definitions'] |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 222 | |
| 223 | for gpio in gpios: |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 224 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 225 | definition = filter(lambda g: g['name'] == gpio['name'], defs) |
| 226 | if len(definition) == 0: |
| 227 | print >> sys.stderr, "Missing definition for GPIO {}".format(gpio['name']) |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 228 | continue |
| 229 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 230 | pin = str(definition[0]['pin']) |
| 231 | active_low = not gpio['polarity'] |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 232 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 233 | if not gpio_deassert(pin, active_low, args): |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 234 | return False |
| 235 | |
| 236 | return True |
| 237 | |
| 238 | def can_chassiskill(): |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 239 | |
Matt Spinler | c4390de | 2018-08-08 11:16:40 -0500 | [diff] [blame^] | 240 | try: |
| 241 | with open(GPIO_DEFS_FILE, 'r') as json_input: |
| 242 | data = json.load(json_input) |
| 243 | gpios = data['gpio_configs']['power_config']['power_up_outs'] |
| 244 | return len(gpios) > 0 |
| 245 | except: |
| 246 | pass |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 247 | |
| 248 | return False |
| 249 | |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 250 | def main(): |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 251 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 252 | |
Gunnar Mills | aa4c310 | 2017-10-25 20:50:47 -0500 | [diff] [blame] | 253 | # Conditionally add the `chassiskill` command based on whether the |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 254 | # required GPIO configuration is present in the system description. |
| 255 | if can_chassiskill(): |
| 256 | descriptors['chassiskill'] = None |
| 257 | |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 258 | parser = argparse.ArgumentParser() |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 259 | parser.add_argument('--verbose', '-v', action='store_true', |
| 260 | help="Verbose output") |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 261 | parser.add_argument('--wait', '-w', action='store_true', |
| 262 | help='Block until the state transition succeeds or fails') |
Andrew Jeffery | b43f7ed | 2017-10-12 01:33:59 +1030 | [diff] [blame] | 263 | parser.add_argument('--wait-tune', '-t', nargs='?', default=8, type=float, |
| 264 | # help='Seconds to wait for journal output to complete after receiving DBus signal', |
| 265 | help=argparse.SUPPRESS) |
Andrew Jeffery | c6b5deb | 2017-10-11 22:18:27 +1030 | [diff] [blame] | 266 | parser.add_argument('recipe', choices=sorted(descriptors.keys())) |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 267 | args = parser.parse_args() |
Andrew Geissler | 8f8cbce | 2017-02-20 16:10:26 -0600 | [diff] [blame] | 268 | |
Andrew Jeffery | 1be4fab | 2017-10-16 14:13:53 +1030 | [diff] [blame] | 269 | # This is a special case: directly pull the power, don't do any D-Bus |
| 270 | # related stuff |
| 271 | if args.recipe == "chassiskill": |
| 272 | return run_chassiskill(args) |
| 273 | |
Andrew Jeffery | bdb7435 | 2017-10-11 17:29:48 +1030 | [diff] [blame] | 274 | dbus_bus = dbus.SystemBus() |
Andrew Jeffery | cf6a784 | 2017-10-12 14:47:43 +1030 | [diff] [blame] | 275 | |
| 276 | # The only way to get a sensible backtrace with python 2 without stupid |
| 277 | # hoops is to let the uncaught exception handler do the work for you. |
| 278 | # Catching and binding an exception appears to overwrite the stack trace at |
| 279 | # the point of bind. |
| 280 | # |
| 281 | # So, if we're in verbose mode, don't try to catch the DBus exception. That |
| 282 | # way we can understand where it originated. |
| 283 | if args.verbose: |
| 284 | return run_all_commands(dbus_bus, descriptors[args.recipe], args) |
| 285 | |
| 286 | # Otherwise, we don't care about the traceback. Just catch it and print the |
| 287 | # error message. |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 288 | try: |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 289 | return run_all_commands(dbus_bus, descriptors[args.recipe], args) |
Andrew Jeffery | cf6a784 | 2017-10-12 14:47:43 +1030 | [diff] [blame] | 290 | except dbus.exceptions.DBusException as e: |
| 291 | print >> sys.stderr, "DBus error occurred: {}".format(e.get_dbus_message()) |
Andrew Jeffery | 0b6883d | 2017-10-11 22:08:55 +1030 | [diff] [blame] | 292 | finally: |
| 293 | dbus_bus.close() |
Andrew Jeffery | 44e6d9c | 2017-10-11 16:57:31 +1030 | [diff] [blame] | 294 | |
| 295 | if __name__ == "__main__": |
Andrew Jeffery | de4ed02 | 2017-10-12 01:26:55 +1030 | [diff] [blame] | 296 | sys.exit(0 if main() else 1) |