xref: /linux/scripts/mod/file2alias.c (revision 3f6bc9e3ab9b127171d39f9ac6eca1abb693b731)
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 <stdarg.h>
14 #include <stdio.h>
15 
16 #include "list.h"
17 #include "xalloc.h"
18 
19 #include "modpost.h"
20 #include "devicetable-offsets.h"
21 
22 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
23  * use either stdint.h or inttypes.h for the rest. */
24 #if KERNEL_ELFCLASS == ELFCLASS32
25 typedef Elf32_Addr	kernel_ulong_t;
26 #define BITS_PER_LONG 32
27 #else
28 typedef Elf64_Addr	kernel_ulong_t;
29 #define BITS_PER_LONG 64
30 #endif
31 #ifdef __sun__
32 #include <inttypes.h>
33 #else
34 #include <stdint.h>
35 #endif
36 
37 #include <ctype.h>
38 #include <stdbool.h>
39 
40 /**
41  * module_alias_printf - add auto-generated MODULE_ALIAS()
42  *
43  * @mod: module
44  * @append_wildcard: append '*' for future extension if not exist yet
45  * @fmt: printf(3)-like format
46  */
47 static void __attribute__((format (printf, 3, 4)))
48 module_alias_printf(struct module *mod, bool append_wildcard,
49 		    const char *fmt, ...)
50 {
51 	struct module_alias *new, *als;
52 	size_t len;
53 	int n;
54 	va_list ap;
55 
56 	/* Determine required size. */
57 	va_start(ap, fmt);
58 	n = vsnprintf(NULL, 0, fmt, ap);
59 	va_end(ap);
60 
61 	if (n < 0) {
62 		error("vsnprintf failed\n");
63 		return;
64 	}
65 
66 	len = n + 1;	/* extra byte for '\0' */
67 
68 	if (append_wildcard)
69 		len++;	/* extra byte for '*' */
70 
71 	new = xmalloc(sizeof(*new) + len);
72 
73 	/* Now, really print it to the allocated buffer */
74 	va_start(ap, fmt);
75 	n = vsnprintf(new->str, len, fmt, ap);
76 	va_end(ap);
77 
78 	if (n < 0) {
79 		error("vsnprintf failed\n");
80 		free(new);
81 		return;
82 	}
83 
84 	if (append_wildcard && (n == 0 || new->str[n - 1] != '*')) {
85 		new->str[n] = '*';
86 		new->str[n + 1] = '\0';
87 	}
88 
89 	/* avoid duplication */
90 	list_for_each_entry(als, &mod->aliases, node) {
91 		if (!strcmp(als->str, new->str)) {
92 			free(new);
93 			return;
94 		}
95 	}
96 
97 	list_add_tail(&new->node, &mod->aliases);
98 }
99 
100 typedef uint32_t	__u32;
101 typedef uint16_t	__u16;
102 typedef unsigned char	__u8;
103 
104 /* UUID types for backward compatibility, don't use in new code */
105 typedef struct {
106 	__u8 b[16];
107 } guid_t;
108 
109 typedef struct {
110 	__u8 b[16];
111 } uuid_t;
112 
113 #define	UUID_STRING_LEN		36
114 
115 /* MEI UUID type, don't use anywhere else */
116 typedef struct {
117 	__u8 b[16];
118 } uuid_le;
119 
120 /* Big exception to the "don't include kernel headers into userspace, which
121  * even potentially has different endianness and word sizes, since
122  * we handle those differences explicitly below */
123 #include "../../include/linux/mod_devicetable.h"
124 
125 struct devtable {
126 	const char *device_id;
127 	unsigned long id_size;
128 	void (*do_entry)(struct module *mod, void *symval);
129 };
130 
131 /* Define a variable f that holds the value of field f of struct devid
132  * based at address m.
133  */
134 #define DEF_FIELD(m, devid, f) \
135 	typeof(((struct devid *)0)->f) f = \
136 		get_unaligned_native((typeof(f) *)((m) + OFF_##devid##_##f))
137 
138 /* Define a variable f that holds the address of field f of struct devid
139  * based at address m.  Due to the way typeof works, for a field of type
140  * T[N] the variable has type T(*)[N], _not_ T*.
141  */
142 #define DEF_FIELD_ADDR(m, devid, f) \
143 	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
144 
145 #define ADD(str, sep, cond, field)                              \
146 do {                                                            \
147         strcat(str, sep);                                       \
148         if (cond)                                               \
149                 sprintf(str + strlen(str),                      \
150                         sizeof(field) == 1 ? "%02X" :           \
151                         sizeof(field) == 2 ? "%04X" :           \
152                         sizeof(field) == 4 ? "%08X" : "",       \
153                         field);                                 \
154         else                                                    \
155                 sprintf(str + strlen(str), "*");                \
156 } while(0)
157 
158 static inline void add_uuid(char *str, uuid_le uuid)
159 {
160 	int len = strlen(str);
161 
162 	sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
163 		uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
164 		uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
165 		uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
166 		uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
167 }
168 
169 static inline void add_guid(char *str, guid_t guid)
170 {
171 	int len = strlen(str);
172 
173 	sprintf(str + len, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
174 		guid.b[3], guid.b[2], guid.b[1], guid.b[0],
175 		guid.b[5], guid.b[4], guid.b[7], guid.b[6],
176 		guid.b[8], guid.b[9], guid.b[10], guid.b[11],
177 		guid.b[12], guid.b[13], guid.b[14], guid.b[15]);
178 }
179 
180 /* USB is special because the bcdDevice can be matched against a numeric range */
181 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
182 static void do_usb_entry(void *symval,
183 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
184 			 unsigned char range_lo, unsigned char range_hi,
185 			 unsigned char max, struct module *mod)
186 {
187 	char alias[500];
188 	DEF_FIELD(symval, usb_device_id, match_flags);
189 	DEF_FIELD(symval, usb_device_id, idVendor);
190 	DEF_FIELD(symval, usb_device_id, idProduct);
191 	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
192 	DEF_FIELD(symval, usb_device_id, bDeviceClass);
193 	DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
194 	DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
195 	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
196 	DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
197 	DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
198 	DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
199 
200 	strcpy(alias, "usb:");
201 	ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
202 	    idVendor);
203 	ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
204 	    idProduct);
205 
206 	strcat(alias, "d");
207 	if (bcdDevice_initial_digits)
208 		sprintf(alias + strlen(alias), "%0*X",
209 			bcdDevice_initial_digits, bcdDevice_initial);
210 	if (range_lo == range_hi)
211 		sprintf(alias + strlen(alias), "%X", range_lo);
212 	else if (range_lo > 0 || range_hi < max) {
213 		if (range_lo > 0x9 || range_hi < 0xA)
214 			sprintf(alias + strlen(alias),
215 				"[%X-%X]",
216 				range_lo,
217 				range_hi);
218 		else {
219 			sprintf(alias + strlen(alias),
220 				range_lo < 0x9 ? "[%X-9" : "[%X",
221 				range_lo);
222 			sprintf(alias + strlen(alias),
223 				range_hi > 0xA ? "A-%X]" : "%X]",
224 				range_hi);
225 		}
226 	}
227 	if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
228 		strcat(alias, "*");
229 
230 	ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
231 	    bDeviceClass);
232 	ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
233 	    bDeviceSubClass);
234 	ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
235 	    bDeviceProtocol);
236 	ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
237 	    bInterfaceClass);
238 	ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
239 	    bInterfaceSubClass);
240 	ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
241 	    bInterfaceProtocol);
242 	ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
243 	    bInterfaceNumber);
244 
245 	module_alias_printf(mod, true, "%s", alias);
246 }
247 
248 /* Handles increment/decrement of BCD formatted integers */
249 /* Returns the previous value, so it works like i++ or i-- */
250 static unsigned int incbcd(unsigned int *bcd,
251 			   int inc,
252 			   unsigned char max,
253 			   size_t chars)
254 {
255 	unsigned int init = *bcd, i, j;
256 	unsigned long long c, dec = 0;
257 
258 	/* If bcd is not in BCD format, just increment */
259 	if (max > 0x9) {
260 		*bcd += inc;
261 		return init;
262 	}
263 
264 	/* Convert BCD to Decimal */
265 	for (i=0 ; i < chars ; i++) {
266 		c = (*bcd >> (i << 2)) & 0xf;
267 		c = c > 9 ? 9 : c; /* force to bcd just in case */
268 		for (j=0 ; j < i ; j++)
269 			c = c * 10;
270 		dec += c;
271 	}
272 
273 	/* Do our increment/decrement */
274 	dec += inc;
275 	*bcd  = 0;
276 
277 	/* Convert back to BCD */
278 	for (i=0 ; i < chars ; i++) {
279 		for (c=1,j=0 ; j < i ; j++)
280 			c = c * 10;
281 		c = (dec / c) % 10;
282 		*bcd += c << (i << 2);
283 	}
284 	return init;
285 }
286 
287 static void do_usb_entry_multi(struct module *mod, void *symval)
288 {
289 	unsigned int devlo, devhi;
290 	unsigned char chi, clo, max;
291 	int ndigits;
292 
293 	DEF_FIELD(symval, usb_device_id, match_flags);
294 	DEF_FIELD(symval, usb_device_id, idVendor);
295 	DEF_FIELD(symval, usb_device_id, idProduct);
296 	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
297 	DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
298 	DEF_FIELD(symval, usb_device_id, bDeviceClass);
299 	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
300 
301 	devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
302 		bcdDevice_lo : 0x0U;
303 	devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
304 		bcdDevice_hi : ~0x0U;
305 
306 	/* Figure out if this entry is in bcd or hex format */
307 	max = 0x9; /* Default to decimal format */
308 	for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
309 		clo = (devlo >> (ndigits << 2)) & 0xf;
310 		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
311 		if (clo > max || chi > max) {
312 			max = 0xf;
313 			break;
314 		}
315 	}
316 
317 	/*
318 	 * Some modules (visor) have empty slots as placeholder for
319 	 * run-time specification that results in catch-all alias
320 	 */
321 	if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
322 		return;
323 
324 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
325 	for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
326 		clo = devlo & 0xf;
327 		chi = devhi & 0xf;
328 		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
329 			chi = max;
330 		devlo >>= 4;
331 		devhi >>= 4;
332 
333 		if (devlo == devhi || !ndigits) {
334 			do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
335 			break;
336 		}
337 
338 		if (clo > 0x0)
339 			do_usb_entry(symval,
340 				     incbcd(&devlo, 1, max,
341 					    sizeof(bcdDevice_lo) * 2),
342 				     ndigits, clo, max, max, mod);
343 
344 		if (chi < max)
345 			do_usb_entry(symval,
346 				     incbcd(&devhi, -1, max,
347 					    sizeof(bcdDevice_lo) * 2),
348 				     ndigits, 0x0, chi, max, mod);
349 	}
350 }
351 
352 static void do_of_entry(struct module *mod, void *symval)
353 {
354 	char alias[500];
355 	int len;
356 	char *tmp;
357 
358 	DEF_FIELD_ADDR(symval, of_device_id, name);
359 	DEF_FIELD_ADDR(symval, of_device_id, type);
360 	DEF_FIELD_ADDR(symval, of_device_id, compatible);
361 
362 	len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
363 		      (*type)[0] ? *type : "*");
364 
365 	if ((*compatible)[0])
366 		sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
367 			*compatible);
368 
369 	/* Replace all whitespace with underscores */
370 	for (tmp = alias; tmp && *tmp; tmp++)
371 		if (isspace(*tmp))
372 			*tmp = '_';
373 
374 	module_alias_printf(mod, false, "%s", alias);
375 	module_alias_printf(mod, false, "%sC*", alias);
376 }
377 
378 /* Looks like: hid:bNvNpN */
379 static void do_hid_entry(struct module *mod, void *symval)
380 {
381 	char alias[256] = {};
382 
383 	DEF_FIELD(symval, hid_device_id, bus);
384 	DEF_FIELD(symval, hid_device_id, group);
385 	DEF_FIELD(symval, hid_device_id, vendor);
386 	DEF_FIELD(symval, hid_device_id, product);
387 
388 	ADD(alias, "b", bus != HID_BUS_ANY, bus);
389 	ADD(alias, "g", group != HID_GROUP_ANY, group);
390 	ADD(alias, "v", vendor != HID_ANY_ID, vendor);
391 	ADD(alias, "p", product != HID_ANY_ID, product);
392 
393 	module_alias_printf(mod, false, "hid:%s", alias);
394 }
395 
396 /* Looks like: ieee1394:venNmoNspNverN */
397 static void do_ieee1394_entry(struct module *mod, void *symval)
398 {
399 	char alias[256] = {};
400 
401 	DEF_FIELD(symval, ieee1394_device_id, match_flags);
402 	DEF_FIELD(symval, ieee1394_device_id, vendor_id);
403 	DEF_FIELD(symval, ieee1394_device_id, model_id);
404 	DEF_FIELD(symval, ieee1394_device_id, specifier_id);
405 	DEF_FIELD(symval, ieee1394_device_id, version);
406 
407 	ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
408 	    vendor_id);
409 	ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
410 	    model_id);
411 	ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
412 	    specifier_id);
413 	ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
414 	    version);
415 
416 	module_alias_printf(mod, true, "ieee1394:%s", alias);
417 }
418 
419 /* Looks like: pci:vNdNsvNsdNbcNscNiN or <prefix>_pci:vNdNsvNsdNbcNscNiN. */
420 static void do_pci_entry(struct module *mod, void *symval)
421 {
422 	char alias[256];
423 	/* Class field can be divided into these three. */
424 	unsigned char baseclass, subclass, interface,
425 		baseclass_mask, subclass_mask, interface_mask;
426 
427 	DEF_FIELD(symval, pci_device_id, vendor);
428 	DEF_FIELD(symval, pci_device_id, device);
429 	DEF_FIELD(symval, pci_device_id, subvendor);
430 	DEF_FIELD(symval, pci_device_id, subdevice);
431 	DEF_FIELD(symval, pci_device_id, class);
432 	DEF_FIELD(symval, pci_device_id, class_mask);
433 	DEF_FIELD(symval, pci_device_id, override_only);
434 
435 	switch (override_only) {
436 	case 0:
437 		strcpy(alias, "pci:");
438 		break;
439 	case PCI_ID_F_VFIO_DRIVER_OVERRIDE:
440 		strcpy(alias, "vfio_pci:");
441 		break;
442 	default:
443 		warn("Unknown PCI driver_override alias %08X\n",
444 		     override_only);
445 	}
446 
447 	ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
448 	ADD(alias, "d", device != PCI_ANY_ID, device);
449 	ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
450 	ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
451 
452 	baseclass = (class) >> 16;
453 	baseclass_mask = (class_mask) >> 16;
454 	subclass = (class) >> 8;
455 	subclass_mask = (class_mask) >> 8;
456 	interface = class;
457 	interface_mask = class_mask;
458 
459 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
460 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
461 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
462 		warn("Can't handle masks in %s:%04X\n",
463 		     mod->name, class_mask);
464 		return;
465 	}
466 
467 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
468 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
469 	ADD(alias, "i", interface_mask == 0xFF, interface);
470 
471 	module_alias_printf(mod, true, "%s", alias);
472 }
473 
474 /* looks like: "ccw:tNmNdtNdmN" */
475 static void do_ccw_entry(struct module *mod, void *symval)
476 {
477 	char alias[256] = {};
478 
479 	DEF_FIELD(symval, ccw_device_id, match_flags);
480 	DEF_FIELD(symval, ccw_device_id, cu_type);
481 	DEF_FIELD(symval, ccw_device_id, cu_model);
482 	DEF_FIELD(symval, ccw_device_id, dev_type);
483 	DEF_FIELD(symval, ccw_device_id, dev_model);
484 
485 	ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
486 	    cu_type);
487 	ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
488 	    cu_model);
489 	ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
490 	    dev_type);
491 	ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
492 	    dev_model);
493 
494 	module_alias_printf(mod, true, "ccw:%s", alias);
495 }
496 
497 /* looks like: "ap:tN" */
498 static void do_ap_entry(struct module *mod, void *symval)
499 {
500 	DEF_FIELD(symval, ap_device_id, dev_type);
501 
502 	module_alias_printf(mod, false, "ap:t%02X*", dev_type);
503 }
504 
505 /* looks like: "css:tN" */
506 static void do_css_entry(struct module *mod, void *symval)
507 {
508 	DEF_FIELD(symval, css_device_id, type);
509 
510 	module_alias_printf(mod, false, "css:t%01X", type);
511 }
512 
513 /* Looks like: "serio:tyNprNidNexN" */
514 static void do_serio_entry(struct module *mod, void *symval)
515 {
516 	char alias[256] = {};
517 
518 	DEF_FIELD(symval, serio_device_id, type);
519 	DEF_FIELD(symval, serio_device_id, proto);
520 	DEF_FIELD(symval, serio_device_id, id);
521 	DEF_FIELD(symval, serio_device_id, extra);
522 
523 	ADD(alias, "ty", type != SERIO_ANY, type);
524 	ADD(alias, "pr", proto != SERIO_ANY, proto);
525 	ADD(alias, "id", id != SERIO_ANY, id);
526 	ADD(alias, "ex", extra != SERIO_ANY, extra);
527 
528 	module_alias_printf(mod, true, "serio:%s", alias);
529 }
530 
531 /* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
532  *             "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
533  *
534  * NOTE: Each driver should use one of the following : _HID, _CIDs
535  *       or _CLS. Also, bb, ss, and pp can be substituted with ??
536  *       as don't care byte.
537  */
538 static void do_acpi_entry(struct module *mod, void *symval)
539 {
540 	DEF_FIELD_ADDR(symval, acpi_device_id, id);
541 	DEF_FIELD(symval, acpi_device_id, cls);
542 	DEF_FIELD(symval, acpi_device_id, cls_msk);
543 
544 	if ((*id)[0])
545 		module_alias_printf(mod, false, "acpi*:%s:*", *id);
546 	else {
547 		char alias[256];
548 		int i, byte_shift, cnt = 0;
549 		unsigned int msk;
550 
551 		for (i = 1; i <= 3; i++) {
552 			byte_shift = 8 * (3-i);
553 			msk = (cls_msk >> byte_shift) & 0xFF;
554 			if (msk)
555 				sprintf(&alias[cnt], "%02x",
556 					(cls >> byte_shift) & 0xFF);
557 			else
558 				sprintf(&alias[cnt], "??");
559 			cnt += 2;
560 		}
561 		module_alias_printf(mod, false, "acpi*:%s:*", alias);
562 	}
563 }
564 
565 /* looks like: "pnp:dD" */
566 static void do_pnp_device_entry(struct module *mod, void *symval)
567 {
568 	DEF_FIELD_ADDR(symval, pnp_device_id, id);
569 	char acpi_id[sizeof(*id)];
570 
571 	/* fix broken pnp bus lowercasing */
572 	for (unsigned int i = 0; i < sizeof(acpi_id); i++)
573 		acpi_id[i] = toupper((*id)[i]);
574 	module_alias_printf(mod, false, "pnp:d%s*", *id);
575 	module_alias_printf(mod, false, "acpi*:%s:*", acpi_id);
576 }
577 
578 /* looks like: "pnp:dD" for every device of the card */
579 static void do_pnp_card_entry(struct module *mod, void *symval)
580 {
581 	DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
582 
583 	for (unsigned int i = 0; i < PNP_MAX_DEVICES; i++) {
584 		const char *id = (char *)(*devs)[i].id;
585 		char acpi_id[PNP_ID_LEN];
586 
587 		if (!id[0])
588 			break;
589 
590 		/* fix broken pnp bus lowercasing */
591 		for (unsigned int j = 0; j < sizeof(acpi_id); j++)
592 			acpi_id[j] = toupper(id[j]);
593 
594 		/* add an individual alias for every device entry */
595 		module_alias_printf(mod, false, "pnp:d%s*", id);
596 		module_alias_printf(mod, false, "acpi*:%s:*", acpi_id);
597 	}
598 }
599 
600 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
601 static void do_pcmcia_entry(struct module *mod, void *symval)
602 {
603 	char alias[256] = {};
604 
605 	DEF_FIELD(symval, pcmcia_device_id, match_flags);
606 	DEF_FIELD(symval, pcmcia_device_id, manf_id);
607 	DEF_FIELD(symval, pcmcia_device_id, card_id);
608 	DEF_FIELD(symval, pcmcia_device_id, func_id);
609 	DEF_FIELD(symval, pcmcia_device_id, function);
610 	DEF_FIELD(symval, pcmcia_device_id, device_no);
611 	DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
612 
613 	ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
614 	    manf_id);
615 	ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
616 	    card_id);
617 	ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
618 	    func_id);
619 	ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
620 	    function);
621 	ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
622 	    device_no);
623 	ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1,
624 	    get_unaligned_native(*prod_id_hash + 0));
625 	ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2,
626 	    get_unaligned_native(*prod_id_hash + 1));
627 	ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3,
628 	    get_unaligned_native(*prod_id_hash + 2));
629 	ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4,
630 	    get_unaligned_native(*prod_id_hash + 3));
631 
632 	module_alias_printf(mod, true, "pcmcia:%s", alias);
633 }
634 
635 static void do_vio_entry(struct module *mod, void *symval)
636 {
637 	char alias[256];
638 	char *tmp;
639 	DEF_FIELD_ADDR(symval, vio_device_id, type);
640 	DEF_FIELD_ADDR(symval, vio_device_id, compat);
641 
642 	sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
643 			(*compat)[0] ? *compat : "*");
644 
645 	/* Replace all whitespace with underscores */
646 	for (tmp = alias; tmp && *tmp; tmp++)
647 		if (isspace (*tmp))
648 			*tmp = '_';
649 
650 	module_alias_printf(mod, true, "%s", alias);
651 }
652 
653 static void do_input(char *alias,
654 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
655 {
656 	unsigned int i;
657 
658 	for (i = min; i <= max; i++)
659 		if (get_unaligned_native(arr + i / BITS_PER_LONG) &
660 		    (1ULL << (i % BITS_PER_LONG)))
661 			sprintf(alias + strlen(alias), "%X,*", i);
662 }
663 
664 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
665 static void do_input_entry(struct module *mod, void *symval)
666 {
667 	char alias[256] = {};
668 
669 	DEF_FIELD(symval, input_device_id, flags);
670 	DEF_FIELD(symval, input_device_id, bustype);
671 	DEF_FIELD(symval, input_device_id, vendor);
672 	DEF_FIELD(symval, input_device_id, product);
673 	DEF_FIELD(symval, input_device_id, version);
674 	DEF_FIELD_ADDR(symval, input_device_id, evbit);
675 	DEF_FIELD_ADDR(symval, input_device_id, keybit);
676 	DEF_FIELD_ADDR(symval, input_device_id, relbit);
677 	DEF_FIELD_ADDR(symval, input_device_id, absbit);
678 	DEF_FIELD_ADDR(symval, input_device_id, mscbit);
679 	DEF_FIELD_ADDR(symval, input_device_id, ledbit);
680 	DEF_FIELD_ADDR(symval, input_device_id, sndbit);
681 	DEF_FIELD_ADDR(symval, input_device_id, ffbit);
682 	DEF_FIELD_ADDR(symval, input_device_id, swbit);
683 
684 	ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
685 	ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
686 	ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
687 	ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
688 
689 	sprintf(alias + strlen(alias), "-e*");
690 	if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
691 		do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
692 	sprintf(alias + strlen(alias), "k*");
693 	if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
694 		do_input(alias, *keybit,
695 			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
696 			 INPUT_DEVICE_ID_KEY_MAX);
697 	sprintf(alias + strlen(alias), "r*");
698 	if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
699 		do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
700 	sprintf(alias + strlen(alias), "a*");
701 	if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
702 		do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
703 	sprintf(alias + strlen(alias), "m*");
704 	if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
705 		do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
706 	sprintf(alias + strlen(alias), "l*");
707 	if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
708 		do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
709 	sprintf(alias + strlen(alias), "s*");
710 	if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
711 		do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
712 	sprintf(alias + strlen(alias), "f*");
713 	if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
714 		do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
715 	sprintf(alias + strlen(alias), "w*");
716 	if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
717 		do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
718 
719 	module_alias_printf(mod, false, "input:%s", alias);
720 }
721 
722 static void do_eisa_entry(struct module *mod, void *symval)
723 {
724 	DEF_FIELD_ADDR(symval, eisa_device_id, sig);
725 	module_alias_printf(mod, false, EISA_DEVICE_MODALIAS_FMT "*", *sig);
726 }
727 
728 /* Looks like: parisc:tNhvNrevNsvN */
729 static void do_parisc_entry(struct module *mod, void *symval)
730 {
731 	char alias[256] = {};
732 
733 	DEF_FIELD(symval, parisc_device_id, hw_type);
734 	DEF_FIELD(symval, parisc_device_id, hversion);
735 	DEF_FIELD(symval, parisc_device_id, hversion_rev);
736 	DEF_FIELD(symval, parisc_device_id, sversion);
737 
738 	ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
739 	ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
740 	ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
741 	ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
742 
743 	module_alias_printf(mod, true, "parisc:%s", alias);
744 }
745 
746 /* Looks like: sdio:cNvNdN. */
747 static void do_sdio_entry(struct module *mod, void *symval)
748 {
749 	char alias[256] = {};
750 
751 	DEF_FIELD(symval, sdio_device_id, class);
752 	DEF_FIELD(symval, sdio_device_id, vendor);
753 	DEF_FIELD(symval, sdio_device_id, device);
754 
755 	ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
756 	ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
757 	ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
758 
759 	module_alias_printf(mod, true, "sdio:%s", alias);
760 }
761 
762 /* Looks like: ssb:vNidNrevN. */
763 static void do_ssb_entry(struct module *mod, void *symval)
764 {
765 	char alias[256] = {};
766 
767 	DEF_FIELD(symval, ssb_device_id, vendor);
768 	DEF_FIELD(symval, ssb_device_id, coreid);
769 	DEF_FIELD(symval, ssb_device_id, revision);
770 
771 	ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
772 	ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
773 	ADD(alias, "rev", revision != SSB_ANY_REV, revision);
774 
775 	module_alias_printf(mod, true, "ssb:%s", alias);
776 }
777 
778 /* Looks like: bcma:mNidNrevNclN. */
779 static void do_bcma_entry(struct module *mod, void *symval)
780 {
781 	char alias[256] = {};
782 
783 	DEF_FIELD(symval, bcma_device_id, manuf);
784 	DEF_FIELD(symval, bcma_device_id, id);
785 	DEF_FIELD(symval, bcma_device_id, rev);
786 	DEF_FIELD(symval, bcma_device_id, class);
787 
788 	ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
789 	ADD(alias, "id", id != BCMA_ANY_ID, id);
790 	ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
791 	ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
792 
793 	module_alias_printf(mod, true, "bcma:%s", alias);
794 }
795 
796 /* Looks like: virtio:dNvN */
797 static void do_virtio_entry(struct module *mod, void *symval)
798 {
799 	char alias[256] = {};
800 
801 	DEF_FIELD(symval, virtio_device_id, device);
802 	DEF_FIELD(symval, virtio_device_id, vendor);
803 
804 	ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
805 	ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
806 
807 	module_alias_printf(mod, true, "virtio:%s", alias);
808 }
809 
810 /*
811  * Looks like: vmbus:guid
812  * Each byte of the guid will be represented by two hex characters
813  * in the name.
814  */
815 static void do_vmbus_entry(struct module *mod, void *symval)
816 {
817 	DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
818 	char guid_name[sizeof(*guid) * 2 + 1];
819 
820 	for (int i = 0; i < sizeof(*guid); i++)
821 		sprintf(&guid_name[i * 2], "%02x", guid->b[i]);
822 
823 	module_alias_printf(mod, false, "vmbus:%s", guid_name);
824 }
825 
826 /* Looks like: rpmsg:S */
827 static void do_rpmsg_entry(struct module *mod, void *symval)
828 {
829 	DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
830 
831 	module_alias_printf(mod, false, RPMSG_DEVICE_MODALIAS_FMT, *name);
832 }
833 
834 /* Looks like: i2c:S */
835 static void do_i2c_entry(struct module *mod, void *symval)
836 {
837 	DEF_FIELD_ADDR(symval, i2c_device_id, name);
838 
839 	module_alias_printf(mod, false, I2C_MODULE_PREFIX "%s", *name);
840 }
841 
842 static void do_i3c_entry(struct module *mod, void *symval)
843 {
844 	char alias[256] = {};
845 
846 	DEF_FIELD(symval, i3c_device_id, match_flags);
847 	DEF_FIELD(symval, i3c_device_id, dcr);
848 	DEF_FIELD(symval, i3c_device_id, manuf_id);
849 	DEF_FIELD(symval, i3c_device_id, part_id);
850 	DEF_FIELD(symval, i3c_device_id, extra_info);
851 
852 	ADD(alias, "dcr", match_flags & I3C_MATCH_DCR, dcr);
853 	ADD(alias, "manuf", match_flags & I3C_MATCH_MANUF, manuf_id);
854 	ADD(alias, "part", match_flags & I3C_MATCH_PART, part_id);
855 	ADD(alias, "ext", match_flags & I3C_MATCH_EXTRA_INFO, extra_info);
856 
857 	module_alias_printf(mod, false, "i3c:%s", alias);
858 }
859 
860 static void do_slim_entry(struct module *mod, void *symval)
861 {
862 	DEF_FIELD(symval, slim_device_id, manf_id);
863 	DEF_FIELD(symval, slim_device_id, prod_code);
864 
865 	module_alias_printf(mod, false, "slim:%x:%x:*", manf_id, prod_code);
866 }
867 
868 /* Looks like: spi:S */
869 static void do_spi_entry(struct module *mod, void *symval)
870 {
871 	DEF_FIELD_ADDR(symval, spi_device_id, name);
872 
873 	module_alias_printf(mod, false, SPI_MODULE_PREFIX "%s", *name);
874 }
875 
876 static const struct dmifield {
877 	const char *prefix;
878 	int field;
879 } dmi_fields[] = {
880 	{ "bvn", DMI_BIOS_VENDOR },
881 	{ "bvr", DMI_BIOS_VERSION },
882 	{ "bd",  DMI_BIOS_DATE },
883 	{ "br",  DMI_BIOS_RELEASE },
884 	{ "efr", DMI_EC_FIRMWARE_RELEASE },
885 	{ "svn", DMI_SYS_VENDOR },
886 	{ "pn",  DMI_PRODUCT_NAME },
887 	{ "pvr", DMI_PRODUCT_VERSION },
888 	{ "rvn", DMI_BOARD_VENDOR },
889 	{ "rn",  DMI_BOARD_NAME },
890 	{ "rvr", DMI_BOARD_VERSION },
891 	{ "cvn", DMI_CHASSIS_VENDOR },
892 	{ "ct",  DMI_CHASSIS_TYPE },
893 	{ "cvr", DMI_CHASSIS_VERSION },
894 	{ NULL,  DMI_NONE }
895 };
896 
897 static void dmi_ascii_filter(char *d, const char *s)
898 {
899 	/* Filter out characters we don't want to see in the modalias string */
900 	for (; *s; s++)
901 		if (*s > ' ' && *s < 127 && *s != ':')
902 			*(d++) = *s;
903 
904 	*d = 0;
905 }
906 
907 
908 static void do_dmi_entry(struct module *mod, void *symval)
909 {
910 	char alias[256] = {};
911 	int i, j;
912 	DEF_FIELD_ADDR(symval, dmi_system_id, matches);
913 
914 	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
915 		for (j = 0; j < 4; j++) {
916 			if ((*matches)[j].slot &&
917 			    (*matches)[j].slot == dmi_fields[i].field) {
918 				sprintf(alias + strlen(alias), ":%s*",
919 					dmi_fields[i].prefix);
920 				dmi_ascii_filter(alias + strlen(alias),
921 						 (*matches)[j].substr);
922 				strcat(alias, "*");
923 			}
924 		}
925 	}
926 
927 	module_alias_printf(mod, false, "dmi*%s:", alias);
928 }
929 
930 static void do_platform_entry(struct module *mod, void *symval)
931 {
932 	DEF_FIELD_ADDR(symval, platform_device_id, name);
933 
934 	module_alias_printf(mod, false, PLATFORM_MODULE_PREFIX "%s", *name);
935 }
936 
937 static void do_mdio_entry(struct module *mod, void *symval)
938 {
939 	char id[33];
940 	int i;
941 	DEF_FIELD(symval, mdio_device_id, phy_id);
942 	DEF_FIELD(symval, mdio_device_id, phy_id_mask);
943 
944 	for (i = 0; i < 32; i++) {
945 		if (!((phy_id_mask >> (31-i)) & 1))
946 			id[i] = '?';
947 		else if ((phy_id >> (31-i)) & 1)
948 			id[i] = '1';
949 		else
950 			id[i] = '0';
951 	}
952 
953 	/* Terminate the string */
954 	id[32] = '\0';
955 
956 	module_alias_printf(mod, false, MDIO_MODULE_PREFIX "%s", id);
957 }
958 
959 /* Looks like: zorro:iN. */
960 static void do_zorro_entry(struct module *mod, void *symval)
961 {
962 	char alias[256] = {};
963 	DEF_FIELD(symval, zorro_device_id, id);
964 
965 	ADD(alias, "i", id != ZORRO_WILDCARD, id);
966 
967 	module_alias_printf(mod, false, "zorro:%s", alias);
968 }
969 
970 /* looks like: "pnp:dD" */
971 static void do_isapnp_entry(struct module *mod, void *symval)
972 {
973 	DEF_FIELD(symval, isapnp_device_id, vendor);
974 	DEF_FIELD(symval, isapnp_device_id, function);
975 	module_alias_printf(mod, false, "pnp:d%c%c%c%x%x%x%x*",
976 		'A' + ((vendor >> 2) & 0x3f) - 1,
977 		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
978 		'A' + ((vendor >> 8) & 0x1f) - 1,
979 		(function >> 4) & 0x0f, function & 0x0f,
980 		(function >> 12) & 0x0f, (function >> 8) & 0x0f);
981 }
982 
983 /* Looks like: "ipack:fNvNdN". */
984 static void do_ipack_entry(struct module *mod, void *symval)
985 {
986 	char alias[256] = {};
987 	DEF_FIELD(symval, ipack_device_id, format);
988 	DEF_FIELD(symval, ipack_device_id, vendor);
989 	DEF_FIELD(symval, ipack_device_id, device);
990 
991 	ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
992 	ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
993 	ADD(alias, "d", device != IPACK_ANY_ID, device);
994 
995 	module_alias_printf(mod, true, "ipack:%s", alias);
996 }
997 
998 /*
999  * Append a match expression for a single masked hex digit.
1000  * outp points to a pointer to the character at which to append.
1001  *	*outp is updated on return to point just after the appended text,
1002  *	to facilitate further appending.
1003  */
1004 static void append_nibble_mask(char **outp,
1005 			       unsigned int nibble, unsigned int mask)
1006 {
1007 	char *p = *outp;
1008 	unsigned int i;
1009 
1010 	switch (mask) {
1011 	case 0:
1012 		*p++ = '?';
1013 		break;
1014 
1015 	case 0xf:
1016 		p += sprintf(p, "%X",  nibble);
1017 		break;
1018 
1019 	default:
1020 		/*
1021 		 * Dumbly emit a match pattern for all possible matching
1022 		 * digits.  This could be improved in some cases using ranges,
1023 		 * but it has the advantage of being trivially correct, and is
1024 		 * often optimal.
1025 		 */
1026 		*p++ = '[';
1027 		for (i = 0; i < 0x10; i++)
1028 			if ((i & mask) == nibble)
1029 				p += sprintf(p, "%X", i);
1030 		*p++ = ']';
1031 	}
1032 
1033 	/* Ensure that the string remains NUL-terminated: */
1034 	*p = '\0';
1035 
1036 	/* Advance the caller's end-of-string pointer: */
1037 	*outp = p;
1038 }
1039 
1040 /*
1041  * looks like: "amba:dN"
1042  *
1043  * N is exactly 8 digits, where each is an upper-case hex digit, or
1044  *	a ? or [] pattern matching exactly one digit.
1045  */
1046 static void do_amba_entry(struct module *mod, void *symval)
1047 {
1048 	char alias[256];
1049 	unsigned int digit;
1050 	char *p = alias;
1051 	DEF_FIELD(symval, amba_id, id);
1052 	DEF_FIELD(symval, amba_id, mask);
1053 
1054 	if ((id & mask) != id)
1055 		fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: id=0x%08X, mask=0x%08X.  Please fix this driver.\n",
1056 		      mod->name, id, mask);
1057 
1058 	for (digit = 0; digit < 8; digit++)
1059 		append_nibble_mask(&p,
1060 				   (id >> (4 * (7 - digit))) & 0xf,
1061 				   (mask >> (4 * (7 - digit))) & 0xf);
1062 
1063 	module_alias_printf(mod, false, "amba:d%s", alias);
1064 }
1065 
1066 /*
1067  * looks like: "mipscdmm:tN"
1068  *
1069  * N is exactly 2 digits, where each is an upper-case hex digit, or
1070  *	a ? or [] pattern matching exactly one digit.
1071  */
1072 static void do_mips_cdmm_entry(struct module *mod, void *symval)
1073 {
1074 	DEF_FIELD(symval, mips_cdmm_device_id, type);
1075 
1076 	module_alias_printf(mod, false, "mipscdmm:t%02X*", type);
1077 }
1078 
1079 /* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
1080  * All fields are numbers. It would be nicer to use strings for vendor
1081  * and feature, but getting those out of the build system here is too
1082  * complicated.
1083  */
1084 
1085 static void do_x86cpu_entry(struct module *mod, void *symval)
1086 {
1087 	char alias[256] = {};
1088 
1089 	DEF_FIELD(symval, x86_cpu_id, feature);
1090 	DEF_FIELD(symval, x86_cpu_id, family);
1091 	DEF_FIELD(symval, x86_cpu_id, model);
1092 	DEF_FIELD(symval, x86_cpu_id, vendor);
1093 
1094 	ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1095 	ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1096 	ADD(alias, "mod", model  != X86_MODEL_ANY,  model);
1097 	strcat(alias, ":feature:*");
1098 	if (feature != X86_FEATURE_ANY)
1099 		sprintf(alias + strlen(alias), "%04X*", feature);
1100 
1101 	module_alias_printf(mod, false, "cpu:type:x86,%s", alias);
1102 }
1103 
1104 /* LOOKS like cpu:type:*:feature:*FEAT* */
1105 static void do_cpu_entry(struct module *mod, void *symval)
1106 {
1107 	DEF_FIELD(symval, cpu_feature, feature);
1108 
1109 	module_alias_printf(mod, false, "cpu:type:*:feature:*%04X*", feature);
1110 }
1111 
1112 /* Looks like: mei:S:uuid:N:* */
1113 static void do_mei_entry(struct module *mod, void *symval)
1114 {
1115 	char alias[256] = {};
1116 
1117 	DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
1118 	DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
1119 	DEF_FIELD(symval, mei_cl_device_id, version);
1120 
1121 	add_uuid(alias, *uuid);
1122 	ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
1123 
1124 	module_alias_printf(mod, false, MEI_CL_MODULE_PREFIX "%s:%s:*",
1125 			    (*name)[0] ? *name : "*", alias);
1126 }
1127 
1128 /* Looks like: rapidio:vNdNavNadN */
1129 static void do_rio_entry(struct module *mod, void *symval)
1130 {
1131 	char alias[256] = {};
1132 
1133 	DEF_FIELD(symval, rio_device_id, did);
1134 	DEF_FIELD(symval, rio_device_id, vid);
1135 	DEF_FIELD(symval, rio_device_id, asm_did);
1136 	DEF_FIELD(symval, rio_device_id, asm_vid);
1137 
1138 	ADD(alias, "v", vid != RIO_ANY_ID, vid);
1139 	ADD(alias, "d", did != RIO_ANY_ID, did);
1140 	ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1141 	ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1142 
1143 	module_alias_printf(mod, true, "rapidio:%s", alias);
1144 }
1145 
1146 /* Looks like: ulpi:vNpN */
1147 static void do_ulpi_entry(struct module *mod, void *symval)
1148 {
1149 	DEF_FIELD(symval, ulpi_device_id, vendor);
1150 	DEF_FIELD(symval, ulpi_device_id, product);
1151 
1152 	module_alias_printf(mod, false, "ulpi:v%04xp%04x", vendor, product);
1153 }
1154 
1155 /* Looks like: hdaudio:vNrNaN */
1156 static void do_hda_entry(struct module *mod, void *symval)
1157 {
1158 	char alias[256] = {};
1159 
1160 	DEF_FIELD(symval, hda_device_id, vendor_id);
1161 	DEF_FIELD(symval, hda_device_id, rev_id);
1162 	DEF_FIELD(symval, hda_device_id, api_version);
1163 
1164 	ADD(alias, "v", vendor_id != 0, vendor_id);
1165 	ADD(alias, "r", rev_id != 0, rev_id);
1166 	ADD(alias, "a", api_version != 0, api_version);
1167 
1168 	module_alias_printf(mod, true, "hdaudio:%s", alias);
1169 }
1170 
1171 /* Looks like: sdw:mNpNvNcN */
1172 static void do_sdw_entry(struct module *mod, void *symval)
1173 {
1174 	char alias[256] = {};
1175 
1176 	DEF_FIELD(symval, sdw_device_id, mfg_id);
1177 	DEF_FIELD(symval, sdw_device_id, part_id);
1178 	DEF_FIELD(symval, sdw_device_id, sdw_version);
1179 	DEF_FIELD(symval, sdw_device_id, class_id);
1180 
1181 	ADD(alias, "m", mfg_id != 0, mfg_id);
1182 	ADD(alias, "p", part_id != 0, part_id);
1183 	ADD(alias, "v", sdw_version != 0, sdw_version);
1184 	ADD(alias, "c", class_id != 0, class_id);
1185 
1186 	module_alias_printf(mod, true, "sdw:%s", alias);
1187 }
1188 
1189 /* Looks like: fsl-mc:vNdN */
1190 static void do_fsl_mc_entry(struct module *mod, void *symval)
1191 {
1192 	DEF_FIELD(symval, fsl_mc_device_id, vendor);
1193 	DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1194 
1195 	module_alias_printf(mod, false, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1196 }
1197 
1198 /* Looks like: tbsvc:kSpNvNrN */
1199 static void do_tbsvc_entry(struct module *mod, void *symval)
1200 {
1201 	char alias[256] = {};
1202 
1203 	DEF_FIELD(symval, tb_service_id, match_flags);
1204 	DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1205 	DEF_FIELD(symval, tb_service_id, protocol_id);
1206 	DEF_FIELD(symval, tb_service_id, protocol_version);
1207 	DEF_FIELD(symval, tb_service_id, protocol_revision);
1208 
1209 	if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1210 		sprintf(alias + strlen(alias), "k%s", *protocol_key);
1211 	else
1212 		strcat(alias + strlen(alias), "k*");
1213 	ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1214 	ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1215 	    protocol_version);
1216 	ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1217 	    protocol_revision);
1218 
1219 	module_alias_printf(mod, true, "tbsvc:%s", alias);
1220 }
1221 
1222 /* Looks like: typec:idNmN */
1223 static void do_typec_entry(struct module *mod, void *symval)
1224 {
1225 	char alias[256] = {};
1226 
1227 	DEF_FIELD(symval, typec_device_id, svid);
1228 	DEF_FIELD(symval, typec_device_id, mode);
1229 
1230 	ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1231 
1232 	module_alias_printf(mod, false, "typec:id%04X%s", svid, alias);
1233 }
1234 
1235 /* Looks like: tee:uuid */
1236 static void do_tee_entry(struct module *mod, void *symval)
1237 {
1238 	DEF_FIELD_ADDR(symval, tee_client_device_id, uuid);
1239 
1240 	module_alias_printf(mod, true,
1241 			    "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1242 		uuid->b[0], uuid->b[1], uuid->b[2], uuid->b[3], uuid->b[4],
1243 		uuid->b[5], uuid->b[6], uuid->b[7], uuid->b[8], uuid->b[9],
1244 		uuid->b[10], uuid->b[11], uuid->b[12], uuid->b[13], uuid->b[14],
1245 		uuid->b[15]);
1246 }
1247 
1248 /* Looks like: wmi:guid */
1249 static void do_wmi_entry(struct module *mod, void *symval)
1250 {
1251 	DEF_FIELD_ADDR(symval, wmi_device_id, guid_string);
1252 
1253 	if (strlen(*guid_string) != UUID_STRING_LEN) {
1254 		warn("Invalid WMI device id 'wmi:%s' in '%s'\n",
1255 				*guid_string, mod->name);
1256 		return;
1257 	}
1258 
1259 	module_alias_printf(mod, false, WMI_MODULE_PREFIX "%s", *guid_string);
1260 }
1261 
1262 /* Looks like: mhi:S */
1263 static void do_mhi_entry(struct module *mod, void *symval)
1264 {
1265 	DEF_FIELD_ADDR(symval, mhi_device_id, chan);
1266 	module_alias_printf(mod, false, MHI_DEVICE_MODALIAS_FMT, *chan);
1267 }
1268 
1269 /* Looks like: mhi_ep:S */
1270 static void do_mhi_ep_entry(struct module *mod, void *symval)
1271 {
1272 	DEF_FIELD_ADDR(symval, mhi_device_id, chan);
1273 
1274 	module_alias_printf(mod, false, MHI_EP_DEVICE_MODALIAS_FMT, *chan);
1275 }
1276 
1277 /* Looks like: ishtp:{guid} */
1278 static void do_ishtp_entry(struct module *mod, void *symval)
1279 {
1280 	char alias[256] = {};
1281 	DEF_FIELD_ADDR(symval, ishtp_device_id, guid);
1282 
1283 	add_guid(alias, *guid);
1284 
1285 	module_alias_printf(mod, false, ISHTP_MODULE_PREFIX "{%s}", alias);
1286 }
1287 
1288 static void do_auxiliary_entry(struct module *mod, void *symval)
1289 {
1290 	DEF_FIELD_ADDR(symval, auxiliary_device_id, name);
1291 
1292 	module_alias_printf(mod, false, AUXILIARY_MODULE_PREFIX "%s", *name);
1293 }
1294 
1295 /*
1296  * Looks like: ssam:dNcNtNiNfN
1297  *
1298  * N is exactly 2 digits, where each is an upper-case hex digit.
1299  */
1300 static void do_ssam_entry(struct module *mod, void *symval)
1301 {
1302 	char alias[256] = {};
1303 
1304 	DEF_FIELD(symval, ssam_device_id, match_flags);
1305 	DEF_FIELD(symval, ssam_device_id, domain);
1306 	DEF_FIELD(symval, ssam_device_id, category);
1307 	DEF_FIELD(symval, ssam_device_id, target);
1308 	DEF_FIELD(symval, ssam_device_id, instance);
1309 	DEF_FIELD(symval, ssam_device_id, function);
1310 
1311 	ADD(alias, "t", match_flags & SSAM_MATCH_TARGET, target);
1312 	ADD(alias, "i", match_flags & SSAM_MATCH_INSTANCE, instance);
1313 	ADD(alias, "f", match_flags & SSAM_MATCH_FUNCTION, function);
1314 
1315 	module_alias_printf(mod, false, "ssam:d%02Xc%02X%s",
1316 			    domain, category, alias);
1317 }
1318 
1319 /* Looks like: dfl:tNfN */
1320 static void do_dfl_entry(struct module *mod, void *symval)
1321 {
1322 	DEF_FIELD(symval, dfl_device_id, type);
1323 	DEF_FIELD(symval, dfl_device_id, feature_id);
1324 
1325 	module_alias_printf(mod, true, "dfl:t%04Xf%04X", type, feature_id);
1326 }
1327 
1328 /* Looks like: cdx:vNdN */
1329 static void do_cdx_entry(struct module *mod, void *symval)
1330 {
1331 	char alias[256];
1332 
1333 	DEF_FIELD(symval, cdx_device_id, vendor);
1334 	DEF_FIELD(symval, cdx_device_id, device);
1335 	DEF_FIELD(symval, cdx_device_id, subvendor);
1336 	DEF_FIELD(symval, cdx_device_id, subdevice);
1337 	DEF_FIELD(symval, cdx_device_id, class);
1338 	DEF_FIELD(symval, cdx_device_id, class_mask);
1339 	DEF_FIELD(symval, cdx_device_id, override_only);
1340 
1341 	switch (override_only) {
1342 	case 0:
1343 		strcpy(alias, "cdx:");
1344 		break;
1345 	case CDX_ID_F_VFIO_DRIVER_OVERRIDE:
1346 		strcpy(alias, "vfio_cdx:");
1347 		break;
1348 	default:
1349 		warn("Unknown CDX driver_override alias %08X\n",
1350 		     override_only);
1351 		return;
1352 	}
1353 
1354 	ADD(alias, "v", vendor != CDX_ANY_ID, vendor);
1355 	ADD(alias, "d", device != CDX_ANY_ID, device);
1356 	ADD(alias, "sv", subvendor != CDX_ANY_ID, subvendor);
1357 	ADD(alias, "sd", subdevice != CDX_ANY_ID, subdevice);
1358 	ADD(alias, "c", class_mask == 0xFFFFFF, class);
1359 
1360 	module_alias_printf(mod, false, "%s", alias);
1361 }
1362 
1363 static void do_vchiq_entry(struct module *mod, void *symval)
1364 {
1365 	DEF_FIELD_ADDR(symval, vchiq_device_id, name);
1366 
1367 	module_alias_printf(mod, false, "vchiq:%s", *name);
1368 }
1369 
1370 /* Looks like: coreboot:tN */
1371 static void do_coreboot_entry(struct module *mod, void *symval)
1372 {
1373 	DEF_FIELD(symval, coreboot_device_id, tag);
1374 
1375 	module_alias_printf(mod, false, "coreboot:t%08X", tag);
1376 }
1377 
1378 /* Does namelen bytes of name exactly match the symbol? */
1379 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1380 {
1381 	if (namelen != strlen(symbol))
1382 		return false;
1383 
1384 	return memcmp(name, symbol, namelen) == 0;
1385 }
1386 
1387 static void do_table(const char *name, void *symval, unsigned long size,
1388 		     unsigned long id_size,
1389 		     const char *device_id,
1390 		     void (*do_entry)(struct module *mod, void *symval),
1391 		     struct module *mod)
1392 {
1393 	unsigned int i;
1394 
1395 	if (size % id_size || size < id_size) {
1396 		error("%s: type mismatch between %s[] and MODULE_DEVICE_TABLE(%s, ...)\n",
1397 		      mod->name, name, device_id);
1398 		return;
1399 	}
1400 
1401 	/* Verify the last entry is a terminator */
1402 	for (i = size - id_size; i < size; i++) {
1403 		if (*(uint8_t *)(symval + i)) {
1404 			error("%s: %s[] is not terminated with a NULL entry\n",
1405 			      mod->name, name);
1406 			return;
1407 		}
1408 	}
1409 
1410 	/* Leave last one: it's the terminator. */
1411 	size -= id_size;
1412 
1413 	for (i = 0; i < size; i += id_size)
1414 		do_entry(mod, symval + i);
1415 }
1416 
1417 static const struct devtable devtable[] = {
1418 	{"hid", SIZE_hid_device_id, do_hid_entry},
1419 	{"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1420 	{"pci", SIZE_pci_device_id, do_pci_entry},
1421 	{"ccw", SIZE_ccw_device_id, do_ccw_entry},
1422 	{"ap", SIZE_ap_device_id, do_ap_entry},
1423 	{"css", SIZE_css_device_id, do_css_entry},
1424 	{"serio", SIZE_serio_device_id, do_serio_entry},
1425 	{"acpi", SIZE_acpi_device_id, do_acpi_entry},
1426 	{"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1427 	{"vio", SIZE_vio_device_id, do_vio_entry},
1428 	{"input", SIZE_input_device_id, do_input_entry},
1429 	{"eisa", SIZE_eisa_device_id, do_eisa_entry},
1430 	{"parisc", SIZE_parisc_device_id, do_parisc_entry},
1431 	{"sdio", SIZE_sdio_device_id, do_sdio_entry},
1432 	{"ssb", SIZE_ssb_device_id, do_ssb_entry},
1433 	{"bcma", SIZE_bcma_device_id, do_bcma_entry},
1434 	{"virtio", SIZE_virtio_device_id, do_virtio_entry},
1435 	{"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1436 	{"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1437 	{"i2c", SIZE_i2c_device_id, do_i2c_entry},
1438 	{"i3c", SIZE_i3c_device_id, do_i3c_entry},
1439 	{"slim", SIZE_slim_device_id, do_slim_entry},
1440 	{"spi", SIZE_spi_device_id, do_spi_entry},
1441 	{"dmi", SIZE_dmi_system_id, do_dmi_entry},
1442 	{"platform", SIZE_platform_device_id, do_platform_entry},
1443 	{"mdio", SIZE_mdio_device_id, do_mdio_entry},
1444 	{"zorro", SIZE_zorro_device_id, do_zorro_entry},
1445 	{"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1446 	{"ipack", SIZE_ipack_device_id, do_ipack_entry},
1447 	{"amba", SIZE_amba_id, do_amba_entry},
1448 	{"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1449 	{"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1450 	{"cpu", SIZE_cpu_feature, do_cpu_entry},
1451 	{"mei", SIZE_mei_cl_device_id, do_mei_entry},
1452 	{"rapidio", SIZE_rio_device_id, do_rio_entry},
1453 	{"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1454 	{"hdaudio", SIZE_hda_device_id, do_hda_entry},
1455 	{"sdw", SIZE_sdw_device_id, do_sdw_entry},
1456 	{"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1457 	{"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1458 	{"typec", SIZE_typec_device_id, do_typec_entry},
1459 	{"tee", SIZE_tee_client_device_id, do_tee_entry},
1460 	{"wmi", SIZE_wmi_device_id, do_wmi_entry},
1461 	{"mhi", SIZE_mhi_device_id, do_mhi_entry},
1462 	{"mhi_ep", SIZE_mhi_device_id, do_mhi_ep_entry},
1463 	{"auxiliary", SIZE_auxiliary_device_id, do_auxiliary_entry},
1464 	{"ssam", SIZE_ssam_device_id, do_ssam_entry},
1465 	{"dfl", SIZE_dfl_device_id, do_dfl_entry},
1466 	{"ishtp", SIZE_ishtp_device_id, do_ishtp_entry},
1467 	{"cdx", SIZE_cdx_device_id, do_cdx_entry},
1468 	{"vchiq", SIZE_vchiq_device_id, do_vchiq_entry},
1469 	{"coreboot", SIZE_coreboot_device_id, do_coreboot_entry},
1470 	{"of", SIZE_of_device_id, do_of_entry},
1471 	{"usb", SIZE_usb_device_id, do_usb_entry_multi},
1472 	{"pnp", SIZE_pnp_device_id, do_pnp_device_entry},
1473 	{"pnp_card", SIZE_pnp_card_device_id, do_pnp_card_entry},
1474 };
1475 
1476 /* Create MODULE_ALIAS() statements.
1477  * At this time, we cannot write the actual output C source yet,
1478  * so we write into the mod->dev_table_buf buffer. */
1479 void handle_moddevtable(struct module *mod, struct elf_info *info,
1480 			Elf_Sym *sym, const char *symname)
1481 {
1482 	void *symval;
1483 	char *zeros = NULL;
1484 	const char *type, *name;
1485 	size_t typelen;
1486 	static const char *prefix = "__mod_device_table__";
1487 
1488 	/* We're looking for a section relative symbol */
1489 	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1490 		return;
1491 
1492 	/* We're looking for an object */
1493 	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1494 		return;
1495 
1496 	/* All our symbols are of form __mod_device_table__<type>__<name>. */
1497 	if (!strstarts(symname, prefix))
1498 		return;
1499 	type = symname + strlen(prefix);
1500 
1501 	name = strstr(type, "__");
1502 	if (!name)
1503 		return;
1504 	typelen = name - type;
1505 	name += strlen("__");
1506 
1507 	/* Handle all-NULL symbols allocated into .bss */
1508 	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1509 		zeros = calloc(1, sym->st_size);
1510 		symval = zeros;
1511 	} else {
1512 		symval = sym_get_data(info, sym);
1513 	}
1514 
1515 	for (int i = 0; i < ARRAY_SIZE(devtable); i++) {
1516 		const struct devtable *p = &devtable[i];
1517 
1518 		if (sym_is(type, typelen, p->device_id)) {
1519 			do_table(name, symval, sym->st_size, p->id_size,
1520 				 p->device_id, p->do_entry, mod);
1521 			break;
1522 		}
1523 	}
1524 
1525 	free(zeros);
1526 }
1527