From bc4b786156b3dd51de75f512d05679d5b413399b Mon Sep 17 00:00:00 2001 From: Daisuke Nojiri Date: Fri, 12 Jan 2018 12:52:18 -0800 Subject: CBI: Add host command to set board info This patch adds host command to write board information in EERPOM. BUG=b:70294260 BRANCH=none TEST=Run ectool cbi set to write BOARD_VERSION, OEM_ID, and SKU_ID. Enable WP and verify cbi set command fails. Change-Id: I39536d146313408ace666f350a107d89b331bf7a Signed-off-by: Daisuke Nojiri Reviewed-on: https://chromium-review.googlesource.com/865570 --- common/cbi.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) (limited to 'common/cbi.c') diff --git a/common/cbi.c b/common/cbi.c index b82d0375c2..41e8606487 100644 --- a/common/cbi.c +++ b/common/cbi.c @@ -9,12 +9,14 @@ #include "console.h" #include "crc8.h" #include "cros_board_info.h" +#include "gpio.h" #include "host_command.h" #include "i2c.h" #include "util.h" #define CPRINTS(format, args...) cprints(CC_SYSTEM, "CBI " format, ## args) +#define EEPROM_PAGE_WRITE_SIZE 16 static struct board_info bi; /* TODO: Init it to -1. On error (I2C or bad contents), retry a read and set it * to enum ec_error_list if it still fails. The successive calls can be @@ -95,6 +97,38 @@ static int read_board_info(void) return EC_SUCCESS; } +static int eeprom_is_write_protected(void) +{ + return !gpio_get_level(GPIO_WP_L); +} + +static int write_board_info(void) +{ + uint8_t buf[sizeof(bi) + 1]; + /* The code is only tested for ST M24C02, whose page size for a single + * write is 16 byte. To support different EEPROMs, you may need to + * craft the i2c packets accordingly. */ + _Static_assert(sizeof(bi) <= EEPROM_PAGE_WRITE_SIZE, + "struct board_info exceeds page write size"); + int rv; + + if (eeprom_is_write_protected()) { + CPRINTS("Failed to write for WP"); + return EC_ERROR_ACCESS_DENIED; + } + + buf[0] = 0; /* Offset 0 */ + memcpy(&buf[1], &bi, sizeof(bi)); + rv = i2c_xfer(I2C_PORT_EEPROM, I2C_ADDR_EEPROM, buf, + sizeof(bi) + 1, NULL, 0, I2C_XFER_SINGLE); + if (rv) { + CPRINTS("Failed to write for %d", rv); + return rv; + } + + return EC_SUCCESS; +} + int cbi_get_board_version(uint32_t *version) { if (read_board_info()) @@ -154,3 +188,56 @@ static int hc_cbi_get(struct host_cmd_handler_args *args) DECLARE_HOST_COMMAND(EC_CMD_GET_CROS_BOARD_INFO, hc_cbi_get, EC_VER_MASK(0)); + +static int hc_cbi_set(struct host_cmd_handler_args *args) +{ + const struct __ec_align4 ec_params_set_cbi *p = args->params; + + if (p->flag & CBI_SET_INIT) { + memset(&bi, 0, sizeof(bi)); + memcpy(&bi.head.magic, cbi_magic, sizeof(cbi_magic)); + initialized = 1; + } else { + if (read_board_info()) + return EC_RES_ERROR; + } + + switch (p->type) { + case CBI_DATA_BOARD_VERSION: + if (p->data > UINT16_MAX) + return EC_RES_INVALID_PARAM; + bi.version = p->data; + break; + case CBI_DATA_OEM_ID: + if (p->data > UINT8_MAX) + return EC_RES_INVALID_PARAM; + bi.oem_id = p->data; + break; + case CBI_DATA_SKU_ID: + if (p->data > UINT8_MAX) + return EC_RES_INVALID_PARAM; + bi.sku_id = p->data; + break; + default: + return EC_RES_INVALID_PARAM; + } + + /* Whether we're modifying existing data or creating new one, + * we take over the format. */ + bi.head.major_version = CBI_VERSION_MAJOR; + bi.head.minor_version = CBI_VERSION_MINOR; + bi.head.total_size = sizeof(bi); + bi.head.crc = cbi_crc8(&bi); + + /* Skip write if client asks so. */ + if (p->flag & CBI_SET_NO_SYNC) + return EC_RES_SUCCESS; + + if (write_board_info()) + return EC_RES_ERROR; + + return EC_RES_SUCCESS; +} +DECLARE_HOST_COMMAND(EC_CMD_SET_CROS_BOARD_INFO, + hc_cbi_set, + EC_VER_MASK(0)); -- cgit v1.2.1