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> |
| 7 | #include <spl.h> |
| 8 | |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 9 | static ulong spl_nor_load_read(struct spl_load_info *load, ulong sector, |
| 10 | ulong count, void *buf) |
| 11 | { |
| 12 | debug("%s: sector %lx, count %lx, buf %p\n", |
| 13 | __func__, sector, count, buf); |
| 14 | memcpy(buf, (void *)sector, count); |
| 15 | |
| 16 | return count; |
| 17 | } |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 18 | |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 19 | unsigned long __weak spl_nor_get_uboot_base(void) |
| 20 | { |
| 21 | return CONFIG_SYS_UBOOT_BASE; |
| 22 | } |
| 23 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 24 | static int spl_nor_load_image(struct spl_image_info *spl_image, |
| 25 | struct spl_boot_device *bootdev) |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 26 | { |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 27 | int ret; |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 28 | __maybe_unused const struct image_header *header; |
| 29 | __maybe_unused struct spl_load_info load; |
| 30 | |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 31 | /* |
| 32 | * Loading of the payload to SDRAM is done with skipping of |
| 33 | * the mkimage header in this SPL NOR driver |
| 34 | */ |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 35 | spl_image->flags |= SPL_COPY_PAYLOAD_ONLY; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 36 | |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 37 | #ifdef CONFIG_SPL_OS_BOOT |
| 38 | if (!spl_start_uboot()) { |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 39 | /* |
| 40 | * Load Linux from its location in NOR flash to its defined |
| 41 | * location in SDRAM |
| 42 | */ |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 43 | header = (const struct image_header *)CONFIG_SYS_OS_BASE; |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_SPL_LOAD_FIT |
| 45 | if (image_get_magic(header) == FDT_MAGIC) { |
| 46 | debug("Found FIT\n"); |
| 47 | load.bl_len = 1; |
| 48 | load.read = spl_nor_load_read; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 49 | |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 50 | ret = spl_load_simple_fit(spl_image, &load, |
| 51 | CONFIG_SYS_OS_BASE, |
| 52 | (void *)header); |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | #endif |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 57 | if (image_get_os(header) == IH_OS_LINUX) { |
| 58 | /* happy - was a Linux */ |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 59 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 60 | ret = spl_parse_image_header(spl_image, header); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 61 | if (ret) |
| 62 | return ret; |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 63 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 64 | memcpy((void *)spl_image->load_addr, |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 65 | (void *)(CONFIG_SYS_OS_BASE + |
| 66 | sizeof(struct image_header)), |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 67 | spl_image->size); |
York Sun | f0370c0 | 2018-06-26 10:10:04 -0700 | [diff] [blame] | 68 | #ifdef CONFIG_SYS_FDT_BASE |
Vikas Manocha | 62b021c | 2017-04-07 15:38:13 -0700 | [diff] [blame] | 69 | spl_image->arg = (void *)CONFIG_SYS_FDT_BASE; |
York Sun | f0370c0 | 2018-06-26 10:10:04 -0700 | [diff] [blame] | 70 | #endif |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 71 | |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 72 | return 0; |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 73 | } else { |
| 74 | puts("The Expected Linux image was not found.\n" |
| 75 | "Please check your NOR configuration.\n" |
| 76 | "Trying to start u-boot now...\n"); |
| 77 | } |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 78 | } |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 79 | #endif |
| 80 | |
| 81 | /* |
| 82 | * Load real U-Boot from its location in NOR flash to its |
| 83 | * defined location in SDRAM |
| 84 | */ |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_SPL_LOAD_FIT |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 86 | header = (const struct image_header *)spl_nor_get_uboot_base(); |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 87 | if (image_get_magic(header) == FDT_MAGIC) { |
| 88 | debug("Found FIT format U-Boot\n"); |
| 89 | load.bl_len = 1; |
| 90 | load.read = spl_nor_load_read; |
| 91 | ret = spl_load_simple_fit(spl_image, &load, |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 92 | spl_nor_get_uboot_base(), |
York Sun | 7975725 | 2018-06-26 10:10:03 -0700 | [diff] [blame] | 93 | (void *)header); |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | #endif |
Peng Fan | 3d52a6f | 2019-09-23 10:18:48 +0800 | [diff] [blame] | 98 | if (IS_ENABLED(CONFIG_SPL_LOAD_IMX_CONTAINER)) { |
| 99 | load.bl_len = 1; |
| 100 | load.read = spl_nor_load_read; |
| 101 | return spl_load_imx_container(spl_image, &load, |
| 102 | spl_nor_get_uboot_base()); |
| 103 | } |
| 104 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 105 | ret = spl_parse_image_header(spl_image, |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 106 | (const struct image_header *)spl_nor_get_uboot_base()); |
Marek Vasut | 02266e2 | 2016-04-29 00:44:54 +0200 | [diff] [blame] | 107 | if (ret) |
| 108 | return ret; |
Masahiro Yamada | 225581b | 2015-01-08 19:23:35 +0900 | [diff] [blame] | 109 | |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 110 | memcpy((void *)(unsigned long)spl_image->load_addr, |
Peng Fan | 6e773f9 | 2019-09-23 10:18:42 +0800 | [diff] [blame] | 111 | (void *)(spl_nor_get_uboot_base() + sizeof(struct image_header)), |
Simon Glass | ee30679 | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 112 | spl_image->size); |
Nikita Kiryanov | 33eefe4 | 2015-11-08 17:11:49 +0200 | [diff] [blame] | 113 | |
| 114 | return 0; |
Stefan Roese | 900c177 | 2012-08-27 12:50:59 +0200 | [diff] [blame] | 115 | } |
Simon Glass | 4fc1f25 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 116 | SPL_LOAD_IMAGE_METHOD("NOR", 0, BOOT_DEVICE_NOR, spl_nor_load_image); |