Skip to content

Commit

Permalink
mfd: cros_ec: Use a zero-length array for command data
Browse files Browse the repository at this point in the history
Commit 1b84f2a ("mfd: cros_ec: Use fixed size arrays to transfer
data with the EC") modified the struct cros_ec_command fields to not
use pointers for the input and output buffers and use fixed length
arrays instead.

This change was made because the cros_ec ioctl API uses that struct
cros_ec_command to allow user-space to send commands to the EC and
to get data from the EC. So using pointers made the API not 64-bit
safe. Unfortunately this approach was not flexible enough for all
the use-cases since there may be a need to send larger commands
on newer versions of the EC command protocol.

So to avoid to choose a constant length that it may be too big for
most commands and thus wasting memory and CPU cycles on copy from
and to user-space or having a size that is too small for some big
commands, use a zero-length array that is both 64-bit safe and
flexible. The same buffer is used for both output and input data
so the maximum of these values should be used to allocate it.

Suggested-by: Gwendal Grignou <gwendal@chromium.org>
Signed-off-by: Javier Martinez Canillas <javier.martinez@collabora.co.uk>
Tested-by: Heiko Stuebner <heiko@sntech.de>
Acked-by: Lee Jones <lee.jones@linaro.org>
Acked-by: Olof Johansson <olof@lixom.net>
Signed-off-by: Lee Jones <lee.jones@linaro.org>
  • Loading branch information
Javier Martinez Canillas authored and Lee Jones committed Jun 15, 2015
1 parent bb03ffb commit a841178
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 175 deletions.
45 changes: 30 additions & 15 deletions drivers/i2c/busses/i2c-cros-ec-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
const u16 bus_num = bus->remote_bus;
int request_len;
int response_len;
int alloc_size;
int result;
struct cros_ec_command msg = { };
struct cros_ec_command *msg;

request_len = ec_i2c_count_message(i2c_msgs, num);
if (request_len < 0) {
Expand All @@ -198,25 +199,39 @@ static int ec_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg i2c_msgs[],
return response_len;
}

result = ec_i2c_construct_message(msg.outdata, i2c_msgs, num, bus_num);
if (result)
return result;
alloc_size = max(request_len, response_len);
msg = kmalloc(sizeof(*msg) + alloc_size, GFP_KERNEL);
if (!msg)
return -ENOMEM;

msg.version = 0;
msg.command = EC_CMD_I2C_PASSTHRU;
msg.outsize = request_len;
msg.insize = response_len;
result = ec_i2c_construct_message(msg->data, i2c_msgs, num, bus_num);
if (result) {
dev_err(dev, "Error constructing EC i2c message %d\n", result);
goto exit;
}

result = cros_ec_cmd_xfer(bus->ec, &msg);
if (result < 0)
return result;
msg->version = 0;
msg->command = EC_CMD_I2C_PASSTHRU;
msg->outsize = request_len;
msg->insize = response_len;

result = ec_i2c_parse_response(msg.indata, i2c_msgs, &num);
if (result < 0)
return result;
result = cros_ec_cmd_xfer(bus->ec, msg);
if (result < 0) {
dev_err(dev, "Error transferring EC i2c message %d\n", result);
goto exit;
}

result = ec_i2c_parse_response(msg->data, i2c_msgs, &num);
if (result < 0) {
dev_err(dev, "Error parsing EC i2c message %d\n", result);
goto exit;
}

/* Indicate success by saying how many messages were sent */
return num;
result = num;
exit:
kfree(msg);
return result;
}

static u32 ec_i2c_functionality(struct i2c_adapter *adap)
Expand Down
29 changes: 19 additions & 10 deletions drivers/input/keyboard/cros_ec_keyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,28 @@ static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,

static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
{
int ret;
struct cros_ec_command msg = {
.command = EC_CMD_MKBP_STATE,
.insize = ckdev->cols,
};
int ret = 0;
struct cros_ec_command *msg;

ret = cros_ec_cmd_xfer(ckdev->ec, &msg);
if (ret < 0)
return ret;
msg = kmalloc(sizeof(*msg) + ckdev->cols, GFP_KERNEL);
if (!msg)
return -ENOMEM;

memcpy(kb_state, msg.indata, ckdev->cols);
msg->version = 0;
msg->command = EC_CMD_MKBP_STATE;
msg->insize = ckdev->cols;
msg->outsize = 0;

return 0;
ret = cros_ec_cmd_xfer(ckdev->ec, msg);
if (ret < 0) {
dev_err(ckdev->dev, "Error transferring EC message %d\n", ret);
goto exit;
}

memcpy(kb_state, msg->data, ckdev->cols);
exit:
kfree(msg);
return ret;
}

static irqreturn_t cros_ec_keyb_irq(int irq, void *data)
Expand Down
28 changes: 20 additions & 8 deletions drivers/mfd/cros_ec.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
out[2] = msg->outsize;
csum = out[0] + out[1] + out[2];
for (i = 0; i < msg->outsize; i++)
csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->outdata[i];
csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = (uint8_t)(csum & 0xff);

return EC_MSG_TX_PROTO_BYTES + msg->outsize;
Expand Down Expand Up @@ -75,11 +75,20 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
ret = ec_dev->cmd_xfer(ec_dev, msg);
if (msg->result == EC_RES_IN_PROGRESS) {
int i;
struct cros_ec_command status_msg = { };
struct cros_ec_command *status_msg;
struct ec_response_get_comms_status *status;

status_msg.command = EC_CMD_GET_COMMS_STATUS;
status_msg.insize = sizeof(*status);
status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
GFP_KERNEL);
if (!status_msg) {
ret = -ENOMEM;
goto exit;
}

status_msg->version = 0;
status_msg->command = EC_CMD_GET_COMMS_STATUS;
status_msg->insize = sizeof(*status);
status_msg->outsize = 0;

/*
* Query the EC's status until it's no longer busy or
Expand All @@ -88,20 +97,23 @@ int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
for (i = 0; i < EC_COMMAND_RETRIES; i++) {
usleep_range(10000, 11000);

ret = ec_dev->cmd_xfer(ec_dev, &status_msg);
ret = ec_dev->cmd_xfer(ec_dev, status_msg);
if (ret < 0)
break;

msg->result = status_msg.result;
if (status_msg.result != EC_RES_SUCCESS)
msg->result = status_msg->result;
if (status_msg->result != EC_RES_SUCCESS)
break;

status = (struct ec_response_get_comms_status *)
status_msg.indata;
status_msg->data;
if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
break;
}

kfree(status_msg);
}
exit:
mutex_unlock(&ec_dev->lock);

return ret;
Expand Down
4 changes: 2 additions & 2 deletions drivers/mfd/cros_ec_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
/* copy message payload and compute checksum */
sum = out_buf[0] + out_buf[1] + out_buf[2];
for (i = 0; i < msg->outsize; i++) {
out_buf[3 + i] = msg->outdata[i];
out_buf[3 + i] = msg->data[i];
sum += out_buf[3 + i];
}
out_buf[3 + msg->outsize] = sum;
Expand Down Expand Up @@ -109,7 +109,7 @@ static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev,
/* copy response packet payload and compute checksum */
sum = in_buf[0] + in_buf[1];
for (i = 0; i < len; i++) {
msg->indata[i] = in_buf[2 + i];
msg->data[i] = in_buf[2 + i];
sum += in_buf[2 + i];
}
dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n",
Expand Down
2 changes: 1 addition & 1 deletion drivers/mfd/cros_ec_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ static int cros_ec_cmd_xfer_spi(struct cros_ec_device *ec_dev,
for (i = 0; i < len; i++) {
sum += ptr[i + 2];
if (ec_msg->insize)
ec_msg->indata[i] = ptr[i + 2];
ec_msg->data[i] = ptr[i + 2];
}
sum &= 0xff;

Expand Down
65 changes: 42 additions & 23 deletions drivers/platform/chrome/cros_ec_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <linux/fs.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
#include <linux/uaccess.h>

#include "cros_ec_dev.h"
Expand All @@ -36,36 +37,42 @@ static int ec_get_version(struct cros_ec_device *ec, char *str, int maxlen)
static const char * const current_image_name[] = {
"unknown", "read-only", "read-write", "invalid",
};
struct cros_ec_command msg = {
.version = 0,
.command = EC_CMD_GET_VERSION,
.outdata = { 0 },
.outsize = 0,
.indata = { 0 },
.insize = sizeof(*resp),
};
struct cros_ec_command *msg;
int ret;

ret = cros_ec_cmd_xfer(ec, &msg);
msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
if (!msg)
return -ENOMEM;

msg->version = 0;
msg->command = EC_CMD_GET_VERSION;
msg->insize = sizeof(*resp);
msg->outsize = 0;

ret = cros_ec_cmd_xfer(ec, msg);
if (ret < 0)
return ret;
goto exit;

if (msg.result != EC_RES_SUCCESS) {
if (msg->result != EC_RES_SUCCESS) {
snprintf(str, maxlen,
"%s\nUnknown EC version: EC returned %d\n",
CROS_EC_DEV_VERSION, msg.result);
return 0;
CROS_EC_DEV_VERSION, msg->result);
ret = -EINVAL;
goto exit;
}

resp = (struct ec_response_get_version *)msg.indata;
resp = (struct ec_response_get_version *)msg->data;
if (resp->current_image >= ARRAY_SIZE(current_image_name))
resp->current_image = 3; /* invalid */

snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION,
resp->version_string_ro, resp->version_string_rw,
current_image_name[resp->current_image]);

return 0;
ret = 0;
exit:
kfree(msg);
return ret;
}

/* Device file ops */
Expand Down Expand Up @@ -110,20 +117,32 @@ static ssize_t ec_device_read(struct file *filp, char __user *buffer,
static long ec_device_ioctl_xcmd(struct cros_ec_device *ec, void __user *arg)
{
long ret;
struct cros_ec_command s_cmd = { };
struct cros_ec_command u_cmd;
struct cros_ec_command *s_cmd;

if (copy_from_user(&s_cmd, arg, sizeof(s_cmd)))
if (copy_from_user(&u_cmd, arg, sizeof(u_cmd)))
return -EFAULT;

ret = cros_ec_cmd_xfer(ec, &s_cmd);
s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize),
GFP_KERNEL);
if (!s_cmd)
return -ENOMEM;

if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) {
ret = -EFAULT;
goto exit;
}

ret = cros_ec_cmd_xfer(ec, s_cmd);
/* Only copy data to userland if data was received. */
if (ret < 0)
return ret;
goto exit;

if (copy_to_user(arg, &s_cmd, sizeof(s_cmd)))
return -EFAULT;

return 0;
if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + u_cmd.insize))
ret = -EFAULT;
exit:
kfree(s_cmd);
return ret;
}

static long ec_device_ioctl_readmem(struct cros_ec_device *ec, void __user *arg)
Expand Down

0 comments on commit a841178

Please sign in to comment.