Richard Spiegel has uploaded this change for review.

View Change

lib/gpio.c: Check num_gpio

Several functions in src/lib/gpio.c don't check for valid number of gpio,
which must be at least 1 and at most 32 (due to returning int). Create a
function to execute the test, and call it from wherever needed.

BUG=b:113788440
TEST=Add a fake code to southbridge_final calling the functions with and
without valid num_gpio, printing the result. Build and boot grunt, check
results.

Change-Id: I08c47fcd784e14e25ec1409571c1e59cb2e50ed8
Signed-off-by: Richard Spiegel <richard.spiegel@silverbackltd.com>
---
M src/lib/gpio.c
1 file changed, 12 insertions(+), 2 deletions(-)

git pull ssh://review.coreboot.org:29418/coreboot refs/changes/46/28446/1
diff --git a/src/lib/gpio.c b/src/lib/gpio.c
index 4549166..001994a 100644
--- a/src/lib/gpio.c
+++ b/src/lib/gpio.c
@@ -20,6 +20,13 @@
#include <delay.h>
#include <gpio.h>

+static void _check_num(const char *name, int num)
+{
+ if ((num > 32) && (num < 1)) {
+ printk(BIOS_WARNING, "%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 +45,7 @@
{
int i;

+ _check_num(__func__, num_gpio);
for (i = 0; i < num_gpio; i++)
gpio_input(gpio[i]);

@@ -48,6 +56,7 @@
{
int i;

+ _check_num(__func__, num_gpio);
for (i = 0; i < num_gpio; i++)
gpio_input_pulldown(gpio[i]);

@@ -58,6 +67,7 @@
{
int i;

+ _check_num(__func__, num_gpio);
for (i = 0; i < num_gpio; i++)
gpio_input_pullup(gpio[i]);

@@ -82,8 +92,8 @@
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)

To view, visit change 28446. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: coreboot
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I08c47fcd784e14e25ec1409571c1e59cb2e50ed8
Gerrit-Change-Number: 28446
Gerrit-PatchSet: 1
Gerrit-Owner: Richard Spiegel <richard.spiegel@silverbackltd.com>