blob: eeca58927b000ad3755a73d7e27cfb1a79f7ea0e [file] [log] [blame]
Patrick Venture22e38752018-11-21 08:52:49 -08001/*
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 Venture148cd652018-11-06 10:59:47 -080017#include "config.h"
18
Patrick Venturea6b4abd2019-07-19 10:58:55 -070019#include "buildjson.hpp"
Patrick Venture64919ec2018-11-15 11:52:07 -080020#include "file_handler.hpp"
Patrick Venturec7ca2912018-11-02 11:38:33 -070021#include "firmware_handler.hpp"
Patrick Venture84778b82019-06-26 20:11:09 -070022#include "flags.hpp"
Patrick Venturecf066ac2019-08-06 09:03:47 -070023#include "general_systemd.hpp"
Patrick Venturea78e39f2018-11-06 18:37:06 -080024#include "image_handler.hpp"
Patrick Ventureefce8f92018-12-14 16:39:00 -080025#include "lpc_aspeed.hpp"
Patrick Venture1cde5f92018-11-07 08:26:47 -080026#include "lpc_handler.hpp"
Patrick Ventureefce8f92018-12-14 16:39:00 -080027#include "lpc_nuvoton.hpp"
Benjamin Fair545f5652019-10-09 14:18:54 -070028#include "net_handler.hpp"
Patrick Venture1cde5f92018-11-07 08:26:47 -080029#include "pci_handler.hpp"
Patrick Venture1d66fe62019-06-03 14:57:27 -070030#include "status.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070031#include "util.hpp"
Patrick Venturec7ca2912018-11-02 11:38:33 -070032
Patrick Venture4eb55952018-11-16 15:36:24 -080033#include <sdbusplus/bus.hpp>
Patrick Venture9b37b092020-05-28 20:58:57 -070034
35#include <cstdint>
36#include <memory>
Patrick Venturea6b4abd2019-07-19 10:58:55 -070037#include <string>
Patrick Venturefa06a5f2019-07-01 09:22:38 -070038#include <unordered_map>
Patrick Venturea6b4abd2019-07-19 10:58:55 -070039#include <vector>
Patrick Venturec7ca2912018-11-02 11:38:33 -070040
Patrick Venture1d5a31c2019-05-20 11:38:22 -070041namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070042{
Patrick Venturea6b4abd2019-07-19 10:58:55 -070043
Patrick Venturea78e39f2018-11-06 18:37:06 -080044namespace
45{
Patrick Venture48e309c2019-06-29 07:51:19 -070046
Patrick Venturea6b4abd2019-07-19 10:58:55 -070047static constexpr const char* jsonConfigurationPath =
48 "/usr/share/phosphor-ipmi-flash/";
49
Patrick Venturef32a4532019-07-19 10:15:40 -070050/**
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 */
57HandlerPack CreateFileHandlerPack(const std::string& name,
58 const std::string& path)
59{
60 return HandlerPack(name, std::make_unique<FileHandler>(path));
61}
62
Patrick Venturef62ec6b2020-09-24 09:07:10 -070063#ifdef NUVOTON_P2A_MBOX
Patrick Venture43a2d162020-09-24 15:55:41 -070064static constexpr std::size_t memoryRegionSize = 16 * 1024UL;
Patrick Venturef62ec6b2020-09-24 09:07:10 -070065#elif defined NUVOTON_P2A_VGA
Patrick Venture43a2d162020-09-24 15:55:41 -070066static constexpr std::size_t memoryRegionSize = 4 * 1024 * 1024UL;
Patrick Venturef62ec6b2020-09-24 09:07:10 -070067#else
Patrick Venture43a2d162020-09-24 15:55:41 -070068/* The maximum external buffer size we expect is 64KB. */
69static constexpr std::size_t memoryRegionSize = 64 * 1024UL;
Patrick Venturef62ec6b2020-09-24 09:07:10 -070070#endif
71
Patrick Venturea78e39f2018-11-06 18:37:06 -080072} // namespace
Patrick Venture1d5a31c2019-05-20 11:38:22 -070073} // namespace ipmi_flash
Patrick Venturec7ca2912018-11-02 11:38:33 -070074
Patrick Venture9b37b092020-05-28 20:58:57 -070075extern "C"
76{
77 std::unique_ptr<blobs::GenericBlobInterface> createHandler();
Patrick Venture5b451e62018-11-14 14:20:08 -080078}
Patrick Venture5b451e62018-11-14 14:20:08 -080079
80std::unique_ptr<blobs::GenericBlobInterface> createHandler()
Patrick Venturec7ca2912018-11-02 11:38:33 -070081{
Patrick Venturee7402692020-09-22 16:26:22 -070082 using namespace ipmi_flash;
Patrick Venture7c2a00e2019-07-01 17:33:03 -070083
Patrick Venture4934daa2020-09-22 16:37:44 -070084 std::vector<DataHandlerPack> supportedTransports;
85
86 supportedTransports.emplace_back(FirmwareFlags::UpdateFlags::ipmi, nullptr);
87
Patrick Venture12b76c92020-09-22 16:31:39 -070088#ifdef ENABLE_PCI_BRIDGE
Patrick Venture4934daa2020-09-22 16:37:44 -070089 supportedTransports.emplace_back(
90 FirmwareFlags::UpdateFlags::p2a,
Patrick Venture43a2d162020-09-24 15:55:41 -070091 std::make_unique<PciDataHandler>(MAPPED_ADDRESS, memoryRegionSize));
Patrick Venture12b76c92020-09-22 16:31:39 -070092#endif
Patrick Venture4934daa2020-09-22 16:37:44 -070093
Patrick Venture12b76c92020-09-22 16:31:39 -070094#ifdef ENABLE_LPC_BRIDGE
Patrick Venture4934daa2020-09-22 16:37:44 -070095#if defined(ASPEED_LPC)
96 supportedTransports.emplace_back(
97 FirmwareFlags::UpdateFlags::lpc,
98 std::make_unique<LpcDataHandler>(LpcMapperAspeed::createAspeedMapper(
Patrick Venture43a2d162020-09-24 15:55:41 -070099 MAPPED_ADDRESS, memoryRegionSize)));
Patrick Venture4934daa2020-09-22 16:37:44 -0700100#elif defined(NUVOTON_LPC)
101 supportedTransports.emplace_back(
102 FirmwareFlags::UpdateFlags::lpc,
103 std::make_unique<LpcDataHandler>(LpcMapperNuvoton::createNuvotonMapper(
Patrick Venture43a2d162020-09-24 15:55:41 -0700104 MAPPED_ADDRESS, memoryRegionSize)));
Patrick Venture4934daa2020-09-22 16:37:44 -0700105#else
106#error "You must specify a hardware implementation."
Patrick Venture12b76c92020-09-22 16:31:39 -0700107#endif
Patrick Venture4934daa2020-09-22 16:37:44 -0700108#endif
109
Patrick Venture12b76c92020-09-22 16:31:39 -0700110#ifdef ENABLE_NET_BRIDGE
Patrick Venture4934daa2020-09-22 16:37:44 -0700111 supportedTransports.emplace_back(FirmwareFlags::UpdateFlags::net,
112 std::make_unique<NetDataHandler>());
Patrick Venture12b76c92020-09-22 16:31:39 -0700113#endif
Patrick Venture12b76c92020-09-22 16:31:39 -0700114
Patrick Venturee7402692020-09-22 16:26:22 -0700115 ActionMap actionPacks = {};
Patrick Venture7c2a00e2019-07-01 17:33:03 -0700116
Patrick Venturee7402692020-09-22 16:26:22 -0700117 std::vector<HandlerConfig> configsFromJson =
118 BuildHandlerConfigs(jsonConfigurationPath);
Patrick Ventured4e20de2019-07-18 12:48:05 -0700119
Patrick Venturee7402692020-09-22 16:26:22 -0700120 std::vector<HandlerPack> supportedFirmware;
121
122 supportedFirmware.push_back(
123 std::move(CreateFileHandlerPack(hashBlobId, HASH_FILENAME)));
Patrick Ventured4e20de2019-07-18 12:48:05 -0700124
Patrick Venture1871fe92019-07-19 14:46:08 -0700125 for (auto& config : configsFromJson)
126 {
Patrick Venturec6ba8ff2020-09-23 12:42:57 -0700127 supportedFirmware.emplace_back(config.blobId,
128 std::move(config.handler));
Patrick Venture1871fe92019-07-19 14:46:08 -0700129 actionPacks[config.blobId] = std::move(config.actions);
130
131 std::fprintf(stderr, "config loaded: %s\n", config.blobId.c_str());
132 }
Patrick Ventured4e20de2019-07-18 12:48:05 -0700133
Patrick Venturee7402692020-09-22 16:26:22 -0700134 auto handler = FirmwareBlobHandler::CreateFirmwareBlobHandler(
135 std::move(supportedFirmware), std::move(supportedTransports),
136 std::move(actionPacks));
Patrick Venture52854622018-11-06 12:30:00 -0800137
138 if (!handler)
139 {
Patrick Venture1ab824a2020-05-26 19:29:57 -0700140 std::fprintf(stderr, "Firmware Handler has an invalid configuration");
Patrick Venture5b451e62018-11-14 14:20:08 -0800141 return nullptr;
Patrick Venture52854622018-11-06 12:30:00 -0800142 }
143
Patrick Venturee1118bc2019-06-19 07:32:48 -0700144 return handler;
Patrick Venturec7ca2912018-11-02 11:38:33 -0700145}