127755cf7SEnric Balletbo i Serra // SPDX-License-Identifier: GPL-2.0+ 227755cf7SEnric Balletbo i Serra // Expose the vboot context nvram to userspace 327755cf7SEnric Balletbo i Serra // 427755cf7SEnric Balletbo i Serra // Copyright (C) 2012 Google, Inc. 527755cf7SEnric Balletbo i Serra // Copyright (C) 2015 Collabora Ltd. 618800fc7SEmilio López 718800fc7SEmilio López #include <linux/of.h> 818800fc7SEmilio López #include <linux/platform_device.h> 9acb9900fSEnric Balletbo i Serra #include <linux/module.h> 10840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_commands.h> 11840d9f13SEnric Balletbo i Serra #include <linux/platform_data/cros_ec_proto.h> 1218800fc7SEmilio López #include <linux/slab.h> 1318800fc7SEmilio López 14acb9900fSEnric Balletbo i Serra #define DRV_NAME "cros-ec-vbc" 15acb9900fSEnric Balletbo i Serra 1618800fc7SEmilio López static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj, 1718800fc7SEmilio López struct bin_attribute *att, char *buf, 1818800fc7SEmilio López loff_t pos, size_t count) 1918800fc7SEmilio López { 209aa7bd45SWang Qing struct device *dev = kobj_to_dev(kobj); 2179a3d603SGwendal Grignou struct cros_ec_dev *ec = to_cros_ec_dev(dev); 2218800fc7SEmilio López struct cros_ec_device *ecdev = ec->ec_dev; 2318800fc7SEmilio López struct ec_params_vbnvcontext *params; 2418800fc7SEmilio López struct cros_ec_command *msg; 2518800fc7SEmilio López int err; 2618800fc7SEmilio López const size_t para_sz = sizeof(params->op); 2718800fc7SEmilio López const size_t resp_sz = sizeof(struct ec_response_vbnvcontext); 2818800fc7SEmilio López const size_t payload = max(para_sz, resp_sz); 2918800fc7SEmilio López 3018800fc7SEmilio López msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL); 3118800fc7SEmilio López if (!msg) 3218800fc7SEmilio López return -ENOMEM; 3318800fc7SEmilio López 3418800fc7SEmilio López /* NB: we only kmalloc()ated enough space for the op field */ 3518800fc7SEmilio López params = (struct ec_params_vbnvcontext *)msg->data; 3618800fc7SEmilio López params->op = EC_VBNV_CONTEXT_OP_READ; 3718800fc7SEmilio López 3818800fc7SEmilio López msg->version = EC_VER_VBNV_CONTEXT; 3918800fc7SEmilio López msg->command = EC_CMD_VBNV_CONTEXT; 4018800fc7SEmilio López msg->outsize = para_sz; 4118800fc7SEmilio López msg->insize = resp_sz; 4218800fc7SEmilio López 436bc15ad7SEnric Balletbo i Serra err = cros_ec_cmd_xfer_status(ecdev, msg); 4418800fc7SEmilio López if (err < 0) { 4518800fc7SEmilio López dev_err(dev, "Error sending read request: %d\n", err); 4618800fc7SEmilio López kfree(msg); 4718800fc7SEmilio López return err; 4818800fc7SEmilio López } 4918800fc7SEmilio López 5018800fc7SEmilio López memcpy(buf, msg->data, resp_sz); 5118800fc7SEmilio López 5218800fc7SEmilio López kfree(msg); 5318800fc7SEmilio López return resp_sz; 5418800fc7SEmilio López } 5518800fc7SEmilio López 5618800fc7SEmilio López static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj, 5718800fc7SEmilio López struct bin_attribute *attr, char *buf, 5818800fc7SEmilio López loff_t pos, size_t count) 5918800fc7SEmilio López { 609aa7bd45SWang Qing struct device *dev = kobj_to_dev(kobj); 6179a3d603SGwendal Grignou struct cros_ec_dev *ec = to_cros_ec_dev(dev); 6218800fc7SEmilio López struct cros_ec_device *ecdev = ec->ec_dev; 6318800fc7SEmilio López struct ec_params_vbnvcontext *params; 6418800fc7SEmilio López struct cros_ec_command *msg; 6518800fc7SEmilio López int err; 6618800fc7SEmilio López const size_t para_sz = sizeof(*params); 6718800fc7SEmilio López const size_t data_sz = sizeof(params->block); 6818800fc7SEmilio López 6918800fc7SEmilio López /* Only write full values */ 7018800fc7SEmilio López if (count != data_sz) 7118800fc7SEmilio López return -EINVAL; 7218800fc7SEmilio López 7318800fc7SEmilio López msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL); 7418800fc7SEmilio López if (!msg) 7518800fc7SEmilio López return -ENOMEM; 7618800fc7SEmilio López 7718800fc7SEmilio López params = (struct ec_params_vbnvcontext *)msg->data; 7818800fc7SEmilio López params->op = EC_VBNV_CONTEXT_OP_WRITE; 7918800fc7SEmilio López memcpy(params->block, buf, data_sz); 8018800fc7SEmilio López 8118800fc7SEmilio López msg->version = EC_VER_VBNV_CONTEXT; 8218800fc7SEmilio López msg->command = EC_CMD_VBNV_CONTEXT; 8318800fc7SEmilio López msg->outsize = para_sz; 8418800fc7SEmilio López msg->insize = 0; 8518800fc7SEmilio López 866bc15ad7SEnric Balletbo i Serra err = cros_ec_cmd_xfer_status(ecdev, msg); 8718800fc7SEmilio López if (err < 0) { 8818800fc7SEmilio López dev_err(dev, "Error sending write request: %d\n", err); 8918800fc7SEmilio López kfree(msg); 9018800fc7SEmilio López return err; 9118800fc7SEmilio López } 9218800fc7SEmilio López 9318800fc7SEmilio López kfree(msg); 9418800fc7SEmilio López return data_sz; 9518800fc7SEmilio López } 9618800fc7SEmilio López 9718800fc7SEmilio López static BIN_ATTR_RW(vboot_context, 16); 9818800fc7SEmilio López 9918800fc7SEmilio López static struct bin_attribute *cros_ec_vbc_bin_attrs[] = { 10018800fc7SEmilio López &bin_attr_vboot_context, 10118800fc7SEmilio López NULL 10218800fc7SEmilio López }; 10318800fc7SEmilio López 104d7c1fef7SRikard Falkeborn static const struct attribute_group cros_ec_vbc_attr_group = { 10518800fc7SEmilio López .name = "vbc", 10618800fc7SEmilio López .bin_attrs = cros_ec_vbc_bin_attrs, 10718800fc7SEmilio López }; 108acb9900fSEnric Balletbo i Serra 109acb9900fSEnric Balletbo i Serra static int cros_ec_vbc_probe(struct platform_device *pd) 110acb9900fSEnric Balletbo i Serra { 111acb9900fSEnric Balletbo i Serra struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent); 112acb9900fSEnric Balletbo i Serra struct device *dev = &pd->dev; 113acb9900fSEnric Balletbo i Serra int ret; 114acb9900fSEnric Balletbo i Serra 115acb9900fSEnric Balletbo i Serra ret = sysfs_create_group(&ec_dev->class_dev.kobj, 116acb9900fSEnric Balletbo i Serra &cros_ec_vbc_attr_group); 117acb9900fSEnric Balletbo i Serra if (ret < 0) 118acb9900fSEnric Balletbo i Serra dev_err(dev, "failed to create %s attributes. err=%d\n", 119acb9900fSEnric Balletbo i Serra cros_ec_vbc_attr_group.name, ret); 120acb9900fSEnric Balletbo i Serra 121acb9900fSEnric Balletbo i Serra return ret; 122acb9900fSEnric Balletbo i Serra } 123acb9900fSEnric Balletbo i Serra 124*58b15196SUwe Kleine-König static void cros_ec_vbc_remove(struct platform_device *pd) 125acb9900fSEnric Balletbo i Serra { 126acb9900fSEnric Balletbo i Serra struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent); 127acb9900fSEnric Balletbo i Serra 128acb9900fSEnric Balletbo i Serra sysfs_remove_group(&ec_dev->class_dev.kobj, 129acb9900fSEnric Balletbo i Serra &cros_ec_vbc_attr_group); 130acb9900fSEnric Balletbo i Serra } 131acb9900fSEnric Balletbo i Serra 132acb9900fSEnric Balletbo i Serra static struct platform_driver cros_ec_vbc_driver = { 133acb9900fSEnric Balletbo i Serra .driver = { 134acb9900fSEnric Balletbo i Serra .name = DRV_NAME, 135acb9900fSEnric Balletbo i Serra }, 136acb9900fSEnric Balletbo i Serra .probe = cros_ec_vbc_probe, 137*58b15196SUwe Kleine-König .remove_new = cros_ec_vbc_remove, 138acb9900fSEnric Balletbo i Serra }; 139acb9900fSEnric Balletbo i Serra 140acb9900fSEnric Balletbo i Serra module_platform_driver(cros_ec_vbc_driver); 141acb9900fSEnric Balletbo i Serra 142acb9900fSEnric Balletbo i Serra MODULE_LICENSE("GPL"); 143acb9900fSEnric Balletbo i Serra MODULE_DESCRIPTION("Expose the vboot context nvram to userspace"); 144acb9900fSEnric Balletbo i Serra MODULE_ALIAS("platform:" DRV_NAME); 145