1bf58cd64SPatrick Venture /*
2bf58cd64SPatrick Venture  * Copyright 2018 Google Inc.
3bf58cd64SPatrick Venture  *
4bf58cd64SPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5bf58cd64SPatrick Venture  * you may not use this file except in compliance with the License.
6bf58cd64SPatrick Venture  * You may obtain a copy of the License at
7bf58cd64SPatrick Venture  *
8bf58cd64SPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9bf58cd64SPatrick Venture  *
10bf58cd64SPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11bf58cd64SPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12bf58cd64SPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf58cd64SPatrick Venture  * See the License for the specific language governing permissions and
14bf58cd64SPatrick Venture  * limitations under the License.
15bf58cd64SPatrick Venture  */
16bf58cd64SPatrick Venture 
17bf58cd64SPatrick Venture #include "updater.hpp"
18bf58cd64SPatrick Venture 
19d61b0ff8SPatrick Venture #include "firmware_handler.hpp"
203ecb3503SPatrick Venture #include "status.hpp"
212bc23fe1SPatrick Venture #include "tool_errors.hpp"
227dad86fdSPatrick Venture #include "util.hpp"
230533d0b0SPatrick Venture 
2400887597SPatrick Venture #include <algorithm>
25664c5bc7SPatrick Venture #include <blobs-ipmid/blobs.hpp>
26339dece8SPatrick Venture #include <cstring>
27664c5bc7SPatrick Venture #include <ipmiblob/blob_errors.hpp>
28af69625fSPatrick Venture #include <memory>
292a927e87SPatrick Venture #include <string>
30d61b0ff8SPatrick Venture #include <thread>
3155646decSPatrick Venture #include <vector>
32af69625fSPatrick Venture 
339b534f06SPatrick Venture namespace host_tool
349b534f06SPatrick Venture {
359b534f06SPatrick Venture 
3655646decSPatrick Venture bool UpdateHandler::checkAvailable(const std::string& goalFirmware)
3755646decSPatrick Venture {
3855646decSPatrick Venture     std::vector<std::string> blobs = blob->getBlobList();
3955646decSPatrick Venture 
4055646decSPatrick Venture     auto blobInst = std::find_if(
4155646decSPatrick Venture         blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
4255646decSPatrick Venture             /* Running into weird scenarios where the string comparison doesn't
4355646decSPatrick Venture              * work.  TODO: revisit.
4455646decSPatrick Venture              */
4555646decSPatrick Venture             return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
4655646decSPatrick Venture                                      goalFirmware.length()));
4755646decSPatrick Venture             // return (goalFirmware.compare(iter));
4855646decSPatrick Venture         });
4955646decSPatrick Venture     if (blobInst == blobs.end())
5055646decSPatrick Venture     {
5155646decSPatrick Venture         std::fprintf(stderr, "%s not found\n", goalFirmware.c_str());
5255646decSPatrick Venture         return false;
5355646decSPatrick Venture     }
5455646decSPatrick Venture 
5555646decSPatrick Venture     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
5655646decSPatrick Venture      * is supported.
5755646decSPatrick Venture      */
5855646decSPatrick Venture     ipmiblob::StatResponse stat;
5955646decSPatrick Venture 
6055646decSPatrick Venture     try
6155646decSPatrick Venture     {
6255646decSPatrick Venture         stat = blob->getStat(goalFirmware);
6355646decSPatrick Venture     }
6455646decSPatrick Venture     catch (const ipmiblob::BlobException& b)
6555646decSPatrick Venture     {
6655646decSPatrick Venture         std::fprintf(stderr, "Received exception '%s' on getStat\n", b.what());
6755646decSPatrick Venture         return false;
6855646decSPatrick Venture     }
6955646decSPatrick Venture 
7055646decSPatrick Venture     auto supported = handler->supportedType();
7155646decSPatrick Venture     if ((stat.blob_state & supported) == 0)
7255646decSPatrick Venture     {
7355646decSPatrick Venture         std::fprintf(stderr, "data interface selected not supported.\n");
7455646decSPatrick Venture         return false;
7555646decSPatrick Venture     }
7655646decSPatrick Venture 
7755646decSPatrick Venture     return true;
7855646decSPatrick Venture }
7955646decSPatrick Venture 
8055646decSPatrick Venture void UpdateHandler::sendFile(const std::string& target, const std::string& path)
8155646decSPatrick Venture {
8255646decSPatrick Venture     std::uint16_t session;
8355646decSPatrick Venture     auto supported = handler->supportedType();
8455646decSPatrick Venture 
8555646decSPatrick Venture     try
8655646decSPatrick Venture     {
8755646decSPatrick Venture         session = blob->openBlob(
8855646decSPatrick Venture             target, static_cast<std::uint16_t>(supported) |
8955646decSPatrick Venture                         static_cast<std::uint16_t>(blobs::OpenFlags::write));
9055646decSPatrick Venture     }
9155646decSPatrick Venture     catch (const ipmiblob::BlobException& b)
9255646decSPatrick Venture     {
9355646decSPatrick Venture         throw ToolException("blob exception received: " +
9455646decSPatrick Venture                             std::string(b.what()));
9555646decSPatrick Venture     }
9655646decSPatrick Venture 
9755646decSPatrick Venture     if (!handler->sendContents(path, session))
9855646decSPatrick Venture     {
9955646decSPatrick Venture         /* Need to close the session on failure, or it's stuck open (until the
10055646decSPatrick Venture          * blob handler timeout is implemented, and even then, why make it wait.
10155646decSPatrick Venture          */
10255646decSPatrick Venture         blob->closeBlob(session);
10355646decSPatrick Venture         throw ToolException("Failed to send contents of " + path);
10455646decSPatrick Venture     }
10555646decSPatrick Venture 
10655646decSPatrick Venture     blob->closeBlob(session);
10755646decSPatrick Venture }
10855646decSPatrick Venture 
109d61b0ff8SPatrick Venture /* Poll an open verification session.  Handling closing the session is not yet
110d61b0ff8SPatrick Venture  * owned by this method. */
111d61b0ff8SPatrick Venture bool pollVerificationStatus(std::uint16_t session,
112d61b0ff8SPatrick Venture                             ipmiblob::BlobInterface* blob)
113d61b0ff8SPatrick Venture {
114d61b0ff8SPatrick Venture     using namespace std::chrono_literals;
115d61b0ff8SPatrick Venture 
116d61b0ff8SPatrick Venture     static constexpr auto verificationSleep = 5s;
117d61b0ff8SPatrick Venture     static constexpr int commandAttempts = 20;
118d61b0ff8SPatrick Venture     int attempts = 0;
119d61b0ff8SPatrick Venture     bool exitLoop = false;
120*1d5a31c9SPatrick Venture     ipmi_flash::VerifyCheckResponses result =
121*1d5a31c9SPatrick Venture         ipmi_flash::VerifyCheckResponses::other;
122d61b0ff8SPatrick Venture 
123d61b0ff8SPatrick Venture     try
124d61b0ff8SPatrick Venture     {
125d61b0ff8SPatrick Venture         /* Reach back the current status from the verification service output.
126d61b0ff8SPatrick Venture          */
127d61b0ff8SPatrick Venture         while (attempts++ < commandAttempts)
128d61b0ff8SPatrick Venture         {
129d61b0ff8SPatrick Venture             ipmiblob::StatResponse resp = blob->getStat(session);
130d61b0ff8SPatrick Venture 
131d61b0ff8SPatrick Venture             if (resp.metadata.size() != sizeof(std::uint8_t))
132d61b0ff8SPatrick Venture             {
133d61b0ff8SPatrick Venture                 /* TODO: How do we want to handle the verification failures,
134d61b0ff8SPatrick Venture                  * because closing the session to the verify blob has a special
135d61b0ff8SPatrick Venture                  * as-of-yet not fully defined behavior.
136d61b0ff8SPatrick Venture                  */
137d61b0ff8SPatrick Venture                 std::fprintf(stderr, "Received invalid metadata response!!!\n");
138d61b0ff8SPatrick Venture             }
139d61b0ff8SPatrick Venture 
140*1d5a31c9SPatrick Venture             result =
141*1d5a31c9SPatrick Venture                 static_cast<ipmi_flash::VerifyCheckResponses>(resp.metadata[0]);
142d61b0ff8SPatrick Venture 
143d61b0ff8SPatrick Venture             switch (result)
144d61b0ff8SPatrick Venture             {
145*1d5a31c9SPatrick Venture                 case ipmi_flash::VerifyCheckResponses::failed:
146d61b0ff8SPatrick Venture                     std::fprintf(stderr, "failed\n");
147d61b0ff8SPatrick Venture                     exitLoop = true;
148d61b0ff8SPatrick Venture                     break;
149*1d5a31c9SPatrick Venture                 case ipmi_flash::VerifyCheckResponses::other:
150d61b0ff8SPatrick Venture                     std::fprintf(stderr, "other\n");
151d61b0ff8SPatrick Venture                     break;
152*1d5a31c9SPatrick Venture                 case ipmi_flash::VerifyCheckResponses::running:
153d61b0ff8SPatrick Venture                     std::fprintf(stderr, "running\n");
154d61b0ff8SPatrick Venture                     break;
155*1d5a31c9SPatrick Venture                 case ipmi_flash::VerifyCheckResponses::success:
156d61b0ff8SPatrick Venture                     std::fprintf(stderr, "success\n");
157d61b0ff8SPatrick Venture                     exitLoop = true;
158d61b0ff8SPatrick Venture                     break;
159d61b0ff8SPatrick Venture                 default:
160d61b0ff8SPatrick Venture                     std::fprintf(stderr, "wat\n");
161d61b0ff8SPatrick Venture             }
162d61b0ff8SPatrick Venture 
163d61b0ff8SPatrick Venture             if (exitLoop)
164d61b0ff8SPatrick Venture             {
165d61b0ff8SPatrick Venture                 break;
166d61b0ff8SPatrick Venture             }
167d61b0ff8SPatrick Venture             std::this_thread::sleep_for(verificationSleep);
168d61b0ff8SPatrick Venture         }
169d61b0ff8SPatrick Venture     }
170d61b0ff8SPatrick Venture     catch (const ipmiblob::BlobException& b)
171d61b0ff8SPatrick Venture     {
172d61b0ff8SPatrick Venture         throw ToolException("blob exception received: " +
173d61b0ff8SPatrick Venture                             std::string(b.what()));
174d61b0ff8SPatrick Venture     }
175d61b0ff8SPatrick Venture 
176d61b0ff8SPatrick Venture     /* TODO: If this is reached and it's not success, it may be worth just
177d61b0ff8SPatrick Venture      * throwing a ToolException with a timeout message specifying the final
178d61b0ff8SPatrick Venture      * read's value.
179d61b0ff8SPatrick Venture      *
180d61b0ff8SPatrick Venture      * TODO: Given that excepting from certain points leaves the BMC update
181d61b0ff8SPatrick Venture      * state machine in an inconsistent state, we need to carefully evaluate
182d61b0ff8SPatrick Venture      * which exceptions from the lower layers allow one to try and delete the
183d61b0ff8SPatrick Venture      * blobs to rollback the state and progress.
184d61b0ff8SPatrick Venture      */
185*1d5a31c9SPatrick Venture     return (result == ipmi_flash::VerifyCheckResponses::success);
186d61b0ff8SPatrick Venture }
187d61b0ff8SPatrick Venture 
18855646decSPatrick Venture bool UpdateHandler::verifyFile(const std::string& target)
189bf58cd64SPatrick Venture {
1900533d0b0SPatrick Venture     std::uint16_t session;
19155646decSPatrick Venture     bool success = false;
19255646decSPatrick Venture 
1930533d0b0SPatrick Venture     try
1940533d0b0SPatrick Venture     {
195664c5bc7SPatrick Venture         session = blob->openBlob(
19655646decSPatrick Venture             target, static_cast<std::uint16_t>(blobs::OpenFlags::write));
1977dcca5ddSPatrick Venture     }
1987dcca5ddSPatrick Venture     catch (const ipmiblob::BlobException& b)
1997dcca5ddSPatrick Venture     {
2007dcca5ddSPatrick Venture         throw ToolException("blob exception received: " +
2017dcca5ddSPatrick Venture                             std::string(b.what()));
2027dcca5ddSPatrick Venture     }
2037dcca5ddSPatrick Venture 
2047dcca5ddSPatrick Venture     std::fprintf(
2057dcca5ddSPatrick Venture         stderr,
2067dcca5ddSPatrick Venture         "Committing to verification file to trigger verification service\n");
20755646decSPatrick Venture 
2087dcca5ddSPatrick Venture     try
2097dcca5ddSPatrick Venture     {
2107dcca5ddSPatrick Venture         blob->commit(session, {});
2117dcca5ddSPatrick Venture     }
2127dcca5ddSPatrick Venture     catch (const ipmiblob::BlobException& b)
2137dcca5ddSPatrick Venture     {
2147dcca5ddSPatrick Venture         throw ToolException("blob exception received: " +
2157dcca5ddSPatrick Venture                             std::string(b.what()));
2167dcca5ddSPatrick Venture     }
2177dcca5ddSPatrick Venture 
218d61b0ff8SPatrick Venture     std::fprintf(stderr,
219d61b0ff8SPatrick Venture                  "Calling stat on verification session to check status\n");
2207dcca5ddSPatrick Venture 
221d61b0ff8SPatrick Venture     if (pollVerificationStatus(session, blob))
222d61b0ff8SPatrick Venture     {
223d61b0ff8SPatrick Venture         std::fprintf(stderr, "Verification returned success\n");
22455646decSPatrick Venture         success = true;
225d61b0ff8SPatrick Venture     }
226d61b0ff8SPatrick Venture     else
227d61b0ff8SPatrick Venture     {
228d61b0ff8SPatrick Venture         std::fprintf(stderr, "Verification returned non-success (could still "
229d61b0ff8SPatrick Venture                              "be running (unlikely))\n");
230d61b0ff8SPatrick Venture     }
231d61b0ff8SPatrick Venture 
2327dcca5ddSPatrick Venture     blob->closeBlob(session);
23355646decSPatrick Venture     return (success == true);
23455646decSPatrick Venture }
23555646decSPatrick Venture 
23655646decSPatrick Venture void updaterMain(UpdateHandler* updater, const std::string& imagePath,
23755646decSPatrick Venture                  const std::string& signaturePath)
23855646decSPatrick Venture {
23955646decSPatrick Venture     /* TODO(venture): Add optional parameter to specify the flash type, default
24055646decSPatrick Venture      * to legacy for now.
24155646decSPatrick Venture      */
242*1d5a31c9SPatrick Venture     bool goalSupported =
243*1d5a31c9SPatrick Venture         updater->checkAvailable(ipmi_flash::staticLayoutBlobId);
24455646decSPatrick Venture     if (!goalSupported)
24555646decSPatrick Venture     {
24655646decSPatrick Venture         throw ToolException("Goal firmware or interface not supported");
24755646decSPatrick Venture     }
24855646decSPatrick Venture 
24955646decSPatrick Venture     /* Yay, our data handler is supported. */
25055646decSPatrick Venture 
25155646decSPatrick Venture     /* Send over the firmware image. */
25255646decSPatrick Venture     std::fprintf(stderr, "Sending over the firmware image.\n");
253*1d5a31c9SPatrick Venture     updater->sendFile(ipmi_flash::staticLayoutBlobId, imagePath);
25455646decSPatrick Venture 
25555646decSPatrick Venture     /* Send over the hash contents. */
25655646decSPatrick Venture     std::fprintf(stderr, "Sending over the hash file.\n");
257*1d5a31c9SPatrick Venture     updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
25855646decSPatrick Venture 
25955646decSPatrick Venture     /* Trigger the verification by opening the verify file. */
26055646decSPatrick Venture     std::fprintf(stderr, "Opening the verification file\n");
261*1d5a31c9SPatrick Venture     if (updater->verifyFile(ipmi_flash::verifyBlobId))
26255646decSPatrick Venture     {
26355646decSPatrick Venture         std::fprintf(stderr, "succeeded\n");
26455646decSPatrick Venture     }
26555646decSPatrick Venture     else
26655646decSPatrick Venture     {
26755646decSPatrick Venture         std::fprintf(stderr, "failed\n");
26855646decSPatrick Venture     }
269bf58cd64SPatrick Venture }
2709b534f06SPatrick Venture 
2719b534f06SPatrick Venture } // namespace host_tool
272