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