From patchwork Tue Jun 7 14:56:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tzung-Bi Shih X-Patchwork-Id: 12872046 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6371028FB for ; Tue, 7 Jun 2022 14:57:04 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 51FE0C34119; Tue, 7 Jun 2022 14:57:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1654613824; bh=sZ92Vbg57S7TtM+sV+KDARqwBNuHipwIzFH3X1WEAiU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=esXdknyayI9W8EsppGtlhbroojzQXj4wLtZKcE8gVFCMIGkTJbKb+aqBrENvJ6Ybp 9pi9BGH2mejpEnqWVuK0Dy94baDQRXGaVHs6X5MZOUIkMOA0K60Io5AGg6k3FL2FJZ DG1Z6Ozhmsdlf4Z35mup3HHObRu2UlnZ2+RDDrswoJRdZNXLmvsAhVDtmV6HQ8ifj9 KpUtv8BTTFH9m02joscKhNAgewfELZSQPAu5bfz5+7pTloRh7+xx0KBYKUdRAqadMC Nm2R2ZwGj0OZqNp7PPNIPlMwrWh5fIMqKhO/EKZ4bk2Ob/FFA8Q13C0svwKHev1n06 ipkZ7vzj/PKWw== From: Tzung-Bi Shih To: bleung@chromium.org, groeck@chromium.org Cc: chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org, tzungbi@kernel.org Subject: [PATCH v2 10/15] platform/chrome: cros_ec_proto: handle empty payload in getting info legacy Date: Tue, 7 Jun 2022 14:56:34 +0000 Message-Id: <20220607145639.2362750-11-tzungbi@kernel.org> X-Mailer: git-send-email 2.36.1.255.ge46751e96f-goog In-Reply-To: <20220607145639.2362750-1-tzungbi@kernel.org> References: <20220607145639.2362750-1-tzungbi@kernel.org> Precedence: bulk X-Mailing-List: chrome-platform@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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. Add a Kunit test for returning 0 in send_command() and handle the case in cros_ec_get_proto_info_legacy(). Signed-off-by: Tzung-Bi Shih --- No v1. New in the series. drivers/platform/chrome/cros_ec_proto.c | 12 +++-- drivers/platform/chrome/cros_ec_proto_test.c | 49 ++++++++++++++++++++ 2 files changed, 58 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, diff --git a/drivers/platform/chrome/cros_ec_proto_test.c b/drivers/platform/chrome/cros_ec_proto_test.c index 9f7d9666369f..730248be42a7 100644 --- a/drivers/platform/chrome/cros_ec_proto_test.c +++ b/drivers/platform/chrome/cros_ec_proto_test.c @@ -750,6 +750,54 @@ static void cros_ec_proto_test_query_all_legacy_data_error(struct kunit *test) } } +static void cros_ec_proto_test_query_all_legacy_return0(struct kunit *test) +{ + struct cros_ec_proto_test_priv *priv = test->priv; + struct cros_ec_device *ec_dev = &priv->ec_dev; + struct ec_xfer_mock *mock; + int ret; + + /* For cros_ec_get_proto_info() without passthru. */ + { + mock = cros_kunit_ec_xfer_mock_addx(test, 0, EC_RES_INVALID_COMMAND, 0); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + } + + /* For cros_ec_get_proto_info_legacy(). */ + { + mock = cros_kunit_ec_xfer_mock_add(test, 0); + KUNIT_ASSERT_PTR_NE(test, mock, NULL); + } + + cros_ec_proto_test_query_all_pretest(test); + ret = cros_ec_query_all(ec_dev); + KUNIT_EXPECT_EQ(test, ret, -EPROTO); + KUNIT_EXPECT_EQ(test, ec_dev->proto_version, EC_PROTO_VERSION_UNKNOWN); + + /* For cros_ec_get_proto_info() without passthru. */ + { + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_GET_PROTOCOL_INFO); + KUNIT_EXPECT_EQ(test, mock->msg.insize, + sizeof(struct ec_response_get_protocol_info)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, 0); + } + + /* For cros_ec_get_proto_info_legacy(). */ + { + mock = cros_kunit_ec_xfer_mock_next(); + KUNIT_EXPECT_PTR_NE(test, mock, NULL); + + KUNIT_EXPECT_EQ(test, mock->msg.version, 0); + KUNIT_EXPECT_EQ(test, mock->msg.command, EC_CMD_HELLO); + KUNIT_EXPECT_EQ(test, mock->msg.insize, sizeof(struct ec_response_hello)); + KUNIT_EXPECT_EQ(test, mock->msg.outsize, sizeof(struct ec_params_hello)); + } +} + static void cros_ec_proto_test_query_all_no_mkbp(struct kunit *test) { struct cros_ec_proto_test_priv *priv = test->priv; @@ -1134,6 +1182,7 @@ static struct kunit_case cros_ec_proto_test_cases[] = { KUNIT_CASE(cros_ec_proto_test_query_all_legacy_xfer_error), KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return_error), KUNIT_CASE(cros_ec_proto_test_query_all_legacy_data_error), + KUNIT_CASE(cros_ec_proto_test_query_all_legacy_return0), KUNIT_CASE(cros_ec_proto_test_query_all_no_mkbp), KUNIT_CASE(cros_ec_proto_test_query_all_no_host_sleep), KUNIT_CASE(cros_ec_proto_test_query_all_default_wake_mask_return_error),