Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 17 | #include <fcntl.h> |
Anatol Pomazau | 354350e | 2012-02-03 18:20:07 -0800 | [diff] [blame] | 18 | #include <libgen.h> |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 19 | #include <stdio.h> |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 20 | #include <unistd.h> |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 21 | |
| 22 | #if defined(__linux__) |
| 23 | #include <linux/fs.h> |
| 24 | #elif defined(__APPLE__) && defined(__MACH__) |
| 25 | #include <sys/disk.h> |
| 26 | #endif |
| 27 | |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 28 | #ifdef ANDROID |
| 29 | #include <private/android_filesystem_config.h> |
Mohamad Ayyash | aa8b355 | 2016-04-07 22:15:57 -0700 | [diff] [blame] | 30 | #include <private/canned_fs_config.h> |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 31 | #endif |
| 32 | |
Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 33 | #ifndef _WIN32 |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 34 | #include <selinux/selinux.h> |
| 35 | #include <selinux/label.h> |
Jeff Vander Stoep | c28ca56 | 2015-05-13 14:29:15 -0700 | [diff] [blame] | 36 | #if !defined(HOST) |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 37 | #include <selinux/android.h> |
Jeff Vander Stoep | c28ca56 | 2015-05-13 14:29:15 -0700 | [diff] [blame] | 38 | #endif |
Colin Cross | 9652986 | 2013-01-23 15:38:57 -0800 | [diff] [blame] | 39 | #else |
| 40 | struct selabel_handle; |
| 41 | #endif |
| 42 | |
Tao Bao | 018ef1b | 2016-10-05 12:44:18 -0700 | [diff] [blame] | 43 | #include "ext4_utils/ext4_utils.h" |
| 44 | #include "ext4_utils/make_ext4fs.h" |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 46 | #ifndef _WIN32 /* O_BINARY is windows-specific flag */ |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 47 | #define O_BINARY 0 |
| 48 | #endif |
| 49 | |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 50 | extern struct fs_info info; |
| 51 | |
| 52 | |
| 53 | static void usage(char *path) |
| 54 | { |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 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"); |
Connor O'Brien | 6a6c37f | 2017-01-20 11:50:32 -0800 | [diff] [blame] | 57 | fprintf(stderr, " [ -e <flash erase block size> ] [ -o <flash logical block size> ]\n"); |
Jeff Sharkey | 7538cc9 | 2015-04-06 22:29:04 -0700 | [diff] [blame] | 58 | fprintf(stderr, " [ -L <label> ] [ -f ] [ -a <android mountpoint> ] [ -u ]\n"); |
Doug Zongker | aad1acc | 2014-06-16 09:07:44 -0700 | [diff] [blame] | 59 | fprintf(stderr, " [ -S file_contexts ] [ -C fs_config ] [ -T timestamp ]\n"); |
Mohamad Ayyash | 18785a8 | 2016-02-19 21:16:34 +0000 | [diff] [blame] | 60 | fprintf(stderr, " [ -z | -s ] [ -w ] [ -c ] [ -J ] [ -v ] [ -B <block_list_file> ]\n"); |
Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 61 | fprintf(stderr, " [ -d <base_alloc_file_in> ] [ -D <base_alloc_file_out> ]\n"); |
Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 62 | fprintf(stderr, " <filename> [[<directory>] <target_out_directory>]\n"); |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | int main(int argc, char **argv) |
| 66 | { |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 67 | int opt; |
| 68 | const char *filename = NULL; |
| 69 | const char *directory = NULL; |
Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 70 | const char *target_out_directory = NULL; |
Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 71 | char *mountpoint = NULL; |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 72 | fs_config_func_t fs_config_func = NULL; |
Doug Zongker | aad1acc | 2014-06-16 09:07:44 -0700 | [diff] [blame] | 73 | const char *fs_config_file = NULL; |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 74 | int gzip = 0; |
| 75 | int sparse = 0; |
| 76 | int crc = 0; |
| 77 | int wipe = 0; |
Jeff Sharkey | 7538cc9 | 2015-04-06 22:29:04 -0700 | [diff] [blame] | 78 | int real_uuid = 0; |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 79 | int fd; |
| 80 | int exitcode; |
William Roberts | 2057370 | 2013-01-17 13:24:27 -0800 | [diff] [blame] | 81 | int verbose = 0; |
Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 82 | time_t fixed_time = -1; |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 83 | struct selabel_handle *sehnd = NULL; |
Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 84 | FILE* block_list_file = NULL; |
Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 85 | FILE* base_alloc_file_in = NULL; |
| 86 | FILE* base_alloc_file_out = NULL; |
Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 87 | #ifndef _WIN32 |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 88 | struct selinux_opt seopts[] = { { SELABEL_OPT_PATH, "" } }; |
Stephen Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 89 | #endif |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 90 | |
Connor O'Brien | 6a6c37f | 2017-01-20 11:50:32 -0800 | [diff] [blame] | 91 | while ((opt = getopt(argc, argv, "l:j:b:g:i:I:e:o:L:a:S:T:C:B:d:D:fwzJsctvu")) != -1) { |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 92 | 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'Brien | 6a6c37f | 2017-01-20 11:50:32 -0800 | [diff] [blame] | 111 | 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 Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 117 | case 'L': |
| 118 | info.label = optarg; |
| 119 | break; |
| 120 | case 'f': |
| 121 | force = 1; |
| 122 | break; |
| 123 | case 'a': |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 124 | #ifdef ANDROID |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 125 | mountpoint = optarg; |
Kenny Root | 68e3dfd | 2012-03-29 14:43:22 -0700 | [diff] [blame] | 126 | #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 Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 131 | break; |
| 132 | case 'w': |
| 133 | wipe = 1; |
| 134 | break; |
Jeff Sharkey | 7538cc9 | 2015-04-06 22:29:04 -0700 | [diff] [blame] | 135 | case 'u': |
| 136 | real_uuid = 1; |
| 137 | break; |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 138 | case 'z': |
| 139 | gzip = 1; |
| 140 | break; |
Colin Cross | e4b5ae8 | 2010-08-03 14:10:07 -0700 | [diff] [blame] | 141 | case 'J': |
| 142 | info.no_journal = 1; |
| 143 | break; |
Colin Cross | 757ace5 | 2010-12-29 13:57:01 -0800 | [diff] [blame] | 144 | case 'c': |
| 145 | crc = 1; |
| 146 | break; |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 147 | case 's': |
| 148 | sparse = 1; |
| 149 | break; |
| 150 | case 't': |
Colin Cross | 56497f2 | 2013-02-04 00:44:55 -0800 | [diff] [blame] | 151 | fprintf(stderr, "Warning: -t (initialize inode tables) is deprecated\n"); |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 152 | break; |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 153 | case 'S': |
Elliott Hughes | 1eb2f54 | 2016-10-05 09:44:48 -0700 | [diff] [blame] | 154 | #ifndef _WIN32 |
Kenny Root | 2e5c523 | 2012-03-30 20:38:32 -0700 | [diff] [blame] | 155 | 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 Smalley | b4eca4b | 2012-01-13 09:00:56 -0500 | [diff] [blame] | 161 | #endif |
Kenny Root | 723f1c7 | 2012-10-10 11:13:13 -0700 | [diff] [blame] | 162 | break; |
William Roberts | 2057370 | 2013-01-17 13:24:27 -0800 | [diff] [blame] | 163 | case 'v': |
| 164 | verbose = 1; |
| 165 | break; |
Doug Zongker | 9526680 | 2013-12-05 15:51:28 -0800 | [diff] [blame] | 166 | case 'T': |
| 167 | fixed_time = strtoll(optarg, NULL, 0); |
| 168 | break; |
Doug Zongker | aad1acc | 2014-06-16 09:07:44 -0700 | [diff] [blame] | 169 | case 'C': |
| 170 | fs_config_file = optarg; |
| 171 | break; |
Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 172 | 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 Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 179 | 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 Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 193 | default: /* '?' */ |
| 194 | usage(argv[0]); |
| 195 | exit(EXIT_FAILURE); |
| 196 | } |
Ken Sumrall | 75249ed | 2010-08-13 16:04:49 -0700 | [diff] [blame] | 197 | } |
| 198 | |
rpcraig | 5da2f36 | 2012-09-21 11:46:20 -0400 | [diff] [blame] | 199 | #if !defined(HOST) |
| 200 | // Use only if -S option not requested |
Colin Cross | a446014 | 2012-12-20 01:00:33 -0800 | [diff] [blame] | 201 | if (!sehnd && mountpoint) { |
rpcraig | 5da2f36 | 2012-09-21 11:46:20 -0400 | [diff] [blame] | 202 | sehnd = selinux_android_file_context_handle(); |
| 203 | |
| 204 | if (!sehnd) { |
| 205 | perror(optarg); |
| 206 | exit(EXIT_FAILURE); |
| 207 | } |
| 208 | } |
| 209 | #endif |
| 210 | |
Doug Zongker | aad1acc | 2014-06-16 09:07:44 -0700 | [diff] [blame] | 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); |
| 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 Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 221 | if (wipe && sparse) { |
| 222 | fprintf(stderr, "Cannot specifiy both wipe and sparse\n"); |
| 223 | usage(argv[0]); |
| 224 | exit(EXIT_FAILURE); |
| 225 | } |
Colin Cross | c247065 | 2011-01-26 16:39:46 -0800 | [diff] [blame] | 226 | |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 227 | if (wipe && gzip) { |
| 228 | fprintf(stderr, "Cannot specifiy both wipe and gzip\n"); |
| 229 | usage(argv[0]); |
| 230 | exit(EXIT_FAILURE); |
| 231 | } |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 232 | |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 233 | if (optind >= argc) { |
| 234 | fprintf(stderr, "Expected filename after options\n"); |
| 235 | usage(argv[0]); |
| 236 | exit(EXIT_FAILURE); |
| 237 | } |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 238 | |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 239 | filename = argv[optind++]; |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 240 | |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 241 | if (optind < argc) |
| 242 | directory = argv[optind++]; |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 243 | |
Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 244 | if (optind < argc) |
| 245 | target_out_directory = argv[optind++]; |
| 246 | |
Anatol Pomazau | 00740cc | 2012-02-06 17:13:51 -0800 | [diff] [blame] | 247 | if (optind < argc) { |
| 248 | fprintf(stderr, "Unexpected argument: %s\n", argv[optind]); |
| 249 | usage(argv[0]); |
| 250 | exit(EXIT_FAILURE); |
| 251 | } |
| 252 | |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 253 | if (strcmp(filename, "-")) { |
| 254 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); |
| 255 | if (fd < 0) { |
Colin Cross | 27f830f | 2012-12-20 12:24:52 -0800 | [diff] [blame] | 256 | perror("open"); |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 257 | return EXIT_FAILURE; |
| 258 | } |
| 259 | } else { |
| 260 | fd = STDOUT_FILENO; |
| 261 | } |
| 262 | |
Thierry Strudel | b89e81d | 2015-07-09 16:31:39 -0700 | [diff] [blame] | 263 | exitcode = make_ext4fs_internal(fd, directory, target_out_directory, mountpoint, fs_config_func, gzip, |
Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 264 | sparse, crc, wipe, real_uuid, sehnd, verbose, fixed_time, |
| 265 | block_list_file, base_alloc_file_in, base_alloc_file_out); |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 266 | close(fd); |
Doug Zongker | bec598e | 2014-08-12 11:35:37 -0700 | [diff] [blame] | 267 | if (block_list_file) |
| 268 | fclose(block_list_file); |
Mohamad Ayyash | 9579198 | 2016-02-20 03:46:00 +0000 | [diff] [blame] | 269 | if (base_alloc_file_out) |
| 270 | fclose(base_alloc_file_out); |
| 271 | if (base_alloc_file_in) |
| 272 | fclose(base_alloc_file_in); |
JP Abgrall | 39ef30b | 2014-02-14 17:31:40 -0800 | [diff] [blame] | 273 | if (exitcode && strcmp(filename, "-")) |
| 274 | unlink(filename); |
Anatol Pomazau | 0349bd9 | 2012-01-11 15:12:27 -0800 | [diff] [blame] | 275 | return exitcode; |
Doug Zongker | 263eefd | 2010-06-29 17:23:14 -0700 | [diff] [blame] | 276 | } |