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 #ifndef USE_MINGW /* O_BINARY is windows-specific flag */
35 extern struct fs_info info;
37 static int verbose = 0;
39 static void usage(char *path)
41 fprintf(stderr, "%s [ options ] <image or block device> <output image>\n", path);
42 fprintf(stderr, "\n");
43 fprintf(stderr, " -c include CRC block\n");
44 fprintf(stderr, " -v verbose output\n");
45 fprintf(stderr, " -z gzip output\n");
46 fprintf(stderr, " -S don't use sparse output format\n");
49 static int read_ext(int fd)
52 struct ext4_super_block sb;
55 ret = lseek64(fd, 1024, SEEK_SET);
57 critical_error_errno("failed to seek to superblock");
59 ret = read(fd, &sb, sizeof(sb));
61 critical_error_errno("failed to read superblock");
62 if (ret != sizeof(sb))
63 critical_error("failed to read all of superblock");
67 ret = lseek64(fd, info.len, SEEK_SET);
69 critical_error_errno("failed to seek to end of input image");
71 ret = lseek64(fd, info.block_size * (aux_info.first_data_block + 1), SEEK_SET);
73 critical_error_errno("failed to seek to block group descriptors");
75 ret = read(fd, aux_info.bg_desc, info.block_size * aux_info.bg_desc_blocks);
77 critical_error_errno("failed to read block group descriptors");
78 if (ret != (int)info.block_size * (int)aux_info.bg_desc_blocks)
79 critical_error("failed to read all of block group descriptors");
82 printf("Found filesystem with parameters:\n");
83 printf(" Size: %llu\n", info.len);
84 printf(" Block size: %d\n", info.block_size);
85 printf(" Blocks per group: %d\n", info.blocks_per_group);
86 printf(" Inodes per group: %d\n", info.inodes_per_group);
87 printf(" Inode size: %d\n", info.inode_size);
88 printf(" Label: %s\n", info.label);
89 printf(" Blocks: %llu\n", aux_info.len_blocks);
90 printf(" Block groups: %d\n", aux_info.groups);
91 printf(" Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
92 printf(" Used %d/%d inodes and %d/%d blocks\n",
93 aux_info.sb->s_inodes_count - aux_info.sb->s_free_inodes_count,
94 aux_info.sb->s_inodes_count,
95 aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
96 aux_info.sb->s_blocks_count_lo);
102 static int bitmap_get_bit(u8 *bitmap, u32 bit)
104 if (bitmap[bit / 8] & 1 << (bit % 8))
110 static int build_sparse_ext(int fd, const char *filename)
114 int start_contiguous_block;
118 block_bitmap = malloc(info.block_size);
120 critical_error("failed to allocate block bitmap");
122 if (aux_info.first_data_block > 0)
123 queue_data_file(filename, 0,
124 info.block_size * aux_info.first_data_block, 0);
126 for (i = 0; i < aux_info.groups; i++) {
127 u32 first_block = aux_info.first_data_block + i * info.blocks_per_group;
128 u32 last_block = min(info.blocks_per_group, aux_info.len_blocks - first_block);
130 ret = lseek64(fd, (u64)info.block_size * aux_info.bg_desc[i].bg_block_bitmap,
133 critical_error_errno("failed to seek to block group bitmap %d", i);
135 ret = read(fd, block_bitmap, info.block_size);
137 critical_error_errno("failed to read block group bitmap %d", i);
138 if (ret != (int)info.block_size)
139 critical_error("failed to read all of block group bitmap %d", i);
141 start_contiguous_block = -1;
142 for (block = 0; block < last_block; block++) {
143 if (start_contiguous_block >= 0) {
144 if (!bitmap_get_bit(block_bitmap, block)) {
145 u32 start_block = first_block + start_contiguous_block;
146 u32 len_blocks = block - start_contiguous_block;
148 queue_data_file(filename, (u64)info.block_size * start_block,
149 info.block_size * len_blocks, start_block);
150 start_contiguous_block = -1;
153 if (bitmap_get_bit(block_bitmap, block))
154 start_contiguous_block = block;
158 if (start_contiguous_block >= 0) {
159 u32 start_block = first_block + start_contiguous_block;
160 u32 len_blocks = last_block - start_contiguous_block;
161 queue_data_file(filename, (u64)info.block_size * start_block,
162 info.block_size * len_blocks, start_block);
169 int main(int argc, char **argv)
172 const char *in = NULL;
173 const char *out = NULL;
179 while ((opt = getopt(argc, argv, "cvzS")) != -1) {
196 if (optind >= argc) {
197 fprintf(stderr, "Expected image or block device after options\n");
204 if (optind >= argc) {
205 fprintf(stderr, "Expected output image after input image\n");
210 out = argv[optind++];
213 fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
218 infd = open(in, O_RDONLY);
221 critical_error_errno("failed to open input image");
225 build_sparse_ext(infd, in);
229 if (strcmp(out, "-")) {
230 outfd = open(out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
236 outfd = STDOUT_FILENO;
239 write_ext4_image(outfd, gzip, sparse, crc, 0);