blob: 77930cd417ad662d582add66410cb0f6fa0f5c93 [file] [log] [blame]
Doug Zongker263eefd2010-06-29 17:23:14 -07001/*
2 * Copyright (C) 2010 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
Anatol Pomazau0349bd92012-01-11 15:12:27 -080017#include <fcntl.h>
Anatol Pomazau354350e2012-02-03 18:20:07 -080018#include <libgen.h>
Colin Cross96529862013-01-23 15:38:57 -080019#include <stdio.h>
Anatol Pomazau0349bd92012-01-11 15:12:27 -080020#include <unistd.h>
Doug Zongker263eefd2010-06-29 17:23:14 -070021
22#if defined(__linux__)
23#include <linux/fs.h>
24#elif defined(__APPLE__) && defined(__MACH__)
25#include <sys/disk.h>
26#endif
27
Kenny Root68e3dfd2012-03-29 14:43:22 -070028#ifdef ANDROID
29#include <private/android_filesystem_config.h>
Mohamad Ayyashaa8b3552016-04-07 22:15:57 -070030#include <private/canned_fs_config.h>
Kenny Root68e3dfd2012-03-29 14:43:22 -070031#endif
32
Elliott Hughes1eb2f542016-10-05 09:44:48 -070033#ifndef _WIN32
Colin Cross96529862013-01-23 15:38:57 -080034#include <selinux/selinux.h>
35#include <selinux/label.h>
Jeff Vander Stoepc28ca562015-05-13 14:29:15 -070036#if !defined(HOST)
Colin Cross96529862013-01-23 15:38:57 -080037#include <selinux/android.h>
Jeff Vander Stoepc28ca562015-05-13 14:29:15 -070038#endif
Colin Cross96529862013-01-23 15:38:57 -080039#else
40struct selabel_handle;
41#endif
42
Tao Bao018ef1b2016-10-05 12:44:18 -070043#include "ext4_utils/ext4_utils.h"
44#include "ext4_utils/make_ext4fs.h"
Doug Zongker263eefd2010-06-29 17:23:14 -070045
Elliott Hughes1eb2f542016-10-05 09:44:48 -070046#ifndef _WIN32 /* O_BINARY is windows-specific flag */
Anatol Pomazau0349bd92012-01-11 15:12:27 -080047#define O_BINARY 0
48#endif
49
Doug Zongker263eefd2010-06-29 17:23:14 -070050extern struct fs_info info;
51
52
53static void usage(char *path)
54{
Anatol Pomazau00740cc2012-02-06 17:13:51 -080055 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");
Connor O'Brien6a6c37f2017-01-20 11:50:32 -080057 fprintf(stderr, " [ -e <flash erase block size> ] [ -o <flash logical block size> ]\n");
Jeff Sharkey7538cc92015-04-06 22:29:04 -070058 fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ] [ -u ]\n");
Doug Zongkeraad1acc2014-06-16 09:07:44 -070059 fprintf(stderr, " [ -S file_contexts ] [ -C fs_config ] [ -T timestamp ]\n");
Mohamad Ayyash18785a82016-02-19 21:16:34 +000060 fprintf(stderr, " [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] [ -B <block_list_file> ]\n");
Mohamad Ayyash95791982016-02-20 03:46:00 +000061 fprintf(stderr, " [ -d <base_alloc_file_in> ] [ -D <base_alloc_file_out> ]\n");
Thierry Strudelb89e81d2015-07-09 16:31:39 -070062 fprintf(stderr, " <filename> [[<directory>] <target_out_directory>]\n");
Doug Zongker263eefd2010-06-29 17:23:14 -070063}
64
65int main(int argc, char **argv)
66{
Anatol Pomazau00740cc2012-02-06 17:13:51 -080067 int opt;
68 const char *filename = NULL;
69 const char *directory = NULL;
Thierry Strudelb89e81d2015-07-09 16:31:39 -070070 const char *target_out_directory = NULL;
Colin Crossa4460142012-12-20 01:00:33 -080071 char *mountpoint = NULL;
Kenny Root68e3dfd2012-03-29 14:43:22 -070072 fs_config_func_t fs_config_func = NULL;
Doug Zongkeraad1acc2014-06-16 09:07:44 -070073 const char *fs_config_file = NULL;
Anatol Pomazau00740cc2012-02-06 17:13:51 -080074 int gzip = 0;
75 int sparse = 0;
76 int crc = 0;
77 int wipe = 0;
Jeff Sharkey7538cc92015-04-06 22:29:04 -070078 int real_uuid = 0;
Anatol Pomazau0349bd92012-01-11 15:12:27 -080079 int fd;
80 int exitcode;
William Roberts20573702013-01-17 13:24:27 -080081 int verbose = 0;
Doug Zongker95266802013-12-05 15:51:28 -080082 time_t fixed_time = -1;
Kenny Root2e5c5232012-03-30 20:38:32 -070083 struct selabel_handle *sehnd = NULL;
Doug Zongkerbec598e2014-08-12 11:35:37 -070084 FILE* block_list_file = NULL;
Mohamad Ayyash95791982016-02-20 03:46:00 +000085 FILE* base_alloc_file_in = NULL;
86 FILE* base_alloc_file_out = NULL;
Elliott Hughes1eb2f542016-10-05 09:44:48 -070087#ifndef _WIN32
Kenny Root2e5c5232012-03-30 20:38:32 -070088 struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } };
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -050089#endif
Doug Zongker263eefd2010-06-29 17:23:14 -070090
Connor O'Brien6a6c37f2017-01-20 11:50:32 -080091 while ((opt = getopt(argc, argv, "l:j:b:g:i:I:e:o:L:a:S:T:C:B:d:D:fwzJsctvu")) != -1) {
Anatol Pomazau00740cc2012-02-06 17:13:51 -080092 switch (opt) {
93 case 'l':
94 info.len = parse_num(optarg);
95 break;
96 case 'j':
97 info.journal_blocks = parse_num(optarg);
98 break;
99 case 'b':
100 info.block_size = parse_num(optarg);
101 break;
102 case 'g':
103 info.blocks_per_group = parse_num(optarg);
104 break;
105 case 'i':
106 info.inodes = parse_num(optarg);
107 break;
108 case 'I':
109 info.inode_size = parse_num(optarg);
110 break;
Connor O'Brien6a6c37f2017-01-20 11:50:32 -0800111 case 'e':
112 info.flash_erase_block_size = parse_num(optarg);
113 break;
114 case 'o':
115 info.flash_logical_block_size = parse_num(optarg);
116 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800117 case 'L':
118 info.label = optarg;
119 break;
120 case 'f':
121 force = 1;
122 break;
123 case 'a':
Kenny Root68e3dfd2012-03-29 14:43:22 -0700124#ifdef ANDROID
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800125 mountpoint = optarg;
Kenny Root68e3dfd2012-03-29 14:43:22 -0700126#else
127 fprintf(stderr, "can't set android permissions - built without android support\n");
128 usage(argv[0]);
129 exit(EXIT_FAILURE);
130#endif
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800131 break;
132 case 'w':
133 wipe = 1;
134 break;
Jeff Sharkey7538cc92015-04-06 22:29:04 -0700135 case 'u':
136 real_uuid = 1;
137 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800138 case 'z':
139 gzip = 1;
140 break;
Colin Crosse4b5ae82010-08-03 14:10:07 -0700141 case 'J':
142 info.no_journal = 1;
143 break;
Colin Cross757ace52010-12-29 13:57:01 -0800144 case 'c':
145 crc = 1;
146 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800147 case 's':
148 sparse = 1;
149 break;
150 case 't':
Colin Cross56497f22013-02-04 00:44:55 -0800151 fprintf(stderr, "Warning: -t (initialize inode tables) is deprecated\n");
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800152 break;
Kenny Root2e5c5232012-03-30 20:38:32 -0700153 case 'S':
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700154#ifndef _WIN32
Kenny Root2e5c5232012-03-30 20:38:32 -0700155 seopts[0].value = optarg;
156 sehnd = selabel_open(SELABEL_CTX_FILE, seopts, 1);
157 if (!sehnd) {
158 perror(optarg);
159 exit(EXIT_FAILURE);
160 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500161#endif
Kenny Root723f1c72012-10-10 11:13:13 -0700162 break;
William Roberts20573702013-01-17 13:24:27 -0800163 case 'v':
164 verbose = 1;
165 break;
Doug Zongker95266802013-12-05 15:51:28 -0800166 case 'T':
167 fixed_time = strtoll(optarg, NULL, 0);
168 break;
Doug Zongkeraad1acc2014-06-16 09:07:44 -0700169 case 'C':
170 fs_config_file = optarg;
171 break;
Doug Zongkerbec598e2014-08-12 11:35:37 -0700172 case 'B':
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));
176 exit(EXIT_FAILURE);
177 }
178 break;
Mohamad Ayyash95791982016-02-20 03:46:00 +0000179 case 'd':
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));
183 exit(EXIT_FAILURE);
184 }
185 break;
186 case 'D':
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));
190 exit(EXIT_FAILURE);
191 }
192 break;
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800193 default: /* '?' */
194 usage(argv[0]);
195 exit(EXIT_FAILURE);
196 }
Ken Sumrall75249ed2010-08-13 16:04:49 -0700197 }
198
rpcraig5da2f362012-09-21 11:46:20 -0400199#if !defined(HOST)
200 // Use only if -S option not requested
Colin Crossa4460142012-12-20 01:00:33 -0800201 if (!sehnd && mountpoint) {
rpcraig5da2f362012-09-21 11:46:20 -0400202 sehnd = selinux_android_file_context_handle();
203
204 if (!sehnd) {
205 perror(optarg);
206 exit(EXIT_FAILURE);
207 }
208 }
209#endif
210
Doug Zongkeraad1acc2014-06-16 09:07:44 -0700211 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);
214 exit(EXIT_FAILURE);
215 }
216 fs_config_func = canned_fs_config;
217 } else if (mountpoint) {
218 fs_config_func = fs_config;
219 }
220
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800221 if (wipe && sparse) {
222 fprintf(stderr, "Cannot specifiy both wipe and sparse\n");
223 usage(argv[0]);
224 exit(EXIT_FAILURE);
225 }
Colin Crossc2470652011-01-26 16:39:46 -0800226
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800227 if (wipe && gzip) {
228 fprintf(stderr, "Cannot specifiy both wipe and gzip\n");
229 usage(argv[0]);
230 exit(EXIT_FAILURE);
231 }
Doug Zongker263eefd2010-06-29 17:23:14 -0700232
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800233 if (optind >= argc) {
234 fprintf(stderr, "Expected filename after options\n");
235 usage(argv[0]);
236 exit(EXIT_FAILURE);
237 }
Doug Zongker263eefd2010-06-29 17:23:14 -0700238
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800239 filename = argv[optind++];
Doug Zongker263eefd2010-06-29 17:23:14 -0700240
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800241 if (optind < argc)
242 directory = argv[optind++];
Doug Zongker263eefd2010-06-29 17:23:14 -0700243
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700244 if (optind < argc)
245 target_out_directory = argv[optind++];
246
Anatol Pomazau00740cc2012-02-06 17:13:51 -0800247 if (optind < argc) {
248 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
249 usage(argv[0]);
250 exit(EXIT_FAILURE);
251 }
252
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800253 if (strcmp(filename, "-")) {
254 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
255 if (fd < 0) {
Colin Cross27f830f2012-12-20 12:24:52 -0800256 perror("open");
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800257 return EXIT_FAILURE;
258 }
259 } else {
260 fd = STDOUT_FILENO;
261 }
262
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700263 exitcode = make_ext4fs_internal(fd, directory, target_out_directory, mountpoint, fs_config_func, gzip,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000264 sparse, crc, wipe, real_uuid, sehnd, verbose, fixed_time,
265 block_list_file, base_alloc_file_in, base_alloc_file_out);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800266 close(fd);
Doug Zongkerbec598e2014-08-12 11:35:37 -0700267 if (block_list_file)
268 fclose(block_list_file);
Mohamad Ayyash95791982016-02-20 03:46:00 +0000269 if (base_alloc_file_out)
270 fclose(base_alloc_file_out);
271 if (base_alloc_file_in)
272 fclose(base_alloc_file_in);
JP Abgrall39ef30b2014-02-14 17:31:40 -0800273 if (exitcode && strcmp(filename, "-"))
274 unlink(filename);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800275 return exitcode;
Doug Zongker263eefd2010-06-29 17:23:14 -0700276}