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