1
2
3
4
5
6
7
8
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <limits.h>
14#include <stdlib.h>
15#include <string.h>
16#include <stdio.h>
17#include <unistd.h>
18#include <ctype.h>
19
20#undef ARRAY_SIZE
21#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
22
23#include "../include/autoconf.h"
24#include "../include/applet_metadata.h"
25
26struct bb_applet {
27 const char *name;
28 const char *main;
29 enum bb_install_loc_t install_loc;
30 enum bb_suid_t need_suid;
31
32
33 unsigned char noexec;
34
35
36
37 unsigned char nofork;
38};
39
40
41#include "../include/applets.h"
42
43enum { NUM_APPLETS = ARRAY_SIZE(applets) };
44
45static int cmp_name(const void *a, const void *b)
46{
47 const struct bb_applet *aa = a;
48 const struct bb_applet *bb = b;
49 return strcmp(aa->name, bb->name);
50}
51
52static int str_isalnum_(const char *s)
53{
54 while (*s) {
55 if (!isalnum(*s) && *s != '_')
56 return 0;
57 s++;
58 }
59 return 1;
60}
61
62int main(int argc, char **argv)
63{
64 int i, j;
65 char tmp1[PATH_MAX], tmp2[PATH_MAX];
66
67
68
69
70
71
72
73
74
75
76
77 int KNOWN_APPNAME_OFFSETS = 8;
78
79
80 if (NUM_APPLETS < 128)
81 KNOWN_APPNAME_OFFSETS = 4;
82 if (NUM_APPLETS < 32)
83 KNOWN_APPNAME_OFFSETS = 0;
84
85 qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
86
87 for (i = j = 0; i < NUM_APPLETS-1; ++i) {
88 if (cmp_name(applets+i, applets+i+1) == 0) {
89 fprintf(stderr, "%s: duplicate applet name '%s'\n", argv[0],
90 applets[i].name);
91 j = 1;
92 }
93 }
94
95 if (j != 0 || !argv[1])
96 return 1;
97 snprintf(tmp1, PATH_MAX, "%s.%u.new", argv[1], (int) getpid());
98 i = open(tmp1, O_WRONLY | O_TRUNC | O_CREAT, 0666);
99 if (i < 0)
100 return 1;
101 dup2(i, 1);
102
103
104
105 printf("/* This is a generated file, don't edit */\n\n");
106
107 printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
108 if (NUM_APPLETS == 1) {
109 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
110 printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].main);
111 }
112
113 printf("#define KNOWN_APPNAME_OFFSETS %u\n\n", KNOWN_APPNAME_OFFSETS);
114 if (KNOWN_APPNAME_OFFSETS > 0) {
115 int ofs, offset[KNOWN_APPNAME_OFFSETS], index[KNOWN_APPNAME_OFFSETS];
116 for (i = 0; i < KNOWN_APPNAME_OFFSETS; i++)
117 index[i] = i * NUM_APPLETS / KNOWN_APPNAME_OFFSETS;
118 ofs = 0;
119 for (i = 0; i < NUM_APPLETS; i++) {
120 for (j = 0; j < KNOWN_APPNAME_OFFSETS; j++)
121 if (i == index[j])
122 offset[j] = ofs;
123 ofs += strlen(applets[i].name) + 1;
124 }
125
126 if (ofs > 0xffff)
127 return 1;
128 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
129 for (i = 1; i < KNOWN_APPNAME_OFFSETS; i++)
130 printf("%d,\n", offset[i]);
131 printf("};\n\n");
132 }
133
134
135 printf("const char applet_names[] ALIGN1 = \"\"\n");
136 for (i = 0; i < NUM_APPLETS; i++) {
137 printf("\"%s\" \"\\0\"\n", applets[i].name);
138
139
140 }
141 printf(";\n\n");
142
143 for (i = 0; i < NUM_APPLETS; i++) {
144 if (str_isalnum_(applets[i].name))
145 printf("#define APPLET_NO_%s %d\n", applets[i].name, i);
146 }
147 printf("\n");
148
149 printf("#ifndef SKIP_applet_main\n");
150 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
151 for (i = 0; i < NUM_APPLETS; i++) {
152 printf("%s_main,\n", applets[i].main);
153 }
154 printf("};\n");
155 printf("#endif\n\n");
156
157#if ENABLE_FEATURE_PREFER_APPLETS \
158 || ENABLE_FEATURE_SH_STANDALONE \
159 || ENABLE_FEATURE_SH_NOFORK
160 printf("const uint8_t applet_flags[] ALIGN1 = {\n");
161 i = 0;
162 while (i < NUM_APPLETS) {
163 int v = applets[i].nofork + (applets[i].noexec << 1);
164 if (++i < NUM_APPLETS)
165 v |= (applets[i].nofork + (applets[i].noexec << 1)) << 2;
166 if (++i < NUM_APPLETS)
167 v |= (applets[i].nofork + (applets[i].noexec << 1)) << 4;
168 if (++i < NUM_APPLETS)
169 v |= (applets[i].nofork + (applets[i].noexec << 1)) << 6;
170 printf("0x%02x,\n", v);
171 i++;
172 }
173 printf("};\n\n");
174#endif
175
176#if ENABLE_FEATURE_SUID
177 printf("const uint8_t applet_suid[] ALIGN1 = {\n");
178 i = 0;
179 while (i < NUM_APPLETS) {
180 int v = applets[i].need_suid;
181 if (++i < NUM_APPLETS)
182 v |= applets[i].need_suid << 2;
183 if (++i < NUM_APPLETS)
184 v |= applets[i].need_suid << 4;
185 if (++i < NUM_APPLETS)
186 v |= applets[i].need_suid << 6;
187 printf("0x%02x,\n", v);
188 i++;
189 }
190 printf("};\n\n");
191#endif
192
193#if ENABLE_FEATURE_INSTALLER
194 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
195 i = 0;
196 while (i < NUM_APPLETS) {
197 int v = applets[i].install_loc;
198 if (++i < NUM_APPLETS)
199 v |= applets[i].install_loc << 4;
200 printf("0x%02x,\n", v);
201 i++;
202 }
203 printf("};\n");
204#endif
205
206
207
208
209
210 if (argv[2]) {
211 FILE *fp;
212 char line_new[80];
213
214
215 sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
216
217
218
219
220
221
222
223 snprintf(tmp2, PATH_MAX, "%s.%u.new", argv[2], (int) getpid());
224 fp = fopen(tmp2, "w");
225 if (!fp)
226 return 1;
227 fputs(line_new, fp);
228 if (fclose(fp))
229 return 1;
230
231 }
232
233 if (fclose(stdout))
234 return 1;
235 if (rename(tmp1, argv[1]))
236 return 1;
237 if (rename(tmp2, argv[2]))
238 return 1;
239 return 0;
240}
241