blob: 04eac6f306bdda08113d04cd220473c61149094e [file] [log] [blame]
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +05301// 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 Anderson6029a0e2023-11-08 11:48:54 -050010#include <spl_load.h>
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053011#include <image.h>
12#include <fs.h>
Sean Andersonb63664b2023-11-08 11:48:41 -050013#include <asm/cache.h>
Sean Andersonb02c4e92023-10-14 16:47:55 -040014#include <asm/io.h>
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053015
16struct blk_dev {
17 const char *ifname;
Sean Andersonafdd2d92023-11-08 11:48:42 -050018 const char *filename;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053019 char dev_part_str[8];
20};
21
22static 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 Andersonafdd2d92023-11-08 11:48:42 -050036 ret = fs_read(dev->filename, virt_to_phys(buf), file_offset, size,
Sean Andersonb02c4e92023-10-14 16:47:55 -040037 &actlen);
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053038 if (ret < 0) {
39 printf("spl: error reading image %s. Err - %d\n",
Sean Andersonafdd2d92023-11-08 11:48:42 -050040 dev->filename, ret);
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053041 return ret;
42 }
43
44 return actlen;
45}
46
47int 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 Schuchardt8acfd7d2023-07-21 14:09:43 +020051 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053052 struct blk_desc *blk_desc;
Sean Anderson6029a0e2023-11-08 11:48:54 -050053 loff_t filesize;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053054 struct blk_dev dev;
Sean Anderson6029a0e2023-11-08 11:48:54 -050055 struct spl_load_info load;
56 int ret;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053057
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 Schuchardt323e91a2023-09-06 14:25:11 +020061 return -ENODEV;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053062 }
63
64 blk_show_device(uclass_id, devnum);
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053065
Sean Anderson6029a0e2023-11-08 11:48:54 -050066 dev.filename = filename;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053067 dev.ifname = blk_get_uclass_name(uclass_id);
Heinrich Schuchardtd62e7b82023-07-22 12:45:44 +020068 snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053069 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 Anderson6029a0e2023-11-08 11:48:54 -050074 return ret;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053075 }
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 Anderson6029a0e2023-11-08 11:48:54 -050081 return ret;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053082 }
83
Sean Anderson6029a0e2023-11-08 11:48:54 -050084 load.read = spl_fit_read;
Sean Anderson54a8d842023-11-08 11:48:58 -050085 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 Anderson6029a0e2023-11-08 11:48:54 -050089 load.priv = &dev;
90 return spl_load(spl_image, bootdev, &load, filesize, 0);
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053091}