forked from pengutronix/genimage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image-flash.c
148 lines (125 loc) · 3.55 KB
/
image-flash.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2011 Sascha Hauer <[email protected]>, Pengutronix
*
* 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 <unistd.h>
#include "list.h"
#include "genimage.h"
struct flash_image {
};
static int flash_generate(struct image *image)
{
struct partition *part;
enum pad_mode mode = MODE_OVERWRITE;
const char *outfile = imageoutfile(image);
list_for_each_entry(part, &image->partitions, list) {
struct image *child;
const char *infile;
int ret;
image_log(image, 1, "writing image partition '%s' (0x%llx@0x%llx)\n",
part->name, part->size, part->offset);
ret = pad_file(image, NULL, outfile, part->offset, 0xFF, mode);
if (ret) {
image_error(image, "failed to pad image to size %lld\n",
part->offset);
return ret;
}
mode = MODE_APPEND;
if (!part->image)
continue;
child = image_get(part->image);
if (!child) {
image_error(image, "could not find %s\n", part->name);
return -EINVAL;
}
infile = imageoutfile(child);
ret = pad_file(image, infile, outfile, part->size, 0xFF, mode);
if (ret) {
image_error(image, "failed to write image partition '%s'\n",
part->name);
return ret;
}
}
return 0;
}
static int flash_setup(struct image *image, cfg_t *cfg)
{
struct flash_image *f = xzalloc(sizeof(*f));
struct partition *part;
int last = 0;
unsigned long long partsize = 0, flashsize;
image->handler_priv = f;
if (!image->flash_type) {
image_error(image, "no flash type given\n");
return -EINVAL;
}
flashsize = image->flash_type->pebsize * image->flash_type->numpebs;
list_for_each_entry(part, &image->partitions, list) {
if (last) {
image_error(image, "only last partition may have size 0\n");
return -EINVAL;
}
if (!part->size) {
last = 1;
if (partsize > flashsize)
goto err_exceed;
part->size = flashsize - partsize;
}
if (part->size % image->flash_type->pebsize) {
image_error(image, "part %s size (%lld) must be a "
"multiple of erase block size (%i bytes)\n",
part->name, part->size, image->flash_type->pebsize);
return -EINVAL;
}
if (part->offset % image->flash_type->pebsize) {
image_error(image, "part %s offset (%lld) must be a"
"multiple of erase block size (%i bytes)\n",
part->name, part->offset, image->flash_type->pebsize);
return -EINVAL;
}
if (part->offset) {
if (partsize > part->offset) {
image_error(image, "part %s overlaps with previous partition\n",
part->name);
return -EINVAL;
}
} else {
part->offset = partsize;
}
partsize = part->offset + part->size;
}
if (partsize > flashsize) {
err_exceed:
image_error(image, "size of partitions (%lld) exceeds flash size (%lld)\n",
partsize, flashsize);
return -EINVAL;
}
return 0;
}
static cfg_opt_t flash_opts[] = {
CFG_END()
};
struct image_handler flash_handler = {
.type = "flash",
.generate = flash_generate,
.setup = flash_setup,
.opts = flash_opts,
};