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 "tool_errors.hpp"
20 
21 #include <algorithm>
22 #include <blobs-ipmid/blobs.hpp>
23 #include <cstring>
24 #include <ipmiblob/blob_errors.hpp>
25 #include <memory>
26 #include <string>
27 
28 namespace host_tool
29 {
30 
31 void updaterMain(ipmiblob::BlobInterface* blob, DataInterface* handler,
32                  const std::string& imagePath, const std::string& signaturePath)
33 {
34     /* TODO(venture): Add optional parameter to specify the flash type, default
35      * to legacy for now.
36      */
37     std::string goalFirmware = "/flash/image";
38 
39     /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
40      * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
41      * will have in mind which they're sending and we need to verify it's
42      * available and use it.
43      */
44     std::vector<std::string> blobs = blob->getBlobList();
45     auto blobInst = std::find_if(
46         blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
47             /* Running into weird scenarios where the string comparison doesn't
48              * work.  TODO: revisit.
49              */
50             return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
51                                      goalFirmware.length()));
52             // return (goalFirmware.compare(iter));
53         });
54     if (blobInst == blobs.end())
55     {
56         throw ToolException(goalFirmware + " not found");
57     }
58 
59     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
60      * is supported.
61      */
62     ipmiblob::StatResponse stat;
63     try
64     {
65         stat = blob->getStat(goalFirmware);
66     }
67     catch (const ipmiblob::BlobException& b)
68     {
69         throw ToolException("blob exception received: " +
70                             std::string(b.what()));
71     }
72 
73     auto supported = handler->supportedType();
74     if ((stat.blob_state & supported) == 0)
75     {
76         throw ToolException("data interface selected not supported.");
77     }
78 
79     /* Yay, our data handler is supported. */
80     std::uint16_t session;
81     try
82     {
83         session = blob->openBlob(
84             goalFirmware,
85             static_cast<std::uint16_t>(supported) |
86                 static_cast<std::uint16_t>(blobs::OpenFlags::write));
87     }
88     catch (const ipmiblob::BlobException& b)
89     {
90         throw ToolException("blob exception received: " +
91                             std::string(b.what()));
92     }
93 
94     /* Send over the firmware image. */
95     if (!handler->sendContents(imagePath, session))
96     {
97         /* Need to close the session on failure, or it's stuck open (until the
98          * blob handler timeout is implemented, and even then, why make it wait.
99          */
100         blob->closeBlob(session);
101         throw ToolException("Failed to send contents of " + imagePath);
102     }
103 
104     blob->closeBlob(session);
105 
106     /* Send over the hash contents. */
107     /* Trigger the verification. */
108     /* Check the verification. */
109 
110     return;
111 }
112 
113 } // namespace host_tool
114