Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 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 | |
Patrick Venture | 84778b8 | 2019-06-26 20:11:09 -0700 | [diff] [blame^] | 19 | #include "flags.hpp" |
Patrick Venture | 01123b2 | 2019-06-20 13:49:06 -0700 | [diff] [blame] | 20 | #include "handler.hpp" |
Patrick Venture | 3ecb350 | 2019-05-17 11:03:51 -0700 | [diff] [blame] | 21 | #include "status.hpp" |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 22 | #include "tool_errors.hpp" |
Patrick Venture | 7dad86f | 2019-05-17 08:52:20 -0700 | [diff] [blame] | 23 | #include "util.hpp" |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 24 | |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 25 | #include <algorithm> |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 26 | #include <cstring> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 27 | #include <ipmiblob/blob_errors.hpp> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 28 | #include <memory> |
Patrick Venture | 2a927e8 | 2019-02-01 07:29:47 -0800 | [diff] [blame] | 29 | #include <string> |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 30 | #include <thread> |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 31 | #include <vector> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 32 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 33 | namespace host_tool |
| 34 | { |
| 35 | |
Patrick Venture | 1f09d41 | 2019-06-19 16:01:06 -0700 | [diff] [blame] | 36 | void updaterMain(UpdateHandlerInterface* updater, const std::string& imagePath, |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 37 | const std::string& signaturePath, |
| 38 | const std::string& layoutType) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 39 | { |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 40 | const auto& layout = (layoutType == "static") |
| 41 | ? ipmi_flash::staticLayoutBlobId |
| 42 | : ipmi_flash::ubiTarballBlobId; |
| 43 | |
| 44 | bool goalSupported = updater->checkAvailable(layout); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 45 | if (!goalSupported) |
| 46 | { |
| 47 | throw ToolException("Goal firmware or interface not supported"); |
| 48 | } |
| 49 | |
| 50 | /* Yay, our data handler is supported. */ |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 51 | try |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 52 | { |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 53 | /* Send over the firmware image. */ |
| 54 | std::fprintf(stderr, "Sending over the firmware image.\n"); |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 55 | updater->sendFile(layout, imagePath); |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 56 | |
| 57 | /* Send over the hash contents. */ |
| 58 | std::fprintf(stderr, "Sending over the hash file.\n"); |
| 59 | updater->sendFile(ipmi_flash::hashBlobId, signaturePath); |
| 60 | |
| 61 | /* Trigger the verification by opening and committing the verify file. |
Patrick Venture | 14713be | 2019-06-05 13:42:28 -0700 | [diff] [blame] | 62 | */ |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 63 | std::fprintf(stderr, "Opening the verification file\n"); |
| 64 | if (updater->verifyFile(ipmi_flash::verifyBlobId)) |
| 65 | { |
| 66 | std::fprintf(stderr, "succeeded\n"); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | std::fprintf(stderr, "failed\n"); |
| 71 | throw ToolException("Verification failed"); |
| 72 | } |
| 73 | |
| 74 | /* Trigger the update by opening and committing the update file. */ |
| 75 | std::fprintf(stderr, "Opening the update file\n"); |
| 76 | if (updater->verifyFile(ipmi_flash::updateBlobId)) |
| 77 | { |
| 78 | std::fprintf(stderr, "succeeded\n"); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | /* Depending on the update mechanism used, this may be |
| 83 | * uninteresting. For instance, for the static layout, we use the |
| 84 | * reboot update mechanism. Which doesn't always lead to a |
| 85 | * successful return before the BMC starts shutting down services. |
| 86 | */ |
| 87 | std::fprintf(stderr, "failed\n"); |
| 88 | throw ToolException("Update failed"); |
| 89 | } |
| 90 | } |
| 91 | catch (...) |
| 92 | { |
| 93 | updater->cleanArtifacts(); |
| 94 | throw; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 95 | } |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 96 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 97 | |
| 98 | } // namespace host_tool |