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 
19*84778b8dSPatrick Venture #include "flags.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>
26339dece8SPatrick Venture #include <cstring>
27664c5bc7SPatrick Venture #include <ipmiblob/blob_errors.hpp>
28af69625fSPatrick Venture #include <memory>
292a927e87SPatrick Venture #include <string>
30d61b0ff8SPatrick Venture #include <thread>
3155646decSPatrick Venture #include <vector>
32af69625fSPatrick Venture 
339b534f06SPatrick Venture namespace host_tool
349b534f06SPatrick Venture {
359b534f06SPatrick Venture 
361f09d414SPatrick Venture void updaterMain(UpdateHandlerInterface* updater, const std::string& imagePath,
379f937c45SPatrick Venture                  const std::string& signaturePath,
389f937c45SPatrick Venture                  const std::string& layoutType)
3955646decSPatrick Venture {
409f937c45SPatrick Venture     const auto& layout = (layoutType == "static")
419f937c45SPatrick Venture                              ? ipmi_flash::staticLayoutBlobId
429f937c45SPatrick Venture                              : ipmi_flash::ubiTarballBlobId;
439f937c45SPatrick Venture 
449f937c45SPatrick Venture     bool goalSupported = updater->checkAvailable(layout);
4555646decSPatrick Venture     if (!goalSupported)
4655646decSPatrick Venture     {
4755646decSPatrick Venture         throw ToolException("Goal firmware or interface not supported");
4855646decSPatrick Venture     }
4955646decSPatrick Venture 
5055646decSPatrick Venture     /* Yay, our data handler is supported. */
515f2fcc4eSPatrick Venture     try
525f2fcc4eSPatrick Venture     {
5355646decSPatrick Venture         /* Send over the firmware image. */
5455646decSPatrick Venture         std::fprintf(stderr, "Sending over the firmware image.\n");
559f937c45SPatrick Venture         updater->sendFile(layout, imagePath);
5655646decSPatrick Venture 
5755646decSPatrick Venture         /* Send over the hash contents. */
5855646decSPatrick Venture         std::fprintf(stderr, "Sending over the hash file.\n");
591d5a31c9SPatrick Venture         updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
6055646decSPatrick Venture 
615f2fcc4eSPatrick Venture         /* Trigger the verification by opening and committing the verify file.
625f2fcc4eSPatrick Venture          */
6355646decSPatrick Venture         std::fprintf(stderr, "Opening the verification file\n");
641d5a31c9SPatrick Venture         if (updater->verifyFile(ipmi_flash::verifyBlobId))
6555646decSPatrick Venture         {
6655646decSPatrick Venture             std::fprintf(stderr, "succeeded\n");
6755646decSPatrick Venture         }
6855646decSPatrick Venture         else
6955646decSPatrick Venture         {
7055646decSPatrick Venture             std::fprintf(stderr, "failed\n");
7114713becSPatrick Venture             throw ToolException("Verification failed");
7214713becSPatrick Venture         }
7314713becSPatrick Venture 
7414713becSPatrick Venture         /* Trigger the update by opening and committing the update file. */
7514713becSPatrick Venture         std::fprintf(stderr, "Opening the update file\n");
7614713becSPatrick Venture         if (updater->verifyFile(ipmi_flash::updateBlobId))
7714713becSPatrick Venture         {
7814713becSPatrick Venture             std::fprintf(stderr, "succeeded\n");
7914713becSPatrick Venture         }
8014713becSPatrick Venture         else
8114713becSPatrick Venture         {
825f2fcc4eSPatrick Venture             /* Depending on the update mechanism used, this may be
835f2fcc4eSPatrick Venture              * uninteresting. For instance, for the static layout, we use the
845f2fcc4eSPatrick Venture              * reboot update mechanism.  Which doesn't always lead to a
855f2fcc4eSPatrick Venture              * successful return before the BMC starts shutting down services.
8614713becSPatrick Venture              */
8714713becSPatrick Venture             std::fprintf(stderr, "failed\n");
8814713becSPatrick Venture             throw ToolException("Update failed");
8955646decSPatrick Venture         }
90bf58cd64SPatrick Venture     }
915f2fcc4eSPatrick Venture     catch (...)
925f2fcc4eSPatrick Venture     {
935f2fcc4eSPatrick Venture         updater->cleanArtifacts();
945f2fcc4eSPatrick Venture         throw;
955f2fcc4eSPatrick Venture     }
965f2fcc4eSPatrick Venture }
979b534f06SPatrick Venture 
989b534f06SPatrick Venture } // namespace host_tool
99