Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 17 | #include <sparse/sparse.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include <fcntl.h> |
Colin Cross | 0c4c47f | 2012-04-25 19:02:58 -0700 | [diff] [blame] | 20 | #include <stdbool.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 28 | #ifndef O_BINARY |
| 29 | #define O_BINARY 0 |
| 30 | #endif |
| 31 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 32 | void usage() { |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 33 | fprintf(stderr, "Usage: simg2img <sparse_image_files> <raw_image_file>\n"); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 36 | int main(int argc, char* argv[]) { |
| 37 | int in; |
| 38 | int out; |
| 39 | int i; |
| 40 | struct sparse_file* s; |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 41 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 42 | if (argc < 3) { |
| 43 | usage(); |
| 44 | exit(-1); |
| 45 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 46 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 47 | out = open(argv[argc - 1], O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0664); |
| 48 | if (out < 0) { |
| 49 | fprintf(stderr, "Cannot open output file %s\n", argv[argc - 1]); |
| 50 | exit(-1); |
| 51 | } |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 52 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 53 | for (i = 1; i < argc - 1; i++) { |
| 54 | if (strcmp(argv[i], "-") == 0) { |
| 55 | in = STDIN_FILENO; |
| 56 | } else { |
| 57 | in = open(argv[i], O_RDONLY | O_BINARY); |
| 58 | if (in < 0) { |
| 59 | fprintf(stderr, "Cannot open input file %s\n", argv[i]); |
| 60 | exit(-1); |
| 61 | } |
| 62 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 63 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 64 | s = sparse_file_import(in, true, false); |
| 65 | if (!s) { |
| 66 | fprintf(stderr, "Failed to read sparse file\n"); |
| 67 | exit(-1); |
| 68 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 69 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 70 | if (lseek(out, 0, SEEK_SET) == -1) { |
| 71 | perror("lseek failed"); |
| 72 | exit(EXIT_FAILURE); |
| 73 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 74 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 75 | if (sparse_file_write(s, out, false, false, false) < 0) { |
| 76 | fprintf(stderr, "Cannot write output file\n"); |
| 77 | exit(-1); |
| 78 | } |
| 79 | sparse_file_destroy(s); |
| 80 | close(in); |
| 81 | } |
Colin Cross | bdc6d39 | 2012-05-02 15:18:22 -0700 | [diff] [blame] | 82 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 83 | close(out); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 84 | |
Jerry Zhang | 7b444f0 | 2018-06-12 16:42:09 -0700 | [diff] [blame] | 85 | exit(0); |
Colin Cross | 28fa5bc | 2012-05-20 13:28:05 -0700 | [diff] [blame] | 86 | } |