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