2012-05-20 22:28:05 +02:00
|
|
|
/*
|
2012-04-26 04:02:58 +02:00
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
2012-05-20 22:28:05 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2012-04-26 04:02:58 +02:00
|
|
|
#define _FILE_OFFSET_BITS 64
|
|
|
|
#define _LARGEFILE64_SOURCE 1
|
2012-05-20 22:28:05 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
2012-04-26 04:02:58 +02:00
|
|
|
#include <stdbool.h>
|
2012-05-20 22:28:05 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2012-04-26 04:02:58 +02:00
|
|
|
#include <unistd.h>
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-26 04:02:58 +02:00
|
|
|
#include <sparse/sparse.h>
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-26 04:02:58 +02:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2012-04-26 04:02:58 +02:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#define lseek64 lseek
|
|
|
|
#define off64_t off_t
|
|
|
|
#endif
|
2012-05-20 22:28:05 +02:00
|
|
|
|
2018-06-13 01:42:09 +02:00
|
|
|
void usage() {
|
2021-12-30 21:34:28 +01:00
|
|
|
fprintf(stderr, "Usage: img2simg [-s] <raw_image_file> <sparse_image_file> [<block_size>]\n");
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|
|
|
|
|
2018-06-13 01:42:09 +02:00
|
|
|
int main(int argc, char* argv[]) {
|
2021-12-30 21:34:28 +01:00
|
|
|
char *arg_in;
|
|
|
|
char *arg_out;
|
|
|
|
enum sparse_read_mode mode = SPARSE_READ_MODE_NORMAL;
|
|
|
|
int extra;
|
2018-06-13 01:42:09 +02:00
|
|
|
int in;
|
2021-12-30 21:34:28 +01:00
|
|
|
int opt;
|
2018-06-13 01:42:09 +02:00
|
|
|
int out;
|
|
|
|
int ret;
|
|
|
|
struct sparse_file* s;
|
|
|
|
unsigned int block_size = 4096;
|
|
|
|
off64_t len;
|
|
|
|
|
2021-12-30 21:34:28 +01:00
|
|
|
while ((opt = getopt(argc, argv, "s")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 's':
|
|
|
|
mode = SPARSE_READ_MODE_HOLE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2021-12-30 21:34:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extra = argc - optind;
|
|
|
|
if (extra < 2 || extra > 3) {
|
2018-06-13 01:42:09 +02:00
|
|
|
usage();
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 21:34:28 +01:00
|
|
|
if (extra == 3) {
|
|
|
|
block_size = atoi(argv[optind + 2]);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (block_size < 1024 || block_size % 4 != 0) {
|
|
|
|
usage();
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
|
2021-12-30 21:34:28 +01:00
|
|
|
arg_in = argv[optind];
|
|
|
|
if (strcmp(arg_in, "-") == 0) {
|
2018-06-13 01:42:09 +02:00
|
|
|
in = STDIN_FILENO;
|
|
|
|
} else {
|
2021-12-30 21:34:28 +01:00
|
|
|
in = open(arg_in, O_RDONLY | O_BINARY);
|
2018-06-13 01:42:09 +02:00
|
|
|
if (in < 0) {
|
2021-12-30 21:34:28 +01:00
|
|
|
fprintf(stderr, "Cannot open input file %s\n", arg_in);
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 21:34:28 +01:00
|
|
|
arg_out = argv[optind + 1];
|
|
|
|
if (strcmp(arg_out, "-") == 0) {
|
2018-06-13 01:42:09 +02:00
|
|
|
out = STDOUT_FILENO;
|
|
|
|
} else {
|
2021-12-30 21:34:28 +01:00
|
|
|
out = open(arg_out, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664);
|
2018-06-13 01:42:09 +02:00
|
|
|
if (out < 0) {
|
2021-12-30 21:34:28 +01:00
|
|
|
fprintf(stderr, "Cannot open output file %s\n", arg_out);
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = lseek64(in, 0, SEEK_END);
|
|
|
|
lseek64(in, 0, SEEK_SET);
|
|
|
|
|
|
|
|
s = sparse_file_new(block_size, len);
|
|
|
|
if (!s) {
|
|
|
|
fprintf(stderr, "Failed to create sparse file\n");
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sparse_file_verbose(s);
|
2021-12-30 21:34:28 +01:00
|
|
|
ret = sparse_file_read(s, in, mode, false);
|
2018-06-13 01:42:09 +02:00
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "Failed to read file\n");
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = sparse_file_write(s, out, false, true, false);
|
|
|
|
if (ret) {
|
|
|
|
fprintf(stderr, "Failed to write sparse file\n");
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_FAILURE);
|
2018-06-13 01:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(in);
|
|
|
|
close(out);
|
|
|
|
|
2022-11-10 21:24:40 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2012-05-20 22:28:05 +02:00
|
|
|
}
|