blob: 26207152a9404d7f094c3bf6442a0f051f701ef4 [file] [log] [blame]
Patrick Venturebf58cd62018-12-11 09:05:46 -08001/*
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 Venture0533d0b2018-12-13 08:48:24 -080019#include "blob_errors.hpp"
Patrick Venture2bc23fe2018-12-13 10:16:36 -080020#include "tool_errors.hpp"
Patrick Venture0533d0b2018-12-13 08:48:24 -080021
Patrick Venture00887592018-12-11 10:57:06 -080022#include <algorithm>
Patrick Venture339dece2018-12-14 18:32:04 -080023#include <cstring>
Patrick Ventureaf696252018-12-11 10:22:14 -080024#include <memory>
Patrick Venture2a927e82019-02-01 07:29:47 -080025#include <string>
Patrick Ventureaf696252018-12-11 10:22:14 -080026
Patrick Venture9b534f02018-12-13 16:10:02 -080027namespace host_tool
28{
29
Patrick Venture2bc23fe2018-12-13 10:16:36 -080030void updaterMain(BlobInterface* blob, DataInterface* handler,
31 const std::string& imagePath, const std::string& signaturePath)
Patrick Venturebf58cd62018-12-11 09:05:46 -080032{
Patrick Ventureaf696252018-12-11 10:22:14 -080033 /* TODO(venture): Add optional parameter to specify the flash type, default
34 * to legacy for now.
35 */
Patrick Venture00887592018-12-11 10:57:06 -080036 std::string goalFirmware = "/flash/image";
37
Patrick Venture0bf8bf02018-12-12 20:43:25 -080038 /* 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 Venture00887592018-12-11 10:57:06 -080043 std::vector<std::string> blobs = blob->getBlobList();
Patrick Venture339dece2018-12-14 18:32:04 -080044 auto blobInst = std::find_if(
Patrick Venture2a927e82019-02-01 07:29:47 -080045 blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
Patrick Venture339dece2018-12-14 18:32:04 -080046 /* 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 Venture00887592018-12-11 10:57:06 -080053 if (blobInst == blobs.end())
54 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080055 throw ToolException(goalFirmware + " not found");
Patrick Venture00887592018-12-11 10:57:06 -080056 }
Patrick Ventureaf696252018-12-11 10:22:14 -080057
Patrick Ventureaf696252018-12-11 10:22:14 -080058 /* Call stat on /flash/image (or /flash/tarball) and check if data interface
Patrick Venture00887592018-12-11 10:57:06 -080059 * is supported.
60 */
Patrick Venture339dece2018-12-14 18:32:04 -080061 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 Ventureaa32a362018-12-13 10:52:33 -080072 auto supported = handler->supportedType();
73 if ((stat.blob_state & supported) == 0)
Patrick Venture8a55dcb2018-12-12 21:12:58 -080074 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080075 throw ToolException("data interface selected not supported.");
Patrick Venture8a55dcb2018-12-12 21:12:58 -080076 }
Patrick Ventureaf696252018-12-11 10:22:14 -080077
Patrick Venture0533d0b2018-12-13 08:48:24 -080078 /* Yay, our data handler is supported. */
79 std::uint16_t session;
80 try
81 {
Patrick Ventureaa32a362018-12-13 10:52:33 -080082 session = blob->openBlob(goalFirmware, supported);
Patrick Venture0533d0b2018-12-13 08:48:24 -080083 }
84 catch (const BlobException& b)
85 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080086 throw ToolException("blob exception received: " +
87 std::string(b.what()));
Patrick Venture0533d0b2018-12-13 08:48:24 -080088 }
89
Patrick Venturefd6aaec2018-12-13 09:06:02 -080090 /* Send over the firmware image. */
91 if (!handler->sendContents(imagePath, session))
92 {
Patrick Venturef9566d82019-01-16 09:44:01 -080093 /* 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 Venture2bc23fe2018-12-13 10:16:36 -080097 throw ToolException("Failed to send contents of " + imagePath);
Patrick Venturefd6aaec2018-12-13 09:06:02 -080098 }
99
Patrick Venture9a5ce562018-12-14 18:56:04 -0800100 blob->closeBlob(session);
101
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800102 /* Send over the hash contents. */
103 /* Trigger the verification. */
104 /* Check the verification. */
105
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800106 return;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800107}
Patrick Venture9b534f02018-12-13 16:10:02 -0800108
109} // namespace host_tool