diff mbox

[2/3] acpi: fadt: use new I/O port helpers to avoid segfaults

Message ID 1358252735-14227-3-git-send-email-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King Jan. 15, 2013, 12:25 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

Use he new I/O port helper functions to avoid any segfaults. We
also add some extra error checking on the ioperm() calls.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/acpi/fadt/fadt.c | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

Comments

Keng-Yu Lin Jan. 29, 2013, 8:31 a.m. UTC | #1
On Tue, Jan 15, 2013 at 8:25 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Use he new I/O port helper functions to avoid any segfaults. We
> also add some extra error checking on the ioperm() calls.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/acpi/fadt/fadt.c | 43 ++++++++++++++++++++++++++++++++-----------
>  1 file changed, 32 insertions(+), 11 deletions(-)
>
> diff --git a/src/acpi/fadt/fadt.c b/src/acpi/fadt/fadt.c
> index d7ee0aa..d18ca4e 100644
> --- a/src/acpi/fadt/fadt.c
> +++ b/src/acpi/fadt/fadt.c
> @@ -54,7 +54,8 @@ static int fadt_init(fwts_framework *fw)
>
>  static int fadt_test1(fwts_framework *fw)
>  {
> -       uint32_t port, width, value;
> +       uint32_t port, width, val32;
> +       int ret = FWTS_OK;
>
>         /*  Not having a FADT is not a failure */
>         if (fadt_size == 0) {
> @@ -94,19 +95,34 @@ static int fadt_test1(fwts_framework *fw)
>
>         switch (width) {
>         case 8:
> -               ioperm(port, width/8, 1);
> -               value = inb(fadt->pm1a_cnt_blk);
> -               ioperm(port, width/8, 0);
> +               if (ioperm(port, width/8, 1) < 0)
> +                       ret = FWTS_ERROR;
> +               else {
> +                       uint8_t val8;
> +
> +                       ret = fwts_inb(port, &val8);
> +                       val32 = val8;
> +                       ioperm(port, width/8, 0);
> +               }
>                 break;
>         case 16:
> -               ioperm(port, width/8, 1);
> -               value = inw(fadt->pm1a_cnt_blk);
> -               ioperm(port, width/8, 0);
> +               if (ioperm(port, width/8, 1) < 0)
> +                       ret = FWTS_ERROR;
> +               else {
> +                       uint16_t val16;
> +
> +                       ret = fwts_inw(port, &val16);
> +                       val32 = val16;
> +                       ioperm(port, width/8, 0);
> +               }
>                 break;
>         case 32:
> -               ioperm(port, width/8, 1);
> -               value = inl(fadt->pm1a_cnt_blk);
> -               ioperm(port, width/8, 0);
> +               if (ioperm(port, width/8, 1) < 0)
> +                       ret = FWTS_ERROR;
> +               else {
> +                       ret = fwts_inl(port, &val32);
> +                       ioperm(port, width/8, 0);
> +               }
>                 break;
>         default:
>                 fwts_failed(fw, LOG_LEVEL_HIGH, "FADTPM1AInvalidWidth",
> @@ -116,7 +132,12 @@ static int fadt_test1(fwts_framework *fw)
>                 return FWTS_OK;
>         }
>
> -       if (value & 0x01)
> +       if (ret != FWTS_OK) {
> +               fwts_log_error(fw, "Cannot read FADT PM1A_CNT_BLK port 0x%" PRIx32 ".", port);
> +               return FWTS_ERROR;
> +       }
> +
> +       if (val32 & 0x01)
>                 fwts_passed(fw, "SCI_EN bit in PM1a Control Register Block is enabled.");
>         else {
>                 fwts_failed(fw, LOG_LEVEL_HIGH, "SCI_ENNotEnabled",
> --
> 1.8.0
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Ivan Hu Feb. 1, 2013, 2:04 a.m. UTC | #2
On 01/15/2013 08:25 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Use he new I/O port helper functions to avoid any segfaults. We
> also add some extra error checking on the ioperm() calls.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/acpi/fadt/fadt.c | 43 ++++++++++++++++++++++++++++++++-----------
>   1 file changed, 32 insertions(+), 11 deletions(-)
>
> diff --git a/src/acpi/fadt/fadt.c b/src/acpi/fadt/fadt.c
> index d7ee0aa..d18ca4e 100644
> --- a/src/acpi/fadt/fadt.c
> +++ b/src/acpi/fadt/fadt.c
> @@ -54,7 +54,8 @@ static int fadt_init(fwts_framework *fw)
>
>   static int fadt_test1(fwts_framework *fw)
>   {
> -	uint32_t port, width, value;
> +	uint32_t port, width, val32;
> +	int ret = FWTS_OK;
>
>   	/*  Not having a FADT is not a failure */
>   	if (fadt_size == 0) {
> @@ -94,19 +95,34 @@ static int fadt_test1(fwts_framework *fw)
>
>   	switch (width) {
>   	case 8:
> -		ioperm(port, width/8, 1);
> -		value = inb(fadt->pm1a_cnt_blk);
> -		ioperm(port, width/8, 0);
> +		if (ioperm(port, width/8, 1) < 0)
> +			ret = FWTS_ERROR;
> +		else {
> +			uint8_t val8;
> +
> +			ret = fwts_inb(port, &val8);
> +			val32 = val8;
> +			ioperm(port, width/8, 0);
> +		}
>   		break;
>   	case 16:
> -		ioperm(port, width/8, 1);
> -		value = inw(fadt->pm1a_cnt_blk);
> -		ioperm(port, width/8, 0);
> +		if (ioperm(port, width/8, 1) < 0)
> +			ret = FWTS_ERROR;
> +		else {
> +			uint16_t val16;
> +
> +			ret = fwts_inw(port, &val16);
> +			val32 = val16;
> +			ioperm(port, width/8, 0);
> +		}
>   		break;
>   	case 32:
> -		ioperm(port, width/8, 1);
> -		value = inl(fadt->pm1a_cnt_blk);
> -		ioperm(port, width/8, 0);
> +		if (ioperm(port, width/8, 1) < 0)
> +			ret = FWTS_ERROR;
> +		else {
> +			ret = fwts_inl(port, &val32);
> +			ioperm(port, width/8, 0);
> +		}
>   		break;
>   	default:
>   		fwts_failed(fw, LOG_LEVEL_HIGH, "FADTPM1AInvalidWidth",
> @@ -116,7 +132,12 @@ static int fadt_test1(fwts_framework *fw)
>   		return FWTS_OK;
>   	}
>
> -	if (value & 0x01)
> +	if (ret != FWTS_OK) {
> +		fwts_log_error(fw, "Cannot read FADT PM1A_CNT_BLK port 0x%" PRIx32 ".", port);
> +		return FWTS_ERROR;
> +	}
> +
> +	if (val32 & 0x01)
>   		fwts_passed(fw, "SCI_EN bit in PM1a Control Register Block is enabled.");
>   	else {
>   		fwts_failed(fw, LOG_LEVEL_HIGH, "SCI_ENNotEnabled",
>
Acked-by: Ivan Hu <ivan.hu@canonical.com>
Alex Hung Feb. 4, 2013, 12:19 a.m. UTC | #3
On 01/15/2013 04:25 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Use he new I/O port helper functions to avoid any segfaults. We
> also add some extra error checking on the ioperm() calls.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/acpi/fadt/fadt.c | 43 ++++++++++++++++++++++++++++++++-----------
>   1 file changed, 32 insertions(+), 11 deletions(-)
>
> diff --git a/src/acpi/fadt/fadt.c b/src/acpi/fadt/fadt.c
> index d7ee0aa..d18ca4e 100644
> --- a/src/acpi/fadt/fadt.c
> +++ b/src/acpi/fadt/fadt.c
> @@ -54,7 +54,8 @@ static int fadt_init(fwts_framework *fw)
>
>   static int fadt_test1(fwts_framework *fw)
>   {
> -	uint32_t port, width, value;
> +	uint32_t port, width, val32;
> +	int ret = FWTS_OK;
>
>   	/*  Not having a FADT is not a failure */
>   	if (fadt_size == 0) {
> @@ -94,19 +95,34 @@ static int fadt_test1(fwts_framework *fw)
>
>   	switch (width) {
>   	case 8:
> -		ioperm(port, width/8, 1);
> -		value = inb(fadt->pm1a_cnt_blk);
> -		ioperm(port, width/8, 0);
> +		if (ioperm(port, width/8, 1) < 0)
> +			ret = FWTS_ERROR;
> +		else {
> +			uint8_t val8;
> +
> +			ret = fwts_inb(port, &val8);
> +			val32 = val8;
> +			ioperm(port, width/8, 0);
> +		}
>   		break;
>   	case 16:
> -		ioperm(port, width/8, 1);
> -		value = inw(fadt->pm1a_cnt_blk);
> -		ioperm(port, width/8, 0);
> +		if (ioperm(port, width/8, 1) < 0)
> +			ret = FWTS_ERROR;
> +		else {
> +			uint16_t val16;
> +
> +			ret = fwts_inw(port, &val16);
> +			val32 = val16;
> +			ioperm(port, width/8, 0);
> +		}
>   		break;
>   	case 32:
> -		ioperm(port, width/8, 1);
> -		value = inl(fadt->pm1a_cnt_blk);
> -		ioperm(port, width/8, 0);
> +		if (ioperm(port, width/8, 1) < 0)
> +			ret = FWTS_ERROR;
> +		else {
> +			ret = fwts_inl(port, &val32);
> +			ioperm(port, width/8, 0);
> +		}
>   		break;
>   	default:
>   		fwts_failed(fw, LOG_LEVEL_HIGH, "FADTPM1AInvalidWidth",
> @@ -116,7 +132,12 @@ static int fadt_test1(fwts_framework *fw)
>   		return FWTS_OK;
>   	}
>
> -	if (value & 0x01)
> +	if (ret != FWTS_OK) {
> +		fwts_log_error(fw, "Cannot read FADT PM1A_CNT_BLK port 0x%" PRIx32 ".", port);
> +		return FWTS_ERROR;
> +	}
> +
> +	if (val32 & 0x01)
>   		fwts_passed(fw, "SCI_EN bit in PM1a Control Register Block is enabled.");
>   	else {
>   		fwts_failed(fw, LOG_LEVEL_HIGH, "SCI_ENNotEnabled",
>
Acked-by: Alex Hung <alex.hung@canonical.com>
diff mbox

Patch

diff --git a/src/acpi/fadt/fadt.c b/src/acpi/fadt/fadt.c
index d7ee0aa..d18ca4e 100644
--- a/src/acpi/fadt/fadt.c
+++ b/src/acpi/fadt/fadt.c
@@ -54,7 +54,8 @@  static int fadt_init(fwts_framework *fw)
 
 static int fadt_test1(fwts_framework *fw)
 {
-	uint32_t port, width, value;
+	uint32_t port, width, val32;
+	int ret = FWTS_OK;
 
 	/*  Not having a FADT is not a failure */
 	if (fadt_size == 0) {
@@ -94,19 +95,34 @@  static int fadt_test1(fwts_framework *fw)
 
 	switch (width) {
 	case 8:
-		ioperm(port, width/8, 1);
-		value = inb(fadt->pm1a_cnt_blk);
-		ioperm(port, width/8, 0);
+		if (ioperm(port, width/8, 1) < 0)
+			ret = FWTS_ERROR;
+		else {
+			uint8_t val8;
+
+			ret = fwts_inb(port, &val8);
+			val32 = val8;
+			ioperm(port, width/8, 0);
+		}
 		break;
 	case 16:
-		ioperm(port, width/8, 1);
-		value = inw(fadt->pm1a_cnt_blk);
-		ioperm(port, width/8, 0);
+		if (ioperm(port, width/8, 1) < 0)
+			ret = FWTS_ERROR;
+		else {
+			uint16_t val16;
+
+			ret = fwts_inw(port, &val16);
+			val32 = val16;
+			ioperm(port, width/8, 0);
+		}
 		break;
 	case 32:
-		ioperm(port, width/8, 1);
-		value = inl(fadt->pm1a_cnt_blk);
-		ioperm(port, width/8, 0);
+		if (ioperm(port, width/8, 1) < 0)
+			ret = FWTS_ERROR;
+		else {
+			ret = fwts_inl(port, &val32);
+			ioperm(port, width/8, 0);
+		}
 		break;
 	default:
 		fwts_failed(fw, LOG_LEVEL_HIGH, "FADTPM1AInvalidWidth",
@@ -116,7 +132,12 @@  static int fadt_test1(fwts_framework *fw)
 		return FWTS_OK;
 	}
 
-	if (value & 0x01)
+	if (ret != FWTS_OK) {
+		fwts_log_error(fw, "Cannot read FADT PM1A_CNT_BLK port 0x%" PRIx32 ".", port);
+		return FWTS_ERROR;
+	}
+
+	if (val32 & 0x01)
 		fwts_passed(fw, "SCI_EN bit in PM1a Control Register Block is enabled.");
 	else {
 		fwts_failed(fw, LOG_LEVEL_HIGH, "SCI_ENNotEnabled",