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 | |
terryguan | e7f1d41 | 2024-10-29 13:20:46 -0700 | [diff] [blame] | 22 | #include <android-base/file.h> |
| 23 | #include <string> |
| 24 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | /* NOTES |
| 26 | ** |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 27 | ** - see https://www.kernel.org/doc/Documentation/early-userspace/buffer-format.txt |
| 28 | ** for an explanation of this file format |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | ** - dotfiles are ignored |
| 30 | ** - directories named 'root' are ignored |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 33 | struct fs_config_entry { |
| 34 | char* name; |
| 35 | int uid, gid, mode; |
| 36 | }; |
| 37 | |
| 38 | static struct fs_config_entry* canned_config = NULL; |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 39 | static const char* target_out_path = NULL; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 40 | |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 41 | #define TRAILER "TRAILER!!!" |
| 42 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | static int total_size = 0; |
| 44 | |
| 45 | static void fix_stat(const char *path, struct stat *s) |
| 46 | { |
Nick Kralevich | e9e74f3 | 2013-02-07 14:22:12 -0800 | [diff] [blame] | 47 | uint64_t capabilities; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 48 | if (canned_config) { |
| 49 | // Use the list of file uid/gid/modes loaded from the file |
| 50 | // given with -f. |
| 51 | |
| 52 | struct fs_config_entry* empty_path_config = NULL; |
| 53 | struct fs_config_entry* p; |
| 54 | for (p = canned_config; p->name; ++p) { |
| 55 | if (!p->name[0]) { |
| 56 | empty_path_config = p; |
| 57 | } |
| 58 | if (strcmp(p->name, path) == 0) { |
| 59 | s->st_uid = p->uid; |
| 60 | s->st_gid = p->gid; |
| 61 | s->st_mode = p->mode | (s->st_mode & ~07777); |
| 62 | return; |
| 63 | } |
| 64 | } |
| 65 | s->st_uid = empty_path_config->uid; |
| 66 | s->st_gid = empty_path_config->gid; |
| 67 | s->st_mode = empty_path_config->mode | (s->st_mode & ~07777); |
| 68 | } else { |
| 69 | // Use the compiled-in fs_config() function. |
Mark Salyzyn | 5ce7575 | 2014-05-15 15:05:25 -0700 | [diff] [blame] | 70 | unsigned st_mode = s->st_mode; |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 71 | int is_dir = S_ISDIR(s->st_mode) || strcmp(path, TRAILER) == 0; |
| 72 | 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] | 73 | s->st_mode = (typeof(s->st_mode)) st_mode; |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 74 | } |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 75 | |
| 76 | if (S_ISREG(s->st_mode) || S_ISDIR(s->st_mode) || S_ISLNK(s->st_mode)) { |
| 77 | s->st_rdev = 0; |
| 78 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 81 | 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] | 82 | { |
| 83 | // Nothing is special about this value, just picked something in the |
| 84 | // approximate range that was being used already, and avoiding small |
| 85 | // values which may be special. |
| 86 | static unsigned next_inode = 300000; |
| 87 | |
| 88 | while(total_size & 3) { |
| 89 | total_size++; |
| 90 | putchar(0); |
| 91 | } |
| 92 | |
| 93 | fix_stat(out, s); |
| 94 | // fprintf(stderr, "_eject %s: mode=0%o\n", out, s->st_mode); |
| 95 | |
| 96 | printf("%06x%08x%08x%08x%08x%08x%08x" |
| 97 | "%08x%08x%08x%08x%08x%08x%08x%s%c", |
| 98 | 0x070701, |
| 99 | next_inode++, // s.st_ino, |
| 100 | s->st_mode, |
| 101 | 0, // s.st_uid, |
| 102 | 0, // s.st_gid, |
| 103 | 1, // s.st_nlink, |
| 104 | 0, // s.st_mtime, |
| 105 | datasize, |
| 106 | 0, // volmajor |
| 107 | 0, // volminor |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 108 | major(s->st_rdev), |
| 109 | minor(s->st_rdev), |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 110 | olen + 1, |
| 111 | 0, |
| 112 | out, |
| 113 | 0 |
| 114 | ); |
| 115 | |
| 116 | total_size += 6 + 8*13 + olen + 1; |
| 117 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 118 | if(strlen(out) != (unsigned int)olen) errx(1, "ACK!"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 119 | |
| 120 | while(total_size & 3) { |
| 121 | total_size++; |
| 122 | putchar(0); |
| 123 | } |
| 124 | |
| 125 | if(datasize) { |
| 126 | fwrite(data, datasize, 1, stdout); |
| 127 | total_size += datasize; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void _eject_trailer() |
| 132 | { |
| 133 | struct stat s; |
| 134 | memset(&s, 0, sizeof(s)); |
Tao Bao | 3b5fbd8 | 2016-10-07 10:20:52 -0700 | [diff] [blame] | 135 | _eject(&s, TRAILER, 10, 0, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 136 | |
| 137 | while(total_size & 0xff) { |
| 138 | total_size++; |
| 139 | putchar(0); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static void _archive(char *in, char *out, int ilen, int olen); |
| 144 | |
| 145 | static int compare(const void* a, const void* b) { |
| 146 | return strcmp(*(const char**)a, *(const char**)b); |
| 147 | } |
| 148 | |
| 149 | static void _archive_dir(char *in, char *out, int ilen, int olen) |
| 150 | { |
| 151 | int i, t; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 152 | struct dirent *de; |
| 153 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 154 | DIR* d = opendir(in); |
| 155 | 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] | 156 | |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 157 | // TODO: switch to std::vector |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 158 | int size = 32; |
| 159 | int entries = 0; |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 160 | char** names = (char**) malloc(size * sizeof(char*)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 161 | if (names == NULL) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 162 | 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] | 163 | } |
| 164 | |
| 165 | while((de = readdir(d)) != 0){ |
| 166 | /* xxx: feature? maybe some dotfiles are okay */ |
| 167 | if(de->d_name[0] == '.') continue; |
| 168 | |
| 169 | /* xxx: hack. use a real exclude list */ |
| 170 | if(!strcmp(de->d_name, "root")) continue; |
| 171 | |
| 172 | if (entries >= size) { |
| 173 | size *= 2; |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 174 | names = (char**) realloc(names, size * sizeof(char*)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 175 | if (names == NULL) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 176 | 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] | 177 | } |
| 178 | } |
| 179 | names[entries] = strdup(de->d_name); |
| 180 | if (names[entries] == NULL) { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 181 | 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] | 182 | } |
| 183 | ++entries; |
| 184 | } |
| 185 | |
| 186 | qsort(names, entries, sizeof(char*), compare); |
| 187 | |
| 188 | for (i = 0; i < entries; ++i) { |
| 189 | t = strlen(names[i]); |
| 190 | in[ilen] = '/'; |
| 191 | memcpy(in + ilen + 1, names[i], t + 1); |
| 192 | |
| 193 | if(olen > 0) { |
| 194 | out[olen] = '/'; |
| 195 | memcpy(out + olen + 1, names[i], t + 1); |
| 196 | _archive(in, out, ilen + t + 1, olen + t + 1); |
| 197 | } else { |
| 198 | memcpy(out, names[i], t + 1); |
| 199 | _archive(in, out, ilen + t + 1, t); |
| 200 | } |
| 201 | |
| 202 | in[ilen] = 0; |
| 203 | out[olen] = 0; |
| 204 | |
| 205 | free(names[i]); |
| 206 | } |
| 207 | free(names); |
Elliott Hughes | 14e28d3 | 2013-10-29 14:12:46 -0700 | [diff] [blame] | 208 | |
| 209 | closedir(d); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static void _archive(char *in, char *out, int ilen, int olen) |
| 213 | { |
| 214 | struct stat s; |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 215 | 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] | 216 | |
| 217 | if(S_ISREG(s.st_mode)){ |
terryguan | e7f1d41 | 2024-10-29 13:20:46 -0700 | [diff] [blame] | 218 | std::string content; |
| 219 | if (!android::base::ReadFileToString(in, &content)) { |
| 220 | err(1, "cannot read '%s'", in); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 221 | } |
| 222 | |
terryguan | e7f1d41 | 2024-10-29 13:20:46 -0700 | [diff] [blame] | 223 | _eject(&s, out, olen, content.data(), content.size()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 224 | } else if(S_ISDIR(s.st_mode)) { |
| 225 | _eject(&s, out, olen, 0, 0); |
| 226 | _archive_dir(in, out, ilen, olen); |
| 227 | } else if(S_ISLNK(s.st_mode)) { |
| 228 | char buf[1024]; |
| 229 | int size; |
| 230 | size = readlink(in, buf, 1024); |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 231 | 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] | 232 | _eject(&s, out, olen, buf, size); |
Vincent Donnefort | c297135 | 2022-12-20 11:25:49 +0000 | [diff] [blame] | 233 | } else if(S_ISBLK(s.st_mode) || S_ISCHR(s.st_mode) || |
| 234 | S_ISFIFO(s.st_mode) || S_ISSOCK(s.st_mode)) { |
| 235 | _eject(&s, out, olen, NULL, 0); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 236 | } else { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 237 | 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] | 238 | } |
| 239 | } |
| 240 | |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 241 | static void archive(const char* start, const char* prefix) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 242 | char in[8192]; |
| 243 | char out[8192]; |
| 244 | |
| 245 | strcpy(in, start); |
| 246 | strcpy(out, prefix); |
| 247 | |
| 248 | _archive_dir(in, out, strlen(in), strlen(out)); |
| 249 | } |
| 250 | |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 251 | static void read_canned_config(char* filename) |
| 252 | { |
| 253 | int allocated = 8; |
| 254 | int used = 0; |
| 255 | |
| 256 | canned_config = |
| 257 | (struct fs_config_entry*)malloc(allocated * sizeof(struct fs_config_entry)); |
| 258 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 259 | FILE* fp = fopen(filename, "r"); |
| 260 | if (fp == NULL) err(1, "failed to open canned file '%s'", filename); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 261 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 262 | char* line = NULL; |
| 263 | size_t allocated_len; |
| 264 | while (getline(&line, &allocated_len, fp) != -1) { |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 265 | if (!line[0]) break; |
| 266 | if (used >= allocated) { |
| 267 | allocated *= 2; |
| 268 | canned_config = (struct fs_config_entry*)realloc( |
| 269 | canned_config, allocated * sizeof(struct fs_config_entry)); |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 270 | if (canned_config == NULL) errx(1, "failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | struct fs_config_entry* cc = canned_config + used; |
| 274 | |
| 275 | if (isspace(line[0])) { |
| 276 | cc->name = strdup(""); |
| 277 | cc->uid = atoi(strtok(line, " \n")); |
| 278 | } else { |
| 279 | cc->name = strdup(strtok(line, " \n")); |
| 280 | cc->uid = atoi(strtok(NULL, " \n")); |
| 281 | } |
| 282 | cc->gid = atoi(strtok(NULL, " \n")); |
| 283 | cc->mode = strtol(strtok(NULL, " \n"), NULL, 8); |
| 284 | ++used; |
| 285 | } |
| 286 | if (used >= allocated) { |
| 287 | ++allocated; |
| 288 | canned_config = (struct fs_config_entry*)realloc( |
| 289 | canned_config, allocated * sizeof(struct fs_config_entry)); |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 290 | if (canned_config == NULL) errx(1, "failed to reallocate memory"); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 291 | } |
| 292 | canned_config[used].name = NULL; |
| 293 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 294 | free(line); |
| 295 | fclose(fp); |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 298 | static void devnodes_desc_error(const char* filename, unsigned long line_num, |
| 299 | const char* msg) |
| 300 | { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 301 | 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] | 302 | } |
| 303 | |
| 304 | static int append_devnodes_desc_dir(char* path, char* args) |
| 305 | { |
| 306 | struct stat s; |
| 307 | |
| 308 | if (sscanf(args, "%o %d %d", &s.st_mode, &s.st_uid, &s.st_gid) != 3) return -1; |
| 309 | |
| 310 | s.st_mode |= S_IFDIR; |
| 311 | |
| 312 | _eject(&s, path, strlen(path), NULL, 0); |
| 313 | |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | static int append_devnodes_desc_nod(char* path, char* args) |
| 318 | { |
| 319 | int minor, major; |
| 320 | struct stat s; |
| 321 | char dev; |
| 322 | |
| 323 | if (sscanf(args, "%o %d %d %c %d %d", &s.st_mode, &s.st_uid, &s.st_gid, |
| 324 | &dev, &major, &minor) != 6) return -1; |
| 325 | |
| 326 | s.st_rdev = MKDEV(major, minor); |
| 327 | switch (dev) { |
| 328 | case 'b': |
| 329 | s.st_mode |= S_IFBLK; |
| 330 | break; |
| 331 | case 'c': |
| 332 | s.st_mode |= S_IFCHR; |
| 333 | break; |
| 334 | default: |
| 335 | return -1; |
| 336 | } |
| 337 | |
| 338 | _eject(&s, path, strlen(path), NULL, 0); |
| 339 | |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | static void append_devnodes_desc(const char* filename) |
| 344 | { |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 345 | FILE* fp = fopen(filename, "re"); |
| 346 | if (!fp) err(1, "failed to open nodes description file '%s'", filename); |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 347 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 348 | unsigned long line_num = 0; |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 349 | |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 350 | char* line = NULL; |
| 351 | size_t allocated_len; |
| 352 | while (getline(&line, &allocated_len, fp) != -1) { |
| 353 | char *type, *path, *args; |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 354 | |
| 355 | line_num++; |
| 356 | |
| 357 | if (*line == '#') continue; |
| 358 | |
| 359 | if (!(type = strtok(line, " \t"))) { |
| 360 | devnodes_desc_error(filename, line_num, "a type is missing"); |
| 361 | } |
| 362 | |
| 363 | if (*type == '\n') continue; |
| 364 | |
| 365 | if (!(path = strtok(NULL, " \t"))) { |
| 366 | devnodes_desc_error(filename, line_num, "a path is missing"); |
| 367 | } |
| 368 | |
| 369 | if (!(args = strtok(NULL, "\n"))) { |
| 370 | devnodes_desc_error(filename, line_num, "args are missing"); |
| 371 | } |
| 372 | |
| 373 | if (!strcmp(type, "dir")) { |
| 374 | if (append_devnodes_desc_dir(path, args)) { |
| 375 | devnodes_desc_error(filename, line_num, "bad arguments for dir"); |
| 376 | } |
| 377 | } else if (!strcmp(type, "nod")) { |
| 378 | if (append_devnodes_desc_nod(path, args)) { |
| 379 | devnodes_desc_error(filename, line_num, "bad arguments for nod"); |
| 380 | } |
| 381 | } else { |
| 382 | devnodes_desc_error(filename, line_num, "type unknown"); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | free(line); |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 387 | fclose(fp); |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 390 | static const struct option long_options[] = { |
| 391 | { "dirname", required_argument, NULL, 'd' }, |
| 392 | { "file", required_argument, NULL, 'f' }, |
| 393 | { "help", no_argument, NULL, 'h' }, |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 394 | { "nodes", required_argument, NULL, 'n' }, |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 395 | { NULL, 0, NULL, 0 }, |
| 396 | }; |
| 397 | |
| 398 | static void usage(void) |
| 399 | { |
| 400 | fprintf(stderr, |
Keir Fraser | 7ae5864 | 2024-05-13 13:09:18 +0000 | [diff] [blame] | 401 | "Usage: mkbootfs [-n FILE] [-d DIR|-f FILE] DIR...\n" |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 402 | "\n" |
| 403 | "\t-d, --dirname=DIR: fs-config directory\n" |
| 404 | "\t-f, --file=FILE: Canned configuration file\n" |
| 405 | "\t-h, --help: Print this help\n" |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 406 | "\t-n, --nodes=FILE: Dev nodes description file\n" |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 407 | "\n" |
| 408 | "Dev nodes description:\n" |
Keir Fraser | b2016ec | 2024-05-13 10:07:26 +0000 | [diff] [blame] | 409 | "\t[dir|nod] [perms] [uid] [gid] [c|b] [major] [minor]\n" |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 410 | "\tExample:\n" |
| 411 | "\t\t# My device nodes\n" |
| 412 | "\t\tdir dev 0755 0 0\n" |
Keir Fraser | b2016ec | 2024-05-13 10:07:26 +0000 | [diff] [blame] | 413 | "\t\tnod dev/null 0600 0 0 c 1 3\n" |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 414 | ); |
| 415 | } |
Doug Zongker | f2c3a83 | 2012-05-03 15:55:56 -0700 | [diff] [blame] | 416 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 417 | int main(int argc, char *argv[]) |
| 418 | { |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 419 | int opt, unused; |
| 420 | |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 421 | 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] | 422 | switch (opt) { |
| 423 | case 'd': |
| 424 | target_out_path = argv[optind - 1]; |
| 425 | break; |
| 426 | case 'f': |
| 427 | read_canned_config(argv[optind - 1]); |
| 428 | break; |
| 429 | case 'h': |
| 430 | usage(); |
| 431 | return 0; |
Vincent Donnefort | 99ab521 | 2023-01-04 10:33:18 +0000 | [diff] [blame] | 432 | case 'n': |
| 433 | append_devnodes_desc(argv[optind - 1]); |
| 434 | break; |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 435 | default: |
| 436 | usage(); |
Elliott Hughes | ad1a0a9 | 2023-01-12 04:26:52 +0000 | [diff] [blame] | 437 | errx(1, "Unknown option %s", argv[optind - 1]); |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 438 | } |
Mateus Azis | 023f67b | 2022-07-12 15:38:47 -0700 | [diff] [blame] | 439 | } |
| 440 | |
Vincent Donnefort | 7f01774 | 2022-12-20 11:22:26 +0000 | [diff] [blame] | 441 | int num_dirs = argc - optind; |
| 442 | argv += optind; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 443 | |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 444 | while (num_dirs-- > 0){ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 445 | char *x = strchr(*argv, '='); |
Elliott Hughes | 132b1ec | 2024-10-29 22:12:01 +0000 | [diff] [blame] | 446 | if (x != nullptr) { |
| 447 | *x++ = '\0'; |
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 | archive(*argv, x ?: ""); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 450 | |
| 451 | argv++; |
| 452 | } |
| 453 | |
| 454 | _eject_trailer(); |
| 455 | |
| 456 | return 0; |
| 457 | } |