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, since we can't rely on stdint.h being present. */
17 #if KERNEL_ELFCLASS == ELFCLASS32
18 typedef Elf32_Addr kernel_ulong_t
;
20 typedef Elf64_Addr kernel_ulong_t
;
23 typedef Elf32_Word __u32
;
24 typedef Elf32_Half __u16
;
25 typedef unsigned char __u8
;
27 /* Big exception to the "don't include kernel headers into userspace, which
28 * even potentially has different endianness and word sizes, since
29 * we handle those differences explicitly below */
30 #include "../include/linux/mod_devicetable.h"
32 #define ADD(str, sep, cond, field) \
36 sprintf(str + strlen(str), \
37 sizeof(field) == 1 ? "%02X" : \
38 sizeof(field) == 2 ? "%04X" : \
39 sizeof(field) == 4 ? "%08X" : "", \
42 sprintf(str + strlen(str), "*"); \
45 /* Looks like "usb:vNpNdlNdhNdcNdscNdpNicNiscNipN" */
46 static int do_usb_entry(const char *filename
,
47 struct usb_device_id
*id
, char *alias
)
49 id
->match_flags
= TO_NATIVE(id
->match_flags
);
50 id
->idVendor
= TO_NATIVE(id
->idVendor
);
51 id
->idProduct
= TO_NATIVE(id
->idProduct
);
52 id
->bcdDevice_lo
= TO_NATIVE(id
->bcdDevice_lo
);
53 id
->bcdDevice_hi
= TO_NATIVE(id
->bcdDevice_hi
);
55 strcpy(alias
, "usb:");
56 ADD(alias
, "v", id
->match_flags
&USB_DEVICE_ID_MATCH_VENDOR
,
58 ADD(alias
, "p", id
->match_flags
&USB_DEVICE_ID_MATCH_PRODUCT
,
60 ADD(alias
, "dl", id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_LO
,
62 ADD(alias
, "dh", id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_HI
,
64 ADD(alias
, "dc", id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_CLASS
,
67 id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_SUBCLASS
,
70 id
->match_flags
&USB_DEVICE_ID_MATCH_DEV_PROTOCOL
,
73 id
->match_flags
&USB_DEVICE_ID_MATCH_INT_CLASS
,
76 id
->match_flags
&USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
77 id
->bInterfaceSubClass
);
79 id
->match_flags
&USB_DEVICE_ID_MATCH_INT_PROTOCOL
,
80 id
->bInterfaceProtocol
);
84 /* Looks like: ieee1394:venNmoNspNverN */
85 static int do_ieee1394_entry(const char *filename
,
86 struct ieee1394_device_id
*id
, char *alias
)
88 id
->match_flags
= TO_NATIVE(id
->match_flags
);
89 id
->vendor_id
= TO_NATIVE(id
->vendor_id
);
90 id
->model_id
= TO_NATIVE(id
->model_id
);
91 id
->specifier_id
= TO_NATIVE(id
->specifier_id
);
92 id
->version
= TO_NATIVE(id
->version
);
94 strcpy(alias
, "ieee1394:");
95 ADD(alias
, "ven", id
->match_flags
& IEEE1394_MATCH_VENDOR_ID
,
97 ADD(alias
, "mo", id
->match_flags
& IEEE1394_MATCH_MODEL_ID
,
99 ADD(alias
, "sp", id
->match_flags
& IEEE1394_MATCH_SPECIFIER_ID
,
101 ADD(alias
, "ver", id
->match_flags
& IEEE1394_MATCH_VERSION
,
107 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
108 static int do_pci_entry(const char *filename
,
109 struct pci_device_id
*id
, char *alias
)
111 /* Class field can be divided into these three. */
112 unsigned char baseclass
, subclass
, interface
,
113 baseclass_mask
, subclass_mask
, interface_mask
;
115 id
->vendor
= TO_NATIVE(id
->vendor
);
116 id
->device
= TO_NATIVE(id
->device
);
117 id
->subvendor
= TO_NATIVE(id
->subvendor
);
118 id
->subdevice
= TO_NATIVE(id
->subdevice
);
119 id
->class = TO_NATIVE(id
->class);
120 id
->class_mask
= TO_NATIVE(id
->class_mask
);
122 strcpy(alias
, "pci:");
123 ADD(alias
, "v", id
->vendor
!= PCI_ANY_ID
, id
->vendor
);
124 ADD(alias
, "d", id
->device
!= PCI_ANY_ID
, id
->device
);
125 ADD(alias
, "sv", id
->subvendor
!= PCI_ANY_ID
, id
->subvendor
);
126 ADD(alias
, "sd", id
->subdevice
!= PCI_ANY_ID
, id
->subdevice
);
128 baseclass
= (id
->class) >> 16;
129 baseclass_mask
= (id
->class_mask
) >> 16;
130 subclass
= (id
->class) >> 8;
131 subclass_mask
= (id
->class_mask
) >> 8;
132 interface
= id
->class;
133 interface_mask
= id
->class_mask
;
135 if ((baseclass_mask
!= 0 && baseclass_mask
!= 0xFF)
136 || (subclass_mask
!= 0 && subclass_mask
!= 0xFF)
137 || (interface_mask
!= 0 && interface_mask
!= 0xFF)) {
139 "*** Warning: Can't handle masks in %s:%04X\n",
140 filename
, id
->class_mask
);
144 ADD(alias
, "bc", baseclass_mask
== 0xFF, baseclass
);
145 ADD(alias
, "sc", subclass_mask
== 0xFF, subclass
);
146 ADD(alias
, "i", interface_mask
== 0xFF, interface
);
150 /* looks like: "ccw:tNmNdtNdmN" */
151 static int do_ccw_entry(const char *filename
,
152 struct ccw_device_id
*id
, char *alias
)
154 id
->match_flags
= TO_NATIVE(id
->match_flags
);
155 id
->cu_type
= TO_NATIVE(id
->cu_type
);
156 id
->cu_model
= TO_NATIVE(id
->cu_model
);
157 id
->dev_type
= TO_NATIVE(id
->dev_type
);
158 id
->dev_model
= TO_NATIVE(id
->dev_model
);
160 strcpy(alias
, "ccw:");
161 ADD(alias
, "t", id
->match_flags
&CCW_DEVICE_ID_MATCH_CU_TYPE
,
163 ADD(alias
, "m", id
->match_flags
&CCW_DEVICE_ID_MATCH_CU_MODEL
,
165 ADD(alias
, "dt", id
->match_flags
&CCW_DEVICE_ID_MATCH_DEVICE_TYPE
,
167 ADD(alias
, "dm", id
->match_flags
&CCW_DEVICE_ID_MATCH_DEVICE_TYPE
,
172 /* Ignore any prefix, eg. v850 prepends _ */
173 static inline int sym_is(const char *symbol
, const char *name
)
177 match
= strstr(symbol
, name
);
180 return match
[strlen(symbol
)] == '\0';
183 static void do_table(void *symval
, unsigned long size
,
184 unsigned long id_size
,
190 int (*do_entry
)(const char *, void *entry
, char *alias
) = function
;
192 if (size
% id_size
|| size
< id_size
) {
193 fprintf(stderr
, "*** Warning: %s ids %lu bad size "
194 "(each on %lu)\n", mod
->name
, size
, id_size
);
196 /* Leave last one: it's the terminator. */
199 for (i
= 0; i
< size
; i
+= id_size
) {
200 if (do_entry(mod
->name
, symval
+i
, alias
)) {
201 /* Always end in a wildcard, for future extension */
202 if (alias
[strlen(alias
)-1] != '*')
204 buf_printf(&mod
->dev_table_buf
,
205 "MODULE_ALIAS(\"%s\");\n", alias
);
210 /* Create MODULE_ALIAS() statements.
211 * At this time, we cannot write the actual output C source yet,
212 * so we write into the mod->dev_table_buf buffer. */
213 void handle_moddevtable(struct module
*mod
, struct elf_info
*info
,
214 Elf_Sym
*sym
, const char *symname
)
218 /* We're looking for a section relative symbol */
219 if (!sym
->st_shndx
|| sym
->st_shndx
>= info
->hdr
->e_shnum
)
222 symval
= (void *)info
->hdr
223 + info
->sechdrs
[sym
->st_shndx
].sh_offset
226 if (sym_is(symname
, "__mod_pci_device_table"))
227 do_table(symval
, sym
->st_size
, sizeof(struct pci_device_id
),
229 else if (sym_is(symname
, "__mod_usb_device_table"))
230 do_table(symval
, sym
->st_size
, sizeof(struct usb_device_id
),
232 else if (sym_is(symname
, "__mod_ieee1394_device_table"))
233 do_table(symval
, sym
->st_size
, sizeof(struct ieee1394_device_id
),
234 do_ieee1394_entry
, mod
);
235 else if (sym_is(symname
, "__mod_ccw_device_table"))
236 do_table(symval
, sym
->st_size
, sizeof(struct ccw_device_id
),
240 /* Now add out buffered information to the generated C source */
241 void add_moddevtable(struct buffer
*buf
, struct module
*mod
)
243 buf_printf(buf
, "\n");
244 buf_write(buf
, mod
->dev_table_buf
.p
, mod
->dev_table_buf
.pos
);
245 free(mod
->dev_table_buf
.p
);