18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// Expose the vboot context nvram to userspace 38c2ecf20Sopenharmony_ci// 48c2ecf20Sopenharmony_ci// Copyright (C) 2012 Google, Inc. 58c2ecf20Sopenharmony_ci// Copyright (C) 2015 Collabora Ltd. 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/of.h> 88c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_commands.h> 118c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h> 128c2ecf20Sopenharmony_ci#include <linux/slab.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define DRV_NAME "cros-ec-vbc" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_cistatic ssize_t vboot_context_read(struct file *filp, struct kobject *kobj, 178c2ecf20Sopenharmony_ci struct bin_attribute *att, char *buf, 188c2ecf20Sopenharmony_ci loff_t pos, size_t count) 198c2ecf20Sopenharmony_ci{ 208c2ecf20Sopenharmony_ci struct device *dev = kobj_to_dev(kobj); 218c2ecf20Sopenharmony_ci struct cros_ec_dev *ec = to_cros_ec_dev(dev); 228c2ecf20Sopenharmony_ci struct cros_ec_device *ecdev = ec->ec_dev; 238c2ecf20Sopenharmony_ci struct ec_params_vbnvcontext *params; 248c2ecf20Sopenharmony_ci struct cros_ec_command *msg; 258c2ecf20Sopenharmony_ci int err; 268c2ecf20Sopenharmony_ci const size_t para_sz = sizeof(params->op); 278c2ecf20Sopenharmony_ci const size_t resp_sz = sizeof(struct ec_response_vbnvcontext); 288c2ecf20Sopenharmony_ci const size_t payload = max(para_sz, resp_sz); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL); 318c2ecf20Sopenharmony_ci if (!msg) 328c2ecf20Sopenharmony_ci return -ENOMEM; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci /* NB: we only kmalloc()ated enough space for the op field */ 358c2ecf20Sopenharmony_ci params = (struct ec_params_vbnvcontext *)msg->data; 368c2ecf20Sopenharmony_ci params->op = EC_VBNV_CONTEXT_OP_READ; 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci msg->version = EC_VER_VBNV_CONTEXT; 398c2ecf20Sopenharmony_ci msg->command = EC_CMD_VBNV_CONTEXT; 408c2ecf20Sopenharmony_ci msg->outsize = para_sz; 418c2ecf20Sopenharmony_ci msg->insize = resp_sz; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci err = cros_ec_cmd_xfer_status(ecdev, msg); 448c2ecf20Sopenharmony_ci if (err < 0) { 458c2ecf20Sopenharmony_ci dev_err(dev, "Error sending read request: %d\n", err); 468c2ecf20Sopenharmony_ci kfree(msg); 478c2ecf20Sopenharmony_ci return err; 488c2ecf20Sopenharmony_ci } 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci memcpy(buf, msg->data, resp_sz); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci kfree(msg); 538c2ecf20Sopenharmony_ci return resp_sz; 548c2ecf20Sopenharmony_ci} 558c2ecf20Sopenharmony_ci 568c2ecf20Sopenharmony_cistatic ssize_t vboot_context_write(struct file *filp, struct kobject *kobj, 578c2ecf20Sopenharmony_ci struct bin_attribute *attr, char *buf, 588c2ecf20Sopenharmony_ci loff_t pos, size_t count) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci struct device *dev = kobj_to_dev(kobj); 618c2ecf20Sopenharmony_ci struct cros_ec_dev *ec = to_cros_ec_dev(dev); 628c2ecf20Sopenharmony_ci struct cros_ec_device *ecdev = ec->ec_dev; 638c2ecf20Sopenharmony_ci struct ec_params_vbnvcontext *params; 648c2ecf20Sopenharmony_ci struct cros_ec_command *msg; 658c2ecf20Sopenharmony_ci int err; 668c2ecf20Sopenharmony_ci const size_t para_sz = sizeof(*params); 678c2ecf20Sopenharmony_ci const size_t data_sz = sizeof(params->block); 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci /* Only write full values */ 708c2ecf20Sopenharmony_ci if (count != data_sz) 718c2ecf20Sopenharmony_ci return -EINVAL; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL); 748c2ecf20Sopenharmony_ci if (!msg) 758c2ecf20Sopenharmony_ci return -ENOMEM; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci params = (struct ec_params_vbnvcontext *)msg->data; 788c2ecf20Sopenharmony_ci params->op = EC_VBNV_CONTEXT_OP_WRITE; 798c2ecf20Sopenharmony_ci memcpy(params->block, buf, data_sz); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci msg->version = EC_VER_VBNV_CONTEXT; 828c2ecf20Sopenharmony_ci msg->command = EC_CMD_VBNV_CONTEXT; 838c2ecf20Sopenharmony_ci msg->outsize = para_sz; 848c2ecf20Sopenharmony_ci msg->insize = 0; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci err = cros_ec_cmd_xfer_status(ecdev, msg); 878c2ecf20Sopenharmony_ci if (err < 0) { 888c2ecf20Sopenharmony_ci dev_err(dev, "Error sending write request: %d\n", err); 898c2ecf20Sopenharmony_ci kfree(msg); 908c2ecf20Sopenharmony_ci return err; 918c2ecf20Sopenharmony_ci } 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci kfree(msg); 948c2ecf20Sopenharmony_ci return data_sz; 958c2ecf20Sopenharmony_ci} 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_cistatic BIN_ATTR_RW(vboot_context, 16); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_cistatic struct bin_attribute *cros_ec_vbc_bin_attrs[] = { 1008c2ecf20Sopenharmony_ci &bin_attr_vboot_context, 1018c2ecf20Sopenharmony_ci NULL 1028c2ecf20Sopenharmony_ci}; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_cistatic struct attribute_group cros_ec_vbc_attr_group = { 1058c2ecf20Sopenharmony_ci .name = "vbc", 1068c2ecf20Sopenharmony_ci .bin_attrs = cros_ec_vbc_bin_attrs, 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic int cros_ec_vbc_probe(struct platform_device *pd) 1108c2ecf20Sopenharmony_ci{ 1118c2ecf20Sopenharmony_ci struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent); 1128c2ecf20Sopenharmony_ci struct device *dev = &pd->dev; 1138c2ecf20Sopenharmony_ci int ret; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci ret = sysfs_create_group(&ec_dev->class_dev.kobj, 1168c2ecf20Sopenharmony_ci &cros_ec_vbc_attr_group); 1178c2ecf20Sopenharmony_ci if (ret < 0) 1188c2ecf20Sopenharmony_ci dev_err(dev, "failed to create %s attributes. err=%d\n", 1198c2ecf20Sopenharmony_ci cros_ec_vbc_attr_group.name, ret); 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci return ret; 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cistatic int cros_ec_vbc_remove(struct platform_device *pd) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct cros_ec_dev *ec_dev = dev_get_drvdata(pd->dev.parent); 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci sysfs_remove_group(&ec_dev->class_dev.kobj, 1298c2ecf20Sopenharmony_ci &cros_ec_vbc_attr_group); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci return 0; 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic struct platform_driver cros_ec_vbc_driver = { 1358c2ecf20Sopenharmony_ci .driver = { 1368c2ecf20Sopenharmony_ci .name = DRV_NAME, 1378c2ecf20Sopenharmony_ci }, 1388c2ecf20Sopenharmony_ci .probe = cros_ec_vbc_probe, 1398c2ecf20Sopenharmony_ci .remove = cros_ec_vbc_remove, 1408c2ecf20Sopenharmony_ci}; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cimodule_platform_driver(cros_ec_vbc_driver); 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1458c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Expose the vboot context nvram to userspace"); 1468c2ecf20Sopenharmony_ciMODULE_ALIAS("platform:" DRV_NAME); 147