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
18
19import sys
20import dbus
21import argparse
22import subprocess
23
24
25INV_DBUS_NAME = 'org.openbmc.Inventory'
26INV_INTF_NAME = 'org.openbmc.InventoryItem'
27NET_DBUS_NAME = 'org.openbmc.NetworkManager'
28NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface'
29CHS_DBUS_NAME = 'org.openbmc.control.Chassis'
30CHS_OBJ_NAME = '/org/openbmc/control/chassis0'
31PROP_INTF_NAME = 'org.freedesktop.DBus.Properties'
32
33FRUS = {}
34
35# IEEE 802 MAC address mask for locally administered.
36# This means the admin has set the MAC and is no longer
37# the unique number set by the device manufacturer.
38MAC_LOCALLY_ADMIN_MASK = 0x20000000000
39
40
41# Get the inventory dbus path based on the requested fru
42def get_inv_obj_path(fru_type, fru_name):
43    obj_path = ''
44    for f in FRUS.keys():
45        import obmc.inventory
46        if (FRUS[f]['fru_type'] == fru_type and f.endswith(fru_name)):
47            obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
48    return obj_path
49
50
51# Get the inventory property value
52def get_inv_value(obj, prop_name):
53    value = ''
54    dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME)
55    value = dbus_method(INV_INTF_NAME, prop_name)
56    return value
57
58
59# Get the value of the mac on the system without ':' separators
60def get_sys_mac(obj):
61    sys_mac = ''
62    dbus_method = obj.get_dbus_method("GetHwAddress", NET_DBUS_NAME)
63    sys_mac = dbus_method("eth0")
64    sys_mac = sys_mac.replace(":", "")
65    return sys_mac
66
67
68# Replace the value of the system mac with the value of the inventory
69# MAC if the system MAC is not locally administered because this means
70# the system admin has purposely set the MAC
71def sync_mac(obj, inv_mac, sys_mac):
72    # Convert sys MAC to int to perform bitwise '&'
73    int_sys_mac = int(sys_mac, 16)
74    if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK:
75        # Sys MAC is not locally administered, go replace it with inv value
76        # Add the ':' separators
77        mac_str = ':'.join([inv_mac[i]+inv_mac[i+1] for i in range(0, 12, 2)])
78        # The Set HW Method already has checking for mac format
79        dbus_method = obj.get_dbus_method("SetHwAddress", NET_DBUS_NAME)
80        dbus_method("eth0", mac_str)
81
82
83# Get sys uuid
84def get_sys_uuid(obj):
85    sys_uuid = ''
86    dbus_method = obj.get_dbus_method("Get", PROP_INTF_NAME)
87    sys_uuid = dbus_method(CHS_DBUS_NAME, "uuid")
88    return sys_uuid
89
90
91# Set sys uuid, this reboots the BMC for the value to take effect
92def set_sys_uuid(uuid):
93    rc = subprocess.call(["fw_setenv", "uuid", uuid])
94    if rc == 0:
95        print "Rebooting BMC to set uuid"
96        # TODO Uncomment once sync from u-boot to /etc/machine-id is in place
97        # Issue openbmc/openbmc#479
98        # rc = subprocess.call(["reboot"])
99    else:
100        print "Error setting uuid"
101
102if __name__ == '__main__':
103    arg = argparse.ArgumentParser()
104    arg.add_argument('-t')
105    arg.add_argument('-n')
106    arg.add_argument('-p')
107    arg.add_argument('-s')
108
109    opt = arg.parse_args()
110    fru_type = opt.t
111    fru_name = opt.n
112    prop_name = opt.p
113    sync_type = opt.s
114
115    inventory = os.path.join(
116        sys.prefix, 'share', 'inventory', 'inventory.json')
117    if os.path.exists(inventory):
118        import json
119        with open(inventory, 'r') as f:
120            try:
121                inv = json.load(f)
122            except ValueError:
123                print "Invalid JSON detected in " + inventory
124            else:
125                FRUS = inv
126    else:
127        import obmc_system_config as System
128        FRUS = System.FRU_INSTANCES
129
130    bus = dbus.SystemBus()
131    inv_obj_path = get_inv_obj_path(fru_type, fru_name)
132    inv_obj = bus.get_object(INV_DBUS_NAME, inv_obj_path)
133    net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME)
134    chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME)
135
136    # Get the value of the requested inventory property
137    inv_value = get_inv_value(inv_obj, prop_name)
138
139    if sync_type == "mac":
140        sys_mac = get_sys_mac(net_obj)
141        if inv_value != sys_mac:
142            sync_mac(net_obj, inv_value, sys_mac)
143    elif sync_type == "uuid":
144        sys_uuid = get_sys_uuid(chs_obj)
145        if inv_value != sys_uuid:
146            set_sys_uuid(inv_value)
147
148# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
149