From 7310e7c861d0c90640d9842cc0729a46357ff28f Mon Sep 17 00:00:00 2001 From: Ting Shen Date: Fri, 14 Feb 2020 15:14:14 +0800 Subject: charger_state: check BATT_FLAG_* flags before trusting the value battery_get_params() did not ask drivers to set bad values to zero. And actually some driver did not initialize batt_params to zero. So the callee should be more careful when using the value. BUG=None TEST=On juniper, make sure `charge_request(some-buggy-value)` doesn't exist BRANCH=kukui Change-Id: I0d15f17c852f07e4ce5a4cd5cae4c25e84efea8a Signed-off-by: Ting Shen Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2056111 Reviewed-by: Jett Rink Tested-by: Devin Lu Tested-by: Ting Shen Commit-Queue: Ting Shen --- common/charge_state_v2.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'common/charge_state_v2.c') diff --git a/common/charge_state_v2.c b/common/charge_state_v2.c index cb225c7a3b..c5592cf5d1 100644 --- a/common/charge_state_v2.c +++ b/common/charge_state_v2.c @@ -1754,8 +1754,14 @@ void charger_task(void *u) * battery it uses (0, 0), which is probably safer than blindly * applying power to a battery we can't talk to. */ - curr.requested_voltage = curr.batt.desired_voltage; - curr.requested_current = curr.batt.desired_current; + if (curr.batt.flags & (BATT_FLAG_BAD_DESIRED_VOLTAGE | + BATT_FLAG_BAD_DESIRED_CURRENT)) { + curr.requested_voltage = 0; + curr.requested_current = 0; + } else { + curr.requested_voltage = curr.batt.desired_voltage; + curr.requested_current = curr.batt.desired_current; + } /* If we *know* there's no battery, wait for one to appear. */ if (curr.batt.is_present == BP_NO) { -- cgit v1.2.1