This commit was generated by cvs2svn to compensate for changes in r525,
[bootcd.git] / scripts / rewrite-pcitable.py
1 #!/usr/bin/env python
2
3 """
4 The point of this small utility is to take a file in the format
5 of /lib/modules/`uname -r`/modules.pcimap and output a condensed, more
6 easily used format for module detection
7
8 The output is used by the PlanetLab boot cd (3.0+) and the pl_hwinit script
9 to load all the applicable modules by scanning lspci output.
10
11 Excepted format of file includes lines of:
12
13 # pci_module vendor device subvendor subdevice class class_mask driver_data
14 cciss 0x00000e11 0x0000b060 0x00000e11 0x00004070 0x00000000 0x00000000 0x0
15 cciss 0x00000e11 0x0000b178 0x00000e11 0x00004070 0x00000000 0x00000000 0x0
16
17 Output format, for each line that matches the above lines:
18 cciss 0e11:b060 0e11:b178
19 """
20
21 import os, sys
22 import string
23
24
25 def usage():
26     print( "Usage:" )
27     print( "rewrite-pcitable.py <pcitable> [<output>]" )
28     print( "" )
29
30
31 if len(sys.argv) < 2:
32     usage()
33     sys.exit(1)
34
35
36 pcitable_file_name= sys.argv[1]
37 try:
38     pcitable_file= file(pcitable_file_name,"r")
39 except IOError:
40     sys.stderr.write( "Unable to open: %s\n" % pcitable_file_name )
41     sys.exit(1)
42
43 if len(sys.argv) > 2:
44     output_file_name= sys.argv[2]
45     try:
46         output_file= file(output_file_name,"w")
47     except IOError:
48         sys.stderr.write( "Unable to open %s for writing.\n" % output_file )
49         sys.exit(1)
50 else:
51     output_file= sys.stdout
52
53
54 line_num= 0
55
56 # associative array to store all matches of module -> ['vendor:device',..]
57 # entries
58 all_modules= {}
59
60 for line in pcitable_file:
61     line_num= line_num+1
62     
63     # skip blank lines, or lines that begin with # (comments)
64     line= string.strip(line)
65     if len(line) == 0:
66         continue
67     
68     if line[0] == "#":
69         continue
70
71     line_parts= string.split(line)
72     if line_parts is None or len(line_parts) != 8:
73         sys.stderr.write( "Skipping line %d (incorrect format)\n" % line_num )
74         continue
75
76     # first two parts are always vendor / device id
77     module= line_parts[0]
78     vendor_id= line_parts[1]
79     device_id= line_parts[2]
80     
81
82     # valid vendor and devices are 10 chars (0xXXXXXXXX) and begin with 0x
83     if len(vendor_id) != 10 or len(device_id) != 10:
84         sys.stderr.write( "Skipping line %d (invalid vendor/device id length)\n"
85                           % line_num )
86         continue
87
88     if string.lower(vendor_id[:2]) != "0x" \
89            or string.lower(device_id[:2]) != "0x":
90         sys.stderr.write( "Skipping line %d (invalid vendor/device id format)\n"
91                           % line_num )
92         continue
93
94     # cut down the ids, only need last 4 bytes
95     # start at 6 = (10 total chars - 4 last chars need)
96     vendor_id= string.lower(vendor_id[6:])
97     device_id= string.lower(device_id[6:])
98
99     full_id= "%s:%s" % (vendor_id, device_id)
100
101     if all_modules.has_key(module):
102         all_modules[module].append( full_id )
103     else:
104         all_modules[module]= [full_id,]
105
106 for module in all_modules.keys():
107     devices= string.join( all_modules[module], " " )
108     output_file.write( "%s %s\n" % (module,devices) )
109
110     
111 output_file.close()
112 pcitable_file.close()