blob: 246594570a1cbfad1868899f68b9ce726bf0a661 [file] [log] [blame]
Patrick Venturebf58cd62018-12-11 09:05:46 -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
17#include "updater.hpp"
18
Patrick Venture2bc23fe2018-12-13 10:16:36 -080019#include "tool_errors.hpp"
Patrick Venture0533d0b2018-12-13 08:48:24 -080020
Patrick Venture00887592018-12-11 10:57:06 -080021#include <algorithm>
Patrick Venture664c5bc2019-03-07 08:09:45 -080022#include <blobs-ipmid/blobs.hpp>
Patrick Venture339dece2018-12-14 18:32:04 -080023#include <cstring>
Patrick Venture664c5bc2019-03-07 08:09:45 -080024#include <ipmiblob/blob_errors.hpp>
Patrick Ventureaf696252018-12-11 10:22:14 -080025#include <memory>
Patrick Venture2a927e82019-02-01 07:29:47 -080026#include <string>
Patrick Ventureaf696252018-12-11 10:22:14 -080027
Patrick Venture9b534f02018-12-13 16:10:02 -080028namespace host_tool
29{
30
Patrick Venture664c5bc2019-03-07 08:09:45 -080031void updaterMain(ipmiblob::BlobInterface* blob, DataInterface* handler,
Patrick Venture2bc23fe2018-12-13 10:16:36 -080032 const std::string& imagePath, const std::string& signaturePath)
Patrick Venturebf58cd62018-12-11 09:05:46 -080033{
Patrick Ventureaf696252018-12-11 10:22:14 -080034 /* TODO(venture): Add optional parameter to specify the flash type, default
35 * to legacy for now.
36 */
Patrick Venture00887592018-12-11 10:57:06 -080037 std::string goalFirmware = "/flash/image";
Patrick Venture73528382019-05-14 12:43:37 -070038 std::string hashFilename = "/flash/hash";
Patrick Venture00887592018-12-11 10:57:06 -080039
Patrick Venture0bf8bf02018-12-12 20:43:25 -080040 /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
41 * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
42 * will have in mind which they're sending and we need to verify it's
43 * available and use it.
44 */
Patrick Venture00887592018-12-11 10:57:06 -080045 std::vector<std::string> blobs = blob->getBlobList();
Patrick Venture339dece2018-12-14 18:32:04 -080046 auto blobInst = std::find_if(
Patrick Venture2a927e82019-02-01 07:29:47 -080047 blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
Patrick Venture339dece2018-12-14 18:32:04 -080048 /* Running into weird scenarios where the string comparison doesn't
49 * work. TODO: revisit.
50 */
51 return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
52 goalFirmware.length()));
53 // return (goalFirmware.compare(iter));
54 });
Patrick Venture00887592018-12-11 10:57:06 -080055 if (blobInst == blobs.end())
56 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080057 throw ToolException(goalFirmware + " not found");
Patrick Venture00887592018-12-11 10:57:06 -080058 }
Patrick Ventureaf696252018-12-11 10:22:14 -080059
Patrick Ventureaf696252018-12-11 10:22:14 -080060 /* Call stat on /flash/image (or /flash/tarball) and check if data interface
Patrick Venture00887592018-12-11 10:57:06 -080061 * is supported.
62 */
Patrick Venture664c5bc2019-03-07 08:09:45 -080063 ipmiblob::StatResponse stat;
Patrick Venture339dece2018-12-14 18:32:04 -080064 try
65 {
66 stat = blob->getStat(goalFirmware);
67 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080068 catch (const ipmiblob::BlobException& b)
Patrick Venture339dece2018-12-14 18:32:04 -080069 {
70 throw ToolException("blob exception received: " +
71 std::string(b.what()));
72 }
73
Patrick Ventureaa32a362018-12-13 10:52:33 -080074 auto supported = handler->supportedType();
75 if ((stat.blob_state & supported) == 0)
Patrick Venture8a55dcb2018-12-12 21:12:58 -080076 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080077 throw ToolException("data interface selected not supported.");
Patrick Venture8a55dcb2018-12-12 21:12:58 -080078 }
Patrick Ventureaf696252018-12-11 10:22:14 -080079
Patrick Venture0533d0b2018-12-13 08:48:24 -080080 /* Yay, our data handler is supported. */
Patrick Venture73528382019-05-14 12:43:37 -070081
82 /* Send over the firmware image. */
83 std::fprintf(stderr, "Sending over the firmware image.\n");
Patrick Venture0533d0b2018-12-13 08:48:24 -080084 std::uint16_t session;
85 try
86 {
Patrick Venture664c5bc2019-03-07 08:09:45 -080087 session = blob->openBlob(
88 goalFirmware,
89 static_cast<std::uint16_t>(supported) |
90 static_cast<std::uint16_t>(blobs::OpenFlags::write));
Patrick Venture0533d0b2018-12-13 08:48:24 -080091 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080092 catch (const ipmiblob::BlobException& b)
Patrick Venture0533d0b2018-12-13 08:48:24 -080093 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080094 throw ToolException("blob exception received: " +
95 std::string(b.what()));
Patrick Venture0533d0b2018-12-13 08:48:24 -080096 }
97
Patrick Venturefd6aaec2018-12-13 09:06:02 -080098 if (!handler->sendContents(imagePath, session))
99 {
Patrick Venturef9566d82019-01-16 09:44:01 -0800100 /* Need to close the session on failure, or it's stuck open (until the
101 * blob handler timeout is implemented, and even then, why make it wait.
102 */
103 blob->closeBlob(session);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800104 throw ToolException("Failed to send contents of " + imagePath);
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800105 }
106
Patrick Venture9a5ce562018-12-14 18:56:04 -0800107 blob->closeBlob(session);
108
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800109 /* Send over the hash contents. */
Patrick Venture73528382019-05-14 12:43:37 -0700110 std::fprintf(stderr, "Sending over the hash file.\n");
111 try
112 {
113 session = blob->openBlob(
114 hashFilename,
115 static_cast<std::uint16_t>(supported) |
116 static_cast<std::uint16_t>(blobs::OpenFlags::write));
117 }
118 catch (const ipmiblob::BlobException& b)
119 {
120 throw ToolException("blob exception received: " +
121 std::string(b.what()));
122 }
123
124 if (!handler->sendContents(signaturePath, session))
125 {
126 blob->closeBlob(session);
127 throw ToolException("Failed to send contents of " + signaturePath);
128 }
129
130 blob->closeBlob(session);
131
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800132 /* Trigger the verification. */
133 /* Check the verification. */
134
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800135 return;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800136}
Patrick Venture9b534f02018-12-13 16:10:02 -0800137
138} // namespace host_tool