amd/agesa/f*/Lib/amdlib.c: Integer overflow in loop construct

As is the case in commit:

 3312ed7 amd/agesa/f1?/Lib/amdlib.c: Integer overflow in loop construct

The semantics of this loop relies on an integer overflow in Index >=0
that implies a return value of (UINT8)-1 which around wraps to 0xFF, or
VOLT_UNSUPPORTED.

Also fix an infinite loop.

Change-Id: Iced3eff3ae7b8935db3bdd6147372cf3b540883c
Signed-off-by: Edward O'Callaghan <eocallaghan@alterapraxis.com>
Reviewed-on: http://review.coreboot.org/7676
Reviewed-by: Bruce Griffith <Bruce.Griffith@se-eng.com>
Tested-by: build bot (Jenkins)
Reviewed-by: Alexandru Gagniuc <mr.nuke.me@gmail.com>
This commit is contained in:
Edward O'Callaghan 2014-12-07 05:20:14 +11:00
parent 68402103ee
commit cb0dd58b37
6 changed files with 77 additions and 30 deletions

View File

@ -339,17 +339,25 @@ LibAmdBitScanForward (
} }
return (UINT8) Index; return (UINT8) Index;
} }
UINT8 UINT8
LibAmdBitScanReverse ( LibAmdBitScanReverse (
IN UINT32 value IN UINT32 value
) )
{ {
UINT8 Index; uint8_t bit = 31;
for (Index = 31; Index >= 0; Index--){ do {
if (value & (1 << Index)) return Index; if (value & (1 << 31))
} return bit;
return 0xFF;
value <<= 1;
bit--;
} while (value != 0);
return 0xFF; /* Error code indicating no bit found */
} }
VOID VOID
LibAmdMsrRead ( LibAmdMsrRead (
IN UINT32 MsrAddress, IN UINT32 MsrAddress,

View File

@ -343,17 +343,25 @@ LibAmdBitScanForward (
} }
return (UINT8) Index; return (UINT8) Index;
} }
UINT8 UINT8
LibAmdBitScanReverse ( LibAmdBitScanReverse (
IN UINT32 value IN UINT32 value
) )
{ {
UINT8 Index; uint8_t bit = 31;
for (Index = 31; Index >= 0; Index--){ do {
if (value & (1 << Index)) return Index; if (value & (1 << 31))
} return bit;
return 0xFF;
value <<= 1;
bit--;
} while (value != 0);
return 0xFF; /* Error code indicating no bit found */
} }
VOID VOID
LibAmdMsrRead ( LibAmdMsrRead (
IN UINT32 MsrAddress, IN UINT32 MsrAddress,

View File

@ -343,17 +343,25 @@ LibAmdBitScanForward (
} }
return (UINT8) Index; return (UINT8) Index;
} }
UINT8 UINT8
LibAmdBitScanReverse ( LibAmdBitScanReverse (
IN UINT32 value IN UINT32 value
) )
{ {
UINT8 Index; uint8_t bit = 31;
for (Index = 31; Index >= 0; Index--){ do {
if (value & (1 << Index)) return Index; if (value & (1 << 31))
} return bit;
return 0xFF;
value <<= 1;
bit--;
} while (value != 0);
return 0xFF; /* Error code indicating no bit found */
} }
VOID VOID
LibAmdMsrRead ( LibAmdMsrRead (
IN UINT32 MsrAddress, IN UINT32 MsrAddress,

View File

@ -343,16 +343,23 @@ LibAmdBitScanForward (
} }
return (UINT8) Index; return (UINT8) Index;
} }
UINT8 UINT8
LibAmdBitScanReverse ( LibAmdBitScanReverse (
IN UINT32 value IN UINT32 value
) )
{ {
UINT8 Index; uint8_t bit = 31;
for (Index = 31; Index >= 0; Index--){ do {
if (value & (1 << Index)) return Index; if (value & (1 << 31))
} return bit;
return 0xFF;
value <<= 1;
bit--;
} while (value != 0);
return 0xFF; /* Error code indicating no bit found */
} }
UINT64 UINT64

View File

@ -354,17 +354,25 @@ LibAmdBitScanForward (
} }
return (UINT8) Index; return (UINT8) Index;
} }
UINT8 UINT8
LibAmdBitScanReverse ( LibAmdBitScanReverse (
IN UINT32 value IN UINT32 value
) )
{ {
UINT8 Index; uint8_t bit = 31;
for (Index = 31; Index >= 0; Index--){ do {
if (value & (1 << Index)) return Index; if (value & (1 << 31))
} return bit;
return 0xFF;
value <<= 1;
bit--;
} while (value != 0);
return 0xFF; /* Error code indicating no bit found */
} }
VOID VOID
LibAmdMsrRead ( LibAmdMsrRead (
IN UINT32 MsrAddress, IN UINT32 MsrAddress,

View File

@ -355,17 +355,25 @@ LibAmdBitScanForward (
} }
return (UINT8) Index; return (UINT8) Index;
} }
UINT8 UINT8
LibAmdBitScanReverse ( LibAmdBitScanReverse (
IN UINT32 value IN UINT32 value
) )
{ {
UINTN Index; uint8_t bit = 31;
for (Index = 31; Index >= 0; Index--){ do {
if (value & (1 << Index)) break; if (value & (1 << 31))
} return bit;
return (UINT8) Index;
value <<= 1;
bit--;
} while (value != 0);
return 0xFF; /* Error code indicating no bit found */
} }
VOID VOID
LibAmdMsrRead ( LibAmdMsrRead (
IN UINT32 MsrAddress, IN UINT32 MsrAddress,