1From a193c5ce59758ed5971b5bd7494f1aaf3489ed9d Mon Sep 17 00:00:00 2001
2From: Alexander Amelkin <a.amelkin@yadro.com>
3Date: Mon, 8 Apr 2019 17:58:42 +0300
4Subject: [PATCH] Add support for boot initiator mailbox
5
6Add handlers to process the chassis system option 7
7(boot initiator mailbox). The format of mailbox is
8specific to the machine/bootloader. This commit only
9adds generic handlers to process getting and setting
10of the mailbox data regardless of the content.
11
12Only the IANA Enterprise number is checked in the data
13block 0. Also checked are the data boundaries.
14
15It is expected that a machine-specific override for
16phosphor-settingsd sets the supported state and
17the IANA number according to the used bootloader.
18
19Resolves openbmc/openbmc#3391
20
21Change-Id: Iccbf74c0775f20c70e8deaa7b0a8bd995ebbffea
22Signed-off-by: Alexander Amelkin <a.amelkin@yadro.com>
23Signed-off-by: Ivan Mikhaylov <i.mikhaylov@yadro.com>
24
25---
26 chassishandler.cpp | 329 ++++++++++++++++++++++++++++++++++++++++++++-
27 chassishandler.hpp |   1 +
28 2 files changed, 326 insertions(+), 4 deletions(-)
29
30diff --git a/chassishandler.cpp b/chassishandler.cpp
31index fb3d644..7f92b85 100644
32--- a/chassishandler.cpp
33+++ b/chassishandler.cpp
34@@ -131,6 +131,7 @@ namespace internal
35 {
36
37 constexpr auto bootModeIntf = "xyz.openbmc_project.Control.Boot.Mode";
38+constexpr auto bootMboxIntf = "xyz.openbmc_project.Control.Boot.Mailbox";
39 constexpr auto bootTypeIntf = "xyz.openbmc_project.Control.Boot.Type";
40 constexpr auto bootSourceIntf = "xyz.openbmc_project.Control.Boot.Source";
41 constexpr auto powerRestoreIntf =
42@@ -147,8 +148,9 @@ settings::Objects& getObjects()
43     if (objectsPtr == nullptr)
44     {
45         objectsPtr = std::make_unique<settings::Objects>(
46-            dbus, std::vector<std::string>{bootModeIntf, bootTypeIntf,
47-                                           bootSourceIntf, powerRestoreIntf});
48+            dbus,
49+            std::vector<std::string>{bootMboxIntf, bootModeIntf, bootTypeIntf,
50+                                     bootSourceIntf, powerRestoreIntf});
51     }
52     return *objectsPtr;
53 }
54@@ -1708,6 +1710,127 @@ static ipmi::Cc setBootType(ipmi::Context::ptr& ctx, const Type::Types& type)
55     return ipmi::ccSuccess;
56 }
57
58+using MboxVec = std::vector<uint8_t>;
59+
60+// Check if Boot Mailbox is supported.
61+static std::optional<bool> isBootMboxSupported()
62+{
63+    using namespace chassis::internal;
64+    using namespace chassis::internal::cache;
65+
66+    try
67+    {
68+        settings::Objects& objects = getObjects();
69+        auto bootMbox = settings::boot::setting(objects, bootMboxIntf);
70+        const auto& bootMboxSetting = std::get<settings::Path>(bootMbox);
71+        auto method = dbus.new_method_call(
72+            objects.service(bootMboxSetting, bootMboxIntf).c_str(),
73+            bootMboxSetting.c_str(), ipmi::PROP_INTF, "Get");
74+
75+        method.append(bootMboxIntf, "Supported");
76+        auto reply = dbus.call(method);
77+        std::variant<bool> result;
78+        reply.read(result);
79+        return std::get<bool>(result);
80+    }
81+    catch (const std::exception& e)
82+    {
83+        log<level::ERR>("Error getting Boot/Mailbox/Supported",
84+                        entry("ERROR=%s", e.what()));
85+        report<InternalFailure>();
86+        return std::nullopt;
87+    }
88+}
89+
90+static std::optional<uint24_t> getBootMboxIANA()
91+{
92+    using namespace chassis::internal;
93+    using namespace chassis::internal::cache;
94+
95+    try
96+    {
97+        settings::Objects& objects = getObjects();
98+        auto bootMbox = settings::boot::setting(objects, bootMboxIntf);
99+        const auto& bootMboxSetting = std::get<settings::Path>(bootMbox);
100+        auto method = dbus.new_method_call(
101+            objects.service(bootMboxSetting, bootMboxIntf).c_str(),
102+            bootMboxSetting.c_str(), ipmi::PROP_INTF, "Get");
103+
104+        method.append(bootMboxIntf, "IANAEnterpriseNumber");
105+        auto reply = dbus.call(method);
106+        std::variant<uint32_t> result;
107+        reply.read(result);
108+        return std::get<uint32_t>(result);
109+    }
110+    catch (const std::exception& e)
111+    {
112+        log<level::ERR>("Error getting Boot/Mailbox/IANAEnterpriseNumber",
113+                        entry("ERROR=%s", e.what()));
114+        report<InternalFailure>();
115+        return std::nullopt;
116+    }
117+}
118+
119+static std::optional<MboxVec> getBootMbox()
120+{
121+    using namespace chassis::internal;
122+    using namespace chassis::internal::cache;
123+
124+    try
125+    {
126+        settings::Objects& objects = getObjects();
127+        auto bootMbox = settings::boot::setting(objects, bootMboxIntf);
128+        const auto& bootMboxSetting = std::get<settings::Path>(bootMbox);
129+        auto method = dbus.new_method_call(
130+            objects.service(bootMboxSetting, bootMboxIntf).c_str(),
131+            bootMboxSetting.c_str(), ipmi::PROP_INTF, "Get");
132+
133+        method.append(bootMboxIntf, "Data");
134+        auto reply = dbus.call(method);
135+        std::variant<MboxVec> result;
136+        reply.read(result);
137+        return std::get<MboxVec>(result);
138+    }
139+    catch (const std::exception& e)
140+    {
141+        log<level::ERR>("Error getting Boot/Mailbox/Data",
142+                        entry("ERROR=%s", e.what()));
143+        report<InternalFailure>();
144+        return std::nullopt;
145+    }
146+}
147+
148+static bool setBootMbox(MboxVec data)
149+{
150+    using namespace chassis::internal;
151+    using namespace chassis::internal::cache;
152+
153+    try
154+    {
155+        settings::Objects& objects = getObjects();
156+        std::variant<MboxVec> property(data);
157+        auto bootMbox = settings::boot::setting(objects, bootMboxIntf);
158+        const auto& bootMboxSetting = std::get<settings::Path>(bootMbox);
159+        auto method = dbus.new_method_call(
160+            objects.service(bootMboxSetting, bootMboxIntf).c_str(),
161+            bootMboxSetting.c_str(), ipmi::PROP_INTF, "Set");
162+
163+        method.append(bootMboxIntf, "Data", property);
164+        dbus.call(method);
165+        return true;
166+    }
167+    catch (const std::exception& e)
168+    {
169+        log<level::ERR>("Error setting Boot/Mailbox/Data",
170+                        entry("ERROR=%s", e.what()));
171+        report<InternalFailure>();
172+        return false;
173+    }
174+}
175+
176+static constexpr size_t normalBlockSize = 16;
177+static constexpr size_t IANAEnterpriseLength = 3;
178+
179 static constexpr uint8_t setComplete = 0x0;
180 static constexpr uint8_t setInProgress = 0x1;
181 static uint8_t transferStatus = setComplete;
182@@ -1882,6 +2005,87 @@ ipmi::RspType<ipmi::message::Payload>
183             return ipmi::responseUnspecifiedError();
184         }
185     }
186+    else if (types::enum_cast<BootOptionParameter>(bootOptionParameter) ==
187+             BootOptionParameter::bootInitiatorMbox)
188+    {
189+        // Only allow reading the boot initiator mailbox if Mailbox is supported
190+        //
191+        // Algorithm:
192+        // 1. Get 'Supported' property from the Control.Boot.Mailbox interface
193+        // 2. If {1} is 'false', report Parameter not supported (0x80)
194+        // 3. Get Block Selector from request
195+        // 4. Get 'Data' vector from Control.Boot.Mailbox
196+        // 5. If requested block {3} exceeds total vector size {4},
197+        //    report Out of space (0xC4)
198+        // 6. Return the selected block (16 bytes) from the vector
199+        try
200+        {
201+            // Check whether this option is supported
202+            std::optional<bool> isSupported = isBootMboxSupported();
203+            if (!isSupported)
204+            {
205+                return ipmi::responseUnspecifiedError();
206+            }
207+
208+            if (!*isSupported)
209+            {
210+                log<level::INFO>("Attempt to read unsupported Boot/Mailbox");
211+                return ipmi::responseParmNotSupported();
212+            }
213+
214+            // Initially assume it's block 1+
215+            std::optional<uint24_t> IANAEnterprise;
216+            size_t blockDataSize = normalBlockSize;
217+            size_t dataVecStartOffset =
218+                setSelector * normalBlockSize - IANAEnterpriseLength;
219+
220+            response.pack(bootOptionParameter, reserved1, setSelector);
221+
222+            // Adjust pointers and sizes for block 0, and fill in the IANA PEN
223+            if (0 == setSelector)
224+            {
225+                IANAEnterprise = getBootMboxIANA();
226+                if (!IANAEnterprise)
227+                {
228+                    return ipmi::responseInvalidCommand();
229+                }
230+
231+                blockDataSize = normalBlockSize - IANAEnterpriseLength;
232+                dataVecStartOffset = 0;
233+
234+                response.pack(*IANAEnterprise);
235+            }
236+
237+            // Get the total data size
238+            std::optional<MboxVec> dataVec = getBootMbox();
239+            if (!dataVec)
240+            {
241+                return ipmi::responseInvalidCommand();
242+            }
243+
244+            if ((*dataVec).size() < dataVecStartOffset + blockDataSize)
245+            {
246+                size_t totalSize = (*dataVec).size() + IANAEnterpriseLength;
247+                log<level::ERR>(
248+                    "Attempt to read unsupported block",
249+                    entry("REQUESTED_BLOCK=%d", setSelector),
250+                    entry("MAX_BLOCK=%d", totalSize / normalBlockSize));
251+                return ipmi::responseParmOutOfRange();
252+            }
253+
254+            // Copy the data to response from specified offset in d-bus vector
255+            response.append((*dataVec).data() + dataVecStartOffset,
256+                            (*dataVec).data() + dataVecStartOffset +
257+                                blockDataSize);
258+
259+            return ipmi::responseSuccess(std::move(response));
260+        }
261+        catch (InternalFailure& e)
262+        {
263+            report<InternalFailure>();
264+            return ipmi::responseUnspecifiedError();
265+        }
266+    }
267     else
268     {
269         if ((bootOptionParameter >= oemParmStart) &&
270@@ -1946,9 +2150,8 @@ ipmi::RspType<> ipmiChassisSetSysBootOptions(ipmi::Context::ptr ctx,
271         return ipmi::responseSuccess();
272     }
273
274-    /*  000101
275+    /*
276      * Parameter #5 means boot flags. Please refer to 28.13 of ipmi doc.
277-     * This is the only parameter used by petitboot.
278      */
279
280     if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
281@@ -2084,6 +2287,124 @@ ipmi::RspType<> ipmiChassisSetSysBootOptions(ipmi::Context::ptr ctx,
282             return ipmi::responseUnspecifiedError();
283         }
284     }
285+    else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
286+             BootOptionParameter::bootInitiatorMbox)
287+    {
288+        // Only allow writing to boot initiator mailbox if:
289+        // 1. Mailbox is supported
290+        // 2. IANA PEN matches.
291+        //
292+        // Algorithm:
293+        // 1. Get 'Supported' property from Control.Boot.Mailbox interface
294+        // 2. If {1} is 'false', report Parameter not supported (0x80)
295+        // 3. Get Block Selector from request
296+        // 4. Get 'Data' array from Control.Boot.Mailbox
297+        // 5. If requested block {3} exceeds total vector size {4},
298+        //    report Out of range (0xC9)
299+        // 6. If requsted block {3} is 0:
300+        //    4.1. Get IANA PEN from request
301+        //    4.2. Get 'IANAEnterpriseNumber' property from Control.Boot.Mailbox
302+        //    4.3. If {4.1} doesn't match {4.2}, report 0xCC error (Invalid
303+        //         data field in request)
304+        // 7. Overwrite the 16 bytes at offset {3}*16 with the data from request
305+        // 8. Update the 'Data' array in Control.Boot.Mailbox
306+
307+        try
308+        {
309+            std::optional<bool> isSupported = isBootMboxSupported();
310+            if (!isSupported)
311+            {
312+                return ipmi::responseUnspecifiedError();
313+            }
314+
315+            if (!*isSupported)
316+            {
317+                log<level::INFO>("Attempt to read unsupported Boot/Mailbox");
318+                return ipmi::responseParmNotSupported();
319+            }
320+
321+            // Requested block
322+            uint8_t reqBlock;
323+            if (data.unpack(reqBlock) != 0)
324+            {
325+                return ipmi::responseReqDataLenInvalid();
326+            }
327+
328+            // Initially assume it's blcok 1+
329+            uint24_t reqIANAEnterprise;
330+            std::vector<uint8_t> blockData(normalBlockSize);
331+            size_t dataVecStartOffset =
332+                reqBlock * normalBlockSize - IANAEnterpriseLength;
333+
334+            // Adjust pointers and sizes for block 0, and fill in the IANA PEN
335+            if (0 == reqBlock)
336+            {
337+                if (data.unpack(reqIANAEnterprise) != 0)
338+                {
339+                    return ipmi::responseReqDataLenInvalid();
340+                }
341+
342+                std::optional<uint24_t> IANAEnterprise = getBootMboxIANA();
343+                if (!IANAEnterprise)
344+                {
345+                    return ipmi::responseInvalidCommand();
346+                }
347+
348+                if (*IANAEnterprise != reqIANAEnterprise)
349+                {
350+                    log<level::ERR>(
351+                        "Unsupported IANA Enterprise number",
352+                        entry("REQUESTED_IANA=%d",
353+                              static_cast<uint32_t>(reqIANAEnterprise)),
354+                        entry("SUPPORTED_IANA=%d",
355+                              static_cast<uint32_t>(*IANAEnterprise)));
356+                    return ipmi::responseInvalidFieldRequest();
357+                }
358+
359+                // For block 0 operate on data after IANA PEN
360+                blockData.resize(normalBlockSize - IANAEnterpriseLength);
361+                dataVecStartOffset = 0;
362+            }
363+
364+            // Get the data vector from d-bus
365+            std::optional<MboxVec> dataVec = getBootMbox();
366+            if (!dataVec)
367+            {
368+                return ipmi::responseInvalidCommand();
369+            }
370+
371+            // Does the requested block exist?
372+            if ((*dataVec).size() < dataVecStartOffset + blockData.size())
373+            {
374+                size_t totalSize = (*dataVec).size() + IANAEnterpriseLength;
375+                log<level::ERR>(
376+                    "Attempt to read unsupported block",
377+                    entry("REQUESTED_BLOCK=%d", reqBlock),
378+                    entry("MAX_BLOCK=%d", totalSize / normalBlockSize));
379+                return ipmi::responseParmOutOfRange();
380+            }
381+
382+            if (data.unpack(blockData) != 0 || !data.fullyUnpacked())
383+            {
384+                return ipmi::responseReqDataLenInvalid();
385+            }
386+
387+            // Copy the data from request to specified offset in d-bus vector
388+            for (size_t i = 0; i < blockData.size(); ++i)
389+            {
390+                (*dataVec)[dataVecStartOffset + i] = blockData[i];
391+            }
392+            if (!setBootMbox(*dataVec))
393+            {
394+                return ipmi::responseUnspecifiedError();
395+            }
396+        }
397+        catch (InternalFailure& e)
398+        {
399+            report<InternalFailure>();
400+            return ipmi::responseUnspecifiedError();
401+        }
402+    }
403     else if (types::enum_cast<BootOptionParameter>(parameterSelector) ==
404              BootOptionParameter::bootInfo)
405     {
406diff --git a/chassishandler.hpp b/chassishandler.hpp
407index 2957127..a783bff 100644
408--- a/chassishandler.hpp
409+++ b/chassishandler.hpp
410@@ -51,6 +51,7 @@ enum class BootOptionParameter : size_t
411     bootFlagValidClr = 0x3,
412     bootInfo = 0x4,
413     bootFlags = 0x5,
414+    bootInitiatorMbox = 0x07,
415     opalNetworkSettings = 0x61
416 };
417
418