simg2img.c revision f27036726a039f516f59df79351c2717b0fb4aad
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 "sparse_format.h"
19#include "sparse_crc32.h"
20
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/mman.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <stdio.h>
28
29#if defined(__APPLE__) && defined(__MACH__)
30#define lseek64 lseek
31#define off64_t off_t
32#endif
33
34#define COPY_BUF_SIZE (1024*1024)
35u8 *copybuf;
36
37/* This will be malloc'ed with the size of blk_sz from the sparse file header */
38u8* zerobuf;
39
40#define SPARSE_HEADER_MAJOR_VER 1
41#define SPARSE_HEADER_LEN       (sizeof(sparse_header_t))
42#define CHUNK_HEADER_LEN (sizeof(chunk_header_t))
43
44void usage()
45{
46  fprintf(stderr, "Usage: simg2img <sparse_image_file> <raw_image_file>\n");
47}
48
49static int read_all(int fd, void *buf, size_t len)
50{
51	size_t total = 0;
52	int ret;
53	char *ptr = buf;
54
55	while (total < len) {
56		ret = read(fd, ptr, len - total);
57
58		if (ret < 0)
59			return ret;
60
61		if (ret == 0)
62			return total;
63
64		ptr += ret;
65		total += ret;
66	}
67
68	return total;
69}
70
71static int write_all(int fd, void *buf, size_t len)
72{
73	size_t total = 0;
74	int ret;
75	char *ptr = buf;
76
77	while (total < len) {
78		ret = write(fd, ptr, len - total);
79
80		if (ret < 0)
81			return ret;
82
83		if (ret == 0)
84			return total;
85
86		ptr += ret;
87		total += ret;
88	}
89
90	return total;
91}
92
93int process_raw_chunk(int in, int out, u32 blocks, u32 blk_sz, u32 *crc32)
94{
95	u64 len = (u64)blocks * blk_sz;
96	int ret;
97	int chunk;
98
99	while (len) {
100		chunk = (len > COPY_BUF_SIZE) ? COPY_BUF_SIZE : len;
101		ret = read_all(in, copybuf, chunk);
102		if (ret != chunk) {
103			fprintf(stderr, "read returned an error copying a raw chunk: %d %d\n",
104					ret, chunk);
105			exit(-1);
106		}
107		*crc32 = sparse_crc32(*crc32, copybuf, chunk);
108		ret = write_all(out, copybuf, chunk);
109		if (ret != chunk) {
110			fprintf(stderr, "write returned an error copying a raw chunk\n");
111			exit(-1);
112		}
113		len -= chunk;
114	}
115
116	return blocks;
117}
118
119
120int process_skip_chunk(int out, u32 blocks, u32 blk_sz, u32 *crc32)
121{
122	/* len needs to be 64 bits, as the sparse file specifies the skip amount
123	 * as a 32 bit value of blocks.
124	 */
125	u64 len = (u64)blocks * blk_sz;
126
127	lseek64(out, len, SEEK_CUR);
128
129	return blocks;
130}
131
132int process_crc32_chunk(int in, u32 crc32)
133{
134	u32 file_crc32;
135	int ret;
136
137	ret = read_all(in, &file_crc32, 4);
138	if (ret != 4) {
139		fprintf(stderr, "read returned an error copying a crc32 chunk\n");
140		exit(-1);
141	}
142
143	if (file_crc32 != crc32) {
144		fprintf(stderr, "computed crc32 of 0x%8.8x, expected 0x%8.8x\n",
145			 crc32, file_crc32);
146		exit(-1);
147	}
148
149	return 0;
150}
151
152int main(int argc, char *argv[])
153{
154	int in;
155	int out;
156	unsigned int i;
157	sparse_header_t sparse_header;
158	chunk_header_t chunk_header;
159	u32 crc32 = 0;
160	u32 total_blocks = 0;
161	int ret;
162
163	if (argc != 3) {
164		usage();
165		exit(-1);
166	}
167
168	if ( (copybuf = malloc(COPY_BUF_SIZE)) == 0) {
169		fprintf(stderr, "Cannot malloc copy buf\n");
170		exit(-1);
171	}
172
173	if (strcmp(argv[1], "-") == 0) {
174		in = STDIN_FILENO;
175	} else {
176		if ((in = open(argv[1], O_RDONLY)) == 0) {
177			fprintf(stderr, "Cannot open input file %s\n", argv[1]);
178			exit(-1);
179		}
180	}
181
182	if (strcmp(argv[2], "-") == 0) {
183		out = STDOUT_FILENO;
184	} else {
185		if ((out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC)) == 0) {
186			fprintf(stderr, "Cannot open output file %s\n", argv[2]);
187			exit(-1);
188		}
189	}
190
191	ret = read_all(in, &sparse_header, sizeof(sparse_header));
192	if (ret != sizeof(sparse_header)) {
193		fprintf(stderr, "Error reading sparse file header\n");
194		exit(-1);
195	}
196
197	if (sparse_header.magic != SPARSE_HEADER_MAGIC) {
198		fprintf(stderr, "Bad magic\n");
199		exit(-1);
200	}
201
202	if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) {
203		fprintf(stderr, "Unknown major version number\n");
204		exit(-1);
205	}
206
207	if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) {
208		/* Skip the remaining bytes in a header that is longer than
209		 * we expected.
210		 */
211		lseek64(in, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR);
212	}
213
214	if ( (zerobuf = malloc(sparse_header.blk_sz)) == 0) {
215		fprintf(stderr, "Cannot malloc zero buf\n");
216		exit(-1);
217	}
218
219	for (i=0; i<sparse_header.total_chunks; i++) {
220		ret = read_all(in, &chunk_header, sizeof(chunk_header));
221		if (ret != sizeof(chunk_header)) {
222			fprintf(stderr, "Error reading chunk header\n");
223			exit(-1);
224		}
225
226		if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) {
227			/* Skip the remaining bytes in a header that is longer than
228			 * we expected.
229			 */
230			lseek64(in, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR);
231		}
232
233		switch (chunk_header.chunk_type) {
234		    case CHUNK_TYPE_RAW:
235			if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz +
236				 (chunk_header.chunk_sz * sparse_header.blk_sz)) ) {
237				fprintf(stderr, "Bogus chunk size for chunk %d, type Raw\n", i);
238				exit(-1);
239			}
240			total_blocks += process_raw_chunk(in, out,
241					 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
242			break;
243		    case CHUNK_TYPE_DONT_CARE:
244			if (chunk_header.total_sz != sparse_header.chunk_hdr_sz) {
245				fprintf(stderr, "Bogus chunk size for chunk %d, type Dont Care\n", i);
246				exit(-1);
247			}
248			total_blocks += process_skip_chunk(out,
249					 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32);
250			break;
251		    case CHUNK_TYPE_CRC32:
252			process_crc32_chunk(in, crc32);
253			break;
254		    default:
255			fprintf(stderr, "Unknown chunk type 0x%4.4x\n", chunk_header.chunk_type);
256		}
257
258	}
259
260	/* If the last chunk was a skip, then the code just did a seek, but
261	 * no write, and the file won't actually be the correct size.  This
262	 * will make the file the correct size.  Make sure the offset is
263	 * computed in 64 bits, and the function called can handle 64 bits.
264	 */
265	if (ftruncate64(out, (u64)total_blocks * sparse_header.blk_sz)) {
266		fprintf(stderr, "Error calling ftruncate() to set the image size\n");
267		exit(-1);
268	}
269
270	close(in);
271	close(out);
272
273	if (sparse_header.total_blks != total_blocks) {
274		fprintf(stderr, "Wrote %d blocks, expected to write %d blocks\n",
275			 total_blocks, sparse_header.total_blks);
276		exit(-1);
277	}
278
279	exit(0);
280}
281
282