1 // 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
22 enum {
23 MDATA_READ = 1,
24 MDATA_WRITE,
25 };
26
27 static uint g_mdata_part[2]; /* = {0, 0} to check against uninit parts */
28
gpt_get_mdata_partitions(struct blk_desc * desc)29 static int gpt_get_mdata_partitions(struct blk_desc *desc)
30 {
31 int i;
32 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
37 /* if primary and secondary partitions already found */
38 if (g_mdata_part[0] && g_mdata_part[1])
39 return 0;
40
41 nparts = 0;
42 for (i = 1; i < MAX_SEARCH_PARTITIONS && nparts < 2; i++) {
43 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
48 if (!guidcmp(&fwu_mdata_guid, &part_type_guid))
49 g_mdata_part[nparts++] = i;
50 }
51
52 if (nparts != 2) {
53 log_debug("Expect two copies of the FWU metadata instead of %d\n",
54 nparts);
55 g_mdata_part[0] = 0;
56 g_mdata_part[1] = 0;
57 return -EINVAL;
58 }
59
60 return 0;
61 }
62
gpt_get_mdata_disk_part(struct blk_desc * desc,struct disk_partition * info,u32 part_num)63 static 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
gpt_read_write_mdata(struct blk_desc * desc,struct fwu_mdata * mdata,u8 access,u32 part_num,u32 size)84 static int gpt_read_write_mdata(struct blk_desc *desc, struct fwu_mdata *mdata,
85 u8 access, u32 part_num, u32 size)
86 {
87 int ret;
88 u32 len, blk_start, blkcnt;
89 struct disk_partition info;
90
91 ALLOC_CACHE_ALIGN_BUFFER_PAD(u8, mdata_aligned, size,
92 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
103 len = size;
104 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 }
116 memcpy(mdata, mdata_aligned, size);
117 } 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
124 return 0;
125 }
126
fwu_get_mdata_device(struct udevice * dev,struct udevice ** mdata_dev)127 static 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
fwu_mdata_gpt_blk_probe(struct udevice * dev)150 static 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
fwu_gpt_read_mdata(struct udevice * dev,struct fwu_mdata * mdata,bool primary,u32 size)165 static int fwu_gpt_read_mdata(struct udevice *dev, struct fwu_mdata *mdata,
166 bool primary, u32 size)
167 {
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,
179 primary ?
180 g_mdata_part[0] : g_mdata_part[1],
181 size);
182 }
183
fwu_gpt_write_mdata(struct udevice * dev,struct fwu_mdata * mdata,bool primary,u32 size)184 static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata,
185 bool primary, u32 size)
186 {
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,
198 primary ?
199 g_mdata_part[0] : g_mdata_part[1],
200 size);
201 }
202
203 static const struct fwu_mdata_ops fwu_gpt_blk_ops = {
204 .read_mdata = fwu_gpt_read_mdata,
205 .write_mdata = fwu_gpt_write_mdata,
206 };
207
208 static const struct udevice_id fwu_mdata_ids[] = {
209 { .compatible = "u-boot,fwu-mdata-gpt" },
210 { }
211 };
212
213 U_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 };
221