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 "sparse_format.h"
19 #include "sparse_crc32.h"
21 #include <sys/types.h>
23 #include <sys/types.h>
29 #define COPY_BUF_SIZE (1024*1024)
32 /* This will be malloc'ed with the size of blk_sz from the sparse file header */
35 #define SPARSE_HEADER_MAJOR_VER 1
36 #define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
37 #define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
41 fprintf(stderr, "Usage: simg2img <sparse_image_file> <raw_image_file>\n");
44 static int read_all(int fd, void *buf, size_t len)
51 ret = read(fd, ptr, len - total);
66 static int write_all(int fd, void *buf, size_t len)
73 ret = write(fd, ptr, len - total);
88 int process_raw_chunk(int in, int out, u32 blocks, u32 blk_sz, u32 *crc32)
90 u64 len = (u64)blocks * blk_sz;
95 chunk = (len > COPY_BUF_SIZE) ? COPY_BUF_SIZE : len;
96 ret = read_all(in, copybuf, chunk);
98 fprintf(stderr, "read returned an error copying a raw chunk: %d %d\n",
102 *crc32 = sparse_crc32(*crc32, copybuf, chunk);
103 ret = write_all(out, copybuf, chunk);
105 fprintf(stderr, "write returned an error copying a raw chunk\n");
115 int process_skip_chunk(int out, u32 blocks, u32 blk_sz, u32 *crc32)
117 /* len needs to be 64 bits, as the sparse file specifies the skip amount
118 * as a 32 bit value of blocks.
120 u64 len = (u64)blocks * blk_sz;
122 lseek64(out, len, SEEK_CUR);
127 int process_crc32_chunk(int in, u32 crc32)
132 ret = read_all(in, &file_crc32, 4);
134 fprintf(stderr, "read returned an error copying a crc32 chunk\n");
138 if (file_crc32 != crc32) {
139 fprintf(stderr, "computed crc32 of 0x%8.8x, expected 0x%8.8x\n",
147 int main(int argc, char *argv[])
152 sparse_header_t sparse_header;
153 chunk_header_t chunk_header;
155 u32 total_blocks = 0;
163 if ( (copybuf = malloc(COPY_BUF_SIZE)) == 0) {
164 fprintf(stderr, "Cannot malloc copy buf\n");
168 if (strcmp(argv[1], "-") == 0) {
171 if ((in = open(argv[1], O_RDONLY)) == 0) {
172 fprintf(stderr, "Cannot open input file %s\n", argv[1]);
177 if (strcmp(argv[2], "-") == 0) {
180 if ((out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC)) == 0) {
181 fprintf(stderr, "Cannot open output file %s\n", argv[2]);
186 ret = read_all(in, &sparse_header, sizeof(sparse_header));
187 if (ret != sizeof(sparse_header)) {
188 fprintf(stderr, "Error reading sparse file header\n");
192 if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
193 fprintf(stderr, "Bad magic\n");
197 if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
198 fprintf(stderr, "Unknown major version number\n");
202 if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
203 /* Skip the remaining bytes in a header that is longer than
206 lseek64(in, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR);
209 if ( (zerobuf = malloc(sparse_header.blk_sz)) == 0) {
210 fprintf(stderr, "Cannot malloc zero buf\n");
214 for (i=0; i<sparse_header.total_chunks; i++) {
215 ret = read_all(in, &chunk_header, sizeof(chunk_header));
216 if (ret != sizeof(chunk_header)) {
217 fprintf(stderr, "Error reading chunk header\n");
221 if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
222 /* Skip the remaining bytes in a header that is longer than
225 lseek64(in, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR);
228 switch (chunk_header.chunk_type) {
230 if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz +
231 (chunk_header.chunk_sz * sparse_header.blk_sz)) ) {
232 fprintf(stderr, "Bogus chunk size for chunk %d, type Raw\n", i);
235 total_blocks += process_raw_chunk(in, out,
236 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
238 case CHUNK_TYPE_DONT_CARE:
239 if (chunk_header.total_sz != sparse_header.chunk_hdr_sz) {
240 fprintf(stderr, "Bogus chunk size for chunk %d, type Dont Care\n", i);
243 total_blocks += process_skip_chunk(out,
244 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
246 case CHUNK_TYPE_CRC32:
247 process_crc32_chunk(in, crc32);
250 fprintf(stderr, "Unknown chunk type 0x%4.4x\n", chunk_header.chunk_type);
255 /* If the last chunk was a skip, then the code just did a seek, but
256 * no write, and the file won't actually be the correct size. This
257 * will make the file the correct size. Make sure the offset is
258 * computed in 64 bits, and the function called can handle 64 bits.
260 if (ftruncate64(out, (u64)total_blocks * sparse_header.blk_sz)) {
261 fprintf(stderr, "Error calling ftruncate() to set the image size\n");
268 if (sparse_header.total_blks != total_blocks) {
269 fprintf(stderr, "Wrote %d blocks, expected to write %d blocks\n",
270 total_blocks, sparse_header.total_blks);