blob: 53b8e1b11b4b5efcef8c264530424565ffb3c35e [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>
10#include <image.h>
11#include <fs.h>
Sean Andersonb63664b2023-11-08 11:48:41 -050012#include <asm/cache.h>
Sean Andersonb02c4e92023-10-14 16:47:55 -040013#include <asm/io.h>
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053014
15struct blk_dev {
16 const char *ifname;
Sean Andersonafdd2d92023-11-08 11:48:42 -050017 const char *filename;
Mayuresh Chitale8ce6a2e2023-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 Andersonafdd2d92023-11-08 11:48:42 -050035 ret = fs_read(dev->filename, virt_to_phys(buf), file_offset, size,
Sean Andersonb02c4e92023-10-14 16:47:55 -040036 &actlen);
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053037 if (ret < 0) {
38 printf("spl: error reading image %s. Err - %d\n",
Sean Andersonafdd2d92023-11-08 11:48:42 -050039 dev->filename, ret);
Mayuresh Chitale8ce6a2e2023-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 Schuchardt8acfd7d2023-07-21 14:09:43 +020050 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053051 struct legacy_img_hdr *header;
52 struct blk_desc *blk_desc;
53 loff_t actlen, filesize;
54 struct blk_dev dev;
Sean Anderson1e862962023-11-08 11:48:33 -050055 int ret = -ENODEV;
Mayuresh Chitale8ce6a2e2023-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 Schuchardt323e91a2023-09-06 14:25:11 +020060 return -ENODEV;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053061 }
62
63 blk_show_device(uclass_id, devnum);
64 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053065
66 dev.ifname = blk_get_uclass_name(uclass_id);
Heinrich Schuchardtd62e7b82023-07-22 12:45:44 +020067 snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
Mayuresh Chitale8ce6a2e2023-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);
73 goto out;
74 }
75
Sean Andersonb02c4e92023-10-14 16:47:55 -040076 ret = fs_read(filename, virt_to_phys(header), 0,
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053077 sizeof(struct legacy_img_hdr), &actlen);
78 if (ret) {
79 printf("spl: unable to read file %s. Err - %d\n", filename,
80 ret);
81 goto out;
82 }
83
84 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
85 image_get_magic(header) == FDT_MAGIC) {
86 struct spl_load_info load;
87
88 debug("Found FIT\n");
89 load.read = spl_fit_read;
Sean Anderson5271e352023-11-08 11:48:43 -050090 spl_set_bl_len(&load, ARCH_DMA_MINALIGN);
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053091 load.priv = &dev;
Sean Andersonafdd2d92023-11-08 11:48:42 -050092 dev.filename = filename;
Mayuresh Chitale8ce6a2e2023-06-03 19:32:54 +053093
94 return spl_load_simple_fit(spl_image, &load, 0, header);
95 }
96
97 ret = spl_parse_image_header(spl_image, bootdev, header);
98 if (ret) {
99 printf("spl: unable to parse image header. Err - %d\n",
100 ret);
101 goto out;
102 }
103
104 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
105 if (ret) {
106 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
107 dev.ifname, dev.dev_part_str, ret);
108 goto out;
109 }
110
111 ret = fs_size(filename, &filesize);
112 if (ret) {
113 printf("spl: unable to get file size: %s. Err - %d\n",
114 filename, ret);
115 goto out;
116 }
117
118 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
119 if (ret) {
120 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
121 dev.ifname, dev.dev_part_str, ret);
122 goto out;
123 }
124
125 ret = fs_read(filename, (ulong)spl_image->load_addr, 0, filesize,
126 &actlen);
127 if (ret)
128 printf("spl: unable to read file %s. Err - %d\n",
129 filename, ret);
130out:
131 return ret;
132}