Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2023 |
| 4 | * Ventana Micro Systems Inc. |
| 5 | * |
| 6 | */ |
| 7 | |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 8 | #include <spl.h> |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 9 | #include <spl_load.h> |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 10 | #include <image.h> |
| 11 | #include <fs.h> |
Sean Anderson | b27c5f8 | 2023-11-08 11:48:41 -0500 | [diff] [blame] | 12 | #include <asm/cache.h> |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 13 | #include <asm/io.h> |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 14 | |
| 15 | struct blk_dev { |
| 16 | const char *ifname; |
Sean Anderson | fcb9176 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 17 | const char *filename; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 18 | char dev_part_str[8]; |
| 19 | }; |
| 20 | |
| 21 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 22 | ulong size, void *buf) |
| 23 | { |
| 24 | loff_t actlen; |
| 25 | int ret; |
| 26 | struct blk_dev *dev = (struct blk_dev *)load->priv; |
| 27 | |
| 28 | ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY); |
| 29 | if (ret) { |
| 30 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 31 | dev->ifname, dev->dev_part_str, ret); |
| 32 | return ret; |
| 33 | } |
| 34 | |
Sean Anderson | fcb9176 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 35 | ret = fs_read(dev->filename, virt_to_phys(buf), file_offset, size, |
Sean Anderson | 5ff7772 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 36 | &actlen); |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 37 | if (ret < 0) { |
| 38 | printf("spl: error reading image %s. Err - %d\n", |
Sean Anderson | fcb9176 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 39 | dev->filename, ret); |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | return actlen; |
| 44 | } |
| 45 | |
| 46 | int spl_blk_load_image(struct spl_image_info *spl_image, |
| 47 | struct spl_boot_device *bootdev, |
| 48 | enum uclass_id uclass_id, int devnum, int partnum) |
| 49 | { |
Heinrich Schuchardt | 5048844 | 2023-07-21 14:09:43 +0200 | [diff] [blame] | 50 | const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 51 | struct blk_desc *blk_desc; |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 52 | loff_t filesize; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 53 | struct blk_dev dev; |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 54 | struct spl_load_info load; |
| 55 | int ret; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 56 | |
| 57 | blk_desc = blk_get_devnum_by_uclass_id(uclass_id, devnum); |
| 58 | if (!blk_desc) { |
| 59 | printf("blk desc for %d %d not found\n", uclass_id, devnum); |
Heinrich Schuchardt | 6eba2bc | 2023-09-06 14:25:11 +0200 | [diff] [blame] | 60 | return -ENODEV; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | blk_show_device(uclass_id, devnum); |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 64 | |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 65 | dev.filename = filename; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 66 | dev.ifname = blk_get_uclass_name(uclass_id); |
Heinrich Schuchardt | 83a91f3 | 2023-07-22 12:45:44 +0200 | [diff] [blame] | 67 | snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x", |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 68 | devnum, partnum); |
| 69 | ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY); |
| 70 | if (ret) { |
| 71 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 72 | dev.ifname, dev.dev_part_str, ret); |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 73 | return ret; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | ret = fs_size(filename, &filesize); |
| 77 | if (ret) { |
| 78 | printf("spl: unable to get file size: %s. Err - %d\n", |
| 79 | filename, ret); |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 80 | return ret; |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 81 | } |
| 82 | |
Simon Glass | e806686 | 2024-08-22 07:55:02 -0600 | [diff] [blame] | 83 | spl_load_init(&load, spl_fit_read, &dev, |
| 84 | IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN) ? |
| 85 | ARCH_DMA_MINALIGN : 1); |
Sean Anderson | 7314325 | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 86 | return spl_load(spl_image, bootdev, &load, filesize, 0); |
Mayuresh Chitale | 5d96a91 | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 87 | } |