xref: /openbmc/phosphor-ipmi-blobs/main.cpp (revision aceb4baa)
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 
22 #include <host-ipmid/ipmid-api.h>
23 
24 #include <cstdio>
25 #include <host-ipmid/iana.hpp>
26 #include <host-ipmid/oemrouter.hpp>
27 #include <memory>
28 
29 #if ENABLE_EXAMPLE
30 #include "example/example.hpp"
31 #endif
32 
33 /* TODO: Swap out once https://gerrit.openbmc-project.xyz/12743 is merged */
34 namespace oem
35 {
36 constexpr auto blobTransferCmd = 128;
37 } // namespace oem
38 
39 namespace blobs
40 {
41 
42 static std::unique_ptr<BlobManager> manager;
43 
44 static ipmi_ret_t handleBlobCommand(ipmi_cmd_t cmd, const uint8_t* reqBuf,
45                                     uint8_t* replyCmdBuf, size_t* dataLen)
46 {
47     /* It's holding at least a sub-command.  The OEN is trimmed from the bytes
48      * before this is called.
49      */
50     if ((*dataLen) < 1)
51     {
52         return IPMI_CC_INVALID;
53     }
54 
55     Crc16 crc;
56     IpmiBlobHandler command =
57         validateBlobCommand(&crc, reqBuf, replyCmdBuf, dataLen);
58     if (command == nullptr)
59     {
60         return IPMI_CC_INVALID;
61     }
62 
63     return processBlobCommand(command, manager.get(), &crc, reqBuf, replyCmdBuf,
64                               dataLen);
65 }
66 
67 void setupBlobGlobalHandler() __attribute__((constructor));
68 
69 void setupBlobGlobalHandler()
70 {
71     oem::Router* oemRouter = oem::mutableRouter();
72     std::fprintf(stderr,
73                  "Registering OEM:[%#08X], Cmd:[%#04X] for Blob Commands\n",
74                  oem::obmcOemNumber, oem::blobTransferCmd);
75 
76     oemRouter->registerHandler(oem::obmcOemNumber, oem::blobTransferCmd,
77                                handleBlobCommand);
78 
79     manager = std::make_unique<BlobManager>();
80 
81 #if ENABLE_EXAMPLE
82     manager->registerHandler(std::move(std::make_unique<ExampleBlobHandler>()));
83 #endif
84 }
85 } // namespace blobs
86