blob: e08642fa4557a2b4b21efe39c4685eb28fe4432d [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
17#include <stdbool.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <unistd.h>
21
22#include <private/android_filesystem_config.h>
23
24/*
25 * This program expects android_device_dirs and android_device_files
26 * to be defined in the supplied android_filesystem_config.h file in
27 * the device/<vendor>/<product> $(TARGET_DEVICE_DIR). Then generates
28 * the binary format used in the /system/etc/fs_config_dirs and
29 * the /system/etc/fs_config_files to be used by the runtimes.
30 */
Mark Salyzyn5649b312017-03-22 08:46:55 -070031#ifdef ANDROID_FILESYSTEM_CONFIG
32#include ANDROID_FILESYSTEM_CONFIG
33#else
Mark Salyzyn06b91b92015-04-01 14:41:29 -070034#include "android_filesystem_config.h"
Mark Salyzyn5649b312017-03-22 08:46:55 -070035#endif
Mark Salyzyn06b91b92015-04-01 14:41:29 -070036
37#ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS
38 static const struct fs_path_config android_device_dirs[] = {
39};
40#endif
41
42#ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_FILES
43static const struct fs_path_config android_device_files[] = {
44#ifdef NO_ANDROID_FILESYSTEM_CONFIG_DEVICE_DIRS
45 { 0, AID_ROOT, AID_ROOT, 0, "system/etc/fs_config_dirs" },
46#endif
47 { 0, AID_ROOT, AID_ROOT, 0, "system/etc/fs_config_files" },
48};
49#endif
50
51static void usage() {
52 fprintf(stderr,
53 "Generate binary content for fs_config_dirs (-D) and fs_config_files (-F)\n"
54 "from device-specific android_filesystem_config.h override\n\n"
55 "Usage: fs_config_generate -D|-F [-o output-file]\n");
56}
57
58int main(int argc, char** argv) {
59 const struct fs_path_config *pc;
60 const struct fs_path_config *end;
61 bool dir = false, file = false;
62 FILE *fp = stdout;
63 int opt;
64
65 while((opt = getopt(argc, argv, "DFho:")) != -1) {
66 switch(opt) {
67 case 'D':
68 if (file) {
69 fprintf(stderr, "Must specify only -D or -F\n");
70 usage();
71 exit(EXIT_FAILURE);
72 }
73 dir = true;
74 break;
75 case 'F':
76 if (dir) {
77 fprintf(stderr, "Must specify only -F or -D\n");
78 usage();
79 exit(EXIT_FAILURE);
80 }
81 file = true;
82 break;
83 case 'o':
84 if (fp != stdout) {
85 fprintf(stderr, "Specify only one output file\n");
86 usage();
87 exit(EXIT_FAILURE);
88 }
Mark Salyzyn60240692015-04-16 08:42:19 -070089 fp = fopen(optarg, "wb");
Mark Salyzyn06b91b92015-04-01 14:41:29 -070090 if (fp == NULL) {
91 fprintf(stderr, "Can not open \"%s\"\n", optarg);
92 exit(EXIT_FAILURE);
93 }
94 break;
95 case 'h':
96 usage();
97 exit(EXIT_SUCCESS);
98 default:
99 usage();
100 exit(EXIT_FAILURE);
101 }
102 }
103
104 if (!file && !dir) {
105 fprintf(stderr, "Must specify either -F or -D\n");
106 usage();
107 exit(EXIT_FAILURE);
108 }
109
110 if (dir) {
111 pc = android_device_dirs;
112 end = &android_device_dirs[sizeof(android_device_dirs) / sizeof(android_device_dirs[0])];
113 } else {
114 pc = android_device_files;
115 end = &android_device_files[sizeof(android_device_files) / sizeof(android_device_files[0])];
116 }
117 for(; (pc < end) && pc->prefix; pc++) {
118 char buffer[512];
119 ssize_t len = fs_config_generate(buffer, sizeof(buffer), pc);
120 if (len < 0) {
121 fprintf(stderr, "Entry too large\n");
122 exit(EXIT_FAILURE);
123 }
124 if (fwrite(buffer, 1, len, fp) != (size_t)len) {
125 fprintf(stderr, "Write failure\n");
126 exit(EXIT_FAILURE);
127 }
128 }
129 fclose(fp);
130
131 return 0;
132}