1 /*
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 
19 #include "firmware_handler.hpp"
20 #include "status.hpp"
21 #include "tool_errors.hpp"
22 #include "util.hpp"
23 
24 #include <algorithm>
25 #include <blobs-ipmid/blobs.hpp>
26 #include <cstring>
27 #include <ipmiblob/blob_errors.hpp>
28 #include <memory>
29 #include <string>
30 #include <thread>
31 #include <vector>
32 
33 namespace host_tool
34 {
35 
36 bool 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 
80 void 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 
109 /* Poll an open verification session.  Handling closing the session is not yet
110  * owned by this method. */
111 bool pollVerificationStatus(std::uint16_t session,
112                             ipmiblob::BlobInterface* blob)
113 {
114     using namespace std::chrono_literals;
115 
116     static constexpr auto verificationSleep = 5s;
117     ipmi_flash::ActionStatus result = ipmi_flash::ActionStatus::unknown;
118 
119     try
120     {
121         static constexpr int commandAttempts = 20;
122         int attempts = 0;
123         bool exitLoop = false;
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 
140             result = static_cast<ipmi_flash::ActionStatus>(resp.metadata[0]);
141 
142             switch (result)
143             {
144                 case ipmi_flash::ActionStatus::failed:
145                     std::fprintf(stderr, "failed\n");
146                     exitLoop = true;
147                     break;
148                 case ipmi_flash::ActionStatus::unknown:
149                     std::fprintf(stderr, "other\n");
150                     break;
151                 case ipmi_flash::ActionStatus::running:
152                     std::fprintf(stderr, "running\n");
153                     break;
154                 case ipmi_flash::ActionStatus::success:
155                     std::fprintf(stderr, "success\n");
156                     exitLoop = true;
157                     break;
158                 default:
159                     std::fprintf(stderr, "wat\n");
160             }
161 
162             if (exitLoop)
163             {
164                 break;
165             }
166             std::this_thread::sleep_for(verificationSleep);
167         }
168     }
169     catch (const ipmiblob::BlobException& b)
170     {
171         throw ToolException("blob exception received: " +
172                             std::string(b.what()));
173     }
174 
175     /* TODO: If this is reached and it's not success, it may be worth just
176      * throwing a ToolException with a timeout message specifying the final
177      * read's value.
178      *
179      * TODO: Given that excepting from certain points leaves the BMC update
180      * state machine in an inconsistent state, we need to carefully evaluate
181      * which exceptions from the lower layers allow one to try and delete the
182      * blobs to rollback the state and progress.
183      */
184     return (result == ipmi_flash::ActionStatus::success);
185 }
186 
187 bool UpdateHandler::verifyFile(const std::string& target)
188 {
189     std::uint16_t session;
190     bool success = false;
191 
192     try
193     {
194         session = blob->openBlob(
195             target, static_cast<std::uint16_t>(blobs::OpenFlags::write));
196     }
197     catch (const ipmiblob::BlobException& b)
198     {
199         throw ToolException("blob exception received: " +
200                             std::string(b.what()));
201     }
202 
203     std::fprintf(
204         stderr,
205         "Committing to verification file to trigger verification service\n");
206 
207     try
208     {
209         blob->commit(session, {});
210     }
211     catch (const ipmiblob::BlobException& b)
212     {
213         throw ToolException("blob exception received: " +
214                             std::string(b.what()));
215     }
216 
217     std::fprintf(stderr,
218                  "Calling stat on verification session to check status\n");
219 
220     if (pollVerificationStatus(session, blob))
221     {
222         std::fprintf(stderr, "Verification returned success\n");
223         success = true;
224     }
225     else
226     {
227         std::fprintf(stderr, "Verification returned non-success (could still "
228                              "be running (unlikely))\n");
229     }
230 
231     blob->closeBlob(session);
232     return (success == true);
233 }
234 
235 void updaterMain(UpdateHandler* updater, const std::string& imagePath,
236                  const std::string& signaturePath)
237 {
238     /* TODO(venture): Add optional parameter to specify the flash type, default
239      * to legacy for now.
240      */
241     bool goalSupported =
242         updater->checkAvailable(ipmi_flash::staticLayoutBlobId);
243     if (!goalSupported)
244     {
245         throw ToolException("Goal firmware or interface not supported");
246     }
247 
248     /* Yay, our data handler is supported. */
249 
250     /* Send over the firmware image. */
251     std::fprintf(stderr, "Sending over the firmware image.\n");
252     updater->sendFile(ipmi_flash::staticLayoutBlobId, imagePath);
253 
254     /* Send over the hash contents. */
255     std::fprintf(stderr, "Sending over the hash file.\n");
256     updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
257 
258     /* Trigger the verification by opening the verify file. */
259     std::fprintf(stderr, "Opening the verification file\n");
260     if (updater->verifyFile(ipmi_flash::verifyBlobId))
261     {
262         std::fprintf(stderr, "succeeded\n");
263     }
264     else
265     {
266         std::fprintf(stderr, "failed\n");
267     }
268 }
269 
270 } // namespace host_tool
271