blob: afef26e7c4cc74c80b3d6d2695c7f0e587aa8081 [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 Ventured61b0ff2019-05-15 15:58:06 -070019#include "firmware_handler.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -070020#include "status.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080021#include "tool_errors.hpp"
Patrick Venture7dad86f2019-05-17 08:52:20 -070022#include "util.hpp"
Patrick Venture0533d0b2018-12-13 08:48:24 -080023
Patrick Venture00887592018-12-11 10:57:06 -080024#include <algorithm>
Patrick Venture664c5bc2019-03-07 08:09:45 -080025#include <blobs-ipmid/blobs.hpp>
Patrick Venture339dece2018-12-14 18:32:04 -080026#include <cstring>
Patrick Venture664c5bc2019-03-07 08:09:45 -080027#include <ipmiblob/blob_errors.hpp>
Patrick Ventureaf696252018-12-11 10:22:14 -080028#include <memory>
Patrick Venture2a927e82019-02-01 07:29:47 -080029#include <string>
Patrick Ventured61b0ff2019-05-15 15:58:06 -070030#include <thread>
Patrick Venture55646de2019-05-16 10:06:26 -070031#include <vector>
Patrick Ventureaf696252018-12-11 10:22:14 -080032
Patrick Venture9b534f02018-12-13 16:10:02 -080033namespace host_tool
34{
35
Patrick Venture55646de2019-05-16 10:06:26 -070036bool UpdateHandler::checkAvailable(const std::string& goalFirmware)
37{
38 std::vector<std::string> blobs = blob->getBlobList();
39
40 auto blobInst = std::find_if(
41 blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
42 /* Running into weird scenarios where the string comparison doesn't
43 * work. TODO: revisit.
44 */
45 return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
46 goalFirmware.length()));
47 // return (goalFirmware.compare(iter));
48 });
49 if (blobInst == blobs.end())
50 {
51 std::fprintf(stderr, "%s not found\n", goalFirmware.c_str());
52 return false;
53 }
54
55 /* Call stat on /flash/image (or /flash/tarball) and check if data interface
56 * is supported.
57 */
58 ipmiblob::StatResponse stat;
59
60 try
61 {
62 stat = blob->getStat(goalFirmware);
63 }
64 catch (const ipmiblob::BlobException& b)
65 {
66 std::fprintf(stderr, "Received exception '%s' on getStat\n", b.what());
67 return false;
68 }
69
70 auto supported = handler->supportedType();
71 if ((stat.blob_state & supported) == 0)
72 {
73 std::fprintf(stderr, "data interface selected not supported.\n");
74 return false;
75 }
76
77 return true;
78}
79
80void UpdateHandler::sendFile(const std::string& target, const std::string& path)
81{
82 std::uint16_t session;
83 auto supported = handler->supportedType();
84
85 try
86 {
87 session = blob->openBlob(
88 target, static_cast<std::uint16_t>(supported) |
89 static_cast<std::uint16_t>(blobs::OpenFlags::write));
90 }
91 catch (const ipmiblob::BlobException& b)
92 {
93 throw ToolException("blob exception received: " +
94 std::string(b.what()));
95 }
96
97 if (!handler->sendContents(path, session))
98 {
99 /* Need to close the session on failure, or it's stuck open (until the
100 * blob handler timeout is implemented, and even then, why make it wait.
101 */
102 blob->closeBlob(session);
103 throw ToolException("Failed to send contents of " + path);
104 }
105
106 blob->closeBlob(session);
107}
108
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700109/* Poll an open verification session. Handling closing the session is not yet
110 * owned by this method. */
111bool pollVerificationStatus(std::uint16_t session,
112 ipmiblob::BlobInterface* blob)
113{
114 using namespace std::chrono_literals;
115
116 static constexpr auto verificationSleep = 5s;
117 static constexpr int commandAttempts = 20;
118 int attempts = 0;
119 bool exitLoop = false;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700120 ipmi_flash::VerifyCheckResponses result =
121 ipmi_flash::VerifyCheckResponses::other;
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700122
123 try
124 {
125 /* Reach back the current status from the verification service output.
126 */
127 while (attempts++ < commandAttempts)
128 {
129 ipmiblob::StatResponse resp = blob->getStat(session);
130
131 if (resp.metadata.size() != sizeof(std::uint8_t))
132 {
133 /* TODO: How do we want to handle the verification failures,
134 * because closing the session to the verify blob has a special
135 * as-of-yet not fully defined behavior.
136 */
137 std::fprintf(stderr, "Received invalid metadata response!!!\n");
138 }
139
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700140 result =
141 static_cast<ipmi_flash::VerifyCheckResponses>(resp.metadata[0]);
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700142
143 switch (result)
144 {
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700145 case ipmi_flash::VerifyCheckResponses::failed:
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700146 std::fprintf(stderr, "failed\n");
147 exitLoop = true;
148 break;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700149 case ipmi_flash::VerifyCheckResponses::other:
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700150 std::fprintf(stderr, "other\n");
151 break;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700152 case ipmi_flash::VerifyCheckResponses::running:
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700153 std::fprintf(stderr, "running\n");
154 break;
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700155 case ipmi_flash::VerifyCheckResponses::success:
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700156 std::fprintf(stderr, "success\n");
157 exitLoop = true;
158 break;
159 default:
160 std::fprintf(stderr, "wat\n");
161 }
162
163 if (exitLoop)
164 {
165 break;
166 }
167 std::this_thread::sleep_for(verificationSleep);
168 }
169 }
170 catch (const ipmiblob::BlobException& b)
171 {
172 throw ToolException("blob exception received: " +
173 std::string(b.what()));
174 }
175
176 /* TODO: If this is reached and it's not success, it may be worth just
177 * throwing a ToolException with a timeout message specifying the final
178 * read's value.
179 *
180 * TODO: Given that excepting from certain points leaves the BMC update
181 * state machine in an inconsistent state, we need to carefully evaluate
182 * which exceptions from the lower layers allow one to try and delete the
183 * blobs to rollback the state and progress.
184 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700185 return (result == ipmi_flash::VerifyCheckResponses::success);
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700186}
187
Patrick Venture55646de2019-05-16 10:06:26 -0700188bool UpdateHandler::verifyFile(const std::string& target)
Patrick Venturebf58cd62018-12-11 09:05:46 -0800189{
Patrick Venture0533d0b2018-12-13 08:48:24 -0800190 std::uint16_t session;
Patrick Venture55646de2019-05-16 10:06:26 -0700191 bool success = false;
192
Patrick Venture0533d0b2018-12-13 08:48:24 -0800193 try
194 {
Patrick Venture664c5bc2019-03-07 08:09:45 -0800195 session = blob->openBlob(
Patrick Venture55646de2019-05-16 10:06:26 -0700196 target, static_cast<std::uint16_t>(blobs::OpenFlags::write));
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700197 }
198 catch (const ipmiblob::BlobException& b)
199 {
200 throw ToolException("blob exception received: " +
201 std::string(b.what()));
202 }
203
204 std::fprintf(
205 stderr,
206 "Committing to verification file to trigger verification service\n");
Patrick Venture55646de2019-05-16 10:06:26 -0700207
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700208 try
209 {
210 blob->commit(session, {});
211 }
212 catch (const ipmiblob::BlobException& b)
213 {
214 throw ToolException("blob exception received: " +
215 std::string(b.what()));
216 }
217
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700218 std::fprintf(stderr,
219 "Calling stat on verification session to check status\n");
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700220
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700221 if (pollVerificationStatus(session, blob))
222 {
223 std::fprintf(stderr, "Verification returned success\n");
Patrick Venture55646de2019-05-16 10:06:26 -0700224 success = true;
Patrick Ventured61b0ff2019-05-15 15:58:06 -0700225 }
226 else
227 {
228 std::fprintf(stderr, "Verification returned non-success (could still "
229 "be running (unlikely))\n");
230 }
231
Patrick Venture7dcca5d2019-05-15 12:32:33 -0700232 blob->closeBlob(session);
Patrick Venture55646de2019-05-16 10:06:26 -0700233 return (success == true);
234}
235
236void updaterMain(UpdateHandler* updater, const std::string& imagePath,
237 const std::string& signaturePath)
238{
239 /* TODO(venture): Add optional parameter to specify the flash type, default
240 * to legacy for now.
Patrick Venture55646de2019-05-16 10:06:26 -0700241 */
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700242 bool goalSupported =
243 updater->checkAvailable(ipmi_flash::staticLayoutBlobId);
Patrick Venture55646de2019-05-16 10:06:26 -0700244 if (!goalSupported)
245 {
246 throw ToolException("Goal firmware or interface not supported");
247 }
248
249 /* Yay, our data handler is supported. */
250
251 /* Send over the firmware image. */
252 std::fprintf(stderr, "Sending over the firmware image.\n");
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700253 updater->sendFile(ipmi_flash::staticLayoutBlobId, imagePath);
Patrick Venture55646de2019-05-16 10:06:26 -0700254
255 /* Send over the hash contents. */
256 std::fprintf(stderr, "Sending over the hash file.\n");
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700257 updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
Patrick Venture55646de2019-05-16 10:06:26 -0700258
259 /* Trigger the verification by opening the verify file. */
260 std::fprintf(stderr, "Opening the verification file\n");
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700261 if (updater->verifyFile(ipmi_flash::verifyBlobId))
Patrick Venture55646de2019-05-16 10:06:26 -0700262 {
263 std::fprintf(stderr, "succeeded\n");
264 }
265 else
266 {
267 std::fprintf(stderr, "failed\n");
268 }
Patrick Venturebf58cd62018-12-11 09:05:46 -0800269}
Patrick Venture9b534f02018-12-13 16:10:02 -0800270
271} // namespace host_tool