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