Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Stefan Roese <sr@denx.de> |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 7 | #include <image.h> |
Sean Anderson | ab12179 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 8 | #include <imx_container.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 10 | #include <mapmem.h> |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 11 | #include <spl.h> |
| 12 | |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 13 | static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector, |
| 14 | ulong count, void *buf) |
| 15 | { |
| 16 | debug("%s: sector %lx, count %lx, buf %p\n", |
| 17 | __func__, sector, count, buf); |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 18 | memcpy(buf, map_sysmem(sector, count), count); |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 19 | |
| 20 | return count; |
| 21 | } |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 22 | |
Peng Fan | 07d900a | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 23 | unsigned long __weak spl_nor_get_uboot_base(void) |
| 24 | { |
Tom Rini | 65cc0e2 | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 25 | return CFG_SYS_UBOOT_BASE; |
Peng Fan | 07d900a | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 26 | } |
| 27 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 28 | static int spl_nor_load_image(struct spl_image_info *spl_image, |
| 29 | struct spl_boot_device *bootdev) |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 30 | { |
Sean Anderson | 6cc2182 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 31 | struct legacy_img_hdr *header; |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 32 | __maybe_unused struct spl_load_info load; |
| 33 | |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 34 | /* |
| 35 | * Loading of the payload to SDRAM is done with skipping of |
| 36 | * the mkimage header in this SPL NOR driver |
| 37 | */ |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 38 | spl_image->flags |= SPL_COPY_PAYLOAD_ONLY; |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 39 | |
Tom Rini | 7115007 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 40 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 41 | if (!spl_start_uboot()) { |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 42 | /* |
| 43 | * Load Linux from its location in NOR flash to its defined |
| 44 | * location in SDRAM |
| 45 | */ |
Sean Anderson | 6cc2182 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 46 | header = (void *)CONFIG_SYS_OS_BASE; |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 47 | #ifdef CONFIG_SPL_LOAD_FIT |
| 48 | if (image_get_magic(header) == FDT_MAGIC) { |
Stefan Roese | f1b0f15 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 49 | int ret; |
| 50 | |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 51 | debug("Found FIT\n"); |
| 52 | load.bl_len = 1; |
| 53 | load.read = spl_nor_load_read; |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 54 | |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 55 | ret = spl_load_simple_fit(spl_image, &load, |
| 56 | CONFIG_SYS_OS_BASE, |
| 57 | (void *)header); |
| 58 | |
Simon Glass | 9cbdc3a | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 59 | #if defined CONFIG_SPL_PAYLOAD_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS |
| 60 | memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, |
Lukasz Majewski | a4a16c9 | 2019-10-15 10:28:45 +0200 | [diff] [blame] | 61 | (void *)CONFIG_CMD_SPL_NOR_OFS, |
| 62 | CONFIG_CMD_SPL_WRITE_SIZE); |
| 63 | #endif |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | #endif |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 67 | if (image_get_os(header) == IH_OS_LINUX) { |
| 68 | /* happy - was a Linux */ |
Stefan Roese | f1b0f15 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 69 | int ret; |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 70 | |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 71 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 7e0f226 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 74 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 75 | memcpy((void *)spl_image->load_addr, |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 76 | (void *)(CONFIG_SYS_OS_BASE + |
Simon Glass | f3543e6 | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 77 | sizeof(struct legacy_img_hdr)), |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 78 | spl_image->size); |
Simon Glass | 9cbdc3a | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 79 | #ifdef CONFIG_SPL_PAYLOAD_ARGS_ADDR |
| 80 | spl_image->arg = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR; |
York Sun | 14acea0 | 2018-06-26 10:10:04 -0700 | [diff] [blame] | 81 | #endif |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 82 | |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 83 | return 0; |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 84 | } else { |
| 85 | puts("The Expected Linux image was not found.\n" |
| 86 | "Please check your NOR configuration.\n" |
| 87 | "Trying to start u-boot now...\n"); |
| 88 | } |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 89 | } |
Masahiro Yamada | 9f9d870 | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | /* |
| 93 | * Load real U-Boot from its location in NOR flash to its |
| 94 | * defined location in SDRAM |
| 95 | */ |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 96 | header = map_sysmem(spl_nor_get_uboot_base(), sizeof(*header)); |
Sean Anderson | 6cc2182 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 97 | #ifdef CONFIG_SPL_LOAD_FIT |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 98 | if (image_get_magic(header) == FDT_MAGIC) { |
| 99 | debug("Found FIT format U-Boot\n"); |
| 100 | load.bl_len = 1; |
| 101 | load.read = spl_nor_load_read; |
Stefan Roese | f1b0f15 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 102 | return spl_load_simple_fit(spl_image, &load, |
| 103 | spl_nor_get_uboot_base(), |
| 104 | (void *)header); |
York Sun | 6ecd820 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 105 | } |
| 106 | #endif |
Sean Anderson | ab12179 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 107 | if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) && |
| 108 | valid_container_hdr((void *)header)) { |
Peng Fan | 3bc4f1d | 2019-09-23 10:18:48 +0800 | [diff] [blame] | 109 | load.bl_len = 1; |
| 110 | load.read = spl_nor_load_read; |
| 111 | return spl_load_imx_container(spl_image, &load, |
| 112 | spl_nor_get_uboot_base()); |
| 113 | } |
| 114 | |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 115 | /* Legacy image handling */ |
Andrew Davis | 6665ab1 | 2022-05-04 15:52:25 -0500 | [diff] [blame] | 116 | if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) { |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 117 | load.bl_len = 1; |
| 118 | load.read = spl_nor_load_read; |
Pali Rohár | 2e0429b | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 119 | return spl_load_legacy_img(spl_image, bootdev, &load, |
Roger Quadros | 06377c5 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 120 | spl_nor_get_uboot_base(), |
Sean Anderson | 6cc2182 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 121 | header); |
Stefan Roese | 2fc91ed | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 122 | } |
Nikita Kiryanov | 36afd45 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 123 | |
Mario Kicherer | 1aeedef | 2023-01-30 10:21:43 +0100 | [diff] [blame] | 124 | return -EINVAL; |
Stefan Roese | 33d3464 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 125 | } |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 126 | SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image); |