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 
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>
31c9792e75SPatrick Venture #include <unordered_map>
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,
389f937c45SPatrick Venture                  const std::string& signaturePath,
396749ba1cSBrandon Kim                  const std::string& layoutType, bool ignoreUpdate)
4055646decSPatrick Venture {
41c498caa4SPatrick Venture     /* TODO: validate the layoutType isn't a special value such as: 'update',
42c498caa4SPatrick Venture      * 'verify', or 'hash'
43c498caa4SPatrick Venture      */
44c498caa4SPatrick Venture     std::string layout = "/flash/" + layoutType;
459f937c45SPatrick Venture 
469f937c45SPatrick Venture     bool goalSupported = updater->checkAvailable(layout);
4755646decSPatrick Venture     if (!goalSupported)
4855646decSPatrick Venture     {
49*060be01fSBenjamin Fair         throw ToolException("Goal firmware not supported");
5055646decSPatrick Venture     }
5155646decSPatrick Venture 
52*060be01fSBenjamin Fair     /* Yay, our layout type is supported. */
535f2fcc4eSPatrick Venture     try
545f2fcc4eSPatrick Venture     {
5555646decSPatrick Venture         /* Send over the firmware image. */
5655646decSPatrick Venture         std::fprintf(stderr, "Sending over the firmware image.\n");
579f937c45SPatrick Venture         updater->sendFile(layout, imagePath);
5855646decSPatrick Venture 
5955646decSPatrick Venture         /* Send over the hash contents. */
6055646decSPatrick Venture         std::fprintf(stderr, "Sending over the hash file.\n");
611d5a31c9SPatrick Venture         updater->sendFile(ipmi_flash::hashBlobId, signaturePath);
6255646decSPatrick Venture 
635f2fcc4eSPatrick Venture         /* Trigger the verification by opening and committing the verify file.
645f2fcc4eSPatrick Venture          */
6555646decSPatrick Venture         std::fprintf(stderr, "Opening the verification file\n");
666749ba1cSBrandon Kim         if (updater->verifyFile(ipmi_flash::verifyBlobId, false))
6755646decSPatrick Venture         {
6855646decSPatrick Venture             std::fprintf(stderr, "succeeded\n");
6955646decSPatrick Venture         }
7055646decSPatrick Venture         else
7155646decSPatrick Venture         {
7255646decSPatrick Venture             std::fprintf(stderr, "failed\n");
7314713becSPatrick Venture             throw ToolException("Verification failed");
7414713becSPatrick Venture         }
7514713becSPatrick Venture 
7614713becSPatrick Venture         /* Trigger the update by opening and committing the update file. */
7714713becSPatrick Venture         std::fprintf(stderr, "Opening the update file\n");
786749ba1cSBrandon Kim         if (updater->verifyFile(ipmi_flash::updateBlobId, ignoreUpdate))
7914713becSPatrick Venture         {
8014713becSPatrick Venture             std::fprintf(stderr, "succeeded\n");
8114713becSPatrick Venture         }
8214713becSPatrick Venture         else
8314713becSPatrick Venture         {
845f2fcc4eSPatrick Venture             /* Depending on the update mechanism used, this may be
855f2fcc4eSPatrick Venture              * uninteresting. For instance, for the static layout, we use the
865f2fcc4eSPatrick Venture              * reboot update mechanism.  Which doesn't always lead to a
875f2fcc4eSPatrick Venture              * successful return before the BMC starts shutting down services.
8814713becSPatrick Venture              */
8914713becSPatrick Venture             std::fprintf(stderr, "failed\n");
9014713becSPatrick Venture             throw ToolException("Update failed");
9155646decSPatrick Venture         }
92bf58cd64SPatrick Venture     }
935f2fcc4eSPatrick Venture     catch (...)
945f2fcc4eSPatrick Venture     {
955f2fcc4eSPatrick Venture         updater->cleanArtifacts();
965f2fcc4eSPatrick Venture         throw;
975f2fcc4eSPatrick Venture     }
985f2fcc4eSPatrick Venture }
999b534f06SPatrick Venture 
1009b534f06SPatrick Venture } // namespace host_tool
101