Return an error for invalid target addresses

If the provided target address is outside of the valid range,
return an error instead of sending the command.

Tested:
Ran each command with valid and invalid target addresses and
confirmed that the error is returned appropriately.

Change-Id: I49940a3e6353429b70f9c2012b53fa94c5355d13
Signed-off-by: Jason M. Bills <jason.m.bills@linux.intel.com>
diff --git a/peci.c b/peci.c
index 90a0d20..ba38e00 100644
--- a/peci.c
+++ b/peci.c
@@ -205,6 +205,12 @@
     int peci_fd = -1;
     EPECIStatus ret;
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -224,6 +230,12 @@
     EPECIStatus ret;
     struct peci_ping_msg cmd;
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     cmd.addr = target;
     ret = HW_peci_issue_cmd(PECI_IOC_PING, (char*)&cmd, peci_fd);
 
@@ -243,6 +255,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -268,6 +286,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     ret = HW_peci_issue_cmd(PECI_IOC_GET_DIB, (char*)&cmd, peci_fd);
 
     if (ret == PECI_CC_SUCCESS)
@@ -292,6 +316,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -328,6 +358,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -355,6 +391,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the write length must be a byte, word, or dword
     if (u8ReadLen != 1 && u8ReadLen != 2 && u8ReadLen != 4)
     {
@@ -397,6 +439,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -424,6 +472,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the write length must be a byte, word, or dword
     if ((u8WriteLen != 1) && (u8WriteLen != 2) && (u8WriteLen != 4))
     {
@@ -458,6 +512,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -494,6 +554,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -522,6 +588,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // The PECI buffer must be large enough to hold the PCI data
     if (sizeof(cmd.pci_config) < 4)
     {
@@ -561,6 +633,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -590,6 +668,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a byte, word, or dword
     if (u8ReadLen != 1 && u8ReadLen != 2 && u8ReadLen != 4)
     {
@@ -637,6 +721,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -680,6 +770,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // The PECI buffer must be large enough to hold the requested data
     if (sizeof(cmd.data) < u8ReadLen)
     {
@@ -728,6 +824,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -754,6 +856,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a byte, word, or dword
     if (u8ReadLen != 1 && u8ReadLen != 2 && u8ReadLen != 4)
     {
@@ -783,6 +891,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -810,6 +924,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a byte, word, or dword
     if (u8ReadLen != 1 && u8ReadLen != 2 && u8ReadLen != 4)
     {
@@ -840,6 +960,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -868,6 +994,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a byte, word, dword, or qword
     if (u8ReadLen != 1 && u8ReadLen != 2 && u8ReadLen != 4 && u8ReadLen != 8)
     {
@@ -925,6 +1057,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the write length must be a byte, word, or dword
     if (DataLen != 1 && DataLen != 2 && DataLen != 4)
     {
@@ -1013,6 +1151,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -1041,6 +1185,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a byte, word, dword, or qword
     if (u8DataLen != 1 && u8DataLen != 2 && u8DataLen != 4 && u8DataLen != 8)
     {
@@ -1082,6 +1232,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a byte, word, or qword
     if (u8ReadLen != 1 && u8ReadLen != 2 && u8ReadLen != 8)
     {
@@ -1138,6 +1294,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     // Per the PECI spec, the read length must be a qword or dqword
     if (u8ReadLen != 8 && u8ReadLen != 16)
     {
@@ -1194,6 +1356,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The target address must be in the valid range
+    if (target < MIN_CLIENT_ADDR || target > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Open(&peci_fd) != PECI_CC_SUCCESS)
     {
         return PECI_CC_DRIVER_ERR;
@@ -1242,6 +1410,12 @@
         return PECI_CC_INVALID_REQ;
     }
 
+    // The client address must be in the valid range
+    if (clientAddr < MIN_CLIENT_ADDR || clientAddr > MAX_CLIENT_ADDR)
+    {
+        return PECI_CC_INVALID_REQ;
+    }
+
     if (peci_Ping(clientAddr) != PECI_CC_SUCCESS)
     {
         return PECI_CC_CPU_NOT_PRESENT;