[PATCH v3 12/23] platform/chrome: cros_ec_proto: handle empty payload in getting info legacy

From: Tzung-Bi Shih
Date: Wed Jun 08 2022 - 07:09:40 EST


cros_ec_get_proto_info_legacy() expects to receive
sizeof(struct ec_response_hello) from send_command(). The payload is
valid only if the return value is positive.

Return -EPROTO if send_command() returns 0 in
cros_ec_get_proto_info_legacy().

Signed-off-by: Tzung-Bi Shih <tzungbi@xxxxxxxxxx>
---
Changes from v2:
- Separate Kunit test to another patch.

drivers/platform/chrome/cros_ec_proto.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_proto.c b/drivers/platform/chrome/cros_ec_proto.c
index 04b9704ed302..473654f50bca 100644
--- a/drivers/platform/chrome/cros_ec_proto.c
+++ b/drivers/platform/chrome/cros_ec_proto.c
@@ -356,7 +356,7 @@ static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
struct cros_ec_command *msg;
struct ec_params_hello *params;
struct ec_response_hello *response;
- int ret;
+ int ret, mapped;

ec_dev->proto_version = 2;

@@ -377,12 +377,18 @@ static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
goto exit;
}

- ret = cros_ec_map_error(msg->result);
- if (ret) {
+ mapped = cros_ec_map_error(msg->result);
+ if (mapped) {
+ ret = mapped;
dev_err(ec_dev->dev, "EC responded to v2 hello with error: %d\n", msg->result);
goto exit;
}

+ if (ret == 0) {
+ ret = -EPROTO;
+ goto exit;
+ }
+
response = (struct ec_response_hello *)msg->data;
if (response->out_data != 0xa1b2c3d4) {
dev_err(ec_dev->dev,
--
2.36.1.255.ge46751e96f-goog