Detect Write Protect when programming MAC address

When Write Protect is enabled, write to eeprom chips from different
vendors fail differently. For ATMEL chip, write returns success, read
and verify is needed to detect write failure. For STMicroelectronics,
write return error, add read attempt to detect if Write Protect is
enabled.

Tested:
No regression in MAC programing commands.
Fake write error and verified read is tried through debug print.

Signed-off-by: Zhikui Ren <zhikui.ren@intel.com>
Change-Id: I8d9a851d9fde7a0bd6b0a681d13c5be55f43f6cc
diff --git a/src/manufacturingcommands.cpp b/src/manufacturingcommands.cpp
index 15bfc7f..975484f 100644
--- a/src/manufacturingcommands.cpp
+++ b/src/manufacturingcommands.cpp
@@ -983,12 +983,13 @@
         ipmi::Cc ret =
             ipmi::i2cWriteRead(i2cBus, fruAddress, writeData, readBuf);
 
+        // prepare for read to detect chip is write protected
+        writeData.resize(1);
+        readBuf.resize(maxEthSize + 1); // include macHeader
+
         switch (ret)
         {
             case ipmi::ccSuccess:
-                // chip is write protected, if write is success but fails verify
-                writeData.resize(1);
-                readBuf.resize(maxEthSize + 1); // include macHeader
                 // Wait for internal write cycle to complete
                 // example: ATMEL 24c0x chip has Twr spec as 5ms
 
@@ -1013,10 +1014,21 @@
                         "protected.");
                 }
                 return ipmi::ccCommandNotAvailable;
-            default: // assumes no actual eeprom for other failure
-                phosphor::logging::log<phosphor::logging::level::ERR>(
-                    "ERROR: write mac fru failed, assume no eeprom is "
-                    "available.");
+            default:
+                if (ipmi::i2cWriteRead(i2cBus, fruAddress, writeData,
+                                       readBuf) == ipmi::ccSuccess)
+                {
+                    phosphor::logging::log<phosphor::logging::level::INFO>(
+                        "INFO: write mac fru failed, but successfully read "
+                        "from fru, fru may be write protected.");
+                    return ipmi::ccCommandNotAvailable;
+                }
+                else // assume failure is due to no eeprom on board
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "ERROR: write mac fru failed, assume no eeprom is "
+                        "available.");
+                }
                 break;
         }
     }