1 /* vi: set sw=4 ts=4: */
3 * depmod - generate modules.dep
4 * Copyright (c) 2008 Bernhard Reutner-Fischer
5 * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
15 #include <sys/utsname.h> /* uname() */
18 * Theory of operation:
19 * - iterate over all modules and record their full path
20 * - iterate over all modules looking for "depends=" entries
21 * for each depends, look through our list of full paths and emit if found
24 typedef struct module_info {
25 struct module_info *next;
27 llist_t *dependencies;
30 struct module_info *dnext, *dprev;
34 ARG_a = (1<<0), /* All modules, ignore mods in argv */
35 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
36 ARG_b = (1<<2), /* base directory when modules are in staging area */
37 ARG_e = (1<<3), /* with -F, print unresolved symbols */
38 ARG_F = (1<<4), /* System.map that contains the symbols */
39 ARG_n = (1<<5), /* dry-run, print to stdout only */
40 ARG_r = (1<<6) /* Compat dummy. Linux Makefile uses it */
43 static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
44 void *data, int depth UNUSED_PARAM)
46 char modname[MODULE_NAME_LEN];
47 module_info **first = (module_info **) data;
50 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
51 size_t len = (64*1024*1024 - 4096);
53 if (strrstr(fname, ".ko") == NULL)
56 image = xmalloc_open_zipped_read_close(fname, &len);
57 info = xzalloc(sizeof(*info));
62 info->dnext = info->dprev = info;
63 info->name = xasprintf("/%s", fname);
64 info->modname = xstrdup(filename2modname(fname, modname));
65 for (ptr = image; ptr < image + len - 10; ptr++) {
66 if (strncmp(ptr, "depends=", 8) == 0) {
70 for (u = ptr; *u; u++)
73 ptr += string_to_llist(ptr, &info->dependencies, ",");
74 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
75 && strncmp(ptr, "alias=", 6) == 0
77 llist_add_to(&info->aliases, xstrdup(ptr + 6));
79 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
80 && strncmp(ptr, "__ksymtab_", 10) == 0
83 if (strncmp(ptr, "gpl", 3) == 0 ||
84 strcmp(ptr, "strings") == 0)
86 llist_add_to(&info->symbols, xstrdup(ptr));
95 static module_info *find_module(module_info *modules, const char *modname)
99 for (m = modules; m != NULL; m = m->next)
100 if (strcmp(m->modname, modname) == 0)
105 static void order_dep_list(module_info *modules, module_info *start,
111 for (n = add; n != NULL; n = n->link) {
112 m = find_module(modules, n->data);
116 /* unlink current entry */
117 m->dnext->dprev = m->dprev;
118 m->dprev->dnext = m->dnext;
120 /* and add it to tail */
122 m->dprev = start->dprev;
123 start->dprev->dnext = m;
127 order_dep_list(modules, start, m->dependencies);
131 static void xfreopen_write(const char *file, FILE *f)
133 if (freopen(file, "w", f) == NULL)
134 bb_perror_msg_and_die("can't open '%s'", file);
137 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
138 int depmod_main(int argc UNUSED_PARAM, char **argv)
140 module_info *modules = NULL, *m, *dep;
141 const char *moddir_base = "/";
142 char *moddir, *version;
146 getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
149 /* goto modules location */
152 /* If a version is provided, then that kernel version's module directory
153 * is used, rather than the current kernel version (as returned by
155 if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
159 version = uts.release;
161 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
168 modfile = concat_path_file(moddir, *argv);
170 parse_module(modfile, &sb, &modules, 0);
174 recursive_action(moddir, ACTION_RECURSE,
175 parse_module, NULL, &modules, 0);
178 /* Prepare for writing out the dep files */
180 if (ENABLE_FEATURE_CLEAN_UP)
183 /* Generate dependency and alias files */
184 if (!(option_mask32 & ARG_n))
185 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
186 for (m = modules; m != NULL; m = m->next) {
187 printf("%s:", m->name);
189 order_dep_list(modules, m, m->dependencies);
190 while (m->dnext != m) {
192 printf(" %s", dep->name);
194 /* unlink current entry */
195 dep->dnext->dprev = dep->dprev;
196 dep->dprev->dnext = dep->dnext;
197 dep->dnext = dep->dprev = dep;
202 #if ENABLE_FEATURE_MODUTILS_ALIAS
203 if (!(option_mask32 & ARG_n))
204 xfreopen_write("modules.alias", stdout);
205 for (m = modules; m != NULL; m = m->next) {
206 const char *fname = bb_basename(m->name);
207 int fnlen = strchrnul(fname, '.') - fname;
209 /* Last word can well be m->modname instead,
210 * but depmod from module-init-tools 3.4
211 * uses module basename, i.e., no s/-/_/g.
212 * (pathname and .ko.* are still stripped)
213 * Mimicking that... */
214 printf("alias %s %.*s\n",
215 (char*)llist_pop(&m->aliases),
220 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
221 if (!(option_mask32 & ARG_n))
222 xfreopen_write("modules.symbols", stdout);
223 for (m = modules; m != NULL; m = m->next) {
224 const char *fname = bb_basename(m->name);
225 int fnlen = strchrnul(fname, '.') - fname;
227 printf("alias symbol:%s %.*s\n",
228 (char*)llist_pop(&m->symbols),
234 if (ENABLE_FEATURE_CLEAN_UP) {
236 module_info *old = modules;
237 modules = modules->next;