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.
5 * Copyright 2002-2003 Rusty Russell, IBM Corporation
6 * 2003 Kai Germaschewski
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
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
;
20 typedef Elf64_Addr kernel_ulong_t
;
30 typedef uint32_t __u32
;
31 typedef uint16_t __u16
;
32 typedef unsigned char __u8
;
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"
39 #define ADD(str, sep, cond, field) \
43 sprintf(str + strlen(str), \
44 sizeof(field) == 1 ? "%02X" : \
45 sizeof(field) == 2 ? "%04X" : \
46 sizeof(field) == 4 ? "%08X" : "", \
49 sprintf(str + strlen(str), "*"); \
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
,
60 strcpy(alias
, "usb:");
61 ADD(alias
, "v", id
->match_flags
&USB_DEVICE_ID_MATCH_VENDOR
,
63 ADD(alias
, "p", id
->match_flags
&USB_DEVICE_ID_MATCH_PRODUCT
,
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))
77 ADD(alias
, "dc", id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_CLASS
,
80 id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_SUBCLASS
,
83 id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_PROTOCOL
,
86 id
->match_flags
&USB_DEVICE_ID_MATCH_INT_CLASS
,
89 id
->match_flags
&USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
90 id
->bInterfaceSubClass
);
92 id
->match_flags
&USB_DEVICE_ID_MATCH_INT_PROTOCOL
,
93 id
->bInterfaceProtocol
);
95 /* Always end in a wildcard, for future extension */
96 if (alias
[strlen(alias
)-1] != '*')
98 buf_printf(&mod
->dev_table_buf
,
99 "MODULE_ALIAS(\"%s\");\n", alias
);
102 static void do_usb_entry_multi(struct usb_device_id
*id
, struct module
*mod
)
104 unsigned int devlo
, devhi
;
105 unsigned char chi
, clo
;
108 id
->match_flags
= TO_NATIVE(id
->match_flags
);
109 id
->idVendor
= TO_NATIVE(id
->idVendor
);
110 id
->idProduct
= TO_NATIVE(id
->idProduct
);
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
;
118 * Some modules (visor) have empty slots as placeholder for
119 * run-time specification that results in catch-all alias
121 if (!(id
->idVendor
| id
->bDeviceClass
| id
->bInterfaceClass
))
124 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
125 for (ndigits
= sizeof(id
->bcdDevice_lo
) * 2 - 1; devlo
<= devhi
; ndigits
--) {
128 if (chi
> 9) /* it's bcd not hex */
133 if (devlo
== devhi
|| !ndigits
) {
134 do_usb_entry(id
, devlo
, ndigits
, clo
, chi
, mod
);
139 do_usb_entry(id
, devlo
++, ndigits
, clo
, 9, mod
);
142 do_usb_entry(id
, devhi
--, ndigits
, 0, chi
, mod
);
146 static void do_usb_table(void *symval
, unsigned long size
,
150 const unsigned long id_size
= sizeof(struct usb_device_id
);
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
);
156 /* Leave last one: it's the terminator. */
159 for (i
= 0; i
< size
; i
+= id_size
)
160 do_usb_entry_multi(symval
+ i
, mod
);
163 /* Looks like: ieee1394:venNmoNspNverN */
164 static int do_ieee1394_entry(const char *filename
,
165 struct ieee1394_device_id
*id
, char *alias
)
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
);
173 strcpy(alias
, "ieee1394:");
174 ADD(alias
, "ven", id
->match_flags
& IEEE1394_MATCH_VENDOR_ID
,
176 ADD(alias
, "mo", id
->match_flags
& IEEE1394_MATCH_MODEL_ID
,
178 ADD(alias
, "sp", id
->match_flags
& IEEE1394_MATCH_SPECIFIER_ID
,
180 ADD(alias
, "ver", id
->match_flags
& IEEE1394_MATCH_VERSION
,
186 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
187 static int do_pci_entry(const char *filename
,
188 struct pci_device_id
*id
, char *alias
)
190 /* Class field can be divided into these three. */
191 unsigned char baseclass
, subclass
, interface
,
192 baseclass_mask
, subclass_mask
, interface_mask
;
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
);
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
);
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
;
214 if ((baseclass_mask
!= 0 && baseclass_mask
!= 0xFF)
215 || (subclass_mask
!= 0 && subclass_mask
!= 0xFF)
216 || (interface_mask
!= 0 && interface_mask
!= 0xFF)) {
218 "*** Warning: Can't handle masks in %s:%04X\n",
219 filename
, id
->class_mask
);
223 ADD(alias
, "bc", baseclass_mask
== 0xFF, baseclass
);
224 ADD(alias
, "sc", subclass_mask
== 0xFF, subclass
);
225 ADD(alias
, "i", interface_mask
== 0xFF, interface
);
229 /* looks like: "ccw:tNmNdtNdmN" */
230 static int do_ccw_entry(const char *filename
,
231 struct ccw_device_id
*id
, char *alias
)
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
);
239 strcpy(alias
, "ccw:");
240 ADD(alias
, "t", id
->match_flags
&CCW_DEVICE_ID_MATCH_CU_TYPE
,
242 ADD(alias
, "m", id
->match_flags
&CCW_DEVICE_ID_MATCH_CU_MODEL
,
244 ADD(alias
, "dt", id
->match_flags
&CCW_DEVICE_ID_MATCH_DEVICE_TYPE
,
246 ADD(alias
, "dm", id
->match_flags
&CCW_DEVICE_ID_MATCH_DEVICE_TYPE
,
251 /* Looks like: "serio:tyNprNidNexN" */
252 static int do_serio_entry(const char *filename
,
253 struct serio_device_id
*id
, char *alias
)
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
);
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
);
269 /* looks like: "pnp:dD" */
270 static int do_pnp_entry(const char *filename
,
271 struct pnp_device_id
*id
, char *alias
)
273 sprintf(alias
, "pnp:d%s", id
->id
);
277 /* looks like: "pnp:cCdD..." */
278 static int do_pnp_card_entry(const char *filename
,
279 struct pnp_card_device_id
*id
, char *alias
)
283 sprintf(alias
, "pnp:c%s", id
->id
);
284 for (i
= 0; i
< PNP_MAX_DEVICES
; i
++) {
285 if (! *id
->devs
[i
].id
)
287 sprintf(alias
+ strlen(alias
), "d%s", id
->devs
[i
].id
);
292 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
293 static int do_pcmcia_entry(const char *filename
,
294 struct pcmcia_device_id
*id
, char *alias
)
298 id
->match_flags
= TO_NATIVE(id
->match_flags
);
299 id
->manf_id
= TO_NATIVE(id
->manf_id
);
300 id
->card_id
= TO_NATIVE(id
->card_id
);
301 id
->func_id
= TO_NATIVE(id
->func_id
);
302 id
->function
= TO_NATIVE(id
->function
);
303 id
->device_no
= TO_NATIVE(id
->device_no
);
305 for (i
=0; i
<4; i
++) {
306 id
->prod_id_hash
[i
] = TO_NATIVE(id
->prod_id_hash
[i
]);
309 strcpy(alias
, "pcmcia:");
310 ADD(alias
, "m", id
->match_flags
& PCMCIA_DEV_ID_MATCH_MANF_ID
,
312 ADD(alias
, "c", id
->match_flags
& PCMCIA_DEV_ID_MATCH_CARD_ID
,
314 ADD(alias
, "f", id
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNC_ID
,
316 ADD(alias
, "fn", id
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNCTION
,
318 ADD(alias
, "pfn", id
->match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
,
320 ADD(alias
, "pa", id
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID1
, id
->prod_id_hash
[0]);
321 ADD(alias
, "pb", id
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID2
, id
->prod_id_hash
[1]);
322 ADD(alias
, "pc", id
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID3
, id
->prod_id_hash
[2]);
323 ADD(alias
, "pd", id
->match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID4
, id
->prod_id_hash
[3]);
330 static int do_of_entry (const char *filename
, struct of_device_id
*of
, char *alias
)
333 sprintf (alias
, "of:N%sT%sC%s",
334 of
->name
[0] ? of
->name
: "*",
335 of
->type
[0] ? of
->type
: "*",
336 of
->compatible
[0] ? of
->compatible
: "*");
338 /* Replace all whitespace with underscores */
339 for (tmp
= alias
; tmp
&& *tmp
; tmp
++)
346 static int do_vio_entry(const char *filename
, struct vio_device_id
*vio
,
351 sprintf(alias
, "vio:T%sS%s", vio
->type
[0] ? vio
->type
: "*",
352 vio
->compat
[0] ? vio
->compat
: "*");
354 /* Replace all whitespace with underscores */
355 for (tmp
= alias
; tmp
&& *tmp
; tmp
++)
362 /* Ignore any prefix, eg. v850 prepends _ */
363 static inline int sym_is(const char *symbol
, const char *name
)
367 match
= strstr(symbol
, name
);
370 return match
[strlen(symbol
)] == '\0';
373 static void do_table(void *symval
, unsigned long size
,
374 unsigned long id_size
,
380 int (*do_entry
)(const char *, void *entry
, char *alias
) = function
;
382 if (size
% id_size
|| size
< id_size
) {
383 fprintf(stderr
, "*** Warning: %s ids %lu bad size "
384 "(each on %lu)\n", mod
->name
, size
, id_size
);
386 /* Leave last one: it's the terminator. */
389 for (i
= 0; i
< size
; i
+= id_size
) {
390 if (do_entry(mod
->name
, symval
+i
, alias
)) {
391 /* Always end in a wildcard, for future extension */
392 if (alias
[strlen(alias
)-1] != '*')
394 buf_printf(&mod
->dev_table_buf
,
395 "MODULE_ALIAS(\"%s\");\n", alias
);
400 /* Create MODULE_ALIAS() statements.
401 * At this time, we cannot write the actual output C source yet,
402 * so we write into the mod->dev_table_buf buffer. */
403 void handle_moddevtable(struct module
*mod
, struct elf_info
*info
,
404 Elf_Sym
*sym
, const char *symname
)
408 /* We're looking for a section relative symbol */
409 if (!sym
->st_shndx
|| sym
->st_shndx
>= info
->hdr
->e_shnum
)
412 symval
= (void *)info
->hdr
413 + info
->sechdrs
[sym
->st_shndx
].sh_offset
416 if (sym_is(symname
, "__mod_pci_device_table"))
417 do_table(symval
, sym
->st_size
, sizeof(struct pci_device_id
),
419 else if (sym_is(symname
, "__mod_usb_device_table"))
420 /* special case to handle bcdDevice ranges */
421 do_usb_table(symval
, sym
->st_size
, mod
);
422 else if (sym_is(symname
, "__mod_ieee1394_device_table"))
423 do_table(symval
, sym
->st_size
, sizeof(struct ieee1394_device_id
),
424 do_ieee1394_entry
, mod
);
425 else if (sym_is(symname
, "__mod_ccw_device_table"))
426 do_table(symval
, sym
->st_size
, sizeof(struct ccw_device_id
),
428 else if (sym_is(symname
, "__mod_serio_device_table"))
429 do_table(symval
, sym
->st_size
, sizeof(struct serio_device_id
),
430 do_serio_entry
, mod
);
431 else if (sym_is(symname
, "__mod_pnp_device_table"))
432 do_table(symval
, sym
->st_size
, sizeof(struct pnp_device_id
),
434 else if (sym_is(symname
, "__mod_pnp_card_device_table"))
435 do_table(symval
, sym
->st_size
, sizeof(struct pnp_card_device_id
),
436 do_pnp_card_entry
, mod
);
437 else if (sym_is(symname
, "__mod_pcmcia_device_table"))
438 do_table(symval
, sym
->st_size
, sizeof(struct pcmcia_device_id
),
439 do_pcmcia_entry
, mod
);
440 else if (sym_is(symname
, "__mod_of_device_table"))
441 do_table(symval
, sym
->st_size
, sizeof(struct of_device_id
),
443 else if (sym_is(symname
, "__mod_vio_device_table"))
444 do_table(symval
, sym
->st_size
, sizeof(struct vio_device_id
),
449 /* Now add out buffered information to the generated C source */
450 void add_moddevtable(struct buffer
*buf
, struct module
*mod
)
452 buf_printf(buf
, "\n");
453 buf_write(buf
, mod
->dev_table_buf
.p
, mod
->dev_table_buf
.pos
);
454 free(mod
->dev_table_buf
.p
);