blob: 5fcca42fc6d5a6b2541adac472f39a77ac7c0332 [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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050018import sys
Brad Bishop32057422016-10-05 22:48:39 -040019import os
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050020import dbus
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050021import uuid
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050022import argparse
23import subprocess
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050024import obmc.mapper
25import shutil
Brad Bishop3d13c282016-09-21 08:07:47 -040026
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050027INV_INTF_NAME = 'xyz.openbmc_project.Inventory.Item.NetworkInterface'
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050028NET_DBUS_NAME = 'org.openbmc.NetworkManager'
29NET_OBJ_NAME = '/org/openbmc/NetworkManager/Interface'
30CHS_DBUS_NAME = 'org.openbmc.control.Chassis'
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050031CHS_INTF_NAME = 'xyz.openbmc_project.Common.UUID'
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050032CHS_OBJ_NAME = '/org/openbmc/control/chassis0'
33PROP_INTF_NAME = 'org.freedesktop.DBus.Properties'
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050034INVENTORY_ROOT = '/xyz/openbmc_project/inventory'
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050035
Brad Bishop3d13c282016-09-21 08:07:47 -040036FRUS = {}
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050037
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
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050044# 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)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050070
71
Dinesh Chinaric47c6a62017-03-29 23:09:59 -050072# 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)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -050099
100
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500101# Get the value of the mac on the system (from u-boot) without ':' separators
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500102def get_sys_mac(obj):
103 sys_mac = ''
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500104 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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500109 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):
Adriana Kobylakf9deaa02016-11-01 12:57:55 -0500117 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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500123 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
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500132# 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()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500145 arg.add_argument('-p')
146 arg.add_argument('-s')
147
148 opt = arg.parse_args()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500149 prop_name = opt.p
150 sync_type = opt.s
151
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500152 bus = dbus.SystemBus()
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500153 if sync_type == "mac":
Dinesh Chinaric47c6a62017-03-29 23:09:59 -0500154 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)
Adriana Kobylakfb4d2252016-06-29 21:44:30 -0500160 elif sync_type == "uuid":
Dinesh Chinaric47c6a62017-03-29 23:09:59 -0500161 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)
Brad Bishop53066752016-09-21 08:48:04 -0400168
169# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4