doc: board: ti: Move documentation from README to .rst
[platform/kernel/u-boot.git] / tools / fit_common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2014
4  * DENX Software Engineering
5  * Heiko Schocher <hs@denx.de>
6  *
7  * (C) Copyright 2008 Semihalf
8  *
9  * (C) Copyright 2000-2004
10  * DENX Software Engineering
11  * Wolfgang Denk, wd@denx.de
12  *
13  * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
14  *              FIT image specific code abstracted from mkimage.c
15  *              some functions added to address abstraction
16  *
17  * All rights reserved.
18  */
19
20 #include "imagetool.h"
21 #include "mkimage.h"
22 #include "fit_common.h"
23 #include <image.h>
24 #include <u-boot/crc.h>
25
26 void fit_print_header(const void *fit, struct image_tool_params *params)
27 {
28         fit_print_contents(fit);
29 }
30
31 int fit_verify_header(unsigned char *ptr, int image_size,
32                         struct image_tool_params *params)
33 {
34         int ret;
35
36         if (fdt_check_header(ptr) != EXIT_SUCCESS)
37                 return EXIT_FAILURE;
38
39         ret = fit_check_format(ptr, IMAGE_SIZE_INVAL);
40         if (ret) {
41                 if (ret != -EADDRNOTAVAIL)
42                         return EXIT_FAILURE;
43                 fprintf(stderr, "Image contains unit addresses @, this will break signing\n");
44         }
45
46         return EXIT_SUCCESS;
47 }
48
49 int fit_check_image_types(uint8_t type)
50 {
51         if (type == IH_TYPE_FLATDT)
52                 return EXIT_SUCCESS;
53         else
54                 return EXIT_FAILURE;
55 }
56
57 int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
58              void **blobp, struct stat *sbuf, bool delete_on_error,
59              bool read_only)
60 {
61         void *ptr;
62         int fd;
63
64         /* Load FIT blob into memory (we need to write hashes/signatures) */
65         fd = open(fname, (read_only ? O_RDONLY : O_RDWR) | O_BINARY);
66
67         if (fd < 0) {
68                 fprintf(stderr, "%s: Can't open %s: %s\n",
69                         cmdname, fname, strerror(errno));
70                 goto err;
71         }
72
73         if (fstat(fd, sbuf) < 0) {
74                 fprintf(stderr, "%s: Can't stat %s: %s\n",
75                         cmdname, fname, strerror(errno));
76                 goto err;
77         }
78
79         if (size_inc) {
80                 sbuf->st_size += size_inc;
81                 if (ftruncate(fd, sbuf->st_size)) {
82                         fprintf(stderr, "%s: Can't expand %s: %s\n",
83                                 cmdname, fname, strerror(errno));
84                 goto err;
85                 }
86         }
87
88         errno = 0;
89         ptr = mmap(0, sbuf->st_size,
90                    (read_only ? PROT_READ : PROT_READ | PROT_WRITE), MAP_SHARED,
91                    fd, 0);
92         if ((ptr == MAP_FAILED) || (errno != 0)) {
93                 fprintf(stderr, "%s: Can't read %s: %s\n",
94                         cmdname, fname, strerror(errno));
95                 goto err;
96         }
97
98         /* check if ptr has a valid blob */
99         if (fdt_check_header(ptr)) {
100                 fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
101                 goto err;
102         }
103
104         /* expand if needed */
105         if (size_inc) {
106                 int ret;
107
108                 ret = fdt_open_into(ptr, ptr, sbuf->st_size);
109                 if (ret) {
110                         fprintf(stderr, "%s: Cannot expand FDT: %s\n",
111                                 cmdname, fdt_strerror(ret));
112                         goto err;
113                 }
114         }
115
116         *blobp = ptr;
117         return fd;
118
119 err:
120         if (fd >= 0)
121                 close(fd);
122         if (delete_on_error)
123                 unlink(fname);
124
125         return -1;
126 }
127
128 int copyfile(const char *src, const char *dst)
129 {
130         int fd_src = -1, fd_dst = -1;
131         void *buf = NULL;
132         ssize_t size;
133         size_t count;
134         int ret = -1;
135
136         fd_src = open(src, O_RDONLY);
137         if (fd_src < 0) {
138                 printf("Can't open file %s (%s)\n", src, strerror(errno));
139                 goto out;
140         }
141
142         fd_dst = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0666);
143         if (fd_dst < 0) {
144                 printf("Can't open file %s (%s)\n", dst, strerror(errno));
145                 goto out;
146         }
147
148         buf = calloc(1, 512);
149         if (!buf) {
150                 printf("Can't allocate buffer to copy file\n");
151                 goto out;
152         }
153
154         while (1) {
155                 size = read(fd_src, buf, 512);
156                 if (size < 0) {
157                         printf("Can't read file %s\n", src);
158                         goto out;
159                 }
160                 if (!size)
161                         break;
162
163                 count = size;
164                 size = write(fd_dst, buf, count);
165                 if (size < 0) {
166                         printf("Can't write file %s\n", dst);
167                         goto out;
168                 }
169         }
170
171         ret = 0;
172
173  out:
174         if (fd_src >= 0)
175                 close(fd_src);
176         if (fd_dst >= 0)
177                 close(fd_dst);
178         if (buf)
179                 free(buf);
180
181         return ret;
182 }
183
184 void summary_show(struct image_summary *summary, const char *imagefile,
185                   const char *keydest)
186 {
187         if (summary->sig_offset) {
188                 printf("Signature written to '%s', node '%s'\n", imagefile,
189                        summary->sig_path);
190                 if (keydest) {
191                         printf("Public key written to '%s', node '%s'\n",
192                                keydest, summary->keydest_path);
193                 }
194         }
195 }