1 /* modinfo.c - Display module info
2 *
3 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
4 *
5 * TODO: cleanup
6
7 USE_MODINFO(NEWTOY(modinfo, "<1b:k:F:0", TOYFLAG_SBIN))
8
9 config MODINFO
10 bool "modinfo"
11 default y
12 help
13 usage: modinfo [-0] [-b basedir] [-k kernel] [-F field] [module|file...]
14
15 Display module fields for modules specified by name or .ko path.
16
17 -F Only show the given field
18 -0 Separate fields with NUL rather than newline
19 -b Use <basedir> as root for /lib/modules/
20 -k Look in given directory under /lib/modules/
21 */
22
23 #define FOR_modinfo
24 #include "toys.h"
25
26 GLOBALS(
27 char *F, *k, *b;
28
29 long mod;
30 int count;
31 )
32
output_field(char * field,char * value)33 static void output_field(char *field, char *value)
34 {
35 if (!TT.F) xprintf("%s:%*c", field, 15-(int)strlen(field), ' ');
36 else if (strcmp(TT.F, field)) return;
37 xprintf("%s", value);
38 xputc(FLAG(0) ? 0 : '\n');
39 }
40
modinfo_file(char * full_name)41 static void modinfo_file(char *full_name)
42 {
43 int fd, flen, i;
44 char *buf = 0, *end, *modinfo_tags[] = {
45 "license", "author", "description", "firmware", "alias", "srcversion",
46 "depends", "retpoline", "intree", "name", "vermagic", "parm", "parmtype",
47 };
48
49 if (-1 != (fd = open(full_name, O_RDONLY))) {
50 flen = fdlength(fd);
51 buf = xmmap(0, flen, PROT_READ, MAP_SHARED, fd, 0);
52 end = buf + flen;
53 close(fd);
54 }
55
56 if (!buf) {
57 perror_msg_raw(full_name);
58 return;
59 }
60
61 TT.count++;
62 output_field("filename", full_name);
63
64 for (i=0; i<ARRAY_LEN(modinfo_tags); i++) {
65 char *field = modinfo_tags[i], *p = buf;
66 int slen = sprintf(toybuf, "%s=", field);
67
68 while (p && p < end) {
69 p = memmem(p, end-p, toybuf, slen);
70 if (p) output_field(field, p += slen);
71 }
72 }
73
74 munmap(buf, flen);
75 }
76
check_module(struct dirtree * new)77 static int check_module(struct dirtree *new)
78 {
79 char *s;
80 int len;
81
82 if (!dirtree_notdotdot(new)) return 0;
83
84 if (!S_ISREG(new->st.st_mode)) return DIRTREE_RECURSE;
85
86 s = toys.optargs[TT.mod];
87
88 // The kernel treats - and _ the same, so we should too.
89 for (len = 0; s[len]; len++) {
90 if (s[len] == '-' && new->name[len] == '_') continue;
91 if (s[len] == '_' && new->name[len] == '-') continue;
92 if (s[len] != new->name[len]) break;
93 }
94 if (s[len] || strcmp(new->name+len, ".ko")) return DIRTREE_RECURSE;
95
96 modinfo_file(s = dirtree_path(new, 0));
97 free(s);
98
99 return DIRTREE_ABORT;
100 }
101
modinfo_main(void)102 void modinfo_main(void)
103 {
104 struct utsname uts;
105
106 // Android (as shipped by Google) currently only has modules on /vendor.
107 // Android does not support multiple sets of modules for different kernels.
108 if (CFG_TOYBOX_ON_ANDROID) {
109 if (!TT.b) TT.b = "/vendor";
110 if (!TT.k) TT.k = "";
111 } else {
112 uname(&uts);
113 if (!TT.b) TT.b = "";
114 if (!TT.k) TT.k = uts.release;
115 }
116
117 for (TT.mod = 0; TT.mod<toys.optc; TT.mod++) {
118 if (strend(toys.optargs[TT.mod], ".ko")) modinfo_file(toys.optargs[TT.mod]);
119 else {
120 char *path = xmprintf("%s/lib/modules/%s", TT.b, TT.k);
121
122 TT.count = 0;
123 dirtree_read(path, check_module);
124 if (!TT.count) error_msg("%s: not found", toys.optargs[TT.mod]);
125 free(path);
126 }
127 }
128 }
129