diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2018-08-31 12:55:38 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-09-05 05:11:38 -0700 |
commit | f932679e653a4b62fbe1fb28fba8a20d7a7e38c2 (patch) | |
tree | 91dab26624c7dffe72f3a0a797abfd0a1605c2e0 | |
parent | 837c7609f290d4694c3912aaeb4562fbf4b59b8e (diff) | |
download | chrome-ec-f932679e653a4b62fbe1fb28fba8a20d7a7e38c2.tar.gz |
driver: accel: Prevent ODR from crashing the EC
For accelerometers which are interrupt driven, setting ODR too high may
exhaust the EC and trigger the watchdog timer.
Use config variable to verify the ODR requested is not too big.
Return an error when setting unsupported parameters.
BUG=b:112672627
BRANCH=nocturne,eve
TEST=Without this change, ectool motionsense odr 0 500000 crashes the EC
Now, it returns EC result 3 (INVALID_PARAM)
Change-Id: I64a4e522dcad450d619a7fc48a1330479f1cf81f
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1200068
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
-rw-r--r-- | driver/accel_lis2dh.c | 12 | ||||
-rw-r--r-- | driver/accelgyro_bmi160.c | 37 | ||||
-rw-r--r-- | driver/accelgyro_lsm6dsm.c | 4 |
3 files changed, 21 insertions, 32 deletions
diff --git a/driver/accel_lis2dh.c b/driver/accel_lis2dh.c index a8e8827acd..df0b7be7de 100644 --- a/driver/accel_lis2dh.c +++ b/driver/accel_lis2dh.c @@ -96,14 +96,10 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd) normalized_rate = LIS2DH_REG_TO_NORMALIZE(reg_val); } - /* Adjust rounded value */ - if (reg_val > LIS2DH_ODR_400HZ_VAL) { - reg_val = LIS2DH_ODR_400HZ_VAL; - normalized_rate = LIS2DH_ODR_MAX_VAL; - } else if (reg_val < LIS2DH_ODR_1HZ_VAL) { - reg_val = LIS2DH_ODR_1HZ_VAL; - normalized_rate = LIS2DH_ODR_MIN_VAL; - } + if (normalized_rate > MIN(LIS2DH_ODR_MAX_VAL, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ) || + normalized_rate < LIS2DH_ODR_MIN_VAL) + return EC_RES_INVALID_PARAM; /* * Lock accel resource to prevent another task from attempting diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c index 2d03b06bd1..084304af54 100644 --- a/driver/accelgyro_bmi160.c +++ b/driver/accelgyro_bmi160.c @@ -389,41 +389,32 @@ static int set_data_rate(const struct motion_sensor_t *s, normalized_rate = BMI160_REG_TO_ODR(reg_val); if (rnd && (normalized_rate < rate)) { reg_val++; - normalized_rate *= 2; + normalized_rate = BMI160_REG_TO_ODR(reg_val); } switch (s->type) { case MOTIONSENSE_TYPE_ACCEL: - if (reg_val > BMI160_ODR_1600HZ) { - reg_val = BMI160_ODR_1600HZ; - normalized_rate = 1600000; - } else if (reg_val < BMI160_ODR_0_78HZ) { - reg_val = BMI160_ODR_0_78HZ; - normalized_rate = 780; - } + if (normalized_rate > MIN(BMI160_ACCEL_MAX_FREQ, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ) || + normalized_rate < BMI160_ACCEL_MIN_FREQ) + return EC_RES_INVALID_PARAM; break; case MOTIONSENSE_TYPE_GYRO: - if (reg_val > BMI160_ODR_3200HZ) { - reg_val = BMI160_ODR_3200HZ; - normalized_rate = 3200000; - } else if (reg_val < BMI160_ODR_25HZ) { - reg_val = BMI160_ODR_25HZ; - normalized_rate = 25000; - } + if (normalized_rate > MIN(BMI160_GYRO_MAX_FREQ, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ) || + normalized_rate < BMI160_GYRO_MIN_FREQ) + return EC_RES_INVALID_PARAM; break; +#ifdef CONFIG_MAG_BMI160_BMM150 case MOTIONSENSE_TYPE_MAG: /* We use the regular preset we can go about 100Hz */ - if (reg_val > BMI160_ODR_100HZ) { - reg_val = BMI160_ODR_100HZ; - normalized_rate = 100000; - } else if (reg_val < BMI160_ODR_0_78HZ) { - reg_val = BMI160_ODR_0_78HZ; - normalized_rate = 780; - } + if (reg_val > BMI160_ODR_100HZ || reg_val < BMI160_ODR_0_78HZ) + return EC_RES_INVALID_PARAM; break; +#endif default: - return -1; + return EC_RES_INVALID_PARAM; } /* diff --git a/driver/accelgyro_lsm6dsm.c b/driver/accelgyro_lsm6dsm.c index da445ec491..45c76e8068 100644 --- a/driver/accelgyro_lsm6dsm.c +++ b/driver/accelgyro_lsm6dsm.c @@ -446,7 +446,9 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd) reg_val++; normalized_rate = LSM6DSM_REG_TO_ODR(reg_val); } - if (normalized_rate == 0) + if (normalized_rate < LSM6DSM_ODR_MIN_VAL || + normalized_rate > MIN(LSM6DSM_ODR_MAX_VAL, + CONFIG_EC_MAX_SENSOR_FREQ_MILLIHZ)) return EC_RES_INVALID_PARAM; } else { reg_val = 0; |