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' 35 36FRUS = {} 37 38# IEEE 802 MAC address mask for locally administered. 39# This means the admin has set the MAC and is no longer 40# the unique number set by the device manufacturer. 41MAC_LOCALLY_ADMIN_MASK = 0x20000000000 42 43 44# Get inventory MACAddress value. 45def get_bmc_mac_address(bus, prop): 46 mapper = obmc.mapper.Mapper(bus) 47 48 # Get the inventory subtree, limited 49 # to objects that implement NetworkInterface. 50 for path, info in \ 51 mapper.get_subtree( 52 path=INVENTORY_ROOT, 53 interfaces=[INV_INTF_NAME]).iteritems(): 54 55 # Find a NetworkInterface with 'bmc' in the path. 56 if 'bmc' not in path: 57 continue 58 59 # Only expecting a single service to implement 60 # NetworkInterface. Get the service connection 61 # from the mapper response 62 conn = info.keys()[0] 63 64 # Get the inventory object implementing NetworkInterface. 65 obj = bus.get_object(conn, path) 66 67 # Get the MAC address 68 mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME) 69 return mproxy(INV_INTF_NAME, prop) 70 71 72# Get inventory UUID value. 73def get_uuid(bus, prop): 74 mapper = obmc.mapper.Mapper(bus) 75 76 # Get the inventory subtree, limited 77 # to objects that implement UUID. 78 resp = mapper.get_subtree( 79 path=INVENTORY_ROOT, 80 interfaces=[CHS_INTF_NAME]) 81 82 # Only expecting a single object to implement UUID. 83 try: 84 path, info = resp.items()[0] 85 except IndexError as e: 86 return None 87 88 # Only expecting a single service to implement 89 # UUID. Get the service connection 90 # from the mapper response 91 conn = info.keys()[0] 92 93 # Get the inventory object implementing UUID. 94 obj = bus.get_object(conn, path) 95 96 # Get the uuid 97 mproxy = obj.get_dbus_method('Get', PROP_INTF_NAME) 98 return mproxy(CHS_INTF_NAME, prop) 99 100 101# Get the value of the mac on the system (from u-boot) without ':' separators 102def get_sys_mac(obj): 103 sys_mac = '' 104 try: 105 sys_mac = subprocess.check_output(["fw_printenv", "-n", "ethaddr"]) 106 except: 107 # Handle when mac does not exist in u-boot 108 return sys_mac 109 sys_mac = sys_mac.replace(":", "") 110 return sys_mac 111 112 113# Replace the value of the system mac with the value of the inventory 114# MAC if the system MAC is not locally administered because this means 115# the system admin has purposely set the MAC 116def sync_mac(obj, inv_mac, sys_mac): 117 if sys_mac: 118 # Convert sys MAC to int to perform bitwise '&' 119 int_sys_mac = int(sys_mac, 16) 120 else: 121 # Set mac to 0 for when u-boot mac is not present 122 int_sys_mac = 0 123 if not int_sys_mac & MAC_LOCALLY_ADMIN_MASK: 124 # Sys MAC is not locally administered, go replace it with inv value 125 # Add the ':' separators 126 mac_str = ':'.join([inv_mac[i]+inv_mac[i+1] for i in range(0, 12, 2)]) 127 # The Set HW Method already has checking for mac format 128 dbus_method = obj.get_dbus_method("SetHwAddress", NET_DBUS_NAME) 129 dbus_method("eth0", mac_str) 130 131 132# Set sys uuid, this reboots the BMC for the value to take effect 133def set_sys_uuid(uuid): 134 rc = subprocess.call(["fw_setenv", "uuid", uuid]) 135 if rc == 0: 136 print "Rebooting BMC to set uuid" 137 # TODO Uncomment once sync from u-boot to /etc/machine-id is in place 138 # Issue openbmc/openbmc#479 139 # rc = subprocess.call(["reboot"]) 140 else: 141 print "Error setting uuid" 142 143if __name__ == '__main__': 144 arg = argparse.ArgumentParser() 145 arg.add_argument('-p') 146 arg.add_argument('-s') 147 148 opt = arg.parse_args() 149 prop_name = opt.p 150 sync_type = opt.s 151 152 bus = dbus.SystemBus() 153 if sync_type == "mac": 154 inv_mac = get_bmc_mac_address(bus, prop_name) 155 if inv_mac: 156 net_obj = bus.get_object(NET_DBUS_NAME, NET_OBJ_NAME) 157 sys_mac = get_sys_mac(net_obj) 158 if inv_mac != sys_mac: 159 sync_mac(net_obj, inv_mac, sys_mac) 160 elif sync_type == "uuid": 161 inv_uuid = get_uuid(bus, prop_name) 162 if inv_uuid: 163 inv_uuid = uuid.UUID(inv_uuid) 164 chs_obj = bus.get_object(CHS_DBUS_NAME, CHS_OBJ_NAME) 165 chs_uuid = get_sys_uuid(chs_obj) 166 if inv_uuid != sys_uuid: 167 set_sys_uuid(inv_uuid) 168 169# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 170