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 | cf343a6 | 2018-11-16 09:44:07 -0800 | [diff] [blame] | 50 | {0000, AID_ROOT, AID_ROOT, 0, "product/etc/fs_config_dirs"}, |
| 51 | {0000, AID_ROOT, AID_ROOT, 0, "product_services/etc/fs_config_dirs"}, |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 52 | #endif |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 53 | {0000, AID_ROOT, AID_ROOT, 0, "system/etc/fs_config_files"}, |
| 54 | {0000, AID_ROOT, AID_ROOT, 0, "vendor/etc/fs_config_files"}, |
| 55 | {0000, AID_ROOT, AID_ROOT, 0, "oem/etc/fs_config_files"}, |
| 56 | {0000, AID_ROOT, AID_ROOT, 0, "odm/etc/fs_config_files"}, |
Mark Salyzyn | cf343a6 | 2018-11-16 09:44:07 -0800 | [diff] [blame] | 57 | {0000, AID_ROOT, AID_ROOT, 0, "product/etc/fs_config_files"}, |
| 58 | {0000, AID_ROOT, AID_ROOT, 0, "product_services/etc/fs_config_files"}, |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 59 | }; |
| 60 | #endif |
| 61 | |
| 62 | static void usage() { |
| 63 | fprintf(stderr, |
| 64 | "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] | 65 | "from device-specific android_filesystem_config.h override. Filter based\n" |
| 66 | "on a comma separated partition list (-P) whitelist or prefixed by a\n" |
| 67 | "minus blacklist. Partitions are identified as path references to\n" |
| 68 | "<partition>/ or system/<partition>/\n\n" |
| 69 | "Usage: fs_config_generate -D|-F [-P list] [-o output-file]\n"); |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 72 | /* If tool switches to C++, use android-base/macros.h array_size() */ |
| 73 | #ifndef ARRAY_SIZE /* popular macro */ |
| 74 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 75 | #endif |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 76 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 77 | int main(int argc, char** argv) { |
| 78 | const struct fs_path_config* pc; |
| 79 | const struct fs_path_config* end; |
| 80 | bool dir = false, file = false; |
| 81 | const char* partitions = NULL; |
| 82 | FILE* fp = stdout; |
| 83 | int opt; |
| 84 | static const char optstring[] = "DFP:ho:"; |
| 85 | |
| 86 | while ((opt = getopt(argc, argv, optstring)) != -1) { |
| 87 | switch (opt) { |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 88 | case 'D': |
| 89 | if (file) { |
| 90 | fprintf(stderr, "Must specify only -D or -F\n"); |
| 91 | usage(); |
| 92 | exit(EXIT_FAILURE); |
| 93 | } |
| 94 | dir = true; |
| 95 | break; |
| 96 | case 'F': |
| 97 | if (dir) { |
| 98 | fprintf(stderr, "Must specify only -F or -D\n"); |
| 99 | usage(); |
| 100 | exit(EXIT_FAILURE); |
| 101 | } |
| 102 | file = true; |
| 103 | break; |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 104 | case 'P': |
| 105 | if (partitions) { |
| 106 | fprintf(stderr, "Specify only one partition list\n"); |
| 107 | usage(); |
| 108 | exit(EXIT_FAILURE); |
| 109 | } |
| 110 | while (*optarg && isspace(*optarg)) ++optarg; |
| 111 | if (!optarg[0]) { |
| 112 | fprintf(stderr, "Partition list empty\n"); |
| 113 | usage(); |
| 114 | exit(EXIT_FAILURE); |
| 115 | } |
| 116 | if (!optarg[1]) { |
| 117 | fprintf(stderr, "Partition list too short \"%s\"\n", optarg); |
| 118 | usage(); |
| 119 | exit(EXIT_FAILURE); |
| 120 | } |
| 121 | if ((optarg[0] == '-') && strchr(optstring, optarg[1]) && !optarg[2]) { |
| 122 | fprintf(stderr, "Partition list is a flag \"%s\"\n", optarg); |
| 123 | usage(); |
| 124 | exit(EXIT_FAILURE); |
| 125 | } |
| 126 | partitions = optarg; |
| 127 | break; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 128 | case 'o': |
| 129 | if (fp != stdout) { |
| 130 | fprintf(stderr, "Specify only one output file\n"); |
| 131 | usage(); |
| 132 | exit(EXIT_FAILURE); |
| 133 | } |
Mark Salyzyn | 6024069 | 2015-04-16 08:42:19 -0700 | [diff] [blame] | 134 | fp = fopen(optarg, "wb"); |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 135 | if (fp == NULL) { |
| 136 | fprintf(stderr, "Can not open \"%s\"\n", optarg); |
| 137 | exit(EXIT_FAILURE); |
| 138 | } |
| 139 | break; |
| 140 | case 'h': |
| 141 | usage(); |
| 142 | exit(EXIT_SUCCESS); |
| 143 | default: |
| 144 | usage(); |
| 145 | exit(EXIT_FAILURE); |
| 146 | } |
| 147 | } |
| 148 | |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 149 | if (optind < argc) { |
| 150 | fprintf(stderr, "Unknown non-argument \"%s\"\n", argv[optind]); |
| 151 | usage(); |
| 152 | exit(EXIT_FAILURE); |
| 153 | } |
| 154 | |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 155 | if (!file && !dir) { |
| 156 | fprintf(stderr, "Must specify either -F or -D\n"); |
| 157 | usage(); |
| 158 | exit(EXIT_FAILURE); |
| 159 | } |
| 160 | |
| 161 | if (dir) { |
| 162 | pc = android_device_dirs; |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 163 | end = &android_device_dirs[ARRAY_SIZE(android_device_dirs)]; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 164 | } else { |
| 165 | pc = android_device_files; |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 166 | end = &android_device_files[ARRAY_SIZE(android_device_files)]; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 167 | } |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 168 | for (; (pc < end) && pc->prefix; pc++) { |
| 169 | bool submit; |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 170 | char buffer[512]; |
| 171 | ssize_t len = fs_config_generate(buffer, sizeof(buffer), pc); |
| 172 | if (len < 0) { |
| 173 | fprintf(stderr, "Entry too large\n"); |
| 174 | exit(EXIT_FAILURE); |
| 175 | } |
Mark Salyzyn | 256d339 | 2017-03-22 08:46:55 -0700 | [diff] [blame] | 176 | submit = true; |
| 177 | if (partitions) { |
| 178 | char* partitions_copy = strdup(partitions); |
| 179 | char* arg = partitions_copy; |
| 180 | char* sv = NULL; /* Do not leave uninitialized, NULL is known safe. */ |
| 181 | /* Deal with case all iterated partitions are blacklists with no match */ |
| 182 | bool all_blacklist_but_no_match = true; |
| 183 | submit = false; |
| 184 | |
| 185 | if (!partitions_copy) { |
| 186 | fprintf(stderr, "Failed to allocate a copy of %s\n", partitions); |
| 187 | exit(EXIT_FAILURE); |
| 188 | } |
| 189 | /* iterate through (officially) comma separated list of partitions */ |
| 190 | while (!!(arg = strtok_r(arg, ",:; \t\n\r\f", &sv))) { |
| 191 | static const char system[] = "system/"; |
| 192 | size_t plen; |
| 193 | bool blacklist = false; |
| 194 | if (*arg == '-') { |
| 195 | blacklist = true; |
| 196 | ++arg; |
| 197 | } else { |
| 198 | all_blacklist_but_no_match = false; |
| 199 | } |
| 200 | plen = strlen(arg); |
| 201 | /* deal with evil callers */ |
| 202 | while (arg[plen - 1] == '/') { |
| 203 | --plen; |
| 204 | } |
| 205 | /* check if we have <partition>/ or /system/<partition>/ */ |
| 206 | if ((!strncmp(pc->prefix, arg, plen) && (pc->prefix[plen] == '/')) || |
| 207 | (!strncmp(pc->prefix, system, strlen(system)) && |
| 208 | !strncmp(pc->prefix + strlen(system), arg, plen) && |
| 209 | (pc->prefix[strlen(system) + plen] == '/'))) { |
| 210 | all_blacklist_but_no_match = false; |
| 211 | /* we have a match !!! */ |
| 212 | if (!blacklist) submit = true; |
| 213 | break; |
| 214 | } |
| 215 | arg = NULL; |
| 216 | } |
| 217 | free(partitions_copy); |
| 218 | if (all_blacklist_but_no_match) submit = true; |
| 219 | } |
| 220 | if (submit && (fwrite(buffer, 1, len, fp) != (size_t)len)) { |
Mark Salyzyn | 06b91b9 | 2015-04-01 14:41:29 -0700 | [diff] [blame] | 221 | fprintf(stderr, "Write failure\n"); |
| 222 | exit(EXIT_FAILURE); |
| 223 | } |
| 224 | } |
| 225 | fclose(fp); |
| 226 | |
| 227 | return 0; |
| 228 | } |