]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - tools/imagetool.c
imagetool: make the image_save_datafile() available to all image types
[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 /*
14  * Callback function to register a image type within a tool
15  */
16 static imagetool_register_t register_func;
17
18 /*
19  * register_image_tool -
20  *
21  * The tool provides its own registration function in order to all image
22  * types initialize themselves.
23  */
24 void register_image_tool(imagetool_register_t image_register)
25 {
26         /*
27          * Save the image tool callback function. It will be used to register
28          * image types within that tool
29          */
30         register_func = image_register;
31
32         /* Init ATMEL ROM Boot Image generation/list support */
33         init_atmel_image_type();
34         /* Init Freescale PBL Boot image generation/list support */
35         init_pbl_image_type();
36         /* Init Kirkwood Boot image generation/list support */
37         init_kwb_image_type();
38         /* Init Freescale imx Boot image generation/list support */
39         init_imx_image_type();
40         /* Init Freescale mxs Boot image generation/list support */
41         init_mxs_image_type();
42         /* Init FIT image generation/list support */
43         init_fit_image_type();
44         /* Init TI OMAP Boot image generation/list support */
45         init_omap_image_type();
46         /* Init Default image generation/list support */
47         init_default_image_type();
48         /* Init Davinci UBL support */
49         init_ubl_image_type();
50         /* Init Davinci AIS support */
51         init_ais_image_type();
52         /* Init Altera SOCFPGA support */
53         init_socfpga_image_type();
54         /* Init TI Keystone boot image generation/list support */
55         init_gpimage_type();
56 }
57
58 /*
59  * register_image_type -
60  *
61  * Register a image type within a tool
62  */
63 void register_image_type(struct image_type_params *tparams)
64 {
65         register_func(tparams);
66 }
67
68 struct image_type_params *imagetool_get_type(
69         int type,
70         struct image_type_params *tparams)
71 {
72         struct image_type_params *curr;
73
74         for (curr = tparams; curr != NULL; curr = curr->next) {
75                 if (curr->check_image_type) {
76                         if (!curr->check_image_type(type))
77                                 return curr;
78                 }
79         }
80         return NULL;
81 }
82
83 int imagetool_verify_print_header(
84         void *ptr,
85         struct stat *sbuf,
86         struct image_type_params *tparams,
87         struct image_tool_params *params)
88 {
89         int retval = -1;
90         struct image_type_params *curr;
91
92         for (curr = tparams; curr != NULL; curr = curr->next) {
93                 if (curr->verify_header) {
94                         retval = curr->verify_header((unsigned char *)ptr,
95                                                      sbuf->st_size, params);
96
97                         if (retval == 0) {
98                                 /*
99                                  * Print the image information  if verify is
100                                  * successful
101                                  */
102                                 if (curr->print_header) {
103                                         curr->print_header(ptr);
104                                 } else {
105                                         fprintf(stderr,
106                                                 "%s: print_header undefined for %s\n",
107                                                 params->cmdname, curr->name);
108                                 }
109                                 break;
110                         }
111                 }
112         }
113
114         return retval;
115 }
116
117 int imagetool_save_datafile(
118         const char *file_name,
119         ulong file_data,
120         ulong file_len)
121 {
122         int dfd;
123
124         dfd = open(file_name, O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
125                    S_IRUSR | S_IWUSR);
126         if (dfd < 0) {
127                 fprintf(stderr, "Can't open \"%s\": %s\n",
128                         file_name, strerror(errno));
129                 return -1;
130         }
131
132         if (write(dfd, (void *)file_data, file_len) != (ssize_t)file_len) {
133                 fprintf(stderr, "Write error on \"%s\": %s\n",
134                         file_name, strerror(errno));
135                 close(dfd);
136                 return -1;
137         }
138
139         close(dfd);
140
141         return 0;
142 }