2011-06-27 15:58:21 +08:00
|
|
|
/*
|
|
|
|
* 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
|
2015-02-09 00:26:29 +08:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-06-27 15:58:21 +08:00
|
|
|
*/
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <confuse.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <libgen.h>
|
2018-12-17 20:46:01 +08:00
|
|
|
#include <sys/stat.h>
|
2019-01-16 17:12:04 +08:00
|
|
|
#include <sys/time.h>
|
2011-06-27 14:39:51 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "genimage.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
*
|
|
|
|
* - add documentation
|
|
|
|
* - implement missing image types (cpio, iso)
|
|
|
|
* - free memory after usage
|
|
|
|
* - make more failsafe (does flashtype exist where necessary)
|
|
|
|
* - implement command line switches (--verbose, --dry-run, --config=)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static struct image_handler *handlers[] = {
|
2021-01-12 15:46:22 +08:00
|
|
|
&android_sparse_handler,
|
2012-11-25 05:59:58 +08:00
|
|
|
&cpio_handler,
|
2016-09-06 17:54:49 +08:00
|
|
|
&cramfs_handler,
|
2011-06-27 14:39:51 +08:00
|
|
|
&ext2_handler,
|
2011-10-11 17:05:01 +08:00
|
|
|
&ext3_handler,
|
|
|
|
&ext4_handler,
|
2022-03-21 18:30:29 +08:00
|
|
|
&f2fs_handler,
|
2011-06-27 14:39:51 +08:00
|
|
|
&file_handler,
|
2018-02-14 23:03:55 +08:00
|
|
|
&fit_handler,
|
2012-11-25 05:59:58 +08:00
|
|
|
&flash_handler,
|
|
|
|
&hdimage_handler,
|
2012-11-25 06:02:31 +08:00
|
|
|
&iso_handler,
|
2012-11-25 05:59:58 +08:00
|
|
|
&jffs2_handler,
|
2018-03-20 04:44:53 +08:00
|
|
|
&qemu_handler,
|
2016-08-05 17:51:05 +08:00
|
|
|
&rauc_handler,
|
2014-02-19 18:16:54 +08:00
|
|
|
&squashfs_handler,
|
2012-11-25 05:59:58 +08:00
|
|
|
&tar_handler,
|
|
|
|
&ubi_handler,
|
|
|
|
&ubifs_handler,
|
2012-06-22 14:27:52 +08:00
|
|
|
&vfat_handler,
|
2011-06-27 14:39:51 +08:00
|
|
|
};
|
|
|
|
|
2011-06-27 16:07:56 +08:00
|
|
|
static int image_set_handler(struct image *image, cfg_t *cfg)
|
2011-06-27 14:39:51 +08:00
|
|
|
{
|
2011-06-30 01:58:10 +08:00
|
|
|
unsigned int i;
|
|
|
|
int num = 0, x;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(handlers); i++) {
|
|
|
|
struct image_handler *handler = handlers[i];
|
|
|
|
|
|
|
|
x = cfg_size(cfg, handler->type);
|
|
|
|
if (x)
|
|
|
|
image->handler = handler;
|
|
|
|
num += x;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num > 1) {
|
2014-04-24 04:18:35 +08:00
|
|
|
image_error(image, "multiple image types given\n");
|
2011-06-27 14:39:51 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num < 1) {
|
2014-04-24 04:18:35 +08:00
|
|
|
image_error(image, "no image type given\n");
|
2011-06-27 14:39:51 +08:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
image->imagesec = cfg_getsec(cfg, image->handler->type);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static cfg_opt_t partition_opts[] = {
|
|
|
|
CFG_STR("offset", NULL, CFGF_NONE),
|
|
|
|
CFG_STR("size", NULL, CFGF_NONE),
|
2021-01-11 05:54:22 +08:00
|
|
|
CFG_STR("align", NULL, CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_INT("partition-type", 0, CFGF_NONE),
|
2011-10-10 22:38:27 +08:00
|
|
|
CFG_BOOL("bootable", cfg_false, CFGF_NONE),
|
2016-01-04 20:49:49 +08:00
|
|
|
CFG_BOOL("read-only", cfg_false, CFGF_NONE),
|
2020-03-24 02:01:50 +08:00
|
|
|
CFG_BOOL("hidden", cfg_false, CFGF_NONE),
|
|
|
|
CFG_BOOL("no-automount", cfg_false, CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_STR("image", NULL, CFGF_NONE),
|
2021-10-22 21:11:19 +08:00
|
|
|
CFG_STR_LIST("holes", NULL, CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_BOOL("autoresize", 0, CFGF_NONE),
|
2011-10-10 22:38:22 +08:00
|
|
|
CFG_BOOL("in-partition-table", cfg_true, CFGF_NONE),
|
2019-01-16 17:12:04 +08:00
|
|
|
CFG_STR("partition-uuid", NULL, CFGF_NONE),
|
2021-02-12 19:48:57 +08:00
|
|
|
CFG_STR("partition-type-uuid", NULL, CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static cfg_opt_t image_common_opts[] = {
|
|
|
|
CFG_STR("name", NULL, CFGF_NONE),
|
|
|
|
CFG_STR("size", NULL, CFGF_NONE),
|
|
|
|
CFG_STR("mountpoint", NULL, CFGF_NONE),
|
2022-03-19 00:26:27 +08:00
|
|
|
CFG_STR("srcpath", NULL, CFGF_NONE),
|
2019-09-21 18:20:03 +08:00
|
|
|
CFG_BOOL("empty", cfg_false, CFGF_NONE),
|
2020-11-17 22:15:18 +08:00
|
|
|
CFG_BOOL("temporary", cfg_false, CFGF_NONE),
|
2011-10-10 22:38:28 +08:00
|
|
|
CFG_STR("exec-pre", NULL, CFGF_NONE),
|
|
|
|
CFG_STR("exec-post", NULL, CFGF_NONE),
|
2011-06-27 14:39:51 +08:00
|
|
|
CFG_STR("flashtype", NULL, CFGF_NONE),
|
|
|
|
CFG_SEC("partition", partition_opts, CFGF_MULTI | CFGF_TITLE),
|
2015-03-16 18:57:24 +08:00
|
|
|
CFG_FUNC("include", &cfg_include),
|
2011-06-27 14:39:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static cfg_opt_t flashchip_opts[] = {
|
|
|
|
CFG_STR("pebsize", "", CFGF_NONE),
|
|
|
|
CFG_STR("lebsize", "", CFGF_NONE),
|
|
|
|
CFG_STR("numpebs", "", CFGF_NONE),
|
|
|
|
CFG_STR("minimum-io-unit-size", "", CFGF_NONE),
|
|
|
|
CFG_STR("vid-header-offset", "", CFGF_NONE),
|
|
|
|
CFG_STR("sub-page-size", "", CFGF_NONE),
|
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(images);
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* find an image corresponding to a filename
|
|
|
|
*/
|
2011-06-27 14:39:51 +08:00
|
|
|
struct image *image_get(const char *filename)
|
|
|
|
{
|
|
|
|
struct image *image;
|
|
|
|
|
|
|
|
list_for_each_entry(image, &images, list) {
|
|
|
|
if (!strcmp(image->file, filename))
|
|
|
|
return image;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-06-21 15:46:00 +08:00
|
|
|
/*
|
|
|
|
* setup the images. Calls ->setup function for each
|
|
|
|
* image, recursively calls itself for resolving dependencies
|
|
|
|
*/
|
|
|
|
static int image_setup(struct image *image)
|
|
|
|
{
|
2020-08-04 01:23:23 +08:00
|
|
|
int ret = 0;
|
2012-06-21 15:46:00 +08:00
|
|
|
struct partition *part;
|
|
|
|
|
|
|
|
if (image->done < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (image->seen < 0) {
|
|
|
|
image_error(image, "recursive dependency detected\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
image->seen = -1;
|
|
|
|
|
2019-05-06 14:58:42 +08:00
|
|
|
if (image->size_is_percent) {
|
|
|
|
image->size = image_dir_size(image) * image->size / 100;
|
|
|
|
image->size_is_percent = cfg_false;
|
|
|
|
}
|
|
|
|
|
2012-06-21 15:46:00 +08:00
|
|
|
list_for_each_entry(part, &image->partitions, list) {
|
|
|
|
struct image *child;
|
|
|
|
if (!part->image)
|
|
|
|
continue;
|
|
|
|
child = image_get(part->image);
|
|
|
|
if (!child) {
|
|
|
|
image_error(image, "could not find %s\n", part->image);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
ret = image_setup(child);
|
|
|
|
if (ret) {
|
|
|
|
image_error(image, "could not setup %s\n", child->file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (image->handler->setup)
|
|
|
|
ret = image->handler->setup(image, image->imagesec);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
image->done = -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/*
|
|
|
|
* generate the images. Calls ->generate function for each
|
|
|
|
* image, recursively calls itself for resolving dependencies
|
|
|
|
*/
|
2011-06-27 14:39:51 +08:00
|
|
|
static int image_generate(struct image *image)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
struct partition *part;
|
|
|
|
|
2012-06-21 15:46:00 +08:00
|
|
|
if (image->done > 0)
|
2011-06-27 14:39:51 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-06-21 15:46:00 +08:00
|
|
|
if (image->seen > 0) {
|
2011-06-27 16:06:38 +08:00
|
|
|
image_error(image, "recursive dependency detected\n");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
image->seen = 1;
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
list_for_each_entry(part, &image->partitions, list) {
|
|
|
|
struct image *child;
|
2011-11-06 06:27:47 +08:00
|
|
|
if (!part->image)
|
|
|
|
continue;
|
2011-06-27 14:39:51 +08:00
|
|
|
child = image_get(part->image);
|
|
|
|
if (!child) {
|
|
|
|
image_error(image, "could not find %s\n", part->image);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
ret = image_generate(child);
|
|
|
|
if (ret) {
|
|
|
|
image_error(image, "could not generate %s\n", child->file);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-10 22:38:28 +08:00
|
|
|
if (image->exec_pre) {
|
|
|
|
ret = systemp(image, "%s", image->exec_pre);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
if (image->handler->generate) {
|
|
|
|
ret = image->handler->generate(image);
|
|
|
|
} else {
|
2014-04-24 04:18:35 +08:00
|
|
|
image_error(image, "no generate function for %s\n", image->file);
|
2011-06-27 14:39:51 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2012-09-21 16:22:39 +08:00
|
|
|
if (ret) {
|
2018-12-17 20:46:01 +08:00
|
|
|
struct stat s;
|
|
|
|
if (lstat(imageoutfile(image), &s) != 0 ||
|
|
|
|
((s.st_mode & S_IFMT) == S_IFREG) ||
|
|
|
|
((s.st_mode & S_IFMT) == S_IFLNK))
|
|
|
|
systemp(image, "rm -f \"%s\"", imageoutfile(image));
|
2011-06-27 14:39:51 +08:00
|
|
|
return ret;
|
2012-09-21 16:22:39 +08:00
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2011-10-10 22:38:28 +08:00
|
|
|
if (image->exec_post) {
|
|
|
|
ret = systemp(image, "%s", image->exec_post);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
image->done = 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LIST_HEAD(flashlist);
|
|
|
|
|
2011-06-27 15:48:18 +08:00
|
|
|
static int parse_flashes(cfg_t *cfg)
|
2011-06-27 14:39:51 +08:00
|
|
|
{
|
|
|
|
int num_flashes;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
num_flashes = cfg_size(cfg, "flash");
|
|
|
|
|
|
|
|
for (i = 0; i < num_flashes; i++) {
|
|
|
|
cfg_t *flashsec = cfg_getnsec(cfg, "flash", i);
|
|
|
|
struct flash_type *flash = xzalloc(sizeof *flash);
|
|
|
|
|
|
|
|
flash->name = cfg_title(flashsec);
|
|
|
|
flash->pebsize = cfg_getint_suffix(flashsec, "pebsize");
|
|
|
|
flash->lebsize = cfg_getint_suffix(flashsec, "lebsize");
|
|
|
|
flash->numpebs = cfg_getint_suffix(flashsec, "numpebs");
|
|
|
|
flash->minimum_io_unit_size = cfg_getint_suffix(flashsec, "minimum-io-unit-size");
|
|
|
|
flash->vid_header_offset = cfg_getint_suffix(flashsec, "vid-header-offset");
|
|
|
|
flash->sub_page_size = cfg_getint_suffix(flashsec, "sub-page-size");
|
|
|
|
list_add_tail(&flash->list, &flashlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct flash_type *flash_type_get(const char *name)
|
|
|
|
{
|
|
|
|
struct flash_type *flash;
|
|
|
|
|
|
|
|
list_for_each_entry(flash, &flashlist, list) {
|
|
|
|
if (!strcmp(flash->name, name))
|
|
|
|
return flash;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_partitions(struct image *image, cfg_t *imagesec)
|
|
|
|
{
|
|
|
|
struct partition *part;
|
|
|
|
int num_partitions;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
num_partitions = cfg_size(imagesec, "partition");
|
|
|
|
|
|
|
|
for (i = 0; i < num_partitions; i++) {
|
|
|
|
cfg_t *partsec = cfg_getnsec(imagesec, "partition", i);
|
|
|
|
|
|
|
|
part = xzalloc(sizeof *part);
|
2021-10-22 21:11:19 +08:00
|
|
|
part->cfg = partsec;
|
2011-06-27 14:39:51 +08:00
|
|
|
part->name = cfg_title(partsec);
|
|
|
|
list_add_tail(&part->list, &image->partitions);
|
|
|
|
part->size = cfg_getint_suffix(partsec, "size");
|
|
|
|
part->offset = cfg_getint_suffix(partsec, "offset");
|
2021-01-11 05:54:22 +08:00
|
|
|
part->align = cfg_getint_suffix(partsec, "align");
|
2011-06-27 14:39:51 +08:00
|
|
|
part->partition_type = cfg_getint(partsec, "partition-type");
|
2011-10-10 22:38:27 +08:00
|
|
|
part->bootable = cfg_getbool(partsec, "bootable");
|
2016-01-04 20:49:49 +08:00
|
|
|
part->read_only = cfg_getbool(partsec, "read-only");
|
2020-03-24 02:01:50 +08:00
|
|
|
part->hidden = cfg_getbool(partsec, "hidden");
|
|
|
|
part->no_automount = cfg_getbool(partsec, "no-automount");
|
2011-06-27 14:39:51 +08:00
|
|
|
part->image = cfg_getstr(partsec, "image");
|
|
|
|
part->autoresize = cfg_getbool(partsec, "autoresize");
|
2011-10-10 22:38:22 +08:00
|
|
|
part->in_partition_table = cfg_getbool(partsec, "in-partition-table");
|
2019-01-16 17:12:04 +08:00
|
|
|
part->partition_type_uuid = cfg_getstr(partsec, "partition-type-uuid");
|
|
|
|
part->partition_uuid = cfg_getstr(partsec, "partition-uuid");
|
2016-11-19 03:46:23 +08:00
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_flash_type(void)
|
|
|
|
{
|
|
|
|
struct image *image;
|
|
|
|
|
|
|
|
list_for_each_entry(image, &images, list) {
|
|
|
|
struct partition *part;
|
|
|
|
if (!image->flash_type)
|
|
|
|
continue;
|
|
|
|
list_for_each_entry(part, &image->partitions, list) {
|
|
|
|
struct image *i;
|
2015-03-27 11:08:04 +08:00
|
|
|
if (!part->image)
|
|
|
|
continue;
|
2011-06-27 14:39:51 +08:00
|
|
|
i = image_get(part->image);
|
|
|
|
if (!i)
|
|
|
|
return -EINVAL;
|
|
|
|
if (i->flash_type) {
|
|
|
|
if (i->flash_type != image->flash_type) {
|
2014-04-24 04:18:35 +08:00
|
|
|
image_error(i, "conflicting flash types: %s has flashtype %s whereas %s has flashtype %s\n",
|
2011-06-27 14:39:51 +08:00
|
|
|
i->file, i->flash_type->name, image->file, image->flash_type->name);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i->flash_type = image->flash_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static LIST_HEAD(mountpoints);
|
|
|
|
|
2012-06-17 16:56:32 +08:00
|
|
|
static struct mountpoint *get_mountpoint(const char *path)
|
2011-06-27 14:39:51 +08:00
|
|
|
{
|
|
|
|
struct mountpoint *mp;
|
|
|
|
|
|
|
|
list_for_each_entry(mp, &mountpoints, list) {
|
|
|
|
if (!strcmp(mp->path, path))
|
|
|
|
return mp;
|
|
|
|
}
|
2012-06-17 16:56:32 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-01-31 18:39:36 +08:00
|
|
|
char *sanitize_path(const char *path)
|
2017-12-04 21:47:37 +08:00
|
|
|
{
|
|
|
|
char *path_sanitized;
|
|
|
|
char *c;
|
|
|
|
|
|
|
|
path_sanitized = strdup(path);
|
|
|
|
c = path_sanitized;
|
|
|
|
|
|
|
|
while ((c = strchr(c, '/'))) {
|
|
|
|
*c = '-';
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path_sanitized;
|
|
|
|
}
|
|
|
|
|
2012-06-17 16:56:32 +08:00
|
|
|
static struct mountpoint *add_mountpoint(const char *path)
|
|
|
|
{
|
|
|
|
struct mountpoint *mp;
|
2017-12-04 21:47:37 +08:00
|
|
|
char *path_sanitized;
|
2012-06-17 16:56:32 +08:00
|
|
|
|
|
|
|
mp = get_mountpoint(path);
|
|
|
|
if (mp)
|
|
|
|
return mp;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2017-12-04 21:47:37 +08:00
|
|
|
path_sanitized = sanitize_path(path);
|
2011-06-27 14:39:51 +08:00
|
|
|
mp = xzalloc(sizeof(*mp));
|
|
|
|
mp->path = strdup(path);
|
2019-08-07 13:35:20 +08:00
|
|
|
xasprintf(&mp->mountpath, "%s/mp-%s", tmppath(), path_sanitized);
|
2011-06-27 14:39:51 +08:00
|
|
|
list_add_tail(&mp->list, &mountpoints);
|
2017-12-04 21:47:37 +08:00
|
|
|
free(path_sanitized);
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
return mp;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_root_mountpoint(void)
|
|
|
|
{
|
|
|
|
struct mountpoint *mp;
|
|
|
|
|
|
|
|
mp = xzalloc(sizeof(*mp));
|
|
|
|
mp->path = strdup("");
|
2017-11-12 01:02:34 +08:00
|
|
|
xasprintf(&mp->mountpath, "%s/root", tmppath());
|
2011-06-27 14:39:51 +08:00
|
|
|
list_add_tail(&mp->list, &mountpoints);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int collect_mountpoints(void)
|
|
|
|
{
|
|
|
|
struct image *image;
|
|
|
|
struct mountpoint *mp;
|
2021-12-21 00:07:27 +08:00
|
|
|
int ret, need_mtime_fixup = 0;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
add_root_mountpoint();
|
|
|
|
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "mkdir -p \"%s\"", tmppath());
|
2011-06-27 14:39:51 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "cp -a \"%s\" \"%s/root\"", rootpath(), tmppath());
|
2011-06-27 14:39:51 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
list_for_each_entry(image, &images, list) {
|
|
|
|
if (image->mountpoint)
|
|
|
|
image->mp = add_mountpoint(image->mountpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
list_for_each_entry(mp, &mountpoints, list) {
|
|
|
|
if (!strlen(mp->path))
|
|
|
|
continue;
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "mv \"%s/root/%s\" \"%s\"", tmppath(), mp->path, mp->mountpath);
|
2011-06-27 14:39:51 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "mkdir \"%s/root/%s\"", tmppath(), mp->path);
|
2011-10-10 22:38:23 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "chmod --reference=\"%s\" \"%s/root/%s\"", mp->mountpath, tmppath(), mp->path);
|
2011-10-10 22:38:23 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "chown --reference=\"%s\" \"%s/root/%s\"", mp->mountpath, tmppath(), mp->path);
|
2011-10-10 22:38:23 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2021-12-21 00:07:27 +08:00
|
|
|
need_mtime_fixup = 1;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
2018-07-19 17:43:29 +08:00
|
|
|
/*
|
|
|
|
* After the mv/mkdir of the mountpoints the timestamps of the
|
|
|
|
* mountpoint and all parent dirs are changed. Fix that here.
|
|
|
|
*/
|
2021-12-21 00:07:27 +08:00
|
|
|
if (need_mtime_fixup)
|
|
|
|
ret = systemp(NULL,
|
|
|
|
"find '%s/root' -depth -type d -printf '%%P\\0' | xargs -0 -I {} touch -r '%s/{}' '%s/root/{}'",
|
|
|
|
tmppath(), rootpath(), tmppath());
|
2018-07-19 17:43:29 +08:00
|
|
|
|
2021-12-21 00:07:27 +08:00
|
|
|
return ret;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
2020-08-28 21:36:48 +08:00
|
|
|
const char *mountpath(const struct image *image)
|
2012-06-17 16:56:32 +08:00
|
|
|
{
|
2022-03-19 00:26:27 +08:00
|
|
|
if(image->srcpath)
|
|
|
|
return image->srcpath;
|
|
|
|
|
2012-06-17 16:56:32 +08:00
|
|
|
struct mountpoint *mp;
|
|
|
|
|
|
|
|
mp = image->mp;
|
|
|
|
if (!mp)
|
|
|
|
mp = get_mountpoint("");
|
|
|
|
|
|
|
|
return mp->mountpath;
|
|
|
|
}
|
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
static int tmppath_generated;
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
static void check_tmp_path(void)
|
|
|
|
{
|
|
|
|
const char *tmp = tmppath();
|
|
|
|
int ret;
|
|
|
|
DIR *dir;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (!tmp) {
|
2013-05-03 02:49:14 +08:00
|
|
|
error("tmppath not set. aborting\n");
|
2011-06-27 14:39:51 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = opendir(tmp);
|
|
|
|
if (!dir) {
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "mkdir -p \"%s\"", tmppath());
|
2011-06-27 14:39:51 +08:00
|
|
|
if (ret)
|
|
|
|
exit(1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
if (!readdir(dir))
|
|
|
|
break;
|
|
|
|
i++;
|
|
|
|
if (i > 2) {
|
2013-05-03 02:49:14 +08:00
|
|
|
error("tmppath '%s' exists and is not empty\n", tmp);
|
2011-06-27 14:39:51 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2011-06-27 15:38:33 +08:00
|
|
|
tmppath_generated = 1;
|
2015-10-12 00:19:54 +08:00
|
|
|
closedir(dir);
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void cleanup(void)
|
|
|
|
{
|
2011-06-27 15:38:33 +08:00
|
|
|
if (tmppath_generated)
|
2018-02-15 21:02:46 +08:00
|
|
|
systemp(NULL, "rm -rf \"%s\"/*", tmppath());
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static cfg_opt_t top_opts[] = {
|
|
|
|
CFG_SEC("image", NULL, CFGF_MULTI | CFGF_TITLE),
|
|
|
|
CFG_SEC("flash", flashchip_opts, CFGF_MULTI | CFGF_TITLE),
|
|
|
|
CFG_SEC("config", NULL, CFGF_MULTI),
|
|
|
|
CFG_FUNC("include", &cfg_include),
|
|
|
|
CFG_END()
|
|
|
|
};
|
|
|
|
|
2015-09-06 04:05:12 +08:00
|
|
|
static int overwriteenv(const char *name, const char *value)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = setenv(name, value ? : "", 1);
|
|
|
|
if (ret)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int setenv_paths(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("OUTPUTPATH", imagepath());
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("INPUTPATH", inputpath());
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("ROOTPATH", rootpath());
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("TMPPATH", tmppath());
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int setenv_image(const struct image *image)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
char sizestr[20];
|
|
|
|
|
|
|
|
snprintf(sizestr, sizeof(sizestr), "%llu", image->size);
|
|
|
|
|
|
|
|
ret = overwriteenv("IMAGE", image->file);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("IMAGEOUTFILE", imageoutfile(image));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("IMAGENAME", image->name);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("IMAGESIZE", sizestr);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = overwriteenv("IMAGEMOUNTPOINT", image->mountpoint);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2020-08-28 21:31:26 +08:00
|
|
|
ret = overwriteenv("IMAGEMOUNTPATH", image->empty ? NULL : mountpath(image));
|
2017-12-04 21:51:53 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2015-09-06 04:05:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-03 06:10:35 +08:00
|
|
|
#ifdef HAVE_SEARCHPATH
|
|
|
|
static int add_searchpath(cfg_t *cfg, const char *dir)
|
|
|
|
{
|
|
|
|
if (cfg_add_searchpath(cfg, dir)) {
|
|
|
|
error("error adding %s to include search path", dir);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int set_include_path(cfg_t *cfg, const char *path)
|
|
|
|
{
|
|
|
|
char *dup, *s, *e;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
e = dup = strdup(path);
|
|
|
|
do {
|
|
|
|
s = e;
|
|
|
|
e = strchr(s, ':');
|
|
|
|
if (e)
|
|
|
|
*e++ = '\0';
|
|
|
|
ret = add_searchpath(cfg, s);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
} while (e);
|
|
|
|
/* Make sure current directory is always searched. */
|
|
|
|
ret = add_searchpath(cfg, ".");
|
|
|
|
out:
|
|
|
|
free(dup);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2011-06-30 01:58:10 +08:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int num_images;
|
2011-06-27 14:39:51 +08:00
|
|
|
int ret;
|
|
|
|
cfg_opt_t *imageopts = xzalloc((ARRAY_SIZE(image_common_opts) +
|
2016-11-17 10:33:43 +08:00
|
|
|
ARRAY_SIZE(handlers) + 1) * sizeof(cfg_opt_t));
|
2011-06-27 14:39:51 +08:00
|
|
|
int start;
|
|
|
|
struct image *image;
|
2021-06-03 06:10:35 +08:00
|
|
|
const char *str;
|
2011-06-27 14:39:51 +08:00
|
|
|
cfg_t *cfg;
|
|
|
|
struct partition *part;
|
|
|
|
|
|
|
|
cfg_opt_t image_end[] = {
|
|
|
|
CFG_END()
|
|
|
|
};
|
2019-01-16 17:12:04 +08:00
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
/* Seed the rng */
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
srandom(tv.tv_usec);
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
memcpy(imageopts, image_common_opts, sizeof(image_common_opts));
|
|
|
|
|
|
|
|
start = ARRAY_SIZE(image_common_opts);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(handlers); i++) {
|
|
|
|
struct image_handler *handler = handlers[i];
|
|
|
|
cfg_opt_t image_tmp[] = {
|
|
|
|
CFG_SEC("dummy", NULL, CFGF_MULTI),
|
|
|
|
};
|
|
|
|
|
|
|
|
image_tmp[0].name = handler->type;
|
|
|
|
image_tmp[0].subopts = handler->opts;
|
2016-11-19 03:46:23 +08:00
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
memcpy(&imageopts[start + i], image_tmp, sizeof(cfg_opt_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&imageopts[start + i], &image_end[0], sizeof(cfg_opt_t));
|
|
|
|
|
|
|
|
top_opts[0].subopts = imageopts;
|
|
|
|
|
|
|
|
init_config();
|
2011-06-27 15:38:33 +08:00
|
|
|
|
|
|
|
top_opts[2].subopts = get_confuse_opts();
|
|
|
|
|
|
|
|
/* call set_config_opts to make get_opt("config") work */
|
|
|
|
set_config_opts(argc, argv, NULL);
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
cfg = cfg_init(top_opts, CFGF_NONE);
|
2021-06-03 06:10:35 +08:00
|
|
|
str = get_opt("includepath");
|
|
|
|
if (str) {
|
|
|
|
#ifdef HAVE_SEARCHPATH
|
|
|
|
ret = set_include_path(cfg, str);
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
#else
|
|
|
|
error("--includepath used, but genimage built with too old libconfuse\n");
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
#endif
|
|
|
|
}
|
2011-06-27 15:38:33 +08:00
|
|
|
|
|
|
|
ret = cfg_parse(cfg, get_opt("config"));
|
|
|
|
switch (ret) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case CFG_PARSE_ERROR:
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-27 15:38:33 +08:00
|
|
|
case CFG_FILE_ERROR:
|
|
|
|
error("could not open config file '%s'\n", get_opt("config"));
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-27 15:38:33 +08:00
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2011-06-27 15:38:33 +08:00
|
|
|
/* again, with config file this time */
|
2011-06-27 14:39:51 +08:00
|
|
|
set_config_opts(argc, argv, cfg);
|
|
|
|
|
2021-12-20 17:01:35 +08:00
|
|
|
str = get_opt("configdump");
|
|
|
|
if (str) {
|
|
|
|
FILE *dump;
|
|
|
|
|
|
|
|
dump = (strcmp(str, "-")) ? fopen(str, "w") : stdout;
|
|
|
|
if (!dump) {
|
|
|
|
error("could not open dump file %s: %s", str, strerror(errno));
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
cfg_print(cfg, dump);
|
|
|
|
if (dump != stdout)
|
|
|
|
fclose(dump);
|
|
|
|
}
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
check_tmp_path();
|
|
|
|
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "rm -rf \"%s\"/*", tmppath());
|
2011-06-27 14:39:51 +08:00
|
|
|
if (ret)
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
parse_flashes(cfg);
|
|
|
|
|
|
|
|
num_images = cfg_size(cfg, "image");
|
|
|
|
|
|
|
|
for (i = 0; i < num_images; i++) {
|
|
|
|
cfg_t *imagesec = cfg_getnsec(cfg, "image", i);
|
|
|
|
image = xzalloc(sizeof *image);
|
|
|
|
INIT_LIST_HEAD(&image->partitions);
|
|
|
|
list_add_tail(&image->list, &images);
|
|
|
|
image->file = cfg_title(imagesec);
|
|
|
|
image->name = cfg_getstr(imagesec, "name");
|
2019-05-06 14:58:42 +08:00
|
|
|
image->size = cfg_getint_suffix_percent(imagesec, "size",
|
|
|
|
&image->size_is_percent);
|
2022-03-19 00:26:27 +08:00
|
|
|
image->srcpath = cfg_getstr(imagesec, "srcpath");
|
2011-06-27 14:39:51 +08:00
|
|
|
image->mountpoint = cfg_getstr(imagesec, "mountpoint");
|
2019-09-21 18:20:03 +08:00
|
|
|
image->empty = cfg_getbool(imagesec, "empty");
|
2020-11-17 22:15:18 +08:00
|
|
|
image->temporary = cfg_getbool(imagesec, "temporary");
|
2011-10-10 22:38:28 +08:00
|
|
|
image->exec_pre = cfg_getstr(imagesec, "exec-pre");
|
|
|
|
image->exec_post = cfg_getstr(imagesec, "exec-post");
|
2018-12-17 20:47:03 +08:00
|
|
|
if (image->file[0] == '/')
|
|
|
|
image->outfile = strdup(image->file);
|
|
|
|
else
|
2020-11-17 22:15:18 +08:00
|
|
|
xasprintf(&image->outfile, "%s/%s",
|
|
|
|
image->temporary ? tmppath() : imagepath(),
|
2018-12-17 20:47:03 +08:00
|
|
|
image->file);
|
2011-06-27 14:39:51 +08:00
|
|
|
if (image->mountpoint && *image->mountpoint == '/')
|
|
|
|
image->mountpoint++;
|
2022-03-19 00:26:27 +08:00
|
|
|
if (image->srcpath && image->mountpoint && (strlen(image->mountpoint) > 0)) {
|
|
|
|
image_error(image, "Cannot specify both srcpath and mountpoint at the same time.");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
str = cfg_getstr(imagesec, "flashtype");
|
|
|
|
if (str)
|
|
|
|
image->flash_type = flash_type_get(str);
|
2011-06-27 16:07:56 +08:00
|
|
|
image_set_handler(image, imagesec);
|
2011-06-27 14:39:51 +08:00
|
|
|
parse_partitions(image, imagesec);
|
2012-06-26 14:32:28 +08:00
|
|
|
if (image->handler->parse) {
|
|
|
|
ret = image->handler->parse(image, image->imagesec);
|
|
|
|
if (ret)
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2012-06-26 14:32:28 +08:00
|
|
|
}
|
2016-11-19 03:46:23 +08:00
|
|
|
}
|
2011-06-27 14:39:51 +08:00
|
|
|
|
|
|
|
/* check if each partition has a corresponding image */
|
|
|
|
list_for_each_entry(image, &images, list) {
|
|
|
|
list_for_each_entry(part, &image->partitions, list) {
|
2012-06-21 15:48:36 +08:00
|
|
|
struct image *child;
|
2011-10-11 15:22:43 +08:00
|
|
|
|
|
|
|
if (!part->image) {
|
2011-11-06 06:27:47 +08:00
|
|
|
if (part->in_partition_table)
|
|
|
|
continue;
|
2011-10-11 15:22:43 +08:00
|
|
|
image_error(image, "no input file given\n");
|
2015-09-06 04:05:10 +08:00
|
|
|
ret = -EINVAL;
|
|
|
|
goto cleanup;
|
2011-10-11 15:22:43 +08:00
|
|
|
}
|
|
|
|
|
2012-06-21 15:48:36 +08:00
|
|
|
child = image_get(part->image);
|
2021-10-22 21:11:19 +08:00
|
|
|
if (child) {
|
|
|
|
if (cfg_size(part->cfg, "holes") > 0) {
|
|
|
|
image_error(image, "holes in partitions are only valid for implicit child images!\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-06-21 15:48:36 +08:00
|
|
|
continue;
|
2021-10-22 21:11:19 +08:00
|
|
|
}
|
2018-02-14 22:54:14 +08:00
|
|
|
image_debug(image, "adding implicit file rule for '%s'\n", part->image);
|
2012-06-21 15:48:36 +08:00
|
|
|
child = xzalloc(sizeof *image);
|
|
|
|
INIT_LIST_HEAD(&child->partitions);
|
|
|
|
list_add_tail(&child->list, &images);
|
|
|
|
child->file = part->image;
|
|
|
|
child->handler = &file_handler;
|
2021-10-22 21:11:19 +08:00
|
|
|
parse_holes(child, part->cfg);
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* propagate flash types to partitions */
|
|
|
|
ret = set_flash_type();
|
|
|
|
if (ret)
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2019-05-06 14:58:42 +08:00
|
|
|
ret = collect_mountpoints();
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
list_for_each_entry(image, &images, list) {
|
2012-06-21 15:46:00 +08:00
|
|
|
ret = image_setup(image);
|
|
|
|
if (ret)
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
2015-09-06 04:05:12 +08:00
|
|
|
|
|
|
|
ret = setenv_paths();
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
2011-06-27 14:39:51 +08:00
|
|
|
|
2018-02-15 21:02:46 +08:00
|
|
|
ret = systemp(NULL, "mkdir -p \"%s\"", imagepath());
|
2011-06-30 01:58:09 +08:00
|
|
|
if (ret)
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-30 01:58:09 +08:00
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
list_for_each_entry(image, &images, list) {
|
2015-09-06 04:05:12 +08:00
|
|
|
ret = setenv_image(image);
|
|
|
|
if (ret)
|
|
|
|
goto cleanup;
|
|
|
|
|
2011-06-27 14:39:51 +08:00
|
|
|
ret = image_generate(image);
|
|
|
|
if (ret) {
|
2014-04-24 04:18:35 +08:00
|
|
|
image_error(image, "failed to generate %s\n", image->file);
|
2015-09-06 04:05:10 +08:00
|
|
|
goto cleanup;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-06 04:05:10 +08:00
|
|
|
cleanup:
|
2011-06-27 14:39:51 +08:00
|
|
|
cleanup();
|
2015-09-06 04:05:10 +08:00
|
|
|
return ret ? 1 : 0;
|
2011-06-27 14:39:51 +08:00
|
|
|
}
|