Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2014 |
| 3 | * DENX Software Engineering |
| 4 | * Heiko Schocher <hs@denx.de> |
| 5 | * |
| 6 | * (C) Copyright 2008 Semihalf |
| 7 | * |
| 8 | * (C) Copyright 2000-2004 |
| 9 | * DENX Software Engineering |
| 10 | * Wolfgang Denk, wd@denx.de |
| 11 | * |
| 12 | * Updated-by: Prafulla Wadaskar <prafulla@marvell.com> |
| 13 | * FIT image specific code abstracted from mkimage.c |
| 14 | * some functions added to address abstraction |
| 15 | * |
| 16 | * All rights reserved. |
| 17 | * |
| 18 | * SPDX-License-Identifier: GPL-2.0+ |
| 19 | */ |
| 20 | |
| 21 | #include "imagetool.h" |
| 22 | #include "mkimage.h" |
| 23 | #include "fit_common.h" |
| 24 | #include <image.h> |
| 25 | #include <u-boot/crc.h> |
| 26 | |
| 27 | int fit_verify_header(unsigned char *ptr, int image_size, |
| 28 | struct image_tool_params *params) |
| 29 | { |
| 30 | return fdt_check_header(ptr); |
| 31 | } |
| 32 | |
| 33 | int fit_check_image_types(uint8_t type) |
| 34 | { |
| 35 | if (type == IH_TYPE_FLATDT) |
| 36 | return EXIT_SUCCESS; |
| 37 | else |
| 38 | return EXIT_FAILURE; |
| 39 | } |
| 40 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 41 | int mmap_fdt(const char *cmdname, const char *fname, size_t size_inc, |
| 42 | void **blobp, struct stat *sbuf, bool delete_on_error) |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 43 | { |
| 44 | void *ptr; |
| 45 | int fd; |
| 46 | |
| 47 | /* Load FIT blob into memory (we need to write hashes/signatures) */ |
| 48 | fd = open(fname, O_RDWR | O_BINARY); |
| 49 | |
| 50 | if (fd < 0) { |
| 51 | fprintf(stderr, "%s: Can't open %s: %s\n", |
| 52 | cmdname, fname, strerror(errno)); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 53 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | if (fstat(fd, sbuf) < 0) { |
| 57 | fprintf(stderr, "%s: Can't stat %s: %s\n", |
| 58 | cmdname, fname, strerror(errno)); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 59 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 62 | if (size_inc) { |
| 63 | sbuf->st_size += size_inc; |
| 64 | if (ftruncate(fd, sbuf->st_size)) { |
| 65 | fprintf(stderr, "%s: Can't expand %s: %s\n", |
| 66 | cmdname, fname, strerror(errno)); |
| 67 | goto err; |
| 68 | } |
| 69 | } |
| 70 | |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 71 | errno = 0; |
| 72 | ptr = mmap(0, sbuf->st_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 73 | if ((ptr == MAP_FAILED) || (errno != 0)) { |
| 74 | fprintf(stderr, "%s: Can't read %s: %s\n", |
| 75 | cmdname, fname, strerror(errno)); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 76 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /* check if ptr has a valid blob */ |
| 80 | if (fdt_check_header(ptr)) { |
| 81 | fprintf(stderr, "%s: Invalid FIT blob\n", cmdname); |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 82 | goto err; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Simon Glass | a946811 | 2014-06-02 22:04:53 -0600 | [diff] [blame] | 85 | /* expand if needed */ |
| 86 | if (size_inc) { |
| 87 | int ret; |
| 88 | |
| 89 | ret = fdt_open_into(ptr, ptr, sbuf->st_size); |
| 90 | if (ret) { |
| 91 | fprintf(stderr, "%s: Cannot expand FDT: %s\n", |
| 92 | cmdname, fdt_strerror(ret)); |
| 93 | goto err; |
| 94 | } |
| 95 | } |
| 96 | |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 97 | *blobp = ptr; |
| 98 | return fd; |
Simon Glass | ef0af64 | 2014-06-02 22:04:52 -0600 | [diff] [blame] | 99 | |
| 100 | err: |
| 101 | if (fd >= 0) |
| 102 | close(fd); |
| 103 | if (delete_on_error) |
| 104 | unlink(fname); |
| 105 | |
| 106 | return -1; |
Heiko Schocher | 6bf4ca0 | 2014-03-03 12:19:29 +0100 | [diff] [blame] | 107 | } |