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