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 | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 19 | #include "tool_errors.hpp" |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 20 | |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 22 | #include <blobs-ipmid/blobs.hpp> |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 23 | #include <cstring> |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 24 | #include <ipmiblob/blob_errors.hpp> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 25 | #include <memory> |
Patrick Venture | 2a927e8 | 2019-02-01 07:29:47 -0800 | [diff] [blame] | 26 | #include <string> |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 27 | |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 28 | namespace host_tool |
| 29 | { |
| 30 | |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 31 | void updaterMain(ipmiblob::BlobInterface* blob, DataInterface* handler, |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 32 | const std::string& imagePath, const std::string& signaturePath) |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 33 | { |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 34 | /* TODO(venture): Add optional parameter to specify the flash type, default |
| 35 | * to legacy for now. |
| 36 | */ |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 37 | std::string goalFirmware = "/flash/image"; |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 38 | std::string hashFilename = "/flash/hash"; |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 39 | |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 40 | /* Get list of blob_ids, check for /flash/image, or /flash/tarball. |
| 41 | * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc |
| 42 | * will have in mind which they're sending and we need to verify it's |
| 43 | * available and use it. |
| 44 | */ |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 45 | std::vector<std::string> blobs = blob->getBlobList(); |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 46 | auto blobInst = std::find_if( |
Patrick Venture | 2a927e8 | 2019-02-01 07:29:47 -0800 | [diff] [blame] | 47 | blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) { |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 48 | /* Running into weird scenarios where the string comparison doesn't |
| 49 | * work. TODO: revisit. |
| 50 | */ |
| 51 | return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(), |
| 52 | goalFirmware.length())); |
| 53 | // return (goalFirmware.compare(iter)); |
| 54 | }); |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 55 | if (blobInst == blobs.end()) |
| 56 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 57 | throw ToolException(goalFirmware + " not found"); |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 58 | } |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 59 | |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 60 | /* Call stat on /flash/image (or /flash/tarball) and check if data interface |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 61 | * is supported. |
| 62 | */ |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 63 | ipmiblob::StatResponse stat; |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 64 | try |
| 65 | { |
| 66 | stat = blob->getStat(goalFirmware); |
| 67 | } |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 68 | catch (const ipmiblob::BlobException& b) |
Patrick Venture | 339dece | 2018-12-14 18:32:04 -0800 | [diff] [blame] | 69 | { |
| 70 | throw ToolException("blob exception received: " + |
| 71 | std::string(b.what())); |
| 72 | } |
| 73 | |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 74 | auto supported = handler->supportedType(); |
| 75 | if ((stat.blob_state & supported) == 0) |
Patrick Venture | 8a55dcb | 2018-12-12 21:12:58 -0800 | [diff] [blame] | 76 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 77 | throw ToolException("data interface selected not supported."); |
Patrick Venture | 8a55dcb | 2018-12-12 21:12:58 -0800 | [diff] [blame] | 78 | } |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 79 | |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 80 | /* Yay, our data handler is supported. */ |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 81 | |
| 82 | /* Send over the firmware image. */ |
| 83 | std::fprintf(stderr, "Sending over the firmware image.\n"); |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 84 | std::uint16_t session; |
| 85 | try |
| 86 | { |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 87 | session = blob->openBlob( |
| 88 | goalFirmware, |
| 89 | static_cast<std::uint16_t>(supported) | |
| 90 | static_cast<std::uint16_t>(blobs::OpenFlags::write)); |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 91 | } |
Patrick Venture | 664c5bc | 2019-03-07 08:09:45 -0800 | [diff] [blame] | 92 | catch (const ipmiblob::BlobException& b) |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 93 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 94 | throw ToolException("blob exception received: " + |
| 95 | std::string(b.what())); |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Patrick Venture | fd6aaec | 2018-12-13 09:06:02 -0800 | [diff] [blame] | 98 | if (!handler->sendContents(imagePath, session)) |
| 99 | { |
Patrick Venture | f9566d8 | 2019-01-16 09:44:01 -0800 | [diff] [blame] | 100 | /* Need to close the session on failure, or it's stuck open (until the |
| 101 | * blob handler timeout is implemented, and even then, why make it wait. |
| 102 | */ |
| 103 | blob->closeBlob(session); |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 104 | throw ToolException("Failed to send contents of " + imagePath); |
Patrick Venture | fd6aaec | 2018-12-13 09:06:02 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Patrick Venture | 9a5ce56 | 2018-12-14 18:56:04 -0800 | [diff] [blame] | 107 | blob->closeBlob(session); |
| 108 | |
Patrick Venture | fd6aaec | 2018-12-13 09:06:02 -0800 | [diff] [blame] | 109 | /* Send over the hash contents. */ |
Patrick Venture | 7352838 | 2019-05-14 12:43:37 -0700 | [diff] [blame] | 110 | std::fprintf(stderr, "Sending over the hash file.\n"); |
| 111 | try |
| 112 | { |
| 113 | session = blob->openBlob( |
| 114 | hashFilename, |
| 115 | static_cast<std::uint16_t>(supported) | |
| 116 | static_cast<std::uint16_t>(blobs::OpenFlags::write)); |
| 117 | } |
| 118 | catch (const ipmiblob::BlobException& b) |
| 119 | { |
| 120 | throw ToolException("blob exception received: " + |
| 121 | std::string(b.what())); |
| 122 | } |
| 123 | |
| 124 | if (!handler->sendContents(signaturePath, session)) |
| 125 | { |
| 126 | blob->closeBlob(session); |
| 127 | throw ToolException("Failed to send contents of " + signaturePath); |
| 128 | } |
| 129 | |
| 130 | blob->closeBlob(session); |
| 131 | |
Patrick Venture | fd6aaec | 2018-12-13 09:06:02 -0800 | [diff] [blame] | 132 | /* Trigger the verification. */ |
| 133 | /* Check the verification. */ |
| 134 | |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 135 | return; |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 136 | } |
Patrick Venture | 9b534f0 | 2018-12-13 16:10:02 -0800 | [diff] [blame] | 137 | |
| 138 | } // namespace host_tool |