blob: 3cc3fb78a80270702712d8ec656f96eab42d5130 [file] [log] [blame]
Patrick Ventureaccc9172018-07-24 10:51:58 -07001/*
Patrick Venture514f6482018-08-07 14:27:58 -07002 * Copyright 2018 Google Inc.
Patrick Ventureaccc9172018-07-24 10:51:58 -07003 *
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
Patrick Venture54c3b532018-08-01 11:45:49 -070017#include <memory>
18
Patrick Ventureaccc9172018-07-24 10:51:58 -070019#include "host-ipmid/ipmid-api.h"
20#include "host-ipmid/oemrouter.hpp"
21
Patrick Venture8ec019f2018-08-07 11:22:33 -070022#include "config.h"
Patrick Venture3d1786b2018-08-01 11:19:24 -070023#include "flash-ipmi.hpp"
Patrick Venture54c3b532018-08-01 11:45:49 -070024#include "ipmi.hpp"
Patrick Venture3d1786b2018-08-01 11:19:24 -070025
Patrick Venture8ec019f2018-08-07 11:22:33 -070026static constexpr auto stagingPath = STAGING_PATH;
27
Patrick Ventureaccc9172018-07-24 10:51:58 -070028/* TODO: Once OEM IPMI number placement is settled, point to that. */
29namespace oem
30{
31namespace google
32{
33constexpr int number = 11129;
34constexpr int flashOverBTCmd = 127;
35} // namespace google
36} // namespace oem
37
Patrick Venture54c3b532018-08-01 11:45:49 -070038/* We have one instance of the FlashUpdate object tracking commands. */
39std::unique_ptr<FlashUpdate> flashUpdateSingleton;
40
Patrick Ventureaccc9172018-07-24 10:51:58 -070041static ipmi_ret_t flashControl(ipmi_cmd_t cmd, const uint8_t* reqBuf,
42 uint8_t* replyCmdBuf, size_t* dataLen)
43{
Patrick Venture9a5a79a2018-08-03 17:23:57 -070044 size_t requestLength = (*dataLen);
45
Patrick Ventureaccc9172018-07-24 10:51:58 -070046 /* Verify it's at least as long as the shortest message. */
Patrick Venture9a5a79a2018-08-03 17:23:57 -070047 if (requestLength < 1)
Patrick Ventureaccc9172018-07-24 10:51:58 -070048 {
49 return IPMI_CC_INVALID;
50 }
51
Patrick Venturea53a7b32018-08-03 09:15:20 -070052 auto subCmd = static_cast<FlashSubCmds>(reqBuf[0]);
53
54 /* Validate the minimum request length for the command. */
Patrick Venture9a5a79a2018-08-03 17:23:57 -070055 if (!validateRequestLength(subCmd, requestLength))
Patrick Venturea53a7b32018-08-03 09:15:20 -070056 {
57 return IPMI_CC_INVALID;
58 }
Patrick Venture54c3b532018-08-01 11:45:49 -070059
Patrick Venture9a5a79a2018-08-03 17:23:57 -070060 auto handler = getCommandHandler(subCmd);
61 if (handler)
Patrick Venture54c3b532018-08-01 11:45:49 -070062 {
Patrick Venture9a5a79a2018-08-03 17:23:57 -070063 return handler(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
64 dataLen);
Patrick Venture54c3b532018-08-01 11:45:49 -070065 }
66
Patrick Ventureaccc9172018-07-24 10:51:58 -070067 return IPMI_CC_INVALID;
68}
69
70static ipmi_ret_t ipmiFlashControl(ipmi_netfn_t netFn, ipmi_cmd_t cmd,
71 ipmi_request_t request,
72 ipmi_response_t response,
73 ipmi_data_len_t dataLen,
74 ipmi_context_t context)
75{
76 /* request_t, response_t are void*. ipmi_data_len_t is a size_t* */
77 auto reqBuf = static_cast<const uint8_t*>(request);
78 auto replyCmdBuf = static_cast<uint8_t*>(response);
79
80 return flashControl(cmd, reqBuf, replyCmdBuf, dataLen);
81}
82
83void setupGlobalOemFlashControl() __attribute__((constructor));
84
85void setupGlobalOemFlashControl()
86{
Patrick Venture8ec019f2018-08-07 11:22:33 -070087 flashUpdateSingleton = std::make_unique<FlashUpdate>(stagingPath);
Patrick Venture54c3b532018-08-01 11:45:49 -070088
Patrick Ventureaccc9172018-07-24 10:51:58 -070089#ifdef ENABLE_GOOGLE
90 oem::Router* router = oem::mutableRouter();
91
92 fprintf(stderr, "Registering OEM:[%#08X], Cmd:[%#04X] for Flash Update\n",
93 oem::google::number, oem::google::flashOverBTCmd);
94
95 router->registerHandler(oem::google::number, oem::google::flashOverBTCmd,
96 flashControl);
97#endif
98
99 ipmi_register_callback(NETFUN_FIRMWARE, oem::google::flashOverBTCmd, NULL,
100 ipmiFlashControl, PRIVILEGE_USER);
101}