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 | 9b37b09 | 2020-05-28 20:58:57 -0700 | [diff] [blame] | 25 | #include <ipmiblob/blob_errors.hpp> |
| 26 | |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 27 | #include <algorithm> |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 28 | #include <cstring> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 29 | #include <memory> |
Patrick Venture | 2a927e8 | 2019-02-01 07:29:47 -0800 | [diff] [blame] | 30 | #include <string> |
Patrick Venture | d61b0ff | 2019-05-15 15:58:06 -0700 | [diff] [blame] | 31 | #include <thread> |
Patrick Venture | c9792e7 | 2019-07-02 07:35:48 -0700 | [diff] [blame] | 32 | #include <unordered_map> |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 33 | #include <vector> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 34 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 35 | namespace host_tool |
| 36 | { |
| 37 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 38 | void updaterMain(UpdateHandlerInterface* updater, ipmiblob::BlobInterface* blob, |
| 39 | const std::string& imagePath, const std::string& signaturePath, |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 40 | const std::string& layoutType, bool ignoreUpdate) |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 41 | { |
Patrick Venture | c498caa | 2019-08-15 10:12:50 -0700 | [diff] [blame] | 42 | /* TODO: validate the layoutType isn't a special value such as: 'update', |
| 43 | * 'verify', or 'hash' |
| 44 | */ |
| 45 | std::string layout = "/flash/" + layoutType; |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 46 | |
| 47 | bool goalSupported = updater->checkAvailable(layout); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 48 | if (!goalSupported) |
| 49 | { |
Benjamin Fair | 060be01 | 2019-11-14 13:12:49 -0800 | [diff] [blame] | 50 | throw ToolException("Goal firmware not supported"); |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 53 | // Clean all active blobs to support multiple stages |
| 54 | // Check for any active blobs and delete the first one found to reset the |
| 55 | // BMC's phosphor-ipmi-flash state machine then clean any leftover artifacts |
| 56 | const auto blobList = blob->getBlobList(); |
| 57 | for (const auto& activeBlob : blobList) |
| 58 | { |
| 59 | // Prefix is /flash/active/ |
Willy Tu | 7d249a7 | 2021-09-12 23:40:42 -0700 | [diff] [blame] | 60 | if (activeBlob.starts_with("/flash/active/")) |
Willy Tu | 203ad80 | 2021-09-09 20:06:36 -0700 | [diff] [blame] | 61 | { |
| 62 | std::fprintf(stderr, "Found an active blob, deleting %s\n", |
| 63 | activeBlob.c_str()); |
| 64 | blob->deleteBlob(activeBlob); |
| 65 | updater->cleanArtifacts(); |
| 66 | break; |
| 67 | } |
| 68 | } |
| 69 | |
Benjamin Fair | 060be01 | 2019-11-14 13:12:49 -0800 | [diff] [blame] | 70 | /* Yay, our layout type is supported. */ |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 71 | try |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 72 | { |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 73 | /* Send over the firmware image. */ |
| 74 | std::fprintf(stderr, "Sending over the firmware image.\n"); |
Patrick Venture | 9f937c4 | 2019-06-21 07:55:54 -0700 | [diff] [blame] | 75 | updater->sendFile(layout, imagePath); |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 76 | |
| 77 | /* Send over the hash contents. */ |
| 78 | std::fprintf(stderr, "Sending over the hash file.\n"); |
| 79 | updater->sendFile(ipmi_flash::hashBlobId, signaturePath); |
| 80 | |
| 81 | /* Trigger the verification by opening and committing the verify file. |
Patrick Venture | 14713be | 2019-06-05 13:42:28 -0700 | [diff] [blame] | 82 | */ |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 83 | std::fprintf(stderr, "Opening the verification file\n"); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 84 | if (updater->verifyFile(ipmi_flash::verifyBlobId, false)) |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 85 | { |
| 86 | std::fprintf(stderr, "succeeded\n"); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | std::fprintf(stderr, "failed\n"); |
| 91 | throw ToolException("Verification failed"); |
| 92 | } |
| 93 | |
| 94 | /* Trigger the update by opening and committing the update file. */ |
| 95 | std::fprintf(stderr, "Opening the update file\n"); |
Brandon Kim | 6749ba1 | 2019-09-19 13:31:37 -0700 | [diff] [blame] | 96 | if (updater->verifyFile(ipmi_flash::updateBlobId, ignoreUpdate)) |
Patrick Venture | 5f2fcc4 | 2019-06-20 07:21:05 -0700 | [diff] [blame] | 97 | { |
| 98 | std::fprintf(stderr, "succeeded\n"); |
| 99 | } |
| 100 | else |
| 101 | { |
| 102 | /* Depending on the update mechanism used, this may be |
| 103 | * uninteresting. For instance, for the static layout, we use the |
| 104 | * reboot update mechanism. Which doesn't always lead to a |
| 105 | * successful return before the BMC starts shutting down services. |
| 106 | */ |
| 107 | std::fprintf(stderr, "failed\n"); |
| 108 | throw ToolException("Update failed"); |
| 109 | } |
| 110 | } |
| 111 | catch (...) |
| 112 | { |
| 113 | updater->cleanArtifacts(); |
| 114 | throw; |
Patrick Venture | 55646de | 2019-05-16 10:06:26 -0700 | [diff] [blame] | 115 | } |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 116 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 117 | |
| 118 | } // namespace host_tool |