Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <errno.h> |
| 10 | #include <image.h> |
| 11 | #include <libfdt.h> |
| 12 | #include <spl.h> |
| 13 | |
| 14 | static ulong fdt_getprop_u32(const void *fdt, int node, const char *prop) |
| 15 | { |
| 16 | const u32 *cell; |
| 17 | int len; |
| 18 | |
| 19 | cell = fdt_getprop(fdt, node, prop, &len); |
| 20 | if (len != sizeof(*cell)) |
| 21 | return -1U; |
| 22 | return fdt32_to_cpu(*cell); |
| 23 | } |
| 24 | |
| 25 | static int spl_fit_select_fdt(const void *fdt, int images, int *fdt_offsetp) |
| 26 | { |
| 27 | const char *name, *fdt_name; |
| 28 | int conf, node, fdt_node; |
| 29 | int len; |
| 30 | |
| 31 | *fdt_offsetp = 0; |
| 32 | conf = fdt_path_offset(fdt, FIT_CONFS_PATH); |
| 33 | if (conf < 0) { |
| 34 | debug("%s: Cannot find /configurations node: %d\n", __func__, |
| 35 | conf); |
| 36 | return -EINVAL; |
| 37 | } |
| 38 | for (node = fdt_first_subnode(fdt, conf); |
| 39 | node >= 0; |
| 40 | node = fdt_next_subnode(fdt, node)) { |
| 41 | name = fdt_getprop(fdt, node, "description", &len); |
Michal Simek | 8667f8c | 2016-05-04 15:08:00 +0200 | [diff] [blame] | 42 | if (!name) { |
| 43 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 44 | printf("%s: Missing FDT description in DTB\n", |
| 45 | __func__); |
| 46 | #endif |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 47 | return -EINVAL; |
Michal Simek | 8667f8c | 2016-05-04 15:08:00 +0200 | [diff] [blame] | 48 | } |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 49 | if (board_fit_config_name_match(name)) |
| 50 | continue; |
| 51 | |
| 52 | debug("Selecting config '%s'", name); |
| 53 | fdt_name = fdt_getprop(fdt, node, FIT_FDT_PROP, &len); |
| 54 | if (!fdt_name) { |
| 55 | debug("%s: Cannot find fdt name property: %d\n", |
| 56 | __func__, len); |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | debug(", fdt '%s'\n", fdt_name); |
| 61 | fdt_node = fdt_subnode_offset(fdt, images, fdt_name); |
| 62 | if (fdt_node < 0) { |
| 63 | debug("%s: Cannot find fdt node '%s': %d\n", |
| 64 | __func__, fdt_name, fdt_node); |
| 65 | return -EINVAL; |
| 66 | } |
| 67 | |
| 68 | *fdt_offsetp = fdt_getprop_u32(fdt, fdt_node, "data-offset"); |
| 69 | len = fdt_getprop_u32(fdt, fdt_node, "data-size"); |
| 70 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 71 | printf("FIT: Selected '%s'\n", name); |
| 72 | #endif |
| 73 | |
| 74 | return len; |
| 75 | } |
| 76 | |
| 77 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT |
| 78 | printf("No matching DT out of these options:\n"); |
| 79 | for (node = fdt_first_subnode(fdt, conf); |
| 80 | node >= 0; |
| 81 | node = fdt_next_subnode(fdt, node)) { |
| 82 | name = fdt_getprop(fdt, node, "name", &len); |
| 83 | printf(" %s\n", name); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | return -ENOENT; |
| 88 | } |
| 89 | |
| 90 | int spl_load_simple_fit(struct spl_load_info *info, ulong sector, void *fit) |
| 91 | { |
| 92 | int sectors; |
| 93 | ulong size, load; |
| 94 | unsigned long count; |
| 95 | int node, images; |
| 96 | void *load_ptr; |
| 97 | int fdt_offset, fdt_len; |
| 98 | int data_offset, data_size; |
| 99 | int base_offset; |
| 100 | int src_sector; |
| 101 | void *dst; |
| 102 | |
| 103 | /* |
| 104 | * Figure out where the external images start. This is the base for the |
| 105 | * data-offset properties in each image. |
| 106 | */ |
| 107 | size = fdt_totalsize(fit); |
| 108 | size = (size + 3) & ~3; |
| 109 | base_offset = (size + 3) & ~3; |
| 110 | |
| 111 | /* |
| 112 | * So far we only have one block of data from the FIT. Read the entire |
| 113 | * thing, including that first block, placing it so it finishes before |
| 114 | * where we will load the image. |
| 115 | * |
| 116 | * Note that we will load the image such that its first byte will be |
| 117 | * at the load address. Since that byte may be part-way through a |
| 118 | * block, we may load the image up to one block before the load |
| 119 | * address. So take account of that here by subtracting an addition |
| 120 | * block length from the FIT start position. |
| 121 | * |
| 122 | * In fact the FIT has its own load address, but we assume it cannot |
| 123 | * be before CONFIG_SYS_TEXT_BASE. |
| 124 | */ |
| 125 | fit = (void *)(CONFIG_SYS_TEXT_BASE - size - info->bl_len); |
Michal Simek | 2e9306a | 2016-04-28 10:36:11 +0200 | [diff] [blame^] | 126 | fit = (void *)ALIGN((ulong)fit, 8); |
Simon Glass | a6131a2 | 2016-02-22 22:55:56 -0700 | [diff] [blame] | 127 | sectors = (size + info->bl_len - 1) / info->bl_len; |
| 128 | count = info->read(info, sector, sectors, fit); |
| 129 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", |
| 130 | sector, sectors, fit, count); |
| 131 | if (count == 0) |
| 132 | return -EIO; |
| 133 | |
| 134 | /* find the firmware image to load */ |
| 135 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
| 136 | if (images < 0) { |
| 137 | debug("%s: Cannot find /images node: %d\n", __func__, images); |
| 138 | return -1; |
| 139 | } |
| 140 | node = fdt_first_subnode(fit, images); |
| 141 | if (node < 0) { |
| 142 | debug("%s: Cannot find first image node: %d\n", __func__, node); |
| 143 | return -1; |
| 144 | } |
| 145 | |
| 146 | /* Get its information and set up the spl_image structure */ |
| 147 | data_offset = fdt_getprop_u32(fit, node, "data-offset"); |
| 148 | data_size = fdt_getprop_u32(fit, node, "data-size"); |
| 149 | load = fdt_getprop_u32(fit, node, "load"); |
| 150 | debug("data_offset=%x, data_size=%x\n", data_offset, data_size); |
| 151 | spl_image.load_addr = load; |
| 152 | spl_image.entry_point = load; |
| 153 | spl_image.os = IH_OS_U_BOOT; |
| 154 | |
| 155 | /* |
| 156 | * Work out where to place the image. We read it so that the first |
| 157 | * byte will be at 'load'. This may mean we need to load it starting |
| 158 | * before then, since we can only read whole blocks. |
| 159 | */ |
| 160 | sectors = (data_size + info->bl_len - 1) / info->bl_len; |
| 161 | data_offset += base_offset; |
| 162 | load_ptr = (void *)load; |
| 163 | debug("U-Boot size %x, data %p\n", data_size, load_ptr); |
| 164 | dst = load_ptr - (data_offset % info->bl_len); |
| 165 | |
| 166 | /* Read the image */ |
| 167 | src_sector = sector + data_offset / info->bl_len; |
| 168 | debug("image: data_offset=%x, dst=%p, src_sector=%x, sectors=%x\n", |
| 169 | data_offset, dst, src_sector, sectors); |
| 170 | count = info->read(info, src_sector, sectors, dst); |
| 171 | if (count != sectors) |
| 172 | return -EIO; |
| 173 | |
| 174 | /* Figure out which device tree the board wants to use */ |
| 175 | fdt_len = spl_fit_select_fdt(fit, images, &fdt_offset); |
| 176 | if (fdt_len < 0) |
| 177 | return fdt_len; |
| 178 | |
| 179 | /* |
| 180 | * Read the device tree and place it after the image. There may be |
| 181 | * some extra data before it since we can only read entire blocks. |
| 182 | */ |
| 183 | dst = load_ptr + data_size; |
| 184 | fdt_offset += base_offset; |
| 185 | count = info->read(info, sector + fdt_offset / info->bl_len, sectors, |
| 186 | dst); |
| 187 | debug("fit read %x sectors to %x, dst %p, data_offset %x\n", |
| 188 | sectors, spl_image.load_addr, dst, fdt_offset); |
| 189 | if (count != sectors) |
| 190 | return -EIO; |
| 191 | |
| 192 | /* |
| 193 | * Copy the device tree so that it starts immediately after the image. |
| 194 | * After this we will have the U-Boot image and its device tree ready |
| 195 | * for us to start. |
| 196 | */ |
| 197 | memcpy(dst, dst + fdt_offset % info->bl_len, fdt_len); |
| 198 | |
| 199 | return 0; |
| 200 | } |