Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 |
| 4 | * Xilinx, Inc. |
| 5 | * |
| 6 | * (C) Copyright 2016 |
| 7 | * Toradex AG |
| 8 | * |
Michal Simek | a8c9436 | 2023-07-10 14:35:49 +0200 | [diff] [blame] | 9 | * Michal Simek <michal.simek@amd.com> |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 10 | * Stefan Agner <stefan.agner@toradex.com> |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 11 | */ |
Simon Glass | 6767ec7 | 2017-11-13 18:55:04 -0700 | [diff] [blame] | 12 | #include <binman_sym.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 13 | #include <image.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 14 | #include <log.h> |
Simon Glass | 6767ec7 | 2017-11-13 18:55:04 -0700 | [diff] [blame] | 15 | #include <mapmem.h> |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 16 | #include <spl.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 18 | |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 19 | static ulong spl_ram_load_read(struct spl_load_info *load, ulong sector, |
| 20 | ulong count, void *buf) |
| 21 | { |
Simon Glass | 95dbb41 | 2023-09-14 10:55:50 -0600 | [diff] [blame] | 22 | ulong addr = 0; |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 23 | |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 24 | debug("%s: sector %lx, count %lx, buf %lx\n", |
| 25 | __func__, sector, count, (ulong)buf); |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 26 | |
Simon Glass | 95dbb41 | 2023-09-14 10:55:50 -0600 | [diff] [blame] | 27 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) { |
| 28 | addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT, |
| 29 | CONFIG_SPL_LOAD_FIT_ADDRESS); |
| 30 | } |
| 31 | addr += sector; |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 32 | if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) |
| 33 | addr += image_load_offset; |
| 34 | |
| 35 | memcpy(buf, (void *)addr, count); |
| 36 | |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 37 | return count; |
| 38 | } |
| 39 | |
| 40 | static int spl_ram_load_image(struct spl_image_info *spl_image, |
| 41 | struct spl_boot_device *bootdev) |
| 42 | { |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 43 | struct legacy_img_hdr *header; |
Simon Glass | 95dbb41 | 2023-09-14 10:55:50 -0600 | [diff] [blame] | 44 | ulong addr = 0; |
Nikita Shubin | b5f2af8 | 2022-12-12 11:03:35 +0300 | [diff] [blame] | 45 | int ret; |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 46 | |
Simon Glass | 95dbb41 | 2023-09-14 10:55:50 -0600 | [diff] [blame] | 47 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT)) { |
| 48 | addr = IF_ENABLED_INT(CONFIG_SPL_LOAD_FIT, |
| 49 | CONFIG_SPL_LOAD_FIT_ADDRESS); |
| 50 | } |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 51 | |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 52 | if (CONFIG_IS_ENABLED(IMAGE_PRE_LOAD)) { |
Nikita Shubin | b5f2af8 | 2022-12-12 11:03:35 +0300 | [diff] [blame] | 53 | ret = image_pre_load(addr); |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 54 | |
| 55 | if (ret) |
| 56 | return ret; |
| 57 | |
| 58 | addr += image_load_offset; |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 59 | } |
Simon Glass | 95dbb41 | 2023-09-14 10:55:50 -0600 | [diff] [blame] | 60 | header = map_sysmem(addr, 0); |
Philippe Reynes | e9d12f4 | 2022-03-28 22:57:01 +0200 | [diff] [blame] | 61 | |
Andrew F. Davis | 6d932e6 | 2019-01-17 13:43:02 -0600 | [diff] [blame] | 62 | #if CONFIG_IS_ENABLED(DFU) |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 63 | if (bootdev->boot_device == BOOT_DEVICE_DFU) |
| 64 | spl_dfu_cmd(0, "dfu_alt_info_ram", "ram", "0"); |
| 65 | #endif |
| 66 | |
| 67 | if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) && |
| 68 | image_get_magic(header) == FDT_MAGIC) { |
| 69 | struct spl_load_info load; |
| 70 | |
| 71 | debug("Found FIT\n"); |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame] | 72 | spl_set_bl_len(&load, 1); |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 73 | load.read = spl_ram_load_read; |
Nikita Shubin | b5f2af8 | 2022-12-12 11:03:35 +0300 | [diff] [blame] | 74 | ret = spl_load_simple_fit(spl_image, &load, 0, header); |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 75 | } else { |
Alper Nebi Yasak | 173321c | 2022-06-18 15:13:06 +0300 | [diff] [blame] | 76 | ulong u_boot_pos = spl_get_image_pos(); |
Simon Glass | 6767ec7 | 2017-11-13 18:55:04 -0700 | [diff] [blame] | 77 | |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 78 | debug("Legacy image\n"); |
| 79 | /* |
| 80 | * Get the header. It will point to an address defined by |
| 81 | * handoff which will tell where the image located inside |
Simon Glass | 6767ec7 | 2017-11-13 18:55:04 -0700 | [diff] [blame] | 82 | * the flash. |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 83 | */ |
Simon Glass | 6767ec7 | 2017-11-13 18:55:04 -0700 | [diff] [blame] | 84 | debug("u_boot_pos = %lx\n", u_boot_pos); |
| 85 | if (u_boot_pos == BINMAN_SYM_MISSING) { |
| 86 | /* |
| 87 | * No binman support or no information. For now, fix it |
| 88 | * to the address pointed to by U-Boot. |
| 89 | */ |
Michal Simek | 15e1216 | 2018-10-04 09:29:20 +0200 | [diff] [blame] | 90 | u_boot_pos = (ulong)spl_get_load_buffer(-sizeof(*header), |
| 91 | sizeof(*header)); |
Simon Glass | 6767ec7 | 2017-11-13 18:55:04 -0700 | [diff] [blame] | 92 | } |
Simon Glass | 95dbb41 | 2023-09-14 10:55:50 -0600 | [diff] [blame] | 93 | header = map_sysmem(u_boot_pos, 0); |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 94 | |
Nikita Shubin | b5f2af8 | 2022-12-12 11:03:35 +0300 | [diff] [blame] | 95 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Nikita Shubin | b5f2af8 | 2022-12-12 11:03:35 +0300 | [diff] [blame] | 98 | return ret; |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 99 | } |
Marek Vasut | 2d60819 | 2018-04-07 17:03:28 +0200 | [diff] [blame] | 100 | #if CONFIG_IS_ENABLED(RAM_DEVICE) |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 101 | SPL_LOAD_IMAGE_METHOD("RAM", 0, BOOT_DEVICE_RAM, spl_ram_load_image); |
| 102 | #endif |
Andrew F. Davis | 6d932e6 | 2019-01-17 13:43:02 -0600 | [diff] [blame] | 103 | #if CONFIG_IS_ENABLED(DFU) |
Stefan Agner | cd01e64 | 2016-12-23 07:51:53 +0100 | [diff] [blame] | 104 | SPL_LOAD_IMAGE_METHOD("DFU", 0, BOOT_DEVICE_DFU, spl_ram_load_image); |
| 105 | #endif |