The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 2 | #include <getopt.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <unistd.h> |
| 6 | #include <string.h> |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 7 | #include <ctype.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 8 | |
| 9 | #include <sys/types.h> |
| 10 | #include <sys/stat.h> |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame^] | 11 | #include <sys/sysmacros.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 12 | #include <dirent.h> |
| 13 | |
| 14 | #include <stdarg.h> |
| 15 | #include <fcntl.h> |
| 16 | |
| 17 | #include <private/android_filesystem_config.h> |
Tom Cherry | 6ad4d0a | 2020-03-04 13:35:28 -0800 | [diff] [blame] | 18 | #include <private/fs_config.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | */ |
| 27 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 28 | static void die(const char* why, ...) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | 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 Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 39 | struct fs_config_entry { |
| 40 | char* name; |
| 41 | int uid, gid, mode; |
| 42 | }; |
| 43 | |
| 44 | static struct fs_config_entry* canned_config = NULL; |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 45 | static const char* target_out_path = NULL; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 46 | |
| 47 | /* Each line in the canned file should be a path plus three ints (uid, |
| 48 | * gid, mode). */ |
Doug Zongker | a865f6d | 2012-05-04 16:45:35 -0700 | [diff] [blame] | 49 | #ifdef PATH_MAX |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 50 | #define CANNED_LINE_LENGTH (PATH_MAX+100) |
Doug Zongker | a865f6d | 2012-05-04 16:45:35 -0700 | [diff] [blame] | 51 | #else |
| 52 | #define CANNED_LINE_LENGTH (1024) |
| 53 | #endif |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 54 | |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 55 | #define TRAILER "TRAILER!!!" |
| 56 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | static int verbose = 0; |
| 58 | static int total_size = 0; |
| 59 | |
| 60 | static void fix_stat(const char *path, struct stat *s) |
| 61 | { |
Nick Kralevich | e9e74f3 | 2013-02-07 14:22:12 -0800 | [diff] [blame] | 62 | uint64_t capabilities; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 63 | 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 Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 85 | unsigned st_mode = s->st_mode; |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 86 | 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 Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 88 | s->st_mode = (typeof(s->st_mode)) st_mode; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 89 | } |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame^] | 90 | |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static 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 Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame^] | 123 | major(s->st_rdev), |
| 124 | minor(s->st_rdev), |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 125 | olen + 1, |
| 126 | 0, |
| 127 | out, |
| 128 | 0 |
| 129 | ); |
| 130 | |
| 131 | total_size += 6 + 8*13 + olen + 1; |
| 132 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 133 | if(strlen(out) != (unsigned int)olen) die("ACK!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 134 | |
| 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 | |
| 146 | static void _eject_trailer() |
| 147 | { |
| 148 | struct stat s; |
| 149 | memset(&s, 0, sizeof(s)); |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 150 | _eject(&s, TRAILER, 10, 0, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 151 | |
| 152 | while(total_size & 0xff) { |
| 153 | total_size++; |
| 154 | putchar(0); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | static void _archive(char *in, char *out, int ilen, int olen); |
| 159 | |
| 160 | static int compare(const void* a, const void* b) { |
| 161 | return strcmp(*(const char**)a, *(const char**)b); |
| 162 | } |
| 163 | |
| 164 | static 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 Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 233 | |
| 234 | closedir(d); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static 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 Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame^] | 275 | } 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | } else { |
| 279 | die("Unknown '%s' (mode %d)?\n", in, s.st_mode); |
| 280 | } |
| 281 | } |
| 282 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 283 | static void archive(const char* start, const char* prefix) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 284 | 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 Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 293 | static 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 Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 303 | if (f == NULL) die("failed to open canned file '%s'", filename); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 304 | |
| 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 Lappo | 2646491 | 2017-03-23 22:17:27 +0100 | [diff] [blame] | 311 | if (canned_config == NULL) die("failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 312 | } |
| 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 Lappo | 2646491 | 2017-03-23 22:17:27 +0100 | [diff] [blame] | 331 | if (canned_config == NULL) die("failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 332 | } |
| 333 | canned_config[used].name = NULL; |
| 334 | |
| 335 | fclose(f); |
| 336 | } |
| 337 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 338 | static 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 | |
| 345 | static 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 Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 355 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | int main(int argc, char *argv[]) |
| 357 | { |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 358 | 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 Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 375 | } |
| 376 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 377 | int num_dirs = argc - optind; |
| 378 | argv += optind; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 380 | if (num_dirs <= 0) { |
| 381 | usage(); |
| 382 | die("no directories to process?!"); |
Thierry Strudel | df33ffa | 2015-07-09 09:50:31 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 385 | while(num_dirs-- > 0){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 386 | 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 | } |