Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2017 |
| 4 | * Texas Instruments, <www.ti.com> |
| 5 | * |
| 6 | * Franklin S Cooper Jr. <fcooper@ti.com> |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <boot_fit.h> |
| 10 | #include <common.h> |
| 11 | #include <errno.h> |
| 12 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 14 | #include <linux/libfdt.h> |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 15 | |
Jean-Jacques Hiblot | f1f8c4d | 2017-09-15 12:57:25 +0200 | [diff] [blame] | 16 | static int fdt_offset(const void *fit) |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 17 | { |
| 18 | int images, node, fdt_len, fdt_node, fdt_offset; |
| 19 | const char *fdt_name; |
| 20 | |
| 21 | node = fit_find_config_node(fit); |
| 22 | if (node < 0) |
| 23 | return node; |
| 24 | |
| 25 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 26 | if (images < 0) { |
| 27 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
Nobuhiro Iwamatsu | f96367a | 2017-08-05 05:47:02 +0900 | [diff] [blame] | 28 | return -EINVAL; |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | fdt_name = fdt_getprop(fit, node, FIT_FDT_PROP, &fdt_len); |
| 32 | if (!fdt_name) { |
| 33 | debug("%s: Cannot find fdt name property: %d\n", |
| 34 | __func__, fdt_len); |
| 35 | return -EINVAL; |
| 36 | } |
| 37 | |
| 38 | fdt_node = fdt_subnode_offset(fit, images, fdt_name); |
| 39 | if (fdt_node < 0) { |
| 40 | debug("%s: Cannot find fdt node '%s': %d\n", |
| 41 | __func__, fdt_name, fdt_node); |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | |
| 45 | fdt_offset = fdt_getprop_u32(fit, fdt_node, "data-offset"); |
| 46 | |
| 47 | if (fdt_offset == FDT_ERROR) |
| 48 | return -ENOENT; |
| 49 | |
| 50 | fdt_len = fdt_getprop_u32(fit, fdt_node, "data-size"); |
| 51 | |
| 52 | if (fdt_len < 0) |
| 53 | return fdt_len; |
| 54 | |
| 55 | return fdt_offset; |
| 56 | } |
| 57 | |
Jean-Jacques Hiblot | f1f8c4d | 2017-09-15 12:57:25 +0200 | [diff] [blame] | 58 | void *locate_dtb_in_fit(const void *fit) |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 59 | { |
| 60 | struct image_header *header; |
| 61 | int size; |
| 62 | int ret; |
| 63 | |
| 64 | size = fdt_totalsize(fit); |
| 65 | size = (size + 3) & ~3; |
| 66 | |
| 67 | header = (struct image_header *)fit; |
| 68 | |
| 69 | if (image_get_magic(header) != FDT_MAGIC) { |
| 70 | debug("No FIT image appended to U-boot\n"); |
| 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | ret = fdt_offset(fit); |
| 75 | |
Jean-Jacques Hiblot | a922d81 | 2017-09-15 12:57:26 +0200 | [diff] [blame] | 76 | if (ret < 0) |
Cooper Jr., Franklin | 899aad2 | 2017-06-16 17:25:06 -0500 | [diff] [blame] | 77 | return NULL; |
| 78 | else |
| 79 | return (void *)fit+size+ret; |
| 80 | } |