blob: c105c55877af4de86417eda4331639f3e682caf1 [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
8#include <common.h>
9#include <spl.h>
10#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;
17 char dev_part_str[8];
18};
19
20static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
21 ulong size, void *buf)
22{
23 loff_t actlen;
24 int ret;
25 struct blk_dev *dev = (struct blk_dev *)load->priv;
26
27 ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY);
28 if (ret) {
29 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
30 dev->ifname, dev->dev_part_str, ret);
31 return ret;
32 }
33
Sean Anderson5ff77722023-10-14 16:47:55 -040034 ret = fs_read(load->filename, virt_to_phys(buf), file_offset, size,
35 &actlen);
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053036 if (ret < 0) {
37 printf("spl: error reading image %s. Err - %d\n",
38 load->filename, ret);
39 return ret;
40 }
41
42 return actlen;
43}
44
45int spl_blk_load_image(struct spl_image_info *spl_image,
46 struct spl_boot_device *bootdev,
47 enum uclass_id uclass_id, int devnum, int partnum)
48{
Heinrich Schuchardt50488442023-07-21 14:09:43 +020049 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053050 struct legacy_img_hdr *header;
51 struct blk_desc *blk_desc;
52 loff_t actlen, filesize;
53 struct blk_dev dev;
Sean Andersoncc351602023-11-08 11:48:33 -050054 int ret = -ENODEV;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053055
56 blk_desc = blk_get_devnum_by_uclass_id(uclass_id, devnum);
57 if (!blk_desc) {
58 printf("blk desc for %d %d not found\n", uclass_id, devnum);
Heinrich Schuchardt6eba2bc2023-09-06 14:25:11 +020059 return -ENODEV;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053060 }
61
62 blk_show_device(uclass_id, devnum);
63 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053064
65 dev.ifname = blk_get_uclass_name(uclass_id);
Heinrich Schuchardt83a91f32023-07-22 12:45:44 +020066 snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053067 devnum, partnum);
68 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
69 if (ret) {
70 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
71 dev.ifname, dev.dev_part_str, ret);
72 goto out;
73 }
74
Sean Anderson5ff77722023-10-14 16:47:55 -040075 ret = fs_read(filename, virt_to_phys(header), 0,
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053076 sizeof(struct legacy_img_hdr), &actlen);
77 if (ret) {
78 printf("spl: unable to read file %s. Err - %d\n", filename,
79 ret);
80 goto out;
81 }
82
83 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
84 image_get_magic(header) == FDT_MAGIC) {
85 struct spl_load_info load;
86
87 debug("Found FIT\n");
88 load.read = spl_fit_read;
Sean Andersonb27c5f82023-11-08 11:48:41 -050089 load.bl_len = ARCH_DMA_MINALIGN;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053090 load.filename = (void *)filename;
91 load.priv = &dev;
92
93 return spl_load_simple_fit(spl_image, &load, 0, header);
94 }
95
96 ret = spl_parse_image_header(spl_image, bootdev, header);
97 if (ret) {
98 printf("spl: unable to parse image header. Err - %d\n",
99 ret);
100 goto out;
101 }
102
103 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
104 if (ret) {
105 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
106 dev.ifname, dev.dev_part_str, ret);
107 goto out;
108 }
109
110 ret = fs_size(filename, &filesize);
111 if (ret) {
112 printf("spl: unable to get file size: %s. Err - %d\n",
113 filename, ret);
114 goto out;
115 }
116
117 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
118 if (ret) {
119 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
120 dev.ifname, dev.dev_part_str, ret);
121 goto out;
122 }
123
124 ret = fs_read(filename, (ulong)spl_image->load_addr, 0, filesize,
125 &actlen);
126 if (ret)
127 printf("spl: unable to read file %s. Err - %d\n",
128 filename, ret);
129out:
130 return ret;
131}