blob: a8d3f43b452849d2e75fad6ae69317b17624a0c8 [file] [log] [blame]
Tom Rini8b0c8a12018-05-06 18:27:01 -04001// SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
Ladislav Michl2a8ea482016-07-12 20:28:13 +02002/*
3 * Copyright (C) 2016
4 * Ladislav Michl <ladis@linux-mips.org>
Ladislav Michl2a8ea482016-07-12 20:28:13 +02005 */
6
Ladislav Michl2a8ea482016-07-12 20:28:13 +02007#include <config.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06008#include <image.h>
Ladislav Michl2a8ea482016-07-12 20:28:13 +02009#include <nand.h>
10#include <onenand_uboot.h>
11#include <ubispl.h>
12#include <spl.h>
13
Simon Glassee306792016-09-24 18:20:13 -060014int spl_ubi_load_image(struct spl_image_info *spl_image,
15 struct spl_boot_device *bootdev)
Ladislav Michl2a8ea482016-07-12 20:28:13 +020016{
Simon Glassbb7d3bb2022-09-06 20:26:52 -060017 struct legacy_img_hdr *header;
Ladislav Michl2a8ea482016-07-12 20:28:13 +020018 struct ubispl_info info;
19 struct ubispl_load volumes[2];
20 int ret = 1;
21
Simon Glassc0b6c9b2016-09-24 18:19:57 -060022 switch (bootdev->boot_device) {
Ladislav Michl2a8ea482016-07-12 20:28:13 +020023#ifdef CONFIG_SPL_NAND_SUPPORT
24 case BOOT_DEVICE_NAND:
25 nand_init();
26 info.read = nand_spl_read_block;
27 info.peb_size = CONFIG_SYS_NAND_BLOCK_SIZE;
28 break;
29#endif
30#ifdef CONFIG_SPL_ONENAND_SUPPORT
31 case BOOT_DEVICE_ONENAND:
32 info.read = onenand_spl_read_block;
Tom Rini6a5dccc2022-11-16 13:10:41 -050033 info.peb_size = CFG_SYS_ONENAND_BLOCK_SIZE;
Ladislav Michl2a8ea482016-07-12 20:28:13 +020034 break;
35#endif
36 default:
37 goto out;
38 }
39 info.ubi = (struct ubi_scan_info *)CONFIG_SPL_UBI_INFO_ADDR;
Ladislav Michl5727d532017-06-25 10:26:11 +020040 info.fastmap = IS_ENABLED(CONFIG_MTD_UBI_FASTMAP);
Ladislav Michl2a8ea482016-07-12 20:28:13 +020041
42 info.peb_offset = CONFIG_SPL_UBI_PEB_OFFSET;
43 info.vid_offset = CONFIG_SPL_UBI_VID_OFFSET;
44 info.leb_start = CONFIG_SPL_UBI_LEB_START;
45 info.peb_count = CONFIG_SPL_UBI_MAX_PEBS - info.peb_offset;
46
Tom Rinic2e78882021-10-30 23:03:48 -040047#if CONFIG_IS_ENABLED(OS_BOOT)
Ladislav Michl2a8ea482016-07-12 20:28:13 +020048 if (!spl_start_uboot()) {
49 volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_KERNEL_ID;
50 volumes[0].load_addr = (void *)CONFIG_SYS_LOAD_ADDR;
51 volumes[1].vol_id = CONFIG_SPL_UBI_LOAD_ARGS_ID;
Simon Glass259cdb42023-09-26 08:14:17 -060052 volumes[1].load_addr = (void *)CONFIG_SPL_PAYLOAD_ARGS_ADDR;
Ladislav Michl2a8ea482016-07-12 20:28:13 +020053
54 ret = ubispl_load_volumes(&info, volumes, 2);
55 if (!ret) {
Simon Glassbb7d3bb2022-09-06 20:26:52 -060056 header = (struct legacy_img_hdr *)volumes[0].load_addr;
Pali Rohárdda8f882022-01-14 14:31:38 +010057 spl_parse_image_header(spl_image, bootdev, header);
Ladislav Michl2a8ea482016-07-12 20:28:13 +020058 puts("Linux loaded.\n");
59 goto out;
60 }
61 puts("Loading Linux failed, falling back to U-Boot.\n");
62 }
63#endif
Marek Vasut1dda46f2018-08-14 11:27:02 +020064 header = spl_get_load_buffer(-sizeof(*header), sizeof(header));
Hamish Guthrie3ce740d2019-05-15 15:15:59 +020065#ifdef CONFIG_SPL_UBI_LOAD_BY_VOLNAME
66 volumes[0].vol_id = -1;
67 strncpy(volumes[0].name,
68 CONFIG_SPL_UBI_LOAD_MONITOR_VOLNAME,
69 UBI_VOL_NAME_MAX + 1);
70#else
Ladislav Michl2a8ea482016-07-12 20:28:13 +020071 volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_MONITOR_ID;
Hamish Guthrie3ce740d2019-05-15 15:15:59 +020072#endif
Ladislav Michl2a8ea482016-07-12 20:28:13 +020073 volumes[0].load_addr = (void *)header;
74
75 ret = ubispl_load_volumes(&info, volumes, 1);
76 if (!ret)
Pali Rohárdda8f882022-01-14 14:31:38 +010077 spl_parse_image_header(spl_image, bootdev, header);
Ladislav Michl2a8ea482016-07-12 20:28:13 +020078out:
79#ifdef CONFIG_SPL_NAND_SUPPORT
Simon Glassc0b6c9b2016-09-24 18:19:57 -060080 if (bootdev->boot_device == BOOT_DEVICE_NAND)
Ladislav Michl2a8ea482016-07-12 20:28:13 +020081 nand_deselect();
82#endif
83 return ret;
84}
Simon Glass92619e82016-09-24 18:20:01 -060085/* Use priorty 0 so that Ubi will override NAND and ONENAND methods */
Simon Glass4fc1f252016-11-30 15:30:50 -070086SPL_LOAD_IMAGE_METHOD("NAND", 0, BOOT_DEVICE_NAND, spl_ubi_load_image);
87SPL_LOAD_IMAGE_METHOD("OneNAND", 0, BOOT_DEVICE_ONENAND, spl_ubi_load_image);