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