file2alias.c 14 KB

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