blob: 907cad3fbc3cfb55203a10c61e76fa34c2706fc6 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
Vincent Donnefort99ab5212023-01-04 10:33:18 +00002#include <error.h>
3#include <errno.h>
Vincent Donnefort7f017742022-12-20 11:22:26 +00004#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08005#include <stdio.h>
6#include <stdlib.h>
7#include <unistd.h>
8#include <string.h>
Doug Zongkerf2c3a832012-05-03 15:55:56 -07009#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080010
11#include <sys/types.h>
12#include <sys/stat.h>
Vincent Donnefortc2971352022-12-20 11:25:49 +000013#include <sys/sysmacros.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014#include <dirent.h>
15
16#include <stdarg.h>
17#include <fcntl.h>
18
Vincent Donnefort99ab5212023-01-04 10:33:18 +000019#include <linux/kdev_t.h>
20
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <private/android_filesystem_config.h>
Tom Cherry6ad4d0a2020-03-04 13:35:28 -080022#include <private/fs_config.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023
24/* NOTES
25**
26** - see buffer-format.txt from the linux kernel docs for
27** an explanation of this file format
28** - dotfiles are ignored
29** - directories named 'root' are ignored
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030*/
31
Mateus Azis023f67b2022-07-12 15:38:47 -070032static void die(const char* why, ...) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033 va_list ap;
34
35 va_start(ap, why);
36 fprintf(stderr,"error: ");
37 vfprintf(stderr, why, ap);
38 fprintf(stderr,"\n");
39 va_end(ap);
40 exit(1);
41}
42
Doug Zongkerf2c3a832012-05-03 15:55:56 -070043struct fs_config_entry {
44 char* name;
45 int uid, gid, mode;
46};
47
48static struct fs_config_entry* canned_config = NULL;
Mateus Azis023f67b2022-07-12 15:38:47 -070049static const char* target_out_path = NULL;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070050
51/* Each line in the canned file should be a path plus three ints (uid,
52 * gid, mode). */
Doug Zongkera865f6d2012-05-04 16:45:35 -070053#ifdef PATH_MAX
Doug Zongkerf2c3a832012-05-03 15:55:56 -070054#define CANNED_LINE_LENGTH (PATH_MAX+100)
Doug Zongkera865f6d2012-05-04 16:45:35 -070055#else
56#define CANNED_LINE_LENGTH (1024)
57#endif
Doug Zongkerf2c3a832012-05-03 15:55:56 -070058
Tao Bao3b5fbd82016-10-07 10:20:52 -070059#define TRAILER "TRAILER!!!"
60
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080061static int verbose = 0;
62static int total_size = 0;
63
64static void fix_stat(const char *path, struct stat *s)
65{
Nick Kraleviche9e74f32013-02-07 14:22:12 -080066 uint64_t capabilities;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070067 if (canned_config) {
68 // Use the list of file uid/gid/modes loaded from the file
69 // given with -f.
70
71 struct fs_config_entry* empty_path_config = NULL;
72 struct fs_config_entry* p;
73 for (p = canned_config; p->name; ++p) {
74 if (!p->name[0]) {
75 empty_path_config = p;
76 }
77 if (strcmp(p->name, path) == 0) {
78 s->st_uid = p->uid;
79 s->st_gid = p->gid;
80 s->st_mode = p->mode | (s->st_mode & ~07777);
81 return;
82 }
83 }
84 s->st_uid = empty_path_config->uid;
85 s->st_gid = empty_path_config->gid;
86 s->st_mode = empty_path_config->mode | (s->st_mode & ~07777);
87 } else {
88 // Use the compiled-in fs_config() function.
Mark Salyzyn5ce75752014-05-15 15:05:25 -070089 unsigned st_mode = s->st_mode;
Tao Bao3b5fbd82016-10-07 10:20:52 -070090 int is_dir = S_ISDIR(s->st_mode) || strcmp(path, TRAILER) == 0;
91 fs_config(path, is_dir, target_out_path, &s->st_uid, &s->st_gid, &st_mode, &capabilities);
Mark Salyzyn5ce75752014-05-15 15:05:25 -070092 s->st_mode = (typeof(s->st_mode)) st_mode;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070093 }
Vincent Donnefortc2971352022-12-20 11:25:49 +000094
95 if (S_ISREG(s->st_mode) || S_ISDIR(s->st_mode) || S_ISLNK(s->st_mode)) {
96 s->st_rdev = 0;
97 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098}
99
100static void _eject(struct stat *s, char *out, int olen, char *data, unsigned datasize)
101{
102 // Nothing is special about this value, just picked something in the
103 // approximate range that was being used already, and avoiding small
104 // values which may be special.
105 static unsigned next_inode = 300000;
106
107 while(total_size & 3) {
108 total_size++;
109 putchar(0);
110 }
111
112 fix_stat(out, s);
113// fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode);
114
115 printf("%06x%08x%08x%08x%08x%08x%08x"
116 "%08x%08x%08x%08x%08x%08x%08x%s%c",
117 0x070701,
118 next_inode++, // s.st_ino,
119 s->st_mode,
120 0, // s.st_uid,
121 0, // s.st_gid,
122 1, // s.st_nlink,
123 0, // s.st_mtime,
124 datasize,
125 0, // volmajor
126 0, // volminor
Vincent Donnefortc2971352022-12-20 11:25:49 +0000127 major(s->st_rdev),
128 minor(s->st_rdev),
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 olen + 1,
130 0,
131 out,
132 0
133 );
134
135 total_size += 6 + 8*13 + olen + 1;
136
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700137 if(strlen(out) != (unsigned int)olen) die("ACK!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138
139 while(total_size & 3) {
140 total_size++;
141 putchar(0);
142 }
143
144 if(datasize) {
145 fwrite(data, datasize, 1, stdout);
146 total_size += datasize;
147 }
148}
149
150static void _eject_trailer()
151{
152 struct stat s;
153 memset(&s, 0, sizeof(s));
Tao Bao3b5fbd82016-10-07 10:20:52 -0700154 _eject(&s, TRAILER, 10, 0, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155
156 while(total_size & 0xff) {
157 total_size++;
158 putchar(0);
159 }
160}
161
162static void _archive(char *in, char *out, int ilen, int olen);
163
164static int compare(const void* a, const void* b) {
165 return strcmp(*(const char**)a, *(const char**)b);
166}
167
168static void _archive_dir(char *in, char *out, int ilen, int olen)
169{
170 int i, t;
171 DIR *d;
172 struct dirent *de;
173
174 if(verbose) {
175 fprintf(stderr,"_archive_dir('%s','%s',%d,%d)\n",
176 in, out, ilen, olen);
177 }
178
179 d = opendir(in);
180 if(d == 0) die("cannot open directory '%s'", in);
181
182 int size = 32;
183 int entries = 0;
184 char** names = malloc(size * sizeof(char*));
185 if (names == NULL) {
186 fprintf(stderr, "failed to allocate dir names array (size %d)\n", size);
187 exit(1);
188 }
189
190 while((de = readdir(d)) != 0){
191 /* xxx: feature? maybe some dotfiles are okay */
192 if(de->d_name[0] == '.') continue;
193
194 /* xxx: hack. use a real exclude list */
195 if(!strcmp(de->d_name, "root")) continue;
196
197 if (entries >= size) {
198 size *= 2;
199 names = realloc(names, size * sizeof(char*));
200 if (names == NULL) {
201 fprintf(stderr, "failed to reallocate dir names array (size %d)\n",
202 size);
203 exit(1);
204 }
205 }
206 names[entries] = strdup(de->d_name);
207 if (names[entries] == NULL) {
208 fprintf(stderr, "failed to strdup name \"%s\"\n",
209 de->d_name);
210 exit(1);
211 }
212 ++entries;
213 }
214
215 qsort(names, entries, sizeof(char*), compare);
216
217 for (i = 0; i < entries; ++i) {
218 t = strlen(names[i]);
219 in[ilen] = '/';
220 memcpy(in + ilen + 1, names[i], t + 1);
221
222 if(olen > 0) {
223 out[olen] = '/';
224 memcpy(out + olen + 1, names[i], t + 1);
225 _archive(in, out, ilen + t + 1, olen + t + 1);
226 } else {
227 memcpy(out, names[i], t + 1);
228 _archive(in, out, ilen + t + 1, t);
229 }
230
231 in[ilen] = 0;
232 out[olen] = 0;
233
234 free(names[i]);
235 }
236 free(names);
Elliott Hughes14e28d32013-10-29 14:12:46 -0700237
238 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239}
240
241static void _archive(char *in, char *out, int ilen, int olen)
242{
243 struct stat s;
244
245 if(verbose) {
246 fprintf(stderr,"_archive('%s','%s',%d,%d)\n",
247 in, out, ilen, olen);
248 }
249
250 if(lstat(in, &s)) die("could not stat '%s'\n", in);
251
252 if(S_ISREG(s.st_mode)){
253 char *tmp;
254 int fd;
255
256 fd = open(in, O_RDONLY);
257 if(fd < 0) die("cannot open '%s' for read", in);
258
259 tmp = (char*) malloc(s.st_size);
260 if(tmp == 0) die("cannot allocate %d bytes", s.st_size);
261
262 if(read(fd, tmp, s.st_size) != s.st_size) {
263 die("cannot read %d bytes", s.st_size);
264 }
265
266 _eject(&s, out, olen, tmp, s.st_size);
267
268 free(tmp);
269 close(fd);
270 } else if(S_ISDIR(s.st_mode)) {
271 _eject(&s, out, olen, 0, 0);
272 _archive_dir(in, out, ilen, olen);
273 } else if(S_ISLNK(s.st_mode)) {
274 char buf[1024];
275 int size;
276 size = readlink(in, buf, 1024);
277 if(size < 0) die("cannot read symlink '%s'", in);
278 _eject(&s, out, olen, buf, size);
Vincent Donnefortc2971352022-12-20 11:25:49 +0000279 } else if(S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode) ||
280 S_ISFIFO(s.st_mode) || S_ISSOCK(s.st_mode)) {
281 _eject(&s, out, olen, NULL, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282 } else {
283 die("Unknown '%s' (mode %d)?\n", in, s.st_mode);
284 }
285}
286
Mateus Azis023f67b2022-07-12 15:38:47 -0700287static void archive(const char* start, const char* prefix) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 char in[8192];
289 char out[8192];
290
291 strcpy(in, start);
292 strcpy(out, prefix);
293
294 _archive_dir(in, out, strlen(in), strlen(out));
295}
296
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700297static void read_canned_config(char* filename)
298{
299 int allocated = 8;
300 int used = 0;
301
302 canned_config =
303 (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
304
305 char line[CANNED_LINE_LENGTH];
306 FILE* f = fopen(filename, "r");
Mateus Azis023f67b2022-07-12 15:38:47 -0700307 if (f == NULL) die("failed to open canned file '%s'", filename);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700308
309 while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) {
310 if (!line[0]) break;
311 if (used >= allocated) {
312 allocated *= 2;
313 canned_config = (struct fs_config_entry*)realloc(
314 canned_config, allocated * sizeof(struct fs_config_entry));
Mikhail Lappo26464912017-03-23 22:17:27 +0100315 if (canned_config == NULL) die("failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700316 }
317
318 struct fs_config_entry* cc = canned_config + used;
319
320 if (isspace(line[0])) {
321 cc->name = strdup("");
322 cc->uid = atoi(strtok(line, " \n"));
323 } else {
324 cc->name = strdup(strtok(line, " \n"));
325 cc->uid = atoi(strtok(NULL, " \n"));
326 }
327 cc->gid = atoi(strtok(NULL, " \n"));
328 cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
329 ++used;
330 }
331 if (used >= allocated) {
332 ++allocated;
333 canned_config = (struct fs_config_entry*)realloc(
334 canned_config, allocated * sizeof(struct fs_config_entry));
Mikhail Lappo26464912017-03-23 22:17:27 +0100335 if (canned_config == NULL) die("failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700336 }
337 canned_config[used].name = NULL;
338
339 fclose(f);
340}
341
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000342static void devnodes_desc_error(const char* filename, unsigned long line_num,
343 const char* msg)
344{
345 error(EXIT_FAILURE, 0, "failed to read nodes desc file '%s' line %lu: %s",
346 filename, line_num, msg);
347}
348
349static int append_devnodes_desc_dir(char* path, char* args)
350{
351 struct stat s;
352
353 if (sscanf(args, "%o %d %d", &s.st_mode, &s.st_uid, &s.st_gid) != 3) return -1;
354
355 s.st_mode |= S_IFDIR;
356
357 _eject(&s, path, strlen(path), NULL, 0);
358
359 return 0;
360}
361
362static int append_devnodes_desc_nod(char* path, char* args)
363{
364 int minor, major;
365 struct stat s;
366 char dev;
367
368 if (sscanf(args, "%o %d %d %c %d %d", &s.st_mode, &s.st_uid, &s.st_gid,
369 &dev, &major, &minor) != 6) return -1;
370
371 s.st_rdev = MKDEV(major, minor);
372 switch (dev) {
373 case 'b':
374 s.st_mode |= S_IFBLK;
375 break;
376 case 'c':
377 s.st_mode |= S_IFCHR;
378 break;
379 default:
380 return -1;
381 }
382
383 _eject(&s, path, strlen(path), NULL, 0);
384
385 return 0;
386}
387
388static void append_devnodes_desc(const char* filename)
389{
390 FILE* f = fopen(filename, "re");
391 if (!f) error(EXIT_FAILURE, errno,
392 "failed to open nodes description file '%s'", filename);
393
394 char *line, *args, *type, *path;
395 unsigned long line_num = 0;
396 size_t allocated_len;
397
398 while (getline(&line, &allocated_len, f) != -1) {
399 char* type;
400
401 line_num++;
402
403 if (*line == '#') continue;
404
405 if (!(type = strtok(line, " \t"))) {
406 devnodes_desc_error(filename, line_num, "a type is missing");
407 }
408
409 if (*type == '\n') continue;
410
411 if (!(path = strtok(NULL, " \t"))) {
412 devnodes_desc_error(filename, line_num, "a path is missing");
413 }
414
415 if (!(args = strtok(NULL, "\n"))) {
416 devnodes_desc_error(filename, line_num, "args are missing");
417 }
418
419 if (!strcmp(type, "dir")) {
420 if (append_devnodes_desc_dir(path, args)) {
421 devnodes_desc_error(filename, line_num, "bad arguments for dir");
422 }
423 } else if (!strcmp(type, "nod")) {
424 if (append_devnodes_desc_nod(path, args)) {
425 devnodes_desc_error(filename, line_num, "bad arguments for nod");
426 }
427 } else {
428 devnodes_desc_error(filename, line_num, "type unknown");
429 }
430 }
431
432 free(line);
433 fclose(f);
434}
435
Vincent Donnefort7f017742022-12-20 11:22:26 +0000436static const struct option long_options[] = {
437 { "dirname", required_argument, NULL, 'd' },
438 { "file", required_argument, NULL, 'f' },
439 { "help", no_argument, NULL, 'h' },
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000440 { "nodes", required_argument, NULL, 'n' },
Vincent Donnefort7f017742022-12-20 11:22:26 +0000441 { NULL, 0, NULL, 0 },
442};
443
444static void usage(void)
445{
446 fprintf(stderr,
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000447 "Usage: mkbootfs [-n FILE] [-d DIR|-F FILE] DIR...\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000448 "\n"
449 "\t-d, --dirname=DIR: fs-config directory\n"
450 "\t-f, --file=FILE: Canned configuration file\n"
451 "\t-h, --help: Print this help\n"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000452 "\t-n, --nodes=FILE: Dev nodes description file\n"
453 "\nDev nodes description:\n"
454 "\t[dir|nod] [perms] [uid] [gid] [c|b] [minor] [major]\n"
455 "\tExample:\n"
456 "\t\t# My device nodes\n"
457 "\t\tdir dev 0755 0 0\n"
458 "\t\tnod dev/null 0600 0 0 c 1 5\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000459 );
460}
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700461
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800462int main(int argc, char *argv[])
463{
Vincent Donnefort7f017742022-12-20 11:22:26 +0000464 int opt, unused;
465
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000466 while ((opt = getopt_long(argc, argv, "hd:f:n:", long_options, &unused)) != -1) {
Vincent Donnefort7f017742022-12-20 11:22:26 +0000467 switch (opt) {
468 case 'd':
469 target_out_path = argv[optind - 1];
470 break;
471 case 'f':
472 read_canned_config(argv[optind - 1]);
473 break;
474 case 'h':
475 usage();
476 return 0;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000477 case 'n':
478 append_devnodes_desc(argv[optind - 1]);
479 break;
Vincent Donnefort7f017742022-12-20 11:22:26 +0000480 default:
481 usage();
482 die("Unknown option %s", argv[optind - 1]);
483 }
Mateus Azis023f67b2022-07-12 15:38:47 -0700484 }
485
Vincent Donnefort7f017742022-12-20 11:22:26 +0000486 int num_dirs = argc - optind;
487 argv += optind;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800488
Vincent Donnefort7f017742022-12-20 11:22:26 +0000489 if (num_dirs <= 0) {
490 usage();
491 die("no directories to process?!");
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700492 }
493
Vincent Donnefort7f017742022-12-20 11:22:26 +0000494 while(num_dirs-- > 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495 char *x = strchr(*argv, '=');
496 if(x != 0) {
497 *x++ = 0;
498 } else {
499 x = "";
500 }
501
502 archive(*argv, x);
503
504 argv++;
505 }
506
507 _eject_trailer();
508
509 return 0;
510}