| Patrick Venture | 22e3875 | 2018-11-21 08:52:49 -0800 | [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 |  | 
| Patrick Venture | 148cd65 | 2018-11-06 10:59:47 -0800 | [diff] [blame] | 17 | #include "config.h" | 
|  | 18 |  | 
| Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 19 | #include "buildjson.hpp" | 
| Patrick Venture | 64919ec | 2018-11-15 11:52:07 -0800 | [diff] [blame] | 20 | #include "file_handler.hpp" | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 21 | #include "firmware_handler.hpp" | 
| Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame] | 22 | #include "flags.hpp" | 
| Patrick Venture | cf066ac | 2019-08-06 09:03:47 -0700 | [diff] [blame] | 23 | #include "general_systemd.hpp" | 
| Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 24 | #include "image_handler.hpp" | 
| Patrick Venture | efce8f9 | 2018-12-14 16:39:00 -0800 | [diff] [blame] | 25 | #include "lpc_aspeed.hpp" | 
| Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 26 | #include "lpc_handler.hpp" | 
| Patrick Venture | efce8f9 | 2018-12-14 16:39:00 -0800 | [diff] [blame] | 27 | #include "lpc_nuvoton.hpp" | 
| Benjamin Fair | 545f565 | 2019-10-09 14:18:54 -0700 | [diff] [blame] | 28 | #include "net_handler.hpp" | 
| Patrick Venture | 1cde5f9 | 2018-11-07 08:26:47 -0800 | [diff] [blame] | 29 | #include "pci_handler.hpp" | 
| Patrick Venture | 1d66fe6 | 2019-06-03 14:57:27 -0700 | [diff] [blame] | 30 | #include "status.hpp" | 
| Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 31 | #include "util.hpp" | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 32 |  | 
| Patrick Venture | 4eb5595 | 2018-11-16 15:36:24 -0800 | [diff] [blame] | 33 | #include <sdbusplus/bus.hpp> | 
| Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | #include <cstdint> | 
|  | 36 | #include <memory> | 
| Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 37 | #include <string> | 
| Patrick Venture | fa06a5f | 2019-07-01 09:22:38 -0700 | [diff] [blame] | 38 | #include <unordered_map> | 
| Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 39 | #include <vector> | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 40 |  | 
| Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 41 | namespace ipmi_flash | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 42 | { | 
| Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 43 |  | 
| Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 44 | namespace | 
|  | 45 | { | 
| Patrick Venture | 48e309c | 2019-06-29 07:51:19 -0700 | [diff] [blame] | 46 |  | 
| Patrick Venture | a6b4abd | 2019-07-19 10:58:55 -0700 | [diff] [blame] | 47 | static constexpr const char* jsonConfigurationPath = | 
|  | 48 | "/usr/share/phosphor-ipmi-flash/"; | 
|  | 49 |  | 
| Patrick Venture | f32a453 | 2019-07-19 10:15:40 -0700 | [diff] [blame] | 50 | /** | 
|  | 51 | * Given a name and path, create a HandlerPack. | 
|  | 52 | * | 
|  | 53 | * @param[in] name - the blob id path for this | 
|  | 54 | * @param[in] path - the file path to write the contents. | 
|  | 55 | * @return the HandlerPack. | 
|  | 56 | */ | 
|  | 57 | HandlerPack CreateFileHandlerPack(const std::string& name, | 
|  | 58 | const std::string& path) | 
|  | 59 | { | 
|  | 60 | return HandlerPack(name, std::make_unique<FileHandler>(path)); | 
|  | 61 | } | 
|  | 62 |  | 
| Patrick Venture | f62ec6b | 2020-09-24 09:07:10 -0700 | [diff] [blame] | 63 | #ifdef NUVOTON_P2A_MBOX | 
| Patrick Venture | 43a2d16 | 2020-09-24 15:55:41 -0700 | [diff] [blame] | 64 | static constexpr std::size_t memoryRegionSize = 16 * 1024UL; | 
| Patrick Venture | f62ec6b | 2020-09-24 09:07:10 -0700 | [diff] [blame] | 65 | #elif defined NUVOTON_P2A_VGA | 
| Patrick Venture | 43a2d16 | 2020-09-24 15:55:41 -0700 | [diff] [blame] | 66 | static constexpr std::size_t memoryRegionSize = 4 * 1024 * 1024UL; | 
| Patrick Venture | f62ec6b | 2020-09-24 09:07:10 -0700 | [diff] [blame] | 67 | #else | 
| Patrick Venture | 43a2d16 | 2020-09-24 15:55:41 -0700 | [diff] [blame] | 68 | /* The maximum external buffer size we expect is 64KB. */ | 
|  | 69 | static constexpr std::size_t memoryRegionSize = 64 * 1024UL; | 
| Patrick Venture | f62ec6b | 2020-09-24 09:07:10 -0700 | [diff] [blame] | 70 | #endif | 
|  | 71 |  | 
| Patrick Venture | a78e39f | 2018-11-06 18:37:06 -0800 | [diff] [blame] | 72 | } // namespace | 
| Patrick Venture | 1d5a31c | 2019-05-20 11:38:22 -0700 | [diff] [blame] | 73 | } // namespace ipmi_flash | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 74 |  | 
| Patrick Venture | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 75 | extern "C" | 
|  | 76 | { | 
|  | 77 | std::unique_ptr<blobs::GenericBlobInterface> createHandler(); | 
| Patrick Venture | 5b451e6 | 2018-11-14 14:20:08 -0800 | [diff] [blame] | 78 | } | 
| Patrick Venture | 5b451e6 | 2018-11-14 14:20:08 -0800 | [diff] [blame] | 79 |  | 
|  | 80 | std::unique_ptr<blobs::GenericBlobInterface> createHandler() | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 81 | { | 
| Patrick Venture | e740269 | 2020-09-22 16:26:22 -0700 | [diff] [blame] | 82 | using namespace ipmi_flash; | 
| Patrick Venture | 7c2a00e | 2019-07-01 17:33:03 -0700 | [diff] [blame] | 83 |  | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 84 | std::vector<DataHandlerPack> supportedTransports; | 
|  | 85 |  | 
|  | 86 | supportedTransports.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr); | 
|  | 87 |  | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 88 | #ifdef ENABLE_PCI_BRIDGE | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 89 | supportedTransports.emplace_back( | 
|  | 90 | FirmwareFlags::UpdateFlags::p2a, | 
| Patrick Venture | 43a2d16 | 2020-09-24 15:55:41 -0700 | [diff] [blame] | 91 | std::make_unique<PciDataHandler>(MAPPED_ADDRESS, memoryRegionSize)); | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 92 | #endif | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 93 |  | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 94 | #ifdef ENABLE_LPC_BRIDGE | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 95 | #if defined(ASPEED_LPC) | 
|  | 96 | supportedTransports.emplace_back( | 
|  | 97 | FirmwareFlags::UpdateFlags::lpc, | 
|  | 98 | std::make_unique<LpcDataHandler>(LpcMapperAspeed::createAspeedMapper( | 
| Patrick Venture | 43a2d16 | 2020-09-24 15:55:41 -0700 | [diff] [blame] | 99 | MAPPED_ADDRESS, memoryRegionSize))); | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 100 | #elif defined(NUVOTON_LPC) | 
|  | 101 | supportedTransports.emplace_back( | 
|  | 102 | FirmwareFlags::UpdateFlags::lpc, | 
|  | 103 | std::make_unique<LpcDataHandler>(LpcMapperNuvoton::createNuvotonMapper( | 
| Patrick Venture | 43a2d16 | 2020-09-24 15:55:41 -0700 | [diff] [blame] | 104 | MAPPED_ADDRESS, memoryRegionSize))); | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 105 | #else | 
|  | 106 | #error "You must specify a hardware implementation." | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 107 | #endif | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 108 | #endif | 
|  | 109 |  | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 110 | #ifdef ENABLE_NET_BRIDGE | 
| Patrick Venture | 4934daa | 2020-09-22 16:37:44 -0700 | [diff] [blame] | 111 | supportedTransports.emplace_back(FirmwareFlags::UpdateFlags::net, | 
|  | 112 | std::make_unique<NetDataHandler>()); | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 113 | #endif | 
| Patrick Venture | 12b76c9 | 2020-09-22 16:31:39 -0700 | [diff] [blame] | 114 |  | 
| Patrick Venture | e740269 | 2020-09-22 16:26:22 -0700 | [diff] [blame] | 115 | ActionMap actionPacks = {}; | 
| Patrick Venture | 7c2a00e | 2019-07-01 17:33:03 -0700 | [diff] [blame] | 116 |  | 
| Patrick Venture | e740269 | 2020-09-22 16:26:22 -0700 | [diff] [blame] | 117 | std::vector<HandlerConfig> configsFromJson = | 
|  | 118 | BuildHandlerConfigs(jsonConfigurationPath); | 
| Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 119 |  | 
| Patrick Venture | e740269 | 2020-09-22 16:26:22 -0700 | [diff] [blame] | 120 | std::vector<HandlerPack> supportedFirmware; | 
|  | 121 |  | 
|  | 122 | supportedFirmware.push_back( | 
|  | 123 | std::move(CreateFileHandlerPack(hashBlobId, HASH_FILENAME))); | 
| Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 124 |  | 
| Patrick Venture | 1871fe9 | 2019-07-19 14:46:08 -0700 | [diff] [blame] | 125 | for (auto& config : configsFromJson) | 
|  | 126 | { | 
| Patrick Venture | c6ba8ff | 2020-09-23 12:42:57 -0700 | [diff] [blame] | 127 | supportedFirmware.emplace_back(config.blobId, | 
|  | 128 | std::move(config.handler)); | 
| Patrick Venture | 1871fe9 | 2019-07-19 14:46:08 -0700 | [diff] [blame] | 129 | actionPacks[config.blobId] = std::move(config.actions); | 
|  | 130 |  | 
|  | 131 | std::fprintf(stderr, "config loaded: %s\n", config.blobId.c_str()); | 
|  | 132 | } | 
| Patrick Venture | d4e20de | 2019-07-18 12:48:05 -0700 | [diff] [blame] | 133 |  | 
| Patrick Venture | e740269 | 2020-09-22 16:26:22 -0700 | [diff] [blame] | 134 | auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler( | 
|  | 135 | std::move(supportedFirmware), std::move(supportedTransports), | 
|  | 136 | std::move(actionPacks)); | 
| Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 137 |  | 
|  | 138 | if (!handler) | 
|  | 139 | { | 
| Patrick Venture | 1ab824a | 2020-05-26 19:29:57 -0700 | [diff] [blame] | 140 | std::fprintf(stderr, "Firmware Handler has an invalid configuration"); | 
| Patrick Venture | 5b451e6 | 2018-11-14 14:20:08 -0800 | [diff] [blame] | 141 | return nullptr; | 
| Patrick Venture | 5285462 | 2018-11-06 12:30:00 -0800 | [diff] [blame] | 142 | } | 
|  | 143 |  | 
| Patrick Venture | e1118bc | 2019-06-19 07:32:48 -0700 | [diff] [blame] | 144 | return handler; | 
| Patrick Venture | c7ca291 | 2018-11-02 11:38:33 -0700 | [diff] [blame] | 145 | } |