blob: fd7213687e57773c43a09a193c38120d83f4ad36 [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.
Patrick Venture7dcca5d2019-05-15 12:32:33 -070036 *
37 * TODO(venture): Move the strings from the FirmwareHandler object to a
38 * boring utils object so it will be more happly linked cleanly to both the
39 * BMC and host-side.
Patrick Ventureaf696252018-12-11 10:22:14 -080040 */
Patrick Venture00887592018-12-11 10:57:06 -080041 std::string goalFirmware = "/flash/image";
Patrick Venture73528382019-05-14 12:43:37 -070042 std::string hashFilename = "/flash/hash";
Patrick Venture7dcca5d2019-05-15 12:32:33 -070043 std::string verifyFilename = "/flash/verify";
Patrick Venture00887592018-12-11 10:57:06 -080044
Patrick Venture0bf8bf02018-12-12 20:43:25 -080045 /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
46 * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
47 * will have in mind which they're sending and we need to verify it's
48 * available and use it.
49 */
Patrick Venture00887592018-12-11 10:57:06 -080050 std::vector<std::string> blobs = blob->getBlobList();
Patrick Venture339dece2018-12-14 18:32:04 -080051 auto blobInst = std::find_if(
Patrick Venture2a927e82019-02-01 07:29:47 -080052 blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
Patrick Venture339dece2018-12-14 18:32:04 -080053 /* Running into weird scenarios where the string comparison doesn't
54 * work. TODO: revisit.
55 */
56 return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
57 goalFirmware.length()));
58 // return (goalFirmware.compare(iter));
59 });
Patrick Venture00887592018-12-11 10:57:06 -080060 if (blobInst == blobs.end())
61 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080062 throw ToolException(goalFirmware + " not found");
Patrick Venture00887592018-12-11 10:57:06 -080063 }
Patrick Ventureaf696252018-12-11 10:22:14 -080064
Patrick Ventureaf696252018-12-11 10:22:14 -080065 /* Call stat on /flash/image (or /flash/tarball) and check if data interface
Patrick Venture00887592018-12-11 10:57:06 -080066 * is supported.
67 */
Patrick Venture664c5bc2019-03-07 08:09:45 -080068 ipmiblob::StatResponse stat;
Patrick Venture339dece2018-12-14 18:32:04 -080069 try
70 {
71 stat = blob->getStat(goalFirmware);
72 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080073 catch (const ipmiblob::BlobException& b)
Patrick Venture339dece2018-12-14 18:32:04 -080074 {
75 throw ToolException("blob exception received: " +
76 std::string(b.what()));
77 }
78
Patrick Ventureaa32a362018-12-13 10:52:33 -080079 auto supported = handler->supportedType();
80 if ((stat.blob_state & supported) == 0)
Patrick Venture8a55dcb2018-12-12 21:12:58 -080081 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080082 throw ToolException("data interface selected not supported.");
Patrick Venture8a55dcb2018-12-12 21:12:58 -080083 }
Patrick Ventureaf696252018-12-11 10:22:14 -080084
Patrick Venture0533d0b2018-12-13 08:48:24 -080085 /* Yay, our data handler is supported. */
Patrick Venture73528382019-05-14 12:43:37 -070086
87 /* Send over the firmware image. */
88 std::fprintf(stderr, "Sending over the firmware image.\n");
Patrick Venture0533d0b2018-12-13 08:48:24 -080089 std::uint16_t session;
90 try
91 {
Patrick Venture664c5bc2019-03-07 08:09:45 -080092 session = blob->openBlob(
93 goalFirmware,
94 static_cast<std::uint16_t>(supported) |
95 static_cast<std::uint16_t>(blobs::OpenFlags::write));
Patrick Venture0533d0b2018-12-13 08:48:24 -080096 }
Patrick Venture664c5bc2019-03-07 08:09:45 -080097 catch (const ipmiblob::BlobException& b)
Patrick Venture0533d0b2018-12-13 08:48:24 -080098 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080099 throw ToolException("blob exception received: " +
100 std::string(b.what()));
Patrick Venture0533d0b2018-12-13 08:48:24 -0800101 }
102
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800103 if (!handler->sendContents(imagePath, session))
104 {
Patrick Venturef9566d82019-01-16 09:44:01 -0800105 /* Need to close the session on failure, or it's stuck open (until the
106 * blob handler timeout is implemented, and even then, why make it wait.
107 */
108 blob->closeBlob(session);
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800109 throw ToolException("Failed to send contents of " + imagePath);
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800110 }
111
Patrick Venture9a5ce562018-12-14 18:56:04 -0800112 blob->closeBlob(session);
113
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800114 /* Send over the hash contents. */
Patrick Venture73528382019-05-14 12:43:37 -0700115 std::fprintf(stderr, "Sending over the hash file.\n");
116 try
117 {
118 session = blob->openBlob(
119 hashFilename,
120 static_cast<std::uint16_t>(supported) |
121 static_cast<std::uint16_t>(blobs::OpenFlags::write));
122 }
123 catch (const ipmiblob::BlobException& b)
124 {
125 throw ToolException("blob exception received: " +
126 std::string(b.what()));
127 }
128
129 if (!handler->sendContents(signaturePath, session))
130 {
131 blob->closeBlob(session);
132 throw ToolException("Failed to send contents of " + signaturePath);
133 }
134
135 blob->closeBlob(session);
136
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700137 /* Trigger the verification by opening the verify file. */
138 std::fprintf(stderr, "Opening the verification file\n");
139 try
140 {
141 session = blob->openBlob(
142 verifyFilename,
143 static_cast<std::uint16_t>(supported) |
144 static_cast<std::uint16_t>(blobs::OpenFlags::write));
145 }
146 catch (const ipmiblob::BlobException& b)
147 {
148 throw ToolException("blob exception received: " +
149 std::string(b.what()));
150 }
151
152 std::fprintf(
153 stderr,
154 "Committing to verification file to trigger verification service\n");
155 try
156 {
157 blob->commit(session, {});
158 }
159 catch (const ipmiblob::BlobException& b)
160 {
161 throw ToolException("blob exception received: " +
162 std::string(b.what()));
163 }
164
165 /* TODO: Check the verification via stat(session). */
166
167 /* DO NOT CLOSE the verification session until it's done. */
168 blob->closeBlob(session);
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800169
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800170 return;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800171}
Patrick Venture9b534f02018-12-13 16:10:02 -0800172
173} // namespace host_tool