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 
1984778b8dSPatrick 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 
259b37b095SPatrick Venture #include <ipmiblob/blob_errors.hpp>
269b37b095SPatrick Venture 
2700887597SPatrick Venture #include <algorithm>
28339dece8SPatrick Venture #include <cstring>
29af69625fSPatrick Venture #include <memory>
302a927e87SPatrick Venture #include <string>
31d61b0ff8SPatrick Venture #include <thread>
32c9792e75SPatrick Venture #include <unordered_map>
3355646decSPatrick Venture #include <vector>
34af69625fSPatrick Venture 
359b534f06SPatrick Venture namespace host_tool
369b534f06SPatrick Venture {
379b534f06SPatrick Venture 
updaterMain(UpdateHandlerInterface * updater,ipmiblob::BlobInterface * blob,const std::string & imagePath,const std::string & signaturePath,const std::string & layoutType,bool ignoreUpdate)38203ad804SWilly Tu void updaterMain(UpdateHandlerInterface* updater, ipmiblob::BlobInterface* blob,
39203ad804SWilly Tu                  const std::string& imagePath, const std::string& signaturePath,
406749ba1cSBrandon Kim                  const std::string& layoutType, bool ignoreUpdate)
4155646decSPatrick Venture {
42c498caa4SPatrick Venture     /* TODO: validate the layoutType isn't a special value such as: 'update',
43c498caa4SPatrick Venture      * 'verify', or 'hash'
44c498caa4SPatrick Venture      */
45c498caa4SPatrick Venture     std::string layout = "/flash/" + layoutType;
469f937c45SPatrick Venture 
479f937c45SPatrick Venture     bool goalSupported = updater->checkAvailable(layout);
4855646decSPatrick Venture     if (!goalSupported)
4955646decSPatrick Venture     {
50060be01fSBenjamin Fair         throw ToolException("Goal firmware not supported");
5155646decSPatrick Venture     }
5255646decSPatrick Venture 
53203ad804SWilly Tu     // Clean all active blobs to support multiple stages
54203ad804SWilly Tu     // Check for any active blobs and delete the first one found to reset the
55203ad804SWilly Tu     // BMC's phosphor-ipmi-flash state machine then clean any leftover artifacts
56203ad804SWilly Tu     const auto blobList = blob->getBlobList();
57203ad804SWilly Tu     for (const auto& activeBlob : blobList)
58203ad804SWilly Tu     {
59203ad804SWilly Tu         // Prefix is /flash/active/
60*7d249a7aSWilly Tu         if (activeBlob.starts_with("/flash/active/"))
61203ad804SWilly Tu         {
62203ad804SWilly Tu             std::fprintf(stderr, "Found an active blob, deleting %s\n",
63203ad804SWilly Tu                          activeBlob.c_str());
64203ad804SWilly Tu             blob->deleteBlob(activeBlob);
65203ad804SWilly Tu             updater->cleanArtifacts();
66203ad804SWilly Tu             break;
67203ad804SWilly Tu         }
68203ad804SWilly Tu     }
69203ad804SWilly Tu 
70060be01fSBenjamin Fair     /* Yay, our layout type is supported. */
715f2fcc4eSPatrick Venture     try
725f2fcc4eSPatrick Venture     {
7355646decSPatrick Venture         /* Send over the firmware image. */
7455646decSPatrick Venture         std::fprintf(stderr, "Sending over the firmware image.\n");
759f937c45SPatrick Venture         updater->sendFile(layout, imagePath);
7655646decSPatrick Venture 
7755646decSPatrick Venture         /* Send over the hash contents. */
7855646decSPatrick Venture         std::fprintf(stderr, "Sending over the hash file.\n");
791d5a31c9SPatrick Venture         updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
8055646decSPatrick Venture 
815f2fcc4eSPatrick Venture         /* Trigger the verification by opening and committing the verify file.
825f2fcc4eSPatrick Venture          */
8355646decSPatrick Venture         std::fprintf(stderr, "Opening the verification file\n");
846749ba1cSBrandon Kim         if (updater->verifyFile(ipmi_flash::verifyBlobId, false))
8555646decSPatrick Venture         {
8655646decSPatrick Venture             std::fprintf(stderr, "succeeded\n");
8755646decSPatrick Venture         }
8855646decSPatrick Venture         else
8955646decSPatrick Venture         {
9055646decSPatrick Venture             std::fprintf(stderr, "failed\n");
9114713becSPatrick Venture             throw ToolException("Verification failed");
9214713becSPatrick Venture         }
9314713becSPatrick Venture 
9414713becSPatrick Venture         /* Trigger the update by opening and committing the update file. */
9514713becSPatrick Venture         std::fprintf(stderr, "Opening the update file\n");
966749ba1cSBrandon Kim         if (updater->verifyFile(ipmi_flash::updateBlobId, ignoreUpdate))
9714713becSPatrick Venture         {
9814713becSPatrick Venture             std::fprintf(stderr, "succeeded\n");
9914713becSPatrick Venture         }
10014713becSPatrick Venture         else
10114713becSPatrick Venture         {
1025f2fcc4eSPatrick Venture             /* Depending on the update mechanism used, this may be
1035f2fcc4eSPatrick Venture              * uninteresting. For instance, for the static layout, we use the
1045f2fcc4eSPatrick Venture              * reboot update mechanism.  Which doesn't always lead to a
1055f2fcc4eSPatrick Venture              * successful return before the BMC starts shutting down services.
10614713becSPatrick Venture              */
10714713becSPatrick Venture             std::fprintf(stderr, "failed\n");
10814713becSPatrick Venture             throw ToolException("Update failed");
10955646decSPatrick Venture         }
110bf58cd64SPatrick Venture     }
1115f2fcc4eSPatrick Venture     catch (...)
1125f2fcc4eSPatrick Venture     {
1135f2fcc4eSPatrick Venture         updater->cleanArtifacts();
1145f2fcc4eSPatrick Venture         throw;
1155f2fcc4eSPatrick Venture     }
1165f2fcc4eSPatrick Venture }
1179b534f06SPatrick Venture 
1189b534f06SPatrick Venture } // namespace host_tool
119