Masami Hiramatsu | 4898679 | 2023-05-31 00:29:14 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2023, Linaro Limited |
| 4 | */ |
| 5 | |
| 6 | #define LOG_CATEGORY UCLASS_FWU_MDATA |
| 7 | |
| 8 | #include <fwu.h> |
| 9 | #include <fwu_mdata.h> |
| 10 | #include <memalign.h> |
| 11 | |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/types.h> |
| 14 | |
| 15 | /* Internal helper structure to move data around */ |
| 16 | struct fwu_mdata_mtd_priv { |
| 17 | struct mtd_info *mtd; |
| 18 | char pri_label[50]; |
| 19 | char sec_label[50]; |
| 20 | u32 pri_offset; |
| 21 | u32 sec_offset; |
| 22 | }; |
| 23 | |
| 24 | enum fwu_mtd_op { |
| 25 | FWU_MTD_READ, |
| 26 | FWU_MTD_WRITE, |
| 27 | }; |
| 28 | |
| 29 | extern struct fwu_mtd_image_info fwu_mtd_images[]; |
| 30 | |
| 31 | static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size) |
| 32 | { |
| 33 | return !do_div(size, mtd->erasesize); |
| 34 | } |
| 35 | |
| 36 | static int mtd_io_data(struct mtd_info *mtd, u32 offs, u32 size, void *data, |
| 37 | enum fwu_mtd_op op) |
| 38 | { |
| 39 | struct mtd_oob_ops io_op = {}; |
| 40 | u64 lock_len; |
| 41 | size_t len; |
| 42 | void *buf; |
| 43 | int ret; |
| 44 | |
| 45 | if (!mtd_is_aligned_with_block_size(mtd, offs)) { |
| 46 | log_err("Offset unaligned with a block (0x%x)\n", mtd->erasesize); |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | /* This will expand erase size to align with the block size */ |
| 51 | lock_len = round_up(size, mtd->erasesize); |
| 52 | |
| 53 | ret = mtd_unlock(mtd, offs, lock_len); |
| 54 | if (ret && ret != -EOPNOTSUPP) |
| 55 | return ret; |
| 56 | |
| 57 | if (op == FWU_MTD_WRITE) { |
| 58 | struct erase_info erase_op = {}; |
| 59 | |
| 60 | erase_op.mtd = mtd; |
| 61 | erase_op.addr = offs; |
| 62 | erase_op.len = lock_len; |
| 63 | erase_op.scrub = 0; |
| 64 | |
| 65 | ret = mtd_erase(mtd, &erase_op); |
| 66 | if (ret) |
| 67 | goto lock; |
| 68 | } |
| 69 | |
| 70 | /* Also, expand the write size to align with the write size */ |
| 71 | len = round_up(size, mtd->writesize); |
| 72 | |
| 73 | buf = memalign(ARCH_DMA_MINALIGN, len); |
| 74 | if (!buf) { |
| 75 | ret = -ENOMEM; |
| 76 | goto lock; |
| 77 | } |
| 78 | memset(buf, 0xff, len); |
| 79 | |
| 80 | io_op.mode = MTD_OPS_AUTO_OOB; |
| 81 | io_op.len = len; |
| 82 | io_op.datbuf = buf; |
| 83 | |
| 84 | if (op == FWU_MTD_WRITE) { |
| 85 | memcpy(buf, data, size); |
| 86 | ret = mtd_write_oob(mtd, offs, &io_op); |
| 87 | } else { |
| 88 | ret = mtd_read_oob(mtd, offs, &io_op); |
| 89 | if (!ret) |
| 90 | memcpy(data, buf, size); |
| 91 | } |
| 92 | free(buf); |
| 93 | |
| 94 | lock: |
| 95 | mtd_lock(mtd, offs, lock_len); |
| 96 | |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | static int fwu_mtd_read_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary) |
| 101 | { |
| 102 | struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); |
| 103 | struct mtd_info *mtd = mtd_priv->mtd; |
| 104 | u32 offs = primary ? mtd_priv->pri_offset : mtd_priv->sec_offset; |
| 105 | |
| 106 | return mtd_io_data(mtd, offs, sizeof(struct fwu_mdata), mdata, FWU_MTD_READ); |
| 107 | } |
| 108 | |
| 109 | static int fwu_mtd_write_mdata(struct udevice *dev, struct fwu_mdata *mdata, bool primary) |
| 110 | { |
| 111 | struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); |
| 112 | struct mtd_info *mtd = mtd_priv->mtd; |
| 113 | u32 offs = primary ? mtd_priv->pri_offset : mtd_priv->sec_offset; |
| 114 | |
| 115 | return mtd_io_data(mtd, offs, sizeof(struct fwu_mdata), mdata, FWU_MTD_WRITE); |
| 116 | } |
| 117 | |
| 118 | static int flash_partition_offset(struct udevice *dev, const char *part_name, fdt_addr_t *offset) |
| 119 | { |
| 120 | ofnode node, parts_node; |
| 121 | fdt_addr_t size = 0; |
| 122 | |
| 123 | parts_node = ofnode_by_compatible(dev_ofnode(dev), "fixed-partitions"); |
| 124 | node = ofnode_by_prop_value(parts_node, "label", part_name, strlen(part_name) + 1); |
| 125 | if (!ofnode_valid(node)) { |
| 126 | log_err("Warning: Failed to find partition by label <%s>\n", part_name); |
| 127 | return -ENOENT; |
| 128 | } |
| 129 | |
| 130 | *offset = ofnode_get_addr_size_index_notrans(node, 0, &size); |
| 131 | |
| 132 | return (int)size; |
| 133 | } |
| 134 | |
| 135 | static int fwu_mdata_mtd_of_to_plat(struct udevice *dev) |
| 136 | { |
| 137 | struct fwu_mdata_mtd_priv *mtd_priv = dev_get_priv(dev); |
| 138 | const fdt32_t *phandle_p = NULL; |
| 139 | struct udevice *mtd_dev; |
| 140 | struct mtd_info *mtd; |
| 141 | const char *label; |
| 142 | fdt_addr_t offset; |
| 143 | int ret, size; |
| 144 | u32 phandle; |
| 145 | ofnode bank; |
| 146 | int off_img; |
| 147 | |
| 148 | /* Find the FWU mdata storage device */ |
| 149 | phandle_p = ofnode_get_property(dev_ofnode(dev), |
| 150 | "fwu-mdata-store", &size); |
| 151 | if (!phandle_p) { |
| 152 | log_err("FWU meta data store not defined in device-tree\n"); |
| 153 | return -ENOENT; |
| 154 | } |
| 155 | |
| 156 | phandle = fdt32_to_cpu(*phandle_p); |
| 157 | |
| 158 | ret = device_get_global_by_ofnode(ofnode_get_by_phandle(phandle), |
| 159 | &mtd_dev); |
| 160 | if (ret) { |
| 161 | log_err("FWU: failed to get mtd device\n"); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | mtd_probe_devices(); |
| 166 | |
| 167 | mtd_for_each_device(mtd) { |
| 168 | if (mtd->dev == mtd_dev) { |
| 169 | mtd_priv->mtd = mtd; |
| 170 | log_debug("Found the FWU mdata mtd device %s\n", mtd->name); |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | if (!mtd_priv->mtd) { |
| 175 | log_err("Failed to find mtd device by fwu-mdata-store\n"); |
| 176 | return -ENODEV; |
| 177 | } |
| 178 | |
| 179 | /* Get the offset of primary and secondary mdata */ |
| 180 | ret = ofnode_read_string_index(dev_ofnode(dev), "mdata-parts", 0, &label); |
| 181 | if (ret) |
| 182 | return ret; |
| 183 | strncpy(mtd_priv->pri_label, label, 50); |
| 184 | |
| 185 | ret = flash_partition_offset(mtd_dev, mtd_priv->pri_label, &offset); |
| 186 | if (ret <= 0) |
| 187 | return ret; |
| 188 | mtd_priv->pri_offset = offset; |
| 189 | |
| 190 | ret = ofnode_read_string_index(dev_ofnode(dev), "mdata-parts", 1, &label); |
| 191 | if (ret) |
| 192 | return ret; |
| 193 | strncpy(mtd_priv->sec_label, label, 50); |
| 194 | |
| 195 | ret = flash_partition_offset(mtd_dev, mtd_priv->sec_label, &offset); |
| 196 | if (ret <= 0) |
| 197 | return ret; |
| 198 | mtd_priv->sec_offset = offset; |
| 199 | |
| 200 | off_img = 0; |
| 201 | |
| 202 | ofnode_for_each_subnode(bank, dev_ofnode(dev)) { |
| 203 | int bank_num, bank_offset, bank_size; |
| 204 | const char *bank_name; |
| 205 | ofnode image; |
| 206 | |
| 207 | ofnode_read_u32(bank, "id", &bank_num); |
| 208 | bank_name = ofnode_read_string(bank, "label"); |
| 209 | bank_size = flash_partition_offset(mtd_dev, bank_name, &offset); |
| 210 | if (bank_size <= 0) |
| 211 | return bank_size; |
| 212 | bank_offset = offset; |
| 213 | log_debug("Bank%d: %s [0x%x - 0x%x]\n", |
| 214 | bank_num, bank_name, bank_offset, bank_offset + bank_size); |
| 215 | |
| 216 | ofnode_for_each_subnode(image, bank) { |
| 217 | int image_num, image_offset, image_size; |
| 218 | const char *uuid; |
| 219 | |
| 220 | if (off_img == CONFIG_FWU_NUM_BANKS * |
| 221 | CONFIG_FWU_NUM_IMAGES_PER_BANK) { |
| 222 | log_err("DT provides more images than configured!\n"); |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | uuid = ofnode_read_string(image, "uuid"); |
| 227 | ofnode_read_u32(image, "id", &image_num); |
| 228 | ofnode_read_u32(image, "offset", &image_offset); |
| 229 | ofnode_read_u32(image, "size", &image_size); |
| 230 | |
| 231 | fwu_mtd_images[off_img].start = bank_offset + image_offset; |
| 232 | fwu_mtd_images[off_img].size = image_size; |
| 233 | fwu_mtd_images[off_img].bank_num = bank_num; |
| 234 | fwu_mtd_images[off_img].image_num = image_num; |
| 235 | strcpy(fwu_mtd_images[off_img].uuidbuf, uuid); |
| 236 | log_debug("\tImage%d: %s @0x%x\n\n", |
| 237 | image_num, uuid, bank_offset + image_offset); |
| 238 | off_img++; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static int fwu_mdata_mtd_probe(struct udevice *dev) |
| 246 | { |
| 247 | /* Ensure the metadata can be read. */ |
| 248 | return fwu_get_mdata(NULL); |
| 249 | } |
| 250 | |
| 251 | static struct fwu_mdata_ops fwu_mtd_ops = { |
| 252 | .read_mdata = fwu_mtd_read_mdata, |
| 253 | .write_mdata = fwu_mtd_write_mdata, |
| 254 | }; |
| 255 | |
| 256 | static const struct udevice_id fwu_mdata_ids[] = { |
| 257 | { .compatible = "u-boot,fwu-mdata-mtd" }, |
| 258 | { } |
| 259 | }; |
| 260 | |
| 261 | U_BOOT_DRIVER(fwu_mdata_mtd) = { |
| 262 | .name = "fwu-mdata-mtd", |
| 263 | .id = UCLASS_FWU_MDATA, |
| 264 | .of_match = fwu_mdata_ids, |
| 265 | .ops = &fwu_mtd_ops, |
| 266 | .probe = fwu_mdata_mtd_probe, |
| 267 | .of_to_plat = fwu_mdata_mtd_of_to_plat, |
| 268 | .priv_auto = sizeof(struct fwu_mdata_mtd_priv), |
| 269 | }; |