i2c-vr: add support for MPS MP2X6XX VR firmware update over I2C

This commit introduces support for programming MPS MP2X6XX VR devices
over the I2C bus. It enables firmware updates for VR models such as
MP2869(A), MP29612(A), and MP29608(A).
Support for the MP2X6XXFirmware type in EM was added in [1].

[1] https://gerrit.openbmc.org/c/openbmc/entity-manager/+/82575

Tested on the Santabarbara platform:

1. Display the fw inventory
```
curl --silent $creds https://$bmc/redfish/v1/UpdateService/FirmwareInventory
```

```
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory",
  "@odata.type": "#SoftwareInventoryCollection.SoftwareInventoryCollection",
  "Members": [
    {...},
    {
      "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_4228"
    },
    {...}
  ],
  "Members@odata.count": 13,
  "Name": "Software Inventory Collection"
}
```

2. Query version.
```
curl $creds https://$bmc/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_4228
```

```
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_4228",
  "@odata.type": "#SoftwareInventory.v1_1_0.SoftwareInventory",
  "Description": "Unknown image",
  "Id": "Santabarbara_MB_VR_CPU1_4228",
  "Name": "Software Inventory",
  "Status": {
    "Health": "Warning",
    "HealthRollup": "OK",
    "State": "Disabled"
  },
  "Updateable": true,
  "Version": "1F88"
}
```

3. Trigger the fw update via redfish.
```
curl -k ${creds} \
  -H "Content-Type:multipart/form-data" \
  -X POST \
  -F UpdateParameters="{\"Targets\":[\"/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_4228\"],\"@Redfish.OperationApplyTime\":\"OnReset\"};type=application/json" \
  -F "UpdateFile=@${fwpath};type=application/octet-stream" \
  https://${bmc}/redfish/v1/UpdateService/update-multipart
```

4. Task is returned
```
{
  "@odata.id": "/redfish/v1/TaskService/Tasks/0",
  "@odata.type": "#Task.v1_4_3.Task",
  "HidePayload": false,
  "Id": "0",
  "Messages": [
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has started.",
      "MessageArgs": [
        "0"
      ],
      "MessageId": "TaskEvent.1.0.TaskStarted",
      "MessageSeverity": "OK",
      "Resolution": "None."
    }
  ],
  "Name": "Task 0",
  "Payload": {
    "HttpHeaders": [],
    "HttpOperation": "POST",
    "TargetUri": "/redfish/v1/UpdateService/update-multipart"
  },
  "PercentComplete": 0,
  "StartTime": "2025-07-29T08:51:41+00:00",
  "TaskMonitor": "/redfish/v1/TaskService/TaskMonitors/0",
  "TaskState": "Running",
  "TaskStatus": "OK"
}
```

5. Query Task status
```
curl --silent $creds https://$bmc/redfish/v1/TaskService/Tasks/0
```

```
{
  "@odata.id": "/redfish/v1/TaskService/Tasks/0",
  "@odata.type": "#Task.v1_4_3.Task",
  "EndTime": "2025-07-29T08:51:46+00:00",
  "HidePayload": false,
  "Id": "0",
  "Messages": [
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has started.",
      "MessageArgs": [
        "0"
      ],
      "MessageId": "TaskEvent.1.0.TaskStarted",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has changed to progress 20 percent complete.",
      "MessageArgs": [
        "0",
        "20"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has changed to progress 50 percent complete.",
      "MessageArgs": [
        "0",
        "50"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has changed to progress 80 percent complete.",
      "MessageArgs": [
        "0",
        "80"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has changed to progress 100 percent complete.",
      "MessageArgs": [
        "0",
        "100"
      ],
      "MessageId": "TaskEvent.1.0.TaskProgressChanged",
      "MessageSeverity": "OK",
      "Resolution": "None."
    },
    {
      "@odata.type": "#Message.v1_1_1.Message",
      "Message": "The task with Id '0' has completed.",
      "MessageArgs": [
        "0"
      ],
      "MessageId": "TaskEvent.1.0.TaskCompletedOK",
      "MessageSeverity": "OK",
      "Resolution": "None."
    }
  ],
  "Name": "Task 0",
  "Payload": {
    "HttpHeaders": [],
    "HttpOperation": "POST",
    "JsonBody": "null",
    "TargetUri": "/redfish/v1/UpdateService/update-multipart"
  },
  "PercentComplete": 100,
  "StartTime": "2025-07-29T08:51:41+00:00",
  "TaskMonitor": "/redfish/v1/TaskService/TaskMonitors/0",
  "TaskState": "Completed",
  "TaskStatus": "OK"
}
```

6. Display the fw inventory with newly updated fw.
```
curl --silent $creds https://$bmc/redfish/v1/UpdateService/FirmwareInventory
```

```
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory",
  "@odata.type": "#SoftwareInventoryCollection.SoftwareInventoryCollection",
  "Members": [
    {...},
    {
      "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_4228"
    },
    {
      "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_5453"
    },
    {...}
  ],
  "Members@odata.count": 14,
  "Name": "Software Inventory Collection"
}
```

7. Query the new fw version.
```
curl $creds https://$bmc/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_5453
```

```
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_5453",
  "@odata.type": "#SoftwareInventory.v1_1_0.SoftwareInventory",
  "Description": "Unknown image",
  "Id": "Santabarbara_MB_VR_CPU1_5453",
  "Name": "Software Inventory",
  "Status": {
    "Health": "OK",
    "HealthRollup": "OK",
    "State": "Enabled"
  },
  "Updateable": false,
  "Version": "42C3"
}
```

8. Do AC cycle to make sure the new fw is applied.
```
busctl set-property xyz.openbmc_project.State.Chassis0 /xyz/openbmc_project/state/chassis0 \
xyz.openbmc_project.State.Chassis RequestedPowerTransition s xyz.openbmc_project.State.Chassis.Transition.PowerCycle
```

9. Display the fw inventory after AC cycle.
```
curl --silent $creds https://$bmc/redfish/v1/UpdateService/FirmwareInventory
```

```
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory",
  "@odata.type": "#SoftwareInventoryCollection.SoftwareInventoryCollection",
  "Members": [
    {...},
    {
      "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_2249"
    },
    {...}
  ],
  "Members@odata.count": 13,
  "Name": "Software Inventory Collection"
}
```

10. Query the fw version after AC cycle.
```
curl $creds https://$bmc/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_2249
```

```
{
  "@odata.id": "/redfish/v1/UpdateService/FirmwareInventory/Santabarbara_MB_VR_CPU1_2249",
  "@odata.type": "#SoftwareInventory.v1_1_0.SoftwareInventory",
  "Description": "Unknown image",
  "Id": "Santabarbara_MB_VR_CPU1_2249",
  "Name": "Software Inventory",
  "Status": {
    "Health": "Warning",
    "HealthRollup": "OK",
    "State": "Disabled"
  },
  "Updateable": true,
  "Version": "42C3"
}
```

Change-Id: Ifa6fdd79f5d087adb6037e0043897fa6199ae097
Signed-off-by: Kevin Tung <Kevin.Tung@quantatw.com>
diff --git a/i2c-vr/mps/mps.hpp b/i2c-vr/mps/mps.hpp
new file mode 100644
index 0000000..da6f854
--- /dev/null
+++ b/i2c-vr/mps/mps.hpp
@@ -0,0 +1,290 @@
+#pragma once
+
+#include "common/include/i2c/i2c.hpp"
+#include "i2c-vr/vr.hpp"
+
+#include <phosphor-logging/lg2.hpp>
+
+#include <cstdint>
+#include <iterator>
+#include <memory>
+#include <string_view>
+#include <vector>
+
+namespace phosphor::software::VR
+{
+
+/**
+ * @brief
+ * Columns of an Automated Test Equipment (ATE) format configuration file.
+ * Each enumerator corresponds to a tab-separated column index.
+ */
+enum class ATE : uint8_t
+{
+    configId = 0,
+    pageNum,
+    regAddrHex,
+    regAddrDec,
+    regName,
+    regDataHex,
+    regDataDec,
+    writeType,
+    colCount,
+};
+
+enum class MPSPage : uint8_t
+{
+    page0 = 0,
+    page1,
+    page2,
+    page3,
+    page4,
+};
+
+struct MPSData
+{
+    uint8_t page = 0;
+    uint8_t addr = 0;
+    uint8_t length = 0;
+    std::array<uint8_t, 4> data{};
+};
+
+struct MPSConfig
+{
+    uint32_t vendorId = 0;
+    uint32_t productId = 0;
+    uint32_t configId = 0;
+    uint32_t crcUser = 0;
+    uint32_t crcMulti = 0;
+    std::vector<MPSData> registersData;
+};
+
+/**
+ * @brief
+ * Utility class to iterate over lines and tokenize them by tab characters.
+ */
+class TokenizedLines
+{
+  public:
+    TokenizedLines(const uint8_t* d, size_t s) :
+        data(reinterpret_cast<const char*>(d), s)
+    {}
+    /**
+     * @brief Iterator over tokenized lines.
+     */
+    struct Iterator
+    {
+        using iterator_category = std::forward_iterator_tag;
+        using value_type = std::vector<std::string_view>;
+        using difference_type = std::ptrdiff_t;
+        using pointer = const value_type*;
+        using reference = const value_type&;
+
+        Iterator() = default; // End iterator
+        Iterator(std::string_view sv) : remaining(sv)
+        {
+            next();
+        }
+
+        reference operator*() const
+        {
+            return currentTokens;
+        }
+
+        pointer operator->() const
+        {
+            return &currentTokens;
+        }
+
+        Iterator& operator++()
+        {
+            next();
+            return *this;
+        }
+
+        Iterator operator++(int)
+        {
+            auto result = *this;
+            ++(*this);
+            return result;
+        }
+
+        friend bool operator==(const Iterator& a, const Iterator& b)
+        {
+            return a.remaining.empty() && b.remaining.empty();
+        }
+
+        friend bool operator!=(const Iterator& a, const Iterator& b)
+        {
+            return !(a == b);
+        }
+
+      private:
+        std::string_view remaining;
+        std::vector<std::string_view> currentTokens;
+
+        void next()
+        {
+            currentTokens.clear();
+            if (remaining.empty())
+            {
+                return;
+            }
+
+            // Extract current line
+            auto newlinePos = remaining.find('\n');
+            std::string_view line = remaining.substr(0, newlinePos);
+            remaining = (newlinePos == std::string_view::npos)
+                            ? std::string_view{}
+                            : remaining.substr(newlinePos + 1);
+
+            // Tokenize by tab
+            size_t start = 0;
+            while (start < line.size())
+            {
+                start = line.find_first_not_of('\t', start);
+                if (start == std::string_view::npos)
+                {
+                    break;
+                }
+
+                auto end = line.find('\t', start);
+                currentTokens.emplace_back(line.substr(start, end - start));
+                start = (end == std::string_view::npos) ? line.size() : end;
+            }
+        }
+    };
+
+    Iterator begin() const
+    {
+        return Iterator(data);
+    }
+
+    static Iterator end()
+    {
+        return Iterator();
+    }
+
+  private:
+    std::string_view data;
+};
+
+/**
+ * @brief Base parser for MPS configuration images.
+ */
+class MPSImageParser
+{
+  public:
+    MPSImageParser(const uint8_t* image, size_t imageSize) :
+        lineTokens(image, imageSize)
+    {}
+
+    template <typename>
+    inline static constexpr bool always_false = false;
+
+    /**
+     * @brief Extract a typed value from a tokenized line.
+     * @tparam T Return type (string or integral type)
+     * @param tokens Tokenized line
+     * @param index Column index (ATE enum)
+     * @return Parsed value or default if invalid
+     */
+    template <typename T>
+    T getVal(const std::vector<std::string_view>& tokens, ATE index)
+    {
+        size_t idx = static_cast<size_t>(index);
+
+        if (tokens.size() <= idx)
+        {
+            lg2::error("Index out of range for ATE enum: {INDEX}", "INDEX",
+                       static_cast<uint32_t>(idx));
+            return T{};
+        }
+
+        std::string_view token = tokens[idx];
+
+        if constexpr (std::is_same_v<T, std::string>)
+        {
+            return std::string(token);
+        }
+        else if constexpr (std::is_integral_v<T>)
+        {
+            unsigned long val = 0;
+            try
+            {
+                val = std::stoul(std::string(token), nullptr, 16);
+            }
+            catch (...)
+            {
+                lg2::error("Invalid hex value: {INDEX}", "INDEX",
+                           static_cast<uint32_t>(idx));
+                return T{};
+            }
+            return static_cast<T>(val);
+        }
+        else
+        {
+            static_assert(always_false<T>, "Unsupported type in getVal");
+        }
+    }
+
+    /**
+     * @brief Check if a tokenized line contains valid register data.
+     */
+    static bool isValidDataTokens(const std::vector<std::string_view>& tokens);
+
+    /**
+     * @brief Convert tokenized line into MPSData structure.
+     */
+    MPSData extractData(const std::vector<std::string_view>& tokens);
+
+    /**
+     * @brief Collect all register data entries from the parsed image.
+     */
+    std::vector<MPSData> getRegistersData();
+
+    TokenizedLines lineTokens;
+};
+
+/**
+ * @brief Base class for MPS Voltage Regulators.
+ */
+class MPSVoltageRegulator : public VoltageRegulator
+{
+  public:
+    MPSVoltageRegulator(sdbusplus::async::context& ctx, uint16_t bus,
+                        uint16_t address) :
+        VoltageRegulator(ctx), i2cInterface(phosphor::i2c::I2C(bus, address))
+    {}
+
+    /**
+     * @brief Parse device-specific configuration from the loaded image.
+     * @return async task returning true if parsing succeeds
+     */
+    virtual sdbusplus::async::task<bool> parseDeviceConfiguration() = 0;
+
+    /**
+     * @brief Parse an image file into internal MPS configuration.
+     * @param image Pointer to the image data
+     * @param imageSize Size of the image data
+     * @return async task returning true if parsing succeeds
+     */
+    sdbusplus::async::task<bool> parseImage(const uint8_t* image,
+                                            size_t imageSize);
+
+    /**
+     * @brief Group register data by page, optionally masked and shifted.
+     * @param configMask Bitmask to select relevant page bits (default 0xFF)
+     * @param shift Number of bits to shift masked value to obtain group key
+     * @return map of page keys to vector of MPSData
+     */
+    std::map<uint8_t, std::vector<MPSData>> getGroupedConfigData(
+        uint8_t configMask = 0xFF, uint8_t shift = 0);
+
+  protected:
+    phosphor::i2c::I2C i2cInterface;
+    std::unique_ptr<MPSImageParser> parser;
+    std::unique_ptr<MPSConfig> configuration;
+};
+
+} // namespace phosphor::software::VR