blob: 95e9b5bea8fd59da8b4d1200fd7935742c64e2cc [file] [log] [blame]
Colin Cross28fa5bc2012-05-20 13:28:05 -07001/*
2 * Copyright (C) 2010 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
Colin Cross0c4c47f2012-04-25 19:02:58 -070017#include <sparse/sparse.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070018
19#include <fcntl.h>
Colin Cross0c4c47f2012-04-25 19:02:58 -070020#include <stdbool.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <sys/types.h>
Colin Cross28fa5bc2012-05-20 13:28:05 -070027#include <unistd.h>
28
Colin Crossbdc6d392012-05-02 15:18:22 -070029#ifndef O_BINARY
30#define O_BINARY 0
31#endif
32
Colin Cross28fa5bc2012-05-20 13:28:05 -070033void usage()
34{
Colin Crossbdc6d392012-05-02 15:18:22 -070035 fprintf(stderr, "Usage: simg2img <sparse_image_files> <raw_image_file>\n");
Colin Cross28fa5bc2012-05-20 13:28:05 -070036}
37
Colin Cross28fa5bc2012-05-20 13:28:05 -070038int main(int argc, char *argv[])
39{
40 int in;
41 int out;
Colin Crossbdc6d392012-05-02 15:18:22 -070042 int i;
Colin Cross28fa5bc2012-05-20 13:28:05 -070043 int ret;
Colin Cross0c4c47f2012-04-25 19:02:58 -070044 struct sparse_file *s;
Colin Cross28fa5bc2012-05-20 13:28:05 -070045
Colin Crossbdc6d392012-05-02 15:18:22 -070046 if (argc < 3) {
Colin Cross28fa5bc2012-05-20 13:28:05 -070047 usage();
48 exit(-1);
49 }
50
Colin Crossbdc6d392012-05-02 15:18:22 -070051 out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
52 if (out < 0) {
53 fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]);
Colin Cross28fa5bc2012-05-20 13:28:05 -070054 exit(-1);
55 }
Colin Cross28fa5bc2012-05-20 13:28:05 -070056
Colin Crossbdc6d392012-05-02 15:18:22 -070057 for (i = 1; i < argc - 1; i++) {
58 if (strcmp(argv[i], "-") == 0) {
59 in = STDIN_FILENO;
60 } else {
61 in = open(argv[i], O_RDONLY | O_BINARY);
62 if (in < 0) {
63 fprintf(stderr, "Cannot open input file %s\n", argv[i]);
64 exit(-1);
65 }
66 }
67
68 s = sparse_file_import(in, true, false);
69 if (!s) {
70 fprintf(stderr, "Failed to read sparse file\n");
71 exit(-1);
72 }
73
74 lseek(out, SEEK_SET, 0);
75
76 ret = sparse_file_write(s, out, false, false, false);
77 if (ret < 0) {
78 fprintf(stderr, "Cannot write output file\n");
79 exit(-1);
80 }
81 sparse_file_destroy(s);
82 close(in);
83 }
84
Colin Cross28fa5bc2012-05-20 13:28:05 -070085 close(out);
86
Colin Cross28fa5bc2012-05-20 13:28:05 -070087 exit(0);
88}
89