blob: a45c6a20a064a8fc5b0011d0c742a7c999ccbf52 [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
terryguane7f1d412024-10-29 13:20:46 -070022#include <android-base/file.h>
23#include <string>
24
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025/* NOTES
26**
Elliott Hughesad1a0a92023-01-12 04:26:52 +000027** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt
28** for an explanation of this file format
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029** - dotfiles are ignored
30** - directories named 'root' are ignored
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080031*/
32
Doug Zongkerf2c3a832012-05-03 15:55:56 -070033struct fs_config_entry {
34 char* name;
35 int uid, gid, mode;
36};
37
38static struct fs_config_entry* canned_config = NULL;
Mateus Azis023f67b2022-07-12 15:38:47 -070039static const char* target_out_path = NULL;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070040
Tao Bao3b5fbd82016-10-07 10:20:52 -070041#define TRAILER "TRAILER!!!"
42
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043static int total_size = 0;
44
45static void fix_stat(const char *path, struct stat *s)
46{
Nick Kraleviche9e74f32013-02-07 14:22:12 -080047 uint64_t capabilities;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070048 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.
Mark Salyzyn5ce75752014-05-15 15:05:25 -070070 unsigned st_mode = s->st_mode;
Tao Bao3b5fbd82016-10-07 10:20:52 -070071 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);
Mark Salyzyn5ce75752014-05-15 15:05:25 -070073 s->st_mode = (typeof(s->st_mode)) st_mode;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070074 }
Vincent Donnefortc2971352022-12-20 11:25:49 +000075
76 if (S_ISREG(s->st_mode) || S_ISDIR(s->st_mode) || S_ISLNK(s->st_mode)) {
77 s->st_rdev = 0;
78 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079}
80
Elliott Hughes132b1ec2024-10-29 22:12:01 +000081static void _eject(struct stat *s, const char *out, int olen, char *data, unsigned datasize)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082{
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
Vincent Donnefortc2971352022-12-20 11:25:49 +0000108 major(s->st_rdev),
109 minor(s->st_rdev),
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800110 olen + 1,
111 0,
112 out,
113 0
114 );
115
116 total_size += 6 + 8*13 + olen + 1;
117
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000118 if(strlen(out) != (unsigned int)olen) errx(1, "ACK!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119
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
131static void _eject_trailer()
132{
133 struct stat s;
134 memset(&s, 0, sizeof(s));
Tao Bao3b5fbd82016-10-07 10:20:52 -0700135 _eject(&s, TRAILER, 10, 0, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136
137 while(total_size & 0xff) {
138 total_size++;
139 putchar(0);
140 }
141}
142
143static void _archive(char *in, char *out, int ilen, int olen);
144
145static int compare(const void* a, const void* b) {
146 return strcmp(*(const char**)a, *(const char**)b);
147}
148
149static void _archive_dir(char *in, char *out, int ilen, int olen)
150{
151 int i, t;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800152 struct dirent *de;
153
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000154 DIR* d = opendir(in);
155 if (d == NULL) err(1, "cannot open directory '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800156
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000157 // TODO: switch to std::vector
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 int size = 32;
159 int entries = 0;
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000160 char** names = (char**) malloc(size * sizeof(char*));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 if (names == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000162 errx(1, "failed to allocate dir names array (size %d)", size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800163 }
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;
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000174 names = (char**) realloc(names, size * sizeof(char*));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800175 if (names == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000176 errx(1, "failed to reallocate dir names array (size %d)", size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 }
178 }
179 names[entries] = strdup(de->d_name);
180 if (names[entries] == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000181 errx(1, "failed to strdup name \"%s\"", de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800182 }
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);
Elliott Hughes14e28d32013-10-29 14:12:46 -0700208
209 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210}
211
212static void _archive(char *in, char *out, int ilen, int olen)
213{
214 struct stat s;
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000215 if(lstat(in, &s)) err(1, "could not stat '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216
217 if(S_ISREG(s.st_mode)){
terryguane7f1d412024-10-29 13:20:46 -0700218 std::string content;
219 if (!android::base::ReadFileToString(in, &content)) {
220 err(1, "cannot read '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 }
222
terryguane7f1d412024-10-29 13:20:46 -0700223 _eject(&s, out, olen, content.data(), content.size());
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800224 } 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);
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000231 if(size < 0) err(1, "cannot read symlink '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800232 _eject(&s, out, olen, buf, size);
Vincent Donnefortc2971352022-12-20 11:25:49 +0000233 } 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);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800236 } else {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000237 errx(1, "Unknown '%s' (mode %d)?", in, s.st_mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 }
239}
240
Mateus Azis023f67b2022-07-12 15:38:47 -0700241static void archive(const char* start, const char* prefix) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242 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
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700251static 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
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000259 FILE* fp = fopen(filename, "r");
260 if (fp == NULL) err(1, "failed to open canned file '%s'", filename);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700261
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000262 char* line = NULL;
263 size_t allocated_len;
264 while (getline(&line, &allocated_len, fp) != -1) {
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700265 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));
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000270 if (canned_config == NULL) errx(1, "failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700271 }
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));
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000290 if (canned_config == NULL) errx(1, "failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700291 }
292 canned_config[used].name = NULL;
293
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000294 free(line);
295 fclose(fp);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700296}
297
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000298static void devnodes_desc_error(const char* filename, unsigned long line_num,
299 const char* msg)
300{
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000301 errx(1, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000302}
303
304static 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
317static 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
343static void append_devnodes_desc(const char* filename)
344{
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000345 FILE* fp = fopen(filename, "re");
346 if (!fp) err(1, "failed to open nodes description file '%s'", filename);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000347
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000348 unsigned long line_num = 0;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000349
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000350 char* line = NULL;
351 size_t allocated_len;
352 while (getline(&line, &allocated_len, fp) != -1) {
353 char *type, *path, *args;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000354
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);
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000387 fclose(fp);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000388}
389
Vincent Donnefort7f017742022-12-20 11:22:26 +0000390static const struct option long_options[] = {
391 { "dirname", required_argument, NULL, 'd' },
392 { "file", required_argument, NULL, 'f' },
393 { "help", no_argument, NULL, 'h' },
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000394 { "nodes", required_argument, NULL, 'n' },
Vincent Donnefort7f017742022-12-20 11:22:26 +0000395 { NULL, 0, NULL, 0 },
396};
397
398static void usage(void)
399{
400 fprintf(stderr,
Keir Fraser7ae58642024-05-13 13:09:18 +0000401 "Usage: mkbootfs [-n FILE] [-d DIR|-f FILE] DIR...\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000402 "\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"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000406 "\t-n, --nodes=FILE: Dev nodes description file\n"
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000407 "\n"
408 "Dev nodes description:\n"
Keir Fraserb2016ec2024-05-13 10:07:26 +0000409 "\t[dir|nod] [perms] [uid] [gid] [c|b] [major] [minor]\n"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000410 "\tExample:\n"
411 "\t\t# My device nodes\n"
412 "\t\tdir dev 0755 0 0\n"
Keir Fraserb2016ec2024-05-13 10:07:26 +0000413 "\t\tnod dev/null 0600 0 0 c 1 3\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000414 );
415}
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700416
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800417int main(int argc, char *argv[])
418{
Vincent Donnefort7f017742022-12-20 11:22:26 +0000419 int opt, unused;
420
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000421 while ((opt = getopt_long(argc, argv, "hd:f:n:", long_options, &unused)) != -1) {
Vincent Donnefort7f017742022-12-20 11:22:26 +0000422 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;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000432 case 'n':
433 append_devnodes_desc(argv[optind - 1]);
434 break;
Vincent Donnefort7f017742022-12-20 11:22:26 +0000435 default:
436 usage();
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000437 errx(1, "Unknown option %s", argv[optind - 1]);
Vincent Donnefort7f017742022-12-20 11:22:26 +0000438 }
Mateus Azis023f67b2022-07-12 15:38:47 -0700439 }
440
Vincent Donnefort7f017742022-12-20 11:22:26 +0000441 int num_dirs = argc - optind;
442 argv += optind;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000444 while (num_dirs-- > 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445 char *x = strchr(*argv, '=');
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000446 if (x != nullptr) {
447 *x++ = '\0';
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448 }
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000449 archive(*argv, x ?: "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450
451 argv++;
452 }
453
454 _eject_trailer();
455
456 return 0;
457}