Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * |
| 5 | * Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com> |
Guilherme Maciel Ferreira | 8ed4d1c | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "imagetool.h" |
| 9 | |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 10 | #include <image.h> |
| 11 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 12 | struct image_type_params *imagetool_get_type(int type) |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 13 | { |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 14 | struct image_type_params **curr; |
| 15 | INIT_SECTION(image_type); |
| 16 | |
| 17 | struct image_type_params **start = __start_image_type; |
| 18 | struct image_type_params **end = __stop_image_type; |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 19 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 20 | for (curr = start; curr != end; curr++) { |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 21 | if ((*curr)->check_image_type) { |
| 22 | if (!(*curr)->check_image_type(type)) |
| 23 | return *curr; |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | return NULL; |
| 27 | } |
| 28 | |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 29 | static int imagetool_verify_print_header_by_type( |
| 30 | void *ptr, |
| 31 | struct stat *sbuf, |
| 32 | struct image_type_params *tparams, |
| 33 | struct image_tool_params *params); |
| 34 | |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 35 | int imagetool_verify_print_header( |
| 36 | void *ptr, |
| 37 | struct stat *sbuf, |
| 38 | struct image_type_params *tparams, |
| 39 | struct image_tool_params *params) |
| 40 | { |
| 41 | int retval = -1; |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 42 | struct image_type_params **curr; |
| 43 | INIT_SECTION(image_type); |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 44 | |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 45 | struct image_type_params **start = __start_image_type; |
| 46 | struct image_type_params **end = __stop_image_type; |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 47 | |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 48 | if (tparams) |
| 49 | return imagetool_verify_print_header_by_type(ptr, sbuf, tparams, params); |
| 50 | |
Guilherme Maciel Ferreira | 28be1cf | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 51 | for (curr = start; curr != end; curr++) { |
Pali Rohár | 6275a24 | 2023-01-29 17:45:55 +0100 | [diff] [blame] | 52 | /* |
| 53 | * Basically every data file can be guessed / verified as gpimage, |
| 54 | * so skip autodetection of data file as gpimage as it does not work. |
| 55 | */ |
| 56 | if ((*curr)->check_image_type && (*curr)->check_image_type(IH_TYPE_GPIMAGE) == 0) |
| 57 | continue; |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 58 | if ((*curr)->verify_header) { |
| 59 | retval = (*curr)->verify_header((unsigned char *)ptr, |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 60 | sbuf->st_size, params); |
| 61 | |
| 62 | if (retval == 0) { |
| 63 | /* |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 64 | * Print the image information if verify is |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 65 | * successful |
| 66 | */ |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 67 | if ((*curr)->print_header) { |
Simon Glass | da1805f | 2016-05-01 13:55:38 -0600 | [diff] [blame] | 68 | if (!params->quiet) |
| 69 | (*curr)->print_header(ptr); |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 70 | } else { |
| 71 | fprintf(stderr, |
| 72 | "%s: print_header undefined for %s\n", |
Andreas Bießmann | d631d70 | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 73 | params->cmdname, (*curr)->name); |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 74 | } |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Pali Rohár | d88ac09 | 2023-01-29 17:45:54 +0100 | [diff] [blame] | 80 | if (retval != 0) { |
| 81 | fprintf(stderr, "%s: cannot detect image type\n", |
| 82 | params->cmdname); |
| 83 | } |
| 84 | |
Guilherme Maciel Ferreira | 0b33803 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 85 | return retval; |
| 86 | } |
Guilherme Maciel Ferreira | fc3c967 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 87 | |
Pali Rohár | cdf0d1c | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 88 | static int imagetool_verify_print_header_by_type( |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 89 | void *ptr, |
| 90 | struct stat *sbuf, |
| 91 | struct image_type_params *tparams, |
| 92 | struct image_tool_params *params) |
| 93 | { |
Nicolas Heemeryck | 7ba97fa | 2022-04-20 23:58:39 +0200 | [diff] [blame] | 94 | int retval = -1; |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 95 | |
Nicolas Heemeryck | 7ba97fa | 2022-04-20 23:58:39 +0200 | [diff] [blame] | 96 | if (tparams->verify_header) { |
| 97 | retval = tparams->verify_header((unsigned char *)ptr, |
| 98 | sbuf->st_size, params); |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 99 | |
Nicolas Heemeryck | 7ba97fa | 2022-04-20 23:58:39 +0200 | [diff] [blame] | 100 | if (retval == 0) { |
| 101 | /* |
| 102 | * Print the image information if verify is successful |
| 103 | */ |
| 104 | if (tparams->print_header) { |
| 105 | if (!params->quiet) |
| 106 | tparams->print_header(ptr); |
| 107 | } else { |
| 108 | fprintf(stderr, |
| 109 | "%s: print_header undefined for %s\n", |
| 110 | params->cmdname, tparams->name); |
| 111 | } |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 112 | } else { |
| 113 | fprintf(stderr, |
Nicolas Heemeryck | 7ba97fa | 2022-04-20 23:58:39 +0200 | [diff] [blame] | 114 | "%s: verify_header failed for %s with exit code %d\n", |
| 115 | params->cmdname, tparams->name, retval); |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 116 | } |
Nicolas Heemeryck | 7ba97fa | 2022-04-20 23:58:39 +0200 | [diff] [blame] | 117 | |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 118 | } else { |
Pali Rohár | 0c08209 | 2023-01-29 17:45:53 +0100 | [diff] [blame] | 119 | fprintf(stderr, "%s: verify_header undefined for %s\n", |
Nicolas Heemeryck | 7ba97fa | 2022-04-20 23:58:39 +0200 | [diff] [blame] | 120 | params->cmdname, tparams->name); |
Jordan Hand | 256b7f9 | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | return retval; |
| 124 | } |
| 125 | |
Guilherme Maciel Ferreira | f3f4a0a | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 126 | int imagetool_save_subimage( |
Guilherme Maciel Ferreira | fc3c967 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 127 | const char *file_name, |
| 128 | ulong file_data, |
| 129 | ulong file_len) |
| 130 | { |
| 131 | int dfd; |
| 132 | |
| 133 | dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, |
| 134 | S_IRUSR | S_IWUSR); |
| 135 | if (dfd < 0) { |
| 136 | fprintf(stderr, "Can't open \"%s\": %s\n", |
| 137 | file_name, strerror(errno)); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) { |
| 142 | fprintf(stderr, "Write error on \"%s\": %s\n", |
| 143 | file_name, strerror(errno)); |
| 144 | close(dfd); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | close(dfd); |
| 149 | |
| 150 | return 0; |
| 151 | } |
Simon Glass | 1c57023 | 2016-02-22 22:55:49 -0700 | [diff] [blame] | 152 | |
| 153 | int imagetool_get_filesize(struct image_tool_params *params, const char *fname) |
| 154 | { |
| 155 | struct stat sbuf; |
| 156 | int fd; |
| 157 | |
| 158 | fd = open(fname, O_RDONLY | O_BINARY); |
| 159 | if (fd < 0) { |
| 160 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 161 | params->cmdname, fname, strerror(errno)); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | if (fstat(fd, &sbuf) < 0) { |
| 166 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 167 | params->cmdname, fname, strerror(errno)); |
Simon Glass | a1c090b | 2016-03-16 07:45:37 -0600 | [diff] [blame] | 168 | close(fd); |
Simon Glass | 1c57023 | 2016-02-22 22:55:49 -0700 | [diff] [blame] | 169 | return -1; |
| 170 | } |
| 171 | close(fd); |
| 172 | |
| 173 | return sbuf.st_size; |
| 174 | } |
Vagrant Cascadian | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 175 | |
| 176 | time_t imagetool_get_source_date( |
Alex Kiernan | c4c7e0d | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 177 | const char *cmdname, |
Vagrant Cascadian | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 178 | time_t fallback) |
| 179 | { |
| 180 | char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); |
| 181 | |
| 182 | if (source_date_epoch == NULL) |
| 183 | return fallback; |
| 184 | |
| 185 | time_t time = (time_t) strtol(source_date_epoch, NULL, 10); |
| 186 | |
| 187 | if (gmtime(&time) == NULL) { |
| 188 | fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n", |
Alex Kiernan | c4c7e0d | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 189 | cmdname); |
Vagrant Cascadian | f8e066c | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 190 | time = 0; |
| 191 | } |
| 192 | |
| 193 | return time; |
| 194 | } |