simg2img.c revision 33f96c66e9a1f2e266a75e5e84c091dffa6ef118
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17#include "ext4_utils.h"
18#include "output_file.h"
19#include "sparse_format.h"
20#include "sparse_crc32.h"
21
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <sys/types.h>
25#include <sys/mman.h>
26#include <unistd.h>
27#include <fcntl.h>
28#include <stdio.h>
29
30#if defined(__APPLE__) && defined(__MACH__)
31#define lseek64 lseek
32#define off64_t off_t
33#endif
34
35#define COPY_BUF_SIZE (1024*1024)
36u8 *copybuf;
37
38/* This will be malloc'ed with the size of blk_sz from the sparse file header */
39u8* zerobuf;
40
41#define SPARSE_HEADER_MAJOR_VER 1
42#define SPARSE_HEADER_LEN       (sizeof(sparse_header_t))
43#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
44
45void usage()
46{
47  fprintf(stderr, "Usage: simg2img <sparse_image_file> <raw_image_file>\n");
48}
49
50int process_raw_chunk(FILE *in, FILE *out, u32 blocks, u32 blk_sz, u32 *crc32)
51{
52	u64 len = (u64)blocks * blk_sz;
53	int chunk;
54
55	while (len) {
56		chunk = (len > COPY_BUF_SIZE) ? COPY_BUF_SIZE : len;
57		if (fread(copybuf, chunk, 1, in) != 1) {
58			fprintf(stderr, "fread returned an error copying a raw chunk\n");
59			exit(-1);
60		}
61		*crc32 = sparse_crc32(*crc32, copybuf, chunk);
62		if (fwrite(copybuf, chunk, 1, out) != 1) {
63			fprintf(stderr, "fwrite returned an error copying a raw chunk\n");
64			exit(-1);
65		}
66		len -= chunk;
67	}
68
69	return blocks;
70}
71
72
73int process_skip_chunk(FILE *out, u32 blocks, u32 blk_sz, u32 *crc32)
74{
75	/* len needs to be 64 bits, as the sparse file specifies the skip amount
76	 * as a 32 bit value of blocks.
77	 */
78	u64 len = (u64)blocks * blk_sz;
79	u64 len_save;
80	u32 skip_chunk;
81
82	/* Fseek takes the offset as a long, which may be 32 bits on some systems.
83	 * So, lets do a sequence of fseeks() with SEEK_CUR to get the file pointer
84	 * where we want it.
85	 */
86	len_save = len;
87	while (len) {
88		skip_chunk = (len > 0x80000000) ? 0x80000000 : len;
89		fseek(out, skip_chunk, SEEK_CUR);
90		len -= skip_chunk;
91	}
92	/* And compute the CRC of the skipped region a chunk at a time */
93	len = len_save;
94	while (len) {
95		skip_chunk = (skip_chunk > blk_sz) ? blk_sz : skip_chunk;
96		*crc32 = sparse_crc32(*crc32, zerobuf, skip_chunk);
97		len -= skip_chunk;
98	}
99
100	return blocks;
101}
102
103int main(int argc, char *argv[])
104{
105	FILE *in, *out;
106	unsigned int i;
107	sparse_header_t sparse_header;
108	chunk_header_t chunk_header;
109	u32 crc32 = 0;
110	u32 total_blocks = 0;
111
112	if (argc != 3) {
113		usage();
114		exit(-1);
115	}
116
117	if ( (copybuf = malloc(COPY_BUF_SIZE)) == 0) {
118		fprintf(stderr, "Cannot malloc copy buf\n");
119		exit(-1);
120	}
121
122	if ((in = fopen(argv[1], "rb")) == 0) {
123		fprintf(stderr, "Cannot open input file %s\n", argv[1]);
124		exit(-1);
125	}
126
127	if ((out = fopen(argv[2], "wb")) == 0) {
128		fprintf(stderr, "Cannot open output file %s\n", argv[2]);
129		exit(-1);
130	}
131
132	if (fread(&sparse_header, sizeof(sparse_header), 1, in) != 1) {
133		fprintf(stderr, "Error reading sparse file header\n");
134		exit(-1);
135	}
136
137	if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
138		fprintf(stderr, "Bad magic\n");
139		exit(-1);
140	}
141
142	if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
143		fprintf(stderr, "Unknown major version number\n");
144		exit(-1);
145	}
146
147	if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
148		/* Skip the remaining bytes in a header that is longer than
149		 * we expected.
150		 */
151		fseek(in, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR);
152	}
153
154	if ( (zerobuf = malloc(sparse_header.blk_sz)) == 0) {
155		fprintf(stderr, "Cannot malloc zero buf\n");
156		exit(-1);
157	}
158
159	for (i=0; i<sparse_header.total_chunks; i++) {
160		if (fread(&chunk_header, sizeof(chunk_header), 1, in) != 1) {
161			fprintf(stderr, "Error reading chunk header\n");
162			exit(-1);
163		}
164
165		if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
166			/* Skip the remaining bytes in a header that is longer than
167			 * we expected.
168			 */
169			fseek(in, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR);
170		}
171
172		switch (chunk_header.chunk_type) {
173		    case CHUNK_TYPE_RAW:
174			if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz +
175				 (chunk_header.chunk_sz * sparse_header.blk_sz)) ) {
176				fprintf(stderr, "Bogus chunk size for chunk %d, type Raw\n", i);
177				exit(-1);
178			}
179			total_blocks += process_raw_chunk(in, out,
180					 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
181			break;
182		    case CHUNK_TYPE_DONT_CARE:
183			if (chunk_header.total_sz != sparse_header.chunk_hdr_sz) {
184				fprintf(stderr, "Bogus chunk size for chunk %d, type Dont Care\n", i);
185				exit(-1);
186			}
187			total_blocks += process_skip_chunk(out,
188					 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
189			break;
190		    default:
191			fprintf(stderr, "Unknown chunk type 0x%4.4x\n", chunk_header.chunk_type);
192			exit(-1);
193		}
194
195	}
196
197	/* If the last chunk was a skip, then the code just did a seek, but
198	 * no write, and the file won't actually be the correct size.  This
199	 * will make the file the correct size.  Make sure the offset is
200	 * computed in 64 bits, and the function called can handle 64 bits.
201	 */
202	if (ftruncate(fileno(out), (u64)total_blocks * sparse_header.blk_sz)) {
203		fprintf(stderr, "Error calling ftruncate() to set the image size\n");
204		exit(-1);
205	}
206
207	fclose(in);
208	fclose(out);
209
210	if (sparse_header.total_blks != total_blocks) {
211		fprintf(stderr, "Wrote %d blocks, expected to write %d blocks\n",
212			 total_blocks, sparse_header.total_blks);
213		exit(-1);
214	}
215
216	if (sparse_header.image_checksum != crc32) {
217		fprintf(stderr, "computed crc32 of 0x%8.8x, expected 0x%8.8x\n",
218			 crc32, sparse_header.image_checksum);
219		exit(-1);
220	}
221
222	exit(0);
223}
224
225