summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Spiegel <richard.spiegel@amd.corp-partner.google.com>2018-09-03 08:55:51 -0700
committerMartin Roth <martinroth@google.com>2018-09-07 14:53:54 +0000
commit7160766ebf97aa33b2cd708146ba84701b5a6fff (patch)
tree91284b0de15fa2d5bd2eb6502f30c9a05eae2a63
parent653f760b133fcad4b6465a25867f0c6d9b24bdb0 (diff)
downloadcoreboot-7160766ebf97aa33b2cd708146ba84701b5a6fff.tar.xz
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>
-rw-r--r--src/lib/gpio.c15
1 files changed, 13 insertions, 2 deletions
diff --git a/src/lib/gpio.c b/src/lib/gpio.c
index da748492fc..4cc039a7da 100644
--- a/src/lib/gpio.c
+++ b/src/lib/gpio.c
@@ -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)