blob: 63825d620d11a456f72503d52fd00b83c5d87bd7 [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 Anderson5ff77722023-10-14 16:47:55 -040012#include <asm/io.h>
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053013
14struct blk_dev {
15 const char *ifname;
16 char dev_part_str[8];
17};
18
19static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
20 ulong size, void *buf)
21{
22 loff_t actlen;
23 int ret;
24 struct blk_dev *dev = (struct blk_dev *)load->priv;
25
26 ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY);
27 if (ret) {
28 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
29 dev->ifname, dev->dev_part_str, ret);
30 return ret;
31 }
32
Sean Anderson5ff77722023-10-14 16:47:55 -040033 ret = fs_read(load->filename, virt_to_phys(buf), file_offset, size,
34 &actlen);
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053035 if (ret < 0) {
36 printf("spl: error reading image %s. Err - %d\n",
37 load->filename, ret);
38 return ret;
39 }
40
41 return actlen;
42}
43
44int spl_blk_load_image(struct spl_image_info *spl_image,
45 struct spl_boot_device *bootdev,
46 enum uclass_id uclass_id, int devnum, int partnum)
47{
Heinrich Schuchardt50488442023-07-21 14:09:43 +020048 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053049 struct legacy_img_hdr *header;
50 struct blk_desc *blk_desc;
51 loff_t actlen, filesize;
52 struct blk_dev dev;
53 int ret;
54
55 blk_desc = blk_get_devnum_by_uclass_id(uclass_id, devnum);
56 if (!blk_desc) {
57 printf("blk desc for %d %d not found\n", uclass_id, devnum);
Heinrich Schuchardt6eba2bc2023-09-06 14:25:11 +020058 return -ENODEV;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053059 }
60
61 blk_show_device(uclass_id, devnum);
62 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053063
64 dev.ifname = blk_get_uclass_name(uclass_id);
Heinrich Schuchardt83a91f32023-07-22 12:45:44 +020065 snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053066 devnum, partnum);
67 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
68 if (ret) {
69 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
70 dev.ifname, dev.dev_part_str, ret);
71 goto out;
72 }
73
Sean Anderson5ff77722023-10-14 16:47:55 -040074 ret = fs_read(filename, virt_to_phys(header), 0,
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053075 sizeof(struct legacy_img_hdr), &actlen);
76 if (ret) {
77 printf("spl: unable to read file %s. Err - %d\n", filename,
78 ret);
79 goto out;
80 }
81
82 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
83 image_get_magic(header) == FDT_MAGIC) {
84 struct spl_load_info load;
85
86 debug("Found FIT\n");
87 load.read = spl_fit_read;
88 load.bl_len = 1;
89 load.filename = (void *)filename;
90 load.priv = &dev;
91
92 return spl_load_simple_fit(spl_image, &load, 0, header);
93 }
94
95 ret = spl_parse_image_header(spl_image, bootdev, header);
96 if (ret) {
97 printf("spl: unable to parse image header. Err - %d\n",
98 ret);
99 goto out;
100 }
101
102 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
103 if (ret) {
104 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
105 dev.ifname, dev.dev_part_str, ret);
106 goto out;
107 }
108
109 ret = fs_size(filename, &filesize);
110 if (ret) {
111 printf("spl: unable to get file size: %s. Err - %d\n",
112 filename, ret);
113 goto out;
114 }
115
116 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
117 if (ret) {
118 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
119 dev.ifname, dev.dev_part_str, ret);
120 goto out;
121 }
122
123 ret = fs_read(filename, (ulong)spl_image->load_addr, 0, filesize,
124 &actlen);
125 if (ret)
126 printf("spl: unable to read file %s. Err - %d\n",
127 filename, ret);
128out:
129 return ret;
130}