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.
14 #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,
44 void *data, int UNUSED_PARAM depth)
46 module_info **first = (module_info **) data;
49 size_t len = sb->st_size;
51 if (strrstr(fname, ".ko") == NULL)
54 image = xmalloc_open_zipped_read_close(fname, &len);
55 info = xzalloc(sizeof(module_info));
60 info->dnext = info->dprev = info;
61 info->name = xasprintf("/%s", fname);
62 info->modname = filename2modname(fname, NULL);
63 for (ptr = image; ptr < image + len - 10; ptr++) {
64 if (strncmp(ptr, "depends=", 8) == 0) {
68 for (u = ptr; *u; u++)
71 ptr += string_to_llist(ptr, &info->dependencies, ",");
72 } else if (ENABLE_FEATURE_MODUTILS_ALIAS &&
73 strncmp(ptr, "alias=", 6) == 0) {
74 llist_add_to(&info->aliases, xstrdup(ptr + 6));
76 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS &&
77 strncmp(ptr, "__ksymtab_", 10) == 0) {
79 if (strncmp(ptr, "gpl", 3) == 0 ||
80 strcmp(ptr, "strings") == 0)
82 llist_add_to(&info->symbols, xstrdup(ptr));
91 static module_info *find_module(module_info *modules, const char *modname)
95 for (m = modules; m != NULL; m = m->next)
96 if (strcmp(m->modname, modname) == 0)
101 static void order_dep_list(module_info *modules, module_info *start,
107 for (n = add; n != NULL; n = n->link) {
108 m = find_module(modules, n->data);
112 /* unlink current entry */
113 m->dnext->dprev = m->dprev;
114 m->dprev->dnext = m->dnext;
116 /* and add it to tail */
118 m->dprev = start->dprev;
119 start->dprev->dnext = m;
123 order_dep_list(modules, start, m->dependencies);
127 static void xfreopen_write(const char *file, FILE *f)
129 if (freopen(file, "w", f) == NULL)
130 bb_perror_msg_and_die("can't open '%s'", file);
133 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
134 int depmod_main(int argc UNUSED_PARAM, char **argv)
136 module_info *modules = NULL, *m, *dep;
137 const char *moddir_base = "/";
138 char *moddir, *version;
142 getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
145 /* goto modules location */
148 /* If a version is provided, then that kernel version's module directory
149 * is used, rather than the current kernel version (as returned by
151 if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
155 version = uts.release;
157 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
164 modfile = concat_path_file(moddir, *argv);
166 parse_module(modfile, &sb, &modules, 0);
170 recursive_action(moddir, ACTION_RECURSE,
171 parse_module, NULL, &modules, 0);
174 /* Prepare for writing out the dep files */
176 if (ENABLE_FEATURE_CLEAN_UP)
179 /* Generate dependency and alias files */
180 if (!(option_mask32 & ARG_n))
181 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
182 for (m = modules; m != NULL; m = m->next) {
183 printf("%s:", m->name);
185 order_dep_list(modules, m, m->dependencies);
186 while (m->dnext != m) {
188 printf(" %s", dep->name);
190 /* unlink current entry */
191 dep->dnext->dprev = dep->dprev;
192 dep->dprev->dnext = dep->dnext;
193 dep->dnext = dep->dprev = dep;
198 #if ENABLE_FEATURE_MODUTILS_ALIAS
199 if (!(option_mask32 & ARG_n))
200 xfreopen_write("modules.alias", stdout);
201 for (m = modules; m != NULL; m = m->next) {
203 printf("alias %s %s\n",
204 (char*)llist_pop(&m->aliases),
209 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
210 if (!(option_mask32 & ARG_n))
211 xfreopen_write("modules.symbols", stdout);
212 for (m = modules; m != NULL; m = m->next) {
214 printf("alias symbol:%s %s\n",
215 (char*)llist_pop(&m->symbols),
221 if (ENABLE_FEATURE_CLEAN_UP) {
223 module_info *old = modules;
224 modules = modules->next;