1#!/usr/bin/python -u
2#
3# Copyright 2016 IBM Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#   http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14# implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18import sys
19import os
20import dbus
21import uuid
22import argparse
23import subprocess
24import obmc.mapper
25import shutil
26
27INV_INTF_NAME = "xyz.openbmc_project.Inventory.Item.NetworkInterface"
28NET_DBUS_NAME = "org.openbmc.NetworkManager"
29NET_OBJ_NAME = "/org/openbmc/NetworkManager/Interface"
30CHS_DBUS_NAME = "org.openbmc.control.Chassis"
31CHS_INTF_NAME = "xyz.openbmc_project.Common.UUID"
32CHS_OBJ_NAME = "/org/openbmc/control/chassis0"
33PROP_INTF_NAME = "org.freedesktop.DBus.Properties"
34INVENTORY_ROOT = "/xyz/openbmc_project/inventory"
35NETWORK_ROOT = "/xyz/openbmc_project/network"
36ETHERNET_INTF_NAME = "xyz.openbmc_project.Network.EthernetInterface"
37MAC_INTF_NAME = "xyz.openbmc_project.Network.MACAddress"
38
39FRUS = {}
40
41# IEEE 802 MAC address mask for locally administered.
42# This means the admin has set the MAC and is no longer
43# the unique number set by the device manufacturer.
44MAC_LOCALLY_ADMIN_MASK = 0x20000000000
45
46
47# Get inventory MACAddress value.
48def get_bmc_mac_address(bus, prop):
49    mapper = obmc.mapper.Mapper(bus)
50
51    # Get the inventory subtree, limited
52    # to objects that implement NetworkInterface.
53    for path, info in mapper.get_subtree(
54        path=INVENTORY_ROOT, interfaces=[INV_INTF_NAME]
55    ).iteritems():
56
57        # Find a NetworkInterface with 'bmc' in the path.
58        if "bmc" not in path:
59            continue
60
61        # Only expecting a single service to implement
62        # NetworkInterface.  Get the service connection
63        # from the mapper response
64        conn = info.keys()[0]
65
66        # Get the inventory object implementing NetworkInterface.
67        obj = bus.get_object(conn, path)
68
69        # Get the MAC address
70        mproxy = obj.get_dbus_method("Get", PROP_INTF_NAME)
71        return mproxy(INV_INTF_NAME, prop)
72
73
74# Get Network Interface object.
75def get_network_interface_object(bus):
76    mapper = obmc.mapper.Mapper(bus)
77
78    # Get the network subtree, limited
79    # to objects that implements EthernetInterface.
80    for path, info in mapper.get_subtree(
81        path=NETWORK_ROOT, interfaces=[ETHERNET_INTF_NAME]
82    ).iteritems():
83
84        # Find the one which is having physical interface,it may happen
85        # that vlan interface is there and we want the physical
86        # interface here.
87        if path.split("/")[-1].find("_") < 0:
88            service = info.keys()[0]
89            net_obj = bus.get_object(service, path)
90            return net_obj
91
92
93# Get inventory UUID value.
94def get_uuid(bus, prop):
95    mapper = obmc.mapper.Mapper(bus)
96
97    # Get the inventory subtree, limited
98    # to objects that implement UUID.
99    resp = mapper.get_subtree(path=INVENTORY_ROOT, interfaces=[CHS_INTF_NAME])
100
101    # Only expecting a single object to implement UUID.
102    try:
103        path, info = resp.items()[0]
104    except IndexError as e:
105        return None
106
107    # Only expecting a single service to implement
108    # UUID.  Get the service connection
109    # from the mapper response
110    conn = info.keys()[0]
111
112    # Get the inventory object implementing UUID.
113    obj = bus.get_object(conn, path)
114
115    # Get the uuid
116    mproxy = obj.get_dbus_method("Get", PROP_INTF_NAME)
117    return mproxy(CHS_INTF_NAME, prop)
118
119
120# Get the value of the mac on the system (from u-boot) without ':' separators
121def get_sys_mac(obj):
122    sys_mac = ""
123    try:
124        sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"])
125    except Exception:
126        # Handle when mac does not exist in u-boot
127        return sys_mac
128    return sys_mac
129
130
131# Replace the value of the system mac with the value of the inventory
132# MAC if the system MAC is not locally administered because this means
133# the system admin has purposely set the MAC
134def sync_mac(obj, inv_mac, sys_mac):
135    if sys_mac:
136        # Convert sys MAC to int to perform bitwise '&'
137        sys_mac = sys_mac.replace(":", "")
138        int_sys_mac = int(sys_mac, 16)
139    else:
140        # Set mac to 0 for when u-boot mac is not present
141        int_sys_mac = 0
142    if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK:
143        # Sys MAC is not locally administered, go replace it with inv value
144        intf = dbus.Interface(obj, dbus.PROPERTIES_IFACE)
145        intf.Set(MAC_INTF_NAME, "MACAddress", inv_mac)
146
147
148# Set sys uuid, this reboots the BMC for the value to take effect
149def set_sys_uuid(uuid):
150    rc = subprocess.call(["fw_setenv", "uuid", uuid])
151    if rc == 0:
152        print("Rebooting BMC to set uuid")
153        # TODO Uncomment once sync from u-boot to /etc/machine-id is in place
154        # Issue openbmc/openbmc#479
155        # rc = subprocess.call(["reboot"])
156    else:
157        print("Error setting uuid")
158
159
160if __name__ == "__main__":
161    arg = argparse.ArgumentParser()
162    arg.add_argument("-p")
163    arg.add_argument("-s")
164
165    opt = arg.parse_args()
166    prop_name = opt.p
167    sync_type = opt.s
168
169    bus = dbus.SystemBus()
170    if sync_type == "mac":
171        inv_mac = get_bmc_mac_address(bus, prop_name)
172        if not inv_mac:
173            sys.exit(1)
174        net_obj = get_network_interface_object(bus)
175        if not net_obj:
176            print("Unable to get the network object")
177            sys.exit(1)
178        sys_mac = get_sys_mac(net_obj)
179        if inv_mac != sys_mac:
180            print("Inventory MAC=%s,System MAC=%s" % (inv_mac, sys_mac))
181            sync_mac(net_obj, inv_mac, sys_mac)
182    elif sync_type == "uuid":
183        inv_uuid = get_uuid(bus, prop_name)
184        if inv_uuid:
185            inv_uuid = uuid.UUID(inv_uuid)
186            chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME)
187            chs_uuid = get_sys_uuid(chs_obj)
188            if inv_uuid != sys_uuid:
189                set_sys_uuid(inv_uuid)
190
191# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
192