blob: b59000d10c530deb3f3481cf8f13902a3fccfde3 [file] [log] [blame]
Colin Crossec0a2e82010-06-11 14:21:37 -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 *
Mohamad Ayyash95791982016-02-20 03:46:00 +00008 * http://www.apache.org/licenses/LICENSE-2.0
Colin Crossec0a2e82010-06-11 14:21:37 -07009 *
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
Tao Bao018ef1b2016-10-05 12:44:18 -070017#include "ext4_utils/make_ext4fs.h"
Colin Crossdc5abee2012-04-23 23:20:48 -070018
Tao Bao018ef1b2016-10-05 12:44:18 -070019#ifndef _GNU_SOURCE
20#define _GNU_SOURCE
21#endif
Colin Crossec0a2e82010-06-11 14:21:37 -070022
Raphael Moll4605b3f2012-02-03 23:02:33 -080023#include <assert.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070024#include <dirent.h>
Anatol Pomazau0349bd92012-01-11 15:12:27 -080025#include <fcntl.h>
Colin Crossaf072342014-01-23 13:19:27 -080026#include <inttypes.h>
Colin Crossec0a2e82010-06-11 14:21:37 -070027#include <libgen.h>
Colin Cross881cca22010-06-20 23:57:06 -070028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Colin Cross881cca22010-06-20 23:57:06 -070031#include <sys/stat.h>
32#include <sys/types.h>
Tao Bao018ef1b2016-10-05 12:44:18 -070033#include <unistd.h>
34
35#include <sparse/sparse.h>
36
37#include "allocate.h"
38#include "contents.h"
39#include "ext4_utils/ext4_utils.h"
40#include "ext4_utils/wipe.h"
Colin Crossec0a2e82010-06-11 14:21:37 -070041
Elliott Hughes1eb2f542016-10-05 09:44:48 -070042#ifdef _WIN32
Raphael Moll4605b3f2012-02-03 23:02:33 -080043
44#include <winsock2.h>
45
46/* These match the Linux definitions of these flags.
47 L_xx is defined to avoid conflicting with the win32 versions.
48*/
Dan Albert87978502016-02-02 15:35:33 -080049#undef S_IRWXU
50#undef S_IRGRP
51#undef S_IWGRP
52#undef S_IXGRP
53#undef S_IRWXG
54#undef S_IROTH
55#undef S_IWOTH
56#undef S_IXOTH
57#undef S_IRWXO
58#undef S_ISUID
59#undef S_ISGID
60#undef S_ISVTX
61
Raphael Moll4605b3f2012-02-03 23:02:33 -080062#define L_S_IRUSR 00400
63#define L_S_IWUSR 00200
64#define L_S_IXUSR 00100
65#define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
66#define S_IRGRP 00040
67#define S_IWGRP 00020
68#define S_IXGRP 00010
69#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
70#define S_IROTH 00004
71#define S_IWOTH 00002
72#define S_IXOTH 00001
73#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
74#define S_ISUID 0004000
75#define S_ISGID 0002000
76#define S_ISVTX 0001000
77
Anatol Pomazau0349bd92012-01-11 15:12:27 -080078#else
79
Colin Cross96529862013-01-23 15:38:57 -080080#include <selinux/selinux.h>
81#include <selinux/label.h>
Colin Cross96529862013-01-23 15:38:57 -080082
Anatol Pomazau0349bd92012-01-11 15:12:27 -080083#define O_BINARY 0
84
Raphael Moll4605b3f2012-02-03 23:02:33 -080085#endif
86
Mohamad Ayyash95791982016-02-20 03:46:00 +000087#define MAX_PATH 4096
88#define MAX_BLK_MAPPING_STR 1000
89
90const int blk_file_major_ver = 1;
91const int blk_file_minor_ver = 0;
92const char *blk_file_header_fmt = "Base EXT4 version %d.%d";
93
Colin Crossec0a2e82010-06-11 14:21:37 -070094/* TODO: Not implemented:
95 Allocating blocks in the same block group as the file inode
96 Hash or binary tree directories
Colin Cross7a8bee72010-06-20 14:53:14 -070097 Special files: sockets, devices, fifos
Colin Crossec0a2e82010-06-11 14:21:37 -070098 */
99
Colin Crossec0a2e82010-06-11 14:21:37 -0700100static int filter_dot(const struct dirent *d)
101{
102 return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
103}
104
Stephen Smalley7907ac72014-04-25 14:57:55 -0400105static u32 build_default_directory_structure(const char *dir_path,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000106 struct selabel_handle *sehnd)
Colin Crossec0a2e82010-06-11 14:21:37 -0700107{
108 u32 inode;
109 u32 root_inode;
110 struct dentry dentries = {
111 .filename = "lost+found",
112 .file_type = EXT4_FT_DIR,
113 .mode = S_IRWXU,
114 .uid = 0,
Colin Crossde61f982010-08-04 15:06:09 -0700115 .gid = 0,
116 .mtime = 0,
Colin Crossec0a2e82010-06-11 14:21:37 -0700117 };
118 root_inode = make_directory(0, 1, &dentries, 1);
119 inode = make_directory(root_inode, 0, NULL, 0);
120 *dentries.inode = inode;
Ken Sumrall75249ed2010-08-13 16:04:49 -0700121 inode_set_permissions(inode, dentries.mode,
122 dentries.uid, dentries.gid, dentries.mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -0700123
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700124#ifndef _WIN32
Stephen Smalley7907ac72014-04-25 14:57:55 -0400125 if (sehnd) {
126 char *path = NULL;
127 char *secontext = NULL;
128
129 asprintf(&path, "%slost+found", dir_path);
130 if (selabel_lookup(sehnd, &secontext, path, S_IFDIR) < 0) {
131 error("cannot lookup security context for %s", path);
132 } else {
133 inode_set_selinux(inode, secontext);
134 freecon(secontext);
135 }
136 free(path);
137 }
138#endif
139
Colin Crossec0a2e82010-06-11 14:21:37 -0700140 return root_inode;
141}
142
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700143#ifndef _WIN32
Colin Crossec0a2e82010-06-11 14:21:37 -0700144/* Read a local directory and create the same tree in the generated filesystem.
Colin Crossa4460142012-12-20 01:00:33 -0800145 Calls itself recursively with each directory in the given directory.
146 full_path is an absolute or relative path, with a trailing slash, to the
147 directory on disk that should be copied, or NULL if this is a directory
148 that does not exist on disk (e.g. lost+found).
149 dir_path is an absolute path, with trailing slash, to the same directory
150 if the image were mounted at the specified mount point */
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700151static u32 build_directory_structure(const char *full_path, const char *dir_path, const char *target_out_path,
Kenny Root2e5c5232012-03-30 20:38:32 -0700152 u32 dir_inode, fs_config_func_t fs_config_func,
Doug Zongker95266802013-12-05 15:51:28 -0800153 struct selabel_handle *sehnd, int verbose, time_t fixed_time)
Colin Crossec0a2e82010-06-11 14:21:37 -0700154{
155 int entries = 0;
156 struct dentry *dentries;
Colin Crossc18120d2012-11-20 19:41:42 -0800157 struct dirent **namelist = NULL;
Colin Crossec0a2e82010-06-11 14:21:37 -0700158 struct stat stat;
159 int ret;
160 int i;
161 u32 inode;
162 u32 entry_inode;
163 u32 dirs = 0;
Colin Crossc18120d2012-11-20 19:41:42 -0800164 bool needs_lost_and_found = false;
Colin Crossec0a2e82010-06-11 14:21:37 -0700165
Colin Crossc18120d2012-11-20 19:41:42 -0800166 if (full_path) {
167 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
168 if (entries < 0) {
Mihai Serban04f48392015-01-07 12:44:24 +0200169#ifdef __GLIBC__
170 /* The scandir function implemented in glibc has a bug that makes it
171 erroneously fail with ENOMEM under certain circumstances.
172 As a workaround we can retry the scandir call with the same arguments.
173 GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
174 if (errno == ENOMEM)
175 entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
176#endif
177 if (entries < 0) {
178 error_errno("scandir");
179 return EXT4_ALLOCATE_FAILED;
180 }
Colin Crossc18120d2012-11-20 19:41:42 -0800181 }
182 }
183
184 if (dir_inode == 0) {
185 /* root directory, check if lost+found already exists */
186 for (i = 0; i < entries; i++)
187 if (strcmp(namelist[i]->d_name, "lost+found") == 0)
188 break;
189 if (i == entries)
190 needs_lost_and_found = true;
Colin Crossec0a2e82010-06-11 14:21:37 -0700191 }
192
193 dentries = calloc(entries, sizeof(struct dentry));
194 if (dentries == NULL)
195 critical_error_errno("malloc");
196
197 for (i = 0; i < entries; i++) {
198 dentries[i].filename = strdup(namelist[i]->d_name);
199 if (dentries[i].filename == NULL)
200 critical_error_errno("strdup");
201
Colin Crossa4460142012-12-20 01:00:33 -0800202 asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name);
203 asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name);
Colin Crossec0a2e82010-06-11 14:21:37 -0700204
205 free(namelist[i]);
206
207 ret = lstat(dentries[i].full_path, &stat);
208 if (ret < 0) {
209 error_errno("lstat");
210 i--;
211 entries--;
212 continue;
213 }
214
215 dentries[i].size = stat.st_size;
216 dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
Doug Zongker95266802013-12-05 15:51:28 -0800217 if (fixed_time == -1) {
218 dentries[i].mtime = stat.st_mtime;
219 } else {
220 dentries[i].mtime = fixed_time;
221 }
Nick Kralevich4df62f32013-02-07 14:21:34 -0800222 uint64_t capabilities;
Kenny Root68e3dfd2012-03-29 14:43:22 -0700223 if (fs_config_func != NULL) {
Colin Crossec0a2e82010-06-11 14:21:37 -0700224#ifdef ANDROID
225 unsigned int mode = 0;
226 unsigned int uid = 0;
227 unsigned int gid = 0;
228 int dir = S_ISDIR(stat.st_mode);
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700229 fs_config_func(dentries[i].path, dir, target_out_path, &uid, &gid, &mode, &capabilities);
Colin Crossec0a2e82010-06-11 14:21:37 -0700230 dentries[i].mode = mode;
231 dentries[i].uid = uid;
232 dentries[i].gid = gid;
Nick Kralevich4df62f32013-02-07 14:21:34 -0800233 dentries[i].capabilities = capabilities;
Colin Crossec0a2e82010-06-11 14:21:37 -0700234#else
235 error("can't set android permissions - built without android support");
236#endif
237 }
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700238#ifndef _WIN32
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500239 if (sehnd) {
Colin Crossa4460142012-12-20 01:00:33 -0800240 if (selabel_lookup(sehnd, &dentries[i].secon, dentries[i].path, stat.st_mode) < 0) {
241 error("cannot lookup security context for %s", dentries[i].path);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500242 }
William Roberts20573702013-01-17 13:24:27 -0800243
244 if (dentries[i].secon && verbose)
Colin Crossa4460142012-12-20 01:00:33 -0800245 printf("Labeling %s as %s\n", dentries[i].path, dentries[i].secon);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500246 }
247#endif
Colin Crossec0a2e82010-06-11 14:21:37 -0700248
249 if (S_ISREG(stat.st_mode)) {
250 dentries[i].file_type = EXT4_FT_REG_FILE;
251 } else if (S_ISDIR(stat.st_mode)) {
252 dentries[i].file_type = EXT4_FT_DIR;
253 dirs++;
254 } else if (S_ISCHR(stat.st_mode)) {
255 dentries[i].file_type = EXT4_FT_CHRDEV;
256 } else if (S_ISBLK(stat.st_mode)) {
257 dentries[i].file_type = EXT4_FT_BLKDEV;
258 } else if (S_ISFIFO(stat.st_mode)) {
259 dentries[i].file_type = EXT4_FT_FIFO;
260 } else if (S_ISSOCK(stat.st_mode)) {
261 dentries[i].file_type = EXT4_FT_SOCK;
262 } else if (S_ISLNK(stat.st_mode)) {
263 dentries[i].file_type = EXT4_FT_SYMLINK;
264 dentries[i].link = calloc(info.block_size, 1);
265 readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
266 } else {
267 error("unknown file type on %s", dentries[i].path);
268 i--;
269 entries--;
270 }
271 }
272 free(namelist);
273
Colin Crossc18120d2012-11-20 19:41:42 -0800274 if (needs_lost_and_found) {
275 /* insert a lost+found directory at the beginning of the dentries */
276 struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
277 memset(tmp, 0, sizeof(struct dentry));
278 memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
279 dentries = tmp;
280
281 dentries[0].filename = strdup("lost+found");
Colin Crossa4460142012-12-20 01:00:33 -0800282 asprintf(&dentries[0].path, "%slost+found", dir_path);
Colin Crossc18120d2012-11-20 19:41:42 -0800283 dentries[0].full_path = NULL;
284 dentries[0].size = 0;
285 dentries[0].mode = S_IRWXU;
286 dentries[0].file_type = EXT4_FT_DIR;
287 dentries[0].uid = 0;
288 dentries[0].gid = 0;
Colin Crossa532ecf2012-11-26 16:32:16 -0800289 if (sehnd) {
Colin Crossa4460142012-12-20 01:00:33 -0800290 if (selabel_lookup(sehnd, &dentries[0].secon, dentries[0].path, dentries[0].mode) < 0)
Colin Crossa532ecf2012-11-26 16:32:16 -0800291 error("cannot lookup security context for %s", dentries[0].path);
Colin Crossa532ecf2012-11-26 16:32:16 -0800292 }
Colin Crossc18120d2012-11-20 19:41:42 -0800293 entries++;
294 dirs++;
295 }
296
Colin Crossec0a2e82010-06-11 14:21:37 -0700297 inode = make_directory(dir_inode, entries, dentries, dirs);
298
299 for (i = 0; i < entries; i++) {
300 if (dentries[i].file_type == EXT4_FT_REG_FILE) {
301 entry_inode = make_file(dentries[i].full_path, dentries[i].size);
302 } else if (dentries[i].file_type == EXT4_FT_DIR) {
Colin Crossa4460142012-12-20 01:00:33 -0800303 char *subdir_full_path = NULL;
304 char *subdir_dir_path;
305 if (dentries[i].full_path) {
306 ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path);
307 if (ret < 0)
308 critical_error_errno("asprintf");
309 }
310 ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path);
311 if (ret < 0)
312 critical_error_errno("asprintf");
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700313 entry_inode = build_directory_structure(subdir_full_path, subdir_dir_path, target_out_path,
314 inode, fs_config_func, sehnd, verbose, fixed_time);
Colin Crossa4460142012-12-20 01:00:33 -0800315 free(subdir_full_path);
316 free(subdir_dir_path);
Colin Crossec0a2e82010-06-11 14:21:37 -0700317 } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
Nick Kralevich5446bde2013-02-19 19:05:47 -0800318 entry_inode = make_link(dentries[i].link);
Colin Crossec0a2e82010-06-11 14:21:37 -0700319 } else {
320 error("unknown file type on %s", dentries[i].path);
321 entry_inode = 0;
322 }
323 *dentries[i].inode = entry_inode;
324
325 ret = inode_set_permissions(entry_inode, dentries[i].mode,
Colin Crossde61f982010-08-04 15:06:09 -0700326 dentries[i].uid, dentries[i].gid,
327 dentries[i].mtime);
Colin Crossec0a2e82010-06-11 14:21:37 -0700328 if (ret)
329 error("failed to set permissions on %s\n", dentries[i].path);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800330
331 /*
332 * It's important to call inode_set_selinux() before
333 * inode_set_capabilities(). Extended attributes need to
334 * be stored sorted order, and we guarantee this by making
335 * the calls in the proper order.
336 * Please see xattr_assert_sane() in contents.c
337 */
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500338 ret = inode_set_selinux(entry_inode, dentries[i].secon);
339 if (ret)
340 error("failed to set SELinux context on %s\n", dentries[i].path);
Nick Kralevich4df62f32013-02-07 14:21:34 -0800341 ret = inode_set_capabilities(entry_inode, dentries[i].capabilities);
342 if (ret)
343 error("failed to set capability on %s\n", dentries[i].path);
Colin Crossec0a2e82010-06-11 14:21:37 -0700344
345 free(dentries[i].path);
346 free(dentries[i].full_path);
347 free(dentries[i].link);
348 free((void *)dentries[i].filename);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500349 free(dentries[i].secon);
Colin Crossec0a2e82010-06-11 14:21:37 -0700350 }
351
352 free(dentries);
353 return inode;
354}
Raphael Moll4605b3f2012-02-03 23:02:33 -0800355#endif
Colin Crossec0a2e82010-06-11 14:21:37 -0700356
357static u32 compute_block_size()
358{
359 return 4096;
360}
361
Colin Crosse4b5ae82010-08-03 14:10:07 -0700362static u32 compute_journal_blocks()
363{
364 u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
365 if (journal_blocks < 1024)
366 journal_blocks = 1024;
367 if (journal_blocks > 32768)
368 journal_blocks = 32768;
369 return journal_blocks;
370}
371
Colin Crossec0a2e82010-06-11 14:21:37 -0700372static u32 compute_blocks_per_group()
373{
374 return info.block_size * 8;
375}
376
377static u32 compute_inodes()
378{
379 return DIV_ROUND_UP(info.len, info.block_size) / 4;
380}
381
382static u32 compute_inodes_per_group()
383{
384 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
385 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
Colin Cross96cc54a2011-04-29 16:45:48 -0700386 u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
Paul Lawrence40ce87a2014-03-03 10:51:58 -0800387 inodes = EXT4_ALIGN(inodes, (info.block_size / info.inode_size));
Ken Sumrall107a9f12011-06-29 20:28:30 -0700388
389 /* After properly rounding up the number of inodes/group,
390 * make sure to update the total inodes field in the info struct.
391 */
392 info.inodes = inodes * block_groups;
393
394 return inodes;
Colin Crossec0a2e82010-06-11 14:21:37 -0700395}
396
Colin Cross22742ce2010-12-22 16:01:52 -0800397static u32 compute_bg_desc_reserve_blocks()
398{
399 u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
400 u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
401 u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
402 info.block_size);
403
404 u32 bg_desc_reserve_blocks =
405 DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
406 info.block_size) - bg_desc_blocks;
407
408 if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
409 bg_desc_reserve_blocks = info.block_size / sizeof(u32);
410
411 return bg_desc_reserve_blocks;
412}
413
Doug Zongker263eefd2010-06-29 17:23:14 -0700414void reset_ext4fs_info() {
Doug Zongker95266802013-12-05 15:51:28 -0800415 // Reset all the global data structures used by make_ext4fs so it
416 // can be called again.
417 memset(&info, 0, sizeof(info));
418 memset(&aux_info, 0, sizeof(aux_info));
Colin Crossf0ee37f2012-04-24 17:48:43 -0700419
Colin Cross3843c142014-01-31 09:38:20 -0800420 if (ext4_sparse_file) {
421 sparse_file_destroy(ext4_sparse_file);
422 ext4_sparse_file = NULL;
Doug Zongker95266802013-12-05 15:51:28 -0800423 }
Colin Crossec0a2e82010-06-11 14:21:37 -0700424}
425
Colin Cross96529862013-01-23 15:38:57 -0800426int make_ext4fs_sparse_fd(int fd, long long len,
Doug Zongker95266802013-12-05 15:51:28 -0800427 const char *mountpoint, struct selabel_handle *sehnd)
Colin Cross03ad99c2013-01-23 15:25:29 -0800428{
Paul Crowleyd7a3fb42015-11-27 09:05:39 +0000429 return make_ext4fs_sparse_fd_directory(fd, len, mountpoint, sehnd, NULL);
430}
431
432int make_ext4fs_sparse_fd_directory(int fd, long long len,
433 const char *mountpoint, struct selabel_handle *sehnd,
434 const char *directory)
435{
Colin Cross03ad99c2013-01-23 15:25:29 -0800436 reset_ext4fs_info();
437 info.len = len;
438
Paul Crowleyd7a3fb42015-11-27 09:05:39 +0000439 return make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000440 0, 1, 0, 0, 0,
441 sehnd, 0, -1, NULL, NULL, NULL);
Colin Cross03ad99c2013-01-23 15:25:29 -0800442}
443
Colin Cross96529862013-01-23 15:38:57 -0800444int make_ext4fs(const char *filename, long long len,
Doug Zongker95266802013-12-05 15:51:28 -0800445 const char *mountpoint, struct selabel_handle *sehnd)
Ken Sumrall983fb192011-01-19 17:15:42 -0800446{
Paul Lawrence01c43f22015-11-05 13:37:43 -0800447 return make_ext4fs_directory(filename, len, mountpoint, sehnd, NULL);
448}
449
450int make_ext4fs_directory(const char *filename, long long len,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000451 const char *mountpoint, struct selabel_handle *sehnd,
452 const char *directory)
Paul Lawrence01c43f22015-11-05 13:37:43 -0800453{
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800454 int fd;
455 int status;
456
457 reset_ext4fs_info();
458 info.len = len;
459
460 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
461 if (fd < 0) {
462 error_errno("open");
463 return EXIT_FAILURE;
464 }
465
Paul Crowleyd7a3fb42015-11-27 09:05:39 +0000466 status = make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000467 0, 0, 0, 1, 0,
468 sehnd, 0, -1, NULL, NULL, NULL);
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800469 close(fd);
470
471 return status;
Ken Sumrall983fb192011-01-19 17:15:42 -0800472}
473
Colin Cross5b89a4a2012-12-21 11:17:58 -0800474/* return a newly-malloc'd string that is a copy of str. The new string
475 is guaranteed to have a trailing slash. If absolute is true, the new string
476 is also guaranteed to have a leading slash.
477*/
478static char *canonicalize_slashes(const char *str, bool absolute)
479{
480 char *ret;
481 int len = strlen(str);
482 int newlen = len;
483 char *ptr;
484
Benoit Fradin7b4448d2013-08-28 14:44:32 +0200485 if (len == 0) {
486 if (absolute)
487 return strdup("/");
488 else
489 return strdup("");
Colin Cross5b89a4a2012-12-21 11:17:58 -0800490 }
491
492 if (str[0] != '/' && absolute) {
493 newlen++;
494 }
495 if (str[len - 1] != '/') {
496 newlen++;
497 }
498 ret = malloc(newlen + 1);
499 if (!ret) {
500 critical_error("malloc");
501 }
502
503 ptr = ret;
504 if (str[0] != '/' && absolute) {
505 *ptr++ = '/';
506 }
507
508 strcpy(ptr, str);
509 ptr += len;
510
511 if (str[len - 1] != '/') {
512 *ptr++ = '/';
513 }
514
515 if (ptr != ret + newlen) {
516 critical_error("assertion failed\n");
517 }
518
519 *ptr = '\0';
520
521 return ret;
522}
523
524static char *canonicalize_abs_slashes(const char *str)
525{
526 return canonicalize_slashes(str, true);
527}
528
529static char *canonicalize_rel_slashes(const char *str)
530{
531 return canonicalize_slashes(str, false);
532}
533
Mohamad Ayyash95791982016-02-20 03:46:00 +0000534static int compare_chunks(const void* chunk1, const void* chunk2) {
535 struct region* c1 = (struct region*) chunk1;
536 struct region* c2 = (struct region*) chunk2;
537 return c1->block - c2->block;
538}
539
540static int get_block_group(u32 block) {
Dmitry Shmidtcd205302016-11-18 15:38:47 -0800541 unsigned int i, group = 0;
542
Mohamad Ayyash95791982016-02-20 03:46:00 +0000543 for(i = 0; i < aux_info.groups; i++) {
544 if (block >= aux_info.bgs[i].first_block)
545 group = i;
546 else
547 break;
548 }
549 return group;
550}
551
552static void extract_base_fs_allocations(const char *directory, const char *mountpoint,
553 FILE* base_alloc_file_in) {
554#define err_msg "base file badly formatted"
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700555#ifndef _WIN32
Mohamad Ayyash95791982016-02-20 03:46:00 +0000556 // FORMAT Version 1.0: filename blk_mapping
557 const char *base_alloc_file_in_format = "%s %s";
558 const int base_file_format_param_count = 2;
559
560 char stored_file_name[MAX_PATH], real_file_name[MAX_PATH], file_map[MAX_BLK_MAPPING_STR];
561 struct block_allocation *fs_alloc;
562 struct block_group_info *bgs = aux_info.bgs;
Dmitry Shmidtcd205302016-11-18 15:38:47 -0800563 int major_version = 0, minor_version = 0;
564 unsigned int i;
Mohamad Ayyash95791982016-02-20 03:46:00 +0000565 char *base_file_line = NULL;
566 size_t base_file_line_len = 0;
567
568 printf("[v%d.%d] Generating an Incremental EXT4 image\n",
569 blk_file_major_ver, blk_file_minor_ver);
570 if (base_fs_allocations == NULL)
571 base_fs_allocations = create_allocation();
572 fs_alloc = base_fs_allocations;
573
574 fscanf(base_alloc_file_in, blk_file_header_fmt, &major_version, &minor_version);
575 if (major_version == 0) {
576 critical_error("Invalid base file");
577 }
578
579 if (major_version != blk_file_major_ver) {
580 critical_error("Incompatible base file: version required is %d.X",
581 blk_file_major_ver);
582 }
583
584 if (minor_version < blk_file_minor_ver) {
585 critical_error("Incompatible base file: version required is %d.%d or above",
586 blk_file_major_ver, blk_file_minor_ver);
587 }
588
589 while (getline(&base_file_line, &base_file_line_len, base_alloc_file_in) != -1) {
590 if (sscanf(base_file_line, base_alloc_file_in_format, &stored_file_name, &file_map)
591 != base_file_format_param_count) {
592 continue;
593 }
594 if (strlen(stored_file_name) < strlen(mountpoint)) {
595 continue;
596 }
597 snprintf(real_file_name, MAX_PATH, "%s%s", directory, stored_file_name + strlen(mountpoint));
598 if (!access(real_file_name, R_OK)) {
599 char *block_range, *end_string;
600 int real_file_fd;
Dmitry Shmidtcd205302016-11-18 15:38:47 -0800601 int start_block, end_block;
602 u32 block_file_size;
Mohamad Ayyash95791982016-02-20 03:46:00 +0000603 u32 real_file_block_size;
604
605 real_file_fd = open(real_file_name, O_RDONLY);
606 if (real_file_fd == -1) {
607 critical_error(err_msg);
608 }
609 real_file_block_size = get_file_size(real_file_fd);
610 close(real_file_fd);
611 real_file_block_size = DIV_ROUND_UP(real_file_block_size, info.block_size);
612 fs_alloc->filename = strdup(real_file_name);
613 block_range = strtok_r(file_map, ",", &end_string);
614 while (block_range && real_file_block_size) {
615 int block_group;
616 char *range, *end_token = NULL;
617 range = strtok_r(block_range, "-", &end_token);
618 if (!range) {
619 critical_error(err_msg);
620 }
621 start_block = parse_num(range);
622 range = strtok_r(NULL, "-", &end_token);
623 if (!range) {
624 end_block = start_block;
625 } else {
626 end_block = parse_num(range);
627 }
Mohamad Ayyash95791982016-02-20 03:46:00 +0000628 // Assummption is that allocations are within the same block group
629 block_group = get_block_group(start_block);
630 if (block_group != get_block_group(end_block)) {
631 critical_error("base file allocation's end block is in a different "
632 "block group than start block. did you change fs params?");
633 }
634 block_range = strtok_r(NULL, ",", &end_string);
Mohamad Ayyashd2ed02a2016-04-28 21:49:01 -0700635 int bg_first_block = bgs[block_group].first_block;
Colin Crossafb52972016-09-30 01:01:50 +0000636 int min_bg_bound = bgs[block_group].chunks[0].block + bgs[block_group].chunks[0].len;
637 int max_bg_bound = bgs[block_group].chunks[bgs[block_group].chunk_count - 1].block;
Mohamad Ayyashd2ed02a2016-04-28 21:49:01 -0700638
639 if (min_bg_bound >= start_block - bg_first_block ||
640 max_bg_bound <= end_block - bg_first_block) {
641 continue;
642 }
643 block_file_size = end_block - start_block + 1;
644 if (block_file_size > real_file_block_size) {
645 block_file_size = real_file_block_size;
646 }
Mohamad Ayyash95791982016-02-20 03:46:00 +0000647 append_region(fs_alloc, start_block, block_file_size, block_group);
648 reserve_bg_chunk(block_group, start_block - bgs[block_group].first_block, block_file_size);
649 real_file_block_size -= block_file_size;
650 }
651 if (reserve_blocks_for_allocation(fs_alloc) < 0)
652 critical_error("failed to reserve base fs allocation");
653 fs_alloc->next = create_allocation();
654 fs_alloc = fs_alloc->next;
655 }
656 }
657
658 for (i = 0; i < aux_info.groups; i++) {
659 qsort(bgs[i].chunks, bgs[i].chunk_count, sizeof(struct region), compare_chunks);
660 }
661
662 free(base_file_line);
663
Mohamad Ayyashef244242016-02-23 20:13:02 -0800664#else
665 return;
666#endif
Mohamad Ayyash95791982016-02-20 03:46:00 +0000667#undef err_msg
668}
669
670void generate_base_alloc_file_out(FILE* base_alloc_file_out, char* dir, char* mountpoint,
671 struct block_allocation* p)
672{
673 size_t dirlen = dir ? strlen(dir) : 0;
674 fprintf(base_alloc_file_out, blk_file_header_fmt, blk_file_major_ver, blk_file_minor_ver);
675 fputc('\n', base_alloc_file_out);
676 while (p) {
677 if (dir && strncmp(p->filename, dir, dirlen) == 0) {
678 // substitute mountpoint for the leading directory in the filename, in the output file
679 fprintf(base_alloc_file_out, "%s%s", mountpoint, p->filename + dirlen);
680 } else {
681 fprintf(base_alloc_file_out, "%s", p->filename);
682 }
683 print_blocks(base_alloc_file_out, p, ',');
684 struct block_allocation* pn = p->next;
685 p = pn;
686 }
687}
688
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700689int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out_directory,
Doug Zongker95266802013-12-05 15:51:28 -0800690 const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
Jeff Sharkeyf9659682015-04-06 22:29:04 -0700691 int sparse, int crc, int wipe, int real_uuid,
Doug Zongkerbec598e2014-08-12 11:35:37 -0700692 struct selabel_handle *sehnd, int verbose, time_t fixed_time,
Mohamad Ayyash95791982016-02-20 03:46:00 +0000693 FILE* block_list_file, FILE* base_alloc_file_in, FILE* base_alloc_file_out)
Colin Crossec0a2e82010-06-11 14:21:37 -0700694{
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800695 u32 root_inode_num;
696 u16 root_mode;
Colin Crossa4460142012-12-20 01:00:33 -0800697 char *mountpoint;
698 char *directory = NULL;
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700699 char *target_out_directory = NULL;
Mohamad Ayyash95791982016-02-20 03:46:00 +0000700 struct block_allocation* p;
Colin Crossec0a2e82010-06-11 14:21:37 -0700701
Ken Sumrall2ae76632011-03-23 22:08:53 -0700702 if (setjmp(setjmp_env))
703 return EXIT_FAILURE; /* Handle a call to longjmp() */
704
Eric Miaoaaf32ea2015-04-16 15:00:11 -0700705 info.block_device = is_block_device_fd(fd);
706
707 if (info.block_device && (sparse || gzip || crc)) {
708 fprintf(stderr, "No sparse/gzip/crc allowed for block device\n");
709 return EXIT_FAILURE;
710 }
711
Colin Crossa4460142012-12-20 01:00:33 -0800712 if (_mountpoint == NULL) {
713 mountpoint = strdup("");
Colin Crossa4460142012-12-20 01:00:33 -0800714 } else {
Colin Cross5b89a4a2012-12-21 11:17:58 -0800715 mountpoint = canonicalize_abs_slashes(_mountpoint);
Colin Crossa4460142012-12-20 01:00:33 -0800716 }
717
718 if (_directory) {
Colin Cross5b89a4a2012-12-21 11:17:58 -0800719 directory = canonicalize_rel_slashes(_directory);
Colin Crossa4460142012-12-20 01:00:33 -0800720 }
721
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700722 if (_target_out_directory) {
723 target_out_directory = canonicalize_rel_slashes(_target_out_directory);
724 }
725
Ken Sumrall435a8b62011-01-14 18:33:06 -0800726 if (info.len <= 0)
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800727 info.len = get_file_size(fd);
Colin Crossec0a2e82010-06-11 14:21:37 -0700728
729 if (info.len <= 0) {
730 fprintf(stderr, "Need size of filesystem\n");
Anatol Pomazau0349bd92012-01-11 15:12:27 -0800731 return EXIT_FAILURE;
Colin Crossec0a2e82010-06-11 14:21:37 -0700732 }
733
Colin Crossec0a2e82010-06-11 14:21:37 -0700734 if (info.block_size <= 0)
735 info.block_size = compute_block_size();
736
Ken Sumrall88833a62011-07-13 17:27:07 -0700737 /* Round down the filesystem length to be a multiple of the block size */
738 info.len &= ~((u64)info.block_size - 1);
739
Colin Crosse4b5ae82010-08-03 14:10:07 -0700740 if (info.journal_blocks == 0)
741 info.journal_blocks = compute_journal_blocks();
742
743 if (info.no_journal == 0)
744 info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
745 else
746 info.journal_blocks = 0;
747
Colin Crossec0a2e82010-06-11 14:21:37 -0700748 if (info.blocks_per_group <= 0)
749 info.blocks_per_group = compute_blocks_per_group();
750
751 if (info.inodes <= 0)
752 info.inodes = compute_inodes();
753
754 if (info.inode_size <= 0)
755 info.inode_size = 256;
756
757 if (info.label == NULL)
758 info.label = "";
759
760 info.inodes_per_group = compute_inodes_per_group();
761
762 info.feat_compat |=
Nick Kralevich4df62f32013-02-07 14:21:34 -0800763 EXT4_FEATURE_COMPAT_RESIZE_INODE |
764 EXT4_FEATURE_COMPAT_EXT_ATTR;
Colin Crossec0a2e82010-06-11 14:21:37 -0700765
766 info.feat_ro_compat |=
767 EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
Mohamad Ayyashdedf8f92016-04-14 19:43:31 -0700768 EXT4_FEATURE_RO_COMPAT_LARGE_FILE |
769 EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
Colin Crossec0a2e82010-06-11 14:21:37 -0700770
771 info.feat_incompat |=
772 EXT4_FEATURE_INCOMPAT_EXTENTS |
773 EXT4_FEATURE_INCOMPAT_FILETYPE;
774
775
Colin Cross22742ce2010-12-22 16:01:52 -0800776 info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
777
Colin Crossec0a2e82010-06-11 14:21:37 -0700778 printf("Creating filesystem with parameters:\n");
Colin Crossaf072342014-01-23 13:19:27 -0800779 printf(" Size: %"PRIu64"\n", info.len);
Colin Crossec0a2e82010-06-11 14:21:37 -0700780 printf(" Block size: %d\n", info.block_size);
781 printf(" Blocks per group: %d\n", info.blocks_per_group);
782 printf(" Inodes per group: %d\n", info.inodes_per_group);
783 printf(" Inode size: %d\n", info.inode_size);
Colin Crosse4b5ae82010-08-03 14:10:07 -0700784 printf(" Journal blocks: %d\n", info.journal_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700785 printf(" Label: %s\n", info.label);
786
787 ext4_create_fs_aux_info();
788
Colin Crossaf072342014-01-23 13:19:27 -0800789 printf(" Blocks: %"PRIu64"\n", aux_info.len_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700790 printf(" Block groups: %d\n", aux_info.groups);
Colin Cross22742ce2010-12-22 16:01:52 -0800791 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
Colin Crossec0a2e82010-06-11 14:21:37 -0700792
Colin Cross782879a2014-01-23 13:08:16 -0800793 ext4_sparse_file = sparse_file_new(info.block_size, info.len);
Colin Crossf0ee37f2012-04-24 17:48:43 -0700794
Colin Crossec0a2e82010-06-11 14:21:37 -0700795 block_allocator_init();
796
Jeff Sharkeyf9659682015-04-06 22:29:04 -0700797 ext4_fill_in_sb(real_uuid);
Colin Crossec0a2e82010-06-11 14:21:37 -0700798
Mohamad Ayyash95791982016-02-20 03:46:00 +0000799 if (base_alloc_file_in) {
800 extract_base_fs_allocations(directory, mountpoint, base_alloc_file_in);
801 }
Colin Crossec0a2e82010-06-11 14:21:37 -0700802 if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
803 error("failed to reserve first 10 inodes");
804
805 if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
806 ext4_create_journal_inode();
807
808 if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
809 ext4_create_resize_inode();
810
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700811#ifdef _WIN32
Raphael Moll4605b3f2012-02-03 23:02:33 -0800812 // Windows needs only 'create an empty fs image' functionality
813 assert(!directory);
Stephen Smalley7907ac72014-04-25 14:57:55 -0400814 root_inode_num = build_default_directory_structure(mountpoint, sehnd);
Raphael Moll4605b3f2012-02-03 23:02:33 -0800815#else
Colin Crossec0a2e82010-06-11 14:21:37 -0700816 if (directory)
Thierry Strudelb89e81d2015-07-09 16:31:39 -0700817 root_inode_num = build_directory_structure(directory, mountpoint, target_out_directory, 0,
Doug Zongkerbec598e2014-08-12 11:35:37 -0700818 fs_config_func, sehnd, verbose, fixed_time);
Colin Crossec0a2e82010-06-11 14:21:37 -0700819 else
Stephen Smalley7907ac72014-04-25 14:57:55 -0400820 root_inode_num = build_default_directory_structure(mountpoint, sehnd);
Raphael Moll4605b3f2012-02-03 23:02:33 -0800821#endif
Doug Zongker263eefd2010-06-29 17:23:14 -0700822
Colin Crossec0a2e82010-06-11 14:21:37 -0700823 root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
Colin Crossde61f982010-08-04 15:06:09 -0700824 inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
Colin Crossec0a2e82010-06-11 14:21:37 -0700825
Elliott Hughes1eb2f542016-10-05 09:44:48 -0700826#ifndef _WIN32
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500827 if (sehnd) {
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500828 char *secontext = NULL;
Stephen Smalley6ece7082012-02-09 14:19:24 -0500829
Colin Crossa4460142012-12-20 01:00:33 -0800830 if (selabel_lookup(sehnd, &secontext, mountpoint, S_IFDIR) < 0) {
831 error("cannot lookup security context for %s", mountpoint);
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500832 }
Robert Craig29293132013-01-30 12:54:46 -0500833 if (secontext) {
834 if (verbose) {
835 printf("Labeling %s as %s\n", mountpoint, secontext);
836 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500837 inode_set_selinux(root_inode_num, secontext);
838 }
Stephen Smalleyb4eca4b2012-01-13 09:00:56 -0500839 freecon(secontext);
840 }
841#endif
842
Colin Crossec0a2e82010-06-11 14:21:37 -0700843 ext4_update_free();
844
Mohamad Ayyash95791982016-02-20 03:46:00 +0000845 // TODO: Consider migrating the OTA tools to the new base alloc file format
846 // used for generating incremental images (see go/incremental-ext4)
Doug Zongkerbec598e2014-08-12 11:35:37 -0700847 if (block_list_file) {
Mohamad Ayyash18785a82016-02-19 21:16:34 +0000848 size_t dirlen = directory ? strlen(directory) : 0;
Doug Zongkerbec598e2014-08-12 11:35:37 -0700849 struct block_allocation* p = get_saved_allocation_chain();
Mohamad Ayyash18785a82016-02-19 21:16:34 +0000850 while (p) {
851 if (directory && strncmp(p->filename, directory, dirlen) == 0) {
852 // substitute mountpoint for the leading directory in the filename, in the output file
853 fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen);
854 } else {
855 fprintf(block_list_file, "%s", p->filename);
856 }
Mohamad Ayyash95791982016-02-20 03:46:00 +0000857 print_blocks(block_list_file, p, ' ');
Mohamad Ayyash18785a82016-02-19 21:16:34 +0000858 struct block_allocation* pn = p->next;
Mohamad Ayyash18785a82016-02-19 21:16:34 +0000859 p = pn;
860 }
Doug Zongkerbec598e2014-08-12 11:35:37 -0700861 }
862
Mohamad Ayyash95791982016-02-20 03:46:00 +0000863 if (base_alloc_file_out) {
864 struct block_allocation* p = get_saved_allocation_chain();
865 generate_base_alloc_file_out(base_alloc_file_out, directory, mountpoint, p);
866 }
867
Colin Crossec0a2e82010-06-11 14:21:37 -0700868 printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
869 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
870 aux_info.sb->s_inodes_count,
871 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
872 aux_info.sb->s_blocks_count_lo);
873
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +0200874 if (wipe && WIPE_IS_SUPPORTED) {
Colin Crossdc5abee2012-04-23 23:20:48 -0700875 wipe_block_device(fd, info.len);
David 'Digit' Turnereb5fcc32014-06-12 20:54:50 +0200876 }
Colin Crossdc5abee2012-04-23 23:20:48 -0700877
878 write_ext4_image(fd, gzip, sparse, crc);
Colin Crossec0a2e82010-06-11 14:21:37 -0700879
Colin Cross782879a2014-01-23 13:08:16 -0800880 sparse_file_destroy(ext4_sparse_file);
881 ext4_sparse_file = NULL;
Colin Crossf0ee37f2012-04-24 17:48:43 -0700882
Mohamad Ayyash95791982016-02-20 03:46:00 +0000883 p = get_saved_allocation_chain();
884 while (p) {
885 struct block_allocation* pn = p->next;
886 free_alloc(p);
887 p = pn;
888 }
889
Colin Crossa4460142012-12-20 01:00:33 -0800890 free(mountpoint);
891 free(directory);
892
Colin Crossec0a2e82010-06-11 14:21:37 -0700893 return 0;
894}