cros_ec: Add features required for verified boot
Subsystems (2)
cover cros_ec: Add features required for verified boot
The Chromium OS EC has developed a few more features. This series adds support for some of these, to allow a more recent verified-boot implementation to work.
Also some existing features don't have commands available. It is useful to be able to check the EC state from the command line, so this series adds commands for listing features, checking switches, etc.
The following parts of the implementation are added or expanded:
- SKU ID for finding out basic factory information
- 'features' for seeing what the EC supports
- 'switches' for LID open, etc.
- events for seeing when the EC has an event for the AP
- vstore (for storing data in the EC)
The current comment style is used for new functions.
Simon Glass (10):
cros_ec: Add a function for the hello message
cros_ec: Tidy up a few delays
cros_ec: Add run-time check for input buffer overflow
cros_ec: Add support for reading the SKU ID
cros_ec: Support reading EC features
cros_ec: Add documentation for cros_ec driver operations
cros_ec: Add support for switches
cros_ec: Show events in human-readable form
cros_ec: Allow use with of-platdata
cros_ec: Add vstore support
arch/sandbox/include/asm/test.h | 15 +++ cmd/cros_ec.c | 195 ++++++++++++++++++++++++++++++- drivers/misc/cros_ec.c | 200 +++++++++++++++++++++++++++----- drivers/misc/cros_ec_lpc.c | 7 ++ drivers/misc/cros_ec_sandbox.c | 100 +++++++++++++++- include/cros_ec.h | 142 +++++++++++++++++++++++ include/ec_commands.h | 45 ++++++- test/dm/Makefile | 1 + test/dm/cros_ec.c | 178 ++++++++++++++++++++++++++++ 9 files changed, 844 insertions(+), 39 deletions(-) create mode 100644 test/dm/cros_ec.c
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 1/10 cros_ec: Add a function for the hello message
This is used several times in this file. Put it in a function to avoid code duplication.
Also add a test for this function. There are no cros_ec tests at present, so it is time to update the code.
---
arch/sandbox/include/asm/test.h | 14 ++++++++++ drivers/misc/cros_ec.c | 46 ++++++++++++++++++++++----------- drivers/misc/cros_ec_sandbox.c | 12 +++++++++ include/cros_ec.h | 11 ++++++++ test/dm/Makefile | 1 + test/dm/cros_ec.c | 32 +++++++++++++++++++++++ 6 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 test/dm/cros_ec.c
arch/sandbox/include/asm/test.h +14 -0
@@ -57,6 +57,12 @@ enum { SYSCON_COUNT }; +/** +*/ +enum cros_ec_test_t { +CROSECT_BREAK_HELLO =BIT (1 ), +}; +/** * sandbox_i2c_set_test_mode() - set test mode for running unit tests *
@@ -260,4 +266,12 @@ uint sandbox_pci_read_bar(u32 barval, int type, uint size);*/ void sandbox_set_enable_memio (bool enable); +/** +* sandbox_cros_ec_set_test_flags() - Set behaviour for testing purposes +* +* @dev: Device to check +* @flags: Flags to control behaviour (CROSECT_...) +*/ +void sandbox_cros_ec_set_test_flags (struct udevice *dev,uint flags); +#endif
drivers/misc/cros_ec.c +31 -15
76 lines not on this page · raw message
drivers/misc/cros_ec_sandbox.c +12 -0
@@ -18,6 +18,7 @@#include <asm/malloc.h> #include <asm/state.h> #include <asm/sdl.h> +#include <asm/test.h> #include <linux/input.h> /*
@@ -73,6 +74,7 @@ struct ec_keymatrix_entry {* @matrix: Information about keyboard matrix * @keyscan: Current keyscan information (bit set for each row/column pressed) * @recovery_req: Keyboard recovery requested +* @test_flags: Flags that control behaviour for tests */ struct ec_state {u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2 ];
@@ -84,6 +86,7 @@ struct ec_state {struct ec_keymatrix_entry *matrix;/* the key matrix info */ uint8_t keyscan[KEYBOARD_COLS ];bool recovery_req; +uint test_flags; } s_state, *g_state;/**
@@ -295,6 +298,8 @@ static int process_cmd(struct ec_state *ec,struct ec_response_hello *resp = resp_data; resp->out_data = req->in_data +0x01020304 ; +if (ec->test_flags &CROSECT_BREAK_HELLO ) + resp->out_data++; len =sizeof (*resp);break ; }
@@ -518,6 +523,13 @@ void cros_ec_check_keyboard(struct udevice *dev) } } +void sandbox_cros_ec_set_test_flags (struct udevice *dev,uint flags) +{ +struct ec_state *ec =dev_get_priv (dev); + + ec->test_flags = flags; +} +int cros_ec_probe (struct udevice *dev) {struct ec_state *ec =dev_get_priv (dev);
include/cros_ec.h +11 -0
@@ -497,4 +497,15 @@ int cros_ec_get_lid_shutdown_mask(struct udevice *dev);*/ int cros_ec_set_lid_shutdown_mask (struct udevice *dev,int enable); +/** +* cros_ec_hello() - Send a hello message +* +* Sends a message with a fixed input value and checks that the expected output +* value is received +* +* @dev: CROS-EC device +* @handshakep: If non-NULL, returns received handshake value on error +* @return 0 if OK, -ve on error +*/ +int cros_ec_hello (struct udevice *dev,uint *handshakep);#endif
test/dm/Makefile +1 -0
@@ -24,6 +24,7 @@ obj-$(CONFIG_BLK) += blk.o obj-$(CONFIG_BUTTON) += button.o obj-$(CONFIG_DM_BOOTCOUNT) += bootcount.o obj-$(CONFIG_CLK) += clk.o clk_ccf.o +obj-$(CONFIG_CROS_EC) += cros_ec.o obj-$(CONFIG_DEVRES) += devres.o obj-$(CONFIG_VIDEO_MIPI_DSI) += dsi_host.o obj-$(CONFIG_DM_ETH) += eth.o
test/dm/cros_ec.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright 2021 Google LLC +*/ + +#include <common.h> +#include <cros_ec.h> +#include <dm.h> +#include <asm/test.h> +#include <dm/test.h> +#include <test/ut.h> + +static int dm_test_cros_ec_hello (struct unit_test_state *uts) +{ +struct udevice *dev; +uint val; + +ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev)); + +ut_assertok (cros_ec_hello (dev,NULL )); + + val =0xdead1357 ; +ut_assertok (cros_ec_hello (dev, &val)); +ut_asserteq (0xdead1357 , val); + +sandbox_cros_ec_set_test_flags (dev,CROSECT_BREAK_HELLO ); +ut_asserteq (-ENOTSYNC ,cros_ec_hello (dev, &val)); +ut_asserteq (0x12345678 , val); + +return 0 ; +} +DM_TEST (dm_test_cros_ec_hello,UT_TESTF_SCAN_FDT );
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 1/10 cros_ec: Add a function for the hello message
This is used several times in this file. Put it in a function to avoid code duplication.
Also add a test for this function. There are no cros_ec tests at present, so it is time to update the code.
---
arch/sandbox/include/asm/test.h | 14 ++++++++++ drivers/misc/cros_ec.c | 46 ++++++++++++++++++++++----------- drivers/misc/cros_ec_sandbox.c | 12 +++++++++ include/cros_ec.h | 11 ++++++++ test/dm/Makefile | 1 + test/dm/cros_ec.c | 32 +++++++++++++++++++++++ 6 files changed, 101 insertions(+), 15 deletions(-) create mode 100644 test/dm/cros_ec.c
Applied to u-boot-dm, thanks!
PATCH 2/10 cros_ec: Tidy up a few delays
In reply to Simon GlassAllow a longer time for the EC to reboot. Also use a constant for the hash delay time, so it is clear what it is for.
---
drivers/misc/cros_ec.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
drivers/misc/cros_ec.c +17 -7
@@ -44,6 +44,10 @@ enum {CROS_EC_CMD_TIMEOUT_MS =5000 ,/* Timeout waiting for a synchronous hash to be recomputed */ CROS_EC_CMD_HASH_TIMEOUT_MS =2000 , + +/* Wait 10 ms between attempts to check if EC's hash is ready */ + CROS_EC_HASH_CHECK_DELAY_MS = 10, + };#define INVALID_HCMD 0xFF
@@ -502,9 +506,10 @@ static int cros_ec_wait_on_hash_done(struct udevice *dev, start =get_timer (0 );while (hash->status ==EC_VBOOT_HASH_STATUS_BUSY ) { -mdelay (50 );/* Insert some reasonable delay */ +mdelay (CROS_EC_HASH_CHECK_DELAY_MS ); p->cmd =EC_VBOOT_HASH_GET ; +if (ec_command (dev,EC_CMD_VBOOT_HASH ,0 , p,sizeof (*p), hash,sizeof (*hash)) <0 )return -1 ;
@@ -622,18 +627,23 @@ int cros_ec_reboot(struct udevice *dev, enum ec_reboot_cmd cmd, uint8_t flags)return -1 ;if (!(flags &EC_REBOOT_FLAG_ON_AP_SHUTDOWN )) { +ulong start; +/* * EC reboot will take place immediately so delay to allow it * to complete. Note that some reboot types (EC_REBOOT_COLD) * will reboot the AP as well, in which case we won't actually * get to this point. */ -/* -* TODO([email protected]): Would be nice if we had a -* better way to determine when the reboot is complete. Could -* we poll a memory-mapped LPC value? -*/ -udelay (50000 ); +mdelay (50 ); + start =get_timer (0 ); +while (cros_ec_hello (dev,NULL )) { +if (get_timer (start) >3000 ) { +log_err ("EC did not return from reboot\n" ); +return -ETIMEDOUT ; + } +mdelay (5 ); + } }return 0 ;
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 2/10 cros_ec: Tidy up a few delays
Allow a longer time for the EC to reboot. Also use a constant for the hash delay time, so it is clear what it is for.
---
drivers/misc/cros_ec.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 3/10 cros_ec: Add run-time check for input buffer overflow
In reply to Simon GlassThis should not happen in normal operation, but the EC might have a bug, so add a run-time check just in case.
---
drivers/misc/cros_ec.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/misc/cros_ec.c +2 -0
@@ -404,6 +404,8 @@ static int ec_command(struct udevice *dev, uint cmd, int cmd_version,*/ if (din && in_buffer) {assert (len <= din_len); +if (len > din_len) +return -ENOSPC ;memmove (din, in_buffer, len); } }
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 3/10 cros_ec: Add run-time check for input buffer overflow
This should not happen in normal operation, but the EC might have a bug, so add a run-time check just in case.
---
drivers/misc/cros_ec.c | 2 ++ 1 file changed, 2 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 4/10 cros_ec: Add support for reading the SKU ID
In reply to Simon GlassThis allows reading strapping pins attached to the EC. Add an implementation for this.
---
cmd/cros_ec.c | 10 ++++++++++ drivers/misc/cros_ec.c | 13 +++++++++++++ drivers/misc/cros_ec_sandbox.c | 7 +++++++ include/cros_ec.h | 8 ++++++++ test/dm/cros_ec.c | 17 +++++++++++++++++ 5 files changed, 55 insertions(+)
cmd/cros_ec.c +10 -0
@@ -352,6 +352,15 @@ static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,debug ("%s: Could not access LDO%d\n" , __func__, index);return ret; } + }else if (!strcmp ("sku" , cmd)) { + ret =cros_ec_get_sku_id (dev); + +if (ret >=0 ) { +printf ("%d\n" , ret); + ret =0 ; + }else { +printf ("Error: %d\n" , ret); + } }else {return CMD_RET_USAGE ; }
@@ -382,6 +391,7 @@ U_BOOT_CMD("crosec write <ro|rw> <addr> [<size>] Write EC image\n" "crosec vbnvcontext [hexstring] Read [write] VbNvContext from EC\n" "crosec ldo <idx> [<state>] Switch/Read LDO state\n" +"crosec sku Read board SKU ID\n" "crosec test run tests on cros_ec\n" "crosec version Read CROS-EC version" );
drivers/misc/cros_ec.c +13 -0
@@ -1105,6 +1105,19 @@ int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,return 0 ; } +int cros_ec_get_sku_id (struct udevice *dev) +{ +struct ec_sku_id_info *r; +int ret; + + ret =ec_command_inptr (dev,EC_CMD_GET_SKU_ID ,0 ,NULL ,0 , + (uint8_t **)&r,sizeof (*r)); +if (ret !=sizeof (*r)) +return -ret; + +return r->sku_id; +} +int cros_ec_read_nvdata (struct udevice *dev,uint8_t *block,int size) {struct ec_params_vbnvcontext p;
drivers/misc/cros_ec_sandbox.c +7 -0
@@ -473,6 +473,13 @@ static int process_cmd(struct ec_state *ec, len =sizeof (*resp);break ; } +case EC_CMD_GET_SKU_ID : { +struct ec_sku_id_info *resp = resp_data; + + resp->sku_id =1234 ; + len =sizeof (*resp); +break ; + }default :printf (" ** Unknown EC command %#02x\n" , req_hdr->command);return -1 ;
include/cros_ec.h +8 -0
@@ -329,6 +329,14 @@ int cros_ec_flash_write(struct udevice *dev, const uint8_t *data,int cros_ec_flash_offset (struct udevice *dev,enum ec_flash_region region,uint32_t *offset,uint32_t *size); +/** +* cros_ec_get_sku_id() - Read the SKU ID +* +* @dev: CROS-EC device +* return SKU ID, or -ve on error +*/ +int cros_ec_get_sku_id (struct udevice *dev); +/** * Read/write non-volatile data from/to a CROS-EC device. *
test/dm/cros_ec.c +17 -0
@@ -30,3 +30,20 @@ static int dm_test_cros_ec_hello(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cros_ec_hello,UT_TESTF_SCAN_FDT ); + +static int dm_test_cros_ec_sku_id (struct unit_test_state *uts) +{ +struct udevice *dev; + +ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev)); +ut_asserteq (1234 ,cros_ec_get_sku_id (dev)); + +/* try the command */ +console_record_reset (); +ut_assertok (run_command ("crosec sku" ,0 )); +ut_assert_nextline ("1234" ); +ut_assert_console_end (); + +return 0 ; +} +DM_TEST (dm_test_cros_ec_sku_id,UT_TESTF_SCAN_FDT );
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 4/10 cros_ec: Add support for reading the SKU ID
This allows reading strapping pins attached to the EC. Add an implementation for this.
---
cmd/cros_ec.c | 10 ++++++++++ drivers/misc/cros_ec.c | 13 +++++++++++++ drivers/misc/cros_ec_sandbox.c | 7 +++++++ include/cros_ec.h | 8 ++++++++ test/dm/cros_ec.c | 17 +++++++++++++++++ 5 files changed, 55 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 5/10 cros_ec: Support reading EC features
In reply to Simon GlassThe EC can support a variety of features and provides a way to find out what is available. Add support for this.
Also update the feature list to the lastest available while we are here. This is at:
https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/master/include/ec_commands.h
---
cmd/cros_ec.c | 74 ++++++++++++++++++++++++++++++++++ drivers/misc/cros_ec.c | 26 +++++++++--- drivers/misc/cros_ec_sandbox.c | 11 +++++ include/cros_ec.h | 21 ++++++++++ include/ec_commands.h | 45 +++++++++++++++++++-- test/dm/cros_ec.c | 28 +++++++++++++ 6 files changed, 195 insertions(+), 10 deletions(-)
cmd/cros_ec.c +74 -0
@@ -94,6 +94,74 @@ static int do_read_write(struct udevice *dev, int is_write, int argc,return 0 ; } +static const char *const feat_name[64 ] = { +"limited" , +"flash" , +"pwm_fan" , +"pwm_keyb" , +"lightbar" , +"led" , +"motion_sense" , +"keyb" , +"pstore" , +"port80" , +"thermal" , +"bklight_switch" , +"wifi_switch" , +"host_events" , +"gpio" , +"i2c" , +"charger" , +"battery" , +"smart_battery" , +"hang_detect" , +"pmu" , +"sub_mcu" , +"usb_pd" , +"usb_mux" , +"motion_sense_fifo" , +"vstore" , +"usbc_ss_mux_virtual" , +"rtc" , +"fingerprint" , +"touchpad" , +"rwsig" , +"device_event" , +"unified_wake_masks" , +"host_event64" , +"exec_in_ram" , +"cec" , +"motion_sense_tight_timestamps" , +"refined_tablet_mode_hysteresis" , +"efs2" , +"scp" , +"ish" , +"typec_cmd" , +"typec_require_ap_mode_entry" , +"typec_mux_require_ap_ack" , +}; + +static int do_show_features (struct udevice *dev) +{ +u64 feat; +int ret; +uint i; + + ret =cros_ec_get_features (dev, &feat); +if (ret) +return ret; +for (i =0 ; i <ARRAY_SIZE (feat_name); i++) { +if (feat & (1ULL << i)) { +if (feat_name[i]) +printf ("%s\n" , feat_name[i]); +else +printf ("unknown %d\n" , i); + } + } + +return 0 ; +} +static int do_cros_ec (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]) {
@@ -140,6 +208,11 @@ static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc, }printf ("rows = %u\n" , info.rows);printf ("cols = %u\n" , info.cols); + }else if (!strcmp ("features" , cmd)) { + ret =do_show_features (dev); + +if (ret) +printf ("Error: %d\n" , ret); }else if (0 ==strcmp ("curimage" , cmd)) {enum ec_current_image image;
@@ -379,6 +452,7 @@ U_BOOT_CMD("init Re-init CROS-EC (done on startup automatically)\n" "crosec id Read CROS-EC ID\n" "crosec info Read CROS-EC info\n" +"crosec features Read CROS-EC features\n" "crosec curimage Read CROS-EC current image\n" "crosec hash Read CROS-EC hash\n" "crosec reboot [rw | ro | cold] Reboot CROS-EC\n"
drivers/misc/cros_ec.c +20 -6
@@ -1344,19 +1344,33 @@ int cros_ec_i2c_tunnel(struct udevice *dev, int port, struct i2c_msg *in,return 0 ; } -int cros_ec_check_feature (struct udevice *dev,int feature) +int cros_ec_get_features (struct udevice *dev,u64 *featuresp) {struct ec_response_get_features r;int rv; - rv =ec_command (dev,EC_CMD_GET_FEATURES ,0 , &r,sizeof (r),NULL ,0 ); -if (rv) -return rv; + rv =ec_command (dev,EC_CMD_GET_FEATURES ,0 ,NULL ,0 , &r,sizeof (r)); +if (rv !=sizeof (r)) +return -EIO ; + *featuresp = r.flags[0 ] | (u64 )r.flags[1 ] <<32 ; + +return 0 ; +} + +int cros_ec_check_feature (struct udevice *dev,uint feature) +{ +struct ec_response_get_features r; +int rv; + + rv =ec_command (dev,EC_CMD_GET_FEATURES ,0 ,NULL ,0 , &r,sizeof (r)); +if (rv !=sizeof (r)) +return -EIO ;if (feature >=8 *sizeof (r.flags)) -return -1 ; +return -EINVAL ; -return r.flags[feature /32 ] &EC_FEATURE_MASK_0 (feature); +return r.flags[feature /32 ] &EC_FEATURE_MASK_0 (feature) ? true : + false; }/*
drivers/misc/cros_ec_sandbox.c +11 -0
@@ -480,6 +480,17 @@ static int process_cmd(struct ec_state *ec, len =sizeof (*resp);break ; } +case EC_CMD_GET_FEATURES : { +struct ec_response_get_features *resp = resp_data; + + resp->flags[0 ] =EC_FEATURE_MASK_0 (EC_FEATURE_FLASH ) | +EC_FEATURE_MASK_0 (EC_FEATURE_I2C ); + resp->flags[1 ] = +EC_FEATURE_MASK_1 (EC_FEATURE_UNIFIED_WAKE_MASKS ) | +EC_FEATURE_MASK_1 (EC_FEATURE_ISH ); + len =sizeof (*resp); +break ; + }default :printf (" ** Unknown EC command %#02x\n" , req_hdr->command);return -1 ;
include/cros_ec.h +21 -0
@@ -516,4 +516,25 @@ int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable);* @return 0 if OK, -ve on error */ int cros_ec_hello (struct udevice *dev,uint *handshakep); + +/** +* cros_ec_get_features() - Get the set of features provided by the EC +* +* See enum ec_feature_code for the list of available features +* +* @dev: CROS-EC device +* @featuresp: Returns a bitmask of supported features +* @return 0 if OK, -ve on error +*/ +int cros_ec_get_features (struct udevice *dev,u64 *featuresp); + +/** +* cros_ec_check_feature() - Check if a feature is supported +* +* @dev: CROS-EC device +* @feature: Feature number to check (enum ec_feature_code) +* @return true if supported, false if not, -ve on error +*/ +int cros_ec_check_feature (struct udevice *dev,uint feature); +#endif
include/ec_commands.h +41 -4
54 lines not on this page · raw message
test/dm/cros_ec.c +28 -0
@@ -47,3 +47,31 @@ static int dm_test_cros_ec_sku_id(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cros_ec_sku_id,UT_TESTF_SCAN_FDT ); + +static int dm_test_cros_ec_features (struct unit_test_state *uts) +{ +struct udevice *dev; +u64 feat; + +ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev)); +ut_assertok (cros_ec_get_features (dev, &feat)); +ut_asserteq_64 (1U <<EC_FEATURE_FLASH |1U <<EC_FEATURE_I2C | +1ULL <<EC_FEATURE_UNIFIED_WAKE_MASKS |1ULL <<EC_FEATURE_ISH , + feat); + +ut_asserteq (true,cros_ec_check_feature (dev,EC_FEATURE_I2C )); +ut_asserteq (false,cros_ec_check_feature (dev,EC_FEATURE_MOTION_SENSE )); +ut_asserteq (true,cros_ec_check_feature (dev,EC_FEATURE_ISH )); + +/* try the command */ +console_record_reset (); +ut_assertok (run_command ("crosec features" ,0 )); +ut_assert_nextline ("flash" ); +ut_assert_nextline ("i2c" ); +ut_assert_nextline ("unified_wake_masks" ); +ut_assert_nextline ("ish" ); +ut_assert_console_end (); + +return 0 ; +} +DM_TEST (dm_test_cros_ec_features,UT_TESTF_SCAN_FDT );
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 5/10 cros_ec: Support reading EC features
The EC can support a variety of features and provides a way to find out what is available. Add support for this.
Also update the feature list to the lastest available while we are here. This is at:
https://chromium.googlesource.com/chromiumos/platform/ec/+/refs/heads/master/include/ec_commands.h
---
cmd/cros_ec.c | 74 ++++++++++++++++++++++++++++++++++ drivers/misc/cros_ec.c | 26 +++++++++--- drivers/misc/cros_ec_sandbox.c | 11 +++++ include/cros_ec.h | 21 ++++++++++ include/ec_commands.h | 45 +++++++++++++++++++-- test/dm/cros_ec.c | 28 +++++++++++++ 6 files changed, 195 insertions(+), 10 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 6/10 cros_ec: Add documentation for cros_ec driver operations
In reply to Simon GlassAdd comments to these methods so it is documented in this central place, not just in each driver.
---
include/cros_ec.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
include/cros_ec.h +40 -0
@@ -234,10 +234,50 @@ int cros_ec_flash_update_rw(struct udevice *dev, const uint8_t *image,struct udevice *board_get_cros_ec_dev (void );struct dm_cros_ec_ops { +/** +* check_version() - Check the protocol version being used (optional) +* +* If provided, this function should check that the EC can be supported +* by the driver. If not provided, HELLO messages will be sent to try +* to determine the protocol version. +* +* @dev: Device to check +* @return 0 if the protocol is valid, -ve if not supported +*/ int (*check_version)(struct udevice *dev); + +/** +* command() - Old-style command interface +* +* This sends a command and receives a response (deprecated, use +* packet()) +* +* @dev: Device to use +* @cmd: Command to send (only supports 0-0xff) +* @cmd_version: Version of command to send (often 0) +* @dout: Output data (may be NULL If dout_len=0) +* @dout_len: Length of output data excluding 4-byte header +* @dinp: On input, set to point to input data, often struct +* cros_ec_dev->din - typically this is left alone but may be +* updated by the driver +* @din_len: Maximum length of response +* @return number of bytes in response, or -ve on error +*/ int (*command)(struct udevice *dev,uint8_t cmd,int cmd_version,const uint8_t *dout,int dout_len,uint8_t **dinp,int din_len); + +/** +* packet() - New-style command interface +* +* This interface is preferred over command(), since it is typically +* easier to implement. +* +* @dev: Device to use +* @out_bytes: Number of bytes to send (from struct cros_ec_dev->dout) +* @in_bytes: Maximum number of bytes to expect in response +* @return number of bytes in response, or -ve on error +*/ int (*packet)(struct udevice *dev,int out_bytes,int in_bytes); };
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 6/10 cros_ec: Add documentation for cros_ec driver operations
Add comments to these methods so it is documented in this central place, not just in each driver.
---
include/cros_ec.h | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 7/10 cros_ec: Add support for switches
In reply to Simon GlassOn x86 platforms the EC provides a way to read 'switches', which are on/off values determined by the EC.
Add a new driver method for this and implement it for LPC.
---
arch/sandbox/include/asm/test.h | 1 + cmd/cros_ec.c | 41 +++++++++++++++++++++++++++++++++ drivers/misc/cros_ec.c | 16 +++++++++++++ drivers/misc/cros_ec_lpc.c | 7 ++++++ drivers/misc/cros_ec_sandbox.c | 10 ++++++++ include/cros_ec.h | 19 +++++++++++++++ test/dm/cros_ec.c | 26 +++++++++++++++++++++ 7 files changed, 120 insertions(+)
arch/sandbox/include/asm/test.h +1 -0
@@ -61,6 +61,7 @@ enum {*/ enum cros_ec_test_t {CROSECT_BREAK_HELLO =BIT (1 ), +CROSECT_LID_OPEN =BIT (2 ), };/**
cmd/cros_ec.c +41 -0
59 lines not on this page · raw message
drivers/misc/cros_ec.c +16 -0
@@ -1557,6 +1557,22 @@ int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)return 0 ; } +int cros_ec_get_switches (struct udevice *dev) +{ +struct dm_cros_ec_ops *ops; +int ret; + + ops =dm_cros_ec_get_ops (dev); +if (!ops->get_switches) +return -ENOSYS ; + + ret = ops->get_switches (dev); +if (ret <0 ) +return log_msg_ret ("get" , ret); + +return ret; +} +UCLASS_DRIVER (cros_ec) = { .id =UCLASS_CROS_EC , .name ="cros-ec" ,
drivers/misc/cros_ec_lpc.c +7 -0
@@ -207,6 +207,12 @@ int cros_ec_lpc_init(struct cros_ec_dev *dev, const void *blob)return 0 ; } +/* Return the byte of EC switch states */ +static int cros_ec_lpc_get_switches (struct udevice *dev) +{ +return inb (EC_LPC_ADDR_MEMMAP +EC_MEMMAP_SWITCHES ); +} +/* * Test if LPC command args are supported. *
@@ -239,6 +245,7 @@ static struct dm_cros_ec_ops cros_ec_ops = { .packet = cros_ec_lpc_packet, .command = cros_ec_lpc_command, .check_version = cros_ec_lpc_check_version, + .get_switches = cros_ec_lpc_get_switches, };static const struct udevice_id cros_ec_ids[] = {
drivers/misc/cros_ec_sandbox.c +10 -0
@@ -75,6 +75,7 @@ struct ec_keymatrix_entry {* @keyscan: Current keyscan information (bit set for each row/column pressed) * @recovery_req: Keyboard recovery requested * @test_flags: Flags that control behaviour for tests +* @switches: Current switches value (EC_SWITCH_) */ struct ec_state {u8 vbnv_context[EC_VBNV_BLOCK_SIZE_V2 ];
@@ -541,6 +542,14 @@ void cros_ec_check_keyboard(struct udevice *dev) } } +/* Return the byte of EC switch states */ +static int cros_ec_sandbox_get_switches (struct udevice *dev) +{ +struct ec_state *ec =dev_get_priv (dev); + +return ec->test_flags &CROSECT_LID_OPEN ?EC_SWITCH_LID_OPEN :0 ; +} +void sandbox_cros_ec_set_test_flags (struct udevice *dev,uint flags) {struct ec_state *ec =dev_get_priv (dev);
@@ -603,6 +612,7 @@ int cros_ec_probe(struct udevice *dev)struct dm_cros_ec_ops cros_ec_ops = { .packet = cros_ec_sandbox_packet, + .get_switches = cros_ec_sandbox_get_switches, };static const struct udevice_id cros_ec_ids[] = {
include/cros_ec.h +19 -0
@@ -279,6 +279,16 @@ struct dm_cros_ec_ops {* @return number of bytes in response, or -ve on error */ int (*packet)(struct udevice *dev,int out_bytes,int in_bytes); + +/** +* get_switches() - Get value of EC switches +* +* This is currently supported on the LPC EC. +* +* @dev: Device to use +* @return current switches value, or -ENOSYS if not supported +*/ +int (*get_switches)(struct udevice *dev); };#define dm_cros_ec_get_ops (dev) \
@@ -577,4 +587,13 @@ int cros_ec_get_features(struct udevice *dev, u64 *featuresp);*/ int cros_ec_check_feature (struct udevice *dev,uint feature); +/** +* cros_ec_get_switches() - Get switches value +* +* @dev: CROS-EC device +* @return switches value, or -ENOSYS if not supported, or other -ve value on +* other error +*/ +int cros_ec_get_switches (struct udevice *dev); +#endif
test/dm/cros_ec.c +26 -0
@@ -75,3 +75,29 @@ static int dm_test_cros_ec_features(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cros_ec_features,UT_TESTF_SCAN_FDT ); + +static int dm_test_cros_ec_switches (struct unit_test_state *uts) +{ +struct udevice *dev; + +ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev)); +ut_asserteq (0 ,cros_ec_get_switches (dev)); + +/* try the command */ +console_record_reset (); +ut_assertok (run_command ("crosec switches" ,0 )); +ut_assert_console_end (); + +/* Open the lid and check the switch changes */ +sandbox_cros_ec_set_test_flags (dev,CROSECT_LID_OPEN ); +ut_asserteq (EC_SWITCH_LID_OPEN ,cros_ec_get_switches (dev)); + +/* try the command */ +console_record_reset (); +ut_assertok (run_command ("crosec switches" ,0 )); +ut_assert_nextline ("lid open" ); +ut_assert_console_end (); + +return 0 ; +} +DM_TEST (dm_test_cros_ec_switches,UT_TESTF_SCAN_FDT );
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 7/10 cros_ec: Add support for switches
On x86 platforms the EC provides a way to read 'switches', which are on/off values determined by the EC.
Add a new driver method for this and implement it for LPC.
---
arch/sandbox/include/asm/test.h | 1 + cmd/cros_ec.c | 41 +++++++++++++++++++++++++++++++++ drivers/misc/cros_ec.c | 16 +++++++++++++ drivers/misc/cros_ec_lpc.c | 7 ++++++ drivers/misc/cros_ec_sandbox.c | 10 ++++++++ include/cros_ec.h | 19 +++++++++++++++ test/dm/cros_ec.c | 26 +++++++++++++++++++++ 7 files changed, 120 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 8/10 cros_ec: Show events in human-readable form
In reply to Simon GlassAdd a command to show the current events as a list of names. This is easier to decipher than a bit mask.
---
cmd/cros_ec.c | 70 +++++++++++++++++++++++++++++++--- drivers/misc/cros_ec_sandbox.c | 12 +++++- test/dm/cros_ec.c | 37 ++++++++++++++++++ 3 files changed, 112 insertions(+), 7 deletions(-)
cmd/cros_ec.c +64 -6
@@ -197,6 +197,66 @@ static int do_show_switches(struct udevice *dev)return 0 ; } +static const char *const event_name[] = { +"lid_closed" , +"lid_open" , +"power_button" , +"ac_connected" , +"ac_disconnected" , +"battery_low" , +"battery_critical" , +"battery" , +"thermal_threshold" , +"device" , +"thermal" , +"usb_charger" , +"key_pressed" , +"interface_ready" , +"keyboard_recovery" , +"thermal_shutdown" , +"battery_shutdown" , +"throttle_start" , +"throttle_stop" , +"hang_detect" , +"hang_reboot" , +"pd_mcu" , +"battery_status" , +"panic" , +"keyboard_fastboot" , +"rtc" , +"mkbp" , +"usb_mux" , +"mode_change" , +"keyboard_recovery_hw_reinit" , +"extended" , +"invalid" , +}; + +static int do_show_events (struct udevice *dev) +{ +u32 events; +int ret; +uint i; + + ret =cros_ec_get_host_events (dev, &events); +if (ret) +return ret; +printf ("%08x\n" , events); +for (i =0 ; i <ARRAY_SIZE (event_name); i++) { +enum host_event_code code = i +1 ; +u64 mask =EC_HOST_EVENT_MASK (code); + +if (events & mask) { +if (event_name[i]) +printf ("%s\n" , event_name[i]); +else +printf ("unknown code %#x\n" , code); + } + } + +return 0 ; +} +static int do_cros_ec (struct cmd_tbl *cmdtp,int flag,int argc,char *const argv[]) {
@@ -303,13 +363,10 @@ static int do_cros_ec(struct cmd_tbl *cmdtp, int flag, int argc,return 1 ; } }else if (0 ==strcmp ("events" , cmd)) { -uint32_t events; + ret =do_show_events (dev); -if (cros_ec_get_host_events (dev, &events)) { -debug ("%s: Could not read host events\n" , __func__); -return 1 ; - } -printf ("0x%08x\n" , events); +if (ret) +printf ("Error: %d\n" , ret); }else if (0 ==strcmp ("clrevents" , cmd)) {uint32_t events =0x7fffffff ;
@@ -498,6 +555,7 @@ U_BOOT_CMD("crosec hash Read CROS-EC hash\n" "crosec reboot [rw | ro | cold] Reboot CROS-EC\n" "crosec events Read CROS-EC host events\n" +"crosec eventsb Read CROS-EC host events_b\n" "crosec clrevents [mask] Clear CROS-EC host events\n" "crosec regioninfo <ro|rw> Read image info\n" "crosec flashinfo Read flash info\n"
drivers/misc/cros_ec_sandbox.c +11 -1
@@ -364,10 +364,20 @@ static int process_cmd(struct ec_state *ec, resp->mask |=EC_HOST_EVENT_MASK (EC_HOST_EVENT_KEYBOARD_RECOVERY ); } - +if (ec->test_flags &CROSECT_LID_OPEN ) + resp->mask |= +EC_HOST_EVENT_MASK (EC_HOST_EVENT_LID_OPEN ); len =sizeof (*resp);break ; } +case EC_CMD_HOST_EVENT_CLEAR_B : { +const struct ec_params_host_event_mask *req = req_data; + +if (req->mask &EC_HOST_EVENT_MASK (EC_HOST_EVENT_LID_OPEN )) + ec->test_flags &= ~CROSECT_LID_OPEN ; + len =0 ; +break ; + }case EC_CMD_VBOOT_HASH : {const struct ec_params_vboot_hash *req = req_data;struct ec_response_vboot_hash *resp = resp_data;
test/dm/cros_ec.c +37 -0
@@ -101,3 +101,40 @@ static int dm_test_cros_ec_switches(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cros_ec_switches,UT_TESTF_SCAN_FDT ); + +static int dm_test_cros_ec_events (struct unit_test_state *uts) +{ +struct udevice *dev; +u32 events; + +ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev)); +ut_assertok (cros_ec_get_host_events (dev, &events)); +ut_asserteq (0 , events); + +/* try the command */ +console_record_reset (); +ut_assertok (run_command ("crosec events" ,0 )); +ut_assert_nextline ("00000000" ); +ut_assert_console_end (); + +/* Open the lid and check the event appears */ +sandbox_cros_ec_set_test_flags (dev,CROSECT_LID_OPEN ); +ut_assertok (cros_ec_get_host_events (dev, &events)); +ut_asserteq (EC_HOST_EVENT_MASK (EC_HOST_EVENT_LID_OPEN ), events); + +/* try the command */ +console_record_reset (); +ut_assertok (run_command ("crosec events" ,0 )); +ut_assert_nextline ("00000002" ); +ut_assert_nextline ("lid_open" ); +ut_assert_console_end (); + +/* Clear the event */ +ut_assertok (cros_ec_clear_host_events (dev, +EC_HOST_EVENT_MASK (EC_HOST_EVENT_LID_OPEN ))); +ut_assertok (cros_ec_get_host_events (dev, &events)); +ut_asserteq (0 , events); + +return 0 ; +} +DM_TEST (dm_test_cros_ec_events,UT_TESTF_SCAN_FDT );
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 8/10 cros_ec: Show events in human-readable form
Add a command to show the current events as a list of names. This is easier to decipher than a bit mask.
---
cmd/cros_ec.c | 70 +++++++++++++++++++++++++++++++--- drivers/misc/cros_ec_sandbox.c | 12 +++++- test/dm/cros_ec.c | 37 ++++++++++++++++++ 3 files changed, 112 insertions(+), 7 deletions(-)
Applied to u-boot-dm, thanks!
PATCH 9/10 cros_ec: Allow use with of-platdata
In reply to Simon GlassAvoid reading the device tree when of-platdata is in use.
---
drivers/misc/cros_ec.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/misc/cros_ec.c +2 -0
@@ -1577,6 +1577,8 @@ UCLASS_DRIVER(cros_ec) = { .id = UCLASS_CROS_EC, .name = "cros-ec", .per_device_auto = sizeof(struct cros_ec_dev), +#if !CONFIG_IS_ENABLED(OF_PLATDATA) .post_bind = dm_scan_fdt_dev, +#endif .flags = DM_UC_FLAG_ALLOC_PRIV_DMA, };
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 9/10 cros_ec: Allow use with of-platdata
Avoid reading the device tree when of-platdata is in use.
---
drivers/misc/cros_ec.c | 2 ++ 1 file changed, 2 insertions(+)
Applied to u-boot-dm, thanks!
PATCH 10/10 cros_ec: Add vstore support
In reply to Simon GlassThe EC can store small amounts of data for the benefit of the verified boot process. Since the EC is seldom reset, this can allow the AP to store data that survives a reboot or a suspend/resume cycle.
Add support for this.
---
drivers/misc/cros_ec.c | 71 ++++++++++++++++++++++++++++++++++ drivers/misc/cros_ec_sandbox.c | 52 ++++++++++++++++++++++++- include/cros_ec.h | 43 ++++++++++++++++++++ test/dm/cros_ec.c | 38 ++++++++++++++++++ 4 files changed, 202 insertions(+), 2 deletions(-)
drivers/misc/cros_ec.c +71 -0
@@ -1557,6 +1557,77 @@ int cros_ec_set_lid_shutdown_mask(struct udevice *dev, int enable)return 0 ; } +int cros_ec_vstore_supported (struct udevice *dev) +{ +return cros_ec_check_feature (dev,EC_FEATURE_VSTORE ); +} + +int cros_ec_vstore_info (struct udevice *dev,u32 *lockedp) +{ +struct ec_response_vstore_info *resp; + +if (ec_command_inptr (dev,EC_CMD_VSTORE_INFO ,0 ,NULL ,0 , + (uint8_t **)&resp,sizeof (*resp)) !=sizeof (*resp)) +return -EIO ; + +if (lockedp) + *lockedp = resp->slot_locked; + +return resp->slot_count; +} + +/* +* cros_ec_vstore_read - Read data from EC vstore slot +* +* @slot: vstore slot to read from +* @data: buffer to store read data, must be EC_VSTORE_SLOT_SIZE bytes +*/ +int cros_ec_vstore_read (struct udevice *dev,int slot,uint8_t *data) +{ +struct ec_params_vstore_read req; +struct ec_response_vstore_read *resp; + + req.slot = slot; +if (ec_command_inptr (dev,EC_CMD_VSTORE_READ ,0 , &req,sizeof (req), + (uint8_t **)&resp,sizeof (*resp)) !=sizeof (*resp)) +return -EIO ; + +if (!data || req.slot >=EC_VSTORE_SLOT_MAX ) +return -EINVAL ; + +memcpy (data, resp->data,sizeof (resp->data)); + +return 0 ; +} + +/* +* cros_ec_vstore_write - Save data into EC vstore slot +* +* @slot: vstore slot to write into +* @data: data to write +* @size: size of data in bytes +* +* Maximum size of data is EC_VSTORE_SLOT_SIZE. It is the callers +* responsibility to check the number of implemented slots by +* querying the vstore info. +*/ +int cros_ec_vstore_write (struct udevice *dev,int slot,const uint8_t *data, +size_t size) +{ +struct ec_params_vstore_write req; + +if (slot >=EC_VSTORE_SLOT_MAX || size >EC_VSTORE_SLOT_SIZE ) +return -EINVAL ; + + req.slot = slot; +memcpy (req.data, data, size); + +if (ec_command (dev,EC_CMD_VSTORE_WRITE ,0 , &req,sizeof (req),NULL ,0 )) +return -EIO ; + +return 0 ; +} +int cros_ec_get_switches (struct udevice *dev) {struct dm_cros_ec_ops *ops;
drivers/misc/cros_ec_sandbox.c +50 -2
82 lines not on this page · raw message
include/cros_ec.h +43 -0
47 lines not on this page · raw message
test/dm/cros_ec.c +38 -0
@@ -56,6 +56,7 @@ static int dm_test_cros_ec_features(struct unit_test_state *uts)ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev));ut_assertok (cros_ec_get_features (dev, &feat));ut_asserteq_64 (1U <<EC_FEATURE_FLASH |1U <<EC_FEATURE_I2C | +1u <<EC_FEATURE_VSTORE |1ULL <<EC_FEATURE_UNIFIED_WAKE_MASKS |1ULL <<EC_FEATURE_ISH , feat);
@@ -68,6 +69,7 @@ static int dm_test_cros_ec_features(struct unit_test_state *uts)ut_assertok (run_command ("crosec features" ,0 ));ut_assert_nextline ("flash" );ut_assert_nextline ("i2c" ); +ut_assert_nextline ("vstore" );ut_assert_nextline ("unified_wake_masks" );ut_assert_nextline ("ish" );ut_assert_console_end ();
@@ -138,3 +140,39 @@ static int dm_test_cros_ec_events(struct unit_test_state *uts)return 0 ; }DM_TEST (dm_test_cros_ec_events,UT_TESTF_SCAN_FDT ); + +static int dm_test_cros_ec_vstore (struct unit_test_state *uts) +{ +const int size =EC_VSTORE_SLOT_SIZE ; +u8 test_data[size], data[size]; +struct udevice *dev; +u32 locked; +int i; + +ut_assertok (uclass_first_device_err (UCLASS_CROS_EC , &dev)); +ut_asserteq (true,cros_ec_vstore_supported (dev)); + +ut_asserteq (4 ,cros_ec_vstore_info (dev, &locked)); +ut_asserteq (0 , locked); + +/* Write some data */ +for (i =0 ; i < size; i++) + test_data[i] =' ' + i; +ut_assertok (cros_ec_vstore_write (dev,2 , test_data, size)); + +/* Check it is locked */ +ut_asserteq (4 ,cros_ec_vstore_info (dev, &locked)); +ut_asserteq (1 <<2 , locked); + +/* Read it back and compare */ +ut_assertok (cros_ec_vstore_read (dev,2 , data)); +ut_asserteq_mem (test_data, data, size); + +/* Try another slot to make sure it is empty */ +ut_assertok (cros_ec_vstore_read (dev,0 , data)); +for (i =0 ; i < size; i++) +ut_asserteq (0 , data[i]); + +return 0 ; +} +DM_TEST (dm_test_cros_ec_vstore,UT_TESTF_SCAN_FDT );
Signature · 1 line
2.30.0.284.gd98b1dd5eaa7-goog
PATCH 10/10 cros_ec: Add vstore support
The EC can store small amounts of data for the benefit of the verified boot process. Since the EC is seldom reset, this can allow the AP to store data that survives a reboot or a suspend/resume cycle.
Add support for this.
---
drivers/misc/cros_ec.c | 71 ++++++++++++++++++++++++++++++++++ drivers/misc/cros_ec_sandbox.c | 52 ++++++++++++++++++++++++- include/cros_ec.h | 43 ++++++++++++++++++++ test/dm/cros_ec.c | 38 ++++++++++++++++++ 4 files changed, 202 insertions(+), 2 deletions(-)
Applied to u-boot-dm, thanks!