xref: /linux/scripts/mod/file2alias.c (revision 13abf8130139c2ccd4962a7e5a8902be5e6cb5a7)
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 
15 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
16  * use either stdint.h or inttypes.h for the rest. */
17 #if KERNEL_ELFCLASS == ELFCLASS32
18 typedef Elf32_Addr	kernel_ulong_t;
19 #else
20 typedef Elf64_Addr	kernel_ulong_t;
21 #endif
22 #ifdef __sun__
23 #include <inttypes.h>
24 #else
25 #include <stdint.h>
26 #endif
27 
28 #include <ctype.h>
29 
30 typedef uint32_t	__u32;
31 typedef uint16_t	__u16;
32 typedef unsigned char	__u8;
33 
34 /* Big exception to the "don't include kernel headers into userspace, which
35  * even potentially has different endianness and word sizes, since
36  * we handle those differences explicitly below */
37 #include "../../include/linux/mod_devicetable.h"
38 
39 #define ADD(str, sep, cond, field)                              \
40 do {                                                            \
41         strcat(str, sep);                                       \
42         if (cond)                                               \
43                 sprintf(str + strlen(str),                      \
44                         sizeof(field) == 1 ? "%02X" :           \
45                         sizeof(field) == 2 ? "%04X" :           \
46                         sizeof(field) == 4 ? "%08X" : "",       \
47                         field);                                 \
48         else                                                    \
49                 sprintf(str + strlen(str), "*");                \
50 } while(0)
51 
52 /* USB is special because the bcdDevice can be matched against a numeric range */
53 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
54 static void do_usb_entry(struct usb_device_id *id,
55 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
56 			 unsigned char range_lo, unsigned char range_hi,
57 			 struct module *mod)
58 {
59 	char alias[500];
60 	strcpy(alias, "usb:");
61 	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
62 	    id->idVendor);
63 	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
64 	    id->idProduct);
65 
66 	strcat(alias, "d");
67 	if (bcdDevice_initial_digits)
68 		sprintf(alias + strlen(alias), "%0*X",
69 			bcdDevice_initial_digits, bcdDevice_initial);
70 	if (range_lo == range_hi)
71 		sprintf(alias + strlen(alias), "%u", range_lo);
72 	else if (range_lo > 0 || range_hi < 9)
73 		sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
74 	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
75 		strcat(alias, "*");
76 
77 	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
78 	    id->bDeviceClass);
79 	ADD(alias, "dsc",
80 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
81 	    id->bDeviceSubClass);
82 	ADD(alias, "dp",
83 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
84 	    id->bDeviceProtocol);
85 	ADD(alias, "ic",
86 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
87 	    id->bInterfaceClass);
88 	ADD(alias, "isc",
89 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
90 	    id->bInterfaceSubClass);
91 	ADD(alias, "ip",
92 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
93 	    id->bInterfaceProtocol);
94 
95 	/* Always end in a wildcard, for future extension */
96 	if (alias[strlen(alias)-1] != '*')
97 		strcat(alias, "*");
98 	buf_printf(&mod->dev_table_buf,
99 		   "MODULE_ALIAS(\"%s\");\n", alias);
100 }
101 
102 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
103 {
104 	unsigned int devlo, devhi;
105 	unsigned char chi, clo;
106 	int ndigits;
107 
108 	id->match_flags = TO_NATIVE(id->match_flags);
109 	id->idVendor = TO_NATIVE(id->idVendor);
110 	id->idProduct = TO_NATIVE(id->idProduct);
111 
112 	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
113 		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
114 	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
115 		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
116 
117 	/*
118 	 * Some modules (visor) have empty slots as placeholder for
119 	 * run-time specification that results in catch-all alias
120 	 */
121 	if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
122 		return;
123 
124 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
125 	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
126 		clo = devlo & 0xf;
127 		chi = devhi & 0xf;
128 		if (chi > 9)	/* it's bcd not hex */
129 			chi = 9;
130 		devlo >>= 4;
131 		devhi >>= 4;
132 
133 		if (devlo == devhi || !ndigits) {
134 			do_usb_entry(id, devlo, ndigits, clo, chi, mod);
135 			break;
136 		}
137 
138 		if (clo > 0)
139 			do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
140 
141 		if (chi < 9)
142 			do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
143 	}
144 }
145 
146 static void do_usb_table(void *symval, unsigned long size,
147 			 struct module *mod)
148 {
149 	unsigned int i;
150 	const unsigned long id_size = sizeof(struct usb_device_id);
151 
152 	if (size % id_size || size < id_size) {
153 		fprintf(stderr, "*** Warning: %s ids %lu bad size "
154 			"(each on %lu)\n", mod->name, size, id_size);
155 	}
156 	/* Leave last one: it's the terminator. */
157 	size -= id_size;
158 
159 	for (i = 0; i < size; i += id_size)
160 		do_usb_entry_multi(symval + i, mod);
161 }
162 
163 /* Looks like: ieee1394:venNmoNspNverN */
164 static int do_ieee1394_entry(const char *filename,
165 			     struct ieee1394_device_id *id, char *alias)
166 {
167 	id->match_flags = TO_NATIVE(id->match_flags);
168 	id->vendor_id = TO_NATIVE(id->vendor_id);
169 	id->model_id = TO_NATIVE(id->model_id);
170 	id->specifier_id = TO_NATIVE(id->specifier_id);
171 	id->version = TO_NATIVE(id->version);
172 
173 	strcpy(alias, "ieee1394:");
174 	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
175 	    id->vendor_id);
176 	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
177 	    id->model_id);
178 	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
179 	    id->specifier_id);
180 	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
181 	    id->version);
182 
183 	return 1;
184 }
185 
186 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
187 static int do_pci_entry(const char *filename,
188 			struct pci_device_id *id, char *alias)
189 {
190 	/* Class field can be divided into these three. */
191 	unsigned char baseclass, subclass, interface,
192 		baseclass_mask, subclass_mask, interface_mask;
193 
194 	id->vendor = TO_NATIVE(id->vendor);
195 	id->device = TO_NATIVE(id->device);
196 	id->subvendor = TO_NATIVE(id->subvendor);
197 	id->subdevice = TO_NATIVE(id->subdevice);
198 	id->class = TO_NATIVE(id->class);
199 	id->class_mask = TO_NATIVE(id->class_mask);
200 
201 	strcpy(alias, "pci:");
202 	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
203 	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
204 	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
205 	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
206 
207 	baseclass = (id->class) >> 16;
208 	baseclass_mask = (id->class_mask) >> 16;
209 	subclass = (id->class) >> 8;
210 	subclass_mask = (id->class_mask) >> 8;
211 	interface = id->class;
212 	interface_mask = id->class_mask;
213 
214 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
215 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
216 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
217 		fprintf(stderr,
218 			"*** Warning: Can't handle masks in %s:%04X\n",
219 			filename, id->class_mask);
220 		return 0;
221 	}
222 
223 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
224 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
225 	ADD(alias, "i", interface_mask == 0xFF, interface);
226 	return 1;
227 }
228 
229 /* looks like: "ccw:tNmNdtNdmN" */
230 static int do_ccw_entry(const char *filename,
231 			struct ccw_device_id *id, char *alias)
232 {
233 	id->match_flags = TO_NATIVE(id->match_flags);
234 	id->cu_type = TO_NATIVE(id->cu_type);
235 	id->cu_model = TO_NATIVE(id->cu_model);
236 	id->dev_type = TO_NATIVE(id->dev_type);
237 	id->dev_model = TO_NATIVE(id->dev_model);
238 
239 	strcpy(alias, "ccw:");
240 	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
241 	    id->cu_type);
242 	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
243 	    id->cu_model);
244 	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
245 	    id->dev_type);
246 	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
247 	    id->dev_model);
248 	return 1;
249 }
250 
251 /* Looks like: "serio:tyNprNidNexN" */
252 static int do_serio_entry(const char *filename,
253 			  struct serio_device_id *id, char *alias)
254 {
255 	id->type = TO_NATIVE(id->type);
256 	id->proto = TO_NATIVE(id->proto);
257 	id->id = TO_NATIVE(id->id);
258 	id->extra = TO_NATIVE(id->extra);
259 
260 	strcpy(alias, "serio:");
261 	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
262 	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
263 	ADD(alias, "id", id->id != SERIO_ANY, id->id);
264 	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
265 
266 	return 1;
267 }
268 
269 /* looks like: "pnp:dD" */
270 static int do_pnp_entry(const char *filename,
271 			struct pnp_device_id *id, char *alias)
272 {
273 	sprintf(alias, "pnp:d%s", id->id);
274 	return 1;
275 }
276 
277 /* looks like: "pnp:cCdD..." */
278 static int do_pnp_card_entry(const char *filename,
279 			struct pnp_card_device_id *id, char *alias)
280 {
281 	int i;
282 
283 	sprintf(alias, "pnp:c%s", id->id);
284 	for (i = 0; i < PNP_MAX_DEVICES; i++) {
285 		if (! *id->devs[i].id)
286 			break;
287 		sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
288 	}
289 	return 1;
290 }
291 
292 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
293 static int do_pcmcia_entry(const char *filename,
294 			   struct pcmcia_device_id *id, char *alias)
295 {
296 	unsigned int i;
297 
298 	id->manf_id = TO_NATIVE(id->manf_id);
299 	id->card_id = TO_NATIVE(id->card_id);
300 	id->func_id = TO_NATIVE(id->func_id);
301 	id->function = TO_NATIVE(id->function);
302 	id->device_no = TO_NATIVE(id->device_no);
303 	for (i=0; i<4; i++) {
304 		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
305        }
306 
307        strcpy(alias, "pcmcia:");
308        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
309 	   id->manf_id);
310        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
311 	   id->card_id);
312        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
313 	   id->func_id);
314        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
315 	   id->function);
316        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
317 	   id->device_no);
318        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
319        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
320        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
321        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
322 
323        return 1;
324 }
325 
326 
327 
328 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
329 {
330     char *tmp;
331     sprintf (alias, "of:N%sT%sC%s",
332                     of->name[0] ? of->name : "*",
333                     of->type[0] ? of->type : "*",
334                     of->compatible[0] ? of->compatible : "*");
335 
336     /* Replace all whitespace with underscores */
337     for (tmp = alias; tmp && *tmp; tmp++)
338         if (isspace (*tmp))
339             *tmp = '_';
340 
341     return 1;
342 }
343 
344 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
345 		char *alias)
346 {
347 	char *tmp;
348 
349 	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
350 			vio->compat[0] ? vio->compat : "*");
351 
352 	/* Replace all whitespace with underscores */
353 	for (tmp = alias; tmp && *tmp; tmp++)
354 		if (isspace (*tmp))
355 			*tmp = '_';
356 
357 	return 1;
358 }
359 
360 /* Ignore any prefix, eg. v850 prepends _ */
361 static inline int sym_is(const char *symbol, const char *name)
362 {
363 	const char *match;
364 
365 	match = strstr(symbol, name);
366 	if (!match)
367 		return 0;
368 	return match[strlen(symbol)] == '\0';
369 }
370 
371 static void do_table(void *symval, unsigned long size,
372 		     unsigned long id_size,
373 		     void *function,
374 		     struct module *mod)
375 {
376 	unsigned int i;
377 	char alias[500];
378 	int (*do_entry)(const char *, void *entry, char *alias) = function;
379 
380 	if (size % id_size || size < id_size) {
381 		fprintf(stderr, "*** Warning: %s ids %lu bad size "
382 			"(each on %lu)\n", mod->name, size, id_size);
383 	}
384 	/* Leave last one: it's the terminator. */
385 	size -= id_size;
386 
387 	for (i = 0; i < size; i += id_size) {
388 		if (do_entry(mod->name, symval+i, alias)) {
389 			/* Always end in a wildcard, for future extension */
390 			if (alias[strlen(alias)-1] != '*')
391 				strcat(alias, "*");
392 			buf_printf(&mod->dev_table_buf,
393 				   "MODULE_ALIAS(\"%s\");\n", alias);
394 		}
395 	}
396 }
397 
398 /* Create MODULE_ALIAS() statements.
399  * At this time, we cannot write the actual output C source yet,
400  * so we write into the mod->dev_table_buf buffer. */
401 void handle_moddevtable(struct module *mod, struct elf_info *info,
402 			Elf_Sym *sym, const char *symname)
403 {
404 	void *symval;
405 
406 	/* We're looking for a section relative symbol */
407 	if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
408 		return;
409 
410 	symval = (void *)info->hdr
411 		+ info->sechdrs[sym->st_shndx].sh_offset
412 		+ sym->st_value;
413 
414 	if (sym_is(symname, "__mod_pci_device_table"))
415 		do_table(symval, sym->st_size, sizeof(struct pci_device_id),
416 			 do_pci_entry, mod);
417 	else if (sym_is(symname, "__mod_usb_device_table"))
418 		/* special case to handle bcdDevice ranges */
419 		do_usb_table(symval, sym->st_size, mod);
420 	else if (sym_is(symname, "__mod_ieee1394_device_table"))
421 		do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
422 			 do_ieee1394_entry, mod);
423 	else if (sym_is(symname, "__mod_ccw_device_table"))
424 		do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
425 			 do_ccw_entry, mod);
426 	else if (sym_is(symname, "__mod_serio_device_table"))
427 		do_table(symval, sym->st_size, sizeof(struct serio_device_id),
428 			 do_serio_entry, mod);
429 	else if (sym_is(symname, "__mod_pnp_device_table"))
430 		do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
431 			 do_pnp_entry, mod);
432 	else if (sym_is(symname, "__mod_pnp_card_device_table"))
433 		do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
434 			 do_pnp_card_entry, mod);
435 	else if (sym_is(symname, "__mod_pcmcia_device_table"))
436 		do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
437 			 do_pcmcia_entry, mod);
438         else if (sym_is(symname, "__mod_of_device_table"))
439 		do_table(symval, sym->st_size, sizeof(struct of_device_id),
440 			 do_of_entry, mod);
441         else if (sym_is(symname, "__mod_vio_device_table"))
442 		do_table(symval, sym->st_size, sizeof(struct vio_device_id),
443 			 do_vio_entry, mod);
444 
445 }
446 
447 /* Now add out buffered information to the generated C source */
448 void add_moddevtable(struct buffer *buf, struct module *mod)
449 {
450 	buf_printf(buf, "\n");
451 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
452 	free(mod->dev_table_buf.p);
453 }
454