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.
16 #define _LARGEFILE64_SOURCE
17 #define _FILE_OFFSET_BITS 64
19 #include <sys/types.h>
21 #include <sys/types.h>
27 #include "ext4_utils.h"
28 #include "output_file.h"
29 #include "sparse_format.h"
31 #if defined(__APPLE__) && defined(__MACH__)
36 #define COPY_BUF_SIZE (1024*1024)
39 #define SPARSE_HEADER_MAJOR_VER 1
40 #define SPARSE_HEADER_LEN (sizeof(sparse_header_t))
41 #define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
45 fprintf(stderr, "Usage: simg2img <sparse_image_file> <raw_image_file>\n");
48 int process_raw_chunk(FILE *in, FILE *out, u32 blocks, u32 blk_sz, u32 *crc32)
50 u64 len = (u64)blocks * blk_sz;
54 chunk = (len > COPY_BUF_SIZE) ? COPY_BUF_SIZE : len;
55 fread(copybuf, chunk, 1, in);
56 fwrite(copybuf, chunk, 1, out);
64 int process_skip_chunk(FILE *out, u32 blocks, u32 blk_sz, u32 *crc32)
66 /* len needs to be 64 bits, as the sparse file specifies the skip amount
67 * as a 32 bit value of blocks.
69 u64 len = (u64)blocks * blk_sz;
72 /* Fseek takes the offset as a long, which may be 32 bits on some systems.
73 * So, lets do a sequence of fseeks() with SEEK_CUR to get the file pointer
77 skip_chunk = (len > 0x80000000) ? 0x80000000 : len;
78 fseek(out, skip_chunk, SEEK_CUR);
85 int main(int argc, char *argv[])
89 sparse_header_t sparse_header;
90 chunk_header_t chunk_header;
99 if ( (copybuf = malloc(COPY_BUF_SIZE)) == 0) {
100 fprintf(stderr, "Cannot malloc copy buf\n");
104 if ((in = fopen(argv[1], "rb")) == 0) {
105 fprintf(stderr, "Cannot open input file %s\n", argv[1]);
109 if ((out = fopen(argv[2], "wb")) == 0) {
110 fprintf(stderr, "Cannot open output file %s\n", argv[2]);
114 if (fread(&sparse_header, sizeof(sparse_header), 1, in) != 1) {
115 fprintf(stderr, "Error reading sparse file header\n");
119 if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
120 fprintf(stderr, "Bad magic\n");
124 if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
125 fprintf(stderr, "Unknown major version number\n");
129 if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
130 /* Skip the remaining bytes in a header that is longer than
133 fseek(in, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR);
136 for (i=0; i<sparse_header.total_chunks; i++) {
137 if (fread(&chunk_header, sizeof(chunk_header), 1, in) != 1) {
138 fprintf(stderr, "Error reading chunk header\n");
142 if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
143 /* Skip the remaining bytes in a header that is longer than
146 fseek(in, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR);
149 switch (chunk_header.chunk_type) {
151 if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz +
152 (chunk_header.chunk_sz * sparse_header.blk_sz)) ) {
153 fprintf(stderr, "Bogus chunk size for chunk %d, type Raw\n", i);
156 total_blocks += process_raw_chunk(in, out,
157 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
159 case CHUNK_TYPE_DONT_CARE:
160 if (chunk_header.total_sz != sparse_header.chunk_hdr_sz) {
161 fprintf(stderr, "Bogus chunk size for chunk %d, type Dont Care\n", i);
164 total_blocks += process_skip_chunk(out,
165 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
168 fprintf(stderr, "Unknown chunk type 0x%4.4x\n", chunk_header.chunk_type);
174 /* If the last chunk was a skip, then the code just did a seek, but
175 * no write, and the file won't actually be the correct size. This
176 * will make the file the correct size. Make sure the offset is
177 * computed in 64 bits, and the function called can handle 64 bits.
179 ftruncate(fileno(out), (u64)total_blocks * sparse_header.blk_sz);
184 if (sparse_header.total_blks != total_blocks) {
185 fprintf(stderr, "Wrote %d blocks, expected to write %d blocks\n",
186 total_blocks, sparse_header.total_blks);
190 if (sparse_header.image_checksum != crc32) {
191 fprintf(stderr, "computed crc32 of %d, expected %d\n",
192 crc32, sparse_header.image_checksum);