blob: 405ba9e2a09431a17ae89d521aae09473c352850 [file] [log] [blame]
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * depmod - generate modules.dep
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2008 Bernhard Reutner-Fischer
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00005 * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00007 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
10
11#undef _GNU_SOURCE
12#define _GNU_SOURCE
13#include <libbb.h>
14#include <sys/utsname.h> /* uname() */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000015#include "modutils.h"
16
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000017/*
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
22 */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000023
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000024typedef struct module_info {
25 struct module_info *next;
26 char *name, *modname;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000027 llist_t *dependencies;
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000028 llist_t *aliases;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000029 llist_t *symbols;
30 struct module_info *dnext, *dprev;
31} module_info;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000032
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000033enum {
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 */
Denis Vlasenko35261152009-01-12 22:12:22 +000036 ARG_b = (1<<2), /* base directory when modules are in staging area */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000037 ARG_e = (1<<3), /* with -F, print unresolved symbols */
38 ARG_F = (1<<4), /* System.map that contains the symbols */
Ned Ludd039ed432009-02-20 04:56:00 +000039 ARG_n = (1<<5), /* dry-run, print to stdout only */
Denis Vlasenkoa430b692009-02-19 01:25:42 +000040 ARG_r = (1<<6) /* Compat dummy. Linux Makefile uses it */
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000041};
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000042
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000043static int FAST_FUNC parse_module(const char *fname, struct stat *sb,
44 void *data, int UNUSED_PARAM depth)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000045{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000046 module_info **first = (module_info **) data;
47 char *image, *ptr;
48 module_info *info;
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000049 size_t len = sb->st_size;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000050
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000051 if (strrstr(fname, ".ko") == NULL)
52 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000053
Denis Vlasenkofc668922008-11-06 02:32:31 +000054 image = xmalloc_open_zipped_read_close(fname, &len);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000055 info = xzalloc(sizeof(module_info));
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000056
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000057 info->next = *first;
58 *first = info;
59
60 info->dnext = info->dprev = info;
Denis Vlasenko35261152009-01-12 22:12:22 +000061 info->name = xasprintf("/%s", fname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000062 info->modname = filename2modname(fname, NULL);
63 for (ptr = image; ptr < image + len - 10; ptr++) {
64 if (strncmp(ptr, "depends=", 8) == 0) {
65 char *u;
66
67 ptr += 8;
68 for (u = ptr; *u; u++)
69 if (*u == '-')
70 *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));
75 ptr += strlen(ptr);
76 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS &&
77 strncmp(ptr, "__ksymtab_", 10) == 0) {
78 ptr += 10;
79 if (strncmp(ptr, "gpl", 3) == 0 ||
80 strcmp(ptr, "strings") == 0)
81 continue;
82 llist_add_to(&info->symbols, xstrdup(ptr));
83 ptr += strlen(ptr);
84 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000085 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000086 free(image);
87
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000088 return TRUE;
89}
90
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000091static module_info *find_module(module_info *modules, const char *modname)
92{
93 module_info *m;
94
95 for (m = modules; m != NULL; m = m->next)
96 if (strcmp(m->modname, modname) == 0)
97 return m;
98 return NULL;
99}
100
101static void order_dep_list(module_info *modules, module_info *start,
102 llist_t *add)
103{
104 module_info *m;
105 llist_t *n;
106
107 for (n = add; n != NULL; n = n->link) {
108 m = find_module(modules, n->data);
109 if (m == NULL)
110 continue;
111
112 /* unlink current entry */
113 m->dnext->dprev = m->dprev;
114 m->dprev->dnext = m->dnext;
115
116 /* and add it to tail */
117 m->dnext = start;
118 m->dprev = start->dprev;
119 start->dprev->dnext = m;
120 start->dprev = m;
121
122 /* recurse */
123 order_dep_list(modules, start, m->dependencies);
124 }
125}
126
Denis Vlasenko35261152009-01-12 22:12:22 +0000127static void xfreopen_write(const char *file, FILE *f)
128{
129 if (freopen(file, "w", f) == NULL)
130 bb_perror_msg_and_die("can't open '%s'", file);
131}
132
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000133int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000134int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000135{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000136 module_info *modules = NULL, *m, *dep;
Denis Vlasenko35261152009-01-12 22:12:22 +0000137 const char *moddir_base = "/";
138 char *moddir, *version;
139 struct utsname uts;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000140 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000141
Denis Vlasenkoa430b692009-02-19 01:25:42 +0000142 getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000143 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000144
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000145 /* goto modules location */
Denis Vlasenko35261152009-01-12 22:12:22 +0000146 xchdir(moddir_base);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000147
148 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000149 * is used, rather than the current kernel version (as returned by
150 * "uname -r"). */
Denis Vlasenko35261152009-01-12 22:12:22 +0000151 if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
152 version = *argv++;
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000153 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000154 uname(&uts);
Denis Vlasenko35261152009-01-12 22:12:22 +0000155 version = uts.release;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000156 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000157 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000158
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000159 /* Scan modules */
Denis Vlasenko35261152009-01-12 22:12:22 +0000160 if (*argv) {
161 char *modfile;
162 struct stat sb;
163 do {
164 modfile = concat_path_file(moddir, *argv);
165 xstat(modfile, &sb);
166 parse_module(modfile, &sb, &modules, 0);
167 free(modfile);
168 } while (*(++argv));
169 } else {
170 recursive_action(moddir, ACTION_RECURSE,
171 parse_module, NULL, &modules, 0);
172 }
173
174 /* Prepare for writing out the dep files */
175 xchdir(moddir);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000176 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko35261152009-01-12 22:12:22 +0000177 free(moddir);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000178
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000179 /* Generate dependency and alias files */
180 if (!(option_mask32 & ARG_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000181 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000182 for (m = modules; m != NULL; m = m->next) {
183 printf("%s:", m->name);
184
185 order_dep_list(modules, m, m->dependencies);
186 while (m->dnext != m) {
187 dep = m->dnext;
188 printf(" %s", dep->name);
189
190 /* unlink current entry */
191 dep->dnext->dprev = dep->dprev;
192 dep->dprev->dnext = dep->dnext;
193 dep->dnext = dep->dprev = dep;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000194 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000195 bb_putchar('\n');
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000196 }
197
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000198#if ENABLE_FEATURE_MODUTILS_ALIAS
199 if (!(option_mask32 & ARG_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000200 xfreopen_write("modules.alias", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000201 for (m = modules; m != NULL; m = m->next) {
202 while (m->aliases) {
203 printf("alias %s %s\n",
204 (char*)llist_pop(&m->aliases),
205 m->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000206 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000207 }
208#endif
209#if ENABLE_FEATURE_MODUTILS_SYMBOLS
210 if (!(option_mask32 & ARG_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000211 xfreopen_write("modules.symbols", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000212 for (m = modules; m != NULL; m = m->next) {
213 while (m->symbols) {
214 printf("alias symbol:%s %s\n",
215 (char*)llist_pop(&m->symbols),
216 m->modname);
217 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000218 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000219#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000220
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000221 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000222 while (modules) {
223 module_info *old = modules;
224 modules = modules->next;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000225 free(old->name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000226 free(old->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000227 free(old);
228 }
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000229 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000230
231 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000232}