ia64/xen-unstable

view tools/misc/device-path @ 19506:3fecb8f43617

tools: Add device-path command to convert SBDF into device path.

'SBDF' format is "[SEG#:]BUS#:DEV#.FUNC#"
ex) 0000:0a:1f.3
Device path format is "HID[:UID]-DEV#.FUNC#[-DEV#.FUNC#[...]]"
ex) PNP0A08:0-2.0-0.0

The command can be executed as follows.

# device_path 0a:1f.3
PNP0A08:0-2.0-0.0

Signed-off-by: Yuji Shimada <shimada-yxb@necst.nec.co.jp>
author Keir Fraser <keir.fraser@citrix.com>
date Mon Apr 06 13:52:56 2009 +0100 (2009-04-06)
parents
children
line source
1 #!/usr/bin/env python
2 # -*- mode: python; -*-
3 #============================================================================
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of version 2.1 of the GNU Lesser General Public
6 # License as published by the Free Software Foundation.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 #============================================================================
17 # Copyright (c) 2009, NEC Corporation.
18 #============================================================================
19 # This script converts SBDF into device path.
20 # 'SBDF' format is "[SEG#:]BUS#:DEV#.FUNC#"
21 # ex) 0000:0a:1f.3
22 # Device path format is "HID[:UID]-DEV#.FUNC#[-DEV#.FUNC#[...]]"
23 # ex) PNP0A08:0-2.0-0.0
24 #============================================================================
26 import sys
27 import os
29 # add fallback path for non-native python path installs if needed
30 sys.path.append('/usr/lib/python')
31 sys.path.append('/usr/lib64/python')
32 from xen.util.pci import *
34 SYSFS_ACPI_DEVS_PATH = '/firmware/acpi/namespace/ACPI/_SB'
36 def find_hid_uid(dom, b, d, f):
37 sb_path = find_sysfs_mnt() + SYSFS_ACPI_DEVS_PATH
38 obj_list = os.listdir(sb_path)
39 for obj in obj_list:
40 obj_path = sb_path + '/' + obj.strip() + '/'
41 if os.path.exists(obj_path + 'seg') and \
42 os.path.exists(obj_path + 'bbn'):
43 seg = open(obj_path + 'seg').read()
44 bbn = open(obj_path + 'bbn').read()
45 if int(seg) == dom and int(bbn) == b:
46 hid = open(obj_path + 'hid').read()
47 if os.path.exists(obj_path + 'uid') is False:
48 path_str = hid.strip()
49 else:
50 uid = open(obj_path + 'uid').read()
51 path_str = hid.strip() + ':' + uid.strip()
52 return path_str
53 return None
55 def make_device_path(dom, b, d, f):
56 dev = PciDevice(dom, b, d, f)
57 parent = dev.find_parent()
58 if parent is None:
59 path_str = find_hid_uid(dom, b, d, f)
60 path_str = path_str + '-' + hex(d).replace('0x', '') + '.' + \
61 hex(f).replace('0x', '')
62 return path_str
63 (pdom, pb, pd, pf) = parent
64 path_str = make_device_path(pdom, pb, pd, pf)
65 path_str = path_str + '-' + hex(d).replace('0x', '') + '.' + \
66 hex(f).replace('0x', '')
67 return path_str
69 # main
70 if len(sys.argv) <> 2:
71 print 'Usage: device-path SBDF\n'
72 else:
73 path = os.environ['PATH']
74 os.environ['PATH'] = path + ':/sbin' + ':/user/sbin'
75 sbdf = sys.argv[1]
76 (dom, b, d, f) = parse_pci_name(sbdf)
77 path_str = make_device_path(dom, b, d, f)
78 print path_str