Mayuresh Chitale | 8ce6a2e | 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 | |
| 8 | #include <common.h> |
| 9 | #include <spl.h> |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 10 | #include <spl_load.h> |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 11 | #include <image.h> |
| 12 | #include <fs.h> |
Sean Anderson | b63664b | 2023-11-08 11:48:41 -0500 | [diff] [blame] | 13 | #include <asm/cache.h> |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 14 | #include <asm/io.h> |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 15 | |
| 16 | struct blk_dev { |
| 17 | const char *ifname; |
Sean Anderson | afdd2d9 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 18 | const char *filename; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 19 | char dev_part_str[8]; |
| 20 | }; |
| 21 | |
| 22 | static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset, |
| 23 | ulong size, void *buf) |
| 24 | { |
| 25 | loff_t actlen; |
| 26 | int ret; |
| 27 | struct blk_dev *dev = (struct blk_dev *)load->priv; |
| 28 | |
| 29 | ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY); |
| 30 | if (ret) { |
| 31 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 32 | dev->ifname, dev->dev_part_str, ret); |
| 33 | return ret; |
| 34 | } |
| 35 | |
Sean Anderson | afdd2d9 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 36 | ret = fs_read(dev->filename, virt_to_phys(buf), file_offset, size, |
Sean Anderson | b02c4e9 | 2023-10-14 16:47:55 -0400 | [diff] [blame] | 37 | &actlen); |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 38 | if (ret < 0) { |
| 39 | printf("spl: error reading image %s. Err - %d\n", |
Sean Anderson | afdd2d9 | 2023-11-08 11:48:42 -0500 | [diff] [blame] | 40 | dev->filename, ret); |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 41 | return ret; |
| 42 | } |
| 43 | |
| 44 | return actlen; |
| 45 | } |
| 46 | |
| 47 | int spl_blk_load_image(struct spl_image_info *spl_image, |
| 48 | struct spl_boot_device *bootdev, |
| 49 | enum uclass_id uclass_id, int devnum, int partnum) |
| 50 | { |
Heinrich Schuchardt | 8acfd7d | 2023-07-21 14:09:43 +0200 | [diff] [blame] | 51 | const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 52 | struct blk_desc *blk_desc; |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 53 | loff_t filesize; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 54 | struct blk_dev dev; |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 55 | struct spl_load_info load; |
| 56 | int ret; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 57 | |
| 58 | blk_desc = blk_get_devnum_by_uclass_id(uclass_id, devnum); |
| 59 | if (!blk_desc) { |
| 60 | printf("blk desc for %d %d not found\n", uclass_id, devnum); |
Heinrich Schuchardt | 323e91a | 2023-09-06 14:25:11 +0200 | [diff] [blame] | 61 | return -ENODEV; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | blk_show_device(uclass_id, devnum); |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 65 | |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 66 | dev.filename = filename; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 67 | dev.ifname = blk_get_uclass_name(uclass_id); |
Heinrich Schuchardt | d62e7b8 | 2023-07-22 12:45:44 +0200 | [diff] [blame] | 68 | snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x", |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 69 | devnum, partnum); |
| 70 | ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY); |
| 71 | if (ret) { |
| 72 | printf("spl: unable to set blk_dev %s %s. Err - %d\n", |
| 73 | dev.ifname, dev.dev_part_str, ret); |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 74 | return ret; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | ret = fs_size(filename, &filesize); |
| 78 | if (ret) { |
| 79 | printf("spl: unable to get file size: %s. Err - %d\n", |
| 80 | filename, ret); |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 81 | return ret; |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 82 | } |
| 83 | |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 84 | load.read = spl_fit_read; |
Sean Anderson | 54a8d84 | 2023-11-08 11:48:58 -0500 | [diff] [blame] | 85 | if (IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN)) |
| 86 | spl_set_bl_len(&load, ARCH_DMA_MINALIGN); |
| 87 | else |
| 88 | spl_set_bl_len(&load, 1); |
Sean Anderson | 6029a0e | 2023-11-08 11:48:54 -0500 | [diff] [blame] | 89 | load.priv = &dev; |
| 90 | return spl_load(spl_image, bootdev, &load, filesize, 0); |
Mayuresh Chitale | 8ce6a2e | 2023-06-03 19:32:54 +0530 | [diff] [blame] | 91 | } |