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 | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 23 | #include <memory> |
| 24 | |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 25 | void updaterMain(BlobInterface* blob, DataInterface* handler, |
| 26 | const std::string& imagePath, const std::string& signaturePath) |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 27 | { |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 28 | /* TODO(venture): Add optional parameter to specify the flash type, default |
| 29 | * to legacy for now. |
| 30 | */ |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 31 | std::string goalFirmware = "/flash/image"; |
| 32 | |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 33 | /* Get list of blob_ids, check for /flash/image, or /flash/tarball. |
| 34 | * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc |
| 35 | * will have in mind which they're sending and we need to verify it's |
| 36 | * available and use it. |
| 37 | */ |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 38 | std::vector<std::string> blobs = blob->getBlobList(); |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 39 | auto blobInst = std::find(blobs.begin(), blobs.end(), goalFirmware); |
| 40 | if (blobInst == blobs.end()) |
| 41 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 42 | throw ToolException(goalFirmware + " not found"); |
Patrick Venture | 0088759 | 2018-12-11 10:57:06 -0800 | [diff] [blame] | 43 | } |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 44 | |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 45 | /* 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] | 46 | * is supported. |
| 47 | */ |
Patrick Venture | 0bf8bf0 | 2018-12-12 20:43:25 -0800 | [diff] [blame] | 48 | auto stat = blob->getStat(goalFirmware); |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 49 | auto supported = handler->supportedType(); |
| 50 | if ((stat.blob_state & supported) == 0) |
Patrick Venture | 8a55dcb | 2018-12-12 21:12:58 -0800 | [diff] [blame] | 51 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 52 | throw ToolException("data interface selected not supported."); |
Patrick Venture | 8a55dcb | 2018-12-12 21:12:58 -0800 | [diff] [blame] | 53 | } |
Patrick Venture | af69625 | 2018-12-11 10:22:14 -0800 | [diff] [blame] | 54 | |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 55 | /* Yay, our data handler is supported. */ |
| 56 | std::uint16_t session; |
| 57 | try |
| 58 | { |
Patrick Venture | aa32a36 | 2018-12-13 10:52:33 -0800 | [diff] [blame] | 59 | session = blob->openBlob(goalFirmware, supported); |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 60 | } |
| 61 | catch (const BlobException& b) |
| 62 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 63 | throw ToolException("blob exception received: " + |
| 64 | std::string(b.what())); |
Patrick Venture | 0533d0b | 2018-12-13 08:48:24 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | std::fprintf(stderr, "using session: %d\n", session); |
| 68 | |
Patrick Venture | fd6aaec | 2018-12-13 09:06:02 -0800 | [diff] [blame] | 69 | /* Send over the firmware image. */ |
| 70 | if (!handler->sendContents(imagePath, session)) |
| 71 | { |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 72 | throw ToolException("Failed to send contents of " + imagePath); |
Patrick Venture | fd6aaec | 2018-12-13 09:06:02 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* Send over the hash contents. */ |
| 76 | /* Trigger the verification. */ |
| 77 | /* Check the verification. */ |
| 78 | |
Patrick Venture | 2bc23fe | 2018-12-13 10:16:36 -0800 | [diff] [blame] | 79 | return; |
Patrick Venture | bf58cd6 | 2018-12-11 09:05:46 -0800 | [diff] [blame] | 80 | } |