1
2 #include <ctype.h>
3 #include <dirent.h>
4 #include <err.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <getopt.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/stat.h>
13 #include <sys/sysmacros.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17 #include <linux/kdev_t.h>
18
19 #include <private/android_filesystem_config.h>
20 #include <private/fs_config.h>
21
22 #include <android-base/file.h>
23 #include <string>
24
25 /* NOTES
26 **
27 ** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
28 ** for an explanation of this file format
29 ** - dotfiles are ignored
30 ** - directories named 'root' are ignored
31 */
32
33 struct fs_config_entry {
34 char* name;
35 int uid, gid, mode;
36 };
37
38 static struct fs_config_entry* canned_config = NULL;
39 static const char* target_out_path = NULL;
40
41 #define TRAILER "TRAILER!!!"
42
43 static int total_size = 0;
44
fix_stat(const char * path,struct stat * s)45 static void fix_stat(const char *path, struct stat *s)
46 {
47 uint64_t capabilities;
48 if (canned_config) {
49 // Use the list of file uid/gid/modes loaded from the file
50 // given with -f.
51
52 struct fs_config_entry* empty_path_config = NULL;
53 struct fs_config_entry* p;
54 for (p = canned_config; p->name; ++p) {
55 if (!p->name[0]) {
56 empty_path_config = p;
57 }
58 if (strcmp(p->name, path) == 0) {
59 s->st_uid = p->uid;
60 s->st_gid = p->gid;
61 s->st_mode = p->mode | (s->st_mode & ~07777);
62 return;
63 }
64 }
65 s->st_uid = empty_path_config->uid;
66 s->st_gid = empty_path_config->gid;
67 s->st_mode = empty_path_config->mode | (s->st_mode & ~07777);
68 } else {
69 // Use the compiled-in fs_config() function.
70 unsigned st_mode = s->st_mode;
71 int is_dir = S_ISDIR(s->st_mode) || strcmp(path, TRAILER) == 0;
72 fs_config(path, is_dir, target_out_path, &s->st_uid, &s->st_gid, &st_mode, &capabilities);
73 s->st_mode = (typeof(s->st_mode)) st_mode;
74 }
75
76 if (S_ISREG(s->st_mode) || S_ISDIR(s->st_mode) || S_ISLNK(s->st_mode)) {
77 s->st_rdev = 0;
78 }
79 }
80
_eject(struct stat * s,const char * out,int olen,char * data,unsigned datasize)81 static void _eject(struct stat *s, const char *out, int olen, char *data, unsigned datasize)
82 {
83 // Nothing is special about this value, just picked something in the
84 // approximate range that was being used already, and avoiding small
85 // values which may be special.
86 static unsigned next_inode = 300000;
87
88 while(total_size & 3) {
89 total_size++;
90 putchar(0);
91 }
92
93 fix_stat(out, s);
94 // fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode);
95
96 printf("%06x%08x%08x%08x%08x%08x%08x"
97 "%08x%08x%08x%08x%08x%08x%08x%s%c",
98 0x070701,
99 next_inode++, // s.st_ino,
100 s->st_mode,
101 0, // s.st_uid,
102 0, // s.st_gid,
103 1, // s.st_nlink,
104 0, // s.st_mtime,
105 datasize,
106 0, // volmajor
107 0, // volminor
108 major(s->st_rdev),
109 minor(s->st_rdev),
110 olen + 1,
111 0,
112 out,
113 0
114 );
115
116 total_size += 6 + 8*13 + olen + 1;
117
118 if(strlen(out) != (unsigned int)olen) errx(1, "ACK!");
119
120 while(total_size & 3) {
121 total_size++;
122 putchar(0);
123 }
124
125 if(datasize) {
126 fwrite(data, datasize, 1, stdout);
127 total_size += datasize;
128 }
129 }
130
_eject_trailer()131 static void _eject_trailer()
132 {
133 struct stat s;
134 memset(&s, 0, sizeof(s));
135 _eject(&s, TRAILER, 10, 0, 0);
136
137 while(total_size & 0xff) {
138 total_size++;
139 putchar(0);
140 }
141 }
142
143 static void _archive(char *in, char *out, int ilen, int olen);
144
compare(const void * a,const void * b)145 static int compare(const void* a, const void* b) {
146 return strcmp(*(const char**)a, *(const char**)b);
147 }
148
_archive_dir(char * in,char * out,int ilen,int olen)149 static void _archive_dir(char *in, char *out, int ilen, int olen)
150 {
151 int i, t;
152 struct dirent *de;
153
154 DIR* d = opendir(in);
155 if (d == NULL) err(1, "cannot open directory '%s'", in);
156
157 // TODO: switch to std::vector
158 int size = 32;
159 int entries = 0;
160 char** names = (char**) malloc(size * sizeof(char*));
161 if (names == NULL) {
162 errx(1, "failed to allocate dir names array (size %d)", size);
163 }
164
165 while((de = readdir(d)) != 0){
166 /* xxx: feature? maybe some dotfiles are okay */
167 if(de->d_name[0] == '.') continue;
168
169 /* xxx: hack. use a real exclude list */
170 if(!strcmp(de->d_name, "root")) continue;
171
172 if (entries >= size) {
173 size *= 2;
174 names = (char**) realloc(names, size * sizeof(char*));
175 if (names == NULL) {
176 errx(1, "failed to reallocate dir names array (size %d)", size);
177 }
178 }
179 names[entries] = strdup(de->d_name);
180 if (names[entries] == NULL) {
181 errx(1, "failed to strdup name \"%s\"", de->d_name);
182 }
183 ++entries;
184 }
185
186 qsort(names, entries, sizeof(char*), compare);
187
188 for (i = 0; i < entries; ++i) {
189 t = strlen(names[i]);
190 in[ilen] = '/';
191 memcpy(in + ilen + 1, names[i], t + 1);
192
193 if(olen > 0) {
194 out[olen] = '/';
195 memcpy(out + olen + 1, names[i], t + 1);
196 _archive(in, out, ilen + t + 1, olen + t + 1);
197 } else {
198 memcpy(out, names[i], t + 1);
199 _archive(in, out, ilen + t + 1, t);
200 }
201
202 in[ilen] = 0;
203 out[olen] = 0;
204
205 free(names[i]);
206 }
207 free(names);
208
209 closedir(d);
210 }
211
_archive(char * in,char * out,int ilen,int olen)212 static void _archive(char *in, char *out, int ilen, int olen)
213 {
214 struct stat s;
215 if(lstat(in, &s)) err(1, "could not stat '%s'", in);
216
217 if(S_ISREG(s.st_mode)){
218 std::string content;
219 if (!android::base::ReadFileToString(in, &content)) {
220 err(1, "cannot read '%s'", in);
221 }
222
223 _eject(&s, out, olen, content.data(), content.size());
224 } else if(S_ISDIR(s.st_mode)) {
225 _eject(&s, out, olen, 0, 0);
226 _archive_dir(in, out, ilen, olen);
227 } else if(S_ISLNK(s.st_mode)) {
228 char buf[1024];
229 int size;
230 size = readlink(in, buf, 1024);
231 if(size < 0) err(1, "cannot read symlink '%s'", in);
232 _eject(&s, out, olen, buf, size);
233 } else if(S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode) ||
234 S_ISFIFO(s.st_mode) || S_ISSOCK(s.st_mode)) {
235 _eject(&s, out, olen, NULL, 0);
236 } else {
237 errx(1, "Unknown '%s' (mode %d)?", in, s.st_mode);
238 }
239 }
240
archive(const char * start,const char * prefix)241 static void archive(const char* start, const char* prefix) {
242 char in[8192];
243 char out[8192];
244
245 strcpy(in, start);
246 strcpy(out, prefix);
247
248 _archive_dir(in, out, strlen(in), strlen(out));
249 }
250
read_canned_config(char * filename)251 static void read_canned_config(char* filename)
252 {
253 int allocated = 8;
254 int used = 0;
255
256 canned_config =
257 (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
258
259 FILE* fp = fopen(filename, "r");
260 if (fp == NULL) err(1, "failed to open canned file '%s'", filename);
261
262 char* line = NULL;
263 size_t allocated_len;
264 while (getline(&line, &allocated_len, fp) != -1) {
265 if (!line[0]) break;
266 if (used >= allocated) {
267 allocated *= 2;
268 canned_config = (struct fs_config_entry*)realloc(
269 canned_config, allocated * sizeof(struct fs_config_entry));
270 if (canned_config == NULL) errx(1, "failed to reallocate memory");
271 }
272
273 struct fs_config_entry* cc = canned_config + used;
274
275 if (isspace(line[0])) {
276 cc->name = strdup("");
277 cc->uid = atoi(strtok(line, " \n"));
278 } else {
279 cc->name = strdup(strtok(line, " \n"));
280 cc->uid = atoi(strtok(NULL, " \n"));
281 }
282 cc->gid = atoi(strtok(NULL, " \n"));
283 cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
284 ++used;
285 }
286 if (used >= allocated) {
287 ++allocated;
288 canned_config = (struct fs_config_entry*)realloc(
289 canned_config, allocated * sizeof(struct fs_config_entry));
290 if (canned_config == NULL) errx(1, "failed to reallocate memory");
291 }
292 canned_config[used].name = NULL;
293
294 free(line);
295 fclose(fp);
296 }
297
devnodes_desc_error(const char * filename,unsigned long line_num,const char * msg)298 static void devnodes_desc_error(const char* filename, unsigned long line_num,
299 const char* msg)
300 {
301 errx(1, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg);
302 }
303
append_devnodes_desc_dir(char * path,char * args)304 static int append_devnodes_desc_dir(char* path, char* args)
305 {
306 struct stat s;
307
308 if (sscanf(args, "%o %d %d", &s.st_mode, &s.st_uid, &s.st_gid) != 3) return -1;
309
310 s.st_mode |= S_IFDIR;
311
312 _eject(&s, path, strlen(path), NULL, 0);
313
314 return 0;
315 }
316
append_devnodes_desc_nod(char * path,char * args)317 static int append_devnodes_desc_nod(char* path, char* args)
318 {
319 int minor, major;
320 struct stat s;
321 char dev;
322
323 if (sscanf(args, "%o %d %d %c %d %d", &s.st_mode, &s.st_uid, &s.st_gid,
324 &dev, &major, &minor) != 6) return -1;
325
326 s.st_rdev = MKDEV(major, minor);
327 switch (dev) {
328 case 'b':
329 s.st_mode |= S_IFBLK;
330 break;
331 case 'c':
332 s.st_mode |= S_IFCHR;
333 break;
334 default:
335 return -1;
336 }
337
338 _eject(&s, path, strlen(path), NULL, 0);
339
340 return 0;
341 }
342
append_devnodes_desc(const char * filename)343 static void append_devnodes_desc(const char* filename)
344 {
345 FILE* fp = fopen(filename, "re");
346 if (!fp) err(1, "failed to open nodes description file '%s'", filename);
347
348 unsigned long line_num = 0;
349
350 char* line = NULL;
351 size_t allocated_len;
352 while (getline(&line, &allocated_len, fp) != -1) {
353 char *type, *path, *args;
354
355 line_num++;
356
357 if (*line == '#') continue;
358
359 if (!(type = strtok(line, " \t"))) {
360 devnodes_desc_error(filename, line_num, "a type is missing");
361 }
362
363 if (*type == '\n') continue;
364
365 if (!(path = strtok(NULL, " \t"))) {
366 devnodes_desc_error(filename, line_num, "a path is missing");
367 }
368
369 if (!(args = strtok(NULL, "\n"))) {
370 devnodes_desc_error(filename, line_num, "args are missing");
371 }
372
373 if (!strcmp(type, "dir")) {
374 if (append_devnodes_desc_dir(path, args)) {
375 devnodes_desc_error(filename, line_num, "bad arguments for dir");
376 }
377 } else if (!strcmp(type, "nod")) {
378 if (append_devnodes_desc_nod(path, args)) {
379 devnodes_desc_error(filename, line_num, "bad arguments for nod");
380 }
381 } else {
382 devnodes_desc_error(filename, line_num, "type unknown");
383 }
384 }
385
386 free(line);
387 fclose(fp);
388 }
389
390 static const struct option long_options[] = {
391 { "dirname", required_argument, NULL, 'd' },
392 { "file", required_argument, NULL, 'f' },
393 { "help", no_argument, NULL, 'h' },
394 { "nodes", required_argument, NULL, 'n' },
395 { NULL, 0, NULL, 0 },
396 };
397
usage(void)398 static void usage(void)
399 {
400 fprintf(stderr,
401 "Usage: mkbootfs [-n FILE] [-d DIR|-f FILE] DIR...\n"
402 "\n"
403 "\t-d, --dirname=DIR: fs-config directory\n"
404 "\t-f, --file=FILE: Canned configuration file\n"
405 "\t-h, --help: Print this help\n"
406 "\t-n, --nodes=FILE: Dev nodes description file\n"
407 "\n"
408 "Dev nodes description:\n"
409 "\t[dir|nod] [perms] [uid] [gid] [c|b] [major] [minor]\n"
410 "\tExample:\n"
411 "\t\t# My device nodes\n"
412 "\t\tdir dev 0755 0 0\n"
413 "\t\tnod dev/null 0600 0 0 c 1 3\n"
414 );
415 }
416
main(int argc,char * argv[])417 int main(int argc, char *argv[])
418 {
419 int opt, unused;
420
421 while ((opt = getopt_long(argc, argv, "hd:f:n:", long_options, &unused)) != -1) {
422 switch (opt) {
423 case 'd':
424 target_out_path = argv[optind - 1];
425 break;
426 case 'f':
427 read_canned_config(argv[optind - 1]);
428 break;
429 case 'h':
430 usage();
431 return 0;
432 case 'n':
433 append_devnodes_desc(argv[optind - 1]);
434 break;
435 default:
436 usage();
437 errx(1, "Unknown option %s", argv[optind - 1]);
438 }
439 }
440
441 int num_dirs = argc - optind;
442 argv += optind;
443
444 while (num_dirs-- > 0){
445 char *x = strchr(*argv, '=');
446 if (x != nullptr) {
447 *x++ = '\0';
448 }
449 archive(*argv, x ?: "");
450
451 argv++;
452 }
453
454 _eject_trailer();
455
456 return 0;
457 }
458