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