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> |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 10 | #include <spl.h> |
| 11 | |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 12 | static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector, |
| 13 | ulong count, void *buf) |
| 14 | { |
| 15 | debug("%s: sector %lx, count %lx, buf %p\n", |
| 16 | __func__, sector, count, buf); |
| 17 | memcpy(buf, (void *)sector, count); |
| 18 | |
| 19 | return count; |
| 20 | } |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 21 | |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 22 | unsigned long __weak spl_nor_get_uboot_base(void) |
| 23 | { |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 24 | return CFG_SYS_UBOOT_BASE; |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 25 | } |
| 26 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 27 | static int spl_nor_load_image(struct spl_image_info *spl_image, |
| 28 | struct spl_boot_device *bootdev) |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 29 | { |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 30 | struct legacy_img_hdr *header; |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 31 | __maybe_unused struct spl_load_info load; |
| 32 | |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 33 | /* |
| 34 | * Loading of the payload to SDRAM is done with skipping of |
| 35 | * the mkimage header in this SPL NOR driver |
| 36 | */ |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 37 | spl_image->flags |= SPL_COPY_PAYLOAD_ONLY; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 38 | |
Tom Rini | c2e7888 | 2021-10-30 23:03:48 -0400 | [diff] [blame] | 39 | #if CONFIG_IS_ENABLED(OS_BOOT) |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 40 | if (!spl_start_uboot()) { |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 41 | /* |
| 42 | * Load Linux from its location in NOR flash to its defined |
| 43 | * location in SDRAM |
| 44 | */ |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 45 | header = (void *)CONFIG_SYS_OS_BASE; |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 46 | #ifdef CONFIG_SPL_LOAD_FIT |
| 47 | if (image_get_magic(header) == FDT_MAGIC) { |
Stefan Roese | 4ee4cf9 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 48 | int ret; |
| 49 | |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 50 | debug("Found FIT\n"); |
| 51 | load.bl_len = 1; |
| 52 | load.read = spl_nor_load_read; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 53 | |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 54 | ret = spl_load_simple_fit(spl_image, &load, |
| 55 | CONFIG_SYS_OS_BASE, |
| 56 | (void *)header); |
| 57 | |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 58 | #if defined CONFIG_SPL_PAYLOAD_ARGS_ADDR && defined CONFIG_CMD_SPL_NOR_OFS |
| 59 | memcpy((void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR, |
Lukasz Majewski | 55ce92e | 2019-10-15 10:28:45 +0200 | [diff] [blame] | 60 | (void *)CONFIG_CMD_SPL_NOR_OFS, |
| 61 | CONFIG_CMD_SPL_WRITE_SIZE); |
| 62 | #endif |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 63 | return ret; |
| 64 | } |
| 65 | #endif |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 66 | if (image_get_os(header) == IH_OS_LINUX) { |
| 67 | /* happy - was a Linux */ |
Stefan Roese | 4ee4cf9 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 68 | int ret; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 69 | |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 70 | ret = spl_parse_image_header(spl_image, bootdev, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 71 | if (ret) |
| 72 | return ret; |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 73 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 74 | memcpy((void *)spl_image->load_addr, |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 75 | (void *)(CONFIG_SYS_OS_BASE + |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 76 | sizeof(struct legacy_img_hdr)), |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 77 | spl_image->size); |
Simon Glass | 259cdb4 | 2023-09-26 08:14:17 -0600 | [diff] [blame] | 78 | #ifdef CONFIG_SPL_PAYLOAD_ARGS_ADDR |
| 79 | spl_image->arg = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR; |
York Sun | f0370c0 | 2018-06-26 10:10:04 -0700 | [diff] [blame] | 80 | #endif |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 81 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 82 | return 0; |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 83 | } else { |
| 84 | puts("The Expected Linux image was not found.\n" |
| 85 | "Please check your NOR configuration.\n" |
| 86 | "Trying to start u-boot now...\n"); |
| 87 | } |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 88 | } |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 89 | #endif |
| 90 | |
| 91 | /* |
| 92 | * Load real U-Boot from its location in NOR flash to its |
| 93 | * defined location in SDRAM |
| 94 | */ |
Simon Glass | bb7d3bb | 2022-09-06 20:26:52 -0600 | [diff] [blame] | 95 | header = (const struct legacy_img_hdr *)spl_nor_get_uboot_base(); |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 96 | #ifdef CONFIG_SPL_LOAD_FIT |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 97 | if (image_get_magic(header) == FDT_MAGIC) { |
| 98 | debug("Found FIT format U-Boot\n"); |
| 99 | load.bl_len = 1; |
| 100 | load.read = spl_nor_load_read; |
Stefan Roese | 4ee4cf9 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 101 | return spl_load_simple_fit(spl_image, &load, |
| 102 | spl_nor_get_uboot_base(), |
| 103 | (void *)header); |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 104 | } |
| 105 | #endif |
Sean Anderson | 952ed67 | 2023-10-14 16:47:44 -0400 | [diff] [blame] | 106 | if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER) && |
| 107 | valid_container_hdr((void *)header)) { |
Peng Fan | 3d52a6f | 2019-09-23 10:18:48 +0800 | [diff] [blame] | 108 | load.bl_len = 1; |
| 109 | load.read = spl_nor_load_read; |
| 110 | return spl_load_imx_container(spl_image, &load, |
| 111 | spl_nor_get_uboot_base()); |
| 112 | } |
| 113 | |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 114 | /* Legacy image handling */ |
Andrew Davis | 360ec6a | 2022-05-04 15:52:25 -0500 | [diff] [blame] | 115 | if (IS_ENABLED(CONFIG_SPL_LEGACY_IMAGE_FORMAT)) { |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 116 | load.bl_len = 1; |
| 117 | load.read = spl_nor_load_read; |
Pali Rohár | dda8f88 | 2022-01-14 14:31:38 +0100 | [diff] [blame] | 118 | return spl_load_legacy_img(spl_image, bootdev, &load, |
Roger Quadros | 237f018 | 2022-09-29 13:11:28 +0300 | [diff] [blame] | 119 | spl_nor_get_uboot_base(), |
Sean Anderson | fab09a5 | 2023-10-14 16:47:38 -0400 | [diff] [blame] | 120 | header); |
Stefan Roese | 84ae9d8 | 2020-04-21 09:28:43 +0200 | [diff] [blame] | 121 | } |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 122 | |
Mario Kicherer | 4d1ad12 | 2023-01-30 10:21:43 +0100 | [diff] [blame] | 123 | return -EINVAL; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 124 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 125 | SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image); |