blob: 1b96788279fa98357695765920c2644019597e1c [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001
Vincent Donnefort7f017742022-12-20 11:22:26 +00002#include <getopt.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08003#include <stdio.h>
4#include <stdlib.h>
5#include <unistd.h>
6#include <string.h>
Doug Zongkerf2c3a832012-05-03 15:55:56 -07007#include <ctype.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08008
9#include <sys/types.h>
10#include <sys/stat.h>
Vincent Donnefortc2971352022-12-20 11:25:49 +000011#include <sys/sysmacros.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080012#include <dirent.h>
13
14#include <stdarg.h>
15#include <fcntl.h>
16
17#include <private/android_filesystem_config.h>
Tom Cherry6ad4d0a2020-03-04 13:35:28 -080018#include <private/fs_config.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080019
20/* NOTES
21**
22** - see buffer-format.txt from the linux kernel docs for
23** an explanation of this file format
24** - dotfiles are ignored
25** - directories named 'root' are ignored
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080026*/
27
Mateus Azis023f67b2022-07-12 15:38:47 -070028static void die(const char* why, ...) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080029 va_list ap;
30
31 va_start(ap, why);
32 fprintf(stderr,"error: ");
33 vfprintf(stderr, why, ap);
34 fprintf(stderr,"\n");
35 va_end(ap);
36 exit(1);
37}
38
Doug Zongkerf2c3a832012-05-03 15:55:56 -070039struct fs_config_entry {
40 char* name;
41 int uid, gid, mode;
42};
43
44static struct fs_config_entry* canned_config = NULL;
Mateus Azis023f67b2022-07-12 15:38:47 -070045static const char* target_out_path = NULL;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070046
47/* Each line in the canned file should be a path plus three ints (uid,
48 * gid, mode). */
Doug Zongkera865f6d2012-05-04 16:45:35 -070049#ifdef PATH_MAX
Doug Zongkerf2c3a832012-05-03 15:55:56 -070050#define CANNED_LINE_LENGTH (PATH_MAX+100)
Doug Zongkera865f6d2012-05-04 16:45:35 -070051#else
52#define CANNED_LINE_LENGTH (1024)
53#endif
Doug Zongkerf2c3a832012-05-03 15:55:56 -070054
Tao Bao3b5fbd82016-10-07 10:20:52 -070055#define TRAILER "TRAILER!!!"
56
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057static int verbose = 0;
58static int total_size = 0;
59
60static void fix_stat(const char *path, struct stat *s)
61{
Nick Kraleviche9e74f32013-02-07 14:22:12 -080062 uint64_t capabilities;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070063 if (canned_config) {
64 // Use the list of file uid/gid/modes loaded from the file
65 // given with -f.
66
67 struct fs_config_entry* empty_path_config = NULL;
68 struct fs_config_entry* p;
69 for (p = canned_config; p->name; ++p) {
70 if (!p->name[0]) {
71 empty_path_config = p;
72 }
73 if (strcmp(p->name, path) == 0) {
74 s->st_uid = p->uid;
75 s->st_gid = p->gid;
76 s->st_mode = p->mode | (s->st_mode & ~07777);
77 return;
78 }
79 }
80 s->st_uid = empty_path_config->uid;
81 s->st_gid = empty_path_config->gid;
82 s->st_mode = empty_path_config->mode | (s->st_mode & ~07777);
83 } else {
84 // Use the compiled-in fs_config() function.
Mark Salyzyn5ce75752014-05-15 15:05:25 -070085 unsigned st_mode = s->st_mode;
Tao Bao3b5fbd82016-10-07 10:20:52 -070086 int is_dir = S_ISDIR(s->st_mode) || strcmp(path, TRAILER) == 0;
87 fs_config(path, is_dir, target_out_path, &s->st_uid, &s->st_gid, &st_mode, &capabilities);
Mark Salyzyn5ce75752014-05-15 15:05:25 -070088 s->st_mode = (typeof(s->st_mode)) st_mode;
Doug Zongkerf2c3a832012-05-03 15:55:56 -070089 }
Vincent Donnefortc2971352022-12-20 11:25:49 +000090
91 if (S_ISREG(s->st_mode) || S_ISDIR(s->st_mode) || S_ISLNK(s->st_mode)) {
92 s->st_rdev = 0;
93 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094}
95
96static void _eject(struct stat *s, char *out, int olen, char *data, unsigned datasize)
97{
98 // Nothing is special about this value, just picked something in the
99 // approximate range that was being used already, and avoiding small
100 // values which may be special.
101 static unsigned next_inode = 300000;
102
103 while(total_size & 3) {
104 total_size++;
105 putchar(0);
106 }
107
108 fix_stat(out, s);
109// fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode);
110
111 printf("%06x%08x%08x%08x%08x%08x%08x"
112 "%08x%08x%08x%08x%08x%08x%08x%s%c",
113 0x070701,
114 next_inode++, // s.st_ino,
115 s->st_mode,
116 0, // s.st_uid,
117 0, // s.st_gid,
118 1, // s.st_nlink,
119 0, // s.st_mtime,
120 datasize,
121 0, // volmajor
122 0, // volminor
Vincent Donnefortc2971352022-12-20 11:25:49 +0000123 major(s->st_rdev),
124 minor(s->st_rdev),
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800125 olen + 1,
126 0,
127 out,
128 0
129 );
130
131 total_size += 6 + 8*13 + olen + 1;
132
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700133 if(strlen(out) != (unsigned int)olen) die("ACK!");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800134
135 while(total_size & 3) {
136 total_size++;
137 putchar(0);
138 }
139
140 if(datasize) {
141 fwrite(data, datasize, 1, stdout);
142 total_size += datasize;
143 }
144}
145
146static void _eject_trailer()
147{
148 struct stat s;
149 memset(&s, 0, sizeof(s));
Tao Bao3b5fbd82016-10-07 10:20:52 -0700150 _eject(&s, TRAILER, 10, 0, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151
152 while(total_size & 0xff) {
153 total_size++;
154 putchar(0);
155 }
156}
157
158static void _archive(char *in, char *out, int ilen, int olen);
159
160static int compare(const void* a, const void* b) {
161 return strcmp(*(const char**)a, *(const char**)b);
162}
163
164static void _archive_dir(char *in, char *out, int ilen, int olen)
165{
166 int i, t;
167 DIR *d;
168 struct dirent *de;
169
170 if(verbose) {
171 fprintf(stderr,"_archive_dir('%s','%s',%d,%d)\n",
172 in, out, ilen, olen);
173 }
174
175 d = opendir(in);
176 if(d == 0) die("cannot open directory '%s'", in);
177
178 int size = 32;
179 int entries = 0;
180 char** names = malloc(size * sizeof(char*));
181 if (names == NULL) {
182 fprintf(stderr, "failed to allocate dir names array (size %d)\n", size);
183 exit(1);
184 }
185
186 while((de = readdir(d)) != 0){
187 /* xxx: feature? maybe some dotfiles are okay */
188 if(de->d_name[0] == '.') continue;
189
190 /* xxx: hack. use a real exclude list */
191 if(!strcmp(de->d_name, "root")) continue;
192
193 if (entries >= size) {
194 size *= 2;
195 names = realloc(names, size * sizeof(char*));
196 if (names == NULL) {
197 fprintf(stderr, "failed to reallocate dir names array (size %d)\n",
198 size);
199 exit(1);
200 }
201 }
202 names[entries] = strdup(de->d_name);
203 if (names[entries] == NULL) {
204 fprintf(stderr, "failed to strdup name \"%s\"\n",
205 de->d_name);
206 exit(1);
207 }
208 ++entries;
209 }
210
211 qsort(names, entries, sizeof(char*), compare);
212
213 for (i = 0; i < entries; ++i) {
214 t = strlen(names[i]);
215 in[ilen] = '/';
216 memcpy(in + ilen + 1, names[i], t + 1);
217
218 if(olen > 0) {
219 out[olen] = '/';
220 memcpy(out + olen + 1, names[i], t + 1);
221 _archive(in, out, ilen + t + 1, olen + t + 1);
222 } else {
223 memcpy(out, names[i], t + 1);
224 _archive(in, out, ilen + t + 1, t);
225 }
226
227 in[ilen] = 0;
228 out[olen] = 0;
229
230 free(names[i]);
231 }
232 free(names);
Elliott Hughes14e28d32013-10-29 14:12:46 -0700233
234 closedir(d);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800235}
236
237static void _archive(char *in, char *out, int ilen, int olen)
238{
239 struct stat s;
240
241 if(verbose) {
242 fprintf(stderr,"_archive('%s','%s',%d,%d)\n",
243 in, out, ilen, olen);
244 }
245
246 if(lstat(in, &s)) die("could not stat '%s'\n", in);
247
248 if(S_ISREG(s.st_mode)){
249 char *tmp;
250 int fd;
251
252 fd = open(in, O_RDONLY);
253 if(fd < 0) die("cannot open '%s' for read", in);
254
255 tmp = (char*) malloc(s.st_size);
256 if(tmp == 0) die("cannot allocate %d bytes", s.st_size);
257
258 if(read(fd, tmp, s.st_size) != s.st_size) {
259 die("cannot read %d bytes", s.st_size);
260 }
261
262 _eject(&s, out, olen, tmp, s.st_size);
263
264 free(tmp);
265 close(fd);
266 } else if(S_ISDIR(s.st_mode)) {
267 _eject(&s, out, olen, 0, 0);
268 _archive_dir(in, out, ilen, olen);
269 } else if(S_ISLNK(s.st_mode)) {
270 char buf[1024];
271 int size;
272 size = readlink(in, buf, 1024);
273 if(size < 0) die("cannot read symlink '%s'", in);
274 _eject(&s, out, olen, buf, size);
Vincent Donnefortc2971352022-12-20 11:25:49 +0000275 } else if(S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode) ||
276 S_ISFIFO(s.st_mode) || S_ISSOCK(s.st_mode)) {
277 _eject(&s, out, olen, NULL, 0);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800278 } else {
279 die("Unknown '%s' (mode %d)?\n", in, s.st_mode);
280 }
281}
282
Mateus Azis023f67b2022-07-12 15:38:47 -0700283static void archive(const char* start, const char* prefix) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800284 char in[8192];
285 char out[8192];
286
287 strcpy(in, start);
288 strcpy(out, prefix);
289
290 _archive_dir(in, out, strlen(in), strlen(out));
291}
292
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700293static void read_canned_config(char* filename)
294{
295 int allocated = 8;
296 int used = 0;
297
298 canned_config =
299 (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry));
300
301 char line[CANNED_LINE_LENGTH];
302 FILE* f = fopen(filename, "r");
Mateus Azis023f67b2022-07-12 15:38:47 -0700303 if (f == NULL) die("failed to open canned file '%s'", filename);
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700304
305 while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) {
306 if (!line[0]) break;
307 if (used >= allocated) {
308 allocated *= 2;
309 canned_config = (struct fs_config_entry*)realloc(
310 canned_config, allocated * sizeof(struct fs_config_entry));
Mikhail Lappo26464912017-03-23 22:17:27 +0100311 if (canned_config == NULL) die("failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700312 }
313
314 struct fs_config_entry* cc = canned_config + used;
315
316 if (isspace(line[0])) {
317 cc->name = strdup("");
318 cc->uid = atoi(strtok(line, " \n"));
319 } else {
320 cc->name = strdup(strtok(line, " \n"));
321 cc->uid = atoi(strtok(NULL, " \n"));
322 }
323 cc->gid = atoi(strtok(NULL, " \n"));
324 cc->mode = strtol(strtok(NULL, " \n"), NULL, 8);
325 ++used;
326 }
327 if (used >= allocated) {
328 ++allocated;
329 canned_config = (struct fs_config_entry*)realloc(
330 canned_config, allocated * sizeof(struct fs_config_entry));
Mikhail Lappo26464912017-03-23 22:17:27 +0100331 if (canned_config == NULL) die("failed to reallocate memory");
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700332 }
333 canned_config[used].name = NULL;
334
335 fclose(f);
336}
337
Vincent Donnefort7f017742022-12-20 11:22:26 +0000338static const struct option long_options[] = {
339 { "dirname", required_argument, NULL, 'd' },
340 { "file", required_argument, NULL, 'f' },
341 { "help", no_argument, NULL, 'h' },
342 { NULL, 0, NULL, 0 },
343};
344
345static void usage(void)
346{
347 fprintf(stderr,
348 "Usage: mkbootfs [-d DIR|-F FILE] DIR...\n"
349 "\n"
350 "\t-d, --dirname=DIR: fs-config directory\n"
351 "\t-f, --file=FILE: Canned configuration file\n"
352 "\t-h, --help: Print this help\n"
353 );
354}
Doug Zongkerf2c3a832012-05-03 15:55:56 -0700355
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356int main(int argc, char *argv[])
357{
Vincent Donnefort7f017742022-12-20 11:22:26 +0000358 int opt, unused;
359
360 while ((opt = getopt_long(argc, argv, "hd:f:", long_options, &unused)) != -1) {
361 switch (opt) {
362 case 'd':
363 target_out_path = argv[optind - 1];
364 break;
365 case 'f':
366 read_canned_config(argv[optind - 1]);
367 break;
368 case 'h':
369 usage();
370 return 0;
371 default:
372 usage();
373 die("Unknown option %s", argv[optind - 1]);
374 }
Mateus Azis023f67b2022-07-12 15:38:47 -0700375 }
376
Vincent Donnefort7f017742022-12-20 11:22:26 +0000377 int num_dirs = argc - optind;
378 argv += optind;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379
Vincent Donnefort7f017742022-12-20 11:22:26 +0000380 if (num_dirs <= 0) {
381 usage();
382 die("no directories to process?!");
Thierry Strudeldf33ffa2015-07-09 09:50:31 -0700383 }
384
Vincent Donnefort7f017742022-12-20 11:22:26 +0000385 while(num_dirs-- > 0){
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386 char *x = strchr(*argv, '=');
387 if(x != 0) {
388 *x++ = 0;
389 } else {
390 x = "";
391 }
392
393 archive(*argv, x);
394
395 argv++;
396 }
397
398 _eject_trailer();
399
400 return 0;
401}