file2alias.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. typedef uint32_t __u32;
  26. typedef uint16_t __u16;
  27. typedef unsigned char __u8;
  28. /* Big exception to the "don't include kernel headers into userspace, which
  29. * even potentially has different endianness and word sizes, since
  30. * we handle those differences explicitly below */
  31. #include "../../include/linux/mod_devicetable.h"
  32. #define ADD(str, sep, cond, field) \
  33. do { \
  34. strcat(str, sep); \
  35. if (cond) \
  36. sprintf(str + strlen(str), \
  37. sizeof(field) == 1 ? "%02X" : \
  38. sizeof(field) == 2 ? "%04X" : \
  39. sizeof(field) == 4 ? "%08X" : "", \
  40. field); \
  41. else \
  42. sprintf(str + strlen(str), "*"); \
  43. } while(0)
  44. /* Looks like "usb:vNpNdlNdhNdcNdscNdpNicNiscNipN" */
  45. static int do_usb_entry(const char *filename,
  46. struct usb_device_id *id, char *alias)
  47. {
  48. id->match_flags = TO_NATIVE(id->match_flags);
  49. id->idVendor = TO_NATIVE(id->idVendor);
  50. id->idProduct = TO_NATIVE(id->idProduct);
  51. id->bcdDevice_lo = TO_NATIVE(id->bcdDevice_lo);
  52. id->bcdDevice_hi = TO_NATIVE(id->bcdDevice_hi);
  53. /*
  54. * Some modules (visor) have empty slots as placeholder for
  55. * run-time specification that results in catch-all alias
  56. */
  57. if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
  58. return 1;
  59. strcpy(alias, "usb:");
  60. ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
  61. id->idVendor);
  62. ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
  63. id->idProduct);
  64. ADD(alias, "dl", id->match_flags&USB_DEVICE_ID_MATCH_DEV_LO,
  65. id->bcdDevice_lo);
  66. ADD(alias, "dh", id->match_flags&USB_DEVICE_ID_MATCH_DEV_HI,
  67. id->bcdDevice_hi);
  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. return 1;
  86. }
  87. /* Looks like: ieee1394:venNmoNspNverN */
  88. static int do_ieee1394_entry(const char *filename,
  89. struct ieee1394_device_id *id, char *alias)
  90. {
  91. id->match_flags = TO_NATIVE(id->match_flags);
  92. id->vendor_id = TO_NATIVE(id->vendor_id);
  93. id->model_id = TO_NATIVE(id->model_id);
  94. id->specifier_id = TO_NATIVE(id->specifier_id);
  95. id->version = TO_NATIVE(id->version);
  96. strcpy(alias, "ieee1394:");
  97. ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
  98. id->vendor_id);
  99. ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
  100. id->model_id);
  101. ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
  102. id->specifier_id);
  103. ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
  104. id->version);
  105. return 1;
  106. }
  107. /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
  108. static int do_pci_entry(const char *filename,
  109. struct pci_device_id *id, char *alias)
  110. {
  111. /* Class field can be divided into these three. */
  112. unsigned char baseclass, subclass, interface,
  113. baseclass_mask, subclass_mask, interface_mask;
  114. id->vendor = TO_NATIVE(id->vendor);
  115. id->device = TO_NATIVE(id->device);
  116. id->subvendor = TO_NATIVE(id->subvendor);
  117. id->subdevice = TO_NATIVE(id->subdevice);
  118. id->class = TO_NATIVE(id->class);
  119. id->class_mask = TO_NATIVE(id->class_mask);
  120. strcpy(alias, "pci:");
  121. ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
  122. ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
  123. ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
  124. ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
  125. baseclass = (id->class) >> 16;
  126. baseclass_mask = (id->class_mask) >> 16;
  127. subclass = (id->class) >> 8;
  128. subclass_mask = (id->class_mask) >> 8;
  129. interface = id->class;
  130. interface_mask = id->class_mask;
  131. if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
  132. || (subclass_mask != 0 && subclass_mask != 0xFF)
  133. || (interface_mask != 0 && interface_mask != 0xFF)) {
  134. fprintf(stderr,
  135. "*** Warning: Can't handle masks in %s:%04X\n",
  136. filename, id->class_mask);
  137. return 0;
  138. }
  139. ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
  140. ADD(alias, "sc", subclass_mask == 0xFF, subclass);
  141. ADD(alias, "i", interface_mask == 0xFF, interface);
  142. return 1;
  143. }
  144. /* looks like: "ccw:tNmNdtNdmN" */
  145. static int do_ccw_entry(const char *filename,
  146. struct ccw_device_id *id, char *alias)
  147. {
  148. id->match_flags = TO_NATIVE(id->match_flags);
  149. id->cu_type = TO_NATIVE(id->cu_type);
  150. id->cu_model = TO_NATIVE(id->cu_model);
  151. id->dev_type = TO_NATIVE(id->dev_type);
  152. id->dev_model = TO_NATIVE(id->dev_model);
  153. strcpy(alias, "ccw:");
  154. ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
  155. id->cu_type);
  156. ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
  157. id->cu_model);
  158. ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
  159. id->dev_type);
  160. ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
  161. id->dev_model);
  162. return 1;
  163. }
  164. /* Looks like: "serio:tyNprNidNexN" */
  165. static int do_serio_entry(const char *filename,
  166. struct serio_device_id *id, char *alias)
  167. {
  168. id->type = TO_NATIVE(id->type);
  169. id->proto = TO_NATIVE(id->proto);
  170. id->id = TO_NATIVE(id->id);
  171. id->extra = TO_NATIVE(id->extra);
  172. strcpy(alias, "serio:");
  173. ADD(alias, "ty", id->type != SERIO_ANY, id->type);
  174. ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
  175. ADD(alias, "id", id->id != SERIO_ANY, id->id);
  176. ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
  177. return 1;
  178. }
  179. /* looks like: "pnp:dD" */
  180. static int do_pnp_entry(const char *filename,
  181. struct pnp_device_id *id, char *alias)
  182. {
  183. sprintf(alias, "pnp:d%s", id->id);
  184. return 1;
  185. }
  186. /* looks like: "pnp:cCdD..." */
  187. static int do_pnp_card_entry(const char *filename,
  188. struct pnp_card_device_id *id, char *alias)
  189. {
  190. int i;
  191. sprintf(alias, "pnp:c%s", id->id);
  192. for (i = 0; i < PNP_MAX_DEVICES; i++) {
  193. if (! *id->devs[i].id)
  194. break;
  195. sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
  196. }
  197. return 1;
  198. }
  199. /* Ignore any prefix, eg. v850 prepends _ */
  200. static inline int sym_is(const char *symbol, const char *name)
  201. {
  202. const char *match;
  203. match = strstr(symbol, name);
  204. if (!match)
  205. return 0;
  206. return match[strlen(symbol)] == '\0';
  207. }
  208. static void do_table(void *symval, unsigned long size,
  209. unsigned long id_size,
  210. void *function,
  211. struct module *mod)
  212. {
  213. unsigned int i;
  214. char alias[500];
  215. int (*do_entry)(const char *, void *entry, char *alias) = function;
  216. if (size % id_size || size < id_size) {
  217. fprintf(stderr, "*** Warning: %s ids %lu bad size "
  218. "(each on %lu)\n", mod->name, size, id_size);
  219. }
  220. /* Leave last one: it's the terminator. */
  221. size -= id_size;
  222. for (i = 0; i < size; i += id_size) {
  223. if (do_entry(mod->name, symval+i, alias)) {
  224. /* Always end in a wildcard, for future extension */
  225. if (alias[strlen(alias)-1] != '*')
  226. strcat(alias, "*");
  227. buf_printf(&mod->dev_table_buf,
  228. "MODULE_ALIAS(\"%s\");\n", alias);
  229. }
  230. }
  231. }
  232. /* Create MODULE_ALIAS() statements.
  233. * At this time, we cannot write the actual output C source yet,
  234. * so we write into the mod->dev_table_buf buffer. */
  235. void handle_moddevtable(struct module *mod, struct elf_info *info,
  236. Elf_Sym *sym, const char *symname)
  237. {
  238. void *symval;
  239. /* We're looking for a section relative symbol */
  240. if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
  241. return;
  242. symval = (void *)info->hdr
  243. + info->sechdrs[sym->st_shndx].sh_offset
  244. + sym->st_value;
  245. if (sym_is(symname, "__mod_pci_device_table"))
  246. do_table(symval, sym->st_size, sizeof(struct pci_device_id),
  247. do_pci_entry, mod);
  248. else if (sym_is(symname, "__mod_usb_device_table"))
  249. do_table(symval, sym->st_size, sizeof(struct usb_device_id),
  250. do_usb_entry, mod);
  251. else if (sym_is(symname, "__mod_ieee1394_device_table"))
  252. do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
  253. do_ieee1394_entry, mod);
  254. else if (sym_is(symname, "__mod_ccw_device_table"))
  255. do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
  256. do_ccw_entry, mod);
  257. else if (sym_is(symname, "__mod_serio_device_table"))
  258. do_table(symval, sym->st_size, sizeof(struct serio_device_id),
  259. do_serio_entry, mod);
  260. else if (sym_is(symname, "__mod_pnp_device_table"))
  261. do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
  262. do_pnp_entry, mod);
  263. else if (sym_is(symname, "__mod_pnp_card_device_table"))
  264. do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
  265. do_pnp_card_entry, mod);
  266. }
  267. /* Now add out buffered information to the generated C source */
  268. void add_moddevtable(struct buffer *buf, struct module *mod)
  269. {
  270. buf_printf(buf, "\n");
  271. buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
  272. free(mod->dev_table_buf.p);
  273. }