4 The point of this small utility is to take a file in the format
5 of /lib/modules/`uname -r`/modules.pcimap and /usr/share/hwdata/pcitable
6 and output a condensed, more easily used format for module detection. This is
7 done by first getting a list of all the built modules, then loading the
8 pci ids for each of those modules from modules.pcimap, then finally merging
9 in the contents of pcitable (for built modules only). The result should be
10 a file with a pretty comprehensive mapping of pci ids to module names.
12 The output is used by the PlanetLab boot cd (3.0+) and the pl_hwinit script
13 to load all the applicable modules by scanning lspci output.
17 Expected format of file modules.dep includes lines of:
19 /full/module/path/mod.ko: <dependencies>
21 Expected format of file modules.pcimap includes lines of:
23 # pci_module vendor device subvendor subdevice class class_mask driver_data
24 cciss 0x00000e11 0x0000b060 0x00000e11 0x00004070 0x00000000 0x00000000 0x0
25 cciss 0x00000e11 0x0000b178 0x00000e11 0x00004070 0x00000000 0x00000000 0x0
27 Expected format of file pcitable includes lines of:
29 # ("%d\t%d\t%s\t"%s"\n", vendid, devid, moduleName, cardDescription)
30 # or ("%d\t%d\t%d\t%d\t%s\t"%s"\n", vendid, devid, subvendid, subdevid, moduleNa
31 # me, cardDescription)
32 0x0e11 0x0508 "tmspci" "Compaq|Netelligent 4/16 Token Ring"
33 0x1000 0x0407 0x8086 0x0532 "megaraid" "Storage RAID Controller SRCU42X"
35 Lines listing a module name of ignore or unknown from pcitable are skipped
39 Output format, for each line that matches the above lines:
40 cciss 0e11:b060 0e11:b178
42 If no pci ids are found for a particular module:
53 print( "rewrite-pcitable.py <modules.dep> <modules.pcimap> " \
54 "<pcitable> [<output>]" )
63 # open modules.dep (for list of built modules)
64 modulesdep_file_name= sys.argv[1]
66 modulesdep_file= file(modulesdep_file_name,"r")
68 sys.stderr.write( "Unable to open modules.dep: %s\n" % modulesdep_file_name )
73 pcimap_file_name= sys.argv[2]
75 pcimap_file= file(pcimap_file_name,"r")
77 sys.stderr.write( "Unable to open modules.pcimap: %s\n" % pcimap_file_name )
82 pcitable_file_name= sys.argv[3]
84 pcitable_file= file(pcitable_file_name,"r")
86 sys.stderr.write( "Unable to open pcitable: %s\n" % pcitable_file_name )
90 # finally, if we are supposed to output to a file, open that too
92 output_file_name= sys.argv[4]
94 output_file= file(output_file_name,"w")
96 sys.stderr.write( "Unable to open %s for writing.\n" % output_file )
99 output_file= sys.stdout
102 # associative array to store all matches of module -> ['vendor:device',..]
107 # first step, create an associative array of all the built modules
108 for line in modulesdep_file:
109 parts= string.split(line,":")
113 full_mod_path= parts[0]
114 parts= string.split(full_mod_path,"/")
115 module_name= parts[len(parts)-1]
116 module_name_len= len(module_name)
117 if module_name[module_name_len-3:] == ".ko":
118 all_modules[module_name[:-3]]= []
121 # now, parse the pcimap and add devices
123 for line in pcimap_file:
126 # skip blank lines, or lines that begin with # (comments)
127 line= string.strip(line)
134 line_parts= string.split(line)
135 if line_parts is None or len(line_parts) != 8:
136 sys.stderr.write( "Skipping line %d in pcimap (incorrect format)\n" %
140 # first two parts are always vendor / device id
141 module= line_parts[0]
142 vendor_id= line_parts[1]
143 device_id= line_parts[2]
146 # valid vendor and devices are 10 chars (0xXXXXXXXX) and begin with 0x
147 if len(vendor_id) != 10 or len(device_id) != 10:
148 sys.stderr.write( "Skipping line %d in pcimap (invalid vendor/device " \
149 "id length)\n" % line_num )
152 if string.lower(vendor_id[:2]) != "0x" \
153 or string.lower(device_id[:2]) != "0x":
154 sys.stderr.write( "Skipping line %d in pcimap (invalid vendor/device " \
155 "id format)\n" % line_num )
158 # cut down the ids, only need last 4 bytes
159 # start at 6 = (10 total chars - 4 last chars need)
160 vendor_id= string.lower(vendor_id[6:])
161 device_id= string.lower(device_id[6:])
163 full_id= "%s:%s" % (vendor_id, device_id)
165 if all_modules.has_key(module):
166 if full_id not in all_modules[module]:
167 all_modules[module].append( full_id )
169 # normally shouldn't get here, as the list is prepopulated with all
171 all_modules[module]= [full_id,]
174 # parse pcitable, add any more ids for the devices
176 for line in pcitable_file:
179 # skip blank lines, or lines that begin with # (comments)
180 line= string.strip(line)
187 line_parts= string.split(line)
188 if line_parts is None or len(line_parts) < 4:
189 sys.stderr.write( "Skipping line %d in pcitable (incorrect format)\n" %
193 # vendor id is always the first field, device the second. also,
194 # strip off first two chars (the 0x)
195 vendor_id= string.lower(line_parts[0][2:])
196 device_id= string.lower(line_parts[1][2:])
198 full_id= "%s:%s" % (vendor_id, device_id)
200 # if the first char of the third field is a double quote, the third field
201 # is a module, else if the first char of the third field is a 0 (zero),
202 # the fifth field is the module name.
203 # it would nice if there was any easy way to split a string on spaces,
204 # but recognize quoted strings, so they wouldn't be split up. that is
205 # the reason for this wierd check
206 if line_parts[2][0] == '"':
207 module= line_parts[2]
208 elif line_parts[2][0] == '0':
209 module= line_parts[4]
211 sys.stderr.write( "Skipping line %d in pcitable (incorrect format)\n" %
215 # remove the first and last char of module (quote marks)
217 module= module[:len(module)-1]
219 # now add it if we don't already have this module -> id mapping
220 if all_modules.has_key(module):
221 if full_id not in all_modules[module]:
222 all_modules[module].append( full_id )
224 # don't add any modules from pcitable that we don't already know about
228 for module in all_modules.keys():
229 devices= all_modules[module]
231 devices_str= string.join( all_modules[module], " " )
232 output_file.write( "%s %s\n" % (module,devices_str) )
236 pcitable_file.close()
238 modulesdep_file.close()