128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross/*
228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Copyright (C) 2010 The Android Open Source Project
328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Licensed under the Apache License, Version 2.0 (the "License");
528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * you may not use this file except in compliance with the License.
628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * You may obtain a copy of the License at
728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *      http://www.apache.org/licenses/LICENSE-2.0
928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross *
1028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * Unless required by applicable law or agreed to in writing, software
1128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * distributed under the License is distributed on an "AS IS" BASIS,
1228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * See the License for the specific language governing permissions and
1428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross * limitations under the License.
1528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross */
1628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
170c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross#include <sparse/sparse.h>
1828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
1928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <fcntl.h>
200c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross#include <stdbool.h>
2128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <stdio.h>
2228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <stdlib.h>
2328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <string.h>
2428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <sys/types.h>
2528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <sys/stat.h>
2628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <sys/types.h>
2728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross#include <unistd.h>
2828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
29bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross#ifndef O_BINARY
30bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross#define O_BINARY 0
31bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross#endif
32bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross
3328fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossvoid usage()
3428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross{
35bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross  fprintf(stderr, "Usage: simg2img <sparse_image_files> <raw_image_file>\n");
3628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross}
3728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
3828fa5bc347390480fe190294c6c385b6a9f0d68bColin Crossint main(int argc, char *argv[])
3928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross{
4028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	int in;
4128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	int out;
42bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross	int i;
4328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	int ret;
440c4c47f88dfc15cada154a1cf9b4db88b49890f0Colin Cross	struct sparse_file *s;
4528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
46bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross	if (argc < 3) {
4728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		usage();
4828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		exit(-1);
4928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	}
5028fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
51bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross	out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
52bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross	if (out < 0) {
53bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]);
54bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		exit(-1);
5528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	}
5628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
57bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross	for (i = 1; i < argc - 1; i++) {
58bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		if (strcmp(argv[i], "-") == 0) {
59bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			in = STDIN_FILENO;
60bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		} else {
61bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			in = open(argv[i], O_RDONLY | O_BINARY);
62bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			if (in < 0) {
63bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross				fprintf(stderr, "Cannot open input file %s\n", argv[i]);
64bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross				exit(-1);
65bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			}
66bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		}
67bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross
68bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		s = sparse_file_import(in, true, false);
69bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		if (!s) {
70bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			fprintf(stderr, "Failed to read sparse file\n");
7128fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross			exit(-1);
7228fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross		}
7328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
74bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		lseek(out, SEEK_SET, 0);
75bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross
76bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		ret = sparse_file_write(s, out, false, false, false);
77bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		if (ret < 0) {
78bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			fprintf(stderr, "Cannot write output file\n");
79bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross			exit(-1);
80bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		}
81bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		sparse_file_destroy(s);
82bdc6d39ed6c09199a5d806f29b71b44cbb27c5c2Colin Cross		close(in);
8328fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	}
8428fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
8528fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	close(out);
8628fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
8728fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross	exit(0);
8828fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross}
8928fa5bc347390480fe190294c6c385b6a9f0d68bColin Cross
90