blob: 351211cd5387b9e727ce22501428d2a6af19cc7b [file] [log] [blame]
Guilherme Maciel Ferreira8ed4d1c2013-12-01 12:43:10 -07001/*
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 Ferreira0b338032015-01-15 02:48:05 -020011#include <image.h>
12
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -020013struct image_type_params *imagetool_get_type(int type)
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020014{
Andreas Bießmannd631d702015-02-09 00:06:10 +010015 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 Ferreira0b338032015-01-15 02:48:05 -020020
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -020021 for (curr = start; curr != end; curr++) {
Andreas Bießmannd631d702015-02-09 00:06:10 +010022 if ((*curr)->check_image_type) {
23 if (!(*curr)->check_image_type(type))
24 return *curr;
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020025 }
26 }
27 return NULL;
28}
29
30int 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ßmannd631d702015-02-09 00:06:10 +010037 struct image_type_params **curr;
38 INIT_SECTION(image_type);
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020039
Andreas Bießmannd631d702015-02-09 00:06:10 +010040 struct image_type_params **start = __start_image_type;
41 struct image_type_params **end = __stop_image_type;
Guilherme Maciel Ferreira28be1cf2015-01-15 02:48:07 -020042
43 for (curr = start; curr != end; curr++) {
Andreas Bießmannd631d702015-02-09 00:06:10 +010044 if ((*curr)->verify_header) {
45 retval = (*curr)->verify_header((unsigned char *)ptr,
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020046 sbuf->st_size, params);
47
48 if (retval == 0) {
49 /*
50 * Print the image information if verify is
51 * successful
52 */
Andreas Bießmannd631d702015-02-09 00:06:10 +010053 if ((*curr)->print_header) {
54 (*curr)->print_header(ptr);
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020055 } else {
56 fprintf(stderr,
57 "%s: print_header undefined for %s\n",
Andreas Bießmannd631d702015-02-09 00:06:10 +010058 params->cmdname, (*curr)->name);
Guilherme Maciel Ferreira0b338032015-01-15 02:48:05 -020059 }
60 break;
61 }
62 }
63 }
64
65 return retval;
66}
Guilherme Maciel Ferreirafc3c9672015-01-15 02:48:06 -020067
Guilherme Maciel Ferreiraf3f4a0a2015-01-15 02:54:41 -020068int imagetool_save_subimage(
Guilherme Maciel Ferreirafc3c9672015-01-15 02:48:06 -020069 const char *file_name,
70 ulong file_data,
71 ulong file_len)
72{
73 int dfd;
74
75 dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
76 S_IRUSR | S_IWUSR);
77 if (dfd < 0) {
78 fprintf(stderr, "Can't open \"%s\": %s\n",
79 file_name, strerror(errno));
80 return -1;
81 }
82
83 if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
84 fprintf(stderr, "Write error on \"%s\": %s\n",
85 file_name, strerror(errno));
86 close(dfd);
87 return -1;
88 }
89
90 close(dfd);
91
92 return 0;
93}
Simon Glass1c570232016-02-22 22:55:49 -070094
95int imagetool_get_filesize(struct image_tool_params *params, const char *fname)
96{
97 struct stat sbuf;
98 int fd;
99
100 fd = open(fname, O_RDONLY | O_BINARY);
101 if (fd < 0) {
102 fprintf(stderr, "%s: Can't open %s: %s\n",
103 params->cmdname, fname, strerror(errno));
104 return -1;
105 }
106
107 if (fstat(fd, &sbuf) < 0) {
108 fprintf(stderr, "%s: Can't stat %s: %s\n",
109 params->cmdname, fname, strerror(errno));
110 return -1;
111 }
112 close(fd);
113
114 return sbuf.st_size;
115}