The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | |
Colin Cross | 6f2ae48 | 2023-01-09 13:01:46 -0800 | [diff] [blame] | 2 | #include <ctype.h> |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 3 | #include <dirent.h> |
Colin Cross | 6f2ae48 | 2023-01-09 13:01:46 -0800 | [diff] [blame] | 4 | #include <err.h> |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 5 | #include <errno.h> |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 6 | #include <fcntl.h> |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 7 | #include <getopt.h> |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 8 | #include <stdarg.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 11 | #include <string.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 12 | #include <sys/stat.h> |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 13 | #include <sys/sysmacros.h> |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 14 | #include <sys/types.h> |
| 15 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 17 | #include <linux/kdev_t.h> |
| 18 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <private/android_filesystem_config.h> |
Tom Cherry | 6ad4d0a | 2020-03-04 13:35:28 -0800 | [diff] [blame] | 20 | #include <private/fs_config.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | |
| 22 | /* NOTES |
| 23 | ** |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 24 | ** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt |
| 25 | ** for an explanation of this file format |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | ** - dotfiles are ignored |
| 27 | ** - directories named 'root' are ignored |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 28 | */ |
| 29 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 30 | struct fs_config_entry { |
| 31 | char* name; |
| 32 | int uid, gid, mode; |
| 33 | }; |
| 34 | |
| 35 | static struct fs_config_entry* canned_config = NULL; |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 36 | static const char* target_out_path = NULL; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 37 | |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 38 | #define TRAILER "TRAILER!!!" |
| 39 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | static int total_size = 0; |
| 41 | |
| 42 | static void fix_stat(const char *path, struct stat *s) |
| 43 | { |
Nick Kralevich | e9e74f3 | 2013-02-07 14:22:12 -0800 | [diff] [blame] | 44 | uint64_t capabilities; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 45 | 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 Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 67 | unsigned st_mode = s->st_mode; |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 68 | 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 Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 70 | s->st_mode = (typeof(s->st_mode)) st_mode; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 71 | } |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 72 | |
| 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 78 | static void _eject(struct stat *s, const char *out, int olen, char *data, unsigned datasize) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | { |
| 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 Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 105 | major(s->st_rdev), |
| 106 | minor(s->st_rdev), |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | olen + 1, |
| 108 | 0, |
| 109 | out, |
| 110 | 0 |
| 111 | ); |
| 112 | |
| 113 | total_size += 6 + 8*13 + olen + 1; |
| 114 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 115 | if(strlen(out) != (unsigned int)olen) errx(1, "ACK!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 116 | |
| 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 | |
| 128 | static void _eject_trailer() |
| 129 | { |
| 130 | struct stat s; |
| 131 | memset(&s, 0, sizeof(s)); |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 132 | _eject(&s, TRAILER, 10, 0, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | |
| 134 | while(total_size & 0xff) { |
| 135 | total_size++; |
| 136 | putchar(0); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static void _archive(char *in, char *out, int ilen, int olen); |
| 141 | |
| 142 | static int compare(const void* a, const void* b) { |
| 143 | return strcmp(*(const char**)a, *(const char**)b); |
| 144 | } |
| 145 | |
| 146 | static void _archive_dir(char *in, char *out, int ilen, int olen) |
| 147 | { |
| 148 | int i, t; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | struct dirent *de; |
| 150 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 151 | DIR* d = opendir(in); |
| 152 | if (d == NULL) err(1, "cannot open directory '%s'", in); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 154 | // TODO: switch to std::vector |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 155 | int size = 32; |
| 156 | int entries = 0; |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 157 | char** names = (char**) malloc(size * sizeof(char*)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | if (names == NULL) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 159 | errx(1, "failed to allocate dir names array (size %d)", size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | } |
| 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 Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 171 | names = (char**) realloc(names, size * sizeof(char*)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 172 | if (names == NULL) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 173 | errx(1, "failed to reallocate dir names array (size %d)", size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | names[entries] = strdup(de->d_name); |
| 177 | if (names[entries] == NULL) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 178 | errx(1, "failed to strdup name \"%s\"", de->d_name); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 179 | } |
| 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 Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 205 | |
| 206 | closedir(d); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | static void _archive(char *in, char *out, int ilen, int olen) |
| 210 | { |
| 211 | struct stat s; |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 212 | if(lstat(in, &s)) err(1, "could not stat '%s'", in); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 213 | |
| 214 | if(S_ISREG(s.st_mode)){ |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 215 | int fd = open(in, O_RDONLY); |
| 216 | if(fd < 0) err(1, "cannot open '%s' for read", in); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 217 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 218 | char* tmp = (char*) malloc(s.st_size); |
| 219 | if(tmp == 0) errx(1, "cannot allocate %zd bytes", s.st_size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 220 | |
| 221 | if(read(fd, tmp, s.st_size) != s.st_size) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 222 | err(1, "cannot read %zd bytes", s.st_size); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 223 | } |
| 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 Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 236 | if(size < 0) err(1, "cannot read symlink '%s'", in); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 237 | _eject(&s, out, olen, buf, size); |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 238 | } 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 Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | } else { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 242 | errx(1, "Unknown '%s' (mode %d)?", in, s.st_mode); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 246 | static void archive(const char* start, const char* prefix) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | 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 Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 256 | static 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 Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 264 | FILE* fp = fopen(filename, "r"); |
| 265 | if (fp == NULL) err(1, "failed to open canned file '%s'", filename); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 266 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 267 | char* line = NULL; |
| 268 | size_t allocated_len; |
| 269 | while (getline(&line, &allocated_len, fp) != -1) { |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 270 | 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 Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 275 | if (canned_config == NULL) errx(1, "failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 276 | } |
| 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 Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 295 | if (canned_config == NULL) errx(1, "failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 296 | } |
| 297 | canned_config[used].name = NULL; |
| 298 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 299 | free(line); |
| 300 | fclose(fp); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 303 | static void devnodes_desc_error(const char* filename, unsigned long line_num, |
| 304 | const char* msg) |
| 305 | { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 306 | errx(1, "failed to read nodes desc file '%s' line %lu: %s", filename, line_num, msg); |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static 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 | |
| 322 | static 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 | |
| 348 | static void append_devnodes_desc(const char* filename) |
| 349 | { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 350 | FILE* fp = fopen(filename, "re"); |
| 351 | if (!fp) err(1, "failed to open nodes description file '%s'", filename); |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 352 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 353 | unsigned long line_num = 0; |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 354 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 355 | char* line = NULL; |
| 356 | size_t allocated_len; |
| 357 | while (getline(&line, &allocated_len, fp) != -1) { |
| 358 | char *type, *path, *args; |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 359 | |
| 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 Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 392 | fclose(fp); |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 395 | static const struct option long_options[] = { |
| 396 | { "dirname", required_argument, NULL, 'd' }, |
| 397 | { "file", required_argument, NULL, 'f' }, |
| 398 | { "help", no_argument, NULL, 'h' }, |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 399 | { "nodes", required_argument, NULL, 'n' }, |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 400 | { NULL, 0, NULL, 0 }, |
| 401 | }; |
| 402 | |
| 403 | static void usage(void) |
| 404 | { |
| 405 | fprintf(stderr, |
Keir Fraser | 7ae5864 | 2024-05-13 13:09:18 +0000 | [diff] [blame] | 406 | "Usage: mkbootfs [-n FILE] [-d DIR|-f FILE] DIR...\n" |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 407 | "\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 Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 411 | "\t-n, --nodes=FILE: Dev nodes description file\n" |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 412 | "\n" |
| 413 | "Dev nodes description:\n" |
Keir Fraser | b2016ec | 2024-05-13 10:07:26 +0000 | [diff] [blame] | 414 | "\t[dir|nod] [perms] [uid] [gid] [c|b] [major] [minor]\n" |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 415 | "\tExample:\n" |
| 416 | "\t\t# My device nodes\n" |
| 417 | "\t\tdir dev 0755 0 0\n" |
Keir Fraser | b2016ec | 2024-05-13 10:07:26 +0000 | [diff] [blame] | 418 | "\t\tnod dev/null 0600 0 0 c 1 3\n" |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 419 | ); |
| 420 | } |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 421 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 422 | int main(int argc, char *argv[]) |
| 423 | { |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 424 | int opt, unused; |
| 425 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 426 | while ((opt = getopt_long(argc, argv, "hd:f:n:", long_options, &unused)) != -1) { |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 427 | 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 Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 437 | case 'n': |
| 438 | append_devnodes_desc(argv[optind - 1]); |
| 439 | break; |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 440 | default: |
| 441 | usage(); |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 442 | errx(1, "Unknown option %s", argv[optind - 1]); |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 443 | } |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 446 | int num_dirs = argc - optind; |
| 447 | argv += optind; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 449 | while (num_dirs-- > 0){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 | char *x = strchr(*argv, '='); |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 451 | if (x != nullptr) { |
| 452 | *x++ = '\0'; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 453 | } |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 454 | archive(*argv, x ?: ""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 455 | |
| 456 | argv++; |
| 457 | } |
| 458 | |
| 459 | _eject_trailer(); |
| 460 | |
| 461 | return 0; |
| 462 | } |