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
->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
]);
307 strcpy(alias
, "pcmcia:");
308 ADD(alias
, "m", id
->match_flags
& PCMCIA_DEV_ID_MATCH_MANF_ID
,
310 ADD(alias
, "c", id
->match_flags
& PCMCIA_DEV_ID_MATCH_CARD_ID
,
312 ADD(alias
, "f", id
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNC_ID
,
314 ADD(alias
, "fn", id
->match_flags
& PCMCIA_DEV_ID_MATCH_FUNCTION
,
316 ADD(alias
, "pfn", id
->match_flags
& PCMCIA_DEV_ID_MATCH_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]);
328 static int do_of_entry (const char *filename
, struct of_device_id
*of
, char *alias
)
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
: "*");
336 /* Replace all whitespace with underscores */
337 for (tmp
= alias
; tmp
&& *tmp
; tmp
++)
344 /* Ignore any prefix, eg. v850 prepends _ */
345 static inline int sym_is(const char *symbol
, const char *name
)
349 match
= strstr(symbol
, name
);
352 return match
[strlen(symbol
)] == '\0';
355 static void do_table(void *symval
, unsigned long size
,
356 unsigned long id_size
,
362 int (*do_entry
)(const char *, void *entry
, char *alias
) = function
;
364 if (size
% id_size
|| size
< id_size
) {
365 fprintf(stderr
, "*** Warning: %s ids %lu bad size "
366 "(each on %lu)\n", mod
->name
, size
, id_size
);
368 /* Leave last one: it's the terminator. */
371 for (i
= 0; i
< size
; i
+= id_size
) {
372 if (do_entry(mod
->name
, symval
+i
, alias
)) {
373 /* Always end in a wildcard, for future extension */
374 if (alias
[strlen(alias
)-1] != '*')
376 buf_printf(&mod
->dev_table_buf
,
377 "MODULE_ALIAS(\"%s\");\n", alias
);
382 /* Create MODULE_ALIAS() statements.
383 * At this time, we cannot write the actual output C source yet,
384 * so we write into the mod->dev_table_buf buffer. */
385 void handle_moddevtable(struct module
*mod
, struct elf_info
*info
,
386 Elf_Sym
*sym
, const char *symname
)
390 /* We're looking for a section relative symbol */
391 if (!sym
->st_shndx
|| sym
->st_shndx
>= info
->hdr
->e_shnum
)
394 symval
= (void *)info
->hdr
395 + info
->sechdrs
[sym
->st_shndx
].sh_offset
398 if (sym_is(symname
, "__mod_pci_device_table"))
399 do_table(symval
, sym
->st_size
, sizeof(struct pci_device_id
),
401 else if (sym_is(symname
, "__mod_usb_device_table"))
402 /* special case to handle bcdDevice ranges */
403 do_usb_table(symval
, sym
->st_size
, mod
);
404 else if (sym_is(symname
, "__mod_ieee1394_device_table"))
405 do_table(symval
, sym
->st_size
, sizeof(struct ieee1394_device_id
),
406 do_ieee1394_entry
, mod
);
407 else if (sym_is(symname
, "__mod_ccw_device_table"))
408 do_table(symval
, sym
->st_size
, sizeof(struct ccw_device_id
),
410 else if (sym_is(symname
, "__mod_serio_device_table"))
411 do_table(symval
, sym
->st_size
, sizeof(struct serio_device_id
),
412 do_serio_entry
, mod
);
413 else if (sym_is(symname
, "__mod_pnp_device_table"))
414 do_table(symval
, sym
->st_size
, sizeof(struct pnp_device_id
),
416 else if (sym_is(symname
, "__mod_pnp_card_device_table"))
417 do_table(symval
, sym
->st_size
, sizeof(struct pnp_card_device_id
),
418 do_pnp_card_entry
, mod
);
419 else if (sym_is(symname
, "__mod_pcmcia_device_table"))
420 do_table(symval
, sym
->st_size
, sizeof(struct pcmcia_device_id
),
421 do_pcmcia_entry
, mod
);
422 else if (sym_is(symname
, "__mod_of_device_table"))
423 do_table(symval
, sym
->st_size
, sizeof(struct of_device_id
),
428 /* Now add out buffered information to the generated C source */
429 void add_moddevtable(struct buffer
*buf
, struct module
*mod
)
431 buf_printf(buf
, "\n");
432 buf_write(buf
, mod
->dev_table_buf
.p
, mod
->dev_table_buf
.pos
);
433 free(mod
->dev_table_buf
.p
);