blob: d92c3dfe81e3a466730ada7cee4c0faa033949ba [file] [log] [blame]
Adriana Kobylakfb4d2252016-06-29 21:44:30 -05001#!/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
Brad Bishop32057422016-10-05 22:48:39 -040020import os
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050021import dbus
22import argparse
23import subprocess
Brad Bishop3d13c282016-09-21 08:07:47 -040024
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050025
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
Brad Bishop3d13c282016-09-21 08:07:47 -040034FRUS = {}
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050035
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():
Brad Bishop7e5ec462016-09-21 09:09:48 -040046 import obmc.inventory
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050047 if (FRUS[f]['fru_type'] == fru_type and f.endswith(fru_name)):
Brad Bishop7e5ec462016-09-21 09:09:48 -040048 obj_path = f.replace("<inventory_root>", obmc.inventory.INVENTORY_ROOT)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050049 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
Adriana Kobylakf9deaa02016-11-01 12:57:55 -050060# Get the value of the mac on the system (from u-boot) without ':' separators
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050061def get_sys_mac(obj):
62 sys_mac = ''
Adriana Kobylakf9deaa02016-11-01 12:57:55 -050063 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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050068 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):
Adriana Kobylakf9deaa02016-11-01 12:57:55 -050076 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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050082 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
Brad Bishop3d13c282016-09-21 08:07:47 -0400123 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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500138 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)
Brad Bishop53066752016-09-21 08:48:04 -0400155
156# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4