blob: a0db36f4530e797fcf80fc618d171ccb7641bab6 [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 Cross0c4c47f2012-04-25 19:02:58 -070025#include <sys/types.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070026#include <sys/stat.h>
27#include <sys/types.h>
Colin Cross0c4c47f2012-04-25 19:02:58 -070028#include <unistd.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070029
Colin Cross0c4c47f2012-04-25 19:02:58 -070030#include <sparse/sparse.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070031
Colin Cross0c4c47f2012-04-25 19:02:58 -070032#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
41void usage()
Colin Cross28fa5bc2012-05-20 13:28:05 -070042{
Colin Cross0c4c47f2012-04-25 19:02:58 -070043 fprintf(stderr, "Usage: img2simg <raw_image_file> <sparse_image_file> [<block_size>]\n");
Colin Cross28fa5bc2012-05-20 13:28:05 -070044}
45
46int main(int argc, char *argv[])
47{
Colin Cross0c4c47f2012-04-25 19:02:58 -070048 int in;
49 int out;
Colin Cross0c4c47f2012-04-25 19:02:58 -070050 int ret;
51 struct sparse_file *s;
52 unsigned int block_size = 4096;
53 off64_t len;
Colin Cross28fa5bc2012-05-20 13:28:05 -070054
Colin Cross0c4c47f2012-04-25 19:02:58 -070055 if (argc < 3 || argc > 4) {
56 usage();
57 exit(-1);
Colin Cross28fa5bc2012-05-20 13:28:05 -070058 }
59
Colin Cross0c4c47f2012-04-25 19:02:58 -070060 if (argc == 4) {
61 block_size = atoi(argv[3]);
62 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070063
Colin Cross0c4c47f2012-04-25 19:02:58 -070064 if (block_size < 1024 || block_size % 4 != 0) {
65 usage();
66 exit(-1);
67 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070068
Colin Cross0c4c47f2012-04-25 19:02:58 -070069 if (strcmp(argv[1], "-") == 0) {
70 in = STDIN_FILENO;
71 } else {
72 in = open(argv[1], O_RDONLY | O_BINARY);
73 if (in < 0) {
74 fprintf(stderr, "Cannot open input file %s\n", argv[1]);
75 exit(-1);
76 }
77 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070078
Colin Cross0c4c47f2012-04-25 19:02:58 -070079 if (strcmp(argv[2], "-") == 0) {
80 out = STDOUT_FILENO;
81 } else {
82 out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
83 if (out < 0) {
84 fprintf(stderr, "Cannot open output file %s\n", argv[2]);
85 exit(-1);
86 }
87 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070088
Colin Cross0c4c47f2012-04-25 19:02:58 -070089 len = lseek64(in, 0, SEEK_END);
90 lseek64(in, 0, SEEK_SET);
Colin Cross28fa5bc2012-05-20 13:28:05 -070091
Colin Cross0c4c47f2012-04-25 19:02:58 -070092 s = sparse_file_new(block_size, len);
93 if (!s) {
94 fprintf(stderr, "Failed to create sparse file\n");
95 exit(-1);
96 }
97
98 sparse_file_verbose(s);
99 ret = sparse_file_read(s, in, false, false);
100 if (ret) {
101 fprintf(stderr, "Failed to read file\n");
102 exit(-1);
103 }
104
105 ret = sparse_file_write(s, out, false, true, false);
106 if (ret) {
107 fprintf(stderr, "Failed to write sparse file\n");
108 exit(-1);
109 }
110
111 close(in);
112 close(out);
113
114 exit(0);
Colin Cross28fa5bc2012-05-20 13:28:05 -0700115}