barebox/scripts/bareboximd.c
Sascha Hauer 97e81f2d78 Add support for metadata in barebox images
It's often useful to get some information about a barebox image
before starting or flashing it. This patch introduces barebox
Image MetaData (IMD). When enabled a barebox image will contain
a list of tags containing the desired information. We have tags
for:

- the barebox release (2014.07.0-00160-g035de50-dirty)
- the build timestamp (#741 Mon Jul 28 15:08:54 CEST 2014)
- the board model the image is intended for
- the device tree toplevel compatible property

Also there is an additional generic key-value store which stores
parameters for which no dedicated tag exists. In this patch it
is used for the memory size an image supports.

Since there is no fixed offset in a barebox image which can be
used for storing the information, the metadata is stored somewhere
in the image and found by iterating over the image. This works
for most image types, but obviously not for SoC images which are
encoded or encrypted in some way.

There is a 'imd' tool compiled from the same sources for barebox,
for the compile host and for the target, so the metadata information
is available whereever needed.

For device tree boards the model and of_compatible tags are automatically
generated.

Example output of the imd tool for a Phytec phyFLEX image:

build: #889 Wed Jul 30 16:08:54 CEST 2014
release: 2014.07.0-00167-g6b2070d-dirty
parameter: memsize=1024
of_compatible: phytec,imx6x-pbab01 phytec,imx6dl-pfla02 fsl,imx6dl
model: Phytec phyFLEX-i.MX6 Duallite Carrier-Board

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
2014-08-07 06:13:52 +02:00

162 lines
3.3 KiB
C

/*
* (C) Copyright 2014 Sascha Hauer, Pengutronix
*
* See file CREDITS for list of people who contributed to this
* project.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* 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.
*
*/
#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask))
#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#include <stdio.h>
#include <sys/types.h>
#include <stdint.h>
#include <asm-generic/errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include "../include/image-metadata.h"
static void debug(const char *fmt, ...)
{
va_list ap;
if (!imd_command_verbose)
return;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
}
int imd_command_setenv(const char *variable_name, const char *value)
{
fprintf(stderr, "-s option ignored\n");
return -EINVAL;
}
static int read_file_2(const char *filename, size_t *size, void **outbuf, loff_t max_size)
{
off_t fsize;
ssize_t rsize;
int ret, fd;
void *buf;
*size = 0;
*outbuf = NULL;
fd = open(filename, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
return -errno;
}
fsize = lseek(fd, 0, SEEK_END);
if (fsize == -1) {
fprintf(stderr, "Cannot get size %s: %s\n", filename, strerror(errno));
ret = -errno;
goto close;
}
if (fsize < max_size)
max_size = fsize;
if (lseek(fd, 0, SEEK_SET) == -1) {
fprintf(stderr, "Cannot seek to start %s: %s\n", filename, strerror(errno));
ret = -errno;
goto close;
}
buf = malloc(max_size);
if (!buf) {
fprintf(stderr, "Cannot allocate memory\n");
ret = -ENOMEM;
goto close;
}
*outbuf = buf;
while (*size < max_size) {
rsize = read(fd, buf, max_size-*size);
if (rsize == 0) {
ret = -EIO;
goto free;
} else if (rsize < 0) {
if (errno == EAGAIN)
continue;
else {
ret = -errno;
goto free;
}
} /* ret > 0 */
buf += rsize;
*size += rsize;
}
ret = 0;
goto close;
free:
*outbuf = NULL;
free(buf);
close:
close(fd);
return ret;
}
static unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
{
return strtoul(cp, endp, base);
}
#include "../common/imd.c"
static void usage(const char *prgname)
{
printf(
"Extract metadata from a barebox image\n"
"\n"
"Usage: %s [OPTIONS] FILE\n"
"Options:\n"
"-t <type> only show information of <type>\n"
"-n <no> for tags with multiple strings only show string <no>\n"
"\n"
"Without options all information available is printed. Valid types are:\n"
"release, build, model, of_compatible\n",
prgname);
}
int main(int argc, char *argv[])
{
int ret;
ret = imd_command(argc, argv);
if (ret == -ENOSYS) {
usage(argv[0]);
exit(1);
}
if (ret)
fprintf(stderr, "%s\n", strerror(-ret));
return ret ? 1 : 0;
}