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 
17 #include "make_ext4fs.h"
18 #include "ext4_utils.h"
19 #include "allocate.h"
20 #include "contents.h"
21 #include "wipe.h"
22 
23 #include <sparse/sparse.h>
24 
25 #include <assert.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include <libgen.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 
37 #ifdef USE_MINGW
38 
39 #include <winsock2.h>
40 
41 /* These match the Linux definitions of these flags.
42    L_xx is defined to avoid conflicting with the win32 versions.
43 */
44 #undef S_IRWXU
45 #undef S_IRGRP
46 #undef S_IWGRP
47 #undef S_IXGRP
48 #undef S_IRWXG
49 #undef S_IROTH
50 #undef S_IWOTH
51 #undef S_IXOTH
52 #undef S_IRWXO
53 #undef S_ISUID
54 #undef S_ISGID
55 #undef S_ISVTX
56 
57 #define L_S_IRUSR 00400
58 #define L_S_IWUSR 00200
59 #define L_S_IXUSR 00100
60 #define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
61 #define S_IRGRP 00040
62 #define S_IWGRP 00020
63 #define S_IXGRP 00010
64 #define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
65 #define S_IROTH 00004
66 #define S_IWOTH 00002
67 #define S_IXOTH 00001
68 #define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
69 #define S_ISUID 0004000
70 #define S_ISGID 0002000
71 #define S_ISVTX 0001000
72 
73 #else
74 
75 #include <selinux/selinux.h>
76 #include <selinux/label.h>
77 
78 #define O_BINARY 0
79 
80 #endif
81 
82 #define MAX_PATH 4096
83 #define MAX_BLK_MAPPING_STR 1000
84 
85 const int blk_file_major_ver = 1;
86 const int blk_file_minor_ver = 0;
87 const char *blk_file_header_fmt = "Base EXT4 version %d.%d";
88 
89 /* TODO: Not implemented:
90    Allocating blocks in the same block group as the file inode
91    Hash or binary tree directories
92    Special files: sockets, devices, fifos
93  */
94 
filter_dot(const struct dirent * d)95 static int filter_dot(const struct dirent *d)
96 {
97 	return (strcmp(d->d_name, "..") && strcmp(d->d_name, "."));
98 }
99 
build_default_directory_structure(const char * dir_path,struct selabel_handle * sehnd)100 static u32 build_default_directory_structure(const char *dir_path,
101 						 struct selabel_handle *sehnd)
102 {
103 	u32 inode;
104 	u32 root_inode;
105 	struct dentry dentries = {
106 			.filename = "lost+found",
107 			.file_type = EXT4_FT_DIR,
108 			.mode = S_IRWXU,
109 			.uid = 0,
110 			.gid = 0,
111 			.mtime = 0,
112 	};
113 	root_inode = make_directory(0, 1, &dentries, 1);
114 	inode = make_directory(root_inode, 0, NULL, 0);
115 	*dentries.inode = inode;
116 	inode_set_permissions(inode, dentries.mode,
117 		dentries.uid, dentries.gid, dentries.mtime);
118 
119 #ifndef USE_MINGW
120 	if (sehnd) {
121 		char *path = NULL;
122 		char *secontext = NULL;
123 
124 		asprintf(&path, "%slost+found", dir_path);
125 		if (selabel_lookup(sehnd, &secontext, path, S_IFDIR) < 0) {
126 			error("cannot lookup security context for %s", path);
127 		} else {
128 			inode_set_selinux(inode, secontext);
129 			freecon(secontext);
130 		}
131 		free(path);
132 	}
133 #endif
134 
135 	return root_inode;
136 }
137 
138 #ifndef USE_MINGW
139 /* Read a local directory and create the same tree in the generated filesystem.
140    Calls itself recursively with each directory in the given directory.
141    full_path is an absolute or relative path, with a trailing slash, to the
142    directory on disk that should be copied, or NULL if this is a directory
143    that does not exist on disk (e.g. lost+found).
144    dir_path is an absolute path, with trailing slash, to the same directory
145    if the image were mounted at the specified mount point */
build_directory_structure(const char * full_path,const char * dir_path,const char * target_out_path,u32 dir_inode,fs_config_func_t fs_config_func,struct selabel_handle * sehnd,int verbose,time_t fixed_time)146 static u32 build_directory_structure(const char *full_path, const char *dir_path, const char *target_out_path,
147 		u32 dir_inode, fs_config_func_t fs_config_func,
148 		struct selabel_handle *sehnd, int verbose, time_t fixed_time)
149 {
150 	int entries = 0;
151 	struct dentry *dentries;
152 	struct dirent **namelist = NULL;
153 	struct stat stat;
154 	int ret;
155 	int i;
156 	u32 inode;
157 	u32 entry_inode;
158 	u32 dirs = 0;
159 	bool needs_lost_and_found = false;
160 
161 	if (full_path) {
162 		entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
163 		if (entries < 0) {
164 #ifdef __GLIBC__
165 			/* The scandir function implemented in glibc has a bug that makes it
166 			   erroneously fail with ENOMEM under certain circumstances.
167 			   As a workaround we can retry the scandir call with the same arguments.
168 			   GLIBC BZ: https://sourceware.org/bugzilla/show_bug.cgi?id=17804 */
169 			if (errno == ENOMEM)
170 				entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
171 #endif
172 			if (entries < 0) {
173 				error_errno("scandir");
174 				return EXT4_ALLOCATE_FAILED;
175 			}
176 		}
177 	}
178 
179 	if (dir_inode == 0) {
180 		/* root directory, check if lost+found already exists */
181 		for (i = 0; i < entries; i++)
182 			if (strcmp(namelist[i]->d_name, "lost+found") == 0)
183 				break;
184 		if (i == entries)
185 			needs_lost_and_found = true;
186 	}
187 
188 	dentries = calloc(entries, sizeof(struct dentry));
189 	if (dentries == NULL)
190 		critical_error_errno("malloc");
191 
192 	for (i = 0; i < entries; i++) {
193 		dentries[i].filename = strdup(namelist[i]->d_name);
194 		if (dentries[i].filename == NULL)
195 			critical_error_errno("strdup");
196 
197 		asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name);
198 		asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name);
199 
200 		free(namelist[i]);
201 
202 		ret = lstat(dentries[i].full_path, &stat);
203 		if (ret < 0) {
204 			error_errno("lstat");
205 			i--;
206 			entries--;
207 			continue;
208 		}
209 
210 		dentries[i].size = stat.st_size;
211 		dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
212 		if (fixed_time == -1) {
213 			dentries[i].mtime = stat.st_mtime;
214 		} else {
215 			dentries[i].mtime = fixed_time;
216 		}
217 		uint64_t capabilities;
218 		if (fs_config_func != NULL) {
219 #ifdef ANDROID
220 			unsigned int mode = 0;
221 			unsigned int uid = 0;
222 			unsigned int gid = 0;
223 			int dir = S_ISDIR(stat.st_mode);
224 			fs_config_func(dentries[i].path, dir, target_out_path, &uid, &gid, &mode, &capabilities);
225 			dentries[i].mode = mode;
226 			dentries[i].uid = uid;
227 			dentries[i].gid = gid;
228 			dentries[i].capabilities = capabilities;
229 #else
230 			error("can't set android permissions - built without android support");
231 #endif
232 		}
233 #ifndef USE_MINGW
234 		if (sehnd) {
235 			if (selabel_lookup(sehnd, &dentries[i].secon, dentries[i].path, stat.st_mode) < 0) {
236 				error("cannot lookup security context for %s", dentries[i].path);
237 			}
238 
239 			if (dentries[i].secon && verbose)
240 				printf("Labeling %s as %s\n", dentries[i].path, dentries[i].secon);
241 		}
242 #endif
243 
244 		if (S_ISREG(stat.st_mode)) {
245 			dentries[i].file_type = EXT4_FT_REG_FILE;
246 		} else if (S_ISDIR(stat.st_mode)) {
247 			dentries[i].file_type = EXT4_FT_DIR;
248 			dirs++;
249 		} else if (S_ISCHR(stat.st_mode)) {
250 			dentries[i].file_type = EXT4_FT_CHRDEV;
251 		} else if (S_ISBLK(stat.st_mode)) {
252 			dentries[i].file_type = EXT4_FT_BLKDEV;
253 		} else if (S_ISFIFO(stat.st_mode)) {
254 			dentries[i].file_type = EXT4_FT_FIFO;
255 		} else if (S_ISSOCK(stat.st_mode)) {
256 			dentries[i].file_type = EXT4_FT_SOCK;
257 		} else if (S_ISLNK(stat.st_mode)) {
258 			dentries[i].file_type = EXT4_FT_SYMLINK;
259 			dentries[i].link = calloc(info.block_size, 1);
260 			readlink(dentries[i].full_path, dentries[i].link, info.block_size - 1);
261 		} else {
262 			error("unknown file type on %s", dentries[i].path);
263 			i--;
264 			entries--;
265 		}
266 	}
267 	free(namelist);
268 
269 	if (needs_lost_and_found) {
270 		/* insert a lost+found directory at the beginning of the dentries */
271 		struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry));
272 		memset(tmp, 0, sizeof(struct dentry));
273 		memcpy(tmp + 1, dentries, entries * sizeof(struct dentry));
274 		dentries = tmp;
275 
276 		dentries[0].filename = strdup("lost+found");
277 		asprintf(&dentries[0].path, "%slost+found", dir_path);
278 		dentries[0].full_path = NULL;
279 		dentries[0].size = 0;
280 		dentries[0].mode = S_IRWXU;
281 		dentries[0].file_type = EXT4_FT_DIR;
282 		dentries[0].uid = 0;
283 		dentries[0].gid = 0;
284 		if (sehnd) {
285 			if (selabel_lookup(sehnd, &dentries[0].secon, dentries[0].path, dentries[0].mode) < 0)
286 				error("cannot lookup security context for %s", dentries[0].path);
287 		}
288 		entries++;
289 		dirs++;
290 	}
291 
292 	inode = make_directory(dir_inode, entries, dentries, dirs);
293 
294 	for (i = 0; i < entries; i++) {
295 		if (dentries[i].file_type == EXT4_FT_REG_FILE) {
296 			entry_inode = make_file(dentries[i].full_path, dentries[i].size);
297 		} else if (dentries[i].file_type == EXT4_FT_DIR) {
298 			char *subdir_full_path = NULL;
299 			char *subdir_dir_path;
300 			if (dentries[i].full_path) {
301 				ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path);
302 				if (ret < 0)
303 					critical_error_errno("asprintf");
304 			}
305 			ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path);
306 			if (ret < 0)
307 				critical_error_errno("asprintf");
308 			entry_inode = build_directory_structure(subdir_full_path, subdir_dir_path, target_out_path,
309 					inode, fs_config_func, sehnd, verbose, fixed_time);
310 			free(subdir_full_path);
311 			free(subdir_dir_path);
312 		} else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
313 			entry_inode = make_link(dentries[i].link);
314 		} else {
315 			error("unknown file type on %s", dentries[i].path);
316 			entry_inode = 0;
317 		}
318 		*dentries[i].inode = entry_inode;
319 
320 		ret = inode_set_permissions(entry_inode, dentries[i].mode,
321 			dentries[i].uid, dentries[i].gid,
322 			dentries[i].mtime);
323 		if (ret)
324 			error("failed to set permissions on %s\n", dentries[i].path);
325 
326 		/*
327 		 * It's important to call inode_set_selinux() before
328 		 * inode_set_capabilities(). Extended attributes need to
329 		 * be stored sorted order, and we guarantee this by making
330 		 * the calls in the proper order.
331 		 * Please see xattr_assert_sane() in contents.c
332 		 */
333 		ret = inode_set_selinux(entry_inode, dentries[i].secon);
334 		if (ret)
335 			error("failed to set SELinux context on %s\n", dentries[i].path);
336 		ret = inode_set_capabilities(entry_inode, dentries[i].capabilities);
337 		if (ret)
338 			error("failed to set capability on %s\n", dentries[i].path);
339 
340 		free(dentries[i].path);
341 		free(dentries[i].full_path);
342 		free(dentries[i].link);
343 		free((void *)dentries[i].filename);
344 		free(dentries[i].secon);
345 	}
346 
347 	free(dentries);
348 	return inode;
349 }
350 #endif
351 
compute_block_size()352 static u32 compute_block_size()
353 {
354 	return 4096;
355 }
356 
compute_journal_blocks()357 static u32 compute_journal_blocks()
358 {
359 	u32 journal_blocks = DIV_ROUND_UP(info.len, info.block_size) / 64;
360 	if (journal_blocks < 1024)
361 		journal_blocks = 1024;
362 	if (journal_blocks > 32768)
363 		journal_blocks = 32768;
364 	return journal_blocks;
365 }
366 
compute_blocks_per_group()367 static u32 compute_blocks_per_group()
368 {
369 	return info.block_size * 8;
370 }
371 
compute_inodes()372 static u32 compute_inodes()
373 {
374 	return DIV_ROUND_UP(info.len, info.block_size) / 4;
375 }
376 
compute_inodes_per_group()377 static u32 compute_inodes_per_group()
378 {
379 	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
380 	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
381 	u32 inodes = DIV_ROUND_UP(info.inodes, block_groups);
382 	inodes = EXT4_ALIGN(inodes, (info.block_size / info.inode_size));
383 
384 	/* After properly rounding up the number of inodes/group,
385 	 * make sure to update the total inodes field in the info struct.
386 	 */
387 	info.inodes = inodes * block_groups;
388 
389 	return inodes;
390 }
391 
compute_bg_desc_reserve_blocks()392 static u32 compute_bg_desc_reserve_blocks()
393 {
394 	u32 blocks = DIV_ROUND_UP(info.len, info.block_size);
395 	u32 block_groups = DIV_ROUND_UP(blocks, info.blocks_per_group);
396 	u32 bg_desc_blocks = DIV_ROUND_UP(block_groups * sizeof(struct ext2_group_desc),
397 			info.block_size);
398 
399 	u32 bg_desc_reserve_blocks =
400 			DIV_ROUND_UP(block_groups * 1024 * sizeof(struct ext2_group_desc),
401 					info.block_size) - bg_desc_blocks;
402 
403 	if (bg_desc_reserve_blocks > info.block_size / sizeof(u32))
404 		bg_desc_reserve_blocks = info.block_size / sizeof(u32);
405 
406 	return bg_desc_reserve_blocks;
407 }
408 
reset_ext4fs_info()409 void reset_ext4fs_info() {
410 	// Reset all the global data structures used by make_ext4fs so it
411 	// can be called again.
412 	memset(&info, 0, sizeof(info));
413 	memset(&aux_info, 0, sizeof(aux_info));
414 
415 	if (ext4_sparse_file) {
416 		sparse_file_destroy(ext4_sparse_file);
417 		ext4_sparse_file = NULL;
418 	}
419 }
420 
make_ext4fs_sparse_fd(int fd,long long len,const char * mountpoint,struct selabel_handle * sehnd)421 int make_ext4fs_sparse_fd(int fd, long long len,
422 				const char *mountpoint, struct selabel_handle *sehnd)
423 {
424 	return make_ext4fs_sparse_fd_directory(fd, len, mountpoint, sehnd, NULL);
425 }
426 
make_ext4fs_sparse_fd_directory(int fd,long long len,const char * mountpoint,struct selabel_handle * sehnd,const char * directory)427 int make_ext4fs_sparse_fd_directory(int fd, long long len,
428 				const char *mountpoint, struct selabel_handle *sehnd,
429 				const char *directory)
430 {
431 	reset_ext4fs_info();
432 	info.len = len;
433 
434 	return make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
435 								0, 1, 0, 0, 0,
436 								sehnd, 0, -1, NULL, NULL, NULL);
437 }
438 
make_ext4fs(const char * filename,long long len,const char * mountpoint,struct selabel_handle * sehnd)439 int make_ext4fs(const char *filename, long long len,
440 				const char *mountpoint, struct selabel_handle *sehnd)
441 {
442 	return make_ext4fs_directory(filename, len, mountpoint, sehnd, NULL);
443 }
444 
make_ext4fs_directory(const char * filename,long long len,const char * mountpoint,struct selabel_handle * sehnd,const char * directory)445 int make_ext4fs_directory(const char *filename, long long len,
446 						  const char *mountpoint, struct selabel_handle *sehnd,
447 						  const char *directory)
448 {
449 	int fd;
450 	int status;
451 
452 	reset_ext4fs_info();
453 	info.len = len;
454 
455 	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
456 	if (fd < 0) {
457 		error_errno("open");
458 		return EXIT_FAILURE;
459 	}
460 
461 	status = make_ext4fs_internal(fd, directory, NULL, mountpoint, NULL,
462 								  0, 0, 0, 1, 0,
463 								  sehnd, 0, -1, NULL, NULL, NULL);
464 	close(fd);
465 
466 	return status;
467 }
468 
469 /* return a newly-malloc'd string that is a copy of str.  The new string
470    is guaranteed to have a trailing slash.  If absolute is true, the new string
471    is also guaranteed to have a leading slash.
472 */
canonicalize_slashes(const char * str,bool absolute)473 static char *canonicalize_slashes(const char *str, bool absolute)
474 {
475 	char *ret;
476 	int len = strlen(str);
477 	int newlen = len;
478 	char *ptr;
479 
480 	if (len == 0) {
481 		if (absolute)
482 			return strdup("/");
483 		else
484 			return strdup("");
485 	}
486 
487 	if (str[0] != '/' && absolute) {
488 		newlen++;
489 	}
490 	if (str[len - 1] != '/') {
491 		newlen++;
492 	}
493 	ret = malloc(newlen + 1);
494 	if (!ret) {
495 		critical_error("malloc");
496 	}
497 
498 	ptr = ret;
499 	if (str[0] != '/' && absolute) {
500 		*ptr++ = '/';
501 	}
502 
503 	strcpy(ptr, str);
504 	ptr += len;
505 
506 	if (str[len - 1] != '/') {
507 		*ptr++ = '/';
508 	}
509 
510 	if (ptr != ret + newlen) {
511 		critical_error("assertion failed\n");
512 	}
513 
514 	*ptr = '\0';
515 
516 	return ret;
517 }
518 
canonicalize_abs_slashes(const char * str)519 static char *canonicalize_abs_slashes(const char *str)
520 {
521 	return canonicalize_slashes(str, true);
522 }
523 
canonicalize_rel_slashes(const char * str)524 static char *canonicalize_rel_slashes(const char *str)
525 {
526 	return canonicalize_slashes(str, false);
527 }
528 
compare_chunks(const void * chunk1,const void * chunk2)529 static int compare_chunks(const void* chunk1, const void* chunk2) {
530 	struct region* c1 = (struct region*) chunk1;
531 	struct region* c2 = (struct region*) chunk2;
532 	return c1->block - c2->block;
533 }
534 
get_block_group(u32 block)535 static int get_block_group(u32 block) {
536 	int i, group = 0;
537 	for(i = 0; i < aux_info.groups; i++) {
538 		if (block >= aux_info.bgs[i].first_block)
539 			group = i;
540 		else
541 			break;
542 	}
543 	return group;
544 }
545 
extract_base_fs_allocations(const char * directory,const char * mountpoint,FILE * base_alloc_file_in)546 static void extract_base_fs_allocations(const char *directory, const char *mountpoint,
547 										FILE* base_alloc_file_in) {
548 #define err_msg "base file badly formatted"
549 #ifndef USE_MINGW
550 	// FORMAT Version 1.0: filename blk_mapping
551 	const char *base_alloc_file_in_format = "%s %s";
552 	const int base_file_format_param_count = 2;
553 
554 	char stored_file_name[MAX_PATH], real_file_name[MAX_PATH], file_map[MAX_BLK_MAPPING_STR];
555 	struct block_allocation *fs_alloc;
556 	struct block_group_info *bgs = aux_info.bgs;
557 	int i, major_version = 0, minor_version = 0;
558 	char *base_file_line = NULL;
559 	size_t base_file_line_len = 0;
560 
561 	printf("[v%d.%d] Generating an Incremental EXT4 image\n",
562 			blk_file_major_ver, blk_file_minor_ver);
563 	if (base_fs_allocations == NULL)
564 		base_fs_allocations = create_allocation();
565 	fs_alloc = base_fs_allocations;
566 
567 	fscanf(base_alloc_file_in, blk_file_header_fmt, &major_version, &minor_version);
568 	if (major_version == 0) {
569 		critical_error("Invalid base file");
570 	}
571 
572 	if (major_version != blk_file_major_ver) {
573 		critical_error("Incompatible base file: version required is %d.X",
574 				blk_file_major_ver);
575 	}
576 
577 	if (minor_version < blk_file_minor_ver) {
578 		critical_error("Incompatible base file: version required is %d.%d or above",
579 				blk_file_major_ver, blk_file_minor_ver);
580 	}
581 
582 	while (getline(&base_file_line, &base_file_line_len, base_alloc_file_in) != -1) {
583 		if (sscanf(base_file_line, base_alloc_file_in_format, &stored_file_name, &file_map)
584 				!= base_file_format_param_count) {
585 			continue;
586 		}
587 		if (strlen(stored_file_name) < strlen(mountpoint)) {
588 			continue;
589 		}
590 		snprintf(real_file_name, MAX_PATH, "%s%s", directory, stored_file_name + strlen(mountpoint));
591 		if (!access(real_file_name, R_OK)) {
592 			char *block_range, *end_string;
593 			int real_file_fd;
594 			u32 start_block, end_block, block_file_size;
595 			u32 real_file_block_size;
596 
597 			real_file_fd = open(real_file_name, O_RDONLY);
598 			if (real_file_fd == -1) {
599 				critical_error(err_msg);
600 			}
601 			real_file_block_size = get_file_size(real_file_fd);
602 			close(real_file_fd);
603 			real_file_block_size = DIV_ROUND_UP(real_file_block_size, info.block_size);
604 			fs_alloc->filename = strdup(real_file_name);
605 			block_range = strtok_r(file_map, ",", &end_string);
606 			while (block_range && real_file_block_size) {
607 				int block_group;
608 				char *range, *end_token = NULL;
609 				range = strtok_r(block_range, "-", &end_token);
610 				if (!range) {
611 					critical_error(err_msg);
612 				}
613 				start_block = parse_num(range);
614 				range = strtok_r(NULL, "-", &end_token);
615 				if (!range) {
616 					end_block = start_block;
617 				} else {
618 					end_block = parse_num(range);
619 				}
620 				// Assummption is that allocations are within the same block group
621 				block_group = get_block_group(start_block);
622 				if (block_group != get_block_group(end_block)) {
623 					critical_error("base file allocation's end block is in a different "
624 								   "block group than start block. did you change fs params?");
625 				}
626 				block_range = strtok_r(NULL, ",", &end_string);
627 				int bg_first_block = bgs[block_group].first_block;
628 				int min_bg_bound = bgs[block_group].chunks[0].block + bgs[block_group].chunks[0].len;
629 				int max_bg_bound = bgs[block_group].chunks[bgs[block_group].chunk_count - 1].block;
630 
631 				if (min_bg_bound >= start_block - bg_first_block ||
632 					max_bg_bound <= end_block - bg_first_block) {
633 					continue;
634 				}
635 				block_file_size = end_block - start_block + 1;
636 				if (block_file_size > real_file_block_size) {
637 					block_file_size = real_file_block_size;
638 				}
639 				append_region(fs_alloc, start_block, block_file_size, block_group);
640 				reserve_bg_chunk(block_group, start_block - bgs[block_group].first_block, block_file_size);
641 				real_file_block_size -= block_file_size;
642 			}
643 			if (reserve_blocks_for_allocation(fs_alloc) < 0)
644 				critical_error("failed to reserve base fs allocation");
645 			fs_alloc->next = create_allocation();
646 			fs_alloc = fs_alloc->next;
647 		}
648 	}
649 
650 	for (i = 0; i < aux_info.groups; i++) {
651 		qsort(bgs[i].chunks, bgs[i].chunk_count, sizeof(struct region), compare_chunks);
652 	}
653 
654 	free(base_file_line);
655 
656 #else
657     return;
658 #endif
659 #undef err_msg
660 }
661 
generate_base_alloc_file_out(FILE * base_alloc_file_out,char * dir,char * mountpoint,struct block_allocation * p)662 void generate_base_alloc_file_out(FILE* base_alloc_file_out, char* dir, char* mountpoint,
663 								  struct block_allocation* p)
664 {
665 	size_t dirlen = dir ? strlen(dir) : 0;
666 	fprintf(base_alloc_file_out, blk_file_header_fmt, blk_file_major_ver, blk_file_minor_ver);
667 	fputc('\n', base_alloc_file_out);
668 	while (p) {
669 		if (dir && strncmp(p->filename, dir, dirlen) == 0) {
670 			// substitute mountpoint for the leading directory in the filename, in the output file
671 			fprintf(base_alloc_file_out, "%s%s", mountpoint, p->filename + dirlen);
672 		} else {
673 			fprintf(base_alloc_file_out, "%s", p->filename);
674 		}
675 		print_blocks(base_alloc_file_out, p, ',');
676 		struct block_allocation* pn = p->next;
677 		p = pn;
678 	}
679 }
680 
make_ext4fs_internal(int fd,const char * _directory,const char * _target_out_directory,const char * _mountpoint,fs_config_func_t fs_config_func,int gzip,int sparse,int crc,int wipe,int real_uuid,struct selabel_handle * sehnd,int verbose,time_t fixed_time,FILE * block_list_file,FILE * base_alloc_file_in,FILE * base_alloc_file_out)681 int make_ext4fs_internal(int fd, const char *_directory, const char *_target_out_directory,
682 						 const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
683 						 int sparse, int crc, int wipe, int real_uuid,
684 						 struct selabel_handle *sehnd, int verbose, time_t fixed_time,
685 						 FILE* block_list_file, FILE* base_alloc_file_in, FILE* base_alloc_file_out)
686 {
687 	u32 root_inode_num;
688 	u16 root_mode;
689 	char *mountpoint;
690 	char *directory = NULL;
691 	char *target_out_directory = NULL;
692 	struct block_allocation* p;
693 
694 	if (setjmp(setjmp_env))
695 		return EXIT_FAILURE; /* Handle a call to longjmp() */
696 
697 	info.block_device = is_block_device_fd(fd);
698 
699 	if (info.block_device && (sparse || gzip || crc)) {
700 		fprintf(stderr, "No sparse/gzip/crc allowed for block device\n");
701 		return EXIT_FAILURE;
702 	}
703 
704 	if (_mountpoint == NULL) {
705 		mountpoint = strdup("");
706 	} else {
707 		mountpoint = canonicalize_abs_slashes(_mountpoint);
708 	}
709 
710 	if (_directory) {
711 		directory = canonicalize_rel_slashes(_directory);
712 	}
713 
714 	if (_target_out_directory) {
715 		target_out_directory = canonicalize_rel_slashes(_target_out_directory);
716 	}
717 
718 	if (info.len <= 0)
719 		info.len = get_file_size(fd);
720 
721 	if (info.len <= 0) {
722 		fprintf(stderr, "Need size of filesystem\n");
723 		return EXIT_FAILURE;
724 	}
725 
726 	if (info.block_size <= 0)
727 		info.block_size = compute_block_size();
728 
729 	/* Round down the filesystem length to be a multiple of the block size */
730 	info.len &= ~((u64)info.block_size - 1);
731 
732 	if (info.journal_blocks == 0)
733 		info.journal_blocks = compute_journal_blocks();
734 
735 	if (info.no_journal == 0)
736 		info.feat_compat = EXT4_FEATURE_COMPAT_HAS_JOURNAL;
737 	else
738 		info.journal_blocks = 0;
739 
740 	if (info.blocks_per_group <= 0)
741 		info.blocks_per_group = compute_blocks_per_group();
742 
743 	if (info.inodes <= 0)
744 		info.inodes = compute_inodes();
745 
746 	if (info.inode_size <= 0)
747 		info.inode_size = 256;
748 
749 	if (info.label == NULL)
750 		info.label = "";
751 
752 	info.inodes_per_group = compute_inodes_per_group();
753 
754 	info.feat_compat |=
755 			EXT4_FEATURE_COMPAT_RESIZE_INODE |
756 			EXT4_FEATURE_COMPAT_EXT_ATTR;
757 
758 	info.feat_ro_compat |=
759 			EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
760 			EXT4_FEATURE_RO_COMPAT_LARGE_FILE |
761 			EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
762 
763 	info.feat_incompat |=
764 			EXT4_FEATURE_INCOMPAT_EXTENTS |
765 			EXT4_FEATURE_INCOMPAT_FILETYPE;
766 
767 
768 	info.bg_desc_reserve_blocks = compute_bg_desc_reserve_blocks();
769 
770 	printf("Creating filesystem with parameters:\n");
771 	printf("    Size: %"PRIu64"\n", info.len);
772 	printf("    Block size: %d\n", info.block_size);
773 	printf("    Blocks per group: %d\n", info.blocks_per_group);
774 	printf("    Inodes per group: %d\n", info.inodes_per_group);
775 	printf("    Inode size: %d\n", info.inode_size);
776 	printf("    Journal blocks: %d\n", info.journal_blocks);
777 	printf("    Label: %s\n", info.label);
778 
779 	ext4_create_fs_aux_info();
780 
781 	printf("    Blocks: %"PRIu64"\n", aux_info.len_blocks);
782 	printf("    Block groups: %d\n", aux_info.groups);
783 	printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
784 
785 	ext4_sparse_file = sparse_file_new(info.block_size, info.len);
786 
787 	block_allocator_init();
788 
789 	ext4_fill_in_sb(real_uuid);
790 
791 	if (base_alloc_file_in) {
792 		extract_base_fs_allocations(directory, mountpoint, base_alloc_file_in);
793 	}
794 	if (reserve_inodes(0, 10) == EXT4_ALLOCATE_FAILED)
795 		error("failed to reserve first 10 inodes");
796 
797 	if (info.feat_compat & EXT4_FEATURE_COMPAT_HAS_JOURNAL)
798 		ext4_create_journal_inode();
799 
800 	if (info.feat_compat & EXT4_FEATURE_COMPAT_RESIZE_INODE)
801 		ext4_create_resize_inode();
802 
803 #ifdef USE_MINGW
804 	// Windows needs only 'create an empty fs image' functionality
805 	assert(!directory);
806 	root_inode_num = build_default_directory_structure(mountpoint, sehnd);
807 #else
808 	if (directory)
809 		root_inode_num = build_directory_structure(directory, mountpoint, target_out_directory, 0,
810 			fs_config_func, sehnd, verbose, fixed_time);
811 	else
812 		root_inode_num = build_default_directory_structure(mountpoint, sehnd);
813 #endif
814 
815 	root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
816 	inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
817 
818 #ifndef USE_MINGW
819 	if (sehnd) {
820 		char *secontext = NULL;
821 
822 		if (selabel_lookup(sehnd, &secontext, mountpoint, S_IFDIR) < 0) {
823 			error("cannot lookup security context for %s", mountpoint);
824 		}
825 		if (secontext) {
826 			if (verbose) {
827 				printf("Labeling %s as %s\n", mountpoint, secontext);
828 			}
829 			inode_set_selinux(root_inode_num, secontext);
830 		}
831 		freecon(secontext);
832 	}
833 #endif
834 
835 	ext4_update_free();
836 
837 	// TODO: Consider migrating the OTA tools to the new base alloc file format
838 	// used for generating incremental images (see go/incremental-ext4)
839 	if (block_list_file) {
840 		size_t dirlen = directory ? strlen(directory) : 0;
841 		struct block_allocation* p = get_saved_allocation_chain();
842 		while (p) {
843 			if (directory && strncmp(p->filename, directory, dirlen) == 0) {
844 				// substitute mountpoint for the leading directory in the filename, in the output file
845 				fprintf(block_list_file, "%s%s", mountpoint, p->filename + dirlen);
846 			} else {
847 				fprintf(block_list_file, "%s", p->filename);
848 			}
849 			print_blocks(block_list_file, p, ' ');
850 			struct block_allocation* pn = p->next;
851 			p = pn;
852 		}
853 	}
854 
855 	if (base_alloc_file_out) {
856 		struct block_allocation* p = get_saved_allocation_chain();
857 		generate_base_alloc_file_out(base_alloc_file_out, directory, mountpoint, p);
858 	}
859 
860 	printf("Created filesystem with %d/%d inodes and %d/%d blocks\n",
861 			aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
862 			aux_info.sb->s_inodes_count,
863 			aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
864 			aux_info.sb->s_blocks_count_lo);
865 
866 	if (wipe && WIPE_IS_SUPPORTED) {
867 		wipe_block_device(fd, info.len);
868 	}
869 
870 	write_ext4_image(fd, gzip, sparse, crc);
871 
872 	sparse_file_destroy(ext4_sparse_file);
873 	ext4_sparse_file = NULL;
874 
875 	p = get_saved_allocation_chain();
876 	while (p) {
877 		struct block_allocation* pn = p->next;
878 		free_alloc(p);
879 		p = pn;
880 	}
881 
882 	free(mountpoint);
883 	free(directory);
884 
885 	return 0;
886 }
887