Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Guilherme Maciel Ferreira | f86ed6a | 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 | f86ed6a | 2013-12-01 12:43:10 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "imagetool.h" |
| 9 | |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 10 | #include <image.h> |
| 11 | |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 12 | struct image_type_params *imagetool_get_type(int type) |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 13 | { |
Andreas Bießmann | 1fddd7b | 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 | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 19 | |
Guilherme Maciel Ferreira | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 20 | for (curr = start; curr != end; curr++) { |
Andreas Bießmann | 1fddd7b | 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 | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | return NULL; |
| 27 | } |
| 28 | |
Pali Rohár | 11f29d4 | 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 | 0ca6691 | 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 | 1fddd7b | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 42 | struct image_type_params **curr; |
| 43 | INIT_SECTION(image_type); |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 44 | |
Andreas Bießmann | 1fddd7b | 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 | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 47 | |
Pali Rohár | 11f29d4 | 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 | a93648d | 2015-01-15 02:48:07 -0200 | [diff] [blame] | 51 | for (curr = start; curr != end; curr++) { |
Andreas Bießmann | 1fddd7b | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 52 | if ((*curr)->verify_header) { |
| 53 | retval = (*curr)->verify_header((unsigned char *)ptr, |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 54 | sbuf->st_size, params); |
| 55 | |
| 56 | if (retval == 0) { |
| 57 | /* |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 58 | * Print the image information if verify is |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 59 | * successful |
| 60 | */ |
Andreas Bießmann | 1fddd7b | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 61 | if ((*curr)->print_header) { |
Simon Glass | bd6e142 | 2016-05-01 13:55:38 -0600 | [diff] [blame] | 62 | if (!params->quiet) |
| 63 | (*curr)->print_header(ptr); |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 64 | } else { |
| 65 | fprintf(stderr, |
| 66 | "%s: print_header undefined for %s\n", |
Andreas Bießmann | 1fddd7b | 2015-02-09 00:06:10 +0100 | [diff] [blame] | 67 | params->cmdname, (*curr)->name); |
Guilherme Maciel Ferreira | 0ca6691 | 2015-01-15 02:48:05 -0200 | [diff] [blame] | 68 | } |
| 69 | break; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return retval; |
| 75 | } |
Guilherme Maciel Ferreira | 067d156 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 76 | |
Pali Rohár | 11f29d4 | 2022-02-13 01:09:46 +0100 | [diff] [blame] | 77 | static int imagetool_verify_print_header_by_type( |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 78 | void *ptr, |
| 79 | struct stat *sbuf, |
| 80 | struct image_type_params *tparams, |
| 81 | struct image_tool_params *params) |
| 82 | { |
Nicolas Heemeryck | 30705cd | 2022-04-20 23:58:39 +0200 | [diff] [blame^] | 83 | int retval = -1; |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 84 | |
Nicolas Heemeryck | 30705cd | 2022-04-20 23:58:39 +0200 | [diff] [blame^] | 85 | if (tparams->verify_header) { |
| 86 | retval = tparams->verify_header((unsigned char *)ptr, |
| 87 | sbuf->st_size, params); |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 88 | |
Nicolas Heemeryck | 30705cd | 2022-04-20 23:58:39 +0200 | [diff] [blame^] | 89 | if (retval == 0) { |
| 90 | /* |
| 91 | * Print the image information if verify is successful |
| 92 | */ |
| 93 | if (tparams->print_header) { |
| 94 | if (!params->quiet) |
| 95 | tparams->print_header(ptr); |
| 96 | } else { |
| 97 | fprintf(stderr, |
| 98 | "%s: print_header undefined for %s\n", |
| 99 | params->cmdname, tparams->name); |
| 100 | } |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 101 | } else { |
| 102 | fprintf(stderr, |
Nicolas Heemeryck | 30705cd | 2022-04-20 23:58:39 +0200 | [diff] [blame^] | 103 | "%s: verify_header failed for %s with exit code %d\n", |
| 104 | params->cmdname, tparams->name, retval); |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 105 | } |
Nicolas Heemeryck | 30705cd | 2022-04-20 23:58:39 +0200 | [diff] [blame^] | 106 | |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 107 | } else { |
Nicolas Heemeryck | 30705cd | 2022-04-20 23:58:39 +0200 | [diff] [blame^] | 108 | fprintf(stderr, "%s: print_header undefined for %s\n", |
| 109 | params->cmdname, tparams->name); |
Jordan Hand | d32aa3c | 2019-03-05 14:47:56 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return retval; |
| 113 | } |
| 114 | |
Guilherme Maciel Ferreira | 67f946c | 2015-01-15 02:54:41 -0200 | [diff] [blame] | 115 | int imagetool_save_subimage( |
Guilherme Maciel Ferreira | 067d156 | 2015-01-15 02:48:06 -0200 | [diff] [blame] | 116 | const char *file_name, |
| 117 | ulong file_data, |
| 118 | ulong file_len) |
| 119 | { |
| 120 | int dfd; |
| 121 | |
| 122 | dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, |
| 123 | S_IRUSR | S_IWUSR); |
| 124 | if (dfd < 0) { |
| 125 | fprintf(stderr, "Can't open \"%s\": %s\n", |
| 126 | file_name, strerror(errno)); |
| 127 | return -1; |
| 128 | } |
| 129 | |
| 130 | if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) { |
| 131 | fprintf(stderr, "Write error on \"%s\": %s\n", |
| 132 | file_name, strerror(errno)); |
| 133 | close(dfd); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | close(dfd); |
| 138 | |
| 139 | return 0; |
| 140 | } |
Simon Glass | 3837ce6 | 2016-02-22 22:55:49 -0700 | [diff] [blame] | 141 | |
| 142 | int imagetool_get_filesize(struct image_tool_params *params, const char *fname) |
| 143 | { |
| 144 | struct stat sbuf; |
| 145 | int fd; |
| 146 | |
| 147 | fd = open(fname, O_RDONLY | O_BINARY); |
| 148 | if (fd < 0) { |
| 149 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 150 | params->cmdname, fname, strerror(errno)); |
| 151 | return -1; |
| 152 | } |
| 153 | |
| 154 | if (fstat(fd, &sbuf) < 0) { |
| 155 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 156 | params->cmdname, fname, strerror(errno)); |
Simon Glass | b12a81c | 2016-03-16 07:45:37 -0600 | [diff] [blame] | 157 | close(fd); |
Simon Glass | 3837ce6 | 2016-02-22 22:55:49 -0700 | [diff] [blame] | 158 | return -1; |
| 159 | } |
| 160 | close(fd); |
| 161 | |
| 162 | return sbuf.st_size; |
| 163 | } |
Vagrant Cascadian | 5847084 | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 164 | |
| 165 | time_t imagetool_get_source_date( |
Alex Kiernan | 87925df | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 166 | const char *cmdname, |
Vagrant Cascadian | 5847084 | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 167 | time_t fallback) |
| 168 | { |
| 169 | char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); |
| 170 | |
| 171 | if (source_date_epoch == NULL) |
| 172 | return fallback; |
| 173 | |
| 174 | time_t time = (time_t) strtol(source_date_epoch, NULL, 10); |
| 175 | |
| 176 | if (gmtime(&time) == NULL) { |
| 177 | fprintf(stderr, "%s: SOURCE_DATE_EPOCH is not valid\n", |
Alex Kiernan | 87925df | 2018-06-20 20:10:51 +0000 | [diff] [blame] | 178 | cmdname); |
Vagrant Cascadian | 5847084 | 2016-06-16 12:28:40 -0700 | [diff] [blame] | 179 | time = 0; |
| 180 | } |
| 181 | |
| 182 | return time; |
| 183 | } |