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