blob: 65cf497acb65cf6419c394c6ef9ded68a65a2fee [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
Elliott Hughes132b1ec2024-10-29 22:12:01 +000078static void _eject(struct stat *s, const char *out, int olen, char *data, unsigned datasize)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079{
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
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000154 // TODO: switch to std::vector
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 int size = 32;
156 int entries = 0;
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000157 char** names = (char**) malloc(size * sizeof(char*));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 if (names == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000159 errx(1, "failed to allocate dir names array (size %d)", size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 }
161
162 while((de = readdir(d)) != 0){
163 /* xxx: feature? maybe some dotfiles are okay */
164 if(de->d_name[0] == '.') continue;
165
166 /* xxx: hack. use a real exclude list */
167 if(!strcmp(de->d_name, "root")) continue;
168
169 if (entries >= size) {
170 size *= 2;
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000171 names = (char**) realloc(names, size * sizeof(char*));
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800172 if (names == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000173 errx(1, "failed to reallocate dir names array (size %d)", size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800174 }
175 }
176 names[entries] = strdup(de->d_name);
177 if (names[entries] == NULL) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000178 errx(1, "failed to strdup name \"%s\"", de->d_name);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800179 }
180 ++entries;
181 }
182
183 qsort(names, entries, sizeof(char*), compare);
184
185 for (i = 0; i < entries; ++i) {
186 t = strlen(names[i]);
187 in[ilen] = '/';
188 memcpy(in + ilen + 1, names[i], t + 1);
189
190 if(olen > 0) {
191 out[olen] = '/';
192 memcpy(out + olen + 1, names[i], t + 1);
193 _archive(in, out, ilen + t + 1, olen + t + 1);
194 } else {
195 memcpy(out, names[i], t + 1);
196 _archive(in, out, ilen + t + 1, t);
197 }
198
199 in[ilen] = 0;
200 out[olen] = 0;
201
202 free(names[i]);
203 }
204 free(names);
Elliott Hughes14e28d32013-10-29 14:12:46 -0700205
206 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207}
208
209static void _archive(char *in, char *out, int ilen, int olen)
210{
211 struct stat s;
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000212 if(lstat(in, &s)) err(1, "could not stat '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213
214 if(S_ISREG(s.st_mode)){
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000215 int fd = open(in, O_RDONLY);
216 if(fd < 0) err(1, "cannot open '%s' for read", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800217
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000218 char* tmp = (char*) malloc(s.st_size);
219 if(tmp == 0) errx(1, "cannot allocate %zd bytes", s.st_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800220
221 if(read(fd, tmp, s.st_size) != s.st_size) {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000222 err(1, "cannot read %zd bytes", s.st_size);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223 }
224
225 _eject(&s, out, olen, tmp, s.st_size);
226
227 free(tmp);
228 close(fd);
229 } else if(S_ISDIR(s.st_mode)) {
230 _eject(&s, out, olen, 0, 0);
231 _archive_dir(in, out, ilen, olen);
232 } else if(S_ISLNK(s.st_mode)) {
233 char buf[1024];
234 int size;
235 size = readlink(in, buf, 1024);
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000236 if(size < 0) err(1, "cannot read symlink '%s'", in);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 _eject(&s, out, olen, buf, size);
Vincent Donnefortc2971352022-12-20 11:25:49 +0000238 } else if(S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode) ||
239 S_ISFIFO(s.st_mode) || S_ISSOCK(s.st_mode)) {
240 _eject(&s, out, olen, NULL, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 } else {
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000242 errx(1, "Unknown '%s' (mode %d)?", in, s.st_mode);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800243 }
244}
245
Mateus Azis023f67b2022-07-12 15:38:47 -0700246static void archive(const char* start, const char* prefix) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800247 char in[8192];
248 char out[8192];
249
250 strcpy(in, start);
251 strcpy(out, prefix);
252
253 _archive_dir(in, out, strlen(in), strlen(out));
254}
255
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700256static void read_canned_config(char* filename)
257{
258 int allocated = 8;
259 int used = 0;
260
261 canned_config =
262 (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
263
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000264 FILE* fp = fopen(filename, "r");
265 if (fp == NULL) err(1, "failed to open canned file '%s'", filename);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700266
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000267 char* line = NULL;
268 size_t allocated_len;
269 while (getline(&line, &allocated_len, fp) != -1) {
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700270 if (!line[0]) break;
271 if (used >= allocated) {
272 allocated *= 2;
273 canned_config = (struct fs_config_entry*)realloc(
274 canned_config, allocated * sizeof(struct fs_config_entry));
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000275 if (canned_config == NULL) errx(1, "failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700276 }
277
278 struct fs_config_entry* cc = canned_config + used;
279
280 if (isspace(line[0])) {
281 cc->name = strdup("");
282 cc->uid = atoi(strtok(line, " \n"));
283 } else {
284 cc->name = strdup(strtok(line, " \n"));
285 cc->uid = atoi(strtok(NULL, " \n"));
286 }
287 cc->gid = atoi(strtok(NULL, " \n"));
288 cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
289 ++used;
290 }
291 if (used >= allocated) {
292 ++allocated;
293 canned_config = (struct fs_config_entry*)realloc(
294 canned_config, allocated * sizeof(struct fs_config_entry));
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000295 if (canned_config == NULL) errx(1, "failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700296 }
297 canned_config[used].name = NULL;
298
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000299 free(line);
300 fclose(fp);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700301}
302
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000303static void devnodes_desc_error(const char* filename, unsigned long line_num,
304 const char* msg)
305{
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000306 errx(1, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000307}
308
309static int append_devnodes_desc_dir(char* path, char* args)
310{
311 struct stat s;
312
313 if (sscanf(args, "%o %d %d", &s.st_mode, &s.st_uid, &s.st_gid) != 3) return -1;
314
315 s.st_mode |= S_IFDIR;
316
317 _eject(&s, path, strlen(path), NULL, 0);
318
319 return 0;
320}
321
322static int append_devnodes_desc_nod(char* path, char* args)
323{
324 int minor, major;
325 struct stat s;
326 char dev;
327
328 if (sscanf(args, "%o %d %d %c %d %d", &s.st_mode, &s.st_uid, &s.st_gid,
329 &dev, &major, &minor) != 6) return -1;
330
331 s.st_rdev = MKDEV(major, minor);
332 switch (dev) {
333 case 'b':
334 s.st_mode |= S_IFBLK;
335 break;
336 case 'c':
337 s.st_mode |= S_IFCHR;
338 break;
339 default:
340 return -1;
341 }
342
343 _eject(&s, path, strlen(path), NULL, 0);
344
345 return 0;
346}
347
348static void append_devnodes_desc(const char* filename)
349{
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000350 FILE* fp = fopen(filename, "re");
351 if (!fp) err(1, "failed to open nodes description file '%s'", filename);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000352
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000353 unsigned long line_num = 0;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000354
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000355 char* line = NULL;
356 size_t allocated_len;
357 while (getline(&line, &allocated_len, fp) != -1) {
358 char *type, *path, *args;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000359
360 line_num++;
361
362 if (*line == '#') continue;
363
364 if (!(type = strtok(line, " \t"))) {
365 devnodes_desc_error(filename, line_num, "a type is missing");
366 }
367
368 if (*type == '\n') continue;
369
370 if (!(path = strtok(NULL, " \t"))) {
371 devnodes_desc_error(filename, line_num, "a path is missing");
372 }
373
374 if (!(args = strtok(NULL, "\n"))) {
375 devnodes_desc_error(filename, line_num, "args are missing");
376 }
377
378 if (!strcmp(type, "dir")) {
379 if (append_devnodes_desc_dir(path, args)) {
380 devnodes_desc_error(filename, line_num, "bad arguments for dir");
381 }
382 } else if (!strcmp(type, "nod")) {
383 if (append_devnodes_desc_nod(path, args)) {
384 devnodes_desc_error(filename, line_num, "bad arguments for nod");
385 }
386 } else {
387 devnodes_desc_error(filename, line_num, "type unknown");
388 }
389 }
390
391 free(line);
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000392 fclose(fp);
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000393}
394
Vincent Donnefort7f017742022-12-20 11:22:26 +0000395static const struct option long_options[] = {
396 { "dirname", required_argument, NULL, 'd' },
397 { "file", required_argument, NULL, 'f' },
398 { "help", no_argument, NULL, 'h' },
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000399 { "nodes", required_argument, NULL, 'n' },
Vincent Donnefort7f017742022-12-20 11:22:26 +0000400 { NULL, 0, NULL, 0 },
401};
402
403static void usage(void)
404{
405 fprintf(stderr,
Keir Fraser7ae58642024-05-13 13:09:18 +0000406 "Usage: mkbootfs [-n FILE] [-d DIR|-f FILE] DIR...\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000407 "\n"
408 "\t-d, --dirname=DIR: fs-config directory\n"
409 "\t-f, --file=FILE: Canned configuration file\n"
410 "\t-h, --help: Print this help\n"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000411 "\t-n, --nodes=FILE: Dev nodes description file\n"
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000412 "\n"
413 "Dev nodes description:\n"
Keir Fraserb2016ec2024-05-13 10:07:26 +0000414 "\t[dir|nod] [perms] [uid] [gid] [c|b] [major] [minor]\n"
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000415 "\tExample:\n"
416 "\t\t# My device nodes\n"
417 "\t\tdir dev 0755 0 0\n"
Keir Fraserb2016ec2024-05-13 10:07:26 +0000418 "\t\tnod dev/null 0600 0 0 c 1 3\n"
Vincent Donnefort7f017742022-12-20 11:22:26 +0000419 );
420}
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700421
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800422int main(int argc, char *argv[])
423{
Vincent Donnefort7f017742022-12-20 11:22:26 +0000424 int opt, unused;
425
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000426 while ((opt = getopt_long(argc, argv, "hd:f:n:", long_options, &unused)) != -1) {
Vincent Donnefort7f017742022-12-20 11:22:26 +0000427 switch (opt) {
428 case 'd':
429 target_out_path = argv[optind - 1];
430 break;
431 case 'f':
432 read_canned_config(argv[optind - 1]);
433 break;
434 case 'h':
435 usage();
436 return 0;
Vincent Donnefort99ab5212023-01-04 10:33:18 +0000437 case 'n':
438 append_devnodes_desc(argv[optind - 1]);
439 break;
Vincent Donnefort7f017742022-12-20 11:22:26 +0000440 default:
441 usage();
Elliott Hughesad1a0a92023-01-12 04:26:52 +0000442 errx(1, "Unknown option %s", argv[optind - 1]);
Vincent Donnefort7f017742022-12-20 11:22:26 +0000443 }
Mateus Azis023f67b2022-07-12 15:38:47 -0700444 }
445
Vincent Donnefort7f017742022-12-20 11:22:26 +0000446 int num_dirs = argc - optind;
447 argv += optind;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800448
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000449 while (num_dirs-- > 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450 char *x = strchr(*argv, '=');
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000451 if (x != nullptr) {
452 *x++ = '\0';
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800453 }
Elliott Hughes132b1ec2024-10-29 22:12:01 +0000454 archive(*argv, x ?: "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455
456 argv++;
457 }
458
459 _eject_trailer();
460
461 return 0;
462}