Patrick Venture | 7dc4670 | 2018-08-08 09:43:33 -0700 | [diff] [blame^] | 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 "updatehelper.hpp" |
| 20 | |
| 21 | #include <cstring> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace |
| 25 | { |
| 26 | // Output a vector of bytes consisted of the command header and given input. |
| 27 | // Precondition: packet must be preallocated with kFlashCommandHdrSizeBytes. |
| 28 | void constructFlashIpmiPacket(int command, const uint8_t* payload, |
| 29 | int payload_size, std::vector<uint8_t>* packet) |
| 30 | { |
| 31 | struct CommandHdr hdr; |
| 32 | std::memset(&hdr, 0, sizeof(hdr)); |
| 33 | |
| 34 | hdr.command = OEM_FLASH_UPDATE_BT_COMMAND; |
| 35 | hdr.subcommand = command; |
| 36 | |
| 37 | #ifdef ENABLE_GOOGLE |
| 38 | hdr.netfn = NETFN_OEM; |
| 39 | std::memcpy(&hdr.oen, OEN_GOOGLE, sizeof(OEN_GOOGLE)); |
| 40 | #else |
| 41 | hdr.netfn = NETFN_FIRMWARE; |
| 42 | #endif |
| 43 | |
| 44 | std::memcpy(packet->data(), &hdr, sizeof(hdr)); |
| 45 | |
| 46 | if (payload != nullptr && payload_size > 0) |
| 47 | { |
| 48 | packet->insert(packet->end(), payload, payload + payload_size); |
| 49 | } |
| 50 | } |
| 51 | } // namespace |
| 52 | |
| 53 | /* |
| 54 | * For use with BT Flash commands with payloads |
| 55 | */ |
| 56 | struct IpmiResponse |
| 57 | IpmiUpdateHelper::SendCommand(int command, |
| 58 | const std::vector<uint8_t>& payload) |
| 59 | { |
| 60 | std::vector<uint8_t> packet(kFlashCommandHdrSizeBytes); |
| 61 | constructFlashIpmiPacket(command, payload.data(), payload.size(), &packet); |
| 62 | |
| 63 | return raw->Raw(packet); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * For use with BT Flash commands that are only the command itself. |
| 68 | */ |
| 69 | struct IpmiResponse IpmiUpdateHelper::SendEmptyCommand(int command) |
| 70 | { |
| 71 | return SendCommand(command, {}); |
| 72 | } |