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 "flags.hpp"
20 #include "handler.hpp"
21 #include "status.hpp"
22 #include "tool_errors.hpp"
23 #include "util.hpp"
24 
25 #include <algorithm>
26 #include <cstring>
27 #include <ipmiblob/blob_errors.hpp>
28 #include <memory>
29 #include <string>
30 #include <thread>
31 #include <unordered_map>
32 #include <vector>
33 
34 namespace host_tool
35 {
36 
37 void updaterMain(UpdateHandlerInterface* updater, const std::string& imagePath,
38                  const std::string& signaturePath,
39                  const std::string& layoutType)
40 {
41     static std::unordered_map<std::string, std::string> typesToBlob = {
42         {"static", ipmi_flash::staticLayoutBlobId},
43         {"ubitar", ipmi_flash::ubiTarballBlobId},
44         {"bios", ipmi_flash::biosBlobId}};
45     /* We know it's one of the above types already. */
46     auto& layout = typesToBlob[layoutType];
47 
48     bool goalSupported = updater->checkAvailable(layout);
49     if (!goalSupported)
50     {
51         throw ToolException("Goal firmware or interface not supported");
52     }
53 
54     /* Yay, our data handler is supported. */
55     try
56     {
57         /* Send over the firmware image. */
58         std::fprintf(stderr, "Sending over the firmware image.\n");
59         updater->sendFile(layout, imagePath);
60 
61         /* Send over the hash contents. */
62         std::fprintf(stderr, "Sending over the hash file.\n");
63         updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
64 
65         /* Trigger the verification by opening and committing the verify file.
66          */
67         std::fprintf(stderr, "Opening the verification file\n");
68         if (updater->verifyFile(ipmi_flash::verifyBlobId))
69         {
70             std::fprintf(stderr, "succeeded\n");
71         }
72         else
73         {
74             std::fprintf(stderr, "failed\n");
75             throw ToolException("Verification failed");
76         }
77 
78         /* Trigger the update by opening and committing the update file. */
79         std::fprintf(stderr, "Opening the update file\n");
80         if (updater->verifyFile(ipmi_flash::updateBlobId))
81         {
82             std::fprintf(stderr, "succeeded\n");
83         }
84         else
85         {
86             /* Depending on the update mechanism used, this may be
87              * uninteresting. For instance, for the static layout, we use the
88              * reboot update mechanism.  Which doesn't always lead to a
89              * successful return before the BMC starts shutting down services.
90              */
91             std::fprintf(stderr, "failed\n");
92             throw ToolException("Update failed");
93         }
94     }
95     catch (...)
96     {
97         updater->cleanArtifacts();
98         throw;
99     }
100 }
101 
102 } // namespace host_tool
103