blob: 51580f7069c6b4cb59ddb751710fe901475b3fba [file] [log] [blame]
Colin Cross28fa5bc2012-05-20 13:28:05 -07001/*
Colin Cross0c4c47f2012-04-25 19:02:58 -07002 * Copyright (C) 2012 The Android Open Source Project
Colin Cross28fa5bc2012-05-20 13:28:05 -07003 *
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 Cross0c4c47f2012-04-25 19:02:58 -070017#define _FILE_OFFSET_BITS 64
18#define _LARGEFILE64_SOURCE 1
Colin Cross28fa5bc2012-05-20 13:28:05 -070019
Colin Cross28fa5bc2012-05-20 13:28:05 -070020#include <fcntl.h>
Colin Cross0c4c47f2012-04-25 19:02:58 -070021#include <stdbool.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070025#include <sys/stat.h>
26#include <sys/types.h>
Colin Cross0c4c47f2012-04-25 19:02:58 -070027#include <unistd.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070028
Colin Cross0c4c47f2012-04-25 19:02:58 -070029#include <sparse/sparse.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070030
Colin Cross0c4c47f2012-04-25 19:02:58 -070031#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 Zhang7b444f02018-06-12 16:42:09 -070040void usage() {
Sean Anderson6150b002021-12-30 15:34:28 -050041 fprintf(stderr, "Usage: img2simg [-s] <raw_image_file> <sparse_image_file> [<block_size>]\n");
Colin Cross28fa5bc2012-05-20 13:28:05 -070042}
43
Jerry Zhang7b444f02018-06-12 16:42:09 -070044int main(int argc, char* argv[]) {
Sean Anderson6150b002021-12-30 15:34:28 -050045 char *arg_in;
46 char *arg_out;
47 enum sparse_read_mode mode = SPARSE_READ_MODE_NORMAL;
48 int extra;
Jerry Zhang7b444f02018-06-12 16:42:09 -070049 int in;
Sean Anderson6150b002021-12-30 15:34:28 -050050 int opt;
Jerry Zhang7b444f02018-06-12 16:42:09 -070051 int out;
52 int ret;
53 struct sparse_file* s;
54 unsigned int block_size = 4096;
55 off64_t len;
Colin Cross28fa5bc2012-05-20 13:28:05 -070056
Sean Anderson6150b002021-12-30 15:34:28 -050057 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(-1);
65 }
66 }
67
68 extra = argc - optind;
69 if (extra < 2 || extra > 3) {
Jerry Zhang7b444f02018-06-12 16:42:09 -070070 usage();
71 exit(-1);
72 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070073
Sean Anderson6150b002021-12-30 15:34:28 -050074 if (extra == 3) {
75 block_size = atoi(argv[optind + 2]);
Jerry Zhang7b444f02018-06-12 16:42:09 -070076 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070077
Jerry Zhang7b444f02018-06-12 16:42:09 -070078 if (block_size < 1024 || block_size % 4 != 0) {
79 usage();
80 exit(-1);
81 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070082
Sean Anderson6150b002021-12-30 15:34:28 -050083 arg_in = argv[optind];
84 if (strcmp(arg_in, "-") == 0) {
Jerry Zhang7b444f02018-06-12 16:42:09 -070085 in = STDIN_FILENO;
86 } else {
Sean Anderson6150b002021-12-30 15:34:28 -050087 in = open(arg_in, O_RDONLY | O_BINARY);
Jerry Zhang7b444f02018-06-12 16:42:09 -070088 if (in < 0) {
Sean Anderson6150b002021-12-30 15:34:28 -050089 fprintf(stderr, "Cannot open input file %s\n", arg_in);
Jerry Zhang7b444f02018-06-12 16:42:09 -070090 exit(-1);
91 }
92 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070093
Sean Anderson6150b002021-12-30 15:34:28 -050094 arg_out = argv[optind + 1];
95 if (strcmp(arg_out, "-") == 0) {
Jerry Zhang7b444f02018-06-12 16:42:09 -070096 out = STDOUT_FILENO;
97 } else {
Sean Anderson6150b002021-12-30 15:34:28 -050098 out = open(arg_out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
Jerry Zhang7b444f02018-06-12 16:42:09 -070099 if (out < 0) {
Sean Anderson6150b002021-12-30 15:34:28 -0500100 fprintf(stderr, "Cannot open output file %s\n", arg_out);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700101 exit(-1);
102 }
103 }
Colin Cross28fa5bc2012-05-20 13:28:05 -0700104
Jerry Zhang7b444f02018-06-12 16:42:09 -0700105 len = lseek64(in, 0, SEEK_END);
106 lseek64(in, 0, SEEK_SET);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700107
Jerry Zhang7b444f02018-06-12 16:42:09 -0700108 s = sparse_file_new(block_size, len);
109 if (!s) {
110 fprintf(stderr, "Failed to create sparse file\n");
111 exit(-1);
112 }
Colin Cross0c4c47f2012-04-25 19:02:58 -0700113
Jerry Zhang7b444f02018-06-12 16:42:09 -0700114 sparse_file_verbose(s);
Sean Anderson6150b002021-12-30 15:34:28 -0500115 ret = sparse_file_read(s, in, mode, false);
Jerry Zhang7b444f02018-06-12 16:42:09 -0700116 if (ret) {
117 fprintf(stderr, "Failed to read file\n");
118 exit(-1);
119 }
Colin Cross0c4c47f2012-04-25 19:02:58 -0700120
Jerry Zhang7b444f02018-06-12 16:42:09 -0700121 ret = sparse_file_write(s, out, false, true, false);
122 if (ret) {
123 fprintf(stderr, "Failed to write sparse file\n");
124 exit(-1);
125 }
Colin Cross0c4c47f2012-04-25 19:02:58 -0700126
Jerry Zhang7b444f02018-06-12 16:42:09 -0700127 close(in);
128 close(out);
Colin Cross0c4c47f2012-04-25 19:02:58 -0700129
Jerry Zhang7b444f02018-06-12 16:42:09 -0700130 exit(0);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700131}