simg2img.c revision 757ace516d8e4350616b5fd10da0c982d3d5ec74
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 u32 skip_chunk; 80 81 /* Fseek takes the offset as a long, which may be 32 bits on some systems. 82 * So, lets do a sequence of fseeks() with SEEK_CUR to get the file pointer 83 * where we want it. 84 */ 85 while (len) { 86 skip_chunk = (len > 0x80000000) ? 0x80000000 : len; 87 fseek(out, skip_chunk, SEEK_CUR); 88 len -= skip_chunk; 89 } 90 91 return blocks; 92} 93 94int process_crc32_chunk(FILE *in, u32 crc32) 95{ 96 u32 file_crc32; 97 if (fread(&file_crc32, 4, 1, in) != 1) { 98 fprintf(stderr, "fread returned an error copying a crc32 chunk\n"); 99 exit(-1); 100 } 101 102 if (file_crc32 != crc32) { 103 fprintf(stderr, "computed crc32 of 0x%8.8x, expected 0x%8.8x\n", 104 crc32, file_crc32); 105 exit(-1); 106 } 107 108 return 0; 109} 110 111int main(int argc, char *argv[]) 112{ 113 FILE *in, *out; 114 unsigned int i; 115 sparse_header_t sparse_header; 116 chunk_header_t chunk_header; 117 u32 crc32 = 0; 118 u32 total_blocks = 0; 119 120 if (argc != 3) { 121 usage(); 122 exit(-1); 123 } 124 125 if ( (copybuf = malloc(COPY_BUF_SIZE)) == 0) { 126 fprintf(stderr, "Cannot malloc copy buf\n"); 127 exit(-1); 128 } 129 130 if ((in = fopen(argv[1], "rb")) == 0) { 131 fprintf(stderr, "Cannot open input file %s\n", argv[1]); 132 exit(-1); 133 } 134 135 if ((out = fopen(argv[2], "wb")) == 0) { 136 fprintf(stderr, "Cannot open output file %s\n", argv[2]); 137 exit(-1); 138 } 139 140 if (fread(&sparse_header, sizeof(sparse_header), 1, in) != 1) { 141 fprintf(stderr, "Error reading sparse file header\n"); 142 exit(-1); 143 } 144 145 if (sparse_header.magic != SPARSE_HEADER_MAGIC) { 146 fprintf(stderr, "Bad magic\n"); 147 exit(-1); 148 } 149 150 if (sparse_header.major_version != SPARSE_HEADER_MAJOR_VER) { 151 fprintf(stderr, "Unknown major version number\n"); 152 exit(-1); 153 } 154 155 if (sparse_header.file_hdr_sz > SPARSE_HEADER_LEN) { 156 /* Skip the remaining bytes in a header that is longer than 157 * we expected. 158 */ 159 fseek(in, sparse_header.file_hdr_sz - SPARSE_HEADER_LEN, SEEK_CUR); 160 } 161 162 if ( (zerobuf = malloc(sparse_header.blk_sz)) == 0) { 163 fprintf(stderr, "Cannot malloc zero buf\n"); 164 exit(-1); 165 } 166 167 for (i=0; i<sparse_header.total_chunks; i++) { 168 if (fread(&chunk_header, sizeof(chunk_header), 1, in) != 1) { 169 fprintf(stderr, "Error reading chunk header\n"); 170 exit(-1); 171 } 172 173 if (sparse_header.chunk_hdr_sz > CHUNK_HEADER_LEN) { 174 /* Skip the remaining bytes in a header that is longer than 175 * we expected. 176 */ 177 fseek(in, sparse_header.chunk_hdr_sz - CHUNK_HEADER_LEN, SEEK_CUR); 178 } 179 180 switch (chunk_header.chunk_type) { 181 case CHUNK_TYPE_RAW: 182 if (chunk_header.total_sz != (sparse_header.chunk_hdr_sz + 183 (chunk_header.chunk_sz * sparse_header.blk_sz)) ) { 184 fprintf(stderr, "Bogus chunk size for chunk %d, type Raw\n", i); 185 exit(-1); 186 } 187 total_blocks += process_raw_chunk(in, out, 188 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32); 189 break; 190 case CHUNK_TYPE_DONT_CARE: 191 if (chunk_header.total_sz != sparse_header.chunk_hdr_sz) { 192 fprintf(stderr, "Bogus chunk size for chunk %d, type Dont Care\n", i); 193 exit(-1); 194 } 195 total_blocks += process_skip_chunk(out, 196 chunk_header.chunk_sz, sparse_header.blk_sz, &crc32); 197 break; 198 case CHUNK_TYPE_CRC32: 199 process_crc32_chunk(in, crc32); 200 break; 201 default: 202 fprintf(stderr, "Unknown chunk type 0x%4.4x\n", chunk_header.chunk_type); 203 exit(-1); 204 } 205 206 } 207 208 /* If the last chunk was a skip, then the code just did a seek, but 209 * no write, and the file won't actually be the correct size. This 210 * will make the file the correct size. Make sure the offset is 211 * computed in 64 bits, and the function called can handle 64 bits. 212 */ 213 if (ftruncate(fileno(out), (u64)total_blocks * sparse_header.blk_sz)) { 214 fprintf(stderr, "Error calling ftruncate() to set the image size\n"); 215 exit(-1); 216 } 217 218 fclose(in); 219 fclose(out); 220 221 if (sparse_header.total_blks != total_blocks) { 222 fprintf(stderr, "Wrote %d blocks, expected to write %d blocks\n", 223 total_blocks, sparse_header.total_blks); 224 exit(-1); 225 } 226 227 exit(0); 228} 229 230