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