]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - tools/imagetool.c
Merge branch 'master' of git://git.denx.de/u-boot-sunxi
[karo-tx-uboot.git] / tools / imagetool.c
1 /*
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
11 #include <image.h>
12
13 struct image_type_params *imagetool_get_type(int type)
14 {
15         struct image_type_params *curr;
16         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);
20
21         for (curr = start; curr != end; curr++) {
22                 if (curr->check_image_type) {
23                         if (!curr->check_image_type(type))
24                                 return curr;
25                 }
26         }
27         return NULL;
28 }
29
30 int 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
39         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++) {
45                 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 }
68
69 int imagetool_save_subimage(
70         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 }