|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Configfs entries for device-tree |
| 4 | + * |
| 5 | + * Copyright (C) 2013 - Pantelis Antoniou <[email protected]> |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU General Public License |
| 9 | + * as published by the Free Software Foundation; either version |
| 10 | + * 2 of the License, or (at your option) any later version. |
| 11 | + */ |
| 12 | +#include <linux/ctype.h> |
| 13 | +#include <linux/cpu.h> |
| 14 | +#include <linux/module.h> |
| 15 | +#include <linux/of.h> |
| 16 | +#include <linux/of_fdt.h> |
| 17 | +#include <linux/spinlock.h> |
| 18 | +#include <linux/sizes.h> |
| 19 | +#include <linux/slab.h> |
| 20 | +#include <linux/proc_fs.h> |
| 21 | +#include <linux/configfs.h> |
| 22 | +#include <linux/types.h> |
| 23 | +#include <linux/stat.h> |
| 24 | +#include <linux/limits.h> |
| 25 | +#include <linux/file.h> |
| 26 | +#include <linux/vmalloc.h> |
| 27 | +#include <linux/firmware.h> |
| 28 | + |
| 29 | +#include "of_private.h" |
| 30 | + |
| 31 | +struct cfs_overlay_item { |
| 32 | + struct config_item item; |
| 33 | + |
| 34 | + char path[PATH_MAX]; |
| 35 | + |
| 36 | + const struct firmware *fw; |
| 37 | + struct device_node *overlay; |
| 38 | + int ov_id; |
| 39 | + |
| 40 | + void *dtbo; |
| 41 | + int dtbo_size; |
| 42 | + |
| 43 | + void *mem; |
| 44 | +}; |
| 45 | + |
| 46 | +static DEFINE_MUTEX(overlay_lock); |
| 47 | + |
| 48 | +static int create_overlay(struct cfs_overlay_item *overlay, void *blob) |
| 49 | +{ |
| 50 | + int err; |
| 51 | + |
| 52 | + /* FIXME */ |
| 53 | + err = of_overlay_fdt_apply(blob, overlay->dtbo_size, &overlay->ov_id, NULL); |
| 54 | + if (err < 0) { |
| 55 | + pr_err("%s: Failed to create overlay (err=%d)\n", |
| 56 | + __func__, err); |
| 57 | + return err; |
| 58 | + } |
| 59 | + |
| 60 | + return err; |
| 61 | +} |
| 62 | + |
| 63 | +static inline struct cfs_overlay_item |
| 64 | + *to_cfs_overlay_item(struct config_item *item) |
| 65 | +{ |
| 66 | + return item ? container_of(item, struct cfs_overlay_item, item) : NULL; |
| 67 | +} |
| 68 | + |
| 69 | +static ssize_t cfs_overlay_item_path_show(struct config_item *item, char *page) |
| 70 | +{ |
| 71 | + return sprintf(page, "%s\n", to_cfs_overlay_item(item)->path); |
| 72 | +} |
| 73 | + |
| 74 | +static ssize_t cfs_overlay_item_path_store(struct config_item *item, |
| 75 | + const char *page, size_t count) |
| 76 | +{ |
| 77 | + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item); |
| 78 | + const char *p = page; |
| 79 | + char *s; |
| 80 | + int err; |
| 81 | + |
| 82 | + /* if it's set do not allow changes */ |
| 83 | + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0) |
| 84 | + return -EPERM; |
| 85 | + |
| 86 | + /* copy to path buffer (and make sure it's always zero terminated */ |
| 87 | + count = snprintf(overlay->path, sizeof(overlay->path) - 1, "%s", p); |
| 88 | + overlay->path[sizeof(overlay->path) - 1] = '\0'; |
| 89 | + |
| 90 | + /* strip trailing newlines */ |
| 91 | + s = overlay->path + strlen(overlay->path); |
| 92 | + while (s > overlay->path && *--s == '\n') |
| 93 | + *s = '\0'; |
| 94 | + |
| 95 | + pr_debug("%s: path is '%s'\n", __func__, overlay->path); |
| 96 | + |
| 97 | + err = request_firmware(&overlay->fw, overlay->path, NULL); |
| 98 | + if (err != 0) |
| 99 | + goto out_err; |
| 100 | + |
| 101 | + overlay->dtbo_size = overlay->fw->size; |
| 102 | + err = create_overlay(overlay, (void *)overlay->fw->data); |
| 103 | + if (err < 0) |
| 104 | + goto out_err; |
| 105 | + |
| 106 | + return count; |
| 107 | + |
| 108 | +out_err: |
| 109 | + |
| 110 | + release_firmware(overlay->fw); |
| 111 | + overlay->fw = NULL; |
| 112 | + |
| 113 | + overlay->path[0] = '\0'; |
| 114 | + |
| 115 | + return count; |
| 116 | +} |
| 117 | + |
| 118 | +static ssize_t cfs_overlay_item_status_show(struct config_item *item, |
| 119 | + char *page) |
| 120 | +{ |
| 121 | + return sprintf(page, "%s\n", to_cfs_overlay_item(item)->ov_id >= 0 ? |
| 122 | + "applied" : "unapplied"); |
| 123 | +} |
| 124 | + |
| 125 | +CONFIGFS_ATTR(cfs_overlay_item_, path); |
| 126 | +CONFIGFS_ATTR_RO(cfs_overlay_item_, status); |
| 127 | + |
| 128 | +static struct configfs_attribute *cfs_overlay_attrs[] = { |
| 129 | + &cfs_overlay_item_attr_path, |
| 130 | + &cfs_overlay_item_attr_status, |
| 131 | + NULL, |
| 132 | +}; |
| 133 | + |
| 134 | +static ssize_t cfs_overlay_item_dtbo_read(struct config_item *item, |
| 135 | + void *buf, size_t max_count) |
| 136 | +{ |
| 137 | + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item); |
| 138 | + |
| 139 | + pr_debug("%s: buf=%p max_count=%zu\n", __func__, buf, max_count); |
| 140 | + |
| 141 | + if (!overlay->dtbo) |
| 142 | + return 0; |
| 143 | + |
| 144 | + /* copy if buffer provided */ |
| 145 | + if (buf) { |
| 146 | + /* the buffer must be large enough */ |
| 147 | + if (overlay->dtbo_size > max_count) |
| 148 | + return -ENOSPC; |
| 149 | + |
| 150 | + memcpy(buf, overlay->dtbo, overlay->dtbo_size); |
| 151 | + } |
| 152 | + |
| 153 | + return overlay->dtbo_size; |
| 154 | +} |
| 155 | + |
| 156 | +static ssize_t cfs_overlay_item_dtbo_write(struct config_item *item, |
| 157 | + const void *buf, size_t count) |
| 158 | +{ |
| 159 | + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item); |
| 160 | + int err; |
| 161 | + |
| 162 | + /* if it's set do not allow changes */ |
| 163 | + if (overlay->path[0] != '\0' || overlay->dtbo_size > 0) |
| 164 | + return -EPERM; |
| 165 | + |
| 166 | + /* copy the contents */ |
| 167 | + overlay->dtbo = kmemdup(buf, count, GFP_KERNEL); |
| 168 | + if (!overlay->dtbo) |
| 169 | + return -ENOMEM; |
| 170 | + |
| 171 | + overlay->dtbo_size = count; |
| 172 | + |
| 173 | + err = create_overlay(overlay, overlay->dtbo); |
| 174 | + if (err < 0) |
| 175 | + goto out_err; |
| 176 | + |
| 177 | + return count; |
| 178 | + |
| 179 | +out_err: |
| 180 | + kfree(overlay->dtbo); |
| 181 | + overlay->dtbo = NULL; |
| 182 | + overlay->dtbo_size = 0; |
| 183 | + |
| 184 | + return err; |
| 185 | +} |
| 186 | + |
| 187 | +CONFIGFS_BIN_ATTR(cfs_overlay_item_, dtbo, NULL, SZ_1M); |
| 188 | + |
| 189 | +static struct configfs_bin_attribute *cfs_overlay_bin_attrs[] = { |
| 190 | + &cfs_overlay_item_attr_dtbo, |
| 191 | + NULL, |
| 192 | +}; |
| 193 | + |
| 194 | +static void cfs_overlay_release(struct config_item *item) |
| 195 | +{ |
| 196 | + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item); |
| 197 | + |
| 198 | + if (overlay->ov_id >= 0) |
| 199 | + of_overlay_remove(&overlay->ov_id); |
| 200 | + if (overlay->fw) |
| 201 | + release_firmware(overlay->fw); |
| 202 | + /* kfree with NULL is safe */ |
| 203 | + kfree(overlay->dtbo); |
| 204 | + kfree(overlay->mem); |
| 205 | + kfree(overlay); |
| 206 | +} |
| 207 | + |
| 208 | +static struct configfs_item_operations cfs_overlay_item_ops = { |
| 209 | + .release = cfs_overlay_release, |
| 210 | +}; |
| 211 | + |
| 212 | +static struct config_item_type cfs_overlay_type = { |
| 213 | + .ct_item_ops = &cfs_overlay_item_ops, |
| 214 | + .ct_attrs = cfs_overlay_attrs, |
| 215 | + .ct_bin_attrs = cfs_overlay_bin_attrs, |
| 216 | + .ct_owner = THIS_MODULE, |
| 217 | +}; |
| 218 | + |
| 219 | +static struct config_item |
| 220 | + *cfs_overlay_group_make_item(struct config_group *group, |
| 221 | + const char *name) |
| 222 | +{ |
| 223 | + struct cfs_overlay_item *overlay; |
| 224 | + |
| 225 | + overlay = kzalloc(sizeof(*overlay), GFP_KERNEL); |
| 226 | + if (!overlay) |
| 227 | + return ERR_PTR(-ENOMEM); |
| 228 | + overlay->ov_id = -1; |
| 229 | + config_item_init_type_name(&overlay->item, name, &cfs_overlay_type); |
| 230 | + |
| 231 | + return &overlay->item; |
| 232 | +} |
| 233 | + |
| 234 | +static void cfs_overlay_group_drop_item(struct config_group *group, |
| 235 | + struct config_item *item) |
| 236 | +{ |
| 237 | + struct cfs_overlay_item *overlay = to_cfs_overlay_item(item); |
| 238 | + |
| 239 | + config_item_put(&overlay->item); |
| 240 | +} |
| 241 | + |
| 242 | +static struct configfs_group_operations overlays_ops = { |
| 243 | + .make_item = cfs_overlay_group_make_item, |
| 244 | + .drop_item = cfs_overlay_group_drop_item, |
| 245 | +}; |
| 246 | + |
| 247 | +static struct config_item_type overlays_type = { |
| 248 | + .ct_group_ops = &overlays_ops, |
| 249 | + .ct_owner = THIS_MODULE, |
| 250 | +}; |
| 251 | + |
| 252 | +static struct configfs_group_operations of_cfs_ops = { |
| 253 | + /* empty - we don't allow anything to be created */ |
| 254 | +}; |
| 255 | + |
| 256 | +static struct config_item_type of_cfs_type = { |
| 257 | + .ct_group_ops = &of_cfs_ops, |
| 258 | + .ct_owner = THIS_MODULE, |
| 259 | +}; |
| 260 | + |
| 261 | +struct config_group of_cfs_overlay_group; |
| 262 | + |
| 263 | +static struct configfs_subsystem of_cfs_subsys = { |
| 264 | + .su_group = { |
| 265 | + .cg_item = { |
| 266 | + .ci_namebuf = "device-tree", |
| 267 | + .ci_type = &of_cfs_type, |
| 268 | + }, |
| 269 | + }, |
| 270 | + .su_mutex = __MUTEX_INITIALIZER(of_cfs_subsys.su_mutex), |
| 271 | +}; |
| 272 | + |
| 273 | +static int __init of_cfs_init(void) |
| 274 | +{ |
| 275 | + int ret; |
| 276 | + |
| 277 | + config_group_init(&of_cfs_subsys.su_group); |
| 278 | + config_group_init_type_name(&of_cfs_overlay_group, "overlays", |
| 279 | + &overlays_type); |
| 280 | + configfs_add_default_group(&of_cfs_overlay_group, |
| 281 | + &of_cfs_subsys.su_group); |
| 282 | + |
| 283 | + ret = configfs_register_subsystem(&of_cfs_subsys); |
| 284 | + if (ret != 0) { |
| 285 | + pr_err("%s: failed to register subsys\n", __func__); |
| 286 | + goto out; |
| 287 | + } |
| 288 | + pr_debug("%s: OK\n", __func__); |
| 289 | +out: |
| 290 | + return ret; |
| 291 | +} |
| 292 | +late_initcall(of_cfs_init); |
0 commit comments