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> |
| 11 | #include <dirent.h> |
| 12 | |
| 13 | #include <stdarg.h> |
| 14 | #include <fcntl.h> |
| 15 | |
| 16 | #include <private/android_filesystem_config.h> |
Tom Cherry | 6ad4d0a | 2020-03-04 13:35:28 -0800 | [diff] [blame] | 17 | #include <private/fs_config.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 18 | |
| 19 | /* NOTES |
| 20 | ** |
| 21 | ** - see buffer-format.txt from the linux kernel docs for |
| 22 | ** an explanation of this file format |
| 23 | ** - dotfiles are ignored |
| 24 | ** - directories named 'root' are ignored |
| 25 | ** - device notes, pipes, etc are not supported (error) |
| 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 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void _eject(struct stat *s, char *out, int olen, char *data, unsigned datasize) |
| 93 | { |
| 94 | // Nothing is special about this value, just picked something in the |
| 95 | // approximate range that was being used already, and avoiding small |
| 96 | // values which may be special. |
| 97 | static unsigned next_inode = 300000; |
| 98 | |
| 99 | while(total_size & 3) { |
| 100 | total_size++; |
| 101 | putchar(0); |
| 102 | } |
| 103 | |
| 104 | fix_stat(out, s); |
| 105 | // fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode); |
| 106 | |
| 107 | printf("%06x%08x%08x%08x%08x%08x%08x" |
| 108 | "%08x%08x%08x%08x%08x%08x%08x%s%c", |
| 109 | 0x070701, |
| 110 | next_inode++, // s.st_ino, |
| 111 | s->st_mode, |
| 112 | 0, // s.st_uid, |
| 113 | 0, // s.st_gid, |
| 114 | 1, // s.st_nlink, |
| 115 | 0, // s.st_mtime, |
| 116 | datasize, |
| 117 | 0, // volmajor |
| 118 | 0, // volminor |
| 119 | 0, // devmajor |
| 120 | 0, // devminor, |
| 121 | olen + 1, |
| 122 | 0, |
| 123 | out, |
| 124 | 0 |
| 125 | ); |
| 126 | |
| 127 | total_size += 6 + 8*13 + olen + 1; |
| 128 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 129 | if(strlen(out) != (unsigned int)olen) die("ACK!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | |
| 131 | while(total_size & 3) { |
| 132 | total_size++; |
| 133 | putchar(0); |
| 134 | } |
| 135 | |
| 136 | if(datasize) { |
| 137 | fwrite(data, datasize, 1, stdout); |
| 138 | total_size += datasize; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void _eject_trailer() |
| 143 | { |
| 144 | struct stat s; |
| 145 | memset(&s, 0, sizeof(s)); |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 146 | _eject(&s, TRAILER, 10, 0, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | |
| 148 | while(total_size & 0xff) { |
| 149 | total_size++; |
| 150 | putchar(0); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | static void _archive(char *in, char *out, int ilen, int olen); |
| 155 | |
| 156 | static int compare(const void* a, const void* b) { |
| 157 | return strcmp(*(const char**)a, *(const char**)b); |
| 158 | } |
| 159 | |
| 160 | static void _archive_dir(char *in, char *out, int ilen, int olen) |
| 161 | { |
| 162 | int i, t; |
| 163 | DIR *d; |
| 164 | struct dirent *de; |
| 165 | |
| 166 | if(verbose) { |
| 167 | fprintf(stderr,"_archive_dir('%s','%s',%d,%d)\n", |
| 168 | in, out, ilen, olen); |
| 169 | } |
| 170 | |
| 171 | d = opendir(in); |
| 172 | if(d == 0) die("cannot open directory '%s'", in); |
| 173 | |
| 174 | int size = 32; |
| 175 | int entries = 0; |
| 176 | char** names = malloc(size * sizeof(char*)); |
| 177 | if (names == NULL) { |
| 178 | fprintf(stderr, "failed to allocate dir names array (size %d)\n", size); |
| 179 | exit(1); |
| 180 | } |
| 181 | |
| 182 | while((de = readdir(d)) != 0){ |
| 183 | /* xxx: feature? maybe some dotfiles are okay */ |
| 184 | if(de->d_name[0] == '.') continue; |
| 185 | |
| 186 | /* xxx: hack. use a real exclude list */ |
| 187 | if(!strcmp(de->d_name, "root")) continue; |
| 188 | |
| 189 | if (entries >= size) { |
| 190 | size *= 2; |
| 191 | names = realloc(names, size * sizeof(char*)); |
| 192 | if (names == NULL) { |
| 193 | fprintf(stderr, "failed to reallocate dir names array (size %d)\n", |
| 194 | size); |
| 195 | exit(1); |
| 196 | } |
| 197 | } |
| 198 | names[entries] = strdup(de->d_name); |
| 199 | if (names[entries] == NULL) { |
| 200 | fprintf(stderr, "failed to strdup name \"%s\"\n", |
| 201 | de->d_name); |
| 202 | exit(1); |
| 203 | } |
| 204 | ++entries; |
| 205 | } |
| 206 | |
| 207 | qsort(names, entries, sizeof(char*), compare); |
| 208 | |
| 209 | for (i = 0; i < entries; ++i) { |
| 210 | t = strlen(names[i]); |
| 211 | in[ilen] = '/'; |
| 212 | memcpy(in + ilen + 1, names[i], t + 1); |
| 213 | |
| 214 | if(olen > 0) { |
| 215 | out[olen] = '/'; |
| 216 | memcpy(out + olen + 1, names[i], t + 1); |
| 217 | _archive(in, out, ilen + t + 1, olen + t + 1); |
| 218 | } else { |
| 219 | memcpy(out, names[i], t + 1); |
| 220 | _archive(in, out, ilen + t + 1, t); |
| 221 | } |
| 222 | |
| 223 | in[ilen] = 0; |
| 224 | out[olen] = 0; |
| 225 | |
| 226 | free(names[i]); |
| 227 | } |
| 228 | free(names); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 229 | |
| 230 | closedir(d); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static void _archive(char *in, char *out, int ilen, int olen) |
| 234 | { |
| 235 | struct stat s; |
| 236 | |
| 237 | if(verbose) { |
| 238 | fprintf(stderr,"_archive('%s','%s',%d,%d)\n", |
| 239 | in, out, ilen, olen); |
| 240 | } |
| 241 | |
| 242 | if(lstat(in, &s)) die("could not stat '%s'\n", in); |
| 243 | |
| 244 | if(S_ISREG(s.st_mode)){ |
| 245 | char *tmp; |
| 246 | int fd; |
| 247 | |
| 248 | fd = open(in, O_RDONLY); |
| 249 | if(fd < 0) die("cannot open '%s' for read", in); |
| 250 | |
| 251 | tmp = (char*) malloc(s.st_size); |
| 252 | if(tmp == 0) die("cannot allocate %d bytes", s.st_size); |
| 253 | |
| 254 | if(read(fd, tmp, s.st_size) != s.st_size) { |
| 255 | die("cannot read %d bytes", s.st_size); |
| 256 | } |
| 257 | |
| 258 | _eject(&s, out, olen, tmp, s.st_size); |
| 259 | |
| 260 | free(tmp); |
| 261 | close(fd); |
| 262 | } else if(S_ISDIR(s.st_mode)) { |
| 263 | _eject(&s, out, olen, 0, 0); |
| 264 | _archive_dir(in, out, ilen, olen); |
| 265 | } else if(S_ISLNK(s.st_mode)) { |
| 266 | char buf[1024]; |
| 267 | int size; |
| 268 | size = readlink(in, buf, 1024); |
| 269 | if(size < 0) die("cannot read symlink '%s'", in); |
| 270 | _eject(&s, out, olen, buf, size); |
| 271 | } else { |
| 272 | die("Unknown '%s' (mode %d)?\n", in, s.st_mode); |
| 273 | } |
| 274 | } |
| 275 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 276 | static void archive(const char* start, const char* prefix) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 277 | char in[8192]; |
| 278 | char out[8192]; |
| 279 | |
| 280 | strcpy(in, start); |
| 281 | strcpy(out, prefix); |
| 282 | |
| 283 | _archive_dir(in, out, strlen(in), strlen(out)); |
| 284 | } |
| 285 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 286 | static void read_canned_config(char* filename) |
| 287 | { |
| 288 | int allocated = 8; |
| 289 | int used = 0; |
| 290 | |
| 291 | canned_config = |
| 292 | (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry)); |
| 293 | |
| 294 | char line[CANNED_LINE_LENGTH]; |
| 295 | FILE* f = fopen(filename, "r"); |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 296 | if (f == NULL) die("failed to open canned file '%s'", filename); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 297 | |
| 298 | while (fgets(line, CANNED_LINE_LENGTH, f) != NULL) { |
| 299 | if (!line[0]) break; |
| 300 | if (used >= allocated) { |
| 301 | allocated *= 2; |
| 302 | canned_config = (struct fs_config_entry*)realloc( |
| 303 | canned_config, allocated * sizeof(struct fs_config_entry)); |
Mikhail Lappo | 2646491 | 2017-03-23 22:17:27 +0100 | [diff] [blame] | 304 | if (canned_config == NULL) die("failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | struct fs_config_entry* cc = canned_config + used; |
| 308 | |
| 309 | if (isspace(line[0])) { |
| 310 | cc->name = strdup(""); |
| 311 | cc->uid = atoi(strtok(line, " \n")); |
| 312 | } else { |
| 313 | cc->name = strdup(strtok(line, " \n")); |
| 314 | cc->uid = atoi(strtok(NULL, " \n")); |
| 315 | } |
| 316 | cc->gid = atoi(strtok(NULL, " \n")); |
| 317 | cc->mode = strtol(strtok(NULL, " \n"), NULL, 8); |
| 318 | ++used; |
| 319 | } |
| 320 | if (used >= allocated) { |
| 321 | ++allocated; |
| 322 | canned_config = (struct fs_config_entry*)realloc( |
| 323 | canned_config, allocated * sizeof(struct fs_config_entry)); |
Mikhail Lappo | 2646491 | 2017-03-23 22:17:27 +0100 | [diff] [blame] | 324 | if (canned_config == NULL) die("failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 325 | } |
| 326 | canned_config[used].name = NULL; |
| 327 | |
| 328 | fclose(f); |
| 329 | } |
| 330 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame^] | 331 | static const struct option long_options[] = { |
| 332 | { "dirname", required_argument, NULL, 'd' }, |
| 333 | { "file", required_argument, NULL, 'f' }, |
| 334 | { "help", no_argument, NULL, 'h' }, |
| 335 | { NULL, 0, NULL, 0 }, |
| 336 | }; |
| 337 | |
| 338 | static void usage(void) |
| 339 | { |
| 340 | fprintf(stderr, |
| 341 | "Usage: mkbootfs [-d DIR|-F FILE] DIR...\n" |
| 342 | "\n" |
| 343 | "\t-d, --dirname=DIR: fs-config directory\n" |
| 344 | "\t-f, --file=FILE: Canned configuration file\n" |
| 345 | "\t-h, --help: Print this help\n" |
| 346 | ); |
| 347 | } |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 348 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 349 | int main(int argc, char *argv[]) |
| 350 | { |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame^] | 351 | int opt, unused; |
| 352 | |
| 353 | while ((opt = getopt_long(argc, argv, "hd:f:", long_options, &unused)) != -1) { |
| 354 | switch (opt) { |
| 355 | case 'd': |
| 356 | target_out_path = argv[optind - 1]; |
| 357 | break; |
| 358 | case 'f': |
| 359 | read_canned_config(argv[optind - 1]); |
| 360 | break; |
| 361 | case 'h': |
| 362 | usage(); |
| 363 | return 0; |
| 364 | default: |
| 365 | usage(); |
| 366 | die("Unknown option %s", argv[optind - 1]); |
| 367 | } |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame^] | 370 | int num_dirs = argc - optind; |
| 371 | argv += optind; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 372 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame^] | 373 | if (num_dirs <= 0) { |
| 374 | usage(); |
| 375 | die("no directories to process?!"); |
Thierry Strudel | df33ffa | 2015-07-09 09:50:31 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame^] | 378 | while(num_dirs-- > 0){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 379 | char *x = strchr(*argv, '='); |
| 380 | if(x != 0) { |
| 381 | *x++ = 0; |
| 382 | } else { |
| 383 | x = ""; |
| 384 | } |
| 385 | |
| 386 | archive(*argv, x); |
| 387 | |
| 388 | argv++; |
| 389 | } |
| 390 | |
| 391 | _eject_trailer(); |
| 392 | |
| 393 | return 0; |
| 394 | } |