Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 1 | /* |
Patrick Venture | 514f648 | 2018-08-07 14:27:58 -0700 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 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 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 19 | #include "host-ipmid/ipmid-api.h" |
| 20 | #include "host-ipmid/oemrouter.hpp" |
| 21 | |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 22 | #include "config.h" |
Patrick Venture | 3d1786b | 2018-08-01 11:19:24 -0700 | [diff] [blame] | 23 | #include "flash-ipmi.hpp" |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 24 | #include "ipmi.hpp" |
Patrick Venture | 3d1786b | 2018-08-01 11:19:24 -0700 | [diff] [blame] | 25 | |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 26 | static constexpr auto stagingPath = STAGING_PATH; |
| 27 | |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 28 | /* TODO: Once OEM IPMI number placement is settled, point to that. */ |
| 29 | namespace oem |
| 30 | { |
| 31 | namespace google |
| 32 | { |
| 33 | constexpr int number = 11129; |
| 34 | constexpr int flashOverBTCmd = 127; |
| 35 | } // namespace google |
| 36 | } // namespace oem |
| 37 | |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 38 | /* We have one instance of the FlashUpdate object tracking commands. */ |
| 39 | std::unique_ptr<FlashUpdate> flashUpdateSingleton; |
| 40 | |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 41 | static ipmi_ret_t flashControl(ipmi_cmd_t cmd, const uint8_t* reqBuf, |
| 42 | uint8_t* replyCmdBuf, size_t* dataLen) |
| 43 | { |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 44 | size_t requestLength = (*dataLen); |
| 45 | |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 46 | /* Verify it's at least as long as the shortest message. */ |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 47 | if (requestLength < 1) |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 48 | { |
| 49 | return IPMI_CC_INVALID; |
| 50 | } |
| 51 | |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 52 | auto subCmd = static_cast<FlashSubCmds>(reqBuf[0]); |
| 53 | |
| 54 | /* Validate the minimum request length for the command. */ |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 55 | if (!validateRequestLength(subCmd, requestLength)) |
Patrick Venture | a53a7b3 | 2018-08-03 09:15:20 -0700 | [diff] [blame] | 56 | { |
| 57 | return IPMI_CC_INVALID; |
| 58 | } |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 59 | |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 60 | auto handler = getCommandHandler(subCmd); |
| 61 | if (handler) |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 62 | { |
Patrick Venture | 9a5a79a | 2018-08-03 17:23:57 -0700 | [diff] [blame] | 63 | return handler(flashUpdateSingleton.get(), reqBuf, replyCmdBuf, |
| 64 | dataLen); |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 67 | return IPMI_CC_INVALID; |
| 68 | } |
| 69 | |
| 70 | static 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 | |
| 83 | void setupGlobalOemFlashControl() __attribute__((constructor)); |
| 84 | |
| 85 | void setupGlobalOemFlashControl() |
| 86 | { |
Patrick Venture | 8ec019f | 2018-08-07 11:22:33 -0700 | [diff] [blame] | 87 | flashUpdateSingleton = std::make_unique<FlashUpdate>(stagingPath); |
Patrick Venture | 54c3b53 | 2018-08-01 11:45:49 -0700 | [diff] [blame] | 88 | |
Patrick Venture | accc917 | 2018-07-24 10:51:58 -0700 | [diff] [blame] | 89 | #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 | } |