Tom Rini | 4549e78 | 2018-05-06 18:27:01 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 |
| 4 | * Ladislav Michl <ladis@linux-mips.org> |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <config.h> |
| 9 | #include <nand.h> |
| 10 | #include <onenand_uboot.h> |
| 11 | #include <ubispl.h> |
| 12 | #include <spl.h> |
| 13 | |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 14 | int spl_ubi_load_image(struct spl_image_info *spl_image, |
| 15 | struct spl_boot_device *bootdev) |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 16 | { |
| 17 | struct image_header *header; |
| 18 | struct ubispl_info info; |
| 19 | struct ubispl_load volumes[2]; |
| 20 | int ret = 1; |
| 21 | |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 22 | switch (bootdev->boot_device) { |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 23 | #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; |
| 33 | info.peb_size = CONFIG_SYS_ONENAND_BLOCK_SIZE; |
| 34 | break; |
| 35 | #endif |
| 36 | default: |
| 37 | goto out; |
| 38 | } |
| 39 | info.ubi = (struct ubi_scan_info *)CONFIG_SPL_UBI_INFO_ADDR; |
Ladislav Michl | ffc282e | 2017-06-25 10:26:11 +0200 | [diff] [blame] | 40 | info.fastmap = IS_ENABLED(CONFIG_MTD_UBI_FASTMAP); |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 41 | |
| 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 | |
| 47 | #ifdef CONFIG_SPL_OS_BOOT |
| 48 | 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; |
| 52 | volumes[1].load_addr = (void *)CONFIG_SYS_SPL_ARGS_ADDR; |
| 53 | |
| 54 | ret = ubispl_load_volumes(&info, volumes, 2); |
| 55 | if (!ret) { |
| 56 | header = (struct image_header *)volumes[0].load_addr; |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 57 | spl_parse_image_header(spl_image, header); |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 58 | puts("Linux loaded.\n"); |
| 59 | goto out; |
| 60 | } |
| 61 | puts("Loading Linux failed, falling back to U-Boot.\n"); |
| 62 | } |
| 63 | #endif |
Marek Vasut | 04ce542 | 2018-08-14 11:27:02 +0200 | [diff] [blame] | 64 | header = spl_get_load_buffer(-sizeof(*header), sizeof(header)); |
Hamish Guthrie | 6ea31cc | 2019-05-15 15:15:59 +0200 | [diff] [blame] | 65 | #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 Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 71 | volumes[0].vol_id = CONFIG_SPL_UBI_LOAD_MONITOR_ID; |
Hamish Guthrie | 6ea31cc | 2019-05-15 15:15:59 +0200 | [diff] [blame] | 72 | #endif |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 73 | volumes[0].load_addr = (void *)header; |
| 74 | |
| 75 | ret = ubispl_load_volumes(&info, volumes, 1); |
| 76 | if (!ret) |
Simon Glass | 2a2ee2a | 2016-09-24 18:20:13 -0600 | [diff] [blame] | 77 | spl_parse_image_header(spl_image, header); |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 78 | out: |
| 79 | #ifdef CONFIG_SPL_NAND_SUPPORT |
Simon Glass | ecdfd69 | 2016-09-24 18:19:57 -0600 | [diff] [blame] | 80 | if (bootdev->boot_device == BOOT_DEVICE_NAND) |
Ladislav Michl | bf55cd4 | 2016-07-12 20:28:13 +0200 | [diff] [blame] | 81 | nand_deselect(); |
| 82 | #endif |
| 83 | return ret; |
| 84 | } |
Simon Glass | 7d7dd82 | 2016-09-24 18:20:01 -0600 | [diff] [blame] | 85 | /* Use priorty 0 so that Ubi will override NAND and ONENAND methods */ |
Simon Glass | ebc4ef6 | 2016-11-30 15:30:50 -0700 | [diff] [blame] | 86 | SPL_LOAD_IMAGE_METHOD("NAND", 0, BOOT_DEVICE_NAND, spl_ubi_load_image); |
| 87 | SPL_LOAD_IMAGE_METHOD("OneNAND", 0, BOOT_DEVICE_ONENAND, spl_ubi_load_image); |