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