summaryrefslogtreecommitdiffstats
path: root/image-ubi.c
blob: 21d1310130c4f2c6242a0c8cc40e69a6ff2f57b8 (plain)
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
/*
 * Copyright (c) 2011 Sascha Hauer <s.hauer@pengutronix.de>, 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 "genimage.h"

struct ubi {
};

static int ubi_generate(struct image *image)
{
	int ret;
	FILE *fini;
	char *tempfile;
	int i = 0;
	struct partition *part;
	char *extraargs = cfg_getstr(image->imagesec, "extraargs");

	xasprintf(&tempfile, "%s/ubi.ini", tmppath());
	if (!tempfile)
		return -ENOMEM;

	fini = fopen(tempfile, "w");
	if (!fini) {
		ret = -errno;
		image_error(image, "creating temp file failed: %s\n", strerror(errno));
		goto err_free;
	}

	list_for_each_entry(part, &image->partitions, list) {
		struct image *child = NULL;
		unsigned long long size = part->size;
		if (part->image)
			child = image_get(part->image);
		if (!size) {
			if (!child) {
				image_error(image, "could not find %s\n", part->image);
				fclose(fini);
				ret = -EINVAL;
				goto err_free;
			}
			size = child->size;
		}

		fprintf(fini, "[%s]\n", part->name);
		fprintf(fini, "mode=ubi\n");
		if (child)
			fprintf(fini, "image=%s\n", imageoutfile(child));
		fprintf(fini, "vol_id=%d\n", i);
		fprintf(fini, "vol_size=%lld\n", size);
		fprintf(fini, "vol_type=%s\n", part->read_only ? "static" : "dynamic");
		fprintf(fini, "vol_name=%s\n", part->name);
		if (part->autoresize)
			fprintf(fini, "vol_flags=autoresize\n");
		fprintf(fini, "vol_alignment=1\n");
		i++;
	}

	fclose(fini);

	ret = systemp(image, "%s -s %d -O %d -p %d -m %d -o '%s' '%s' %s",
			get_opt("ubinize"),
			image->flash_type->sub_page_size,
			image->flash_type->vid_header_offset,
			image->flash_type->pebsize,
			image->flash_type->minimum_io_unit_size,
			imageoutfile(image),
			tempfile,
			extraargs);

err_free:
	free(tempfile);

	return ret;
}

static int ubi_setup(struct image *image, cfg_t *cfg)
{
	struct ubi *ubi = xzalloc(sizeof(*ubi));
	int autoresize = 0;
	struct partition *part;

	if (!image->flash_type) {
		image_error(image, "no flash type given\n");
		return -EINVAL;
	}

	image->handler_priv = ubi;

	list_for_each_entry(part, &image->partitions, list)
		autoresize += part->autoresize;

	if (autoresize > 1) {
		image_error(image, "more than one volume has the autoresize flag set\n");
		return -EINVAL;
	}

	return 0;
}

static cfg_opt_t ubi_opts[] = {
	CFG_STR("extraargs", "", CFGF_NONE),
	CFG_END()
};

struct image_handler ubi_handler = {
	.type = "ubi",
	.no_rootpath = cfg_true,
	.generate = ubi_generate,
	.setup = ubi_setup,
	.opts = ubi_opts,
};