blob: 569a10cf949614e9ca261c7c9e0295402188e03a [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
Colin Cross6f2ae482023-01-09 13:01:46 -08002#include <ctype.h>
Elliott Hughesad1a0a92023-01-12 04:26:52 +00003#include <dirent.h>
Colin Cross6f2ae482023-01-09 13:01:46 -08004#include <err.h>
Vincent Donnefort99ab5212023-01-04 10:33:18 +00005#include <errno.h>
Elliott Hughesad1a0a92023-01-12 04:26:52 +00006#include <fcntl.h>
Vincent Donnefort7f017742022-12-20 11:22:26 +00007#include <getopt.h>
Elliott Hughesad1a0a92023-01-12 04:26:52 +00008#include <stdarg.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08009#include <stdio.h>
10#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080011#include <string.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012#include <sys/stat.h>
Vincent Donnefortc2971352022-12-20 11:25:49 +000013#include <sys/sysmacros.h>
Elliott Hughesad1a0a92023-01-12 04:26:52 +000014#include <sys/types.h>
15#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080016
Vincent Donnefort99ab5212023-01-04 10:33:18 +000017#include <linux/kdev_t.h>
18
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019#include <private/android_filesystem_config.h>
Tom Cherry6ad4d0a2020-03-04 13:35:28 -080020#include <private/fs_config.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021
22/* NOTES
23**
Elliott Hughesad1a0a92023-01-12 04:26:52 +000024** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
25** for an explanation of this file format
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026** - dotfiles are ignored
27** - directories named 'root' are ignored
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080028*/
29
Doug Zongkerf2c3a832012-05-03 15:55:56 -070030struct fs_config_entry {
31 char* name;
32 int uid, gid, mode;
33};
34
35static struct fs_config_entry* canned_config = NULL;
Mateus Azis023f67b2022-07-12 15:38:47 -070036static const char* target_out_path = NULL;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070037
Tao Bao3b5fbd82016-10-07 10:20:52 -070038#define TRAILER "TRAILER!!!"
39
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040static int total_size = 0;
41
42static void fix_stat(const char *path, struct stat *s)
43{
Nick Kraleviche9e74f32013-02-07 14:22:12 -080044 uint64_t capabilities;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070045 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.
Mark Salyzyn5ce75752014-05-15 15:05:25 -070067 unsigned st_mode = s->st_mode;
Tao Bao3b5fbd82016-10-07 10:20:52 -070068 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);
Mark Salyzyn5ce75752014-05-15 15:05:25 -070070 s->st_mode = (typeof(s->st_mode)) st_mode;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070071 }
Vincent Donnefortc2971352022-12-20 11:25:49 +000072
73 if (S_ISREG(s->st_mode) || S_ISDIR(s->st_mode) || S_ISLNK(s->st_mode)) {
74 s->st_rdev = 0;
75 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080076}
77
78static 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
Vincent Donnefortc2971352022-12-20 11:25:49 +0000105 major(s->st_rdev),
106 minor(s->st_rdev),
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800107 olen + 1,
108 0,
109 out,
110 0
111 );
112
113 total_size += 6 + 8*13 + olen + 1;
114
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000115 if(strlen(out) != (unsigned int)olen) errx(1, "ACK!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800116
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
128static void _eject_trailer()
129{
130 struct stat s;
131 memset(&s, 0, sizeof(s));
Tao Bao3b5fbd82016-10-07 10:20:52 -0700132 _eject(&s, TRAILER, 10, 0, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800133
134 while(total_size & 0xff) {
135 total_size++;
136 putchar(0);
137 }
138}
139
140static void _archive(char *in, char *out, int ilen, int olen);
141
142static int compare(const void* a, const void* b) {
143 return strcmp(*(const char**)a, *(const char**)b);
144}
145
146static void _archive_dir(char *in, char *out, int ilen, int olen)
147{
148 int i, t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 struct dirent *de;
150
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000151 DIR* d = opendir(in);
152 if (d == NULL) err(1, "cannot open directory '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153
154 int size = 32;
155 int entries = 0;
156 char** names = malloc(size * sizeof(char*));
157 if (names == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000158 errx(1, "failed to allocate dir names array (size %d)", size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800159 }
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) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000172 errx(1, "failed to reallocate dir names array (size %d)", size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173 }
174 }
175 names[entries] = strdup(de->d_name);
176 if (names[entries] == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000177 errx(1, "failed to strdup name \"%s\"", de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800178 }
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);
Elliott Hughes14e28d32013-10-29 14:12:46 -0700204
205 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800206}
207
208static void _archive(char *in, char *out, int ilen, int olen)
209{
210 struct stat s;
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000211 if(lstat(in, &s)) err(1, "could not stat '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212
213 if(S_ISREG(s.st_mode)){
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000214 int fd = open(in, O_RDONLY);
215 if(fd < 0) err(1, "cannot open '%s' for read", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000217 char* tmp = (char*) malloc(s.st_size);
218 if(tmp == 0) errx(1, "cannot allocate %zd bytes", s.st_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800219
220 if(read(fd, tmp, s.st_size) != s.st_size) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000221 err(1, "cannot read %zd bytes", s.st_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800222 }
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);
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000235 if(size < 0) err(1, "cannot read symlink '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 _eject(&s, out, olen, buf, size);
Vincent Donnefortc2971352022-12-20 11:25:49 +0000237 } 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);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800240 } else {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000241 errx(1, "Unknown '%s' (mode %d)?", in, s.st_mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 }
243}
244
Mateus Azis023f67b2022-07-12 15:38:47 -0700245static void archive(const char* start, const char* prefix) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 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
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700255static 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
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000263 FILE* fp = fopen(filename, "r");
264 if (fp == NULL) err(1, "failed to open canned file '%s'", filename);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700265
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000266 char* line = NULL;
267 size_t allocated_len;
268 while (getline(&line, &allocated_len, fp) != -1) {
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700269 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));
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000274 if (canned_config == NULL) errx(1, "failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700275 }
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));
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000294 if (canned_config == NULL) errx(1, "failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700295 }
296 canned_config[used].name = NULL;
297
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000298 free(line);
299 fclose(fp);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700300}
301
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000302static void devnodes_desc_error(const char* filename, unsigned long line_num,
303 const char* msg)
304{
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000305 errx(1, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000306}
307
308static 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
321static 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
347static void append_devnodes_desc(const char* filename)
348{
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000349 FILE* fp = fopen(filename, "re");
350 if (!fp) err(1, "failed to open nodes description file '%s'", filename);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000351
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000352 unsigned long line_num = 0;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000353
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000354 char* line = NULL;
355 size_t allocated_len;
356 while (getline(&line, &allocated_len, fp) != -1) {
357 char *type, *path, *args;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000358
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);
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000391 fclose(fp);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000392}
393
Vincent Donnefort7f017742022-12-20 11:22:26 +0000394static const struct option long_options[] = {
395 { "dirname", required_argument, NULL, 'd' },
396 { "file", required_argument, NULL, 'f' },
397 { "help", no_argument, NULL, 'h' },
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000398 { "nodes", required_argument, NULL, 'n' },
Vincent Donnefort7f017742022-12-20 11:22:26 +0000399 { NULL, 0, NULL, 0 },
400};
401
402static void usage(void)
403{
404 fprintf(stderr,
Keir Fraser7ae58642024-05-13 13:09:18 +0000405 "Usage: mkbootfs [-n FILE] [-d DIR|-f FILE] DIR...\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000406 "\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"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000410 "\t-n, --nodes=FILE: Dev nodes description file\n"
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000411 "\n"
412 "Dev nodes description:\n"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000413 "\t[dir|nod] [perms] [uid] [gid] [c|b] [minor] [major]\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 5\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000418 );
419}
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700420
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800421int main(int argc, char *argv[])
422{
Vincent Donnefort7f017742022-12-20 11:22:26 +0000423 int opt, unused;
424
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000425 while ((opt = getopt_long(argc, argv, "hd:f:n:", long_options, &unused)) != -1) {
Vincent Donnefort7f017742022-12-20 11:22:26 +0000426 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;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000436 case 'n':
437 append_devnodes_desc(argv[optind - 1]);
438 break;
Vincent Donnefort7f017742022-12-20 11:22:26 +0000439 default:
440 usage();
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000441 errx(1, "Unknown option %s", argv[optind - 1]);
Vincent Donnefort7f017742022-12-20 11:22:26 +0000442 }
Mateus Azis023f67b2022-07-12 15:38:47 -0700443 }
444
Vincent Donnefort7f017742022-12-20 11:22:26 +0000445 int num_dirs = argc - optind;
446 argv += optind;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800447
Vincent Donnefort7f017742022-12-20 11:22:26 +0000448 if (num_dirs <= 0) {
449 usage();
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000450 errx(1, "no directories to process?!");
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700451 }
452
Vincent Donnefort7f017742022-12-20 11:22:26 +0000453 while(num_dirs-- > 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800454 char *x = strchr(*argv, '=');
455 if(x != 0) {
456 *x++ = 0;
457 } else {
458 x = "";
459 }
460
461 archive(*argv, x);
462
463 argv++;
464 }
465
466 _eject_trailer();
467
468 return 0;
469}