forked from pengutronix/genimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-fit.c
125 lines (101 loc) · 3.01 KB
/
image-fit.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* Copyright (c) 2018 Sascha Hauer <s.hauer@pengutronix.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <confuse.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "genimage.h"
static struct partition *partition_by_name(struct image *image, const char *name)
{
struct partition *part;
list_for_each_entry(part, &image->partitions, list)
if (!strcmp(part->name, name))
return part;
return NULL;
}
static int fit_generate(struct image *image)
{
int ret;
struct partition *part, *its;
char *itspath;
int itsfd;
char *keydir = cfg_getstr(image->imagesec, "keydir");
char *keyopt = NULL;
its = partition_by_name(image, "its");
if (!its)
return -EINVAL;
struct image *itsimg = image_get(its->image);
xasprintf(&itspath, "%s/fit.its", tmppath());
/* Copy input its file to temporary path. Use 'cat' to ignore permissions */
ret = systemp(image, "cat '%s' > '%s'", imageoutfile(itsimg), itspath);
if (ret)
return ret;
itsfd = open(itspath, O_WRONLY | O_APPEND);
if (itsfd < 0) {
printf("Cannot open %s: %s\n", itspath, strerror(errno));
return -errno;
}
dprintf(itsfd, "\n");
/* Add /incbin/ to each /images/<partname>/ node */
list_for_each_entry(part, &image->partitions, list) {
struct image *child = image_get(part->image);
const char *file = imageoutfile(child);
const char *target = part->name;
if (part == its)
continue;
dprintf(itsfd, "/ { images { %s { data = /incbin/(\"%s\"); };};};\n", target, file);
}
close(itsfd);
if (keydir && *keydir) {
if (*keydir != '/') {
image_error(image, "'keydir' must be an absolute path\n");
return -EINVAL;
}
xasprintf(&keyopt, "-k '%s'", keydir);
}
ret = systemp(image, "%s -r %s -f '%s' '%s'",
get_opt("mkimage"), keyopt ? keyopt : "", itspath, imageoutfile(image));
if (ret)
image_error(image, "Failed to create FIT image\n");
return ret;
}
static int fit_parse(struct image *image, cfg_t *cfg)
{
struct partition *part;
char *its = cfg_getstr(image->imagesec, "its");
part = xzalloc(sizeof *part);
part->name = "its";
part->image = its;
list_add_tail(&part->list, &image->partitions);
return 0;
}
static cfg_opt_t fit_opts[] = {
CFG_STR("keydir", "", CFGF_NONE),
CFG_STR("its", "", CFGF_NONE),
CFG_END()
};
struct image_handler fit_handler = {
.type = "fit",
.no_rootpath = cfg_true,
.generate = fit_generate,
.parse = fit_parse,
.opts = fit_opts,
};