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 "handler.hpp"
18 
19 #include "flags.hpp"
20 #include "helper.hpp"
21 #include "status.hpp"
22 #include "tool_errors.hpp"
23 #include "util.hpp"
24 
25 #include <algorithm>
26 #include <cstdint>
27 #include <cstring>
28 #include <ipmiblob/blob_errors.hpp>
29 #include <string>
30 #include <vector>
31 
32 namespace host_tool
33 {
34 
35 bool UpdateHandler::checkAvailable(const std::string& goalFirmware)
36 {
37     std::vector<std::string> blobs = blob->getBlobList();
38 
39     auto blobInst = std::find_if(
40         blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
41             /* Running into weird scenarios where the string comparison doesn't
42              * work.  TODO: revisit.
43              */
44             return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
45                                      goalFirmware.length()));
46             // return (goalFirmware.compare(iter));
47         });
48     if (blobInst == blobs.end())
49     {
50         std::fprintf(stderr, "%s not found\n", goalFirmware.c_str());
51         return false;
52     }
53 
54     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
55      * is supported.
56      */
57     ipmiblob::StatResponse stat;
58 
59     try
60     {
61         stat = blob->getStat(goalFirmware);
62     }
63     catch (const ipmiblob::BlobException& b)
64     {
65         std::fprintf(stderr, "Received exception '%s' on getStat\n", b.what());
66         return false;
67     }
68 
69     auto supported = handler->supportedType();
70     if ((stat.blob_state & supported) == 0)
71     {
72         std::fprintf(stderr, "data interface selected not supported.\n");
73         return false;
74     }
75 
76     return true;
77 }
78 
79 void UpdateHandler::sendFile(const std::string& target, const std::string& path)
80 {
81     std::uint16_t session;
82     auto supported = handler->supportedType();
83 
84     try
85     {
86         session = blob->openBlob(
87             target, static_cast<std::uint16_t>(supported) |
88                         static_cast<std::uint16_t>(
89                             ipmi_flash::FirmwareFlags::UpdateFlags::openWrite));
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 bool UpdateHandler::verifyFile(const std::string& target)
110 {
111     std::uint16_t session;
112     bool success = false;
113 
114     try
115     {
116         session = blob->openBlob(
117             target, static_cast<std::uint16_t>(
118                         ipmi_flash::FirmwareFlags::UpdateFlags::openWrite));
119     }
120     catch (const ipmiblob::BlobException& b)
121     {
122         throw ToolException("blob exception received: " +
123                             std::string(b.what()));
124     }
125 
126     std::fprintf(stderr, "Committing to %s to trigger service\n",
127                  target.c_str());
128 
129     try
130     {
131         blob->commit(session, {});
132     }
133     catch (const ipmiblob::BlobException& b)
134     {
135         blob->closeBlob(session);
136         throw ToolException("blob exception received: " +
137                             std::string(b.what()));
138     }
139 
140     std::fprintf(stderr, "Calling stat on %s session to check status\n",
141                  target.c_str());
142 
143     if (pollStatus(session, blob))
144     {
145         std::fprintf(stderr, "Returned success\n");
146         success = true;
147     }
148     else
149     {
150         std::fprintf(stderr, "Returned non-success (could still "
151                              "be running (unlikely))\n");
152     }
153 
154     blob->closeBlob(session);
155     return (success == true);
156 }
157 
158 void UpdateHandler::cleanArtifacts()
159 {
160     /* open(), commit(), close() */
161     std::uint16_t session;
162 
163     /* Errors aren't important for this call. */
164     try
165     {
166         std::fprintf(stderr, "Opening the cleanup blob\n");
167         session = blob->openBlob(
168             ipmi_flash::cleanupBlobId,
169             static_cast<std::uint16_t>(
170                 ipmi_flash::FirmwareFlags::UpdateFlags::openWrite));
171     }
172     catch (...)
173     {
174         return;
175     }
176 
177     try
178     {
179         std::fprintf(stderr, "Committing to the cleanup blob\n");
180         blob->commit(session, {});
181         std::fprintf(stderr, "Closing cleanup blob\n");
182     }
183     catch (...)
184     {
185     }
186 
187     blob->closeBlob(session);
188 }
189 
190 } // namespace host_tool
191