Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 17 | #include <ctype.h> |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 18 | #include <stdbool.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 21 | #include <string.h> |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
| 24 | #include <private/android_filesystem_config.h> |
| 25 | |
| 26 | /* |
| 27 | * This program expects android_device_dirs and android_device_files |
| 28 | * to be defined in the supplied android_filesystem_config.h file in |
| 29 | * the device/<vendor>/<product> $(TARGET_DEVICE_DIR). Then generates |
| 30 | * the binary format used in the /system/etc/fs_config_dirs and |
| 31 | * the /system/etc/fs_config_files to be used by the runtimes. |
| 32 | */ |
Mark Salyzyn | 5649b31 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 33 | #ifdef ANDROID_FILESYSTEM_CONFIG |
| 34 | #include ANDROID_FILESYSTEM_CONFIG |
| 35 | #else |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 36 | #include "android_filesystem_config.h" |
Mark Salyzyn | 5649b31 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 37 | #endif |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 38 | |
| 39 | #ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 40 | static const struct fs_path_config android_device_dirs[] = { }; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 41 | #endif |
| 42 | |
| 43 | #ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_FILES |
| 44 | static const struct fs_path_config android_device_files[] = { |
| 45 | #ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 46 | {0000, AID_ROOT, AID_ROOT, 0, "system/etc/fs_config_dirs"}, |
| 47 | {0000, AID_ROOT, AID_ROOT, 0, "vendor/etc/fs_config_dirs"}, |
| 48 | {0000, AID_ROOT, AID_ROOT, 0, "oem/etc/fs_config_dirs"}, |
| 49 | {0000, AID_ROOT, AID_ROOT, 0, "odm/etc/fs_config_dirs"}, |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 50 | #endif |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 51 | {0000, AID_ROOT, AID_ROOT, 0, "system/etc/fs_config_files"}, |
| 52 | {0000, AID_ROOT, AID_ROOT, 0, "vendor/etc/fs_config_files"}, |
| 53 | {0000, AID_ROOT, AID_ROOT, 0, "oem/etc/fs_config_files"}, |
| 54 | {0000, AID_ROOT, AID_ROOT, 0, "odm/etc/fs_config_files"}, |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 55 | }; |
| 56 | #endif |
| 57 | |
| 58 | static void usage() { |
| 59 | fprintf(stderr, |
| 60 | "Generate binary content for fs_config_dirs (-D) and fs_config_files (-F)\n" |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 61 | "from device-specific android_filesystem_config.h override. Filter based\n" |
| 62 | "on a comma separated partition list (-P) whitelist or prefixed by a\n" |
| 63 | "minus blacklist. Partitions are identified as path references to\n" |
| 64 | "<partition>/ or system/<partition>/\n\n" |
| 65 | "Usage: fs_config_generate -D|-F [-P list] [-o output-file]\n"); |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 68 | /* If tool switches to C++, use android-base/macros.h array_size() */ |
| 69 | #ifndef ARRAY_SIZE /* popular macro */ |
| 70 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 71 | #endif |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 72 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 73 | int main(int argc, char** argv) { |
| 74 | const struct fs_path_config* pc; |
| 75 | const struct fs_path_config* end; |
| 76 | bool dir = false, file = false; |
| 77 | const char* partitions = NULL; |
| 78 | FILE* fp = stdout; |
| 79 | int opt; |
| 80 | static const char optstring[] = "DFP:ho:"; |
| 81 | |
| 82 | while ((opt = getopt(argc, argv, optstring)) != -1) { |
| 83 | switch (opt) { |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 84 | case 'D': |
| 85 | if (file) { |
| 86 | fprintf(stderr, "Must specify only -D or -F\n"); |
| 87 | usage(); |
| 88 | exit(EXIT_FAILURE); |
| 89 | } |
| 90 | dir = true; |
| 91 | break; |
| 92 | case 'F': |
| 93 | if (dir) { |
| 94 | fprintf(stderr, "Must specify only -F or -D\n"); |
| 95 | usage(); |
| 96 | exit(EXIT_FAILURE); |
| 97 | } |
| 98 | file = true; |
| 99 | break; |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 100 | case 'P': |
| 101 | if (partitions) { |
| 102 | fprintf(stderr, "Specify only one partition list\n"); |
| 103 | usage(); |
| 104 | exit(EXIT_FAILURE); |
| 105 | } |
| 106 | while (*optarg && isspace(*optarg)) ++optarg; |
| 107 | if (!optarg[0]) { |
| 108 | fprintf(stderr, "Partition list empty\n"); |
| 109 | usage(); |
| 110 | exit(EXIT_FAILURE); |
| 111 | } |
| 112 | if (!optarg[1]) { |
| 113 | fprintf(stderr, "Partition list too short \"%s\"\n", optarg); |
| 114 | usage(); |
| 115 | exit(EXIT_FAILURE); |
| 116 | } |
| 117 | if ((optarg[0] == '-') && strchr(optstring, optarg[1]) && !optarg[2]) { |
| 118 | fprintf(stderr, "Partition list is a flag \"%s\"\n", optarg); |
| 119 | usage(); |
| 120 | exit(EXIT_FAILURE); |
| 121 | } |
| 122 | partitions = optarg; |
| 123 | break; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 124 | case 'o': |
| 125 | if (fp != stdout) { |
| 126 | fprintf(stderr, "Specify only one output file\n"); |
| 127 | usage(); |
| 128 | exit(EXIT_FAILURE); |
| 129 | } |
Mark Salyzyn | 6024069 | 2015-04-16 08:42:19 -0700 | [diff] [blame] | 130 | fp = fopen(optarg, "wb"); |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 131 | if (fp == NULL) { |
| 132 | fprintf(stderr, "Can not open \"%s\"\n", optarg); |
| 133 | exit(EXIT_FAILURE); |
| 134 | } |
| 135 | break; |
| 136 | case 'h': |
| 137 | usage(); |
| 138 | exit(EXIT_SUCCESS); |
| 139 | default: |
| 140 | usage(); |
| 141 | exit(EXIT_FAILURE); |
| 142 | } |
| 143 | } |
| 144 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 145 | if (optind < argc) { |
| 146 | fprintf(stderr, "Unknown non-argument \"%s\"\n", argv[optind]); |
| 147 | usage(); |
| 148 | exit(EXIT_FAILURE); |
| 149 | } |
| 150 | |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 151 | if (!file && !dir) { |
| 152 | fprintf(stderr, "Must specify either -F or -D\n"); |
| 153 | usage(); |
| 154 | exit(EXIT_FAILURE); |
| 155 | } |
| 156 | |
| 157 | if (dir) { |
| 158 | pc = android_device_dirs; |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 159 | end = &android_device_dirs[ARRAY_SIZE(android_device_dirs)]; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 160 | } else { |
| 161 | pc = android_device_files; |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 162 | end = &android_device_files[ARRAY_SIZE(android_device_files)]; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 163 | } |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 164 | for (; (pc < end) && pc->prefix; pc++) { |
| 165 | bool submit; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 166 | char buffer[512]; |
| 167 | ssize_t len = fs_config_generate(buffer, sizeof(buffer), pc); |
| 168 | if (len < 0) { |
| 169 | fprintf(stderr, "Entry too large\n"); |
| 170 | exit(EXIT_FAILURE); |
| 171 | } |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame^] | 172 | submit = true; |
| 173 | if (partitions) { |
| 174 | char* partitions_copy = strdup(partitions); |
| 175 | char* arg = partitions_copy; |
| 176 | char* sv = NULL; /* Do not leave uninitialized, NULL is known safe. */ |
| 177 | /* Deal with case all iterated partitions are blacklists with no match */ |
| 178 | bool all_blacklist_but_no_match = true; |
| 179 | submit = false; |
| 180 | |
| 181 | if (!partitions_copy) { |
| 182 | fprintf(stderr, "Failed to allocate a copy of %s\n", partitions); |
| 183 | exit(EXIT_FAILURE); |
| 184 | } |
| 185 | /* iterate through (officially) comma separated list of partitions */ |
| 186 | while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) { |
| 187 | static const char system[] = "system/"; |
| 188 | size_t plen; |
| 189 | bool blacklist = false; |
| 190 | if (*arg == '-') { |
| 191 | blacklist = true; |
| 192 | ++arg; |
| 193 | } else { |
| 194 | all_blacklist_but_no_match = false; |
| 195 | } |
| 196 | plen = strlen(arg); |
| 197 | /* deal with evil callers */ |
| 198 | while (arg[plen - 1] == '/') { |
| 199 | --plen; |
| 200 | } |
| 201 | /* check if we have <partition>/ or /system/<partition>/ */ |
| 202 | if ((!strncmp(pc->prefix, arg, plen) && (pc->prefix[plen] == '/')) || |
| 203 | (!strncmp(pc->prefix, system, strlen(system)) && |
| 204 | !strncmp(pc->prefix + strlen(system), arg, plen) && |
| 205 | (pc->prefix[strlen(system) + plen] == '/'))) { |
| 206 | all_blacklist_but_no_match = false; |
| 207 | /* we have a match !!! */ |
| 208 | if (!blacklist) submit = true; |
| 209 | break; |
| 210 | } |
| 211 | arg = NULL; |
| 212 | } |
| 213 | free(partitions_copy); |
| 214 | if (all_blacklist_but_no_match) submit = true; |
| 215 | } |
| 216 | if (submit && (fwrite(buffer, 1, len, fp) != (size_t)len)) { |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 217 | fprintf(stderr, "Write failure\n"); |
| 218 | exit(EXIT_FAILURE); |
| 219 | } |
| 220 | } |
| 221 | fclose(fp); |
| 222 | |
| 223 | return 0; |
| 224 | } |