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