2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ext4_utils.h"
18 #include "make_ext4fs.h"
19 #include "output_file.h"
20 #include "backed_block.h"
23 #include <sys/types.h>
25 #include <sys/types.h>
31 extern struct fs_info info;
33 static int verbose = 0;
35 static void usage(char *path)
37 fprintf(stderr, "%s [ options ] <image or block device> <output image>\n", path);
38 fprintf(stderr, "\n");
39 fprintf(stderr, " -c include CRC block\n");
40 fprintf(stderr, " -v verbose output\n");
41 fprintf(stderr, " -z gzip output\n");
42 fprintf(stderr, " -S don't use sparse output format\n");
45 static int read_ext(int fd)
48 struct ext4_super_block sb;
51 ret = lseek64(fd, 1024, SEEK_SET);
53 critical_error_errno("failed to seek to superblock");
55 ret = read(fd, &sb, sizeof(sb));
57 critical_error_errno("failed to read superblock");
58 if (ret != sizeof(sb))
59 critical_error("failed to read all of superblock");
63 ret = lseek64(fd, info.len, SEEK_SET);
65 critical_error_errno("failed to seek to end of input image");
67 ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
69 critical_error_errno("failed to seek to block group descriptors");
71 ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
73 critical_error_errno("failed to read block group descriptors");
74 if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
75 critical_error("failed to read all of block group descriptors");
78 printf("Found filesystem with parameters:\n");
79 printf(" Size: %llu\n", info.len);
80 printf(" Block size: %d\n", info.block_size);
81 printf(" Blocks per group: %d\n", info.blocks_per_group);
82 printf(" Inodes per group: %d\n", info.inodes_per_group);
83 printf(" Inode size: %d\n", info.inode_size);
84 printf(" Label: %s\n", info.label);
85 printf(" Blocks: %llu\n", aux_info.len_blocks);
86 printf(" Block groups: %d\n", aux_info.groups);
87 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
88 printf(" Used %d/%d inodes and %d/%d blocks\n",
89 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
90 aux_info.sb->s_inodes_count,
91 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
92 aux_info.sb->s_blocks_count_lo);
98 static int bitmap_get_bit(u8 *bitmap, u32 bit)
100 if (bitmap[bit / 8] & 1 << (bit % 8))
106 static int build_sparse_ext(int fd, const char *filename)
110 int start_contiguous_block;
114 block_bitmap = malloc(info.block_size);
116 critical_error("failed to allocate block bitmap");
118 if (aux_info.first_data_block > 0)
119 queue_data_file(filename, 0,
120 info.block_size * aux_info.first_data_block, 0);
122 for (i = 0; i < aux_info.groups; i++) {
123 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
124 u32 last_block = min(info.blocks_per_group, aux_info.len_blocks - first_block);
126 ret = lseek64(fd, (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap,
129 critical_error_errno("failed to seek to block group bitmap %d", i);
131 ret = read(fd, block_bitmap, info.block_size);
133 critical_error_errno("failed to read block group bitmap %d", i);
134 if (ret != (int)info.block_size)
135 critical_error("failed to read all of block group bitmap %d", i);
137 start_contiguous_block = -1;
138 for (block = 0; block < last_block; block++) {
139 if (start_contiguous_block >= 0) {
140 if (!bitmap_get_bit(block_bitmap, block)) {
141 u32 start_block = first_block + start_contiguous_block;
142 u32 len_blocks = block - start_contiguous_block;
144 queue_data_file(filename, (u64)info.block_size * start_block,
145 info.block_size * len_blocks, start_block);
146 start_contiguous_block = -1;
149 if (bitmap_get_bit(block_bitmap, block))
150 start_contiguous_block = block;
154 if (start_contiguous_block >= 0) {
155 u32 start_block = first_block + start_contiguous_block;
156 u32 len_blocks = last_block - start_contiguous_block;
157 queue_data_file(filename, (u64)info.block_size * start_block,
158 info.block_size * len_blocks, start_block);
165 int main(int argc, char **argv)
168 const char *in = NULL;
169 const char *out = NULL;
175 while ((opt = getopt(argc, argv, "cvzS")) != -1) {
192 if (optind >= argc) {
193 fprintf(stderr, "Expected image or block device after options\n");
200 if (optind >= argc) {
201 fprintf(stderr, "Expected output image after input image\n");
206 out = argv[optind++];
209 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
214 fd = open(in, O_RDONLY);
217 critical_error_errno("failed to open input image");
221 build_sparse_ext(fd, in);
225 write_ext4_image(out, gzip, sparse, crc);