2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #if defined(__linux__)
24 #elif defined(__APPLE__) && defined(__MACH__)
29 #include <private/android_filesystem_config.h>
30 #include <private/canned_fs_config.h>
34 #include <selinux/selinux.h>
35 #include <selinux/label.h>
37 #include <selinux/android.h>
40 struct selabel_handle;
43 #include "ext4_utils/ext4_utils.h"
44 #include "ext4_utils/make_ext4fs.h"
46 #ifndef _WIN32 /* O_BINARY is windows-specific flag */
50 extern struct fs_info info;
53 static void usage(char *path)
55 fprintf(stderr, "%s [ -l <len> ] [ -j <journal size> ] [ -b <block_size> ]\n", basename(path));
56 fprintf(stderr, " [ -g <blocks per group> ] [ -i <inodes> ] [ -I <inode size> ]\n");
57 fprintf(stderr, " [ -e <flash erase block size> ] [ -o <flash logical block size> ]\n");
58 fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ] [ -u ]\n");
59 fprintf(stderr, " [ -S file_contexts ] [ -C fs_config ] [ -T timestamp ]\n");
60 fprintf(stderr, " [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] [ -B <block_list_file> ]\n");
61 fprintf(stderr, " [ -d <base_alloc_file_in> ] [ -D <base_alloc_file_out> ]\n");
62 fprintf(stderr, " <filename> [[<directory>] <target_out_directory>]\n");
65 int main(int argc, char **argv)
68 const char *filename = NULL;
69 const char *directory = NULL;
70 const char *target_out_directory = NULL;
71 char *mountpoint = NULL;
72 fs_config_func_t fs_config_func = NULL;
73 const char *fs_config_file = NULL;
82 time_t fixed_time = -1;
83 struct selabel_handle *sehnd = NULL;
84 FILE* block_list_file = NULL;
85 FILE* base_alloc_file_in = NULL;
86 FILE* base_alloc_file_out = NULL;
88 struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
91 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:e:o:L:a:S:T:C:B:d:D:fwzJsctvu")) != -1) {
94 info.len = parse_num(optarg);
97 info.journal_blocks = parse_num(optarg);
100 info.block_size = parse_num(optarg);
103 info.blocks_per_group = parse_num(optarg);
106 info.inodes = parse_num(optarg);
109 info.inode_size = parse_num(optarg);
112 info.flash_erase_block_size = parse_num(optarg);
115 info.flash_logical_block_size = parse_num(optarg);
127 fprintf(stderr, "can't set android permissions - built without android support\n");
151 fprintf(stderr, "Warning: -t (initialize inode tables) is deprecated\n");
155 seopts[0].value = optarg;
156 sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
167 fixed_time = strtoll(optarg, NULL, 0);
170 fs_config_file = optarg;
173 block_list_file = fopen(optarg, "w");
174 if (block_list_file == NULL) {
175 fprintf(stderr, "failed to open block_list_file: %s\n", strerror(errno));
180 base_alloc_file_in = fopen(optarg, "r");
181 if (base_alloc_file_in == NULL) {
182 fprintf(stderr, "failed to open base_alloc_file_in: %s\n", strerror(errno));
187 base_alloc_file_out = fopen(optarg, "w");
188 if (base_alloc_file_out == NULL) {
189 fprintf(stderr, "failed to open base_alloc_file_out: %s\n", strerror(errno));
200 // Use only if -S option not requested
201 if (!sehnd && mountpoint) {
202 sehnd = selinux_android_file_context_handle();
211 if (fs_config_file) {
212 if (load_canned_fs_config(fs_config_file) < 0) {
213 fprintf(stderr, "failed to load %s\n", fs_config_file);
216 fs_config_func = canned_fs_config;
217 } else if (mountpoint) {
218 fs_config_func = fs_config;
221 if (wipe && sparse) {
222 fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
228 fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
233 if (optind >= argc) {
234 fprintf(stderr, "Expected filename after options\n");
239 filename = argv[optind++];
242 directory = argv[optind++];
245 target_out_directory = argv[optind++];
248 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
253 if (strcmp(filename, "-")) {
254 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
263 exitcode = make_ext4fs_internal(fd, directory, target_out_directory, mountpoint, fs_config_func, gzip,
264 sparse, crc, wipe, real_uuid, sehnd, verbose, fixed_time,
265 block_list_file, base_alloc_file_in, base_alloc_file_out);
268 fclose(block_list_file);
269 if (base_alloc_file_out)
270 fclose(base_alloc_file_out);
271 if (base_alloc_file_in)
272 fclose(base_alloc_file_in);
273 if (exitcode && strcmp(filename, "-"))