blob: 0f0603b23af62a53eae62affd402dd28358e9cb6 [file] [log] [blame]
Mark Salyzyn06b91b92015-04-01 14:41:29 -07001/*
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 Salyzyn256d3392017-03-22 08:46:55 -070017#include <ctype.h>
Mark Salyzyn06b91b92015-04-01 14:41:29 -070018#include <stdbool.h>
19#include <stdio.h>
20#include <stdlib.h>
Mark Salyzyn256d3392017-03-22 08:46:55 -070021#include <string.h>
Mark Salyzyn06b91b92015-04-01 14:41:29 -070022#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 Salyzyn5649b312017-03-22 08:46:55 -070033#ifdef ANDROID_FILESYSTEM_CONFIG
34#include ANDROID_FILESYSTEM_CONFIG
35#else
Mark Salyzyn06b91b92015-04-01 14:41:29 -070036#include "android_filesystem_config.h"
Mark Salyzyn5649b312017-03-22 08:46:55 -070037#endif
Mark Salyzyn06b91b92015-04-01 14:41:29 -070038
39#ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS
Mark Salyzyn256d3392017-03-22 08:46:55 -070040static const struct fs_path_config android_device_dirs[] = { };
Mark Salyzyn06b91b92015-04-01 14:41:29 -070041#endif
42
43#ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_FILES
44static const struct fs_path_config android_device_files[] = {
45#ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS
Mark Salyzyn256d3392017-03-22 08:46:55 -070046 {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 Salyzyncf343a62018-11-16 09:44:07 -080050 {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 Salyzyn06b91b92015-04-01 14:41:29 -070052#endif
Mark Salyzyn256d3392017-03-22 08:46:55 -070053 {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 Salyzyncf343a62018-11-16 09:44:07 -080057 {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 Salyzyn06b91b92015-04-01 14:41:29 -070059};
60#endif
61
62static void usage() {
63 fprintf(stderr,
64 "Generate binary content for fs_config_dirs (-D) and fs_config_files (-F)\n"
Mark Salyzyn256d3392017-03-22 08:46:55 -070065 "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 Salyzyn06b91b92015-04-01 14:41:29 -070070}
71
Mark Salyzyn256d3392017-03-22 08:46:55 -070072/* 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 Salyzyn06b91b92015-04-01 14:41:29 -070076
Mark Salyzyn256d3392017-03-22 08:46:55 -070077int 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 Salyzyn06b91b92015-04-01 14:41:29 -070088 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 Salyzyn256d3392017-03-22 08:46:55 -0700104 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 Salyzyn06b91b92015-04-01 14:41:29 -0700128 case 'o':
129 if (fp != stdout) {
130 fprintf(stderr, "Specify only one output file\n");
131 usage();
132 exit(EXIT_FAILURE);
133 }
Mark Salyzyn60240692015-04-16 08:42:19 -0700134 fp = fopen(optarg, "wb");
Mark Salyzyn06b91b92015-04-01 14:41:29 -0700135 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 Salyzyn256d3392017-03-22 08:46:55 -0700149 if (optind < argc) {
150 fprintf(stderr, "Unknown non-argument \"%s\"\n", argv[optind]);
151 usage();
152 exit(EXIT_FAILURE);
153 }
154
Mark Salyzyn06b91b92015-04-01 14:41:29 -0700155 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 Salyzyn256d3392017-03-22 08:46:55 -0700163 end = &android_device_dirs[ARRAY_SIZE(android_device_dirs)];
Mark Salyzyn06b91b92015-04-01 14:41:29 -0700164 } else {
165 pc = android_device_files;
Mark Salyzyn256d3392017-03-22 08:46:55 -0700166 end = &android_device_files[ARRAY_SIZE(android_device_files)];
Mark Salyzyn06b91b92015-04-01 14:41:29 -0700167 }
Mark Salyzyn256d3392017-03-22 08:46:55 -0700168 for (; (pc < end) && pc->prefix; pc++) {
169 bool submit;
Mark Salyzyn06b91b92015-04-01 14:41:29 -0700170 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 Salyzyn256d3392017-03-22 08:46:55 -0700176 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 Salyzyn06b91b92015-04-01 14:41:29 -0700221 fprintf(stderr, "Write failure\n");
222 exit(EXIT_FAILURE);
223 }
224 }
225 fclose(fp);
226
227 return 0;
228}