2013-12-01 19:43:10 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2013
|
|
|
|
*
|
|
|
|
* Written by Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _IMAGETOOL_H_
|
|
|
|
#define _IMAGETOOL_H_
|
|
|
|
|
|
|
|
#include "os_support.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2014-06-12 20:27:12 +00:00
|
|
|
#include <u-boot/sha1.h>
|
2015-01-15 04:48:07 +00:00
|
|
|
|
2013-12-01 19:43:10 +00:00
|
|
|
#include "fdt_host.h"
|
|
|
|
|
|
|
|
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
|
|
|
|
#define IH_ARCH_DEFAULT IH_ARCH_INVALID
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This structure defines all such variables those are initialized by
|
|
|
|
* mkimage and dumpimage main core and need to be referred by image
|
|
|
|
* type specific functions
|
|
|
|
*/
|
|
|
|
struct image_tool_params {
|
|
|
|
int dflag;
|
|
|
|
int eflag;
|
|
|
|
int fflag;
|
2013-12-01 19:43:11 +00:00
|
|
|
int iflag;
|
2013-12-01 19:43:10 +00:00
|
|
|
int lflag;
|
2013-12-01 19:43:11 +00:00
|
|
|
int pflag;
|
2013-12-01 19:43:10 +00:00
|
|
|
int vflag;
|
|
|
|
int xflag;
|
|
|
|
int skipcpy;
|
|
|
|
int os;
|
|
|
|
int arch;
|
|
|
|
int type;
|
|
|
|
int comp;
|
|
|
|
char *dtc;
|
|
|
|
unsigned int addr;
|
|
|
|
unsigned int ep;
|
|
|
|
char *imagename;
|
|
|
|
char *imagename2;
|
|
|
|
char *datafile;
|
|
|
|
char *imagefile;
|
|
|
|
char *cmdname;
|
2013-12-01 19:43:11 +00:00
|
|
|
const char *outfile; /* Output filename */
|
2013-12-01 19:43:10 +00:00
|
|
|
const char *keydir; /* Directory holding private keys */
|
|
|
|
const char *keydest; /* Destination .dtb for public key */
|
|
|
|
const char *comment; /* Comment to add to signature node */
|
|
|
|
int require_keys; /* 1 to mark signing keys as 'required' */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* image type specific variables and callback functions
|
|
|
|
*/
|
|
|
|
struct image_type_params {
|
|
|
|
/* name is an identification tag string for added support */
|
|
|
|
char *name;
|
|
|
|
/*
|
|
|
|
* header size is local to the specific image type to be supported,
|
|
|
|
* mkimage core treats this as number of bytes
|
|
|
|
*/
|
|
|
|
uint32_t header_size;
|
|
|
|
/* Image type header pointer */
|
|
|
|
void *hdr;
|
|
|
|
/*
|
|
|
|
* There are several arguments that are passed on the command line
|
|
|
|
* and are registered as flags in image_tool_params structure.
|
|
|
|
* This callback function can be used to check the passed arguments
|
|
|
|
* are in-lined with the image type to be supported
|
|
|
|
*
|
|
|
|
* Returns 1 if parameter check is successful
|
|
|
|
*/
|
|
|
|
int (*check_params) (struct image_tool_params *);
|
|
|
|
/*
|
|
|
|
* This function is used by list command (i.e. mkimage -l <filename>)
|
|
|
|
* image type verification code must be put here
|
|
|
|
*
|
|
|
|
* Returns 0 if image header verification is successful
|
|
|
|
* otherwise, returns respective negative error codes
|
|
|
|
*/
|
|
|
|
int (*verify_header) (unsigned char *, int, struct image_tool_params *);
|
|
|
|
/* Prints image information abstracting from image header */
|
|
|
|
void (*print_header) (const void *);
|
|
|
|
/*
|
|
|
|
* The header or image contents need to be set as per image type to
|
|
|
|
* be generated using this callback function.
|
|
|
|
* further output file post processing (for ex. checksum calculation,
|
|
|
|
* padding bytes etc..) can also be done in this callback function.
|
|
|
|
*/
|
|
|
|
void (*set_header) (void *, struct stat *, int,
|
|
|
|
struct image_tool_params *);
|
2013-12-01 19:43:11 +00:00
|
|
|
/*
|
2015-01-15 04:54:41 +00:00
|
|
|
* This function is used by the command to retrieve a component
|
|
|
|
* (sub-image) from the image (i.e. dumpimage -i <image> -p <position>
|
|
|
|
* <sub-image-name>).
|
2013-12-01 19:43:11 +00:00
|
|
|
* Thus the code to extract a file from an image must be put here.
|
|
|
|
*
|
|
|
|
* Returns 0 if the file was successfully retrieved from the image,
|
|
|
|
* or a negative value on error.
|
|
|
|
*/
|
2015-01-15 04:54:41 +00:00
|
|
|
int (*extract_subimage)(void *, struct image_tool_params *);
|
2013-12-01 19:43:10 +00:00
|
|
|
/*
|
|
|
|
* Some image generation support for ex (default image type) supports
|
|
|
|
* more than one type_ids, this callback function is used to check
|
|
|
|
* whether input (-T <image_type>) is supported by registered image
|
|
|
|
* generation/list low level code
|
|
|
|
*/
|
|
|
|
int (*check_image_type) (uint8_t);
|
|
|
|
/* This callback function will be executed if fflag is defined */
|
|
|
|
int (*fflag_handle) (struct image_tool_params *);
|
|
|
|
/*
|
|
|
|
* This callback function will be executed for variable size record
|
|
|
|
* It is expected to build this header in memory and return its length
|
|
|
|
* and a pointer to it by using image_type_params.header_size and
|
|
|
|
* image_type_params.hdr. The return value shall indicate if an
|
|
|
|
* additional padding should be used when copying the data image
|
|
|
|
* by returning the padding length.
|
|
|
|
*/
|
|
|
|
int (*vrec_header) (struct image_tool_params *,
|
|
|
|
struct image_type_params *);
|
|
|
|
};
|
|
|
|
|
2015-01-15 04:48:05 +00:00
|
|
|
/**
|
|
|
|
* imagetool_get_type() - find the image type params for a given image type
|
|
|
|
*
|
|
|
|
* It scans all registers image type supports
|
|
|
|
* checks the input type for each supported image type
|
|
|
|
*
|
|
|
|
* if successful,
|
|
|
|
* returns respective image_type_params pointer if success
|
|
|
|
* if input type_id is not supported by any of image_type_support
|
|
|
|
* returns NULL
|
|
|
|
*/
|
2015-01-15 04:48:07 +00:00
|
|
|
struct image_type_params *imagetool_get_type(int type);
|
2015-01-15 04:48:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* imagetool_verify_print_header() - verifies the image header
|
|
|
|
*
|
|
|
|
* Scan registered image types and verify the image_header for each
|
|
|
|
* supported image type. If verification is successful, this prints
|
|
|
|
* the respective header.
|
|
|
|
*
|
|
|
|
* @return 0 on success, negative if input image format does not match with
|
|
|
|
* any of supported image types
|
|
|
|
*/
|
|
|
|
int imagetool_verify_print_header(
|
|
|
|
void *ptr,
|
|
|
|
struct stat *sbuf,
|
|
|
|
struct image_type_params *tparams,
|
|
|
|
struct image_tool_params *params);
|
|
|
|
|
2015-01-15 04:48:06 +00:00
|
|
|
/**
|
2015-01-15 04:54:41 +00:00
|
|
|
* imagetool_save_subimage - store data into a file
|
2015-01-15 04:48:06 +00:00
|
|
|
* @file_name: name of the destination file
|
|
|
|
* @file_data: data to be written
|
|
|
|
* @file_len: the amount of data to store
|
|
|
|
*
|
2015-01-15 04:54:41 +00:00
|
|
|
* imagetool_save_subimage() store file_len bytes of data pointed by file_data
|
2015-01-15 04:48:06 +00:00
|
|
|
* into the file name by file_name.
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* zero in case of success or a negative value if fail.
|
|
|
|
*/
|
2015-01-15 04:54:41 +00:00
|
|
|
int imagetool_save_subimage(
|
2015-01-15 04:48:06 +00:00
|
|
|
const char *file_name,
|
|
|
|
ulong file_data,
|
|
|
|
ulong file_len);
|
|
|
|
|
2013-12-01 19:43:10 +00:00
|
|
|
/*
|
|
|
|
* There is a c file associated with supported image type low level code
|
|
|
|
* for ex. default_image.c, fit_image.c
|
|
|
|
*/
|
2015-01-15 04:48:07 +00:00
|
|
|
|
2013-12-01 19:43:10 +00:00
|
|
|
|
|
|
|
void pbl_load_uboot(int fd, struct image_tool_params *mparams);
|
|
|
|
|
2015-02-08 23:06:10 +00:00
|
|
|
#define ___cat(a, b) a ## b
|
|
|
|
#define __cat(a, b) ___cat(a, b)
|
|
|
|
|
|
|
|
/* we need some special handling for this host tool running eventually on
|
|
|
|
* Darwin. The Mach-O section handling is a bit different than ELF section
|
|
|
|
* handling. The differnces in detail are:
|
|
|
|
* a) we have segments which have sections
|
|
|
|
* b) we need a API call to get the respective section symbols */
|
|
|
|
#if defined(__MACH__)
|
|
|
|
#include <mach-o/getsect.h>
|
|
|
|
|
|
|
|
#define INIT_SECTION(name) do { \
|
|
|
|
unsigned long name ## _len; \
|
|
|
|
char *__cat(pstart_, name) = getsectdata("__TEXT", \
|
|
|
|
#name, &__cat(name, _len)); \
|
|
|
|
char *__cat(pstop_, name) = __cat(pstart_, name) + \
|
|
|
|
__cat(name, _len); \
|
|
|
|
__cat(__start_, name) = (void *)__cat(pstart_, name); \
|
|
|
|
__cat(__stop_, name) = (void *)__cat(pstop_, name); \
|
|
|
|
} while (0)
|
|
|
|
#define SECTION(name) __attribute__((section("__TEXT, " #name)))
|
|
|
|
|
|
|
|
struct image_type_params **__start_image_type, **__stop_image_type;
|
|
|
|
#else
|
|
|
|
#define INIT_SECTION(name) /* no-op for ELF */
|
|
|
|
#define SECTION(name) __attribute__((section(#name)))
|
|
|
|
|
|
|
|
/* We construct a table of pointers in an ELF section (pointers generally
|
|
|
|
* go unpadded by gcc). ld creates boundary syms for us. */
|
|
|
|
extern struct image_type_params *__start_image_type[], *__stop_image_type[];
|
|
|
|
#endif /* __MACH__ */
|
|
|
|
|
|
|
|
#if !defined(__used)
|
|
|
|
# if __GNUC__ == 3 && __GNUC_MINOR__ < 3
|
|
|
|
# define __used __attribute__((__unused__))
|
|
|
|
# else
|
|
|
|
# define __used __attribute__((__used__))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2015-01-15 04:48:07 +00:00
|
|
|
#define U_BOOT_IMAGE_TYPE( \
|
|
|
|
_id, \
|
|
|
|
_name, \
|
|
|
|
_header_size, \
|
|
|
|
_header, \
|
|
|
|
_check_params, \
|
|
|
|
_verify_header, \
|
|
|
|
_print_header, \
|
|
|
|
_set_header, \
|
2015-01-15 04:54:41 +00:00
|
|
|
_extract_subimage, \
|
2015-01-15 04:48:07 +00:00
|
|
|
_check_image_type, \
|
|
|
|
_fflag_handle, \
|
|
|
|
_vrec_header \
|
|
|
|
) \
|
2015-02-08 23:06:10 +00:00
|
|
|
static struct image_type_params __cat(image_type_, _id) = \
|
|
|
|
{ \
|
2015-01-15 04:48:07 +00:00
|
|
|
.name = _name, \
|
|
|
|
.header_size = _header_size, \
|
|
|
|
.hdr = _header, \
|
|
|
|
.check_params = _check_params, \
|
|
|
|
.verify_header = _verify_header, \
|
|
|
|
.print_header = _print_header, \
|
|
|
|
.set_header = _set_header, \
|
2015-01-15 04:54:41 +00:00
|
|
|
.extract_subimage = _extract_subimage, \
|
2015-01-15 04:48:07 +00:00
|
|
|
.check_image_type = _check_image_type, \
|
|
|
|
.fflag_handle = _fflag_handle, \
|
|
|
|
.vrec_header = _vrec_header \
|
2015-02-08 23:06:10 +00:00
|
|
|
}; \
|
|
|
|
static struct image_type_params *SECTION(image_type) __used \
|
|
|
|
__cat(image_type_ptr_, _id) = &__cat(image_type_, _id)
|
2015-01-15 04:48:07 +00:00
|
|
|
|
2013-12-01 19:43:10 +00:00
|
|
|
#endif /* _IMAGETOOL_H_ */
|