blob: 16ecece702311c2ad3bf84fcb036b35a173336f9 [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>
12
13struct blk_dev {
14 const char *ifname;
15 char dev_part_str[8];
16};
17
18static ulong spl_fit_read(struct spl_load_info *load, ulong file_offset,
19 ulong size, void *buf)
20{
21 loff_t actlen;
22 int ret;
23 struct blk_dev *dev = (struct blk_dev *)load->priv;
24
25 ret = fs_set_blk_dev(dev->ifname, dev->dev_part_str, FS_TYPE_ANY);
26 if (ret) {
27 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
28 dev->ifname, dev->dev_part_str, ret);
29 return ret;
30 }
31
32 ret = fs_read(load->filename, (ulong)buf, file_offset, size, &actlen);
33 if (ret < 0) {
34 printf("spl: error reading image %s. Err - %d\n",
35 load->filename, ret);
36 return ret;
37 }
38
39 return actlen;
40}
41
42int spl_blk_load_image(struct spl_image_info *spl_image,
43 struct spl_boot_device *bootdev,
44 enum uclass_id uclass_id, int devnum, int partnum)
45{
Heinrich Schuchardt50488442023-07-21 14:09:43 +020046 const char *filename = CONFIG_SPL_FS_LOAD_PAYLOAD_NAME;
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053047 struct disk_partition part_info = {};
48 struct legacy_img_hdr *header;
49 struct blk_desc *blk_desc;
50 loff_t actlen, filesize;
51 struct blk_dev dev;
52 int ret;
53
54 blk_desc = blk_get_devnum_by_uclass_id(uclass_id, devnum);
55 if (!blk_desc) {
56 printf("blk desc for %d %d not found\n", uclass_id, devnum);
57 goto out;
58 }
59
60 blk_show_device(uclass_id, devnum);
61 header = spl_get_load_buffer(-sizeof(*header), sizeof(*header));
62 ret = part_get_info(blk_desc, 1, &part_info);
63 if (ret) {
64 printf("spl: no partition table found. Err - %d\n", ret);
65 goto out;
66 }
67
68 dev.ifname = blk_get_uclass_name(uclass_id);
Heinrich Schuchardt83a91f32023-07-22 12:45:44 +020069 snprintf(dev.dev_part_str, sizeof(dev.dev_part_str) - 1, "%x:%x",
Mayuresh Chitale5d96a912023-06-03 19:32:54 +053070 devnum, partnum);
71 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
72 if (ret) {
73 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
74 dev.ifname, dev.dev_part_str, ret);
75 goto out;
76 }
77
78 ret = fs_read(filename, (ulong)header, 0,
79 sizeof(struct legacy_img_hdr), &actlen);
80 if (ret) {
81 printf("spl: unable to read file %s. Err - %d\n", filename,
82 ret);
83 goto out;
84 }
85
86 if (IS_ENABLED(CONFIG_SPL_LOAD_FIT) &&
87 image_get_magic(header) == FDT_MAGIC) {
88 struct spl_load_info load;
89
90 debug("Found FIT\n");
91 load.read = spl_fit_read;
92 load.bl_len = 1;
93 load.filename = (void *)filename;
94 load.priv = &dev;
95
96 return spl_load_simple_fit(spl_image, &load, 0, header);
97 }
98
99 ret = spl_parse_image_header(spl_image, bootdev, header);
100 if (ret) {
101 printf("spl: unable to parse image header. Err - %d\n",
102 ret);
103 goto out;
104 }
105
106 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
107 if (ret) {
108 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
109 dev.ifname, dev.dev_part_str, ret);
110 goto out;
111 }
112
113 ret = fs_size(filename, &filesize);
114 if (ret) {
115 printf("spl: unable to get file size: %s. Err - %d\n",
116 filename, ret);
117 goto out;
118 }
119
120 ret = fs_set_blk_dev(dev.ifname, dev.dev_part_str, FS_TYPE_ANY);
121 if (ret) {
122 printf("spl: unable to set blk_dev %s %s. Err - %d\n",
123 dev.ifname, dev.dev_part_str, ret);
124 goto out;
125 }
126
127 ret = fs_read(filename, (ulong)spl_image->load_addr, 0, filesize,
128 &actlen);
129 if (ret)
130 printf("spl: unable to read file %s. Err - %d\n",
131 filename, ret);
132out:
133 return ret;
134}