1 /*
2 * Copyright (C) 2012 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 #define _FILE_OFFSET_BITS 64
18 #define _LARGEFILE64_SOURCE 1
19
20 #include <fcntl.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <sparse/sparse.h>
30
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
usage()40 void usage() {
41 fprintf(stderr, "Usage: img2simg [-s] <raw_image_file> <sparse_image_file> [<block_size>]\n");
42 }
43
main(int argc,char * argv[])44 int main(int argc, char* argv[]) {
45 char *arg_in;
46 char *arg_out;
47 enum sparse_read_mode mode = SPARSE_READ_MODE_NORMAL;
48 int extra;
49 int in;
50 int opt;
51 int out;
52 int ret;
53 struct sparse_file* s;
54 unsigned int block_size = 4096;
55 off64_t len;
56
57 while ((opt = getopt(argc, argv, "s")) != -1) {
58 switch (opt) {
59 case 's':
60 mode = SPARSE_READ_MODE_HOLE;
61 break;
62 default:
63 usage();
64 exit(EXIT_FAILURE);
65 }
66 }
67
68 extra = argc - optind;
69 if (extra < 2 || extra > 3) {
70 usage();
71 exit(EXIT_FAILURE);
72 }
73
74 if (extra == 3) {
75 block_size = atoi(argv[optind + 2]);
76 }
77
78 if (block_size < 1024 || block_size % 4 != 0) {
79 usage();
80 exit(EXIT_FAILURE);
81 }
82
83 arg_in = argv[optind];
84 if (strcmp(arg_in, "-") == 0) {
85 in = STDIN_FILENO;
86 } else {
87 in = open(arg_in, O_RDONLY | O_BINARY);
88 if (in < 0) {
89 fprintf(stderr, "Cannot open input file %s\n", arg_in);
90 exit(EXIT_FAILURE);
91 }
92 }
93
94 arg_out = argv[optind + 1];
95 if (strcmp(arg_out, "-") == 0) {
96 out = STDOUT_FILENO;
97 } else {
98 out = open(arg_out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
99 if (out < 0) {
100 fprintf(stderr, "Cannot open output file %s\n", arg_out);
101 exit(EXIT_FAILURE);
102 }
103 }
104
105 len = lseek64(in, 0, SEEK_END);
106 lseek64(in, 0, SEEK_SET);
107
108 s = sparse_file_new(block_size, len);
109 if (!s) {
110 fprintf(stderr, "Failed to create sparse file\n");
111 exit(EXIT_FAILURE);
112 }
113
114 sparse_file_verbose(s);
115 ret = sparse_file_read(s, in, mode, false);
116 if (ret) {
117 fprintf(stderr, "Failed to read file\n");
118 exit(EXIT_FAILURE);
119 }
120
121 ret = sparse_file_write(s, out, false, true, false);
122 if (ret) {
123 fprintf(stderr, "Failed to write sparse file\n");
124 exit(EXIT_FAILURE);
125 }
126
127 close(in);
128 close(out);
129
130 exit(EXIT_SUCCESS);
131 }
132