Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 Stefan Roese <sr@denx.de> |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 2dc9c34 | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 7 | #include <image.h> |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 8 | #include <imx_container.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 10 | #include <mapmem.h> |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 11 | #include <spl.h> |
| 12 | |
York Sun | 7975725 | 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 | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 18 | memcpy(buf, map_sysmem(sector, count), count); |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 19 | |
| 20 | return count; |
| 21 | } |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 22 | |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 23 | unsigned long __weak spl_nor_get_uboot_base(void) |
| 24 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 25 | return CFG_SYS_UBOOT_BASE; |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 26 | } |
| 27 | |
Simon Glass | ee30679 | 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 | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 30 | { |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 31 | struct legacy_img_hdr *header; |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 32 | __maybe_unused struct spl_load_info load; |
| 33 | |
Stefan Roese | 900c177 | 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 | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 38 | spl_image->flags |= SPL_COPY_PAYLOAD_ONLY; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 39 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 40 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 41 | if (!spl_start_uboot()) { |
Stefan Roese | 900c177 | 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 | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 46 | header = (void *)CONFIG_SYS_OS_BASE; |
York Sun | 7975725 | 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 | 4ee4cf9 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 49 | int ret; |
| 50 | |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 51 | debug("Found FIT\n"); |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame^] | 52 | spl_set_bl_len(&load, 1); |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 53 | load.read = spl_nor_load_read; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 54 | |
York Sun | 7975725 | 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 | 259cdb4 | 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 | 55ce92e | 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 | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 64 | return ret; |
| 65 | } |
| 66 | #endif |
Masahiro Yamada | 225581b | 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 | 4ee4cf9 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 69 | int ret; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 70 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 71 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 72 | if (ret) |
| 73 | return ret; |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 74 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 75 | memcpy((void *)spl_image->load_addr, |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 76 | (void *)(CONFIG_SYS_OS_BASE + |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 77 | sizeof(struct legacy_img_hdr)), |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 78 | spl_image->size); |
Simon Glass | 259cdb4 | 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 | f0370c0 | 2018-06-26 10:10:04 -0700 | [diff] [blame] | 81 | #endif |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 82 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 83 | return 0; |
Masahiro Yamada | 225581b | 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 | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 89 | } |
Masahiro Yamada | 225581b | 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 | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 96 | header = map_sysmem(spl_nor_get_uboot_base(), sizeof(*header)); |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 97 | #ifdef CONFIG_SPL_LOAD_FIT |
York Sun | 7975725 | 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"); |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame^] | 100 | spl_set_bl_len(&load, 1); |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 101 | load.read = spl_nor_load_read; |
Stefan Roese | 4ee4cf9 | 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 | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 105 | } |
| 106 | #endif |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 107 | if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) && |
| 108 | valid_container_hdr((void *)header)) { |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame^] | 109 | spl_set_bl_len(&load, 1); |
Peng Fan | 3d52a6f | 2019-09-23 10:18:48 +0800 | [diff] [blame] | 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 | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 115 | /* Legacy image handling */ |
Andrew Davis | 360ec6a | 2022-05-04 15:52:25 -0500 | [diff] [blame] | 116 | if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) { |
Sean Anderson | 35f15fe | 2023-11-08 11:48:43 -0500 | [diff] [blame^] | 117 | spl_set_bl_len(&load, 1); |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 118 | load.read = spl_nor_load_read; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 119 | return spl_load_legacy_img(spl_image, bootdev, &load, |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 120 | spl_nor_get_uboot_base(), |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 121 | header); |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 122 | } |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 123 | |
Mario Kicherer | 4d1ad12 | 2023-01-30 10:21:43 +0100 | [diff] [blame] | 124 | return -EINVAL; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 125 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 126 | SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image); |