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