xref: /openbmc/phosphor-ipmi-blobs/main.cpp (revision 2536863d32314250b94794143d6cadaf8e8c86fe)
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "config.h"
18 
19 #include "ipmi.hpp"
20 #include "process.hpp"
21 #include "utils.hpp"
22 
23 #include <host-ipmid/ipmid-api.h>
24 
25 #include <blobs-ipmid/manager.hpp>
26 #include <cstdio>
27 #include <host-ipmid/iana.hpp>
28 #include <host-ipmid/oemopenbmc.hpp>
29 #include <host-ipmid/oemrouter.hpp>
30 #include <memory>
31 #include <phosphor-logging/log.hpp>
32 
33 namespace blobs
34 {
35 
36 using namespace phosphor::logging;
37 
38 static ipmi_ret_t handleBlobCommand(ipmi_cmd_t cmd, const uint8_t* reqBuf,
39                                     uint8_t* replyCmdBuf, size_t* dataLen)
40 {
41     /* It's holding at least a sub-command.  The OEN is trimmed from the bytes
42      * before this is called.
43      */
44     if ((*dataLen) < 1)
45     {
46         return IPMI_CC_REQ_DATA_LEN_INVALID;
47     }
48 
49     /* on failure rc is set to the corresponding IPMI error. */
50     ipmi_ret_t rc = IPMI_CC_OK;
51     Crc16 crc;
52     IpmiBlobHandler command =
53         validateBlobCommand(&crc, reqBuf, replyCmdBuf, dataLen, &rc);
54     if (command == nullptr)
55     {
56         return rc;
57     }
58 
59     return processBlobCommand(command, getBlobManager(), &crc, reqBuf,
60                               replyCmdBuf, dataLen);
61 }
62 
63 /* TODO: this should come from the makefile or recipe... */
64 constexpr auto expectedHandlerPath = "/usr/lib/blob-ipmid";
65 
66 void setupBlobGlobalHandler() __attribute__((constructor));
67 
68 void setupBlobGlobalHandler()
69 {
70     oem::Router* oemRouter = oem::mutableRouter();
71     std::fprintf(stderr,
72                  "Registering OEM:[%#08X], Cmd:[%#04X] for Blob Commands\n",
73                  oem::obmcOemNumber, oem::Cmd::blobTransferCmd);
74 
75     oemRouter->registerHandler(oem::obmcOemNumber, oem::Cmd::blobTransferCmd,
76                                handleBlobCommand);
77 
78     /* Install handlers. */
79     try
80     {
81         loadLibraries(expectedHandlerPath);
82     }
83     catch (const std::exception& e)
84     {
85         log<level::ERR>("ERROR loading blob handlers",
86                         entry("ERROR=%s", e.what()));
87     }
88 }
89 } // namespace blobs
90