1 /* 2 * Copyright 2018 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "updater.hpp" 18 19 #include "flags.hpp" 20 #include "handler.hpp" 21 #include "status.hpp" 22 #include "tool_errors.hpp" 23 #include "util.hpp" 24 25 #include <ipmiblob/blob_errors.hpp> 26 27 #include <algorithm> 28 #include <cstring> 29 #include <memory> 30 #include <string> 31 #include <thread> 32 #include <unordered_map> 33 #include <vector> 34 35 namespace host_tool 36 { 37 38 void updaterMain(UpdateHandlerInterface* updater, const std::string& imagePath, 39 const std::string& signaturePath, 40 const std::string& layoutType, bool ignoreUpdate) 41 { 42 /* TODO: validate the layoutType isn't a special value such as: 'update', 43 * 'verify', or 'hash' 44 */ 45 std::string layout = "/flash/" + layoutType; 46 47 bool goalSupported = updater->checkAvailable(layout); 48 if (!goalSupported) 49 { 50 throw ToolException("Goal firmware not supported"); 51 } 52 53 /* Yay, our layout type is supported. */ 54 try 55 { 56 /* Send over the firmware image. */ 57 std::fprintf(stderr, "Sending over the firmware image.\n"); 58 updater->sendFile(layout, imagePath); 59 60 /* Send over the hash contents. */ 61 std::fprintf(stderr, "Sending over the hash file.\n"); 62 updater->sendFile(ipmi_flash::hashBlobId, signaturePath); 63 64 /* Trigger the verification by opening and committing the verify file. 65 */ 66 std::fprintf(stderr, "Opening the verification file\n"); 67 if (updater->verifyFile(ipmi_flash::verifyBlobId, false)) 68 { 69 std::fprintf(stderr, "succeeded\n"); 70 } 71 else 72 { 73 std::fprintf(stderr, "failed\n"); 74 throw ToolException("Verification failed"); 75 } 76 77 /* Trigger the update by opening and committing the update file. */ 78 std::fprintf(stderr, "Opening the update file\n"); 79 if (updater->verifyFile(ipmi_flash::updateBlobId, ignoreUpdate)) 80 { 81 std::fprintf(stderr, "succeeded\n"); 82 } 83 else 84 { 85 /* Depending on the update mechanism used, this may be 86 * uninteresting. For instance, for the static layout, we use the 87 * reboot update mechanism. Which doesn't always lead to a 88 * successful return before the BMC starts shutting down services. 89 */ 90 std::fprintf(stderr, "failed\n"); 91 throw ToolException("Update failed"); 92 } 93 } 94 catch (...) 95 { 96 updater->cleanArtifacts(); 97 throw; 98 } 99 } 100 101 } // namespace host_tool 102