blob: 791cb22766bb17c10329cb4c19ce17405c44346c [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;
Patrick Venture6f17bd22018-08-07 13:24:17 -070027static constexpr auto hashPath = HASH_PATH;
Patrick Venture8ec019f2018-08-07 11:22:33 -070028
Patrick Ventureaccc9172018-07-24 10:51:58 -070029/* TODO: Once OEM IPMI number placement is settled, point to that. */
30namespace oem
31{
32namespace google
33{
34constexpr int number = 11129;
35constexpr int flashOverBTCmd = 127;
36} // namespace google
37} // namespace oem
38
Patrick Venture54c3b532018-08-01 11:45:49 -070039/* We have one instance of the FlashUpdate object tracking commands. */
40std::unique_ptr<FlashUpdate> flashUpdateSingleton;
41
Patrick Ventureaccc9172018-07-24 10:51:58 -070042static ipmi_ret_t flashControl(ipmi_cmd_t cmd, const uint8_t* reqBuf,
43 uint8_t* replyCmdBuf, size_t* dataLen)
44{
Patrick Venture9a5a79a2018-08-03 17:23:57 -070045 size_t requestLength = (*dataLen);
46
Patrick Ventureaccc9172018-07-24 10:51:58 -070047 /* Verify it's at least as long as the shortest message. */
Patrick Venture9a5a79a2018-08-03 17:23:57 -070048 if (requestLength < 1)
Patrick Ventureaccc9172018-07-24 10:51:58 -070049 {
50 return IPMI_CC_INVALID;
51 }
52
Patrick Venturea53a7b32018-08-03 09:15:20 -070053 auto subCmd = static_cast<FlashSubCmds>(reqBuf[0]);
54
55 /* Validate the minimum request length for the command. */
Patrick Venture9a5a79a2018-08-03 17:23:57 -070056 if (!validateRequestLength(subCmd, requestLength))
Patrick Venturea53a7b32018-08-03 09:15:20 -070057 {
58 return IPMI_CC_INVALID;
59 }
Patrick Venture54c3b532018-08-01 11:45:49 -070060
Patrick Venture9a5a79a2018-08-03 17:23:57 -070061 auto handler = getCommandHandler(subCmd);
62 if (handler)
Patrick Venture54c3b532018-08-01 11:45:49 -070063 {
Patrick Venture9a5a79a2018-08-03 17:23:57 -070064 return handler(flashUpdateSingleton.get(), reqBuf, replyCmdBuf,
65 dataLen);
Patrick Venture54c3b532018-08-01 11:45:49 -070066 }
67
Patrick Ventureaccc9172018-07-24 10:51:58 -070068 return IPMI_CC_INVALID;
69}
70
71static ipmi_ret_t ipmiFlashControl(ipmi_netfn_t netFn, ipmi_cmd_t cmd,
72 ipmi_request_t request,
73 ipmi_response_t response,
74 ipmi_data_len_t dataLen,
75 ipmi_context_t context)
76{
77 /* request_t, response_t are void*. ipmi_data_len_t is a size_t* */
78 auto reqBuf = static_cast<const uint8_t*>(request);
79 auto replyCmdBuf = static_cast<uint8_t*>(response);
80
81 return flashControl(cmd, reqBuf, replyCmdBuf, dataLen);
82}
83
84void setupGlobalOemFlashControl() __attribute__((constructor));
85
86void setupGlobalOemFlashControl()
87{
Patrick Venture6f17bd22018-08-07 13:24:17 -070088 flashUpdateSingleton = std::make_unique<FlashUpdate>(stagingPath, hashPath);
Patrick Venture54c3b532018-08-01 11:45:49 -070089
Patrick Ventureaccc9172018-07-24 10:51:58 -070090#ifdef ENABLE_GOOGLE
91 oem::Router* router = oem::mutableRouter();
92
93 fprintf(stderr, "Registering OEM:[%#08X], Cmd:[%#04X] for Flash Update\n",
94 oem::google::number, oem::google::flashOverBTCmd);
95
96 router->registerHandler(oem::google::number, oem::google::flashOverBTCmd,
97 flashControl);
98#endif
99
100 ipmi_register_callback(NETFUN_FIRMWARE, oem::google::flashOverBTCmd, NULL,
101 ipmiFlashControl, PRIVILEGE_USER);
102}