Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2013 |
| 3 | * |
| 4 | * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include "imagetool.h" |
| 10 | |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 11 | #include <image.h> |
| 12 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 13 | struct image_type_params *imagetool_get_type(int type) |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 14 | { |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 15 | struct image_type_params **curr; |
| 16 | INIT_SECTION(image_type); |
| 17 | |
| 18 | struct image_type_params **start = __start_image_type; |
| 19 | struct image_type_params **end = __stop_image_type; |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 20 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 21 | for (curr = start; curr != end; curr++) { |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 22 | if ((*curr)->check_image_type) { |
| 23 | if (!(*curr)->check_image_type(type)) |
| 24 | return *curr; |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 25 | } |
| 26 | } |
| 27 | return NULL; |
| 28 | } |
| 29 | |
| 30 | int imagetool_verify_print_header( |
| 31 | void *ptr, |
| 32 | struct stat *sbuf, |
| 33 | struct image_type_params *tparams, |
| 34 | struct image_tool_params *params) |
| 35 | { |
| 36 | int retval = -1; |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 37 | struct image_type_params **curr; |
| 38 | INIT_SECTION(image_type); |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 39 | |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 40 | struct image_type_params **start = __start_image_type; |
| 41 | struct image_type_params **end = __stop_image_type; |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 42 | |
| 43 | for (curr = start; curr != end; curr++) { |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 44 | if ((*curr)->verify_header) { |
| 45 | retval = (*curr)->verify_header((unsigned char *)ptr, |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 46 | sbuf->st_size, params); |
| 47 | |
| 48 | if (retval == 0) { |
| 49 | /* |
| 50 | * Print the image information if verify is |
| 51 | * successful |
| 52 | */ |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 53 | if ((*curr)->print_header) { |
Simon Glass | da1805f | 2016-05-01 13:55:38 -0600 | [diff] [blame] | 54 | if (!params->quiet) |
| 55 | (*curr)->print_header(ptr); |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 56 | } else { |
| 57 | fprintf(stderr, |
| 58 | "%s: print_header undefined for %s\n", |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 59 | params->cmdname, (*curr)->name); |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 60 | } |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | return retval; |
| 67 | } |
Guilherme Maciel Ferreira | fc3c967 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 68 | |
Guilherme Maciel Ferreira | f3f4a0a | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 69 | int imagetool_save_subimage( |
Guilherme Maciel Ferreira | fc3c967 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 70 | const char *file_name, |
| 71 | ulong file_data, |
| 72 | ulong file_len) |
| 73 | { |
| 74 | int dfd; |
| 75 | |
| 76 | dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, |
| 77 | S_IRUSR | S_IWUSR); |
| 78 | if (dfd < 0) { |
| 79 | fprintf(stderr, "Can't open \"%s\": %s\n", |
| 80 | file_name, strerror(errno)); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) { |
| 85 | fprintf(stderr, "Write error on \"%s\": %s\n", |
| 86 | file_name, strerror(errno)); |
| 87 | close(dfd); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | close(dfd); |
| 92 | |
| 93 | return 0; |
| 94 | } |
Simon Glass | 1c57023 | 2016-02-22 22:55:49 -0700 | [diff] [blame] | 95 | |
| 96 | int imagetool_get_filesize(struct image_tool_params *params, const char *fname) |
| 97 | { |
| 98 | struct stat sbuf; |
| 99 | int fd; |
| 100 | |
| 101 | fd = open(fname, O_RDONLY | O_BINARY); |
| 102 | if (fd < 0) { |
| 103 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 104 | params->cmdname, fname, strerror(errno)); |
| 105 | return -1; |
| 106 | } |
| 107 | |
| 108 | if (fstat(fd, &sbuf) < 0) { |
| 109 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 110 | params->cmdname, fname, strerror(errno)); |
Simon Glass | a1c090b | 2016-03-16 07:45:37 -0600 | [diff] [blame] | 111 | close(fd); |
Simon Glass | 1c57023 | 2016-02-22 22:55:49 -0700 | [diff] [blame] | 112 | return -1; |
| 113 | } |
| 114 | close(fd); |
| 115 | |
| 116 | return sbuf.st_size; |
| 117 | } |
Vagrant Cascadian | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 118 | |
| 119 | time_t imagetool_get_source_date( |
| 120 | struct image_tool_params *params, |
| 121 | time_t fallback) |
| 122 | { |
| 123 | char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); |
| 124 | |
| 125 | if (source_date_epoch == NULL) |
| 126 | return fallback; |
| 127 | |
| 128 | time_t time = (time_t) strtol(source_date_epoch, NULL, 10); |
| 129 | |
| 130 | if (gmtime(&time) == NULL) { |
| 131 | fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n", |
| 132 | params->cmdname); |
| 133 | time = 0; |
| 134 | } |
| 135 | |
| 136 | return time; |
| 137 | } |