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> 31*c9792e75SPatrick 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, 399f937c45SPatrick Venture const std::string& layoutType) 4055646decSPatrick Venture { 41*c9792e75SPatrick Venture static std::unordered_map<std::string, std::string> typesToBlob = { 42*c9792e75SPatrick Venture {"static", ipmi_flash::staticLayoutBlobId}, 43*c9792e75SPatrick Venture {"ubitar", ipmi_flash::ubiTarballBlobId}, 44*c9792e75SPatrick Venture {"bios", ipmi_flash::biosBlobId}}; 45*c9792e75SPatrick Venture /* We know it's one of the above types already. */ 46*c9792e75SPatrick Venture auto& layout = typesToBlob[layoutType]; 479f937c45SPatrick Venture 489f937c45SPatrick Venture bool goalSupported = updater->checkAvailable(layout); 4955646decSPatrick Venture if (!goalSupported) 5055646decSPatrick Venture { 5155646decSPatrick Venture throw ToolException("Goal firmware or interface not supported"); 5255646decSPatrick Venture } 5355646decSPatrick Venture 5455646decSPatrick Venture /* Yay, our data handler is supported. */ 555f2fcc4eSPatrick Venture try 565f2fcc4eSPatrick Venture { 5755646decSPatrick Venture /* Send over the firmware image. */ 5855646decSPatrick Venture std::fprintf(stderr, "Sending over the firmware image.\n"); 599f937c45SPatrick Venture updater->sendFile(layout, imagePath); 6055646decSPatrick Venture 6155646decSPatrick Venture /* Send over the hash contents. */ 6255646decSPatrick Venture std::fprintf(stderr, "Sending over the hash file.\n"); 631d5a31c9SPatrick Venture updater->sendFile(ipmi_flash::hashBlobId, signaturePath); 6455646decSPatrick Venture 655f2fcc4eSPatrick Venture /* Trigger the verification by opening and committing the verify file. 665f2fcc4eSPatrick Venture */ 6755646decSPatrick Venture std::fprintf(stderr, "Opening the verification file\n"); 681d5a31c9SPatrick Venture if (updater->verifyFile(ipmi_flash::verifyBlobId)) 6955646decSPatrick Venture { 7055646decSPatrick Venture std::fprintf(stderr, "succeeded\n"); 7155646decSPatrick Venture } 7255646decSPatrick Venture else 7355646decSPatrick Venture { 7455646decSPatrick Venture std::fprintf(stderr, "failed\n"); 7514713becSPatrick Venture throw ToolException("Verification failed"); 7614713becSPatrick Venture } 7714713becSPatrick Venture 7814713becSPatrick Venture /* Trigger the update by opening and committing the update file. */ 7914713becSPatrick Venture std::fprintf(stderr, "Opening the update file\n"); 8014713becSPatrick Venture if (updater->verifyFile(ipmi_flash::updateBlobId)) 8114713becSPatrick Venture { 8214713becSPatrick Venture std::fprintf(stderr, "succeeded\n"); 8314713becSPatrick Venture } 8414713becSPatrick Venture else 8514713becSPatrick Venture { 865f2fcc4eSPatrick Venture /* Depending on the update mechanism used, this may be 875f2fcc4eSPatrick Venture * uninteresting. For instance, for the static layout, we use the 885f2fcc4eSPatrick Venture * reboot update mechanism. Which doesn't always lead to a 895f2fcc4eSPatrick Venture * successful return before the BMC starts shutting down services. 9014713becSPatrick Venture */ 9114713becSPatrick Venture std::fprintf(stderr, "failed\n"); 9214713becSPatrick Venture throw ToolException("Update failed"); 9355646decSPatrick Venture } 94bf58cd64SPatrick Venture } 955f2fcc4eSPatrick Venture catch (...) 965f2fcc4eSPatrick Venture { 975f2fcc4eSPatrick Venture updater->cleanArtifacts(); 985f2fcc4eSPatrick Venture throw; 995f2fcc4eSPatrick Venture } 1005f2fcc4eSPatrick Venture } 1019b534f06SPatrick Venture 1029b534f06SPatrick Venture } // namespace host_tool 103