Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 1 | /* |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 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 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 17 | #define _FILE_OFFSET_BITS 64 |
| 18 | #define _LARGEFILE64_SOURCE 1 |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 19 | |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 20 | #include <fcntl.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 21 | #include <stdbool.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 25 | #include <sys/types.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 29 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 30 | #include <sparse/sparse.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 31 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 32 | #ifndef O_BINARY |
| 33 | #define O_BINARY 0 |
| 34 | #endif |
| 35 | |
| 36 | #if defined(__APPLE__) && defined(__MACH__) |
| 37 | #define lseek64 lseek |
| 38 | #define off64_t off_t |
| 39 | #endif |
| 40 | |
| 41 | void usage() |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 42 | { |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 43 | fprintf(stderr, "Usage: img2simg <raw_image_file> <sparse_image_file> [<block_size>]\n"); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int main(int argc, char *argv[]) |
| 47 | { |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 48 | int in; |
| 49 | int out; |
| 50 | unsigned int i; |
| 51 | int ret; |
| 52 | struct sparse_file *s; |
| 53 | unsigned int block_size = 4096; |
| 54 | off64_t len; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 55 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 56 | if (argc < 3 || argc > 4) { |
| 57 | usage(); |
| 58 | exit(-1); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 61 | if (argc == 4) { |
| 62 | block_size = atoi(argv[3]); |
| 63 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 64 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 65 | if (block_size < 1024 || block_size % 4 != 0) { |
| 66 | usage(); |
| 67 | exit(-1); |
| 68 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 69 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 70 | if (strcmp(argv[1], "-") == 0) { |
| 71 | in = STDIN_FILENO; |
| 72 | } else { |
| 73 | in = open(argv[1], O_RDONLY | O_BINARY); |
| 74 | if (in < 0) { |
| 75 | fprintf(stderr, "Cannot open input file %s\n", argv[1]); |
| 76 | exit(-1); |
| 77 | } |
| 78 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 79 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 80 | if (strcmp(argv[2], "-") == 0) { |
| 81 | out = STDOUT_FILENO; |
| 82 | } else { |
| 83 | out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); |
| 84 | if (out < 0) { |
| 85 | fprintf(stderr, "Cannot open output file %s\n", argv[2]); |
| 86 | exit(-1); |
| 87 | } |
| 88 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 89 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 90 | len = lseek64(in, 0, SEEK_END); |
| 91 | lseek64(in, 0, SEEK_SET); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 92 | |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 93 | s = sparse_file_new(block_size, len); |
| 94 | if (!s) { |
| 95 | fprintf(stderr, "Failed to create sparse file\n"); |
| 96 | exit(-1); |
| 97 | } |
| 98 | |
| 99 | sparse_file_verbose(s); |
| 100 | ret = sparse_file_read(s, in, false, false); |
| 101 | if (ret) { |
| 102 | fprintf(stderr, "Failed to read file\n"); |
| 103 | exit(-1); |
| 104 | } |
| 105 | |
| 106 | ret = sparse_file_write(s, out, false, true, false); |
| 107 | if (ret) { |
| 108 | fprintf(stderr, "Failed to write sparse file\n"); |
| 109 | exit(-1); |
| 110 | } |
| 111 | |
| 112 | close(in); |
| 113 | close(out); |
| 114 | |
| 115 | exit(0); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 116 | } |