blob: 97eac3611f7b60d06392316c11d67446d366ff8e [file] [log] [blame]
Sughosh Ganuee062b52022-10-21 18:15:56 +05301// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2022, Linaro Limited
4 */
5
6#define LOG_CATEGORY UCLASS_FWU_MDATA
7
8#include <blk.h>
9#include <dm.h>
10#include <efi_loader.h>
11#include <fwu.h>
12#include <fwu_mdata.h>
13#include <log.h>
14#include <memalign.h>
15#include <part.h>
16#include <part_efi.h>
17
18#include <dm/device-internal.h>
19#include <linux/errno.h>
20#include <linux/types.h>
21
22enum {
23 MDATA_READ = 1,
24 MDATA_WRITE,
25};
26
Jassi Braradfaad22023-03-06 17:18:21 -060027static uint g_mdata_part[2]; /* = {0, 0} to check against uninit parts */
28
29static int gpt_get_mdata_partitions(struct blk_desc *desc)
Sughosh Ganuee062b52022-10-21 18:15:56 +053030{
Jassi Brara1df12f2023-03-06 17:18:41 -060031 int i;
Sughosh Ganuee062b52022-10-21 18:15:56 +053032 u32 nparts;
33 efi_guid_t part_type_guid;
34 struct disk_partition info;
35 const efi_guid_t fwu_mdata_guid = FWU_MDATA_GUID;
36
Jassi Braradfaad22023-03-06 17:18:21 -060037 /* if primary and secondary partitions already found */
38 if (g_mdata_part[0] && g_mdata_part[1])
39 return 0;
40
Sughosh Ganuee062b52022-10-21 18:15:56 +053041 nparts = 0;
Jassi Braradfaad22023-03-06 17:18:21 -060042 for (i = 1; i < MAX_SEARCH_PARTITIONS && nparts < 2; i++) {
Sughosh Ganuee062b52022-10-21 18:15:56 +053043 if (part_get_info(desc, i, &info))
44 continue;
45 uuid_str_to_bin(info.type_guid, part_type_guid.b,
46 UUID_STR_FORMAT_GUID);
47
Jassi Braradfaad22023-03-06 17:18:21 -060048 if (!guidcmp(&fwu_mdata_guid, &part_type_guid))
49 g_mdata_part[nparts++] = i;
Sughosh Ganuee062b52022-10-21 18:15:56 +053050 }
51
52 if (nparts != 2) {
53 log_debug("Expect two copies of the FWU metadata instead of %d\n",
54 nparts);
Jassi Brara1df12f2023-03-06 17:18:41 -060055 g_mdata_part[0] = 0;
56 g_mdata_part[1] = 0;
57 return -EINVAL;
Sughosh Ganuee062b52022-10-21 18:15:56 +053058 }
59
Jassi Brara1df12f2023-03-06 17:18:41 -060060 return 0;
Sughosh Ganuee062b52022-10-21 18:15:56 +053061}
62
63static int gpt_get_mdata_disk_part(struct blk_desc *desc,
64 struct disk_partition *info,
65 u32 part_num)
66{
67 int ret;
68 char *mdata_guid_str = "8a7a84a0-8387-40f6-ab41-a8b9a5a60d23";
69
70 ret = part_get_info(desc, part_num, info);
71 if (ret < 0) {
72 log_debug("Unable to get the partition info for the FWU metadata part %d\n",
73 part_num);
74 return -ENOENT;
75 }
76
77 /* Check that it is indeed the FWU metadata partition */
78 if (!strncmp(info->type_guid, mdata_guid_str, UUID_STR_LEN))
79 return 0;
80
81 return -ENOENT;
82}
83
Sughosh Ganu15665c52024-03-22 16:27:16 +053084static int gpt_read_write_mdata(struct blk_desc *desc, struct fwu_mdata *mdata,
85 u8 access, u32 part_num, u32 size)
Sughosh Ganuee062b52022-10-21 18:15:56 +053086{
87 int ret;
88 u32 len, blk_start, blkcnt;
89 struct disk_partition info;
90
Sughosh Ganu15665c52024-03-22 16:27:16 +053091 ALLOC_CACHE_ALIGN_BUFFER_PAD(u8, mdata_aligned, size,
Sughosh Ganuee062b52022-10-21 18:15:56 +053092 desc->blksz);
93
94 if (!mdata)
95 return -ENOMEM;
96
97 ret = gpt_get_mdata_disk_part(desc, &info, part_num);
98 if (ret < 0) {
99 printf("Unable to get the FWU metadata partition\n");
100 return -ENOENT;
101 }
102
Sughosh Ganu15665c52024-03-22 16:27:16 +0530103 len = size;
Sughosh Ganuee062b52022-10-21 18:15:56 +0530104 blkcnt = BLOCK_CNT(len, desc);
105 if (blkcnt > info.size) {
106 log_debug("Block count exceeds FWU metadata partition size\n");
107 return -ERANGE;
108 }
109
110 blk_start = info.start;
111 if (access == MDATA_READ) {
112 if (blk_dread(desc, blk_start, blkcnt, mdata_aligned) != blkcnt) {
113 log_debug("Error reading FWU metadata from the device\n");
114 return -EIO;
115 }
Sughosh Ganu15665c52024-03-22 16:27:16 +0530116 memcpy(mdata, mdata_aligned, size);
Sughosh Ganuee062b52022-10-21 18:15:56 +0530117 } else {
118 if (blk_dwrite(desc, blk_start, blkcnt, mdata) != blkcnt) {
119 log_debug("Error writing FWU metadata to the device\n");
120 return -EIO;
121 }
122 }
123
Sughosh Ganuee062b52022-10-21 18:15:56 +0530124 return 0;
125}
126
Sughosh Ganuee062b52022-10-21 18:15:56 +0530127static int fwu_get_mdata_device(struct udevice *dev, struct udevice **mdata_dev)
128{
129 u32 phandle;
130 int ret, size;
131 struct udevice *parent;
132 const fdt32_t *phandle_p = NULL;
133
134 phandle_p = dev_read_prop(dev, "fwu-mdata-store", &size);
135 if (!phandle_p) {
136 log_debug("fwu-mdata-store property not found\n");
137 return -ENOENT;
138 }
139
140 phandle = fdt32_to_cpu(*phandle_p);
141
142 ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle),
143 &parent);
144 if (ret)
145 return ret;
146
147 return blk_get_from_parent(parent, mdata_dev);
148}
149
150static int fwu_mdata_gpt_blk_probe(struct udevice *dev)
151{
152 int ret;
153 struct udevice *mdata_dev = NULL;
154 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
155
156 ret = fwu_get_mdata_device(dev, &mdata_dev);
157 if (ret)
158 return ret;
159
160 priv->blk_dev = mdata_dev;
161
162 return 0;
163}
164
Jassi Brar1acc3c82023-03-06 17:18:35 -0600165static int fwu_gpt_read_mdata(struct udevice *dev, struct fwu_mdata *mdata,
Sughosh Ganu15665c52024-03-22 16:27:16 +0530166 bool primary, u32 size)
Jassi Brar1acc3c82023-03-06 17:18:35 -0600167{
168 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
169 struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);
170 int ret;
171
172 ret = gpt_get_mdata_partitions(desc);
173 if (ret < 0) {
174 log_debug("Error getting the FWU metadata partitions\n");
175 return -ENOENT;
176 }
177
178 return gpt_read_write_mdata(desc, mdata, MDATA_READ,
Sughosh Ganu15665c52024-03-22 16:27:16 +0530179 primary ?
180 g_mdata_part[0] : g_mdata_part[1],
181 size);
Jassi Brar1acc3c82023-03-06 17:18:35 -0600182}
183
184static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
Sughosh Ganu15665c52024-03-22 16:27:16 +0530185 bool primary, u32 size)
Jassi Brar1acc3c82023-03-06 17:18:35 -0600186{
187 struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev);
188 struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev);
189 int ret;
190
191 ret = gpt_get_mdata_partitions(desc);
192 if (ret < 0) {
193 log_debug("Error getting the FWU metadata partitions\n");
194 return -ENOENT;
195 }
196
197 return gpt_read_write_mdata(desc, mdata, MDATA_WRITE,
Sughosh Ganu15665c52024-03-22 16:27:16 +0530198 primary ?
199 g_mdata_part[0] : g_mdata_part[1],
200 size);
Jassi Brar1acc3c82023-03-06 17:18:35 -0600201}
202
Sughosh Ganuee062b52022-10-21 18:15:56 +0530203static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
Jassi Brar1acc3c82023-03-06 17:18:35 -0600204 .read_mdata = fwu_gpt_read_mdata,
205 .write_mdata = fwu_gpt_write_mdata,
Sughosh Ganuee062b52022-10-21 18:15:56 +0530206};
207
208static const struct udevice_id fwu_mdata_ids[] = {
209 { .compatible = "u-boot,fwu-mdata-gpt" },
210 { }
211};
212
213U_BOOT_DRIVER(fwu_mdata_gpt_blk) = {
214 .name = "fwu-mdata-gpt-blk",
215 .id = UCLASS_FWU_MDATA,
216 .of_match = fwu_mdata_ids,
217 .ops = &fwu_gpt_blk_ops,
218 .probe = fwu_mdata_gpt_blk_probe,
219 .priv_auto = sizeof(struct fwu_mdata_gpt_blk_priv),
220};