blob: 148e4662b7b366e2b2400639aa733b59e747865f [file] [log] [blame]
Guilherme Maciel Ferreiraf86ed6a2013-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 Ferreira0ca66912015-01-15 02:48:05 -020011#include <image.h>
12
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020013struct image_type_params *imagetool_get_type(int type)
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020014{
15 struct image_type_params *curr;
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020016 struct image_type_params *start = ll_entry_start(
17 struct image_type_params, image_type);
18 struct image_type_params *end = ll_entry_end(
19 struct image_type_params, image_type);
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020020
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020021 for (curr = start; curr != end; curr++) {
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020022 if (curr->check_image_type) {
23 if (!curr->check_image_type(type))
24 return curr;
25 }
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;
37 struct image_type_params *curr;
38
Guilherme Maciel Ferreiraa93648d2015-01-15 02:48:07 -020039 struct image_type_params *start = ll_entry_start(
40 struct image_type_params, image_type);
41 struct image_type_params *end = ll_entry_end(
42 struct image_type_params, image_type);
43
44 for (curr = start; curr != end; curr++) {
Guilherme Maciel Ferreira0ca66912015-01-15 02:48:05 -020045 if (curr->verify_header) {
46 retval = curr->verify_header((unsigned char *)ptr,
47 sbuf->st_size, params);
48
49 if (retval == 0) {
50 /*
51 * Print the image information if verify is
52 * successful
53 */
54 if (curr->print_header) {
55 curr->print_header(ptr);
56 } else {
57 fprintf(stderr,
58 "%s: print_header undefined for %s\n",
59 params->cmdname, curr->name);
60 }
61 break;
62 }
63 }
64 }
65
66 return retval;
67}
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -020068
Guilherme Maciel Ferreira67f946c2015-01-15 02:54:41 -020069int imagetool_save_subimage(
Guilherme Maciel Ferreira067d1562015-01-15 02:48:06 -020070 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}