Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 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 | |
Jassi Brar | b042c70 | 2023-03-06 17:18:21 -0600 | [diff] [blame] | 27 | static uint g_mdata_part[2]; /* = {0, 0} to check against uninit parts */ |
| 28 | |
| 29 | static int gpt_get_mdata_partitions(struct blk_desc *desc) |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 30 | { |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 31 | int i; |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 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 | |
Jassi Brar | b042c70 | 2023-03-06 17:18:21 -0600 | [diff] [blame] | 37 | /* if primary and secondary partitions already found */ |
| 38 | if (g_mdata_part[0] && g_mdata_part[1]) |
| 39 | return 0; |
| 40 | |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 41 | nparts = 0; |
Jassi Brar | b042c70 | 2023-03-06 17:18:21 -0600 | [diff] [blame] | 42 | for (i = 1; i < MAX_SEARCH_PARTITIONS && nparts < 2; i++) { |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 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 | |
Jassi Brar | b042c70 | 2023-03-06 17:18:21 -0600 | [diff] [blame] | 48 | if (!guidcmp(&fwu_mdata_guid, &part_type_guid)) |
| 49 | g_mdata_part[nparts++] = i; |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if (nparts != 2) { |
| 53 | log_debug("Expect two copies of the FWU metadata instead of %d\n", |
| 54 | nparts); |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 55 | g_mdata_part[0] = 0; |
| 56 | g_mdata_part[1] = 0; |
| 57 | return -EINVAL; |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 58 | } |
| 59 | |
Jassi Brar | 246ec2a | 2023-03-06 17:18:41 -0600 | [diff] [blame] | 60 | return 0; |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 61 | } |
| 62 | |
| 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 | |
| 84 | static int gpt_read_write_mdata(struct blk_desc *desc, |
| 85 | struct fwu_mdata *mdata, |
| 86 | u8 access, u32 part_num) |
| 87 | { |
| 88 | int ret; |
| 89 | u32 len, blk_start, blkcnt; |
| 90 | struct disk_partition info; |
| 91 | |
| 92 | ALLOC_CACHE_ALIGN_BUFFER_PAD(struct fwu_mdata, mdata_aligned, 1, |
| 93 | desc->blksz); |
| 94 | |
| 95 | if (!mdata) |
| 96 | return -ENOMEM; |
| 97 | |
| 98 | ret = gpt_get_mdata_disk_part(desc, &info, part_num); |
| 99 | if (ret < 0) { |
| 100 | printf("Unable to get the FWU metadata partition\n"); |
| 101 | return -ENOENT; |
| 102 | } |
| 103 | |
| 104 | len = sizeof(*mdata); |
| 105 | blkcnt = BLOCK_CNT(len, desc); |
| 106 | if (blkcnt > info.size) { |
| 107 | log_debug("Block count exceeds FWU metadata partition size\n"); |
| 108 | return -ERANGE; |
| 109 | } |
| 110 | |
| 111 | blk_start = info.start; |
| 112 | if (access == MDATA_READ) { |
| 113 | if (blk_dread(desc, blk_start, blkcnt, mdata_aligned) != blkcnt) { |
| 114 | log_debug("Error reading FWU metadata from the device\n"); |
| 115 | return -EIO; |
| 116 | } |
| 117 | memcpy(mdata, mdata_aligned, sizeof(struct fwu_mdata)); |
| 118 | } else { |
| 119 | if (blk_dwrite(desc, blk_start, blkcnt, mdata) != blkcnt) { |
| 120 | log_debug("Error writing FWU metadata to the device\n"); |
| 121 | return -EIO; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return 0; |
| 126 | } |
| 127 | |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 128 | static int fwu_get_mdata_device(struct udevice *dev, struct udevice **mdata_dev) |
| 129 | { |
| 130 | u32 phandle; |
| 131 | int ret, size; |
| 132 | struct udevice *parent; |
| 133 | const fdt32_t *phandle_p = NULL; |
| 134 | |
| 135 | phandle_p = dev_read_prop(dev, "fwu-mdata-store", &size); |
| 136 | if (!phandle_p) { |
| 137 | log_debug("fwu-mdata-store property not found\n"); |
| 138 | return -ENOENT; |
| 139 | } |
| 140 | |
| 141 | phandle = fdt32_to_cpu(*phandle_p); |
| 142 | |
| 143 | ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle), |
| 144 | &parent); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | return blk_get_from_parent(parent, mdata_dev); |
| 149 | } |
| 150 | |
| 151 | static int fwu_mdata_gpt_blk_probe(struct udevice *dev) |
| 152 | { |
| 153 | int ret; |
| 154 | struct udevice *mdata_dev = NULL; |
| 155 | struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev); |
| 156 | |
| 157 | ret = fwu_get_mdata_device(dev, &mdata_dev); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | |
| 161 | priv->blk_dev = mdata_dev; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Jassi Brar | 3733e16 | 2023-03-06 17:18:35 -0600 | [diff] [blame] | 166 | static int fwu_gpt_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, |
| 167 | bool primary) |
| 168 | { |
| 169 | struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev); |
| 170 | struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev); |
| 171 | int ret; |
| 172 | |
| 173 | ret = gpt_get_mdata_partitions(desc); |
| 174 | if (ret < 0) { |
| 175 | log_debug("Error getting the FWU metadata partitions\n"); |
| 176 | return -ENOENT; |
| 177 | } |
| 178 | |
| 179 | return gpt_read_write_mdata(desc, mdata, MDATA_READ, |
| 180 | primary ? g_mdata_part[0] : g_mdata_part[1]); |
| 181 | } |
| 182 | |
| 183 | static int fwu_gpt_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, |
| 184 | bool primary) |
| 185 | { |
| 186 | struct fwu_mdata_gpt_blk_priv *priv = dev_get_priv(dev); |
| 187 | struct blk_desc *desc = dev_get_uclass_plat(priv->blk_dev); |
| 188 | int ret; |
| 189 | |
| 190 | ret = gpt_get_mdata_partitions(desc); |
| 191 | if (ret < 0) { |
| 192 | log_debug("Error getting the FWU metadata partitions\n"); |
| 193 | return -ENOENT; |
| 194 | } |
| 195 | |
| 196 | return gpt_read_write_mdata(desc, mdata, MDATA_WRITE, |
| 197 | primary ? g_mdata_part[0] : g_mdata_part[1]); |
| 198 | } |
| 199 | |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 200 | static const struct fwu_mdata_ops fwu_gpt_blk_ops = { |
Jassi Brar | 3733e16 | 2023-03-06 17:18:35 -0600 | [diff] [blame] | 201 | .read_mdata = fwu_gpt_read_mdata, |
| 202 | .write_mdata = fwu_gpt_write_mdata, |
Sughosh Ganu | 554b38f | 2022-10-21 18:15:56 +0530 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | static const struct udevice_id fwu_mdata_ids[] = { |
| 206 | { .compatible = "u-boot,fwu-mdata-gpt" }, |
| 207 | { } |
| 208 | }; |
| 209 | |
| 210 | U_BOOT_DRIVER(fwu_mdata_gpt_blk) = { |
| 211 | .name = "fwu-mdata-gpt-blk", |
| 212 | .id = UCLASS_FWU_MDATA, |
| 213 | .of_match = fwu_mdata_ids, |
| 214 | .ops = &fwu_gpt_blk_ops, |
| 215 | .probe = fwu_mdata_gpt_blk_probe, |
| 216 | .priv_auto = sizeof(struct fwu_mdata_gpt_blk_priv), |
| 217 | }; |