1bf58cd64SPatrick Venture /*
2bf58cd64SPatrick Venture  * Copyright 2018 Google Inc.
3bf58cd64SPatrick Venture  *
4bf58cd64SPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5bf58cd64SPatrick Venture  * you may not use this file except in compliance with the License.
6bf58cd64SPatrick Venture  * You may obtain a copy of the License at
7bf58cd64SPatrick Venture  *
8bf58cd64SPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9bf58cd64SPatrick Venture  *
10bf58cd64SPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11bf58cd64SPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12bf58cd64SPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf58cd64SPatrick Venture  * See the License for the specific language governing permissions and
14bf58cd64SPatrick Venture  * limitations under the License.
15bf58cd64SPatrick Venture  */
16bf58cd64SPatrick Venture 
17bf58cd64SPatrick Venture #include "updater.hpp"
18bf58cd64SPatrick Venture 
19d61b0ff8SPatrick Venture #include "firmware_handler.hpp"
2001123b2aSPatrick Venture #include "handler.hpp"
213ecb3503SPatrick Venture #include "status.hpp"
222bc23fe1SPatrick Venture #include "tool_errors.hpp"
237dad86fdSPatrick Venture #include "util.hpp"
240533d0b0SPatrick Venture 
2500887597SPatrick Venture #include <algorithm>
26664c5bc7SPatrick Venture #include <blobs-ipmid/blobs.hpp>
27339dece8SPatrick Venture #include <cstring>
28664c5bc7SPatrick Venture #include <ipmiblob/blob_errors.hpp>
29af69625fSPatrick Venture #include <memory>
302a927e87SPatrick Venture #include <string>
31d61b0ff8SPatrick Venture #include <thread>
3255646decSPatrick Venture #include <vector>
33af69625fSPatrick Venture 
349b534f06SPatrick Venture namespace host_tool
359b534f06SPatrick Venture {
369b534f06SPatrick Venture 
371f09d414SPatrick Venture void updaterMain(UpdateHandlerInterface* updater, const std::string& imagePath,
38*9f937c45SPatrick Venture                  const std::string& signaturePath,
39*9f937c45SPatrick Venture                  const std::string& layoutType)
4055646decSPatrick Venture {
41*9f937c45SPatrick Venture     const auto& layout = (layoutType == "static")
42*9f937c45SPatrick Venture                              ? ipmi_flash::staticLayoutBlobId
43*9f937c45SPatrick Venture                              : ipmi_flash::ubiTarballBlobId;
44*9f937c45SPatrick Venture 
45*9f937c45SPatrick Venture     bool goalSupported = updater->checkAvailable(layout);
4655646decSPatrick Venture     if (!goalSupported)
4755646decSPatrick Venture     {
4855646decSPatrick Venture         throw ToolException("Goal firmware or interface not supported");
4955646decSPatrick Venture     }
5055646decSPatrick Venture 
5155646decSPatrick Venture     /* Yay, our data handler is supported. */
525f2fcc4eSPatrick Venture     try
535f2fcc4eSPatrick Venture     {
5455646decSPatrick Venture         /* Send over the firmware image. */
5555646decSPatrick Venture         std::fprintf(stderr, "Sending over the firmware image.\n");
56*9f937c45SPatrick Venture         updater->sendFile(layout, imagePath);
5755646decSPatrick Venture 
5855646decSPatrick Venture         /* Send over the hash contents. */
5955646decSPatrick Venture         std::fprintf(stderr, "Sending over the hash file.\n");
601d5a31c9SPatrick Venture         updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
6155646decSPatrick Venture 
625f2fcc4eSPatrick Venture         /* Trigger the verification by opening and committing the verify file.
635f2fcc4eSPatrick Venture          */
6455646decSPatrick Venture         std::fprintf(stderr, "Opening the verification file\n");
651d5a31c9SPatrick Venture         if (updater->verifyFile(ipmi_flash::verifyBlobId))
6655646decSPatrick Venture         {
6755646decSPatrick Venture             std::fprintf(stderr, "succeeded\n");
6855646decSPatrick Venture         }
6955646decSPatrick Venture         else
7055646decSPatrick Venture         {
7155646decSPatrick Venture             std::fprintf(stderr, "failed\n");
7214713becSPatrick Venture             throw ToolException("Verification failed");
7314713becSPatrick Venture         }
7414713becSPatrick Venture 
7514713becSPatrick Venture         /* Trigger the update by opening and committing the update file. */
7614713becSPatrick Venture         std::fprintf(stderr, "Opening the update file\n");
7714713becSPatrick Venture         if (updater->verifyFile(ipmi_flash::updateBlobId))
7814713becSPatrick Venture         {
7914713becSPatrick Venture             std::fprintf(stderr, "succeeded\n");
8014713becSPatrick Venture         }
8114713becSPatrick Venture         else
8214713becSPatrick Venture         {
835f2fcc4eSPatrick Venture             /* Depending on the update mechanism used, this may be
845f2fcc4eSPatrick Venture              * uninteresting. For instance, for the static layout, we use the
855f2fcc4eSPatrick Venture              * reboot update mechanism.  Which doesn't always lead to a
865f2fcc4eSPatrick Venture              * successful return before the BMC starts shutting down services.
8714713becSPatrick Venture              */
8814713becSPatrick Venture             std::fprintf(stderr, "failed\n");
8914713becSPatrick Venture             throw ToolException("Update failed");
9055646decSPatrick Venture         }
91bf58cd64SPatrick Venture     }
925f2fcc4eSPatrick Venture     catch (...)
935f2fcc4eSPatrick Venture     {
945f2fcc4eSPatrick Venture         updater->cleanArtifacts();
955f2fcc4eSPatrick Venture         throw;
965f2fcc4eSPatrick Venture     }
975f2fcc4eSPatrick Venture }
989b534f06SPatrick Venture 
999b534f06SPatrick Venture } // namespace host_tool
100