blob: d7ab9efd1108c08c42e65f85635c186c6041d489 [file] [log] [blame]
Tom Rini4549e782018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Ladislav Michlbf55cd42016-07-12 20:28:13 +02002/*
3 * Copyright (C) 2016
4 * Ladislav Michl <ladis@linux-mips.org>
Ladislav Michlbf55cd42016-07-12 20:28:13 +02005 */
6
7#include <common.h>
8#include <config.h>
Simon Glass4d72caa2020-05-10 11:40:01 -06009#include <image.h>
Ladislav Michlbf55cd42016-07-12 20:28:13 +020010#include <nand.h>
11#include <onenand_uboot.h>
12#include <ubispl.h>
13#include <spl.h>
14
Simon Glass2a2ee2a2016-09-24 18:20:13 -060015int spl_ubi_load_image(struct spl_image_info *spl_image,
16 struct spl_boot_device *bootdev)
Ladislav Michlbf55cd42016-07-12 20:28:13 +020017{
Simon Glassf3543e62022-09-06 20:26:52 -060018 struct legacy_img_hdr *header;
Ladislav Michlbf55cd42016-07-12 20:28:13 +020019 struct ubispl_info info;
20 struct ubispl_load volumes[2];
21 int ret = 1;
22
Simon Glassecdfd692016-09-24 18:19:57 -060023 switch (bootdev->boot_device) {
Ladislav Michlbf55cd42016-07-12 20:28:13 +020024#ifdef CONFIG_SPL_NAND_SUPPORT
25 case BOOT_DEVICE_NAND:
26 nand_init();
27 info.read = nand_spl_read_block;
28 info.peb_size = CONFIG_SYS_NAND_BLOCK_SIZE;
29 break;
30#endif
31#ifdef CONFIG_SPL_ONENAND_SUPPORT
32 case BOOT_DEVICE_ONENAND:
33 info.read = onenand_spl_read_block;
Tom Rini65cc0e22022-11-16 13:10:41 -050034 info.peb_size = CFG_SYS_ONENAND_BLOCK_SIZE;
Ladislav Michlbf55cd42016-07-12 20:28:13 +020035 break;
36#endif
37 default:
38 goto out;
39 }
40 info.ubi = (struct ubi_scan_info *)CONFIG_SPL_UBI_INFO_ADDR;
Ladislav Michlffc282e2017-06-25 10:26:11 +020041 info.fastmap = IS_ENABLED(CONFIG_MTD_UBI_FASTMAP);
Ladislav Michlbf55cd42016-07-12 20:28:13 +020042
43 info.peb_offset = CONFIG_SPL_UBI_PEB_OFFSET;
44 info.vid_offset = CONFIG_SPL_UBI_VID_OFFSET;
45 info.leb_start = CONFIG_SPL_UBI_LEB_START;
46 info.peb_count = CONFIG_SPL_UBI_MAX_PEBS - info.peb_offset;
47
Tom Rini71150072021-10-30 23:03:48 -040048#if CONFIG_IS_ENABLED(OS_BOOT)
Ladislav Michlbf55cd42016-07-12 20:28:13 +020049 if (!spl_start_uboot()) {
50 volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_KERNEL_ID;
51 volumes[0].load_addr = (void *)CONFIG_SYS_LOAD_ADDR;
52 volumes[1].vol_id = CONFIG_SPL_UBI_LOAD_ARGS_ID;
Simon Glass9cbdc3a2023-09-26 08:14:17 -060053 volumes[1].load_addr = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR;
Ladislav Michlbf55cd42016-07-12 20:28:13 +020054
55 ret = ubispl_load_volumes(&info, volumes, 2);
56 if (!ret) {
Simon Glassf3543e62022-09-06 20:26:52 -060057 header = (struct legacy_img_hdr *)volumes[0].load_addr;
Pali Rohár2e0429b2022-01-14 14:31:38 +010058 spl_parse_image_header(spl_image, bootdev, header);
Ladislav Michlbf55cd42016-07-12 20:28:13 +020059 puts("Linux loaded.\n");
60 goto out;
61 }
62 puts("Loading Linux failed, falling back to U-Boot.\n");
63 }
64#endif
Marek Vasut04ce5422018-08-14 11:27:02 +020065 header = spl_get_load_buffer(-sizeof(*header), sizeof(header));
Hamish Guthrie6ea31cc2019-05-15 15:15:59 +020066#ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
67 volumes[0].vol_id = -1;
68 strncpy(volumes[0].name,
69 CONFIG_SPL_UBI_LOAD_MONITOR_VOLNAME,
70 UBI_VOL_NAME_MAX + 1);
71#else
Ladislav Michlbf55cd42016-07-12 20:28:13 +020072 volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_MONITOR_ID;
Hamish Guthrie6ea31cc2019-05-15 15:15:59 +020073#endif
Ladislav Michlbf55cd42016-07-12 20:28:13 +020074 volumes[0].load_addr = (void *)header;
75
76 ret = ubispl_load_volumes(&info, volumes, 1);
77 if (!ret)
Pali Rohár2e0429b2022-01-14 14:31:38 +010078 spl_parse_image_header(spl_image, bootdev, header);
Ladislav Michlbf55cd42016-07-12 20:28:13 +020079out:
80#ifdef CONFIG_SPL_NAND_SUPPORT
Simon Glassecdfd692016-09-24 18:19:57 -060081 if (bootdev->boot_device == BOOT_DEVICE_NAND)
Ladislav Michlbf55cd42016-07-12 20:28:13 +020082 nand_deselect();
83#endif
84 return ret;
85}
Simon Glass7d7dd822016-09-24 18:20:01 -060086/* Use priorty 0 so that Ubi will override NAND and ONENAND methods */
Simon Glassebc4ef62016-11-30 15:30:50 -070087SPL_LOAD_IMAGE_METHOD("NAND", 0, BOOT_DEVICE_NAND, spl_ubi_load_image);
88SPL_LOAD_IMAGE_METHOD("OneNAND", 0, BOOT_DEVICE_ONENAND, spl_ubi_load_image);