summaryrefslogtreecommitdiffstats
path: root/pyiplobserver/ipl_status_observer.py
blob: 890dd616e0a90b69d38b910810f06b1402a2b49a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env python
#
# Copyright 2018 Raptor Engineering, LLC
# Released under the terms of the GPL v3
#
# Get current istep with
# busctl get-property org.openbmc.status.IPL /org/openbmc/status/IPL org.openbmc.status.IPL current_istep
# Get current status with
# busctl get-property org.openbmc.status.IPL /org/openbmc/status/IPL org.openbmc.status.IPL current_status

import gobject
import dbus
import dbus.service
import dbus.mainloop.glib
import os
import subprocess
import traceback
import obmc.dbuslib.propertycacher as PropertyCacher
from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
import obmc.enums
import obmc_system_config as System
import obmc.mapper.utils
import obmc.inventory
import obmc.system

DBUS_NAME = 'org.openbmc.status.IPL'
OBJ_NAME = u'/org/openbmc/status/IPL'

IPL_FLAG_DIR = '/run/openbmc/'
IPL_FLAG_FILE = 'host@0-ipl-complete'

HOST_STATUS_RUNNING = "xyz.openbmc_project.State.Host.HostState.Running"

IPL_SBE_DONE_ISTEP_MAJOR = 5
IPL_SBE_DONE_ISTEP_MINOR = 2

IPL_HOSTBOOT_START_ISTEP_MAJOR = 6
IPL_HOSTBOOT_START_ISTEP_MINOR = 0

IPL_COMPLETE_ISTEP_MAJOR = 21
IPL_COMPLETE_ISTEP_MINOR = 3

SKIBOOT_RUNNING_ISTEP_MAJOR = 22
SKIBOOT_RUNNING_ISTEP_MINOR = 0

class IPLStatus(DbusProperties, DbusObjectManager):
    def __init__(self, bus, obj_name):
        super(IPLStatus, self).__init__(
            conn=bus,
            object_path=obj_name)
        self.bus = bus
        self.prev_access_fail_count = 0;
        self.activateMonitoring = False
        self.last_status = "";
        self.last_istep = "";

        if not os.path.exists(IPL_FLAG_DIR):
            os.makedirs(IPL_FLAG_DIR)

        host_state_dev = self.bus.get_object("xyz.openbmc_project.State.Host", u'/xyz/openbmc_project/state/host0')
        host_state_iface = dbus.Interface(host_state_dev, 'org.freedesktop.DBus.Properties')
        host_state_value = host_state_iface.Get("xyz.openbmc_project.State.Host", "CurrentHostState")
        if host_state_value is not None:
            if (host_state_value == HOST_STATUS_RUNNING):
                self.activateMonitoring = True

        self.Set(DBUS_NAME, "current_istep", "")
        self.Set(DBUS_NAME, "current_status", "UNKNOWN")

        bus.add_signal_receiver(
            self.HostStatusChangeHandler,
            dbus_interface="org.freedesktop.DBus.Properties",
            signal_name="PropertiesChanged",
            path=u'/xyz/openbmc_project/state/host0')

        gobject.timeout_add(100, self.readIPLStatus)

        print "IPLStatus Init Done"

    # BMC bits OEM1 and OEM2 are the signalling flags
    # OEM1 is unconditionally set at skiboot completion and is only cleared by the BMC IPL monitor
    # OEM2 is used as a persistent state indicator, and is only set and cleared by the BMC IPL monitor
    def getSkibootBootFlags(self):
        result = 0
        try:
            proc = subprocess.Popen(["devmem", "0x1e789060", "8"], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
            (out, err) = proc.communicate()
            result = (int(out, 16) & 12) >> 2
        except:
            pass

        return result

    def setSkibootBootFlags(self, value):
        try:
            proc = subprocess.Popen(["devmem", "0x1e789060", "8"], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
            (out, err) = proc.communicate()
            reg_data = (int(out, 16) & (~12)) | ((value & 3) << 2)
            proc = subprocess.Popen(["devmem", "0x1e789060", "8", str(reg_data)], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
            (out, err) = proc.communicate()
        except:
            print "IPLStatus unable to reset skiboot flags!"
            pass

    def readIPLStatus(self):
        try:
            # Set up defaults
            current_status = "OFFLINE"
            current_istep = ""
            sbe_fetch_failed = False
            hostboot_fetch_failed = False
            sbe_istep_major = 0
            sbe_istep_minor = 0
            hostboot_istep_major = 0
            hostboot_istep_minor = 0
            current_istep_major = 0
            current_istep_minor = 0

            if (self.activateMonitoring == False):
                # Publish offline status
                if (self.last_status != current_status):
                    self.Set(DBUS_NAME, "current_status", current_status)
                    self.IPLStatusChanged(current_status)
                    self.last_status = current_status
                if (self.last_istep != current_istep):
                    self.Set(DBUS_NAME, "current_istep", current_istep)
                    self.IPLStepChanged(current_istep)
                    self.last_istep = current_istep
                self.setSkibootBootFlags(0)

                return True;

            # Get SBE status
            proc = subprocess.Popen(["pdbg", "-b", "kernel", "-p0", "getcfam", "0x2809"], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
            (out, err) = proc.communicate()
            status_data = out.split("=")
            if (len(status_data) > 1):
                try:
                    binary_status_data = bin(int(status_data[1], 16))
                except ValueError:
                    sbe_fetch_failed = True

                if (sbe_fetch_failed == False):
                    try:
                        sbe_istep_major = int(binary_status_data[14:22], 2)
                        sbe_istep_minor = int(binary_status_data[22:28], 2)
                        sbe_istep = str(sbe_istep_major) + "," + str(sbe_istep_minor)
                    except ValueError:
                        sbe_fetch_failed = True
            else:
                sbe_fetch_failed = True

            # Determine if hostboot should be checked for activity
            hostboot_active = False
            if (sbe_fetch_failed):
                # Check if hostboot is running
                hostboot_active = True
            if ((sbe_istep_major == IPL_SBE_DONE_ISTEP_MAJOR) and (sbe_istep_minor == IPL_SBE_DONE_ISTEP_MINOR)):
                # Hostboot should definitely be running
                hostboot_active = True

            # Get hostboot status
            if (hostboot_active == False):
                if (sbe_fetch_failed == False):
                    hostboot_istep_major = sbe_istep_major
                    hostboot_istep_minor = sbe_istep_minor
                    hostboot_istep = sbe_istep
            else:
                proc = subprocess.Popen(["pdbg", "-b", "kernel", "-p0", "getcfam", "0x283c"], stdout=subprocess.PIPE, stderr=open(os.devnull, 'wb'), shell=False)
                (out, err) = proc.communicate()
                status_data = out.split("=")
                if (len(status_data) > 1):
                    try:
                        binary_status_data = bin(int(status_data[1], 16))
                    except ValueError:
                        hostboot_fetch_failed = True

                    if (hostboot_fetch_failed == False):
                        try:
                            hostboot_istep_magic = int(binary_status_data[2:10], 2)
                            hostboot_istep_major = int(binary_status_data[18:26], 2)
                            hostboot_istep_minor = int(binary_status_data[26:34], 2)
                        except ValueError:
                            hostboot_fetch_failed = True
                        if (hostboot_fetch_failed == False):
                            if (hostboot_istep_magic != 170):
                                hostboot_istep_major = 0
                                hostboot_istep_minor = 0
                            hostboot_istep = str(hostboot_istep_major) + "," + str(hostboot_istep_minor)
                else:
                    hostboot_fetch_failed = True

            if ((sbe_fetch_failed == False) and (hostboot_fetch_failed == False)):
                # Figure out system status
                current_istep_major = sbe_istep_major
                current_istep_minor = sbe_istep_minor
                if (hostboot_istep_major > current_istep_major):
                    current_istep_major = hostboot_istep_major
                if (hostboot_istep_minor > current_istep_minor):
                    current_istep_minor = hostboot_istep_minor
                current_istep = str(current_istep_major) + "," + str(current_istep_minor)

                # Asseble status strings
                current_status = "OFFLINE"
                if ((current_istep_major > 0) or (current_istep_minor > 0 )):
                    current_status = "IPL_RUNNING"
                if ((current_istep_major == IPL_COMPLETE_ISTEP_MAJOR) and (current_istep_minor == IPL_COMPLETE_ISTEP_MINOR)):
                    if (self.getSkibootBootFlags() == 0):
                        current_status = "IPL_RUNNING"
                        current_istep_major = SKIBOOT_RUNNING_ISTEP_MAJOR
                        current_istep_minor = SKIBOOT_RUNNING_ISTEP_MINOR
                        current_istep = str(current_istep_major) + "," + str(current_istep_minor)
                    else:
                        current_status = "IPL_COMPLETE"

                # Reset failure counter
                self.prev_access_fail_count = 0
            else:
                # Increment failure counter
                if (self.prev_access_fail_count < 255):
                    self.prev_access_fail_count += 1

            if (self.last_status != current_status):
                if (current_status == "IPL_COMPLETE"):
                    # Set skiboot completion flag
                    self.setSkibootBootFlags(2)

                    # Signal systemd that IPL is complete
                    try:
                        with open(IPL_FLAG_DIR + IPL_FLAG_FILE, 'a'):
                            os.utime(IPL_FLAG_DIR + IPL_FLAG_FILE, None)

                        systemd_emit_dev = self.bus.get_object("org.freedesktop.systemd1", u"/org/freedesktop/systemd1")
                        systemd_emit_iface = dbus.Interface(systemd_emit_dev, 'org.freedesktop.systemd1.Manager')
                        systemd_emit_function = getattr(systemd_emit_iface, 'StartUnit')
                        systemd_emit_function.call_async('obmc-host-ipl-complete@0.target', 'replace')
                    except Exception as e:
                        pass
                else:
                    try:
                        os.remove(IPL_FLAG_DIR + IPL_FLAG_FILE)
                    except:
                        pass
                    if (current_istep_major < IPL_COMPLETE_ISTEP_MAJOR):
                        self.setSkibootBootFlags(0)

            # Since it is extremely unlikely that both fetches will fail unless the host is offline, simply check that both fetches passed or failed before continuing
            if (sbe_fetch_failed == hostboot_fetch_failed):
                # Check error results multiple times before signalling unexpected offline status
                if ((current_status == "OFFLINE") and (self.prev_access_fail_count < 10)):
                    return True

                # Publish status
                if (self.last_status != current_status):
                    self.Set(DBUS_NAME, "current_status", current_status)
                    self.IPLStatusChanged(current_status)
                    self.last_status = current_status
                if (self.last_istep != current_istep):
                    self.Set(DBUS_NAME, "current_istep", current_istep)
                    self.IPLStepChanged(current_istep)
                    self.last_istep = current_istep

            return True
        except Exception as e:
            print(e)
            traceback.print_exc()
            return True

    def HostStatusChangeHandler(self, interface_name, changed_properties,
                                 invalidated_properties):
        value = changed_properties.get('CurrentHostState')
        if value is not None:
            if (value == HOST_STATUS_RUNNING):
                self.activateMonitoring = True
            else:
                self.setSkibootBootFlags(0)
                self.activateMonitoring = False

    @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
    def getCurrentIPLStatus(self):
        return self.Get(DBUS_NAME, "current_status")

    @dbus.service.signal(DBUS_NAME)
    def IPLStatusChanged(self, message):
        # Signal is emitted on method return
        pass

    @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
    def getCurrentIPLIStep(self):
        return self.Get(DBUS_NAME, "current_istep")

    @dbus.service.signal(DBUS_NAME)
    def IPLStepChanged(self, message):
        # Signal is emitted on method return
        pass

    def NewObjectHandler(self, obj_path, iprops, bus_name=None):
        current_istep = self.Get(DBUS_NAME, "current_istep")

        if obj_path in System.EXIT_STATE_DEPEND[current_istep]:
            print "New object: "+obj_path+" ("+bus_name+")"

if __name__ == '__main__':
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = get_dbus()
    obj = IPLStatus(bus, OBJ_NAME)
    mainloop = gobject.MainLoop()
    obj.unmask_signals()
    name = dbus.service.BusName(DBUS_NAME, bus)

    print "Running IPLStatus"
    mainloop.run()

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
OpenPOWER on IntegriCloud