lib/gpio.c: Fix _gpio_base3_value invalid shift

Coverity CID 1395334: (BAD_SHIFT) - In function _gpio_base3_value(), if
gpio_num is 32 and gpio[31] is floating, the end result is 1 << 32, which
does not fit into a int. To avoid a possible error, make it an error to have
num_gpio > 31. Function _gpio_base2_value also have the same issue, but the
limit would be 32. As in practice it'll never be used with more than 20 GPIO,
create a helper function to limit it to 31 and call it everywhere needed.

BUG=b:113788440
TEST=Add a fake code to southbridge_final calling the function and printing
the result. Build and boot grunt, check result.

Change-Id: I0b79725bcbaf120587c7440e176643aaa7a1d5bb
Signed-off-by: Richard Spiegel <richard.spiegel@silverbackltd.com>
Reviewed-on: https://review.coreboot.org/28445
Reviewed-by: Martin Roth <martinroth@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Richard Spiegel 2018-09-03 08:55:51 -07:00 committed by Martin Roth
parent 653f760b13
commit 7160766ebf
1 changed files with 13 additions and 2 deletions

View File

@ -20,6 +20,14 @@
#include <delay.h>
#include <gpio.h>
static void _check_num(const char *name, int num)
{
if ((num > 31) || (num < 1)) {
printk(BIOS_EMERG, "%s: %d ", name, num);
die("is an invalid number of GPIOs");
}
}
static uint32_t _gpio_base2_value(const gpio_t gpio[], int num_gpio)
{
uint32_t result = 0;
@ -38,6 +46,7 @@ uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio)
{
int i;
_check_num(__func__, num_gpio);
for (i = 0; i < num_gpio; i++)
gpio_input(gpio[i]);
@ -48,6 +57,7 @@ uint32_t gpio_pulldown_base2_value(const gpio_t gpio[], int num_gpio)
{
int i;
_check_num(__func__, num_gpio);
for (i = 0; i < num_gpio; i++)
gpio_input_pulldown(gpio[i]);
@ -58,6 +68,7 @@ uint32_t gpio_pullup_base2_value(const gpio_t gpio[], int num_gpio)
{
int i;
_check_num(__func__, num_gpio);
for (i = 0; i < num_gpio; i++)
gpio_input_pullup(gpio[i]);
@ -82,8 +93,8 @@ uint32_t _gpio_base3_value(const gpio_t gpio[], int num_gpio, int binary_first)
int index;
int temp;
char value[32];
if ((num_gpio > 32) && (num_gpio < 1))
die("gpio_base3_value: Invalid number of GPIOs");
_check_num(__func__, num_gpio);
/* Enable internal pull up */
for (index = 0; index < num_gpio; ++index)