History log of /u-boot/tools/imximage.c
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# b0adbd6f 23-Jan-2024 Jesse Taube <mr.bossman075@gmail.com>

tools: mkimage: Add support for i.MXRT FlexSPI Header

Modify imx8m Flex SPI Configuration Block to work with imxrt.
Add more Flex SPI configuration options to Kconfig.

Signed-off-by: Jesse Taube <Mr.Bossman075@gmail.com>
Tested-by: Adam Ford <aford173@gmail.com> #imx8mn-beacon

# 2972d7d6 29-Mar-2023 This contributor prefers not to receive mails <noreply@example.com>

tools: imagetool: Extend print_header() by params argument

This allows image type print_header() callback to access struct
image_tool_params *params.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 507a70b1 21-Nov-2022 Mikhail Ilin <ilin.mikhail.ol@gmail.com>

tools: imximage: Fix check array index

The struct dcd_v1_t is initialized to MAX_HW_CFG_SIZE_V1 (60)
structs 'dcd_type_addr_data_t', so the indexes to use on its elements
are [0,59]. But on line 478, the variable 'length' can take on the value
60, which applies to array overflow: cd_v1->addr_data[length].type Thus,
it is necessary to tighten the check on the 'size' variable on line 463.

Fixes: 0b0c6af38738 ("Prepare v2020.01")
Signed-off-by: Mikhail Ilin <ilin.mikhail.ol@gmail.com>

# 0c2b03ca 28-Jul-2020 Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

tools/imximage: fix DCD Blocks message output order

The correct order is load address, offset, length. The order was
accidentally switched a while ago; make it match the HAB Blocks output and
what CST expects again.

Fixes: e97bdfa5da70 ("tools/imximage: share DCD information via Kconfig")
Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

# e97bdfa5 11-Dec-2019 Jorge Ramirez-Ortiz <jorge@foundries.io>

tools/imximage: share DCD information via Kconfig

IMX based platforms can have the DCD table located on different
addresses due to differences in their memory maps (ie iMX7ULP).

This information is required by the user to sign the images for secure
boot so continue making it accessible via mkimage.

Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
Reviewed-by: Fabio Estevam <festevam@gmail.com>
Signed-off-by: Stefano Babic <sbabic@denx.de>

# 7e1a0483 06-Jul-2018 Holger Dengler <dengler@linutronix.de>

tools/imximage: get HAB information from header

Signing parts of a u-boot imximage for image verification in High
Assurance Boot (HAB) in a post-build process, requires some
information from the imximage header. Currently, this information is
only provided during the image build, which makes the transfer of this
information to the post-build process harder than necessary.

The i.MX HAB information (start and length) can be calculated either
by using information from the image-configuration file, or from the
information in the flash header of the imximage.
The advantage of using information from flash header is, that they are
not only available during image creation, but also available if
existing images are processed.

Example:
$ tools/mkimage -l u-boot.imx
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 483328 Bytes = 472.00 KiB = 0.46 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00071c00
DCD Blocks: 0x00910000 0x0000002c 0x00000208

Signed-off-by: Holger Dengler <dengler@linutronix.de>
Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# f3c32628 15-Apr-2018 Fabio Estevam <fabio.estevam@nxp.com>

Revert "imximage: Remove failure when no IVT offset is found"

This reverts commit b5b0e4e351e20a606de22db6a56ad6bc1e2aa8fd.

Commit f916757300c1 ("imx: Create distinct pre-processed mkimage
config files") provided a proper fix for the parallel mkimage
config files build failure, so the original workaround can be
safely reverted now.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# af1b492d 24-Apr-2018 Bryan O'Donoghue <bryan.odonoghue@linaro.org>

tools/imximage: Fix fruity lack of 0x prefix in DCD Blocks

commit 8519c9c98ad6 ("tools/imximage: use 0x prefix in HAB Blocks line")
adds an 0x prefix to each HAB Block number to make it easier for host tools
to process the HAB Block output, however it neglects to apply the same
prefix to the DCD Blocks directive. You need the DCD Blocks directive if
you are making a u-boot recovery image which the BootROM will accept via
the USB upload utility.

This disparity results in a fruity output like this with HAB Blocks
prefixed but DCD Blocks not prefixed - which is pretty inconsistent.

This patch fixes the difference assuming the original commit was a
legitimate change.

Old:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 00910000 0000002c 000001d4

New:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 0x00910000 0x0000002c 0x000001d4

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Breno Lima <breno.lima@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>

# 8519c9c9 22-Mar-2018 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

tools/imximage: use 0x prefix in HAB Blocks line

The u-boot-ivt.img.log file contains 0x prefixes in the HAB Blocks line,
while the SPL.log does not. For consistency, and to make it easier to
extract and put into a .csf file for use with NXP's code signing tool,
add 0x prefixes here.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Tested-by: Breno Lima <breno.lima@nxp.com>

# b5b0e4e3 09-Mar-2018 Fabio Estevam <fabio.estevam@nxp.com>

imximage: Remove failure when no IVT offset is found

Sometimes imximage throws the following error:

CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
MKIMAGE u-boot-dtb.imx
Error: No BOOT_FROM tag in board/freescale/vf610twr/imximage.cfg.cfgtmp
arch/arm/mach-imx/Makefile:100: recipe for target 'u-boot-dtb.imx' failed

Later on, when running mkimage for the u-boot.imx it will succeed in
finding the IVT offset.

Looks like some race condition happening during parallel build when
processing mkimage for u-boot-dtb.imx and u-boot.imx.

A proper fix still needs to be implemented, but as a workaround let's
remove the error when the IVT offset is not found.

It is useful to have such message, especially during bring-up phase,
but the build error that it causes is severe, so better avoid the
build error for now.

The error checking can be re-implemented later when we have a proper
fix.

Reported-by: Breno Lima <breno.lima@nxp.com>
Reported-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

# 3e0a71c1 16-Mar-2017 Peng Fan <peng.fan@nxp.com>

tools: imximage: add set bit command

Add set bit command support.
Usage: SET_BIT 4 [address] [bitmask]

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# 97f17fa6 02-Jan-2017 Martin Kaiser <martin@kaiser.cx>

tools: imximage: refactor header length calculations for imximage v1

We can use the same header length calculations for both imximage v1 and
v2. This addresses TODO comments about imximage v1 in the current code.

With this patch applied, *header_size_ptr in imximage_set_header() will
have the correct value for both imximage v1 and v2. This is necessary
for people wanting to add proprietary data behind the created imximage.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
Cc: sbabic@denx.de

# e5491f3e 16-Nov-2016 Eric Nelson <eric@nelint.com>

tools: imximage: display DCD block offset, length

These values can be used to sign a U-Boot image for use when
loading an image through the Serial Download Protocol (SDP).

Note that the address of 0x910000 is usable with the stock
configuration of imx_usb_loader on i.MX6 and i.MX7 SOCs:

https://github.com/boundarydevices/imx_usb_loader/blob/master/mx6_usb_work.conf#L3

Refer to the section on imx_usb_loader in this post for more
details:

https://boundarydevices.com/high-assurance-boot-hab-dummies/

Signed-off-by: Eric Nelson <eric@nelint.com>

# 2a380ccc 03-Nov-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: check return value when open the plugin file

Check return value when open the plugin file.

Coverity report:
** CID 153926: Error handling issues (NEGATIVE_RETURNS)
/tools/imximage.c: 542 in copy_plugin_code()

ifd = open(plugin_file, O_RDONLY|O_BINARY);
>>> CID 153926: Error handling issues (NEGATIVE_RETURNS)
>>> "ifd" is passed to a parameter that cannot be negative.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reported-by: Coverity (CID: 153926)
Reviewed-by: Tom Rini <trini@konsulko.com>

# b55e4f48 11-Oct-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: add plugin support

Add plugin support for imximage.

Define CONFIG_USE_IMXIMG_PLUGIN in defconfig to enable using plugin.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Eric Nelson <eric@nelint.com>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 0782a880 02-May-2016 Adrian Alonso <adrian.alonso@nxp.com>

imx: tools: imximage: fix CLR bit command

Fix incorrect parametr in CMD_CHECK_BITS_CLR command
Pass CLR parameter to DCD header for CMD_CHECK_BITS_CLR

Signed-off-by: Adrian Alonso <adrian.alonso@nxp.com>

# 61903b75 14-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix commands other than write_data

When CHECK_BITS_SET was added, they forgot to add
a new command table, and instead overwrote the
previous table.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Tested-by: Fabio Estevam <fabio.estevam@freescale.com>

# 835c30e3 21-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: header.length of 4 is valid

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# b893c989 09-Jul-2015 Baruch Siach <baruch@tkos.co.il>

tools/imximage: set DCD pointer to NULL when its length is 0

When dcd_len is 0 the Write Data command that the set_dcd_rst_v2() routine
generates is empty. This causes HAB to complain that the command is invalid.

--------- HAB Event 1 -----------------
event data:
0xdb 0x00 0x0c 0x41 0x33 0x06 0xc0 0x00
0xcc 0x00 0x04 0x04

To fix this set the DCD pointer in the IVT to NULL in this case. The DCD header
itself is still needed for detect_imximage_version() to determine the image
version.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Acked-by: Stefano Babic <sbabic@denx.de>

# 0b7f7c33 20-Jul-2015 Adrian Alonso <aalonso@freescale.com>

imx: imximage: add new CHECK/CLR BIT command

* Extend imximage DCD version 2 to support DCD commands
CMD_WRITE_CLR_BIT 4 [address] [mask bit] means:
while ((*address & ~mask) != 0);
CMD_CHECK_BITS_SET 4 [address] [mask bit] means:
while ((*address & mask) != mask);
CMD_CHECK_BITS_CLR 4 [address] [mask bit] means:
*address = *address & ~mask;
* Add set_dcd_param_v2 helper function to set DCD
command parameters

Signed-off-by: Adrian Alonso <aalonso@freescale.com>
Signed-off-by: Peng Fan <Peng.Fan@freescale.com>

# 307367ea 10-Feb-2015 Tom Rini <trini@konsulko.com>

Merge branch 'master' of git://www.denx.de/git/u-boot-imx


# 9598f8c3 13-Jan-2015 Ye.Li <B37916@freescale.com>

imx: imximage: Add QuadSPI boot support

Add QuadSPI boot support to imximage tool.

Note: The QuadSPI configuration parameters at offset 0x400 are not
included in this patch. Need other tools to generate the parameters
part.

Signed-off-by: Ye.Li <B37916@freescale.com>

# a93648d1 14-Jan-2015 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

imagetool: replace image registration function by linker_lists feature

The registration was introduced in commit f86ed6a8d52c99bb2d17d3cac1647edca0c4399c

This commit also removes all registration functions, and the member "next"
from image_type_params struct

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

# de979804 30-Oct-2014 Ye.Li <B37916@freescale.com>

imximage: Fix the bootdata.size calculation

In system boot chapter of i.MX6 reference manual, the "Image Vector Table"
figure shows the bootdata.start points to the beginning of the destination
memory. It means the bootdata.size should contain the IVT offset part,
but the calculation in imximage tool does not have.

We found this issue when booting from QuadSPI NOR on i.MX6SX. The u-boot
runs into abnormal (crash or stop) after booting. After checked the destination
memory where the image is loaded to, there are hundreds of bytes at
the image end are not loaded into memory. Since there is a 4096 bytes
round in the calculation, for the booting devices using smaller IVT offset,
such as SD and SPI booting, they are not easy to reproduce.

Signed-off-by: Ye.Li <B37916@freescale.com>

# 03ea24b2 20-Aug-2014 Ye.Li <Ye.Li@freescale.com>

imximage: Fix imximage IVT bug for EIM-NOR boot

The load region size of EIM-NOR are defined to 0. For this case,
the parameter "imximage_init_loadsize" must be calculated.
The imximage tool implements the calculation in the "imximage_generate"
function, but the following function "imximage_set_header" resets the value
and not calculate. This bug cause some fields of IVT head are not
correct, for example the boot_data and DCD overlay the application area.

Signed-off-by: Ye.Li <B37916@freescale.com>

# f86ed6a8 01-Dec-2013 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

tools: moved code common to all image tools to a separated module.

In order to avoid duplicating code and keep only one point of modification,
the functions, structs and defines useful for "dumpimage" were moved from
"mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS,
and so on) and image tools (mkimage and dumpimage). Any tool may initialize the
"imagetool" through register_image_tool() function, while the image types
register themselves within an image tool using the register_image_type()
function:

+---------------+
+------| fit_image |
+--------------+ +-----------+ | +---------------+
| mkimage |--------> | | <-----+
+--------------+ | | +---------------+
| imagetool | <------------| imximage |
+--------------+ | | +---------------+
| dumpimage |--------> | | <-----+
+--------------+ +-----------+ | +---------------+
+------| default_image |
+---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make
clear its general purpose.

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3765b3e7 07-Oct-2013 Wolfgang Denk <wd@denx.de>

Coding Style cleanup: remove trailing white space

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 72048bc3 20-Sep-2013 York Sun <yorksun@freescale.com>

tools/imximage.c: Fix compiling warning

Convert set_hdr_func(struct imx_header *imxhdr) to set_hdr_func(void)
to get rid of the warning

warning: ‘imxhdr’ is used uninitialized in this function

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 19d829fa 05-Sep-2013 Albert ARIBAUD <albert.u.boot@aribaud.net>

Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'

Conflicts:
drivers/serial/serial.c

The conflict above was a trivial case of adding one init
function in each branch, and manually resolved in merge.


# 0187c985 27-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: add support for setting the CSF into imximage

Add support for setting the CSF (Command Sequence File) pointer
which is used for HAB (High Assurance Boot) in the imximage by
adding e.g.

CSF 0x2000

in the imximage.cfg file.

This will set the CSF pointer accordingly just after the padded
data image area. The boot_data.length is adjusted with the
value from the imximage.cfg config file.

The resulting u-boot.imx can be signed with the FSL HAB tooling.
The generated CSF block needs to be appended to the u-boot.imx.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 01390aff 19-Aug-2013 Stefano Babic <sbabic@denx.de>

tools: add padding of data image file for imximage

Implement function vrec_header to be able to pad the final
data image file according the what has been calculated for
boot_data.length.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 377e367a 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: dynamically allocate imx_header in imximage

Change to dynamically allocate the imx_header to correctly
allocate the IVT, Boot Data and DCD at correct locations
depending on the boot media.

Also check that the Image Vector Table Offset + IVT +
Boot Data + DCD <= Initial Load Region Size.

Previously struct imx_header was always 4096 bytes and was
not dealing correctly with the Image Vector Table Offset.

Now, the memory allocation looks for e.g. SD boot like this

Storage u-boot.imx RAM
Device

00000000 177ff000 <--------------
|
00000400 00000000 d1 00 20 40 IVT.header 177ff400 <------- |
00000404 00000004 00 00 80 17 IVT.entry 177ff404 ----------- |
00000408 00000008 00 00 00 00 IVT.reserved1 177ff408 | | |
0000040C 0000000C 2c f4 7f 17 IVT.dcd 177ff40C ------ | | |
00000410 00000010 20 f4 7f 17 IVT.boot 177ff410 ---- | | | |
00000414 00000014 00 f4 7f 17 IVT.self 177ff414 -------- | |
00000418 00000018 00 00 00 00 IVT.csf 177ff418 | | | |
0000041C 0000001C 00 00 00 00 IVT.reserved2 177ff41C | | | |
00000420 00000020 00 f0 7f 17 BootData.start 177ff420 <--- | | ---
00000424 00000024 00 60 03 00 BootData.length 177ff424 | |
00000428 00000028 00 00 00 00 BootData.plugin 177ff428 | |
0000042C 0000002C d2 03 30 40 DCD.header 177ff42C <----- |
... |
00001000 00000c00 13 00 00 ea U-Boot Start 17800000 <----------

While at it also remove the unused #define HEADER_OFFSET.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 3150f92c 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: rename mximage_flash_offset to imximage_ivt_offset

This better reflects the naming from the Reference Manual
as well as fits better since "flash" is not really applicabe
for SATA.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 4655d40f 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: imx_header should not include flash_offset

Doing a make distclean; make mx6qsabresd_config; make
and hexdump -C u-boot.imx | less

...
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000003f0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 |................|
^^^^^^^^^^^
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|
...

shows the flash_offset value being written into the final
generated image, wich is not correct.

Instead create flash_offset as static variable such that the
generated image is "clean".

00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 895d9966 20-Apr-2013 Marek Vasut <marex@denx.de>

imx: Align the imximage header and payload to multiples of 4k

The MX53 ROM loads the data from NAND in multiples of pages and
supports maximum page size of 4k. Thus, align the image and header
to 4k to be safe from ROM bugs.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>

# 6cb83829 25-Apr-2013 Marek Vasut <marex@denx.de>

tools: arm: imx: Implement BOOT_OFFSET command for imximage

Implement BOOT_OFFSET command for imximage. This command is parallel
to current BOOT_FROM command, but allows more flexibility in configuring
arbitrary image header offset. Also add an imximage.cfg with default
offset values into arm/arch/imx-common/ so the board-specific imximage.cfg
can include this file to avoid magic constants.

The syntax of BOOT_OFFSET command is "BOOT_OFFSET <u32 offset>".

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Acked-by: Stefano Babic <sbabic@denx.de>
Acked-by: Stefan Roese <sr@denx.de>

# 6e083857 02-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Let .name field be more generic

Since this structure is not i.MX5x specific, remove the '5x' to make it more
generic.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# 1411fb37 03-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Load a size that is multiple of 512

In order to mx53 ROM to properly load the U-boot image, its header size should
be multiple of 512 bytes.

This issue was observed with gcc 4.6.2/4.7.3, which caused data aborts:

U-Boot 2013.01-rc2-00172-gf8cfcf1-dirty (Dec 26 2012 - 13:13:28)

Board: MX53 LOCO
I2C: ready
DRAM: 1 GiB
MMC: FSL_SDHC: 0, FSL_SDHC: 1
In: serial
Out: serial
Err: serial
CPU: Freescale i.MX53 family rev2.1 at 1000 MHz
Reset cause: WDOG
Net: FEC
Warning: FEC using MAC address from net device

Hit any key to stop autoboot: 0
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<aff72220>] lr : [<aff721fc>]
sp : af565e20 ip : af566918 fp : 00000000
r10: 00000003 r9 : affabb5b r8 : af565f58
r7 : 00000000 r6 : 36747fff r5 : af5668e8 r4 : 36747fff
r3 : af5668ec r2 : af5668eb r1 : 00000000 r0 : af5668e8
Flags: NzcV IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...

,and this patch fixes it.

Also, even though the ROUND macro is already defined in common.h,
the reason for redefining it in image.h is explained by Stefano Babic:

"I will remark a previous comment - even if including common.h seems a
good idea to avoid duplications, it makes tools like mkimage to depend
on the selected board, because <board>_config must run. Even if this is
not a problem for us u-boot developers, it becomes an issue when these
tools are included in distros (like u-boot-tools in Ubuntu) and cannot
be packaged."

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# ab857f26 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: make set_imx_hdr_v1/v2 easier to read

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# ad0826dc 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: change parameters to set_imx_hdr

Call with the value the function will use
instead of going through a pointer.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 24331982 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: delay setting of image size

When later we change to variable length
header, we won't know the file size when
set_imx_hdr is called. So this is prep work.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 348ca8ef 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix size of image to load.

sbuf->st_size already includes sizeof(struct imx_header),
so remove extra addition.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 8d8cc828 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: move flash_offset check to common location

Both set_imx_hdr_v1 and set_imx_hdr_v2 perform the
same check. Move check to before the set_imx_hdr call.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# f14e6258 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: remove redundant setting of app_dest_ptr

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 4d5fa985 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: check dcd_len as entries added

Before the len was checked after the entire file
was processed, so it could have already overflowed.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 49d3e272 22-Feb-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Remove overwriting of flash_offset

The flash header supports different flash offsets for different
boot devices. E.g. parallel NOR or OneNAND use a different offset
than FLASH_OFFSET_STANDARD (== 0x400).

The flash offset is correctly read from the configuration in
parse_cfg_cmd(). But is then overwritten wrongly in set_imx_hdr_v1/2().

Fix this by removing this overwriting. Use the flash offset
correctly read from the configuration, instead.

If there is no flash_offset read from the configuration file, i.e.
the BOOT_FROM tag is missing, exit with an error message.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Jason Liu <liu.h.jason@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
Tested-by: Stefano Babic <sbabic@denx.de>

# bd25864c 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Sort bootops alphabetically

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 19b409c0 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Add support for i.MX6

The i.MX6 processor can boot from NOR flash and SATA disks,
additionally. Add the flash offsets for these additional
boot modes.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 8a1edd7d 19-Jan-2011 Liu Hui-R64343 <r64343@freescale.com>

imximage: Add MX53 boot image support

This patch add the MX53 boot image support.

This patch has been tested on Freescale MX53EVK board
and MX51EVK board.

Signed-off-by: Jason Liu <r64343@freescale.com>

# f581e3a2 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 0ad22703 22-Feb-2010 Kim Phillips <kim.phillips@freescale.com>

tools: fix imximage warning

Fix build warning:

Configuring for MPC837XEMDS board...
imximage.c: In function `imximage_parse_cfg_file':
imximage.c:146: warning: passing argument 2 of `getline' from incompatible pointer type
/usr/include/bits/stdio.h:116: note: expected `size_t *' but argument is of type `uint32_t *'

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>

# 5b28e913 05-Feb-2010 Stefano Babic <sbabic@denx.de>

mkimage: SEGFAULT with imximage on 64 bit systems

Running mkimage to generate an imximage produces a SEGFAULT
on 64 bit machines due to pointer arithmetic limited to 32 bit.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Acked-by: Kim Phillips <kim.phillips@freescale.com>

# 249d4dec 27-Jan-2010 Kumar Gala <galak@kernel.crashing.org>

Fix compiler warning in imximage.c due to getline prototype

imximage.c: In function 'imximage_parse_cfg_file':
imximage.c:142: warning: implicit declaration of function 'getline'

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>

# 8edcde5e 20-Jan-2010 Stefano Babic <sbabic@denx.de>

mkimage: Add Freescale imx Boot Image support (imximage)

This patch adds support for "imximage" (MX Boot Image)
to the mkimage utility. The imximage is used on the Freescales's
MX.25, MX.35 and MX.51 processors.

Further details under doc/README.imximage.

This patch was tested on a Freescale mx51evk board.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 2972d7d6 29-Mar-2023 Pali Rohár <pali@kernel.org>

tools: imagetool: Extend print_header() by params argument

This allows image type print_header() callback to access struct
image_tool_params *params.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 507a70b1 21-Nov-2022 Mikhail Ilin <ilin.mikhail.ol@gmail.com>

tools: imximage: Fix check array index

The struct dcd_v1_t is initialized to MAX_HW_CFG_SIZE_V1 (60)
structs 'dcd_type_addr_data_t', so the indexes to use on its elements
are [0,59]. But on line 478, the variable 'length' can take on the value
60, which applies to array overflow: cd_v1->addr_data[length].type Thus,
it is necessary to tighten the check on the 'size' variable on line 463.

Fixes: 0b0c6af38738 ("Prepare v2020.01")
Signed-off-by: Mikhail Ilin <ilin.mikhail.ol@gmail.com>

# 0c2b03ca 28-Jul-2020 Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

tools/imximage: fix DCD Blocks message output order

The correct order is load address, offset, length. The order was
accidentally switched a while ago; make it match the HAB Blocks output and
what CST expects again.

Fixes: e97bdfa5da70 ("tools/imximage: share DCD information via Kconfig")
Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

# e97bdfa5 11-Dec-2019 Jorge Ramirez-Ortiz <jorge@foundries.io>

tools/imximage: share DCD information via Kconfig

IMX based platforms can have the DCD table located on different
addresses due to differences in their memory maps (ie iMX7ULP).

This information is required by the user to sign the images for secure
boot so continue making it accessible via mkimage.

Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
Reviewed-by: Fabio Estevam <festevam@gmail.com>
Signed-off-by: Stefano Babic <sbabic@denx.de>

# 7e1a0483 06-Jul-2018 Holger Dengler <dengler@linutronix.de>

tools/imximage: get HAB information from header

Signing parts of a u-boot imximage for image verification in High
Assurance Boot (HAB) in a post-build process, requires some
information from the imximage header. Currently, this information is
only provided during the image build, which makes the transfer of this
information to the post-build process harder than necessary.

The i.MX HAB information (start and length) can be calculated either
by using information from the image-configuration file, or from the
information in the flash header of the imximage.
The advantage of using information from flash header is, that they are
not only available during image creation, but also available if
existing images are processed.

Example:
$ tools/mkimage -l u-boot.imx
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 483328 Bytes = 472.00 KiB = 0.46 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00071c00
DCD Blocks: 0x00910000 0x0000002c 0x00000208

Signed-off-by: Holger Dengler <dengler@linutronix.de>
Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# f3c32628 15-Apr-2018 Fabio Estevam <fabio.estevam@nxp.com>

Revert "imximage: Remove failure when no IVT offset is found"

This reverts commit b5b0e4e351e20a606de22db6a56ad6bc1e2aa8fd.

Commit f916757300c1 ("imx: Create distinct pre-processed mkimage
config files") provided a proper fix for the parallel mkimage
config files build failure, so the original workaround can be
safely reverted now.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# af1b492d 24-Apr-2018 Bryan O'Donoghue <bryan.odonoghue@linaro.org>

tools/imximage: Fix fruity lack of 0x prefix in DCD Blocks

commit 8519c9c98ad6 ("tools/imximage: use 0x prefix in HAB Blocks line")
adds an 0x prefix to each HAB Block number to make it easier for host tools
to process the HAB Block output, however it neglects to apply the same
prefix to the DCD Blocks directive. You need the DCD Blocks directive if
you are making a u-boot recovery image which the BootROM will accept via
the USB upload utility.

This disparity results in a fruity output like this with HAB Blocks
prefixed but DCD Blocks not prefixed - which is pretty inconsistent.

This patch fixes the difference assuming the original commit was a
legitimate change.

Old:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 00910000 0000002c 000001d4

New:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 0x00910000 0x0000002c 0x000001d4

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Breno Lima <breno.lima@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>

# 8519c9c9 22-Mar-2018 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

tools/imximage: use 0x prefix in HAB Blocks line

The u-boot-ivt.img.log file contains 0x prefixes in the HAB Blocks line,
while the SPL.log does not. For consistency, and to make it easier to
extract and put into a .csf file for use with NXP's code signing tool,
add 0x prefixes here.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Tested-by: Breno Lima <breno.lima@nxp.com>

# b5b0e4e3 09-Mar-2018 Fabio Estevam <fabio.estevam@nxp.com>

imximage: Remove failure when no IVT offset is found

Sometimes imximage throws the following error:

CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
MKIMAGE u-boot-dtb.imx
Error: No BOOT_FROM tag in board/freescale/vf610twr/imximage.cfg.cfgtmp
arch/arm/mach-imx/Makefile:100: recipe for target 'u-boot-dtb.imx' failed

Later on, when running mkimage for the u-boot.imx it will succeed in
finding the IVT offset.

Looks like some race condition happening during parallel build when
processing mkimage for u-boot-dtb.imx and u-boot.imx.

A proper fix still needs to be implemented, but as a workaround let's
remove the error when the IVT offset is not found.

It is useful to have such message, especially during bring-up phase,
but the build error that it causes is severe, so better avoid the
build error for now.

The error checking can be re-implemented later when we have a proper
fix.

Reported-by: Breno Lima <breno.lima@nxp.com>
Reported-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

# 3e0a71c1 16-Mar-2017 Peng Fan <peng.fan@nxp.com>

tools: imximage: add set bit command

Add set bit command support.
Usage: SET_BIT 4 [address] [bitmask]

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# 97f17fa6 02-Jan-2017 Martin Kaiser <martin@kaiser.cx>

tools: imximage: refactor header length calculations for imximage v1

We can use the same header length calculations for both imximage v1 and
v2. This addresses TODO comments about imximage v1 in the current code.

With this patch applied, *header_size_ptr in imximage_set_header() will
have the correct value for both imximage v1 and v2. This is necessary
for people wanting to add proprietary data behind the created imximage.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
Cc: sbabic@denx.de

# e5491f3e 16-Nov-2016 Eric Nelson <eric@nelint.com>

tools: imximage: display DCD block offset, length

These values can be used to sign a U-Boot image for use when
loading an image through the Serial Download Protocol (SDP).

Note that the address of 0x910000 is usable with the stock
configuration of imx_usb_loader on i.MX6 and i.MX7 SOCs:

https://github.com/boundarydevices/imx_usb_loader/blob/master/mx6_usb_work.conf#L3

Refer to the section on imx_usb_loader in this post for more
details:

https://boundarydevices.com/high-assurance-boot-hab-dummies/

Signed-off-by: Eric Nelson <eric@nelint.com>

# 2a380ccc 03-Nov-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: check return value when open the plugin file

Check return value when open the plugin file.

Coverity report:
** CID 153926: Error handling issues (NEGATIVE_RETURNS)
/tools/imximage.c: 542 in copy_plugin_code()

ifd = open(plugin_file, O_RDONLY|O_BINARY);
>>> CID 153926: Error handling issues (NEGATIVE_RETURNS)
>>> "ifd" is passed to a parameter that cannot be negative.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reported-by: Coverity (CID: 153926)
Reviewed-by: Tom Rini <trini@konsulko.com>

# b55e4f48 11-Oct-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: add plugin support

Add plugin support for imximage.

Define CONFIG_USE_IMXIMG_PLUGIN in defconfig to enable using plugin.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Eric Nelson <eric@nelint.com>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 0782a880 02-May-2016 Adrian Alonso <adrian.alonso@nxp.com>

imx: tools: imximage: fix CLR bit command

Fix incorrect parametr in CMD_CHECK_BITS_CLR command
Pass CLR parameter to DCD header for CMD_CHECK_BITS_CLR

Signed-off-by: Adrian Alonso <adrian.alonso@nxp.com>

# 61903b75 14-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix commands other than write_data

When CHECK_BITS_SET was added, they forgot to add
a new command table, and instead overwrote the
previous table.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Tested-by: Fabio Estevam <fabio.estevam@freescale.com>

# 835c30e3 21-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: header.length of 4 is valid

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# b893c989 09-Jul-2015 Baruch Siach <baruch@tkos.co.il>

tools/imximage: set DCD pointer to NULL when its length is 0

When dcd_len is 0 the Write Data command that the set_dcd_rst_v2() routine
generates is empty. This causes HAB to complain that the command is invalid.

--------- HAB Event 1 -----------------
event data:
0xdb 0x00 0x0c 0x41 0x33 0x06 0xc0 0x00
0xcc 0x00 0x04 0x04

To fix this set the DCD pointer in the IVT to NULL in this case. The DCD header
itself is still needed for detect_imximage_version() to determine the image
version.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Acked-by: Stefano Babic <sbabic@denx.de>

# 0b7f7c33 20-Jul-2015 Adrian Alonso <aalonso@freescale.com>

imx: imximage: add new CHECK/CLR BIT command

* Extend imximage DCD version 2 to support DCD commands
CMD_WRITE_CLR_BIT 4 [address] [mask bit] means:
while ((*address & ~mask) != 0);
CMD_CHECK_BITS_SET 4 [address] [mask bit] means:
while ((*address & mask) != mask);
CMD_CHECK_BITS_CLR 4 [address] [mask bit] means:
*address = *address & ~mask;
* Add set_dcd_param_v2 helper function to set DCD
command parameters

Signed-off-by: Adrian Alonso <aalonso@freescale.com>
Signed-off-by: Peng Fan <Peng.Fan@freescale.com>

# 307367ea 10-Feb-2015 Tom Rini <trini@konsulko.com>

Merge branch 'master' of git://www.denx.de/git/u-boot-imx


# 9598f8c3 13-Jan-2015 Ye.Li <B37916@freescale.com>

imx: imximage: Add QuadSPI boot support

Add QuadSPI boot support to imximage tool.

Note: The QuadSPI configuration parameters at offset 0x400 are not
included in this patch. Need other tools to generate the parameters
part.

Signed-off-by: Ye.Li <B37916@freescale.com>

# a93648d1 14-Jan-2015 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

imagetool: replace image registration function by linker_lists feature

The registration was introduced in commit f86ed6a8d52c99bb2d17d3cac1647edca0c4399c

This commit also removes all registration functions, and the member "next"
from image_type_params struct

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

# de979804 30-Oct-2014 Ye.Li <B37916@freescale.com>

imximage: Fix the bootdata.size calculation

In system boot chapter of i.MX6 reference manual, the "Image Vector Table"
figure shows the bootdata.start points to the beginning of the destination
memory. It means the bootdata.size should contain the IVT offset part,
but the calculation in imximage tool does not have.

We found this issue when booting from QuadSPI NOR on i.MX6SX. The u-boot
runs into abnormal (crash or stop) after booting. After checked the destination
memory where the image is loaded to, there are hundreds of bytes at
the image end are not loaded into memory. Since there is a 4096 bytes
round in the calculation, for the booting devices using smaller IVT offset,
such as SD and SPI booting, they are not easy to reproduce.

Signed-off-by: Ye.Li <B37916@freescale.com>

# 03ea24b2 20-Aug-2014 Ye.Li <Ye.Li@freescale.com>

imximage: Fix imximage IVT bug for EIM-NOR boot

The load region size of EIM-NOR are defined to 0. For this case,
the parameter "imximage_init_loadsize" must be calculated.
The imximage tool implements the calculation in the "imximage_generate"
function, but the following function "imximage_set_header" resets the value
and not calculate. This bug cause some fields of IVT head are not
correct, for example the boot_data and DCD overlay the application area.

Signed-off-by: Ye.Li <B37916@freescale.com>

# f86ed6a8 01-Dec-2013 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

tools: moved code common to all image tools to a separated module.

In order to avoid duplicating code and keep only one point of modification,
the functions, structs and defines useful for "dumpimage" were moved from
"mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS,
and so on) and image tools (mkimage and dumpimage). Any tool may initialize the
"imagetool" through register_image_tool() function, while the image types
register themselves within an image tool using the register_image_type()
function:

+---------------+
+------| fit_image |
+--------------+ +-----------+ | +---------------+
| mkimage |--------> | | <-----+
+--------------+ | | +---------------+
| imagetool | <------------| imximage |
+--------------+ | | +---------------+
| dumpimage |--------> | | <-----+
+--------------+ +-----------+ | +---------------+
+------| default_image |
+---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make
clear its general purpose.

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3765b3e7 07-Oct-2013 Wolfgang Denk <wd@denx.de>

Coding Style cleanup: remove trailing white space

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 72048bc3 20-Sep-2013 York Sun <yorksun@freescale.com>

tools/imximage.c: Fix compiling warning

Convert set_hdr_func(struct imx_header *imxhdr) to set_hdr_func(void)
to get rid of the warning

warning: ‘imxhdr’ is used uninitialized in this function

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 19d829fa 05-Sep-2013 Albert ARIBAUD <albert.u.boot@aribaud.net>

Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'

Conflicts:
drivers/serial/serial.c

The conflict above was a trivial case of adding one init
function in each branch, and manually resolved in merge.


# 0187c985 27-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: add support for setting the CSF into imximage

Add support for setting the CSF (Command Sequence File) pointer
which is used for HAB (High Assurance Boot) in the imximage by
adding e.g.

CSF 0x2000

in the imximage.cfg file.

This will set the CSF pointer accordingly just after the padded
data image area. The boot_data.length is adjusted with the
value from the imximage.cfg config file.

The resulting u-boot.imx can be signed with the FSL HAB tooling.
The generated CSF block needs to be appended to the u-boot.imx.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 01390aff 19-Aug-2013 Stefano Babic <sbabic@denx.de>

tools: add padding of data image file for imximage

Implement function vrec_header to be able to pad the final
data image file according the what has been calculated for
boot_data.length.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 377e367a 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: dynamically allocate imx_header in imximage

Change to dynamically allocate the imx_header to correctly
allocate the IVT, Boot Data and DCD at correct locations
depending on the boot media.

Also check that the Image Vector Table Offset + IVT +
Boot Data + DCD <= Initial Load Region Size.

Previously struct imx_header was always 4096 bytes and was
not dealing correctly with the Image Vector Table Offset.

Now, the memory allocation looks for e.g. SD boot like this

Storage u-boot.imx RAM
Device

00000000 177ff000 <--------------
|
00000400 00000000 d1 00 20 40 IVT.header 177ff400 <------- |
00000404 00000004 00 00 80 17 IVT.entry 177ff404 ----------- |
00000408 00000008 00 00 00 00 IVT.reserved1 177ff408 | | |
0000040C 0000000C 2c f4 7f 17 IVT.dcd 177ff40C ------ | | |
00000410 00000010 20 f4 7f 17 IVT.boot 177ff410 ---- | | | |
00000414 00000014 00 f4 7f 17 IVT.self 177ff414 -------- | |
00000418 00000018 00 00 00 00 IVT.csf 177ff418 | | | |
0000041C 0000001C 00 00 00 00 IVT.reserved2 177ff41C | | | |
00000420 00000020 00 f0 7f 17 BootData.start 177ff420 <--- | | ---
00000424 00000024 00 60 03 00 BootData.length 177ff424 | |
00000428 00000028 00 00 00 00 BootData.plugin 177ff428 | |
0000042C 0000002C d2 03 30 40 DCD.header 177ff42C <----- |
... |
00001000 00000c00 13 00 00 ea U-Boot Start 17800000 <----------

While at it also remove the unused #define HEADER_OFFSET.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 3150f92c 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: rename mximage_flash_offset to imximage_ivt_offset

This better reflects the naming from the Reference Manual
as well as fits better since "flash" is not really applicabe
for SATA.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 4655d40f 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: imx_header should not include flash_offset

Doing a make distclean; make mx6qsabresd_config; make
and hexdump -C u-boot.imx | less

...
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000003f0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 |................|
^^^^^^^^^^^
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|
...

shows the flash_offset value being written into the final
generated image, wich is not correct.

Instead create flash_offset as static variable such that the
generated image is "clean".

00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 895d9966 20-Apr-2013 Marek Vasut <marex@denx.de>

imx: Align the imximage header and payload to multiples of 4k

The MX53 ROM loads the data from NAND in multiples of pages and
supports maximum page size of 4k. Thus, align the image and header
to 4k to be safe from ROM bugs.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>

# 6cb83829 25-Apr-2013 Marek Vasut <marex@denx.de>

tools: arm: imx: Implement BOOT_OFFSET command for imximage

Implement BOOT_OFFSET command for imximage. This command is parallel
to current BOOT_FROM command, but allows more flexibility in configuring
arbitrary image header offset. Also add an imximage.cfg with default
offset values into arm/arch/imx-common/ so the board-specific imximage.cfg
can include this file to avoid magic constants.

The syntax of BOOT_OFFSET command is "BOOT_OFFSET <u32 offset>".

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Acked-by: Stefano Babic <sbabic@denx.de>
Acked-by: Stefan Roese <sr@denx.de>

# 6e083857 02-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Let .name field be more generic

Since this structure is not i.MX5x specific, remove the '5x' to make it more
generic.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# 1411fb37 03-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Load a size that is multiple of 512

In order to mx53 ROM to properly load the U-boot image, its header size should
be multiple of 512 bytes.

This issue was observed with gcc 4.6.2/4.7.3, which caused data aborts:

U-Boot 2013.01-rc2-00172-gf8cfcf1-dirty (Dec 26 2012 - 13:13:28)

Board: MX53 LOCO
I2C: ready
DRAM: 1 GiB
MMC: FSL_SDHC: 0, FSL_SDHC: 1
In: serial
Out: serial
Err: serial
CPU: Freescale i.MX53 family rev2.1 at 1000 MHz
Reset cause: WDOG
Net: FEC
Warning: FEC using MAC address from net device

Hit any key to stop autoboot: 0
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<aff72220>] lr : [<aff721fc>]
sp : af565e20 ip : af566918 fp : 00000000
r10: 00000003 r9 : affabb5b r8 : af565f58
r7 : 00000000 r6 : 36747fff r5 : af5668e8 r4 : 36747fff
r3 : af5668ec r2 : af5668eb r1 : 00000000 r0 : af5668e8
Flags: NzcV IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...

,and this patch fixes it.

Also, even though the ROUND macro is already defined in common.h,
the reason for redefining it in image.h is explained by Stefano Babic:

"I will remark a previous comment - even if including common.h seems a
good idea to avoid duplications, it makes tools like mkimage to depend
on the selected board, because <board>_config must run. Even if this is
not a problem for us u-boot developers, it becomes an issue when these
tools are included in distros (like u-boot-tools in Ubuntu) and cannot
be packaged."

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# ab857f26 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: make set_imx_hdr_v1/v2 easier to read

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# ad0826dc 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: change parameters to set_imx_hdr

Call with the value the function will use
instead of going through a pointer.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 24331982 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: delay setting of image size

When later we change to variable length
header, we won't know the file size when
set_imx_hdr is called. So this is prep work.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 348ca8ef 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix size of image to load.

sbuf->st_size already includes sizeof(struct imx_header),
so remove extra addition.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 8d8cc828 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: move flash_offset check to common location

Both set_imx_hdr_v1 and set_imx_hdr_v2 perform the
same check. Move check to before the set_imx_hdr call.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# f14e6258 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: remove redundant setting of app_dest_ptr

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 4d5fa985 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: check dcd_len as entries added

Before the len was checked after the entire file
was processed, so it could have already overflowed.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 49d3e272 22-Feb-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Remove overwriting of flash_offset

The flash header supports different flash offsets for different
boot devices. E.g. parallel NOR or OneNAND use a different offset
than FLASH_OFFSET_STANDARD (== 0x400).

The flash offset is correctly read from the configuration in
parse_cfg_cmd(). But is then overwritten wrongly in set_imx_hdr_v1/2().

Fix this by removing this overwriting. Use the flash offset
correctly read from the configuration, instead.

If there is no flash_offset read from the configuration file, i.e.
the BOOT_FROM tag is missing, exit with an error message.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Jason Liu <liu.h.jason@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
Tested-by: Stefano Babic <sbabic@denx.de>

# bd25864c 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Sort bootops alphabetically

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 19b409c0 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Add support for i.MX6

The i.MX6 processor can boot from NOR flash and SATA disks,
additionally. Add the flash offsets for these additional
boot modes.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 8a1edd7d 19-Jan-2011 Liu Hui-R64343 <r64343@freescale.com>

imximage: Add MX53 boot image support

This patch add the MX53 boot image support.

This patch has been tested on Freescale MX53EVK board
and MX51EVK board.

Signed-off-by: Jason Liu <r64343@freescale.com>

# f581e3a2 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 0ad22703 22-Feb-2010 Kim Phillips <kim.phillips@freescale.com>

tools: fix imximage warning

Fix build warning:

Configuring for MPC837XEMDS board...
imximage.c: In function `imximage_parse_cfg_file':
imximage.c:146: warning: passing argument 2 of `getline' from incompatible pointer type
/usr/include/bits/stdio.h:116: note: expected `size_t *' but argument is of type `uint32_t *'

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>

# 5b28e913 05-Feb-2010 Stefano Babic <sbabic@denx.de>

mkimage: SEGFAULT with imximage on 64 bit systems

Running mkimage to generate an imximage produces a SEGFAULT
on 64 bit machines due to pointer arithmetic limited to 32 bit.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Acked-by: Kim Phillips <kim.phillips@freescale.com>

# 249d4dec 27-Jan-2010 Kumar Gala <galak@kernel.crashing.org>

Fix compiler warning in imximage.c due to getline prototype

imximage.c: In function 'imximage_parse_cfg_file':
imximage.c:142: warning: implicit declaration of function 'getline'

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>

# 8edcde5e 20-Jan-2010 Stefano Babic <sbabic@denx.de>

mkimage: Add Freescale imx Boot Image support (imximage)

This patch adds support for "imximage" (MX Boot Image)
to the mkimage utility. The imximage is used on the Freescales's
MX.25, MX.35 and MX.51 processors.

Further details under doc/README.imximage.

This patch was tested on a Freescale mx51evk board.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 507a70b1 21-Nov-2022 Mikhail Ilin <ilin.mikhail.ol@gmail.com>

tools: imximage: Fix check array index

The struct dcd_v1_t is initialized to MAX_HW_CFG_SIZE_V1 (60)
structs 'dcd_type_addr_data_t', so the indexes to use on its elements
are [0,59]. But on line 478, the variable 'length' can take on the value
60, which applies to array overflow: cd_v1->addr_data[length].type Thus,
it is necessary to tighten the check on the 'size' variable on line 463.

Fixes: 0b0c6af38738 ("Prepare v2020.01")
Signed-off-by: Mikhail Ilin <ilin.mikhail.ol@gmail.com>

# 0c2b03ca 28-Jul-2020 Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

tools/imximage: fix DCD Blocks message output order

The correct order is load address, offset, length. The order was
accidentally switched a while ago; make it match the HAB Blocks output and
what CST expects again.

Fixes: e97bdfa5da70 ("tools/imximage: share DCD information via Kconfig")
Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

# e97bdfa5 11-Dec-2019 Jorge Ramirez-Ortiz <jorge@foundries.io>

tools/imximage: share DCD information via Kconfig

IMX based platforms can have the DCD table located on different
addresses due to differences in their memory maps (ie iMX7ULP).

This information is required by the user to sign the images for secure
boot so continue making it accessible via mkimage.

Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
Reviewed-by: Fabio Estevam <festevam@gmail.com>
Signed-off-by: Stefano Babic <sbabic@denx.de>

# 7e1a0483 06-Jul-2018 Holger Dengler <dengler@linutronix.de>

tools/imximage: get HAB information from header

Signing parts of a u-boot imximage for image verification in High
Assurance Boot (HAB) in a post-build process, requires some
information from the imximage header. Currently, this information is
only provided during the image build, which makes the transfer of this
information to the post-build process harder than necessary.

The i.MX HAB information (start and length) can be calculated either
by using information from the image-configuration file, or from the
information in the flash header of the imximage.
The advantage of using information from flash header is, that they are
not only available during image creation, but also available if
existing images are processed.

Example:
$ tools/mkimage -l u-boot.imx
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 483328 Bytes = 472.00 KiB = 0.46 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00071c00
DCD Blocks: 0x00910000 0x0000002c 0x00000208

Signed-off-by: Holger Dengler <dengler@linutronix.de>
Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# f3c32628 15-Apr-2018 Fabio Estevam <fabio.estevam@nxp.com>

Revert "imximage: Remove failure when no IVT offset is found"

This reverts commit b5b0e4e351e20a606de22db6a56ad6bc1e2aa8fd.

Commit f916757300c1 ("imx: Create distinct pre-processed mkimage
config files") provided a proper fix for the parallel mkimage
config files build failure, so the original workaround can be
safely reverted now.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# af1b492d 24-Apr-2018 Bryan O'Donoghue <bryan.odonoghue@linaro.org>

tools/imximage: Fix fruity lack of 0x prefix in DCD Blocks

commit 8519c9c98ad6 ("tools/imximage: use 0x prefix in HAB Blocks line")
adds an 0x prefix to each HAB Block number to make it easier for host tools
to process the HAB Block output, however it neglects to apply the same
prefix to the DCD Blocks directive. You need the DCD Blocks directive if
you are making a u-boot recovery image which the BootROM will accept via
the USB upload utility.

This disparity results in a fruity output like this with HAB Blocks
prefixed but DCD Blocks not prefixed - which is pretty inconsistent.

This patch fixes the difference assuming the original commit was a
legitimate change.

Old:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 00910000 0000002c 000001d4

New:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 0x00910000 0x0000002c 0x000001d4

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Breno Lima <breno.lima@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>

# 8519c9c9 22-Mar-2018 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

tools/imximage: use 0x prefix in HAB Blocks line

The u-boot-ivt.img.log file contains 0x prefixes in the HAB Blocks line,
while the SPL.log does not. For consistency, and to make it easier to
extract and put into a .csf file for use with NXP's code signing tool,
add 0x prefixes here.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Tested-by: Breno Lima <breno.lima@nxp.com>

# b5b0e4e3 09-Mar-2018 Fabio Estevam <fabio.estevam@nxp.com>

imximage: Remove failure when no IVT offset is found

Sometimes imximage throws the following error:

CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
MKIMAGE u-boot-dtb.imx
Error: No BOOT_FROM tag in board/freescale/vf610twr/imximage.cfg.cfgtmp
arch/arm/mach-imx/Makefile:100: recipe for target 'u-boot-dtb.imx' failed

Later on, when running mkimage for the u-boot.imx it will succeed in
finding the IVT offset.

Looks like some race condition happening during parallel build when
processing mkimage for u-boot-dtb.imx and u-boot.imx.

A proper fix still needs to be implemented, but as a workaround let's
remove the error when the IVT offset is not found.

It is useful to have such message, especially during bring-up phase,
but the build error that it causes is severe, so better avoid the
build error for now.

The error checking can be re-implemented later when we have a proper
fix.

Reported-by: Breno Lima <breno.lima@nxp.com>
Reported-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

# 3e0a71c1 16-Mar-2017 Peng Fan <peng.fan@nxp.com>

tools: imximage: add set bit command

Add set bit command support.
Usage: SET_BIT 4 [address] [bitmask]

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# 97f17fa6 02-Jan-2017 Martin Kaiser <martin@kaiser.cx>

tools: imximage: refactor header length calculations for imximage v1

We can use the same header length calculations for both imximage v1 and
v2. This addresses TODO comments about imximage v1 in the current code.

With this patch applied, *header_size_ptr in imximage_set_header() will
have the correct value for both imximage v1 and v2. This is necessary
for people wanting to add proprietary data behind the created imximage.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
Cc: sbabic@denx.de

# e5491f3e 16-Nov-2016 Eric Nelson <eric@nelint.com>

tools: imximage: display DCD block offset, length

These values can be used to sign a U-Boot image for use when
loading an image through the Serial Download Protocol (SDP).

Note that the address of 0x910000 is usable with the stock
configuration of imx_usb_loader on i.MX6 and i.MX7 SOCs:

https://github.com/boundarydevices/imx_usb_loader/blob/master/mx6_usb_work.conf#L3

Refer to the section on imx_usb_loader in this post for more
details:

https://boundarydevices.com/high-assurance-boot-hab-dummies/

Signed-off-by: Eric Nelson <eric@nelint.com>

# 2a380ccc 03-Nov-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: check return value when open the plugin file

Check return value when open the plugin file.

Coverity report:
** CID 153926: Error handling issues (NEGATIVE_RETURNS)
/tools/imximage.c: 542 in copy_plugin_code()

ifd = open(plugin_file, O_RDONLY|O_BINARY);
>>> CID 153926: Error handling issues (NEGATIVE_RETURNS)
>>> "ifd" is passed to a parameter that cannot be negative.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reported-by: Coverity (CID: 153926)
Reviewed-by: Tom Rini <trini@konsulko.com>

# b55e4f48 11-Oct-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: add plugin support

Add plugin support for imximage.

Define CONFIG_USE_IMXIMG_PLUGIN in defconfig to enable using plugin.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Eric Nelson <eric@nelint.com>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 0782a880 02-May-2016 Adrian Alonso <adrian.alonso@nxp.com>

imx: tools: imximage: fix CLR bit command

Fix incorrect parametr in CMD_CHECK_BITS_CLR command
Pass CLR parameter to DCD header for CMD_CHECK_BITS_CLR

Signed-off-by: Adrian Alonso <adrian.alonso@nxp.com>

# 61903b75 14-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix commands other than write_data

When CHECK_BITS_SET was added, they forgot to add
a new command table, and instead overwrote the
previous table.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Tested-by: Fabio Estevam <fabio.estevam@freescale.com>

# 835c30e3 21-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: header.length of 4 is valid

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# b893c989 09-Jul-2015 Baruch Siach <baruch@tkos.co.il>

tools/imximage: set DCD pointer to NULL when its length is 0

When dcd_len is 0 the Write Data command that the set_dcd_rst_v2() routine
generates is empty. This causes HAB to complain that the command is invalid.

--------- HAB Event 1 -----------------
event data:
0xdb 0x00 0x0c 0x41 0x33 0x06 0xc0 0x00
0xcc 0x00 0x04 0x04

To fix this set the DCD pointer in the IVT to NULL in this case. The DCD header
itself is still needed for detect_imximage_version() to determine the image
version.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Acked-by: Stefano Babic <sbabic@denx.de>

# 0b7f7c33 20-Jul-2015 Adrian Alonso <aalonso@freescale.com>

imx: imximage: add new CHECK/CLR BIT command

* Extend imximage DCD version 2 to support DCD commands
CMD_WRITE_CLR_BIT 4 [address] [mask bit] means:
while ((*address & ~mask) != 0);
CMD_CHECK_BITS_SET 4 [address] [mask bit] means:
while ((*address & mask) != mask);
CMD_CHECK_BITS_CLR 4 [address] [mask bit] means:
*address = *address & ~mask;
* Add set_dcd_param_v2 helper function to set DCD
command parameters

Signed-off-by: Adrian Alonso <aalonso@freescale.com>
Signed-off-by: Peng Fan <Peng.Fan@freescale.com>

# 307367ea 10-Feb-2015 Tom Rini <trini@konsulko.com>

Merge branch 'master' of git://www.denx.de/git/u-boot-imx


# 9598f8c3 13-Jan-2015 Ye.Li <B37916@freescale.com>

imx: imximage: Add QuadSPI boot support

Add QuadSPI boot support to imximage tool.

Note: The QuadSPI configuration parameters at offset 0x400 are not
included in this patch. Need other tools to generate the parameters
part.

Signed-off-by: Ye.Li <B37916@freescale.com>

# a93648d1 14-Jan-2015 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

imagetool: replace image registration function by linker_lists feature

The registration was introduced in commit f86ed6a8d52c99bb2d17d3cac1647edca0c4399c

This commit also removes all registration functions, and the member "next"
from image_type_params struct

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

# de979804 30-Oct-2014 Ye.Li <B37916@freescale.com>

imximage: Fix the bootdata.size calculation

In system boot chapter of i.MX6 reference manual, the "Image Vector Table"
figure shows the bootdata.start points to the beginning of the destination
memory. It means the bootdata.size should contain the IVT offset part,
but the calculation in imximage tool does not have.

We found this issue when booting from QuadSPI NOR on i.MX6SX. The u-boot
runs into abnormal (crash or stop) after booting. After checked the destination
memory where the image is loaded to, there are hundreds of bytes at
the image end are not loaded into memory. Since there is a 4096 bytes
round in the calculation, for the booting devices using smaller IVT offset,
such as SD and SPI booting, they are not easy to reproduce.

Signed-off-by: Ye.Li <B37916@freescale.com>

# 03ea24b2 20-Aug-2014 Ye.Li <Ye.Li@freescale.com>

imximage: Fix imximage IVT bug for EIM-NOR boot

The load region size of EIM-NOR are defined to 0. For this case,
the parameter "imximage_init_loadsize" must be calculated.
The imximage tool implements the calculation in the "imximage_generate"
function, but the following function "imximage_set_header" resets the value
and not calculate. This bug cause some fields of IVT head are not
correct, for example the boot_data and DCD overlay the application area.

Signed-off-by: Ye.Li <B37916@freescale.com>

# f86ed6a8 01-Dec-2013 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

tools: moved code common to all image tools to a separated module.

In order to avoid duplicating code and keep only one point of modification,
the functions, structs and defines useful for "dumpimage" were moved from
"mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS,
and so on) and image tools (mkimage and dumpimage). Any tool may initialize the
"imagetool" through register_image_tool() function, while the image types
register themselves within an image tool using the register_image_type()
function:

+---------------+
+------| fit_image |
+--------------+ +-----------+ | +---------------+
| mkimage |--------> | | <-----+
+--------------+ | | +---------------+
| imagetool | <------------| imximage |
+--------------+ | | +---------------+
| dumpimage |--------> | | <-----+
+--------------+ +-----------+ | +---------------+
+------| default_image |
+---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make
clear its general purpose.

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3765b3e7 07-Oct-2013 Wolfgang Denk <wd@denx.de>

Coding Style cleanup: remove trailing white space

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 72048bc3 20-Sep-2013 York Sun <yorksun@freescale.com>

tools/imximage.c: Fix compiling warning

Convert set_hdr_func(struct imx_header *imxhdr) to set_hdr_func(void)
to get rid of the warning

warning: ‘imxhdr’ is used uninitialized in this function

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 19d829fa 05-Sep-2013 Albert ARIBAUD <albert.u.boot@aribaud.net>

Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'

Conflicts:
drivers/serial/serial.c

The conflict above was a trivial case of adding one init
function in each branch, and manually resolved in merge.


# 0187c985 27-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: add support for setting the CSF into imximage

Add support for setting the CSF (Command Sequence File) pointer
which is used for HAB (High Assurance Boot) in the imximage by
adding e.g.

CSF 0x2000

in the imximage.cfg file.

This will set the CSF pointer accordingly just after the padded
data image area. The boot_data.length is adjusted with the
value from the imximage.cfg config file.

The resulting u-boot.imx can be signed with the FSL HAB tooling.
The generated CSF block needs to be appended to the u-boot.imx.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 01390aff 19-Aug-2013 Stefano Babic <sbabic@denx.de>

tools: add padding of data image file for imximage

Implement function vrec_header to be able to pad the final
data image file according the what has been calculated for
boot_data.length.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 377e367a 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: dynamically allocate imx_header in imximage

Change to dynamically allocate the imx_header to correctly
allocate the IVT, Boot Data and DCD at correct locations
depending on the boot media.

Also check that the Image Vector Table Offset + IVT +
Boot Data + DCD <= Initial Load Region Size.

Previously struct imx_header was always 4096 bytes and was
not dealing correctly with the Image Vector Table Offset.

Now, the memory allocation looks for e.g. SD boot like this

Storage u-boot.imx RAM
Device

00000000 177ff000 <--------------
|
00000400 00000000 d1 00 20 40 IVT.header 177ff400 <------- |
00000404 00000004 00 00 80 17 IVT.entry 177ff404 ----------- |
00000408 00000008 00 00 00 00 IVT.reserved1 177ff408 | | |
0000040C 0000000C 2c f4 7f 17 IVT.dcd 177ff40C ------ | | |
00000410 00000010 20 f4 7f 17 IVT.boot 177ff410 ---- | | | |
00000414 00000014 00 f4 7f 17 IVT.self 177ff414 -------- | |
00000418 00000018 00 00 00 00 IVT.csf 177ff418 | | | |
0000041C 0000001C 00 00 00 00 IVT.reserved2 177ff41C | | | |
00000420 00000020 00 f0 7f 17 BootData.start 177ff420 <--- | | ---
00000424 00000024 00 60 03 00 BootData.length 177ff424 | |
00000428 00000028 00 00 00 00 BootData.plugin 177ff428 | |
0000042C 0000002C d2 03 30 40 DCD.header 177ff42C <----- |
... |
00001000 00000c00 13 00 00 ea U-Boot Start 17800000 <----------

While at it also remove the unused #define HEADER_OFFSET.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 3150f92c 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: rename mximage_flash_offset to imximage_ivt_offset

This better reflects the naming from the Reference Manual
as well as fits better since "flash" is not really applicabe
for SATA.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 4655d40f 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: imx_header should not include flash_offset

Doing a make distclean; make mx6qsabresd_config; make
and hexdump -C u-boot.imx | less

...
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000003f0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 |................|
^^^^^^^^^^^
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|
...

shows the flash_offset value being written into the final
generated image, wich is not correct.

Instead create flash_offset as static variable such that the
generated image is "clean".

00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 895d9966 20-Apr-2013 Marek Vasut <marex@denx.de>

imx: Align the imximage header and payload to multiples of 4k

The MX53 ROM loads the data from NAND in multiples of pages and
supports maximum page size of 4k. Thus, align the image and header
to 4k to be safe from ROM bugs.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>

# 6cb83829 25-Apr-2013 Marek Vasut <marex@denx.de>

tools: arm: imx: Implement BOOT_OFFSET command for imximage

Implement BOOT_OFFSET command for imximage. This command is parallel
to current BOOT_FROM command, but allows more flexibility in configuring
arbitrary image header offset. Also add an imximage.cfg with default
offset values into arm/arch/imx-common/ so the board-specific imximage.cfg
can include this file to avoid magic constants.

The syntax of BOOT_OFFSET command is "BOOT_OFFSET <u32 offset>".

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Acked-by: Stefano Babic <sbabic@denx.de>
Acked-by: Stefan Roese <sr@denx.de>

# 6e083857 02-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Let .name field be more generic

Since this structure is not i.MX5x specific, remove the '5x' to make it more
generic.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# 1411fb37 03-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Load a size that is multiple of 512

In order to mx53 ROM to properly load the U-boot image, its header size should
be multiple of 512 bytes.

This issue was observed with gcc 4.6.2/4.7.3, which caused data aborts:

U-Boot 2013.01-rc2-00172-gf8cfcf1-dirty (Dec 26 2012 - 13:13:28)

Board: MX53 LOCO
I2C: ready
DRAM: 1 GiB
MMC: FSL_SDHC: 0, FSL_SDHC: 1
In: serial
Out: serial
Err: serial
CPU: Freescale i.MX53 family rev2.1 at 1000 MHz
Reset cause: WDOG
Net: FEC
Warning: FEC using MAC address from net device

Hit any key to stop autoboot: 0
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<aff72220>] lr : [<aff721fc>]
sp : af565e20 ip : af566918 fp : 00000000
r10: 00000003 r9 : affabb5b r8 : af565f58
r7 : 00000000 r6 : 36747fff r5 : af5668e8 r4 : 36747fff
r3 : af5668ec r2 : af5668eb r1 : 00000000 r0 : af5668e8
Flags: NzcV IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...

,and this patch fixes it.

Also, even though the ROUND macro is already defined in common.h,
the reason for redefining it in image.h is explained by Stefano Babic:

"I will remark a previous comment - even if including common.h seems a
good idea to avoid duplications, it makes tools like mkimage to depend
on the selected board, because <board>_config must run. Even if this is
not a problem for us u-boot developers, it becomes an issue when these
tools are included in distros (like u-boot-tools in Ubuntu) and cannot
be packaged."

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# ab857f26 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: make set_imx_hdr_v1/v2 easier to read

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# ad0826dc 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: change parameters to set_imx_hdr

Call with the value the function will use
instead of going through a pointer.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 24331982 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: delay setting of image size

When later we change to variable length
header, we won't know the file size when
set_imx_hdr is called. So this is prep work.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 348ca8ef 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix size of image to load.

sbuf->st_size already includes sizeof(struct imx_header),
so remove extra addition.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 8d8cc828 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: move flash_offset check to common location

Both set_imx_hdr_v1 and set_imx_hdr_v2 perform the
same check. Move check to before the set_imx_hdr call.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# f14e6258 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: remove redundant setting of app_dest_ptr

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 4d5fa985 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: check dcd_len as entries added

Before the len was checked after the entire file
was processed, so it could have already overflowed.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 49d3e272 22-Feb-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Remove overwriting of flash_offset

The flash header supports different flash offsets for different
boot devices. E.g. parallel NOR or OneNAND use a different offset
than FLASH_OFFSET_STANDARD (== 0x400).

The flash offset is correctly read from the configuration in
parse_cfg_cmd(). But is then overwritten wrongly in set_imx_hdr_v1/2().

Fix this by removing this overwriting. Use the flash offset
correctly read from the configuration, instead.

If there is no flash_offset read from the configuration file, i.e.
the BOOT_FROM tag is missing, exit with an error message.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Jason Liu <liu.h.jason@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
Tested-by: Stefano Babic <sbabic@denx.de>

# bd25864c 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Sort bootops alphabetically

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 19b409c0 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Add support for i.MX6

The i.MX6 processor can boot from NOR flash and SATA disks,
additionally. Add the flash offsets for these additional
boot modes.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 8a1edd7d 19-Jan-2011 Liu Hui-R64343 <r64343@freescale.com>

imximage: Add MX53 boot image support

This patch add the MX53 boot image support.

This patch has been tested on Freescale MX53EVK board
and MX51EVK board.

Signed-off-by: Jason Liu <r64343@freescale.com>

# f581e3a2 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 0ad22703 22-Feb-2010 Kim Phillips <kim.phillips@freescale.com>

tools: fix imximage warning

Fix build warning:

Configuring for MPC837XEMDS board...
imximage.c: In function `imximage_parse_cfg_file':
imximage.c:146: warning: passing argument 2 of `getline' from incompatible pointer type
/usr/include/bits/stdio.h:116: note: expected `size_t *' but argument is of type `uint32_t *'

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>

# 5b28e913 05-Feb-2010 Stefano Babic <sbabic@denx.de>

mkimage: SEGFAULT with imximage on 64 bit systems

Running mkimage to generate an imximage produces a SEGFAULT
on 64 bit machines due to pointer arithmetic limited to 32 bit.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Acked-by: Kim Phillips <kim.phillips@freescale.com>

# 249d4dec 27-Jan-2010 Kumar Gala <galak@kernel.crashing.org>

Fix compiler warning in imximage.c due to getline prototype

imximage.c: In function 'imximage_parse_cfg_file':
imximage.c:142: warning: implicit declaration of function 'getline'

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>

# 8edcde5e 20-Jan-2010 Stefano Babic <sbabic@denx.de>

mkimage: Add Freescale imx Boot Image support (imximage)

This patch adds support for "imximage" (MX Boot Image)
to the mkimage utility. The imximage is used on the Freescales's
MX.25, MX.35 and MX.51 processors.

Further details under doc/README.imximage.

This patch was tested on a Freescale mx51evk board.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 0c2b03ca 28-Jul-2020 Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

tools/imximage: fix DCD Blocks message output order

The correct order is load address, offset, length. The order was
accidentally switched a while ago; make it match the HAB Blocks output and
what CST expects again.

Fixes: e97bdfa5da70 ("tools/imximage: share DCD information via Kconfig")
Signed-off-by: Matthias Schiffer <matthias.schiffer@ew.tq-group.com>

# e97bdfa5 11-Dec-2019 Jorge Ramirez-Ortiz <jorge@foundries.io>

tools/imximage: share DCD information via Kconfig

IMX based platforms can have the DCD table located on different
addresses due to differences in their memory maps (ie iMX7ULP).

This information is required by the user to sign the images for secure
boot so continue making it accessible via mkimage.

Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
Reviewed-by: Fabio Estevam <festevam@gmail.com>
Signed-off-by: Stefano Babic <sbabic@denx.de>

# 7e1a0483 06-Jul-2018 Holger Dengler <dengler@linutronix.de>

tools/imximage: get HAB information from header

Signing parts of a u-boot imximage for image verification in High
Assurance Boot (HAB) in a post-build process, requires some
information from the imximage header. Currently, this information is
only provided during the image build, which makes the transfer of this
information to the post-build process harder than necessary.

The i.MX HAB information (start and length) can be calculated either
by using information from the image-configuration file, or from the
information in the flash header of the imximage.
The advantage of using information from flash header is, that they are
not only available during image creation, but also available if
existing images are processed.

Example:
$ tools/mkimage -l u-boot.imx
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 483328 Bytes = 472.00 KiB = 0.46 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00071c00
DCD Blocks: 0x00910000 0x0000002c 0x00000208

Signed-off-by: Holger Dengler <dengler@linutronix.de>
Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# f3c32628 15-Apr-2018 Fabio Estevam <fabio.estevam@nxp.com>

Revert "imximage: Remove failure when no IVT offset is found"

This reverts commit b5b0e4e351e20a606de22db6a56ad6bc1e2aa8fd.

Commit f916757300c1 ("imx: Create distinct pre-processed mkimage
config files") provided a proper fix for the parallel mkimage
config files build failure, so the original workaround can be
safely reverted now.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# af1b492d 24-Apr-2018 Bryan O'Donoghue <bryan.odonoghue@linaro.org>

tools/imximage: Fix fruity lack of 0x prefix in DCD Blocks

commit 8519c9c98ad6 ("tools/imximage: use 0x prefix in HAB Blocks line")
adds an 0x prefix to each HAB Block number to make it easier for host tools
to process the HAB Block output, however it neglects to apply the same
prefix to the DCD Blocks directive. You need the DCD Blocks directive if
you are making a u-boot recovery image which the BootROM will accept via
the USB upload utility.

This disparity results in a fruity output like this with HAB Blocks
prefixed but DCD Blocks not prefixed - which is pretty inconsistent.

This patch fixes the difference assuming the original commit was a
legitimate change.

Old:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 00910000 0000002c 000001d4

New:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 0x00910000 0x0000002c 0x000001d4

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Breno Lima <breno.lima@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>

# 8519c9c9 22-Mar-2018 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

tools/imximage: use 0x prefix in HAB Blocks line

The u-boot-ivt.img.log file contains 0x prefixes in the HAB Blocks line,
while the SPL.log does not. For consistency, and to make it easier to
extract and put into a .csf file for use with NXP's code signing tool,
add 0x prefixes here.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Tested-by: Breno Lima <breno.lima@nxp.com>

# b5b0e4e3 09-Mar-2018 Fabio Estevam <fabio.estevam@nxp.com>

imximage: Remove failure when no IVT offset is found

Sometimes imximage throws the following error:

CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
MKIMAGE u-boot-dtb.imx
Error: No BOOT_FROM tag in board/freescale/vf610twr/imximage.cfg.cfgtmp
arch/arm/mach-imx/Makefile:100: recipe for target 'u-boot-dtb.imx' failed

Later on, when running mkimage for the u-boot.imx it will succeed in
finding the IVT offset.

Looks like some race condition happening during parallel build when
processing mkimage for u-boot-dtb.imx and u-boot.imx.

A proper fix still needs to be implemented, but as a workaround let's
remove the error when the IVT offset is not found.

It is useful to have such message, especially during bring-up phase,
but the build error that it causes is severe, so better avoid the
build error for now.

The error checking can be re-implemented later when we have a proper
fix.

Reported-by: Breno Lima <breno.lima@nxp.com>
Reported-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

# 3e0a71c1 16-Mar-2017 Peng Fan <peng.fan@nxp.com>

tools: imximage: add set bit command

Add set bit command support.
Usage: SET_BIT 4 [address] [bitmask]

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# 97f17fa6 02-Jan-2017 Martin Kaiser <martin@kaiser.cx>

tools: imximage: refactor header length calculations for imximage v1

We can use the same header length calculations for both imximage v1 and
v2. This addresses TODO comments about imximage v1 in the current code.

With this patch applied, *header_size_ptr in imximage_set_header() will
have the correct value for both imximage v1 and v2. This is necessary
for people wanting to add proprietary data behind the created imximage.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
Cc: sbabic@denx.de

# e5491f3e 16-Nov-2016 Eric Nelson <eric@nelint.com>

tools: imximage: display DCD block offset, length

These values can be used to sign a U-Boot image for use when
loading an image through the Serial Download Protocol (SDP).

Note that the address of 0x910000 is usable with the stock
configuration of imx_usb_loader on i.MX6 and i.MX7 SOCs:

https://github.com/boundarydevices/imx_usb_loader/blob/master/mx6_usb_work.conf#L3

Refer to the section on imx_usb_loader in this post for more
details:

https://boundarydevices.com/high-assurance-boot-hab-dummies/

Signed-off-by: Eric Nelson <eric@nelint.com>

# 2a380ccc 03-Nov-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: check return value when open the plugin file

Check return value when open the plugin file.

Coverity report:
** CID 153926: Error handling issues (NEGATIVE_RETURNS)
/tools/imximage.c: 542 in copy_plugin_code()

ifd = open(plugin_file, O_RDONLY|O_BINARY);
>>> CID 153926: Error handling issues (NEGATIVE_RETURNS)
>>> "ifd" is passed to a parameter that cannot be negative.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reported-by: Coverity (CID: 153926)
Reviewed-by: Tom Rini <trini@konsulko.com>

# b55e4f48 11-Oct-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: add plugin support

Add plugin support for imximage.

Define CONFIG_USE_IMXIMG_PLUGIN in defconfig to enable using plugin.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Eric Nelson <eric@nelint.com>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 0782a880 02-May-2016 Adrian Alonso <adrian.alonso@nxp.com>

imx: tools: imximage: fix CLR bit command

Fix incorrect parametr in CMD_CHECK_BITS_CLR command
Pass CLR parameter to DCD header for CMD_CHECK_BITS_CLR

Signed-off-by: Adrian Alonso <adrian.alonso@nxp.com>

# 61903b75 14-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix commands other than write_data

When CHECK_BITS_SET was added, they forgot to add
a new command table, and instead overwrote the
previous table.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Tested-by: Fabio Estevam <fabio.estevam@freescale.com>

# 835c30e3 21-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: header.length of 4 is valid

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# b893c989 09-Jul-2015 Baruch Siach <baruch@tkos.co.il>

tools/imximage: set DCD pointer to NULL when its length is 0

When dcd_len is 0 the Write Data command that the set_dcd_rst_v2() routine
generates is empty. This causes HAB to complain that the command is invalid.

--------- HAB Event 1 -----------------
event data:
0xdb 0x00 0x0c 0x41 0x33 0x06 0xc0 0x00
0xcc 0x00 0x04 0x04

To fix this set the DCD pointer in the IVT to NULL in this case. The DCD header
itself is still needed for detect_imximage_version() to determine the image
version.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Acked-by: Stefano Babic <sbabic@denx.de>

# 0b7f7c33 20-Jul-2015 Adrian Alonso <aalonso@freescale.com>

imx: imximage: add new CHECK/CLR BIT command

* Extend imximage DCD version 2 to support DCD commands
CMD_WRITE_CLR_BIT 4 [address] [mask bit] means:
while ((*address & ~mask) != 0);
CMD_CHECK_BITS_SET 4 [address] [mask bit] means:
while ((*address & mask) != mask);
CMD_CHECK_BITS_CLR 4 [address] [mask bit] means:
*address = *address & ~mask;
* Add set_dcd_param_v2 helper function to set DCD
command parameters

Signed-off-by: Adrian Alonso <aalonso@freescale.com>
Signed-off-by: Peng Fan <Peng.Fan@freescale.com>

# 307367ea 10-Feb-2015 Tom Rini <trini@ti.com>

Merge branch 'master' of git://www.denx.de/git/u-boot-imx


# 9598f8c3 13-Jan-2015 Ye.Li <B37916@freescale.com>

imx: imximage: Add QuadSPI boot support

Add QuadSPI boot support to imximage tool.

Note: The QuadSPI configuration parameters at offset 0x400 are not
included in this patch. Need other tools to generate the parameters
part.

Signed-off-by: Ye.Li <B37916@freescale.com>

# a93648d1 14-Jan-2015 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

imagetool: replace image registration function by linker_lists feature

The registration was introduced in commit f86ed6a8d52c99bb2d17d3cac1647edca0c4399c

This commit also removes all registration functions, and the member "next"
from image_type_params struct

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

# de979804 30-Oct-2014 Ye.Li <B37916@freescale.com>

imximage: Fix the bootdata.size calculation

In system boot chapter of i.MX6 reference manual, the "Image Vector Table"
figure shows the bootdata.start points to the beginning of the destination
memory. It means the bootdata.size should contain the IVT offset part,
but the calculation in imximage tool does not have.

We found this issue when booting from QuadSPI NOR on i.MX6SX. The u-boot
runs into abnormal (crash or stop) after booting. After checked the destination
memory where the image is loaded to, there are hundreds of bytes at
the image end are not loaded into memory. Since there is a 4096 bytes
round in the calculation, for the booting devices using smaller IVT offset,
such as SD and SPI booting, they are not easy to reproduce.

Signed-off-by: Ye.Li <B37916@freescale.com>

# 03ea24b2 20-Aug-2014 Ye.Li <Ye.Li@freescale.com>

imximage: Fix imximage IVT bug for EIM-NOR boot

The load region size of EIM-NOR are defined to 0. For this case,
the parameter "imximage_init_loadsize" must be calculated.
The imximage tool implements the calculation in the "imximage_generate"
function, but the following function "imximage_set_header" resets the value
and not calculate. This bug cause some fields of IVT head are not
correct, for example the boot_data and DCD overlay the application area.

Signed-off-by: Ye.Li <B37916@freescale.com>

# f86ed6a8 01-Dec-2013 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

tools: moved code common to all image tools to a separated module.

In order to avoid duplicating code and keep only one point of modification,
the functions, structs and defines useful for "dumpimage" were moved from
"mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS,
and so on) and image tools (mkimage and dumpimage). Any tool may initialize the
"imagetool" through register_image_tool() function, while the image types
register themselves within an image tool using the register_image_type()
function:

+---------------+
+------| fit_image |
+--------------+ +-----------+ | +---------------+
| mkimage |--------> | | <-----+
+--------------+ | | +---------------+
| imagetool | <------------| imximage |
+--------------+ | | +---------------+
| dumpimage |--------> | | <-----+
+--------------+ +-----------+ | +---------------+
+------| default_image |
+---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make
clear its general purpose.

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3765b3e7 07-Oct-2013 Wolfgang Denk <wd@denx.de>

Coding Style cleanup: remove trailing white space

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 72048bc3 20-Sep-2013 York Sun <yorksun@freescale.com>

tools/imximage.c: Fix compiling warning

Convert set_hdr_func(struct imx_header *imxhdr) to set_hdr_func(void)
to get rid of the warning

warning: ‘imxhdr’ is used uninitialized in this function

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 19d829fa 05-Sep-2013 Albert ARIBAUD <albert.u.boot@aribaud.net>

Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'

Conflicts:
drivers/serial/serial.c

The conflict above was a trivial case of adding one init
function in each branch, and manually resolved in merge.


# 0187c985 27-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: add support for setting the CSF into imximage

Add support for setting the CSF (Command Sequence File) pointer
which is used for HAB (High Assurance Boot) in the imximage by
adding e.g.

CSF 0x2000

in the imximage.cfg file.

This will set the CSF pointer accordingly just after the padded
data image area. The boot_data.length is adjusted with the
value from the imximage.cfg config file.

The resulting u-boot.imx can be signed with the FSL HAB tooling.
The generated CSF block needs to be appended to the u-boot.imx.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 01390aff 19-Aug-2013 Stefano Babic <sbabic@denx.de>

tools: add padding of data image file for imximage

Implement function vrec_header to be able to pad the final
data image file according the what has been calculated for
boot_data.length.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 377e367a 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: dynamically allocate imx_header in imximage

Change to dynamically allocate the imx_header to correctly
allocate the IVT, Boot Data and DCD at correct locations
depending on the boot media.

Also check that the Image Vector Table Offset + IVT +
Boot Data + DCD <= Initial Load Region Size.

Previously struct imx_header was always 4096 bytes and was
not dealing correctly with the Image Vector Table Offset.

Now, the memory allocation looks for e.g. SD boot like this

Storage u-boot.imx RAM
Device

00000000 177ff000 <--------------
|
00000400 00000000 d1 00 20 40 IVT.header 177ff400 <------- |
00000404 00000004 00 00 80 17 IVT.entry 177ff404 ----------- |
00000408 00000008 00 00 00 00 IVT.reserved1 177ff408 | | |
0000040C 0000000C 2c f4 7f 17 IVT.dcd 177ff40C ------ | | |
00000410 00000010 20 f4 7f 17 IVT.boot 177ff410 ---- | | | |
00000414 00000014 00 f4 7f 17 IVT.self 177ff414 -------- | |
00000418 00000018 00 00 00 00 IVT.csf 177ff418 | | | |
0000041C 0000001C 00 00 00 00 IVT.reserved2 177ff41C | | | |
00000420 00000020 00 f0 7f 17 BootData.start 177ff420 <--- | | ---
00000424 00000024 00 60 03 00 BootData.length 177ff424 | |
00000428 00000028 00 00 00 00 BootData.plugin 177ff428 | |
0000042C 0000002C d2 03 30 40 DCD.header 177ff42C <----- |
... |
00001000 00000c00 13 00 00 ea U-Boot Start 17800000 <----------

While at it also remove the unused #define HEADER_OFFSET.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 3150f92c 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: rename mximage_flash_offset to imximage_ivt_offset

This better reflects the naming from the Reference Manual
as well as fits better since "flash" is not really applicabe
for SATA.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 4655d40f 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: imx_header should not include flash_offset

Doing a make distclean; make mx6qsabresd_config; make
and hexdump -C u-boot.imx | less

...
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000003f0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 |................|
^^^^^^^^^^^
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|
...

shows the flash_offset value being written into the final
generated image, wich is not correct.

Instead create flash_offset as static variable such that the
generated image is "clean".

00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 895d9966 20-Apr-2013 Marek Vasut <marex@denx.de>

imx: Align the imximage header and payload to multiples of 4k

The MX53 ROM loads the data from NAND in multiples of pages and
supports maximum page size of 4k. Thus, align the image and header
to 4k to be safe from ROM bugs.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>

# 6cb83829 25-Apr-2013 Marek Vasut <marex@denx.de>

tools: arm: imx: Implement BOOT_OFFSET command for imximage

Implement BOOT_OFFSET command for imximage. This command is parallel
to current BOOT_FROM command, but allows more flexibility in configuring
arbitrary image header offset. Also add an imximage.cfg with default
offset values into arm/arch/imx-common/ so the board-specific imximage.cfg
can include this file to avoid magic constants.

The syntax of BOOT_OFFSET command is "BOOT_OFFSET <u32 offset>".

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Acked-by: Stefano Babic <sbabic@denx.de>
Acked-by: Stefan Roese <sr@denx.de>

# 6e083857 02-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Let .name field be more generic

Since this structure is not i.MX5x specific, remove the '5x' to make it more
generic.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# 1411fb37 03-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Load a size that is multiple of 512

In order to mx53 ROM to properly load the U-boot image, its header size should
be multiple of 512 bytes.

This issue was observed with gcc 4.6.2/4.7.3, which caused data aborts:

U-Boot 2013.01-rc2-00172-gf8cfcf1-dirty (Dec 26 2012 - 13:13:28)

Board: MX53 LOCO
I2C: ready
DRAM: 1 GiB
MMC: FSL_SDHC: 0, FSL_SDHC: 1
In: serial
Out: serial
Err: serial
CPU: Freescale i.MX53 family rev2.1 at 1000 MHz
Reset cause: WDOG
Net: FEC
Warning: FEC using MAC address from net device

Hit any key to stop autoboot: 0
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<aff72220>] lr : [<aff721fc>]
sp : af565e20 ip : af566918 fp : 00000000
r10: 00000003 r9 : affabb5b r8 : af565f58
r7 : 00000000 r6 : 36747fff r5 : af5668e8 r4 : 36747fff
r3 : af5668ec r2 : af5668eb r1 : 00000000 r0 : af5668e8
Flags: NzcV IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...

,and this patch fixes it.

Also, even though the ROUND macro is already defined in common.h,
the reason for redefining it in image.h is explained by Stefano Babic:

"I will remark a previous comment - even if including common.h seems a
good idea to avoid duplications, it makes tools like mkimage to depend
on the selected board, because <board>_config must run. Even if this is
not a problem for us u-boot developers, it becomes an issue when these
tools are included in distros (like u-boot-tools in Ubuntu) and cannot
be packaged."

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# ab857f26 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: make set_imx_hdr_v1/v2 easier to read

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# ad0826dc 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: change parameters to set_imx_hdr

Call with the value the function will use
instead of going through a pointer.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 24331982 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: delay setting of image size

When later we change to variable length
header, we won't know the file size when
set_imx_hdr is called. So this is prep work.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 348ca8ef 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix size of image to load.

sbuf->st_size already includes sizeof(struct imx_header),
so remove extra addition.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 8d8cc828 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: move flash_offset check to common location

Both set_imx_hdr_v1 and set_imx_hdr_v2 perform the
same check. Move check to before the set_imx_hdr call.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# f14e6258 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: remove redundant setting of app_dest_ptr

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 4d5fa985 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: check dcd_len as entries added

Before the len was checked after the entire file
was processed, so it could have already overflowed.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 49d3e272 22-Feb-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Remove overwriting of flash_offset

The flash header supports different flash offsets for different
boot devices. E.g. parallel NOR or OneNAND use a different offset
than FLASH_OFFSET_STANDARD (== 0x400).

The flash offset is correctly read from the configuration in
parse_cfg_cmd(). But is then overwritten wrongly in set_imx_hdr_v1/2().

Fix this by removing this overwriting. Use the flash offset
correctly read from the configuration, instead.

If there is no flash_offset read from the configuration file, i.e.
the BOOT_FROM tag is missing, exit with an error message.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Jason Liu <liu.h.jason@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
Tested-by: Stefano Babic <sbabic@denx.de>

# bd25864c 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Sort bootops alphabetically

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 19b409c0 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Add support for i.MX6

The i.MX6 processor can boot from NOR flash and SATA disks,
additionally. Add the flash offsets for these additional
boot modes.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 8a1edd7d 19-Jan-2011 Liu Hui-R64343 <r64343@freescale.com>

imximage: Add MX53 boot image support

This patch add the MX53 boot image support.

This patch has been tested on Freescale MX53EVK board
and MX51EVK board.

Signed-off-by: Jason Liu <r64343@freescale.com>

# f581e3a2 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 0ad22703 22-Feb-2010 Kim Phillips <kim.phillips@freescale.com>

tools: fix imximage warning

Fix build warning:

Configuring for MPC837XEMDS board...
imximage.c: In function `imximage_parse_cfg_file':
imximage.c:146: warning: passing argument 2 of `getline' from incompatible pointer type
/usr/include/bits/stdio.h:116: note: expected `size_t *' but argument is of type `uint32_t *'

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>

# 5b28e913 05-Feb-2010 Stefano Babic <sbabic@denx.de>

mkimage: SEGFAULT with imximage on 64 bit systems

Running mkimage to generate an imximage produces a SEGFAULT
on 64 bit machines due to pointer arithmetic limited to 32 bit.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Acked-by: Kim Phillips <kim.phillips@freescale.com>

# 249d4dec 27-Jan-2010 Kumar Gala <galak@kernel.crashing.org>

Fix compiler warning in imximage.c due to getline prototype

imximage.c: In function 'imximage_parse_cfg_file':
imximage.c:142: warning: implicit declaration of function 'getline'

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>

# 8edcde5e 20-Jan-2010 Stefano Babic <sbabic@denx.de>

mkimage: Add Freescale imx Boot Image support (imximage)

This patch adds support for "imximage" (MX Boot Image)
to the mkimage utility. The imximage is used on the Freescales's
MX.25, MX.35 and MX.51 processors.

Further details under doc/README.imximage.

This patch was tested on a Freescale mx51evk board.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# e97bdfa5 11-Dec-2019 Jorge Ramirez-Ortiz <jorge@foundries.io>

tools/imximage: share DCD information via Kconfig

IMX based platforms can have the DCD table located on different
addresses due to differences in their memory maps (ie iMX7ULP).

This information is required by the user to sign the images for secure
boot so continue making it accessible via mkimage.

Signed-off-by: Jorge Ramirez-Ortiz <jorge@foundries.io>
Reviewed-by: Fabio Estevam <festevam@gmail.com>
Signed-off-by: Stefano Babic <sbabic@denx.de>

# 7e1a0483 06-Jul-2018 Holger Dengler <dengler@linutronix.de>

tools/imximage: get HAB information from header

Signing parts of a u-boot imximage for image verification in High
Assurance Boot (HAB) in a post-build process, requires some
information from the imximage header. Currently, this information is
only provided during the image build, which makes the transfer of this
information to the post-build process harder than necessary.

The i.MX HAB information (start and length) can be calculated either
by using information from the image-configuration file, or from the
information in the flash header of the imximage.
The advantage of using information from flash header is, that they are
not only available during image creation, but also available if
existing images are processed.

Example:
$ tools/mkimage -l u-boot.imx
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 483328 Bytes = 472.00 KiB = 0.46 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00071c00
DCD Blocks: 0x00910000 0x0000002c 0x00000208

Signed-off-by: Holger Dengler <dengler@linutronix.de>
Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# f3c32628 15-Apr-2018 Fabio Estevam <fabio.estevam@nxp.com>

Revert "imximage: Remove failure when no IVT offset is found"

This reverts commit b5b0e4e351e20a606de22db6a56ad6bc1e2aa8fd.

Commit f916757300c1 ("imx: Create distinct pre-processed mkimage
config files") provided a proper fix for the parallel mkimage
config files build failure, so the original workaround can be
safely reverted now.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# af1b492d 24-Apr-2018 Bryan O'Donoghue <bryan.odonoghue@linaro.org>

tools/imximage: Fix fruity lack of 0x prefix in DCD Blocks

commit 8519c9c98ad6 ("tools/imximage: use 0x prefix in HAB Blocks line")
adds an 0x prefix to each HAB Block number to make it easier for host tools
to process the HAB Block output, however it neglects to apply the same
prefix to the DCD Blocks directive. You need the DCD Blocks directive if
you are making a u-boot recovery image which the BootROM will accept via
the USB upload utility.

This disparity results in a fruity output like this with HAB Blocks
prefixed but DCD Blocks not prefixed - which is pretty inconsistent.

This patch fixes the difference assuming the original commit was a
legitimate change.

Old:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 00910000 0000002c 000001d4

New:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 0x00910000 0x0000002c 0x000001d4

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Breno Lima <breno.lima@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>

# 8519c9c9 22-Mar-2018 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

tools/imximage: use 0x prefix in HAB Blocks line

The u-boot-ivt.img.log file contains 0x prefixes in the HAB Blocks line,
while the SPL.log does not. For consistency, and to make it easier to
extract and put into a .csf file for use with NXP's code signing tool,
add 0x prefixes here.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Tested-by: Breno Lima <breno.lima@nxp.com>

# b5b0e4e3 09-Mar-2018 Fabio Estevam <fabio.estevam@nxp.com>

imximage: Remove failure when no IVT offset is found

Sometimes imximage throws the following error:

CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
MKIMAGE u-boot-dtb.imx
Error: No BOOT_FROM tag in board/freescale/vf610twr/imximage.cfg.cfgtmp
arch/arm/mach-imx/Makefile:100: recipe for target 'u-boot-dtb.imx' failed

Later on, when running mkimage for the u-boot.imx it will succeed in
finding the IVT offset.

Looks like some race condition happening during parallel build when
processing mkimage for u-boot-dtb.imx and u-boot.imx.

A proper fix still needs to be implemented, but as a workaround let's
remove the error when the IVT offset is not found.

It is useful to have such message, especially during bring-up phase,
but the build error that it causes is severe, so better avoid the
build error for now.

The error checking can be re-implemented later when we have a proper
fix.

Reported-by: Breno Lima <breno.lima@nxp.com>
Reported-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>

# 3e0a71c1 16-Mar-2017 Peng Fan <peng.fan@nxp.com>

tools: imximage: add set bit command

Add set bit command support.
Usage: SET_BIT 4 [address] [bitmask]

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Stefano Babic <sbabic@denx.de>

# 97f17fa6 02-Jan-2017 Martin Kaiser <martin@kaiser.cx>

tools: imximage: refactor header length calculations for imximage v1

We can use the same header length calculations for both imximage v1 and
v2. This addresses TODO comments about imximage v1 in the current code.

With this patch applied, *header_size_ptr in imximage_set_header() will
have the correct value for both imximage v1 and v2. This is necessary
for people wanting to add proprietary data behind the created imximage.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
Cc: sbabic@denx.de

# e5491f3e 16-Nov-2016 Eric Nelson <eric@nelint.com>

tools: imximage: display DCD block offset, length

These values can be used to sign a U-Boot image for use when
loading an image through the Serial Download Protocol (SDP).

Note that the address of 0x910000 is usable with the stock
configuration of imx_usb_loader on i.MX6 and i.MX7 SOCs:

https://github.com/boundarydevices/imx_usb_loader/blob/master/mx6_usb_work.conf#L3

Refer to the section on imx_usb_loader in this post for more
details:

https://boundarydevices.com/high-assurance-boot-hab-dummies/

Signed-off-by: Eric Nelson <eric@nelint.com>

# 2a380ccc 03-Nov-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: check return value when open the plugin file

Check return value when open the plugin file.

Coverity report:
** CID 153926: Error handling issues (NEGATIVE_RETURNS)
/tools/imximage.c: 542 in copy_plugin_code()

ifd = open(plugin_file, O_RDONLY|O_BINARY);
>>> CID 153926: Error handling issues (NEGATIVE_RETURNS)
>>> "ifd" is passed to a parameter that cannot be negative.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reported-by: Coverity (CID: 153926)
Reviewed-by: Tom Rini <trini@konsulko.com>

# b55e4f48 11-Oct-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: add plugin support

Add plugin support for imximage.

Define CONFIG_USE_IMXIMG_PLUGIN in defconfig to enable using plugin.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Eric Nelson <eric@nelint.com>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 0782a880 02-May-2016 Adrian Alonso <adrian.alonso@nxp.com>

imx: tools: imximage: fix CLR bit command

Fix incorrect parametr in CMD_CHECK_BITS_CLR command
Pass CLR parameter to DCD header for CMD_CHECK_BITS_CLR

Signed-off-by: Adrian Alonso <adrian.alonso@nxp.com>

# 61903b75 14-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix commands other than write_data

When CHECK_BITS_SET was added, they forgot to add
a new command table, and instead overwrote the
previous table.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Tested-by: Fabio Estevam <fabio.estevam@freescale.com>

# 835c30e3 21-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: header.length of 4 is valid

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# b893c989 09-Jul-2015 Baruch Siach <baruch@tkos.co.il>

tools/imximage: set DCD pointer to NULL when its length is 0

When dcd_len is 0 the Write Data command that the set_dcd_rst_v2() routine
generates is empty. This causes HAB to complain that the command is invalid.

--------- HAB Event 1 -----------------
event data:
0xdb 0x00 0x0c 0x41 0x33 0x06 0xc0 0x00
0xcc 0x00 0x04 0x04

To fix this set the DCD pointer in the IVT to NULL in this case. The DCD header
itself is still needed for detect_imximage_version() to determine the image
version.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Acked-by: Stefano Babic <sbabic@denx.de>

# 0b7f7c33 20-Jul-2015 Adrian Alonso <aalonso@freescale.com>

imx: imximage: add new CHECK/CLR BIT command

* Extend imximage DCD version 2 to support DCD commands
CMD_WRITE_CLR_BIT 4 [address] [mask bit] means:
while ((*address & ~mask) != 0);
CMD_CHECK_BITS_SET 4 [address] [mask bit] means:
while ((*address & mask) != mask);
CMD_CHECK_BITS_CLR 4 [address] [mask bit] means:
*address = *address & ~mask;
* Add set_dcd_param_v2 helper function to set DCD
command parameters

Signed-off-by: Adrian Alonso <aalonso@freescale.com>
Signed-off-by: Peng Fan <Peng.Fan@freescale.com>

# 307367ea 10-Feb-2015 Tom Rini <trini@ti.com>

Merge branch 'master' of git://www.denx.de/git/u-boot-imx


# 9598f8c3 13-Jan-2015 Ye.Li <B37916@freescale.com>

imx: imximage: Add QuadSPI boot support

Add QuadSPI boot support to imximage tool.

Note: The QuadSPI configuration parameters at offset 0x400 are not
included in this patch. Need other tools to generate the parameters
part.

Signed-off-by: Ye.Li <B37916@freescale.com>

# a93648d1 14-Jan-2015 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

imagetool: replace image registration function by linker_lists feature

The registration was introduced in commit f86ed6a8d52c99bb2d17d3cac1647edca0c4399c

This commit also removes all registration functions, and the member "next"
from image_type_params struct

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

# de979804 30-Oct-2014 Ye.Li <B37916@freescale.com>

imximage: Fix the bootdata.size calculation

In system boot chapter of i.MX6 reference manual, the "Image Vector Table"
figure shows the bootdata.start points to the beginning of the destination
memory. It means the bootdata.size should contain the IVT offset part,
but the calculation in imximage tool does not have.

We found this issue when booting from QuadSPI NOR on i.MX6SX. The u-boot
runs into abnormal (crash or stop) after booting. After checked the destination
memory where the image is loaded to, there are hundreds of bytes at
the image end are not loaded into memory. Since there is a 4096 bytes
round in the calculation, for the booting devices using smaller IVT offset,
such as SD and SPI booting, they are not easy to reproduce.

Signed-off-by: Ye.Li <B37916@freescale.com>

# 03ea24b2 20-Aug-2014 Ye.Li <Ye.Li@freescale.com>

imximage: Fix imximage IVT bug for EIM-NOR boot

The load region size of EIM-NOR are defined to 0. For this case,
the parameter "imximage_init_loadsize" must be calculated.
The imximage tool implements the calculation in the "imximage_generate"
function, but the following function "imximage_set_header" resets the value
and not calculate. This bug cause some fields of IVT head are not
correct, for example the boot_data and DCD overlay the application area.

Signed-off-by: Ye.Li <B37916@freescale.com>

# f86ed6a8 01-Dec-2013 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

tools: moved code common to all image tools to a separated module.

In order to avoid duplicating code and keep only one point of modification,
the functions, structs and defines useful for "dumpimage" were moved from
"mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS,
and so on) and image tools (mkimage and dumpimage). Any tool may initialize the
"imagetool" through register_image_tool() function, while the image types
register themselves within an image tool using the register_image_type()
function:

+---------------+
+------| fit_image |
+--------------+ +-----------+ | +---------------+
| mkimage |--------> | | <-----+
+--------------+ | | +---------------+
| imagetool | <------------| imximage |
+--------------+ | | +---------------+
| dumpimage |--------> | | <-----+
+--------------+ +-----------+ | +---------------+
+------| default_image |
+---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make
clear its general purpose.

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 3765b3e7 07-Oct-2013 Wolfgang Denk <wd@denx.de>

Coding Style cleanup: remove trailing white space

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 72048bc3 20-Sep-2013 York Sun <yorksun@freescale.com>

tools/imximage.c: Fix compiling warning

Convert set_hdr_func(struct imx_header *imxhdr) to set_hdr_func(void)
to get rid of the warning

warning: ‘imxhdr’ is used uninitialized in this function

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 19d829fa 05-Sep-2013 Albert ARIBAUD <albert.u.boot@aribaud.net>

Merge branch 'u-boot-imx/master' into 'u-boot-arm/master'

Conflicts:
drivers/serial/serial.c

The conflict above was a trivial case of adding one init
function in each branch, and manually resolved in merge.


# 0187c985 27-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: add support for setting the CSF into imximage

Add support for setting the CSF (Command Sequence File) pointer
which is used for HAB (High Assurance Boot) in the imximage by
adding e.g.

CSF 0x2000

in the imximage.cfg file.

This will set the CSF pointer accordingly just after the padded
data image area. The boot_data.length is adjusted with the
value from the imximage.cfg config file.

The resulting u-boot.imx can be signed with the FSL HAB tooling.
The generated CSF block needs to be appended to the u-boot.imx.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 01390aff 19-Aug-2013 Stefano Babic <sbabic@denx.de>

tools: add padding of data image file for imximage

Implement function vrec_header to be able to pad the final
data image file according the what has been calculated for
boot_data.length.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 377e367a 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: dynamically allocate imx_header in imximage

Change to dynamically allocate the imx_header to correctly
allocate the IVT, Boot Data and DCD at correct locations
depending on the boot media.

Also check that the Image Vector Table Offset + IVT +
Boot Data + DCD <= Initial Load Region Size.

Previously struct imx_header was always 4096 bytes and was
not dealing correctly with the Image Vector Table Offset.

Now, the memory allocation looks for e.g. SD boot like this

Storage u-boot.imx RAM
Device

00000000 177ff000 <--------------
|
00000400 00000000 d1 00 20 40 IVT.header 177ff400 <------- |
00000404 00000004 00 00 80 17 IVT.entry 177ff404 ----------- |
00000408 00000008 00 00 00 00 IVT.reserved1 177ff408 | | |
0000040C 0000000C 2c f4 7f 17 IVT.dcd 177ff40C ------ | | |
00000410 00000010 20 f4 7f 17 IVT.boot 177ff410 ---- | | | |
00000414 00000014 00 f4 7f 17 IVT.self 177ff414 -------- | |
00000418 00000018 00 00 00 00 IVT.csf 177ff418 | | | |
0000041C 0000001C 00 00 00 00 IVT.reserved2 177ff41C | | | |
00000420 00000020 00 f0 7f 17 BootData.start 177ff420 <--- | | ---
00000424 00000024 00 60 03 00 BootData.length 177ff424 | |
00000428 00000028 00 00 00 00 BootData.plugin 177ff428 | |
0000042C 0000002C d2 03 30 40 DCD.header 177ff42C <----- |
... |
00001000 00000c00 13 00 00 ea U-Boot Start 17800000 <----------

While at it also remove the unused #define HEADER_OFFSET.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 3150f92c 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: rename mximage_flash_offset to imximage_ivt_offset

This better reflects the naming from the Reference Manual
as well as fits better since "flash" is not really applicabe
for SATA.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 4655d40f 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: imx_header should not include flash_offset

Doing a make distclean; make mx6qsabresd_config; make
and hexdump -C u-boot.imx | less

...
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000003f0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 |................|
^^^^^^^^^^^
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|
...

shows the flash_offset value being written into the final
generated image, wich is not correct.

Instead create flash_offset as static variable such that the
generated image is "clean".

00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 895d9966 20-Apr-2013 Marek Vasut <marex@denx.de>

imx: Align the imximage header and payload to multiples of 4k

The MX53 ROM loads the data from NAND in multiples of pages and
supports maximum page size of 4k. Thus, align the image and header
to 4k to be safe from ROM bugs.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>

# 6cb83829 25-Apr-2013 Marek Vasut <marex@denx.de>

tools: arm: imx: Implement BOOT_OFFSET command for imximage

Implement BOOT_OFFSET command for imximage. This command is parallel
to current BOOT_FROM command, but allows more flexibility in configuring
arbitrary image header offset. Also add an imximage.cfg with default
offset values into arm/arch/imx-common/ so the board-specific imximage.cfg
can include this file to avoid magic constants.

The syntax of BOOT_OFFSET command is "BOOT_OFFSET <u32 offset>".

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Acked-by: Stefano Babic <sbabic@denx.de>
Acked-by: Stefan Roese <sr@denx.de>

# 6e083857 02-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Let .name field be more generic

Since this structure is not i.MX5x specific, remove the '5x' to make it more
generic.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# 1411fb37 03-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Load a size that is multiple of 512

In order to mx53 ROM to properly load the U-boot image, its header size should
be multiple of 512 bytes.

This issue was observed with gcc 4.6.2/4.7.3, which caused data aborts:

U-Boot 2013.01-rc2-00172-gf8cfcf1-dirty (Dec 26 2012 - 13:13:28)

Board: MX53 LOCO
I2C: ready
DRAM: 1 GiB
MMC: FSL_SDHC: 0, FSL_SDHC: 1
In: serial
Out: serial
Err: serial
CPU: Freescale i.MX53 family rev2.1 at 1000 MHz
Reset cause: WDOG
Net: FEC
Warning: FEC using MAC address from net device

Hit any key to stop autoboot: 0
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<aff72220>] lr : [<aff721fc>]
sp : af565e20 ip : af566918 fp : 00000000
r10: 00000003 r9 : affabb5b r8 : af565f58
r7 : 00000000 r6 : 36747fff r5 : af5668e8 r4 : 36747fff
r3 : af5668ec r2 : af5668eb r1 : 00000000 r0 : af5668e8
Flags: NzcV IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...

,and this patch fixes it.

Also, even though the ROUND macro is already defined in common.h,
the reason for redefining it in image.h is explained by Stefano Babic:

"I will remark a previous comment - even if including common.h seems a
good idea to avoid duplications, it makes tools like mkimage to depend
on the selected board, because <board>_config must run. Even if this is
not a problem for us u-boot developers, it becomes an issue when these
tools are included in distros (like u-boot-tools in Ubuntu) and cannot
be packaged."

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>

# ab857f26 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: make set_imx_hdr_v1/v2 easier to read

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# ad0826dc 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: change parameters to set_imx_hdr

Call with the value the function will use
instead of going through a pointer.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 24331982 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: delay setting of image size

When later we change to variable length
header, we won't know the file size when
set_imx_hdr is called. So this is prep work.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 348ca8ef 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix size of image to load.

sbuf->st_size already includes sizeof(struct imx_header),
so remove extra addition.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 8d8cc828 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: move flash_offset check to common location

Both set_imx_hdr_v1 and set_imx_hdr_v2 perform the
same check. Move check to before the set_imx_hdr call.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# f14e6258 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: remove redundant setting of app_dest_ptr

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>

# 4d5fa985 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: check dcd_len as entries added

Before the len was checked after the entire file
was processed, so it could have already overflowed.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>

# 49d3e272 22-Feb-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Remove overwriting of flash_offset

The flash header supports different flash offsets for different
boot devices. E.g. parallel NOR or OneNAND use a different offset
than FLASH_OFFSET_STANDARD (== 0x400).

The flash offset is correctly read from the configuration in
parse_cfg_cmd(). But is then overwritten wrongly in set_imx_hdr_v1/2().

Fix this by removing this overwriting. Use the flash offset
correctly read from the configuration, instead.

If there is no flash_offset read from the configuration file, i.e.
the BOOT_FROM tag is missing, exit with an error message.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Jason Liu <liu.h.jason@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
Tested-by: Stefano Babic <sbabic@denx.de>

# bd25864c 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Sort bootops alphabetically

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 19b409c0 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Add support for i.MX6

The i.MX6 processor can boot from NOR flash and SATA disks,
additionally. Add the flash offsets for these additional
boot modes.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>

# 8a1edd7d 19-Jan-2011 Liu Hui-R64343 <r64343@freescale.com>

imximage: Add MX53 boot image support

This patch add the MX53 boot image support.

This patch has been tested on Freescale MX53EVK board
and MX51EVK board.

Signed-off-by: Jason Liu <r64343@freescale.com>

# f581e3a2 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 0ad22703 22-Feb-2010 Kim Phillips <kim.phillips@freescale.com>

tools: fix imximage warning

Fix build warning:

Configuring for MPC837XEMDS board...
imximage.c: In function `imximage_parse_cfg_file':
imximage.c:146: warning: passing argument 2 of `getline' from incompatible pointer type
/usr/include/bits/stdio.h:116: note: expected `size_t *' but argument is of type `uint32_t *'

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>

# 5b28e913 05-Feb-2010 Stefano Babic <sbabic@denx.de>

mkimage: SEGFAULT with imximage on 64 bit systems

Running mkimage to generate an imximage produces a SEGFAULT
on 64 bit machines due to pointer arithmetic limited to 32 bit.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Acked-by: Kim Phillips <kim.phillips@freescale.com>

# 249d4dec 27-Jan-2010 Kumar Gala <galak@kernel.crashing.org>

Fix compiler warning in imximage.c due to getline prototype

imximage.c: In function 'imximage_parse_cfg_file':
imximage.c:142: warning: implicit declaration of function 'getline'

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>

# 8edcde5e 20-Jan-2010 Stefano Babic <sbabic@denx.de>

mkimage: Add Freescale imx Boot Image support (imximage)

This patch adds support for "imximage" (MX Boot Image)
to the mkimage utility. The imximage is used on the Freescales's
MX.25, MX.35 and MX.51 processors.

Further details under doc/README.imximage.

This patch was tested on a Freescale mx51evk board.

Signed-off-by: Stefano Babic <sbabic@denx.de>

# 7e1a0483 06-Jul-2018 Holger Dengler <dengler@linutronix.de>

tools/imximage: get HAB information from header

Signing parts of a u-boot imximage for image verification in High
Assurance Boot (HAB) in a post-build process, requires some
information from the imximage header. Currently, this information is
only provided during the image build, which makes the transfer of this
information to the post-build process harder than necessary.

The i.MX HAB information (start and length) can be calculated either
by using information from the image-configuration file, or from the
information in the flash header of the imximage.
The advantage of using information from flash header is, that they are
not only available during image creation, but also available if
existing images are processed.

Example:
$ tools/mkimage -l u-boot.imx
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 483328 Bytes = 472.00 KiB = 0.46 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00071c00
DCD Blocks: 0x00910000 0x0000002c 0x00000208

Signed-off-by: Holger Dengler <dengler@linutronix.de>
Tested-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>


# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>


# f3c32628 15-Apr-2018 Fabio Estevam <fabio.estevam@nxp.com>

Revert "imximage: Remove failure when no IVT offset is found"

This reverts commit b5b0e4e351e20a606de22db6a56ad6bc1e2aa8fd.

Commit f916757300c1 ("imx: Create distinct pre-processed mkimage
config files") provided a proper fix for the parallel mkimage
config files build failure, so the original workaround can be
safely reverted now.

Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>
Reviewed-by: Stefano Babic <sbabic@denx.de>


# af1b492d 24-Apr-2018 Bryan O'Donoghue <bryan.odonoghue@linaro.org>

tools/imximage: Fix fruity lack of 0x prefix in DCD Blocks

commit 8519c9c98ad6 ("tools/imximage: use 0x prefix in HAB Blocks line")
adds an 0x prefix to each HAB Block number to make it easier for host tools
to process the HAB Block output, however it neglects to apply the same
prefix to the DCD Blocks directive. You need the DCD Blocks directive if
you are making a u-boot recovery image which the BootROM will accept via
the USB upload utility.

This disparity results in a fruity output like this with HAB Blocks
prefixed but DCD Blocks not prefixed - which is pretty inconsistent.

This patch fixes the difference assuming the original commit was a
legitimate change.

Old:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 00910000 0000002c 000001d4

New:
Image Type: Freescale IMX Boot Image
Image Ver: 2 (i.MX53/6/7 compatible)
Mode: DCD
Data Size: 430080 Bytes = 420.00 KiB = 0.41 MiB
Load Address: 877ff420
Entry Point: 87800000
HAB Blocks: 0x877ff400 0x00000000 0x00066c00
DCD Blocks: 0x00910000 0x0000002c 0x000001d4

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Cc: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Cc: Fabio Estevam <fabio.estevam@nxp.com>
Cc: Breno Lima <breno.lima@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>


# 8519c9c9 22-Mar-2018 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

tools/imximage: use 0x prefix in HAB Blocks line

The u-boot-ivt.img.log file contains 0x prefixes in the HAB Blocks line,
while the SPL.log does not. For consistency, and to make it easier to
extract and put into a .csf file for use with NXP's code signing tool,
add 0x prefixes here.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Fabio Estevam <fabio.estevam@nxp.com>
Tested-by: Breno Lima <breno.lima@nxp.com>


# b5b0e4e3 09-Mar-2018 Fabio Estevam <fabio.estevam@nxp.com>

imximage: Remove failure when no IVT offset is found

Sometimes imximage throws the following error:

CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
CFGS board/freescale/vf610twr/imximage.cfg.cfgtmp
MKIMAGE u-boot-dtb.imx
Error: No BOOT_FROM tag in board/freescale/vf610twr/imximage.cfg.cfgtmp
arch/arm/mach-imx/Makefile:100: recipe for target 'u-boot-dtb.imx' failed

Later on, when running mkimage for the u-boot.imx it will succeed in
finding the IVT offset.

Looks like some race condition happening during parallel build when
processing mkimage for u-boot-dtb.imx and u-boot.imx.

A proper fix still needs to be implemented, but as a workaround let's
remove the error when the IVT offset is not found.

It is useful to have such message, especially during bring-up phase,
but the build error that it causes is severe, so better avoid the
build error for now.

The error checking can be re-implemented later when we have a proper
fix.

Reported-by: Breno Lima <breno.lima@nxp.com>
Reported-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
Signed-off-by: Fabio Estevam <fabio.estevam@nxp.com>


# 3e0a71c1 16-Mar-2017 Peng Fan <peng.fan@nxp.com>

tools: imximage: add set bit command

Add set bit command support.
Usage: SET_BIT 4 [address] [bitmask]

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Reviewed-by: Stefano Babic <sbabic@denx.de>


# 97f17fa6 02-Jan-2017 Martin Kaiser <martin@kaiser.cx>

tools: imximage: refactor header length calculations for imximage v1

We can use the same header length calculations for both imximage v1 and
v2. This addresses TODO comments about imximage v1 in the current code.

With this patch applied, *header_size_ptr in imximage_set_header() will
have the correct value for both imximage v1 and v2. This is necessary
for people wanting to add proprietary data behind the created imximage.

Signed-off-by: Martin Kaiser <martin@kaiser.cx>
Cc: sbabic@denx.de


# e5491f3e 16-Nov-2016 Eric Nelson <eric@nelint.com>

tools: imximage: display DCD block offset, length

These values can be used to sign a U-Boot image for use when
loading an image through the Serial Download Protocol (SDP).

Note that the address of 0x910000 is usable with the stock
configuration of imx_usb_loader on i.MX6 and i.MX7 SOCs:

https://github.com/boundarydevices/imx_usb_loader/blob/master/mx6_usb_work.conf#L3

Refer to the section on imx_usb_loader in this post for more
details:

https://boundarydevices.com/high-assurance-boot-hab-dummies/

Signed-off-by: Eric Nelson <eric@nelint.com>


# 2a380ccc 03-Nov-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: check return value when open the plugin file

Check return value when open the plugin file.

Coverity report:
** CID 153926: Error handling issues (NEGATIVE_RETURNS)
/tools/imximage.c: 542 in copy_plugin_code()

ifd = open(plugin_file, O_RDONLY|O_BINARY);
>>> CID 153926: Error handling issues (NEGATIVE_RETURNS)
>>> "ifd" is passed to a parameter that cannot be negative.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@konsulko.com>
Reported-by: Coverity (CID: 153926)
Reviewed-by: Tom Rini <trini@konsulko.com>


# b55e4f48 11-Oct-2016 Peng Fan <peng.fan@nxp.com>

tools: imximage: add plugin support

Add plugin support for imximage.

Define CONFIG_USE_IMXIMG_PLUGIN in defconfig to enable using plugin.

Signed-off-by: Peng Fan <peng.fan@nxp.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Eric Nelson <eric@nelint.com>
Cc: Ye Li <ye.li@nxp.com>
Reviewed-by: Tom Rini <trini@konsulko.com>


# 0782a880 02-May-2016 Adrian Alonso <adrian.alonso@nxp.com>

imx: tools: imximage: fix CLR bit command

Fix incorrect parametr in CMD_CHECK_BITS_CLR command
Pass CLR parameter to DCD header for CMD_CHECK_BITS_CLR

Signed-off-by: Adrian Alonso <adrian.alonso@nxp.com>


# 61903b75 14-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix commands other than write_data

When CHECK_BITS_SET was added, they forgot to add
a new command table, and instead overwrote the
previous table.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Tested-by: Fabio Estevam <fabio.estevam@freescale.com>


# 835c30e3 21-Sep-2015 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: header.length of 4 is valid

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>


# b893c989 09-Jul-2015 Baruch Siach <baruch@tkos.co.il>

tools/imximage: set DCD pointer to NULL when its length is 0

When dcd_len is 0 the Write Data command that the set_dcd_rst_v2() routine
generates is empty. This causes HAB to complain that the command is invalid.

--------- HAB Event 1 -----------------
event data:
0xdb 0x00 0x0c 0x41 0x33 0x06 0xc0 0x00
0xcc 0x00 0x04 0x04

To fix this set the DCD pointer in the IVT to NULL in this case. The DCD header
itself is still needed for detect_imximage_version() to determine the image
version.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
Acked-by: Stefano Babic <sbabic@denx.de>


# 0b7f7c33 20-Jul-2015 Adrian Alonso <aalonso@freescale.com>

imx: imximage: add new CHECK/CLR BIT command

* Extend imximage DCD version 2 to support DCD commands
CMD_WRITE_CLR_BIT 4 [address] [mask bit] means:
while ((*address & ~mask) != 0);
CMD_CHECK_BITS_SET 4 [address] [mask bit] means:
while ((*address & mask) != mask);
CMD_CHECK_BITS_CLR 4 [address] [mask bit] means:
*address = *address & ~mask;
* Add set_dcd_param_v2 helper function to set DCD
command parameters

Signed-off-by: Adrian Alonso <aalonso@freescale.com>
Signed-off-by: Peng Fan <Peng.Fan@freescale.com>


# 9598f8c3 13-Jan-2015 Ye.Li <B37916@freescale.com>

imx: imximage: Add QuadSPI boot support

Add QuadSPI boot support to imximage tool.

Note: The QuadSPI configuration parameters at offset 0x400 are not
included in this patch. Need other tools to generate the parameters
part.

Signed-off-by: Ye.Li <B37916@freescale.com>


# a93648d1 14-Jan-2015 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

imagetool: replace image registration function by linker_lists feature

The registration was introduced in commit f86ed6a8d52c99bb2d17d3cac1647edca0c4399c

This commit also removes all registration functions, and the member "next"
from image_type_params struct

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>


# de979804 30-Oct-2014 Ye.Li <B37916@freescale.com>

imximage: Fix the bootdata.size calculation

In system boot chapter of i.MX6 reference manual, the "Image Vector Table"
figure shows the bootdata.start points to the beginning of the destination
memory. It means the bootdata.size should contain the IVT offset part,
but the calculation in imximage tool does not have.

We found this issue when booting from QuadSPI NOR on i.MX6SX. The u-boot
runs into abnormal (crash or stop) after booting. After checked the destination
memory where the image is loaded to, there are hundreds of bytes at
the image end are not loaded into memory. Since there is a 4096 bytes
round in the calculation, for the booting devices using smaller IVT offset,
such as SD and SPI booting, they are not easy to reproduce.

Signed-off-by: Ye.Li <B37916@freescale.com>


# 03ea24b2 20-Aug-2014 Ye.Li <Ye.Li@freescale.com>

imximage: Fix imximage IVT bug for EIM-NOR boot

The load region size of EIM-NOR are defined to 0. For this case,
the parameter "imximage_init_loadsize" must be calculated.
The imximage tool implements the calculation in the "imximage_generate"
function, but the following function "imximage_set_header" resets the value
and not calculate. This bug cause some fields of IVT head are not
correct, for example the boot_data and DCD overlay the application area.

Signed-off-by: Ye.Li <B37916@freescale.com>


# f86ed6a8 01-Dec-2013 Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>

tools: moved code common to all image tools to a separated module.

In order to avoid duplicating code and keep only one point of modification,
the functions, structs and defines useful for "dumpimage" were moved from
"mkimage" to a common module called "imagetool".

This modification also weakens the coupling between image types (FIT, IMX, MXS,
and so on) and image tools (mkimage and dumpimage). Any tool may initialize the
"imagetool" through register_image_tool() function, while the image types
register themselves within an image tool using the register_image_type()
function:

+---------------+
+------| fit_image |
+--------------+ +-----------+ | +---------------+
| mkimage |--------> | | <-----+
+--------------+ | | +---------------+
| imagetool | <------------| imximage |
+--------------+ | | +---------------+
| dumpimage |--------> | | <-----+
+--------------+ +-----------+ | +---------------+
+------| default_image |
+---------------+

register_image_tool() register_image_type()

Also, the struct "mkimage_params" was renamed to "image_tool_params" to make
clear its general purpose.

Signed-off-by: Guilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Signed-off-by: Simon Glass <sjg@chromium.org>


# 3765b3e7 07-Oct-2013 Wolfgang Denk <wd@denx.de>

Coding Style cleanup: remove trailing white space

Signed-off-by: Wolfgang Denk <wd@denx.de>


# 72048bc3 20-Sep-2013 York Sun <yorksun@freescale.com>

tools/imximage.c: Fix compiling warning

Convert set_hdr_func(struct imx_header *imxhdr) to set_hdr_func(void)
to get rid of the warning

warning: ‘imxhdr’ is used uninitialized in this function

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Stefano Babic <sbabic@denx.de>


# 0187c985 27-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: add support for setting the CSF into imximage

Add support for setting the CSF (Command Sequence File) pointer
which is used for HAB (High Assurance Boot) in the imximage by
adding e.g.

CSF 0x2000

in the imximage.cfg file.

This will set the CSF pointer accordingly just after the padded
data image area. The boot_data.length is adjusted with the
value from the imximage.cfg config file.

The resulting u-boot.imx can be signed with the FSL HAB tooling.
The generated CSF block needs to be appended to the u-boot.imx.

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 01390aff 19-Aug-2013 Stefano Babic <sbabic@denx.de>

tools: add padding of data image file for imximage

Implement function vrec_header to be able to pad the final
data image file according the what has been calculated for
boot_data.length.

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 377e367a 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: dynamically allocate imx_header in imximage

Change to dynamically allocate the imx_header to correctly
allocate the IVT, Boot Data and DCD at correct locations
depending on the boot media.

Also check that the Image Vector Table Offset + IVT +
Boot Data + DCD <= Initial Load Region Size.

Previously struct imx_header was always 4096 bytes and was
not dealing correctly with the Image Vector Table Offset.

Now, the memory allocation looks for e.g. SD boot like this

Storage u-boot.imx RAM
Device

00000000 177ff000 <--------------
|
00000400 00000000 d1 00 20 40 IVT.header 177ff400 <------- |
00000404 00000004 00 00 80 17 IVT.entry 177ff404 ----------- |
00000408 00000008 00 00 00 00 IVT.reserved1 177ff408 | | |
0000040C 0000000C 2c f4 7f 17 IVT.dcd 177ff40C ------ | | |
00000410 00000010 20 f4 7f 17 IVT.boot 177ff410 ---- | | | |
00000414 00000014 00 f4 7f 17 IVT.self 177ff414 -------- | |
00000418 00000018 00 00 00 00 IVT.csf 177ff418 | | | |
0000041C 0000001C 00 00 00 00 IVT.reserved2 177ff41C | | | |
00000420 00000020 00 f0 7f 17 BootData.start 177ff420 <--- | | ---
00000424 00000024 00 60 03 00 BootData.length 177ff424 | |
00000428 00000028 00 00 00 00 BootData.plugin 177ff428 | |
0000042C 0000002C d2 03 30 40 DCD.header 177ff42C <----- |
... |
00001000 00000c00 13 00 00 ea U-Boot Start 17800000 <----------

While at it also remove the unused #define HEADER_OFFSET.

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 3150f92c 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: rename mximage_flash_offset to imximage_ivt_offset

This better reflects the naming from the Reference Manual
as well as fits better since "flash" is not really applicabe
for SATA.

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 4655d40f 26-Jun-2013 Stefano Babic <sbabic@denx.de>

tools: imx_header should not include flash_offset

Doing a make distclean; make mx6qsabresd_config; make
and hexdump -C u-boot.imx | less

...
00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000003f0 00 00 00 00 00 00 00 00 00 00 00 00 00 04 00 00 |................|
^^^^^^^^^^^
00000400 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|
...

shows the flash_offset value being written into the final
generated image, wich is not correct.

Instead create flash_offset as static variable such that the
generated image is "clean".

00000360 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00001000 13 00 00 ea 14 f0 9f e5 14 f0 9f e5 14 f0 9f e5 |...ê.ð.å.ð.å.ð.å|

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 2db1c3fc 30-Jul-2013 York Sun <yorksun@freescale.com>

tools/Makefile: Move _GNU_SOURCE to Makefile

Commit 669dfc2e adds libfdt_env.h to HOSTCPPFLAGS. It causes stdio.h
to be included before _GNU_SOURCE is defined in C files. On some old hosts
some prototypes are protected by #ifdef __USE_GNU, which is set when
_GNU_SOURCE is defined.

Signed-off-by: York Sun <yorksun@freescale.com>
Acked-by: Simon Glass <sjg@chromium.org>


# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>


# 895d9966 20-Apr-2013 Marek Vasut <marex@denx.de>

imx: Align the imximage header and payload to multiples of 4k

The MX53 ROM loads the data from NAND in multiples of pages and
supports maximum page size of 4k. Thus, align the image and header
to 4k to be safe from ROM bugs.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>


# 6cb83829 25-Apr-2013 Marek Vasut <marex@denx.de>

tools: arm: imx: Implement BOOT_OFFSET command for imximage

Implement BOOT_OFFSET command for imximage. This command is parallel
to current BOOT_FROM command, but allows more flexibility in configuring
arbitrary image header offset. Also add an imximage.cfg with default
offset values into arm/arch/imx-common/ so the board-specific imximage.cfg
can include this file to avoid magic constants.

The syntax of BOOT_OFFSET command is "BOOT_OFFSET <u32 offset>".

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: Albert ARIBAUD <albert.u.boot@aribaud.net>
Cc: Benoît Thébaudeau <benoit.thebaudeau@advansee.com>
Cc: Fabio Estevam <fabio.estevam@freescale.com>
Cc: Scott Wood <scottwood@freescale.com>
Cc: Stefano Babic <sbabic@denx.de>
Cc: Tom Rini <trini@ti.com>
Acked-by: Stefano Babic <sbabic@denx.de>
Acked-by: Stefan Roese <sr@denx.de>


# 6e083857 02-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Let .name field be more generic

Since this structure is not i.MX5x specific, remove the '5x' to make it more
generic.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>


# 1411fb37 03-Jan-2013 Fabio Estevam <fabio.estevam@freescale.com>

tools: imximage: Load a size that is multiple of 512

In order to mx53 ROM to properly load the U-boot image, its header size should
be multiple of 512 bytes.

This issue was observed with gcc 4.6.2/4.7.3, which caused data aborts:

U-Boot 2013.01-rc2-00172-gf8cfcf1-dirty (Dec 26 2012 - 13:13:28)

Board: MX53 LOCO
I2C: ready
DRAM: 1 GiB
MMC: FSL_SDHC: 0, FSL_SDHC: 1
In: serial
Out: serial
Err: serial
CPU: Freescale i.MX53 family rev2.1 at 1000 MHz
Reset cause: WDOG
Net: FEC
Warning: FEC using MAC address from net device

Hit any key to stop autoboot: 0
data abort

MAYBE you should read doc/README.arm-unaligned-accesses

pc : [<aff72220>] lr : [<aff721fc>]
sp : af565e20 ip : af566918 fp : 00000000
r10: 00000003 r9 : affabb5b r8 : af565f58
r7 : 00000000 r6 : 36747fff r5 : af5668e8 r4 : 36747fff
r3 : af5668ec r2 : af5668eb r1 : 00000000 r0 : af5668e8
Flags: NzcV IRQs off FIQs off Mode SVC_32
Resetting CPU ...

resetting ...

,and this patch fixes it.

Also, even though the ROUND macro is already defined in common.h,
the reason for redefining it in image.h is explained by Stefano Babic:

"I will remark a previous comment - even if including common.h seems a
good idea to avoid duplications, it makes tools like mkimage to depend
on the selected board, because <board>_config must run. Even if this is
not a problem for us u-boot developers, it becomes an issue when these
tools are included in distros (like u-boot-tools in Ubuntu) and cannot
be packaged."

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>


# ab857f26 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: make set_imx_hdr_v1/v2 easier to read

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>


# ad0826dc 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: change parameters to set_imx_hdr

Call with the value the function will use
instead of going through a pointer.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>


# 24331982 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: delay setting of image size

When later we change to variable length
header, we won't know the file size when
set_imx_hdr is called. So this is prep work.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>


# 348ca8ef 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: fix size of image to load.

sbuf->st_size already includes sizeof(struct imx_header),
so remove extra addition.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>


# 8d8cc828 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: move flash_offset check to common location

Both set_imx_hdr_v1 and set_imx_hdr_v2 perform the
same check. Move check to before the set_imx_hdr call.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>


# f14e6258 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: remove redundant setting of app_dest_ptr

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>
Acked-by: Stefano Babic <sbabic@denx.de>


# 4d5fa985 03-Oct-2012 Troy Kisky <troy.kisky@boundarydevices.com>

imximage: check dcd_len as entries added

Before the len was checked after the entire file
was processed, so it could have already overflowed.

Signed-off-by: Troy Kisky <troy.kisky@boundarydevices.com>


# 49d3e272 22-Feb-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Remove overwriting of flash_offset

The flash header supports different flash offsets for different
boot devices. E.g. parallel NOR or OneNAND use a different offset
than FLASH_OFFSET_STANDARD (== 0x400).

The flash offset is correctly read from the configuration in
parse_cfg_cmd(). But is then overwritten wrongly in set_imx_hdr_v1/2().

Fix this by removing this overwriting. Use the flash offset
correctly read from the configuration, instead.

If there is no flash_offset read from the configuration file, i.e.
the BOOT_FROM tag is missing, exit with an error message.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Jason Liu <liu.h.jason@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
Tested-by: Stefano Babic <sbabic@denx.de>


# bd25864c 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Sort bootops alphabetically

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
CC: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>


# 19b409c0 11-Jan-2012 Dirk Behme <dirk.behme@de.bosch.com>

imximage: Add support for i.MX6

The i.MX6 processor can boot from NOR flash and SATA disks,
additionally. Add the flash offsets for these additional
boot modes.

Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Signed-off-by: Fabio Estevam <festevam@gmail.com>
CC: Stefano Babic <sbabic@denx.de>
CC: Jason Liu <jason.hui@linaro.org>
Acked-by: Jason Liu <jason.hui@linaro.org>


# 8a1edd7d 19-Jan-2011 Liu Hui-R64343 <r64343@freescale.com>

imximage: Add MX53 boot image support

This patch add the MX53 boot image support.

This patch has been tested on Freescale MX53EVK board
and MX51EVK board.

Signed-off-by: Jason Liu <r64343@freescale.com>


# f581e3a2 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 6686f10d 12-Apr-2010 Stefano Babic <sbabic@denx.de>

mkimage: correct spelling error in imximage

Signed-off-by: Stefano Babic <sbabic@denx.de>


# 0ad22703 22-Feb-2010 Kim Phillips <kim.phillips@freescale.com>

tools: fix imximage warning

Fix build warning:

Configuring for MPC837XEMDS board...
imximage.c: In function `imximage_parse_cfg_file':
imximage.c:146: warning: passing argument 2 of `getline' from incompatible pointer type
/usr/include/bits/stdio.h:116: note: expected `size_t *' but argument is of type `uint32_t *'

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>


# 5b28e913 05-Feb-2010 Stefano Babic <sbabic@denx.de>

mkimage: SEGFAULT with imximage on 64 bit systems

Running mkimage to generate an imximage produces a SEGFAULT
on 64 bit machines due to pointer arithmetic limited to 32 bit.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Acked-by: Kim Phillips <kim.phillips@freescale.com>


# 249d4dec 27-Jan-2010 Kumar Gala <galak@kernel.crashing.org>

Fix compiler warning in imximage.c due to getline prototype

imximage.c: In function 'imximage_parse_cfg_file':
imximage.c:142: warning: implicit declaration of function 'getline'

Signed-off-by: Kumar Gala <galak@kernel.crashing.org>


# 8edcde5e 20-Jan-2010 Stefano Babic <sbabic@denx.de>

mkimage: Add Freescale imx Boot Image support (imximage)

This patch adds support for "imximage" (MX Boot Image)
to the mkimage utility. The imximage is used on the Freescales's
MX.25, MX.35 and MX.51 processors.

Further details under doc/README.imximage.

This patch was tested on a Freescale mx51evk board.

Signed-off-by: Stefano Babic <sbabic@denx.de>