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     std::string hashFilename = "/flash/hash";
39 
40     /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
41      * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
42      * will have in mind which they're sending and we need to verify it's
43      * available and use it.
44      */
45     std::vector<std::string> blobs = blob->getBlobList();
46     auto blobInst = std::find_if(
47         blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
48             /* Running into weird scenarios where the string comparison doesn't
49              * work.  TODO: revisit.
50              */
51             return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
52                                      goalFirmware.length()));
53             // return (goalFirmware.compare(iter));
54         });
55     if (blobInst == blobs.end())
56     {
57         throw ToolException(goalFirmware + " not found");
58     }
59 
60     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
61      * is supported.
62      */
63     ipmiblob::StatResponse stat;
64     try
65     {
66         stat = blob->getStat(goalFirmware);
67     }
68     catch (const ipmiblob::BlobException& b)
69     {
70         throw ToolException("blob exception received: " +
71                             std::string(b.what()));
72     }
73 
74     auto supported = handler->supportedType();
75     if ((stat.blob_state & supported) == 0)
76     {
77         throw ToolException("data interface selected not supported.");
78     }
79 
80     /* Yay, our data handler is supported. */
81 
82     /* Send over the firmware image. */
83     std::fprintf(stderr, "Sending over the firmware image.\n");
84     std::uint16_t session;
85     try
86     {
87         session = blob->openBlob(
88             goalFirmware,
89             static_cast<std::uint16_t>(supported) |
90                 static_cast<std::uint16_t>(blobs::OpenFlags::write));
91     }
92     catch (const ipmiblob::BlobException& b)
93     {
94         throw ToolException("blob exception received: " +
95                             std::string(b.what()));
96     }
97 
98     if (!handler->sendContents(imagePath, session))
99     {
100         /* Need to close the session on failure, or it's stuck open (until the
101          * blob handler timeout is implemented, and even then, why make it wait.
102          */
103         blob->closeBlob(session);
104         throw ToolException("Failed to send contents of " + imagePath);
105     }
106 
107     blob->closeBlob(session);
108 
109     /* Send over the hash contents. */
110     std::fprintf(stderr, "Sending over the hash file.\n");
111     try
112     {
113         session = blob->openBlob(
114             hashFilename,
115             static_cast<std::uint16_t>(supported) |
116                 static_cast<std::uint16_t>(blobs::OpenFlags::write));
117     }
118     catch (const ipmiblob::BlobException& b)
119     {
120         throw ToolException("blob exception received: " +
121                             std::string(b.what()));
122     }
123 
124     if (!handler->sendContents(signaturePath, session))
125     {
126         blob->closeBlob(session);
127         throw ToolException("Failed to send contents of " + signaturePath);
128     }
129 
130     blob->closeBlob(session);
131 
132     /* Trigger the verification. */
133     /* Check the verification. */
134 
135     return;
136 }
137 
138 } // namespace host_tool
139