summaryrefslogtreecommitdiff
path: root/meta-openbmc-mods/meta-common/recipes-phosphor/flash/phosphor-software-manager/0016-Process-PLDM-image-type.patch
blob: bc94f00afadefdc29a169bddc2662a355ea54674 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
From a78b7a609f58ac82623c357426ef0590d6d76971 Mon Sep 17 00:00:00 2001
From: Ayushi Smriti <smriti.ayushi@intel.com>
Date: Mon, 9 Nov 2020 23:04:58 +0530
Subject: [PATCH] Process PLDM image type

This change is to check whether the image uploaded is of PLDM image
type based on the PackageHeaderIdentifier check which is a 16 bytes
uuid field in the pldm package header.

Also, determine image purpose and version.
Purpose is set to pldm enum type and for version, PackageVersionString
is concluded based on PackageVersionStringLength value.

Tested:
- On uploading a pldm image through Redfish. Uuid is identified and
matched correctly.
- Purpose and version is given to the image as expected and activation
intf got added.
  - verified same with busctl cmd on xyz.openbmc_project.Software.Version
    and xyz.openbmc_project.Software.BMC.Updater
- Verified the regular PFR update procedure works
  - received expected redfish response from postman
  - verified fwupd.sh script is reached

Signed-off-by: Ayushi Smriti <smriti.ayushi@intel.com>
---
 item_updater.cpp      |  4 +-
 pfr_image_manager.cpp | 95 +++++++++++++++++++++++++++++++++++++++++--
 pfr_image_manager.hpp |  6 +--
 pldm.hpp              | 21 ++++++++++
 4 files changed, 119 insertions(+), 7 deletions(-)
 create mode 100644 pldm.hpp

diff --git a/item_updater.cpp b/item_updater.cpp
index db255d6..7af80e3 100644
--- a/item_updater.cpp
+++ b/item_updater.cpp
@@ -67,6 +67,7 @@ void ItemUpdater::createActivation(sdbusplus::message::message& msg)
 #if defined(HOST_BIOS_UPGRADE) || defined(PFR_UPDATE)
                         value == VersionPurpose::Host ||
 #endif
+                        value == VersionPurpose::PLDM ||
                         value == VersionPurpose::Other)
                     {
                         purpose = value;
@@ -397,7 +398,8 @@ void ItemUpdater::deleteAll()
 }
 
 ItemUpdater::ActivationStatus
-    ItemUpdater::validateSquashFSImage(const std::string& filePath)
+    ItemUpdater::validateSquashFSImage(__attribute__((unused))
+                                       const std::string& filePath)
 {
 #ifndef PFR_UPDATE
     bool valid = true;
diff --git a/pfr_image_manager.cpp b/pfr_image_manager.cpp
index 178367f..c923494 100644
--- a/pfr_image_manager.cpp
+++ b/pfr_image_manager.cpp
@@ -3,6 +3,7 @@
 #include "pfr_image_manager.hpp"
 
 #include "dbus_helpers.hpp"
+#include "pldm.hpp"
 #include "version.hpp"
 #include "watch.hpp"
 
@@ -44,9 +45,10 @@ using BusyFail = Software::Image::BusyFailure;
 static constexpr const uint32_t pfmPos = 2054;
 static constexpr const uint32_t block0Magic = 0xB6EAFD19;
 static constexpr const uint32_t lengthBlk0Blk1 = 1024;
+static constexpr const uint32_t pldmMagic = 0xF018878C;
 
-int Manager::verifyPFRImage(const std::filesystem::path imgPath,
-                            std::string& version, std::string& purposeString)
+int Manager::verifyImage(const std::filesystem::path imgPath,
+                         std::string& version, std::string& purposeString)
 {
     uint8_t imgType = 0;
     uint32_t imgMagic = 0;
@@ -76,6 +78,93 @@ int Manager::verifyPFRImage(const std::filesystem::path imgPath,
 
             imgMagic = block0Data.tag;
 
+            if (htobe32(imgMagic) == pldmMagic)
+            {
+                if (!version.empty())
+                {
+                    version.clear();
+                }
+
+                imgFile.seekg(0, std::ios_base::end);
+
+                const size_t length = imgFile.tellg();
+                constexpr size_t readBytes = 36;
+
+                if (length < readBytes)
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "Insufficient file length to read the required "
+                        "bytes");
+                    return -1;
+                }
+
+                imgFile.seekg(0, std::ios::beg);
+
+                std::array<char, readBytes> buffer = {};
+
+                imgFile.read(
+                    buffer.data(),
+                    buffer.size()); // read 36 bytes of PLDM Package Header
+
+                if (!imgFile.good())
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "Image file read is not successful");
+                    return -1;
+                }
+
+                if (!std::equal(buffer.begin(),
+                                buffer.begin() + pldm::headerIdLen,
+                                pldm::pldmPkgHeaderId
+                                    .begin())) // comparing 16 bytes of
+                                               // PackageHeaderIdentifier field
+                {
+                    std::string redfishMsgID =
+                        "OpenBMC.0.1.FirmwareUpdateFailed";
+                    sd_journal_send(
+                        "MESSAGE=%s", "Firmware image verification failed",
+                        "PRIORITY=%i", LOG_ERR, "REDFISH_MESSAGE_ID=%s",
+                        redfishMsgID.c_str(), "REDFISH_MESSAGE_ARGS=%s",
+                        "PLDM Image package header identifier check fail",
+                        NULL);
+
+                    return -1;
+                }
+
+                phosphor::logging::log<phosphor::logging::level::INFO>(
+                    "Package header identifier matched");
+                purposeString =
+                    "xyz.openbmc_project.Software.Version.VersionPurpose.PLDM";
+
+                const uint8_t pkgVerStrLen = static_cast<uint8_t>(
+                    buffer[35]); // PackageVersionStringLen byte
+
+                imgFile.seekg(readBytes,
+                              std::ios::beg); // point to the begin of
+                                              // PackageVersionString field
+                                              // i.e. 36th pos
+
+                std::array<char, 255> ver = {};
+                imgFile.read(ver.data(),
+                             pkgVerStrLen); // read PackageVersionString bytes
+
+                if (!imgFile.good())
+                {
+                    phosphor::logging::log<phosphor::logging::level::ERR>(
+                        "Image file read is not successful");
+                    return -1;
+                }
+
+                version.assign(ver.data(), pkgVerStrLen);
+                phosphor::logging::log<phosphor::logging::level::INFO>(
+                    "Package version string value",
+                    phosphor::logging::entry("IMAGE_VERSION=%s",
+                                             version.c_str()));
+
+                imgFile.close();
+                return 0;
+            }
+
             if (imgMagic != block0Magic)
             {
                 phosphor::logging::log<phosphor::logging::level::ERR>(
@@ -226,7 +315,7 @@ int Manager::processImage(const std::string& imgFilePath)
     std::string ver;
     std::string purposeString;
 
-    if (0 != verifyPFRImage(imgFilePath, ver, purposeString))
+    if (0 != verifyImage(imgFilePath, ver, purposeString))
     {
         phosphor::logging::log<phosphor::logging::level::ERR>(
             "Error verifying uploaded image");
diff --git a/pfr_image_manager.hpp b/pfr_image_manager.hpp
index 3591f1a..2facfe6 100644
--- a/pfr_image_manager.hpp
+++ b/pfr_image_manager.hpp
@@ -156,13 +156,13 @@ class Manager
     CustomMap mapFile(const std::filesystem::path& path, size_t size);
 
     /**
-     * @brief Verify the PFR image and return version and purpose
+     * @brief Verify the uploaded image type and return version and purpose
      * @param[in]  - file path
      * @param[out]  - version
      * @param[out]  - purpose
      */
-    int verifyPFRImage(const std::filesystem::path imgPath,
-                       std::string& version, std::string& purposeString);
+    int verifyImage(const std::filesystem::path imgPath, std::string& version,
+                    std::string& purposeString);
     /** @brief Persistent map of Version dbus objects and their
      * version id */
     std::map<std::string, std::unique_ptr<Version>> versions;
diff --git a/pldm.hpp b/pldm.hpp
new file mode 100644
index 0000000..edbd6ae
--- /dev/null
+++ b/pldm.hpp
@@ -0,0 +1,21 @@
+namespace pldm
+{
+
+struct PldmPkgHeader
+{
+    uint8_t uuid[16];      // PackageHeaderIdentifier
+    uint8_t formatRev;     // PackageHeaderFormatRevision
+    uint16_t headerSize;   // PackageHeaderSize
+    uint8_t timestamp[13]; // PackageReleaseDateTime
+    uint16_t bitmapLen;    // ComponentBitmapBitLength
+    uint8_t verStringType; // PackageVersionStringType
+    uint8_t verStringLen;  // PackageVersionStringLength
+} __attribute__((packed));
+
+constexpr size_t headerIdLen = 16;
+
+const std::array<char, headerIdLen> pldmPkgHeaderId = {
+    0xF0, 0x18, 0x87, 0x8C, 0xCB, 0x7D, 0x49, 0x43,
+    0x98, 0x00, 0xA0, 0x2F, 0x05, 0x9A, 0xCA, 0x02}; // 16 bytes package header
+                                                     // identifier uuid
+} // namespace pldm
-- 
2.17.1