blob: bbf90a9741eeb760f710a16db81871b40dc0c910 [file] [log] [blame]
Mayuresh Chitale5d96a912023-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
Mayuresh Chitale5d96a912023-06-03 19:32:54 +05308#include <spl.h>
Sean Anderson73143252023-11-08 11:48:54 -05009#include <spl_load.h>
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053010#include <image.h>
11#include <fs.h>
Sean Andersonb27c5f82023-11-08 11:48:41 -050012#include <asm/cache.h>
Sean Anderson5ff77722023-10-14 16:47:55 -040013#include <asm/io.h>
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053014
15struct blk_dev {
16 const char *ifname;
Sean Andersonfcb91762023-11-08 11:48:42 -050017 const char *filename;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053018 char dev_part_str[8];
19};
20
21static 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 Andersonfcb91762023-11-08 11:48:42 -050035 ret = fs_read(dev->filename, virt_to_phys(buf), file_offset, size,
Sean Anderson5ff77722023-10-14 16:47:55 -040036 &actlen);
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053037 if (ret < 0) {
38 printf("spl: error reading image %s. Err - %d\n",
Sean Andersonfcb91762023-11-08 11:48:42 -050039 dev->filename, ret);
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053040 return ret;
41 }
42
43 return actlen;
44}
45
46int 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 Schuchardt50488442023-07-21 14:09:43 +020050 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053051 struct blk_desc *blk_desc;
Sean Anderson73143252023-11-08 11:48:54 -050052 loff_t filesize;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053053 struct blk_dev dev;
Sean Anderson73143252023-11-08 11:48:54 -050054 struct spl_load_info load;
55 int ret;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053056
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 Schuchardt6eba2bc2023-09-06 14:25:11 +020060 return -ENODEV;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053061 }
62
63 blk_show_device(uclass_id, devnum);
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053064
Sean Anderson73143252023-11-08 11:48:54 -050065 dev.filename = filename;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053066 dev.ifname = blk_get_uclass_name(uclass_id);
Heinrich Schuchardt83a91f32023-07-22 12:45:44 +020067 snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053068 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 Anderson73143252023-11-08 11:48:54 -050073 return ret;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053074 }
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 Anderson73143252023-11-08 11:48:54 -050080 return ret;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053081 }
82
Simon Glasse8066862024-08-22 07:55:02 -060083 spl_load_init(&load, spl_fit_read, &dev,
84 IS_ENABLED(CONFIG_SPL_FS_FAT_DMA_ALIGN) ?
85 ARCH_DMA_MINALIGN : 1);
Sean Anderson73143252023-11-08 11:48:54 -050086 return spl_load(spl_image, bootdev, &load, filesize, 0);
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053087}