From aa636df6ab83d8ca08dc87c4140dceb40dd65900 Mon Sep 17 00:00:00 2001 From: ChromeOS Developer Date: Wed, 15 Jan 2014 22:14:43 -0800 Subject: Check for valid i2c port number on i2c host commands BUG=chrome-os-partner:25052 BRANCH=baytrail TEST=Run ectool i2cread, i2cwrite, and i2cxfer commands with invalid port numbers. Verify machine doesn't reboot. Change-Id: Ifef062cb4a7548278f69689072324704f2f66317 Signed-off-by: Dave Parker Reviewed-on: https://chromium-review.googlesource.com/182911 Reviewed-by: Randall Spangler --- common/i2c.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/common/i2c.c b/common/i2c.c index 564a4e39a7..995d7cb7cb 100644 --- a/common/i2c.c +++ b/common/i2c.c @@ -119,6 +119,16 @@ int i2c_write8(int port, int slave_addr, int offset, int data) * as ectool supports EC_CMD_I2C_PASSTHRU. */ +static int port_is_valid(int port) +{ + int i; + + for (i = 0; i < i2c_ports_used; i++) + if (i2c_ports[i].port == port) + return 1; + return 0; +} + static int i2c_command_read(struct host_cmd_handler_args *args) { const struct ec_params_i2c_read *p = args->params; @@ -130,7 +140,10 @@ static int i2c_command_read(struct host_cmd_handler_args *args) return EC_RES_ACCESS_DENIED; #endif - if (p->read_size == 16) + if (!port_is_valid(p->port)) + return EC_RES_INVALID_PARAM; + + if (p->read_size == 16) rv = i2c_read16(p->port, p->addr, p->offset, &data); else if (p->read_size == 8) rv = i2c_read8(p->port, p->addr, p->offset, &data); @@ -154,6 +167,9 @@ static int i2c_command_write(struct host_cmd_handler_args *args) return EC_RES_ACCESS_DENIED; #endif + if (!port_is_valid(p->port)) + return EC_RES_INVALID_PARAM; + if (p->write_size == 16) rv = i2c_write16(p->port, p->addr, p->offset, p->data); else if (p->write_size == 8) @@ -200,7 +216,7 @@ static int check_i2c_params(const struct host_cmd_handler_args *args) return EC_RES_INVALID_PARAM; } - if (params->port >= I2C_PORT_COUNT) { + if (!port_is_valid(params->port)) { PTHRUPRINTF("[%T i2c passthru invalid port %d]\n", params->port); return EC_RES_INVALID_PARAM; -- cgit v1.2.1