ext4_utils: Add ext2simg

Change-Id: I6c0d71d737be588d03018efdaed378349caa4f7a
diff --git a/ext4_utils/Android.mk b/ext4_utils/Android.mk
index c7934a3..5377cde 100644
--- a/ext4_utils/Android.mk
+++ b/ext4_utils/Android.mk
@@ -64,6 +64,24 @@
 
 include $(CLEAR_VARS)
 
+LOCAL_SRC_FILES := ext2simg.c
+LOCAL_MODULE := ext2simg
+LOCAL_MODULE_TAGS := optional
+LOCAL_SHARED_LIBRARIES += libext4_utils libz
+
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES := ext2simg.c
+LOCAL_MODULE := ext2simg
+LOCAL_MODULE_TAGS := optional
+LOCAL_STATIC_LIBRARIES += libext4_utils libz
+
+include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+
 LOCAL_SRC_FILES := simg2img.c \
 	sparse_crc32.c
 LOCAL_MODULE := simg2img
diff --git a/ext4_utils/ext2simg.c b/ext4_utils/ext2simg.c
new file mode 100644
index 0000000..a18a06e
--- /dev/null
+++ b/ext4_utils/ext2simg.c
@@ -0,0 +1,228 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ext4_utils.h"
+#include "make_ext4fs.h"
+#include "output_file.h"
+#include "backed_block.h"
+#include "allocate.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <libgen.h>
+#include <unistd.h>
+
+extern struct fs_info info;
+
+static int verbose = 0;
+
+static void usage(char *path)
+{
+	fprintf(stderr, "%s [ options ] <image or block device> <output image>\n", path);
+	fprintf(stderr, "\n");
+	fprintf(stderr, "  -c include CRC block\n");
+	fprintf(stderr, "  -v verbose output\n");
+	fprintf(stderr, "  -z gzip output\n");
+	fprintf(stderr, "  -S don't use sparse output format\n");
+}
+
+static int read_ext(int fd)
+{
+	off64_t ret;
+	struct ext4_super_block sb;
+	unsigned int i;
+
+	ret = lseek64(fd, 1024, SEEK_SET);
+	if (ret < 0)
+		critical_error_errno("failed to seek to superblock");
+
+	ret = read(fd, &sb, sizeof(sb));
+	if (ret < 0)
+		critical_error_errno("failed to read superblock");
+	if (ret != sizeof(sb))
+		critical_error("failed to read all of superblock");
+
+	ext4_parse_sb(&sb);
+
+	ret = lseek64(fd, info.len, SEEK_SET);
+	if (ret < 0)
+		critical_error_errno("failed to seek to end of input image");
+
+	ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
+	if (ret < 0)
+		critical_error_errno("failed to seek to block group descriptors");
+
+	ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
+	if (ret < 0)
+		critical_error_errno("failed to read block group descriptors");
+	if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
+		critical_error("failed to read all of block group descriptors");
+
+	if (verbose) {
+		printf("Found filesystem with parameters:\n");
+		printf("    Size: %llu\n", info.len);
+		printf("    Block size: %d\n", info.block_size);
+		printf("    Blocks per group: %d\n", info.blocks_per_group);
+		printf("    Inodes per group: %d\n", info.inodes_per_group);
+		printf("    Inode size: %d\n", info.inode_size);
+		printf("    Label: %s\n", info.label);
+		printf("    Blocks: %llu\n", aux_info.len_blocks);
+		printf("    Block groups: %d\n", aux_info.groups);
+		printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
+		printf("    Used %d/%d inodes and %d/%d blocks\n",
+				aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
+				aux_info.sb->s_inodes_count,
+				aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
+				aux_info.sb->s_blocks_count_lo);
+	}
+
+	return 0;
+}
+
+static int bitmap_get_bit(u8 *bitmap, u32 bit)
+{
+	if (bitmap[bit / 8] & 1 << (bit % 8))
+		return 1;
+
+	return 0;
+}
+
+static int build_sparse_ext(int fd, const char *filename)
+{
+	unsigned int i;
+	unsigned int block;
+	int start_contiguous_block;
+	u8 *block_bitmap;
+	off64_t ret;
+
+	block_bitmap = malloc(info.block_size);
+	if (!block_bitmap)
+		critical_error("failed to allocate block bitmap");
+
+	if (aux_info.first_data_block > 0)
+		queue_data_file(filename, 0,
+				info.block_size * aux_info.first_data_block, 0);
+
+	for (i = 0; i < aux_info.groups; i++) {
+		u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
+		u32 last_block = min(info.blocks_per_group, aux_info.len_blocks - first_block);
+
+		ret = lseek64(fd, (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap,
+				SEEK_SET);
+		if (ret < 0)
+			critical_error_errno("failed to seek to block group bitmap %d", i);
+
+		ret = read(fd, block_bitmap, info.block_size);
+		if (ret < 0)
+			critical_error_errno("failed to read block group bitmap %d", i);
+		if (ret != (int)info.block_size)
+			critical_error("failed to read all of block group bitmap %d", i);
+
+		start_contiguous_block = -1;
+		for (block = 0; block < last_block; block++) {
+			if (start_contiguous_block >= 0) {
+				if (!bitmap_get_bit(block_bitmap, block)) {
+					u32 start_block = first_block + start_contiguous_block;
+					u32 len_blocks = block - start_contiguous_block;
+
+					queue_data_file(filename, (u64)info.block_size * start_block,
+							info.block_size * len_blocks, start_block);
+					start_contiguous_block = -1;
+				}
+			} else {
+				if (bitmap_get_bit(block_bitmap, block))
+					start_contiguous_block = block;
+			}
+		}
+
+		if (start_contiguous_block >= 0) {
+			u32 start_block = first_block + start_contiguous_block;
+			u32 len_blocks = last_block - start_contiguous_block;
+			queue_data_file(filename, (u64)info.block_size * start_block,
+					info.block_size * len_blocks, start_block);
+		}
+	}
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	int opt;
+	const char *in = NULL;
+	const char *out = NULL;
+	int gzip = 0;
+	int sparse = 1;
+	int fd;
+	int crc = 0;
+
+	while ((opt = getopt(argc, argv, "cvzS")) != -1) {
+		switch (opt) {
+		case 'c':
+			crc = 1;
+			break;
+		case 'v':
+			verbose = 1;
+			break;
+		case 'z':
+			gzip = 1;
+			break;
+		case 'S':
+			sparse = 0;
+			break;
+		}
+	}
+
+	if (optind >= argc) {
+		fprintf(stderr, "Expected image or block device after options\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	in = argv[optind++];
+
+	if (optind >= argc) {
+		fprintf(stderr, "Expected output image after input image\n");
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	out = argv[optind++];
+
+	if (optind < argc) {
+		fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
+		usage(argv[0]);
+		exit(EXIT_FAILURE);
+	}
+
+	fd = open(in, O_RDONLY);
+
+	if (fd < 0)
+		critical_error_errno("failed to open input image");
+
+	read_ext(fd);
+
+	build_sparse_ext(fd, in);
+
+	close(fd);
+
+	write_ext4_image(out, gzip, sparse, crc);
+
+	return 0;
+}
diff --git a/ext4_utils/ext4_utils.c b/ext4_utils/ext4_utils.c
index aac9c71..1804e4b 100644
--- a/ext4_utils/ext4_utils.c
+++ b/ext4_utils/ext4_utils.c
@@ -296,6 +296,37 @@
 	}
 }
 
+void ext4_parse_sb(struct ext4_super_block *sb)
+{
+	if (sb->s_magic != EXT4_SUPER_MAGIC)
+		error("superblock magic incorrect");
+
+	if (sb->s_state != EXT4_VALID_FS)
+		error("filesystem state not valid");
+
+	info.block_size = 1024 << sb->s_log_block_size;
+	info.blocks_per_group = sb->s_blocks_per_group;
+	info.inodes_per_group = sb->s_inodes_per_group;
+	info.inode_size = sb->s_inode_size;
+	info.inodes = sb->s_inodes_count;
+	info.feat_ro_compat = sb->s_feature_ro_compat;
+	info.feat_compat = sb->s_feature_compat;
+	info.feat_incompat = sb->s_feature_incompat;
+	info.bg_desc_reserve_blocks = sb->s_reserved_gdt_blocks;
+	info.label = sb->s_volume_name;
+
+	aux_info.len_blocks = ((u64)sb->s_blocks_count_hi << 32) +
+			sb->s_blocks_count_lo;
+	info.len = (u64)info.block_size * aux_info.len_blocks;
+
+	ext4_create_fs_aux_info();
+
+	memcpy(aux_info.sb, sb, sizeof(*sb));
+
+	if (aux_info.first_data_block != sb->s_first_data_block)
+		critical_error("first data block does not match");
+}
+
 void ext4_create_resize_inode()
 {
 	struct block_allocation *reserve_inode_alloc = create_allocation();
diff --git a/ext4_utils/ext4_utils.h b/ext4_utils/ext4_utils.h
index 6e6c1ec..eea40d5 100644
--- a/ext4_utils/ext4_utils.h
+++ b/ext4_utils/ext4_utils.h
@@ -48,9 +48,9 @@
 
 #define warn(fmt, args...) do { fprintf(stderr, "warning: %s: " fmt "\n", __func__, ## args); } while (0)
 #define error(fmt, args...) do { fprintf(stderr, "error: %s: " fmt "\n", __func__, ## args); if (!force) exit(EXIT_FAILURE); } while (0)
-#define error_errno(s) error(s ": %s", strerror(errno))
+#define error_errno(s, args...) error(s ": %s", ##args, strerror(errno))
 #define critical_error(fmt, args...) do { fprintf(stderr, "critical error: %s: " fmt "\n", __func__, ## args); exit(EXIT_FAILURE); } while (0)
-#define critical_error_errno(s) critical_error(s ": %s", strerror(errno))
+#define critical_error_errno(s, args...) critical_error(s ": %s", ##args, strerror(errno))
 
 #define EXT4_SUPER_MAGIC 0xEF53
 #define EXT4_JNL_BACKUP_BLOCKS 1
@@ -146,5 +146,6 @@
 void ext4_queue_sb(void);
 u64 get_file_size(const char *filename);
 u64 parse_num(const char *arg);
+void ext4_parse_sb(struct ext4_super_block *sb);
 
 #endif