• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Simple code to turn various tables in an ELF file into alias definitions.
2  * This deals with kernel datastructures where they should be
3  * dealt with: in the kernel source.
4  *
5  * Copyright 2002-2003  Rusty Russell, IBM Corporation
6  *           2003       Kai Germaschewski
7  *
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  */
12 
13 #include "modpost.h"
14 #include "devicetable-offsets.h"
15 
16 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
17  * use either stdint.h or inttypes.h for the rest. */
18 #if KERNEL_ELFCLASS == ELFCLASS32
19 typedef Elf32_Addr	kernel_ulong_t;
20 #define BITS_PER_LONG 32
21 #else
22 typedef Elf64_Addr	kernel_ulong_t;
23 #define BITS_PER_LONG 64
24 #endif
25 #ifdef __sun__
26 #include <inttypes.h>
27 #else
28 #include <stdint.h>
29 #endif
30 
31 #include <ctype.h>
32 #include <stdbool.h>
33 
34 typedef uint32_t	__u32;
35 typedef uint16_t	__u16;
36 typedef unsigned char	__u8;
37 
38 /* Big exception to the "don't include kernel headers into userspace, which
39  * even potentially has different endianness and word sizes, since
40  * we handle those differences explicitly below */
41 #include "../../include/linux/mod_devicetable.h"
42 
43 /* This array collects all instances that use the generic do_table */
44 struct devtable {
45 	const char *device_id; /* name of table, __mod_<name>_device_table. */
46 	unsigned long id_size;
47 	void *function;
48 };
49 
50 #define ___cat(a,b) a ## b
51 #define __cat(a,b) ___cat(a,b)
52 
53 /* we need some special handling for this host tool running eventually on
54  * Darwin. The Mach-O section handling is a bit different than ELF section
55  * handling. The differnces in detail are:
56  *  a) we have segments which have sections
57  *  b) we need a API call to get the respective section symbols */
58 #if defined(__MACH__)
59 #include <mach-o/getsect.h>
60 
61 #define INIT_SECTION(name)  do {					\
62 		unsigned long name ## _len;				\
63 		char *__cat(pstart_,name) = getsectdata("__TEXT",	\
64 			#name, &__cat(name,_len));			\
65 		char *__cat(pstop_,name) = __cat(pstart_,name) +	\
66 			__cat(name, _len);				\
67 		__cat(__start_,name) = (void *)__cat(pstart_,name);	\
68 		__cat(__stop_,name) = (void *)__cat(pstop_,name);	\
69 	} while (0)
70 #define SECTION(name)   __attribute__((section("__TEXT, " #name)))
71 
72 struct devtable **__start___devtable, **__stop___devtable;
73 #else
74 #define INIT_SECTION(name) /* no-op for ELF */
75 #define SECTION(name)   __attribute__((section(#name)))
76 
77 /* We construct a table of pointers in an ELF section (pointers generally
78  * go unpadded by gcc).  ld creates boundary syms for us. */
79 extern struct devtable *__start___devtable[], *__stop___devtable[];
80 #endif /* __MACH__ */
81 
82 #if __GNUC__ == 3 && __GNUC_MINOR__ < 3
83 # define __used			__attribute__((__unused__))
84 #else
85 # define __used			__attribute__((__used__))
86 #endif
87 
88 /* Define a variable f that holds the value of field f of struct devid
89  * based at address m.
90  */
91 #define DEF_FIELD(m, devid, f) \
92 	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
93 /* Define a variable f that holds the address of field f of struct devid
94  * based at address m.  Due to the way typeof works, for a field of type
95  * T[N] the variable has type T(*)[N], _not_ T*.
96  */
97 #define DEF_FIELD_ADDR(m, devid, f) \
98 	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
99 
100 /* Add a table entry.  We test function type matches while we're here. */
101 #define ADD_TO_DEVTABLE(device_id, type, function) \
102 	static struct devtable __cat(devtable,__LINE__) = {	\
103 		device_id + 0*sizeof((function)((const char *)NULL,	\
104 						(void *)NULL,		\
105 						(char *)NULL)),		\
106 		SIZE_##type, (function) };				\
107 	static struct devtable *SECTION(__devtable) __used \
108 		__cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
109 
110 #define ADD(str, sep, cond, field)                              \
111 do {                                                            \
112         strcat(str, sep);                                       \
113         if (cond)                                               \
114                 sprintf(str + strlen(str),                      \
115                         sizeof(field) == 1 ? "%02X" :           \
116                         sizeof(field) == 2 ? "%04X" :           \
117                         sizeof(field) == 4 ? "%08X" : "",       \
118                         field);                                 \
119         else                                                    \
120                 sprintf(str + strlen(str), "*");                \
121 } while(0)
122 
123 /* Always end in a wildcard, for future extension */
add_wildcard(char * str)124 static inline void add_wildcard(char *str)
125 {
126 	int len = strlen(str);
127 
128 	if (str[len - 1] != '*')
129 		strcat(str + len, "*");
130 }
131 
132 /**
133  * Check that sizeof(device_id type) are consistent with size of section
134  * in .o file. If in-consistent then userspace and kernel does not agree
135  * on actual size which is a bug.
136  * Also verify that the final entry in the table is all zeros.
137  * Ignore both checks if build host differ from target host and size differs.
138  **/
device_id_check(const char * modname,const char * device_id,unsigned long size,unsigned long id_size,void * symval)139 static void device_id_check(const char *modname, const char *device_id,
140 			    unsigned long size, unsigned long id_size,
141 			    void *symval)
142 {
143 	int i;
144 
145 	if (size % id_size || size < id_size) {
146 		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
147 		      "of the size of section __mod_%s_device_table=%lu.\n"
148 		      "Fix definition of struct %s_device_id "
149 		      "in mod_devicetable.h\n",
150 		      modname, device_id, id_size, device_id, size, device_id);
151 	}
152 	/* Verify last one is a terminator */
153 	for (i = 0; i < id_size; i++ ) {
154 		if (*(uint8_t*)(symval+size-id_size+i)) {
155 			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
156 				"The last of %lu is:\n",
157 				modname, device_id, id_size, size / id_size);
158 			for (i = 0; i < id_size; i++ )
159 				fprintf(stderr,"0x%02x ",
160 					*(uint8_t*)(symval+size-id_size+i) );
161 			fprintf(stderr,"\n");
162 			fatal("%s: struct %s_device_id is not terminated "
163 				"with a NULL entry!\n", modname, device_id);
164 		}
165 	}
166 }
167 
168 /* USB is special because the bcdDevice can be matched against a numeric range */
169 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
do_usb_entry(void * symval,unsigned int bcdDevice_initial,int bcdDevice_initial_digits,unsigned char range_lo,unsigned char range_hi,unsigned char max,struct module * mod)170 static void do_usb_entry(void *symval,
171 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
172 			 unsigned char range_lo, unsigned char range_hi,
173 			 unsigned char max, struct module *mod)
174 {
175 	char alias[500];
176 	DEF_FIELD(symval, usb_device_id, match_flags);
177 	DEF_FIELD(symval, usb_device_id, idVendor);
178 	DEF_FIELD(symval, usb_device_id, idProduct);
179 	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
180 	DEF_FIELD(symval, usb_device_id, bDeviceClass);
181 	DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
182 	DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
183 	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
184 	DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
185 	DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
186 	DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
187 
188 	strcpy(alias, "usb:");
189 	ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
190 	    idVendor);
191 	ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
192 	    idProduct);
193 
194 	strcat(alias, "d");
195 	if (bcdDevice_initial_digits)
196 		sprintf(alias + strlen(alias), "%0*X",
197 			bcdDevice_initial_digits, bcdDevice_initial);
198 	if (range_lo == range_hi)
199 		sprintf(alias + strlen(alias), "%X", range_lo);
200 	else if (range_lo > 0 || range_hi < max) {
201 		if (range_lo > 0x9 || range_hi < 0xA)
202 			sprintf(alias + strlen(alias),
203 				"[%X-%X]",
204 				range_lo,
205 				range_hi);
206 		else {
207 			sprintf(alias + strlen(alias),
208 				range_lo < 0x9 ? "[%X-9" : "[%X",
209 				range_lo);
210 			sprintf(alias + strlen(alias),
211 				range_hi > 0xA ? "a-%X]" : "%X]",
212 				range_lo);
213 		}
214 	}
215 	if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
216 		strcat(alias, "*");
217 
218 	ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
219 	    bDeviceClass);
220 	ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
221 	    bDeviceSubClass);
222 	ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
223 	    bDeviceProtocol);
224 	ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
225 	    bInterfaceClass);
226 	ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
227 	    bInterfaceSubClass);
228 	ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
229 	    bInterfaceProtocol);
230 	ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
231 	    bInterfaceNumber);
232 
233 	add_wildcard(alias);
234 	buf_printf(&mod->dev_table_buf,
235 		   "MODULE_ALIAS(\"%s\");\n", alias);
236 }
237 
238 /* Handles increment/decrement of BCD formatted integers */
239 /* Returns the previous value, so it works like i++ or i-- */
incbcd(unsigned int * bcd,int inc,unsigned char max,size_t chars)240 static unsigned int incbcd(unsigned int *bcd,
241 			   int inc,
242 			   unsigned char max,
243 			   size_t chars)
244 {
245 	unsigned int init = *bcd, i, j;
246 	unsigned long long c, dec = 0;
247 
248 	/* If bcd is not in BCD format, just increment */
249 	if (max > 0x9) {
250 		*bcd += inc;
251 		return init;
252 	}
253 
254 	/* Convert BCD to Decimal */
255 	for (i=0 ; i < chars ; i++) {
256 		c = (*bcd >> (i << 2)) & 0xf;
257 		c = c > 9 ? 9 : c; /* force to bcd just in case */
258 		for (j=0 ; j < i ; j++)
259 			c = c * 10;
260 		dec += c;
261 	}
262 
263 	/* Do our increment/decrement */
264 	dec += inc;
265 	*bcd  = 0;
266 
267 	/* Convert back to BCD */
268 	for (i=0 ; i < chars ; i++) {
269 		for (c=1,j=0 ; j < i ; j++)
270 			c = c * 10;
271 		c = (dec / c) % 10;
272 		*bcd += c << (i << 2);
273 	}
274 	return init;
275 }
276 
do_usb_entry_multi(void * symval,struct module * mod)277 static void do_usb_entry_multi(void *symval, struct module *mod)
278 {
279 	unsigned int devlo, devhi;
280 	unsigned char chi, clo, max;
281 	int ndigits;
282 
283 	DEF_FIELD(symval, usb_device_id, match_flags);
284 	DEF_FIELD(symval, usb_device_id, idVendor);
285 	DEF_FIELD(symval, usb_device_id, idProduct);
286 	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
287 	DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
288 	DEF_FIELD(symval, usb_device_id, bDeviceClass);
289 	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
290 
291 	devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
292 		bcdDevice_lo : 0x0U;
293 	devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
294 		bcdDevice_hi : ~0x0U;
295 
296 	/* Figure out if this entry is in bcd or hex format */
297 	max = 0x9; /* Default to decimal format */
298 	for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
299 		clo = (devlo >> (ndigits << 2)) & 0xf;
300 		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
301 		if (clo > max || chi > max) {
302 			max = 0xf;
303 			break;
304 		}
305 	}
306 
307 	/*
308 	 * Some modules (visor) have empty slots as placeholder for
309 	 * run-time specification that results in catch-all alias
310 	 */
311 	if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
312 		return;
313 
314 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
315 	for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
316 		clo = devlo & 0xf;
317 		chi = devhi & 0xf;
318 		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
319 			chi = max;
320 		devlo >>= 4;
321 		devhi >>= 4;
322 
323 		if (devlo == devhi || !ndigits) {
324 			do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
325 			break;
326 		}
327 
328 		if (clo > 0x0)
329 			do_usb_entry(symval,
330 				     incbcd(&devlo, 1, max,
331 					    sizeof(bcdDevice_lo) * 2),
332 				     ndigits, clo, max, max, mod);
333 
334 		if (chi < max)
335 			do_usb_entry(symval,
336 				     incbcd(&devhi, -1, max,
337 					    sizeof(bcdDevice_lo) * 2),
338 				     ndigits, 0x0, chi, max, mod);
339 	}
340 }
341 
do_usb_table(void * symval,unsigned long size,struct module * mod)342 static void do_usb_table(void *symval, unsigned long size,
343 			 struct module *mod)
344 {
345 	unsigned int i;
346 	const unsigned long id_size = SIZE_usb_device_id;
347 
348 	device_id_check(mod->name, "usb", size, id_size, symval);
349 
350 	/* Leave last one: it's the terminator. */
351 	size -= id_size;
352 
353 	for (i = 0; i < size; i += id_size)
354 		do_usb_entry_multi(symval + i, mod);
355 }
356 
357 /* Looks like: hid:bNvNpN */
do_hid_entry(const char * filename,void * symval,char * alias)358 static int do_hid_entry(const char *filename,
359 			     void *symval, char *alias)
360 {
361 	DEF_FIELD(symval, hid_device_id, bus);
362 	DEF_FIELD(symval, hid_device_id, group);
363 	DEF_FIELD(symval, hid_device_id, vendor);
364 	DEF_FIELD(symval, hid_device_id, product);
365 
366 	sprintf(alias, "hid:");
367 	ADD(alias, "b", bus != HID_BUS_ANY, bus);
368 	ADD(alias, "g", group != HID_GROUP_ANY, group);
369 	ADD(alias, "v", vendor != HID_ANY_ID, vendor);
370 	ADD(alias, "p", product != HID_ANY_ID, product);
371 
372 	return 1;
373 }
374 ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
375 
376 /* Looks like: ieee1394:venNmoNspNverN */
do_ieee1394_entry(const char * filename,void * symval,char * alias)377 static int do_ieee1394_entry(const char *filename,
378 			     void *symval, char *alias)
379 {
380 	DEF_FIELD(symval, ieee1394_device_id, match_flags);
381 	DEF_FIELD(symval, ieee1394_device_id, vendor_id);
382 	DEF_FIELD(symval, ieee1394_device_id, model_id);
383 	DEF_FIELD(symval, ieee1394_device_id, specifier_id);
384 	DEF_FIELD(symval, ieee1394_device_id, version);
385 
386 	strcpy(alias, "ieee1394:");
387 	ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
388 	    vendor_id);
389 	ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
390 	    model_id);
391 	ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
392 	    specifier_id);
393 	ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
394 	    version);
395 
396 	add_wildcard(alias);
397 	return 1;
398 }
399 ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry);
400 
401 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
do_pci_entry(const char * filename,void * symval,char * alias)402 static int do_pci_entry(const char *filename,
403 			void *symval, char *alias)
404 {
405 	/* Class field can be divided into these three. */
406 	unsigned char baseclass, subclass, interface,
407 		baseclass_mask, subclass_mask, interface_mask;
408 
409 	DEF_FIELD(symval, pci_device_id, vendor);
410 	DEF_FIELD(symval, pci_device_id, device);
411 	DEF_FIELD(symval, pci_device_id, subvendor);
412 	DEF_FIELD(symval, pci_device_id, subdevice);
413 	DEF_FIELD(symval, pci_device_id, class);
414 	DEF_FIELD(symval, pci_device_id, class_mask);
415 
416 	strcpy(alias, "pci:");
417 	ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
418 	ADD(alias, "d", device != PCI_ANY_ID, device);
419 	ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
420 	ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
421 
422 	baseclass = (class) >> 16;
423 	baseclass_mask = (class_mask) >> 16;
424 	subclass = (class) >> 8;
425 	subclass_mask = (class_mask) >> 8;
426 	interface = class;
427 	interface_mask = class_mask;
428 
429 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
430 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
431 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
432 		warn("Can't handle masks in %s:%04X\n",
433 		     filename, class_mask);
434 		return 0;
435 	}
436 
437 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
438 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
439 	ADD(alias, "i", interface_mask == 0xFF, interface);
440 	add_wildcard(alias);
441 	return 1;
442 }
443 ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
444 
445 /* looks like: "ccw:tNmNdtNdmN" */
do_ccw_entry(const char * filename,void * symval,char * alias)446 static int do_ccw_entry(const char *filename,
447 			void *symval, char *alias)
448 {
449 	DEF_FIELD(symval, ccw_device_id, match_flags);
450 	DEF_FIELD(symval, ccw_device_id, cu_type);
451 	DEF_FIELD(symval, ccw_device_id, cu_model);
452 	DEF_FIELD(symval, ccw_device_id, dev_type);
453 	DEF_FIELD(symval, ccw_device_id, dev_model);
454 
455 	strcpy(alias, "ccw:");
456 	ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
457 	    cu_type);
458 	ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
459 	    cu_model);
460 	ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
461 	    dev_type);
462 	ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
463 	    dev_model);
464 	add_wildcard(alias);
465 	return 1;
466 }
467 ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
468 
469 /* looks like: "ap:tN" */
do_ap_entry(const char * filename,void * symval,char * alias)470 static int do_ap_entry(const char *filename,
471 		       void *symval, char *alias)
472 {
473 	DEF_FIELD(symval, ap_device_id, dev_type);
474 
475 	sprintf(alias, "ap:t%02X*", dev_type);
476 	return 1;
477 }
478 ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
479 
480 /* looks like: "css:tN" */
do_css_entry(const char * filename,void * symval,char * alias)481 static int do_css_entry(const char *filename,
482 			void *symval, char *alias)
483 {
484 	DEF_FIELD(symval, css_device_id, type);
485 
486 	sprintf(alias, "css:t%01X", type);
487 	return 1;
488 }
489 ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
490 
491 /* Looks like: "serio:tyNprNidNexN" */
do_serio_entry(const char * filename,void * symval,char * alias)492 static int do_serio_entry(const char *filename,
493 			  void *symval, char *alias)
494 {
495 	DEF_FIELD(symval, serio_device_id, type);
496 	DEF_FIELD(symval, serio_device_id, proto);
497 	DEF_FIELD(symval, serio_device_id, id);
498 	DEF_FIELD(symval, serio_device_id, extra);
499 
500 	strcpy(alias, "serio:");
501 	ADD(alias, "ty", type != SERIO_ANY, type);
502 	ADD(alias, "pr", proto != SERIO_ANY, proto);
503 	ADD(alias, "id", id != SERIO_ANY, id);
504 	ADD(alias, "ex", extra != SERIO_ANY, extra);
505 
506 	add_wildcard(alias);
507 	return 1;
508 }
509 ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
510 
511 /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
do_acpi_entry(const char * filename,void * symval,char * alias)512 static int do_acpi_entry(const char *filename,
513 			void *symval, char *alias)
514 {
515 	DEF_FIELD_ADDR(symval, acpi_device_id, id);
516 	sprintf(alias, "acpi*:%s:*", *id);
517 	return 1;
518 }
519 ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
520 
521 /* looks like: "pnp:dD" */
do_pnp_device_entry(void * symval,unsigned long size,struct module * mod)522 static void do_pnp_device_entry(void *symval, unsigned long size,
523 				struct module *mod)
524 {
525 	const unsigned long id_size = SIZE_pnp_device_id;
526 	const unsigned int count = (size / id_size)-1;
527 	unsigned int i;
528 
529 	device_id_check(mod->name, "pnp", size, id_size, symval);
530 
531 	for (i = 0; i < count; i++) {
532 		DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
533 		char acpi_id[sizeof(*id)];
534 		int j;
535 
536 		buf_printf(&mod->dev_table_buf,
537 			   "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
538 
539 		/* fix broken pnp bus lowercasing */
540 		for (j = 0; j < sizeof(acpi_id); j++)
541 			acpi_id[j] = toupper((*id)[j]);
542 		buf_printf(&mod->dev_table_buf,
543 			   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
544 	}
545 }
546 
547 /* looks like: "pnp:dD" for every device of the card */
do_pnp_card_entries(void * symval,unsigned long size,struct module * mod)548 static void do_pnp_card_entries(void *symval, unsigned long size,
549 				struct module *mod)
550 {
551 	const unsigned long id_size = SIZE_pnp_card_device_id;
552 	const unsigned int count = (size / id_size)-1;
553 	unsigned int i;
554 
555 	device_id_check(mod->name, "pnp", size, id_size, symval);
556 
557 	for (i = 0; i < count; i++) {
558 		unsigned int j;
559 		DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
560 
561 		for (j = 0; j < PNP_MAX_DEVICES; j++) {
562 			const char *id = (char *)(*devs)[j].id;
563 			int i2, j2;
564 			int dup = 0;
565 
566 			if (!id[0])
567 				break;
568 
569 			/* find duplicate, already added value */
570 			for (i2 = 0; i2 < i && !dup; i2++) {
571 				DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
572 
573 				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
574 					const char *id2 = (char *)(*devs)[j2].id;
575 
576 					if (!id2[0])
577 						break;
578 
579 					if (!strcmp(id, id2)) {
580 						dup = 1;
581 						break;
582 					}
583 				}
584 			}
585 
586 			/* add an individual alias for every device entry */
587 			if (!dup) {
588 				char acpi_id[PNP_ID_LEN];
589 				int k;
590 
591 				buf_printf(&mod->dev_table_buf,
592 					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
593 
594 				/* fix broken pnp bus lowercasing */
595 				for (k = 0; k < sizeof(acpi_id); k++)
596 					acpi_id[k] = toupper(id[k]);
597 				buf_printf(&mod->dev_table_buf,
598 					   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
599 			}
600 		}
601 	}
602 }
603 
604 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
do_pcmcia_entry(const char * filename,void * symval,char * alias)605 static int do_pcmcia_entry(const char *filename,
606 			   void *symval, char *alias)
607 {
608 	unsigned int i;
609 	DEF_FIELD(symval, pcmcia_device_id, match_flags);
610 	DEF_FIELD(symval, pcmcia_device_id, manf_id);
611 	DEF_FIELD(symval, pcmcia_device_id, card_id);
612 	DEF_FIELD(symval, pcmcia_device_id, func_id);
613 	DEF_FIELD(symval, pcmcia_device_id, function);
614 	DEF_FIELD(symval, pcmcia_device_id, device_no);
615 	DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
616 
617 	for (i=0; i<4; i++) {
618 		(*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
619 	}
620 
621 	strcpy(alias, "pcmcia:");
622 	ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
623 	    manf_id);
624 	ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
625 	    card_id);
626 	ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
627 	    func_id);
628 	ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
629 	    function);
630 	ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
631 	    device_no);
632 	ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
633 	ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
634 	ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
635 	ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
636 
637 	add_wildcard(alias);
638 	return 1;
639 }
640 ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
641 
do_of_entry(const char * filename,void * symval,char * alias)642 static int do_of_entry (const char *filename, void *symval, char *alias)
643 {
644     int len;
645     char *tmp;
646     DEF_FIELD_ADDR(symval, of_device_id, name);
647     DEF_FIELD_ADDR(symval, of_device_id, type);
648     DEF_FIELD_ADDR(symval, of_device_id, compatible);
649 
650     len = sprintf (alias, "of:N%sT%s",
651                     (*name)[0] ? *name : "*",
652                     (*type)[0] ? *type : "*");
653 
654     if (compatible[0])
655         sprintf (&alias[len], "%sC%s",
656                      (*type)[0] ? "*" : "",
657                      *compatible);
658 
659     /* Replace all whitespace with underscores */
660     for (tmp = alias; tmp && *tmp; tmp++)
661         if (isspace (*tmp))
662             *tmp = '_';
663 
664     add_wildcard(alias);
665     return 1;
666 }
667 ADD_TO_DEVTABLE("of", of_device_id, do_of_entry);
668 
do_vio_entry(const char * filename,void * symval,char * alias)669 static int do_vio_entry(const char *filename, void *symval,
670 		char *alias)
671 {
672 	char *tmp;
673 	DEF_FIELD_ADDR(symval, vio_device_id, type);
674 	DEF_FIELD_ADDR(symval, vio_device_id, compat);
675 
676 	sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
677 			(*compat)[0] ? *compat : "*");
678 
679 	/* Replace all whitespace with underscores */
680 	for (tmp = alias; tmp && *tmp; tmp++)
681 		if (isspace (*tmp))
682 			*tmp = '_';
683 
684 	add_wildcard(alias);
685 	return 1;
686 }
687 ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
688 
689 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
690 
do_input(char * alias,kernel_ulong_t * arr,unsigned int min,unsigned int max)691 static void do_input(char *alias,
692 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
693 {
694 	unsigned int i;
695 
696 	for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
697 		arr[i] = TO_NATIVE(arr[i]);
698 	for (i = min; i < max; i++)
699 		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
700 			sprintf(alias + strlen(alias), "%X,*", i);
701 }
702 
703 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
do_input_entry(const char * filename,void * symval,char * alias)704 static int do_input_entry(const char *filename, void *symval,
705 			  char *alias)
706 {
707 	DEF_FIELD(symval, input_device_id, flags);
708 	DEF_FIELD(symval, input_device_id, bustype);
709 	DEF_FIELD(symval, input_device_id, vendor);
710 	DEF_FIELD(symval, input_device_id, product);
711 	DEF_FIELD(symval, input_device_id, version);
712 	DEF_FIELD_ADDR(symval, input_device_id, evbit);
713 	DEF_FIELD_ADDR(symval, input_device_id, keybit);
714 	DEF_FIELD_ADDR(symval, input_device_id, relbit);
715 	DEF_FIELD_ADDR(symval, input_device_id, absbit);
716 	DEF_FIELD_ADDR(symval, input_device_id, mscbit);
717 	DEF_FIELD_ADDR(symval, input_device_id, ledbit);
718 	DEF_FIELD_ADDR(symval, input_device_id, sndbit);
719 	DEF_FIELD_ADDR(symval, input_device_id, ffbit);
720 	DEF_FIELD_ADDR(symval, input_device_id, swbit);
721 
722 	sprintf(alias, "input:");
723 
724 	ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
725 	ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
726 	ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
727 	ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
728 
729 	sprintf(alias + strlen(alias), "-e*");
730 	if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
731 		do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
732 	sprintf(alias + strlen(alias), "k*");
733 	if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
734 		do_input(alias, *keybit,
735 			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
736 			 INPUT_DEVICE_ID_KEY_MAX);
737 	sprintf(alias + strlen(alias), "r*");
738 	if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
739 		do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
740 	sprintf(alias + strlen(alias), "a*");
741 	if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
742 		do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
743 	sprintf(alias + strlen(alias), "m*");
744 	if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
745 		do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
746 	sprintf(alias + strlen(alias), "l*");
747 	if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
748 		do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
749 	sprintf(alias + strlen(alias), "s*");
750 	if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
751 		do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
752 	sprintf(alias + strlen(alias), "f*");
753 	if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
754 		do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
755 	sprintf(alias + strlen(alias), "w*");
756 	if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
757 		do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
758 	return 1;
759 }
760 ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
761 
do_eisa_entry(const char * filename,void * symval,char * alias)762 static int do_eisa_entry(const char *filename, void *symval,
763 		char *alias)
764 {
765 	DEF_FIELD_ADDR(symval, eisa_device_id, sig);
766 	if (sig[0])
767 		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
768 	else
769 		strcat(alias, "*");
770 	return 1;
771 }
772 ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
773 
774 /* Looks like: parisc:tNhvNrevNsvN */
do_parisc_entry(const char * filename,void * symval,char * alias)775 static int do_parisc_entry(const char *filename, void *symval,
776 		char *alias)
777 {
778 	DEF_FIELD(symval, parisc_device_id, hw_type);
779 	DEF_FIELD(symval, parisc_device_id, hversion);
780 	DEF_FIELD(symval, parisc_device_id, hversion_rev);
781 	DEF_FIELD(symval, parisc_device_id, sversion);
782 
783 	strcpy(alias, "parisc:");
784 	ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
785 	ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
786 	ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
787 	ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
788 
789 	add_wildcard(alias);
790 	return 1;
791 }
792 ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
793 
794 /* Looks like: sdio:cNvNdN. */
do_sdio_entry(const char * filename,void * symval,char * alias)795 static int do_sdio_entry(const char *filename,
796 			void *symval, char *alias)
797 {
798 	DEF_FIELD(symval, sdio_device_id, class);
799 	DEF_FIELD(symval, sdio_device_id, vendor);
800 	DEF_FIELD(symval, sdio_device_id, device);
801 
802 	strcpy(alias, "sdio:");
803 	ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
804 	ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
805 	ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
806 	add_wildcard(alias);
807 	return 1;
808 }
809 ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
810 
811 /* Looks like: ssb:vNidNrevN. */
do_ssb_entry(const char * filename,void * symval,char * alias)812 static int do_ssb_entry(const char *filename,
813 			void *symval, char *alias)
814 {
815 	DEF_FIELD(symval, ssb_device_id, vendor);
816 	DEF_FIELD(symval, ssb_device_id, coreid);
817 	DEF_FIELD(symval, ssb_device_id, revision);
818 
819 	strcpy(alias, "ssb:");
820 	ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
821 	ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
822 	ADD(alias, "rev", revision != SSB_ANY_REV, revision);
823 	add_wildcard(alias);
824 	return 1;
825 }
826 ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
827 
828 /* Looks like: bcma:mNidNrevNclN. */
do_bcma_entry(const char * filename,void * symval,char * alias)829 static int do_bcma_entry(const char *filename,
830 			 void *symval, char *alias)
831 {
832 	DEF_FIELD(symval, bcma_device_id, manuf);
833 	DEF_FIELD(symval, bcma_device_id, id);
834 	DEF_FIELD(symval, bcma_device_id, rev);
835 	DEF_FIELD(symval, bcma_device_id, class);
836 
837 	strcpy(alias, "bcma:");
838 	ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
839 	ADD(alias, "id", id != BCMA_ANY_ID, id);
840 	ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
841 	ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
842 	add_wildcard(alias);
843 	return 1;
844 }
845 ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
846 
847 /* Looks like: virtio:dNvN */
do_virtio_entry(const char * filename,void * symval,char * alias)848 static int do_virtio_entry(const char *filename, void *symval,
849 			   char *alias)
850 {
851 	DEF_FIELD(symval, virtio_device_id, device);
852 	DEF_FIELD(symval, virtio_device_id, vendor);
853 
854 	strcpy(alias, "virtio:");
855 	ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
856 	ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
857 
858 	add_wildcard(alias);
859 	return 1;
860 }
861 ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
862 
863 /*
864  * Looks like: vmbus:guid
865  * Each byte of the guid will be represented by two hex characters
866  * in the name.
867  */
868 
do_vmbus_entry(const char * filename,void * symval,char * alias)869 static int do_vmbus_entry(const char *filename, void *symval,
870 			  char *alias)
871 {
872 	int i;
873 	DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
874 	char guid_name[(sizeof(*guid) + 1) * 2];
875 
876 	for (i = 0; i < (sizeof(*guid) * 2); i += 2)
877 		sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2]));
878 
879 	strcpy(alias, "vmbus:");
880 	strcat(alias, guid_name);
881 
882 	return 1;
883 }
884 ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
885 
886 /* Looks like: i2c:S */
do_i2c_entry(const char * filename,void * symval,char * alias)887 static int do_i2c_entry(const char *filename, void *symval,
888 			char *alias)
889 {
890 	DEF_FIELD_ADDR(symval, i2c_device_id, name);
891 	sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
892 
893 	return 1;
894 }
895 ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
896 
897 /* Looks like: spi:S */
do_spi_entry(const char * filename,void * symval,char * alias)898 static int do_spi_entry(const char *filename, void *symval,
899 			char *alias)
900 {
901 	DEF_FIELD_ADDR(symval, spi_device_id, name);
902 	sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
903 
904 	return 1;
905 }
906 ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
907 
908 static const struct dmifield {
909 	const char *prefix;
910 	int field;
911 } dmi_fields[] = {
912 	{ "bvn", DMI_BIOS_VENDOR },
913 	{ "bvr", DMI_BIOS_VERSION },
914 	{ "bd",  DMI_BIOS_DATE },
915 	{ "svn", DMI_SYS_VENDOR },
916 	{ "pn",  DMI_PRODUCT_NAME },
917 	{ "pvr", DMI_PRODUCT_VERSION },
918 	{ "rvn", DMI_BOARD_VENDOR },
919 	{ "rn",  DMI_BOARD_NAME },
920 	{ "rvr", DMI_BOARD_VERSION },
921 	{ "cvn", DMI_CHASSIS_VENDOR },
922 	{ "ct",  DMI_CHASSIS_TYPE },
923 	{ "cvr", DMI_CHASSIS_VERSION },
924 	{ NULL,  DMI_NONE }
925 };
926 
dmi_ascii_filter(char * d,const char * s)927 static void dmi_ascii_filter(char *d, const char *s)
928 {
929 	/* Filter out characters we don't want to see in the modalias string */
930 	for (; *s; s++)
931 		if (*s > ' ' && *s < 127 && *s != ':')
932 			*(d++) = *s;
933 
934 	*d = 0;
935 }
936 
937 
do_dmi_entry(const char * filename,void * symval,char * alias)938 static int do_dmi_entry(const char *filename, void *symval,
939 			char *alias)
940 {
941 	int i, j;
942 	DEF_FIELD_ADDR(symval, dmi_system_id, matches);
943 	sprintf(alias, "dmi*");
944 
945 	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
946 		for (j = 0; j < 4; j++) {
947 			if ((*matches)[j].slot &&
948 			    (*matches)[j].slot == dmi_fields[i].field) {
949 				sprintf(alias + strlen(alias), ":%s*",
950 					dmi_fields[i].prefix);
951 				dmi_ascii_filter(alias + strlen(alias),
952 						 (*matches)[j].substr);
953 				strcat(alias, "*");
954 			}
955 		}
956 	}
957 
958 	strcat(alias, ":");
959 	return 1;
960 }
961 ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
962 
do_platform_entry(const char * filename,void * symval,char * alias)963 static int do_platform_entry(const char *filename,
964 			     void *symval, char *alias)
965 {
966 	DEF_FIELD_ADDR(symval, platform_device_id, name);
967 	sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
968 	return 1;
969 }
970 ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
971 
do_mdio_entry(const char * filename,void * symval,char * alias)972 static int do_mdio_entry(const char *filename,
973 			 void *symval, char *alias)
974 {
975 	int i;
976 	DEF_FIELD(symval, mdio_device_id, phy_id);
977 	DEF_FIELD(symval, mdio_device_id, phy_id_mask);
978 
979 	alias += sprintf(alias, MDIO_MODULE_PREFIX);
980 
981 	for (i = 0; i < 32; i++) {
982 		if (!((phy_id_mask >> (31-i)) & 1))
983 			*(alias++) = '?';
984 		else if ((phy_id >> (31-i)) & 1)
985 			*(alias++) = '1';
986 		else
987 			*(alias++) = '0';
988 	}
989 
990 	/* Terminate the string */
991 	*alias = 0;
992 
993 	return 1;
994 }
995 ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
996 
997 /* Looks like: zorro:iN. */
do_zorro_entry(const char * filename,void * symval,char * alias)998 static int do_zorro_entry(const char *filename, void *symval,
999 			  char *alias)
1000 {
1001 	DEF_FIELD(symval, zorro_device_id, id);
1002 	strcpy(alias, "zorro:");
1003 	ADD(alias, "i", id != ZORRO_WILDCARD, id);
1004 	return 1;
1005 }
1006 ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
1007 
1008 /* looks like: "pnp:dD" */
do_isapnp_entry(const char * filename,void * symval,char * alias)1009 static int do_isapnp_entry(const char *filename,
1010 			   void *symval, char *alias)
1011 {
1012 	DEF_FIELD(symval, isapnp_device_id, vendor);
1013 	DEF_FIELD(symval, isapnp_device_id, function);
1014 	sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
1015 		'A' + ((vendor >> 2) & 0x3f) - 1,
1016 		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1017 		'A' + ((vendor >> 8) & 0x1f) - 1,
1018 		(function >> 4) & 0x0f, function & 0x0f,
1019 		(function >> 12) & 0x0f, (function >> 8) & 0x0f);
1020 	return 1;
1021 }
1022 ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
1023 
1024 /* Looks like: "ipack:fNvNdN". */
do_ipack_entry(const char * filename,void * symval,char * alias)1025 static int do_ipack_entry(const char *filename,
1026 			  void *symval, char *alias)
1027 {
1028 	DEF_FIELD(symval, ipack_device_id, format);
1029 	DEF_FIELD(symval, ipack_device_id, vendor);
1030 	DEF_FIELD(symval, ipack_device_id, device);
1031 	strcpy(alias, "ipack:");
1032 	ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1033 	ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1034 	ADD(alias, "d", device != IPACK_ANY_ID, device);
1035 	add_wildcard(alias);
1036 	return 1;
1037 }
1038 ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
1039 
1040 /*
1041  * Append a match expression for a single masked hex digit.
1042  * outp points to a pointer to the character at which to append.
1043  *	*outp is updated on return to point just after the appended text,
1044  *	to facilitate further appending.
1045  */
append_nibble_mask(char ** outp,unsigned int nibble,unsigned int mask)1046 static void append_nibble_mask(char **outp,
1047 			       unsigned int nibble, unsigned int mask)
1048 {
1049 	char *p = *outp;
1050 	unsigned int i;
1051 
1052 	switch (mask) {
1053 	case 0:
1054 		*p++ = '?';
1055 		break;
1056 
1057 	case 0xf:
1058 		p += sprintf(p, "%X",  nibble);
1059 		break;
1060 
1061 	default:
1062 		/*
1063 		 * Dumbly emit a match pattern for all possible matching
1064 		 * digits.  This could be improved in some cases using ranges,
1065 		 * but it has the advantage of being trivially correct, and is
1066 		 * often optimal.
1067 		 */
1068 		*p++ = '[';
1069 		for (i = 0; i < 0x10; i++)
1070 			if ((i & mask) == nibble)
1071 				p += sprintf(p, "%X", i);
1072 		*p++ = ']';
1073 	}
1074 
1075 	/* Ensure that the string remains NUL-terminated: */
1076 	*p = '\0';
1077 
1078 	/* Advance the caller's end-of-string pointer: */
1079 	*outp = p;
1080 }
1081 
1082 /*
1083  * looks like: "amba:dN"
1084  *
1085  * N is exactly 8 digits, where each is an upper-case hex digit, or
1086  *	a ? or [] pattern matching exactly one digit.
1087  */
do_amba_entry(const char * filename,void * symval,char * alias)1088 static int do_amba_entry(const char *filename,
1089 			 void *symval, char *alias)
1090 {
1091 	unsigned int digit;
1092 	char *p = alias;
1093 	DEF_FIELD(symval, amba_id, id);
1094 	DEF_FIELD(symval, amba_id, mask);
1095 
1096 	if ((id & mask) != id)
1097 		fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1098 		      "id=0x%08X, mask=0x%08X.  Please fix this driver.\n",
1099 		      filename, id, mask);
1100 
1101 	p += sprintf(alias, "amba:d");
1102 	for (digit = 0; digit < 8; digit++)
1103 		append_nibble_mask(&p,
1104 				   (id >> (4 * (7 - digit))) & 0xf,
1105 				   (mask >> (4 * (7 - digit))) & 0xf);
1106 
1107 	return 1;
1108 }
1109 ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
1110 
1111 /* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,*
1112  * All fields are numbers. It would be nicer to use strings for vendor
1113  * and feature, but getting those out of the build system here is too
1114  * complicated.
1115  */
1116 
do_x86cpu_entry(const char * filename,void * symval,char * alias)1117 static int do_x86cpu_entry(const char *filename, void *symval,
1118 			   char *alias)
1119 {
1120 	DEF_FIELD(symval, x86_cpu_id, feature);
1121 	DEF_FIELD(symval, x86_cpu_id, family);
1122 	DEF_FIELD(symval, x86_cpu_id, model);
1123 	DEF_FIELD(symval, x86_cpu_id, vendor);
1124 
1125 	strcpy(alias, "x86cpu:");
1126 	ADD(alias, "vendor:",  vendor != X86_VENDOR_ANY, vendor);
1127 	ADD(alias, ":family:", family != X86_FAMILY_ANY, family);
1128 	ADD(alias, ":model:",  model  != X86_MODEL_ANY,  model);
1129 	strcat(alias, ":feature:*");
1130 	if (feature != X86_FEATURE_ANY)
1131 		sprintf(alias + strlen(alias), "%04X*", feature);
1132 	return 1;
1133 }
1134 ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
1135 
1136 /* LOOKS like cpu:type:*:feature:*FEAT* */
do_cpu_entry(const char * filename,void * symval,char * alias)1137 static int do_cpu_entry(const char *filename, void *symval, char *alias)
1138 {
1139 	DEF_FIELD(symval, cpu_feature, feature);
1140 
1141 	sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1142 	return 1;
1143 }
1144 ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry);
1145 
1146 /* Looks like: mei:S */
do_mei_entry(const char * filename,void * symval,char * alias)1147 static int do_mei_entry(const char *filename, void *symval,
1148 			char *alias)
1149 {
1150 	DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
1151 
1152 	sprintf(alias, MEI_CL_MODULE_PREFIX "%s", *name);
1153 
1154 	return 1;
1155 }
1156 ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry);
1157 
1158 /* Does namelen bytes of name exactly match the symbol? */
sym_is(const char * name,unsigned namelen,const char * symbol)1159 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1160 {
1161 	if (namelen != strlen(symbol))
1162 		return false;
1163 
1164 	return memcmp(name, symbol, namelen) == 0;
1165 }
1166 
do_table(void * symval,unsigned long size,unsigned long id_size,const char * device_id,void * function,struct module * mod)1167 static void do_table(void *symval, unsigned long size,
1168 		     unsigned long id_size,
1169 		     const char *device_id,
1170 		     void *function,
1171 		     struct module *mod)
1172 {
1173 	unsigned int i;
1174 	char alias[500];
1175 	int (*do_entry)(const char *, void *entry, char *alias) = function;
1176 
1177 	device_id_check(mod->name, device_id, size, id_size, symval);
1178 	/* Leave last one: it's the terminator. */
1179 	size -= id_size;
1180 
1181 	for (i = 0; i < size; i += id_size) {
1182 		if (do_entry(mod->name, symval+i, alias)) {
1183 			buf_printf(&mod->dev_table_buf,
1184 				   "MODULE_ALIAS(\"%s\");\n", alias);
1185 		}
1186 	}
1187 }
1188 
1189 /* Create MODULE_ALIAS() statements.
1190  * At this time, we cannot write the actual output C source yet,
1191  * so we write into the mod->dev_table_buf buffer. */
handle_moddevtable(struct module * mod,struct elf_info * info,Elf_Sym * sym,const char * symname)1192 void handle_moddevtable(struct module *mod, struct elf_info *info,
1193 			Elf_Sym *sym, const char *symname)
1194 {
1195 	void *symval;
1196 	char *zeros = NULL;
1197 	const char *name;
1198 	unsigned int namelen;
1199 
1200 	/* We're looking for a section relative symbol */
1201 	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1202 		return;
1203 
1204 	/* We're looking for an object */
1205 	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1206 		return;
1207 
1208 	/* All our symbols are of form <prefix>__mod_XXX_device_table. */
1209 	name = strstr(symname, "__mod_");
1210 	if (!name)
1211 		return;
1212 	name += strlen("__mod_");
1213 	namelen = strlen(name);
1214 	if (namelen < strlen("_device_table"))
1215 		return;
1216 	if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1217 		return;
1218 	namelen -= strlen("_device_table");
1219 
1220 	/* Handle all-NULL symbols allocated into .bss */
1221 	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1222 		zeros = calloc(1, sym->st_size);
1223 		symval = zeros;
1224 	} else {
1225 		symval = (void *)info->hdr
1226 			+ info->sechdrs[get_secindex(info, sym)].sh_offset
1227 			+ sym->st_value;
1228 	}
1229 
1230 	/* First handle the "special" cases */
1231 	if (sym_is(name, namelen, "usb"))
1232 		do_usb_table(symval, sym->st_size, mod);
1233 	else if (sym_is(name, namelen, "pnp"))
1234 		do_pnp_device_entry(symval, sym->st_size, mod);
1235 	else if (sym_is(name, namelen, "pnp_card"))
1236 		do_pnp_card_entries(symval, sym->st_size, mod);
1237 	else {
1238 		struct devtable **p;
1239 		INIT_SECTION(__devtable);
1240 
1241 		for (p = __start___devtable; p < __stop___devtable; p++) {
1242 			if (sym_is(name, namelen, (*p)->device_id)) {
1243 				do_table(symval, sym->st_size, (*p)->id_size,
1244 					 (*p)->device_id, (*p)->function, mod);
1245 				break;
1246 			}
1247 		}
1248 	}
1249 	free(zeros);
1250 }
1251 
1252 /* Now add out buffered information to the generated C source */
add_moddevtable(struct buffer * buf,struct module * mod)1253 void add_moddevtable(struct buffer *buf, struct module *mod)
1254 {
1255 	buf_printf(buf, "\n");
1256 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1257 	free(mod->dev_table_buf.p);
1258 }
1259