183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
26bf4ca07SHeiko Schocher /*
36bf4ca07SHeiko Schocher * (C) Copyright 2014
46bf4ca07SHeiko Schocher * DENX Software Engineering
56bf4ca07SHeiko Schocher * Heiko Schocher <hs@denx.de>
66bf4ca07SHeiko Schocher *
76bf4ca07SHeiko Schocher * (C) Copyright 2008 Semihalf
86bf4ca07SHeiko Schocher *
96bf4ca07SHeiko Schocher * (C) Copyright 2000-2004
106bf4ca07SHeiko Schocher * DENX Software Engineering
116bf4ca07SHeiko Schocher * Wolfgang Denk, wd@denx.de
126bf4ca07SHeiko Schocher *
136bf4ca07SHeiko Schocher * Updated-by: Prafulla Wadaskar <prafulla@marvell.com>
146bf4ca07SHeiko Schocher * FIT image specific code abstracted from mkimage.c
156bf4ca07SHeiko Schocher * some functions added to address abstraction
166bf4ca07SHeiko Schocher *
176bf4ca07SHeiko Schocher * All rights reserved.
186bf4ca07SHeiko Schocher */
196bf4ca07SHeiko Schocher
206bf4ca07SHeiko Schocher #include "imagetool.h"
216bf4ca07SHeiko Schocher #include "mkimage.h"
226bf4ca07SHeiko Schocher #include "fit_common.h"
236bf4ca07SHeiko Schocher #include <image.h>
246bf4ca07SHeiko Schocher #include <u-boot/crc.h>
256bf4ca07SHeiko Schocher
fit_verify_header(unsigned char * ptr,int image_size,struct image_tool_params * params)266bf4ca07SHeiko Schocher int fit_verify_header(unsigned char *ptr, int image_size,
276bf4ca07SHeiko Schocher struct image_tool_params *params)
286bf4ca07SHeiko Schocher {
29*d32aa3caSJordan Hand if (fdt_check_header(ptr) != EXIT_SUCCESS || !fit_check_format(ptr))
30*d32aa3caSJordan Hand return EXIT_FAILURE;
31*d32aa3caSJordan Hand
32*d32aa3caSJordan Hand return EXIT_SUCCESS;
336bf4ca07SHeiko Schocher }
346bf4ca07SHeiko Schocher
fit_check_image_types(uint8_t type)356bf4ca07SHeiko Schocher int fit_check_image_types(uint8_t type)
366bf4ca07SHeiko Schocher {
376bf4ca07SHeiko Schocher if (type == IH_TYPE_FLATDT)
386bf4ca07SHeiko Schocher return EXIT_SUCCESS;
396bf4ca07SHeiko Schocher else
406bf4ca07SHeiko Schocher return EXIT_FAILURE;
416bf4ca07SHeiko Schocher }
426bf4ca07SHeiko Schocher
mmap_fdt(const char * cmdname,const char * fname,size_t size_inc,void ** blobp,struct stat * sbuf,bool delete_on_error)43a9468115SSimon Glass int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc,
44a9468115SSimon Glass void **blobp, struct stat *sbuf, bool delete_on_error)
456bf4ca07SHeiko Schocher {
466bf4ca07SHeiko Schocher void *ptr;
476bf4ca07SHeiko Schocher int fd;
486bf4ca07SHeiko Schocher
496bf4ca07SHeiko Schocher /* Load FIT blob into memory (we need to write hashes/signatures) */
506bf4ca07SHeiko Schocher fd = open(fname, O_RDWR | O_BINARY);
516bf4ca07SHeiko Schocher
526bf4ca07SHeiko Schocher if (fd < 0) {
536bf4ca07SHeiko Schocher fprintf(stderr, "%s: Can't open %s: %s\n",
546bf4ca07SHeiko Schocher cmdname, fname, strerror(errno));
55ef0af64bSSimon Glass goto err;
566bf4ca07SHeiko Schocher }
576bf4ca07SHeiko Schocher
586bf4ca07SHeiko Schocher if (fstat(fd, sbuf) < 0) {
596bf4ca07SHeiko Schocher fprintf(stderr, "%s: Can't stat %s: %s\n",
606bf4ca07SHeiko Schocher cmdname, fname, strerror(errno));
61ef0af64bSSimon Glass goto err;
626bf4ca07SHeiko Schocher }
636bf4ca07SHeiko Schocher
64a9468115SSimon Glass if (size_inc) {
65a9468115SSimon Glass sbuf->st_size += size_inc;
66a9468115SSimon Glass if (ftruncate(fd, sbuf->st_size)) {
67a9468115SSimon Glass fprintf(stderr, "%s: Can't expand %s: %s\n",
68a9468115SSimon Glass cmdname, fname, strerror(errno));
69a9468115SSimon Glass goto err;
70a9468115SSimon Glass }
71a9468115SSimon Glass }
72a9468115SSimon Glass
736bf4ca07SHeiko Schocher errno = 0;
746bf4ca07SHeiko Schocher ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
756bf4ca07SHeiko Schocher if ((ptr == MAP_FAILED) || (errno != 0)) {
766bf4ca07SHeiko Schocher fprintf(stderr, "%s: Can't read %s: %s\n",
776bf4ca07SHeiko Schocher cmdname, fname, strerror(errno));
78ef0af64bSSimon Glass goto err;
796bf4ca07SHeiko Schocher }
806bf4ca07SHeiko Schocher
816bf4ca07SHeiko Schocher /* check if ptr has a valid blob */
826bf4ca07SHeiko Schocher if (fdt_check_header(ptr)) {
836bf4ca07SHeiko Schocher fprintf(stderr, "%s: Invalid FIT blob\n", cmdname);
84ef0af64bSSimon Glass goto err;
856bf4ca07SHeiko Schocher }
866bf4ca07SHeiko Schocher
87a9468115SSimon Glass /* expand if needed */
88a9468115SSimon Glass if (size_inc) {
89a9468115SSimon Glass int ret;
90a9468115SSimon Glass
91a9468115SSimon Glass ret = fdt_open_into(ptr, ptr, sbuf->st_size);
92a9468115SSimon Glass if (ret) {
93a9468115SSimon Glass fprintf(stderr, "%s: Cannot expand FDT: %s\n",
94a9468115SSimon Glass cmdname, fdt_strerror(ret));
95a9468115SSimon Glass goto err;
96a9468115SSimon Glass }
97a9468115SSimon Glass }
98a9468115SSimon Glass
996bf4ca07SHeiko Schocher *blobp = ptr;
1006bf4ca07SHeiko Schocher return fd;
101ef0af64bSSimon Glass
102ef0af64bSSimon Glass err:
103ef0af64bSSimon Glass if (fd >= 0)
104ef0af64bSSimon Glass close(fd);
105ef0af64bSSimon Glass if (delete_on_error)
106ef0af64bSSimon Glass unlink(fname);
107ef0af64bSSimon Glass
108ef0af64bSSimon Glass return -1;
1096bf4ca07SHeiko Schocher }
110