blob: 0c45d7bae0e3e79972392bf7b8da8c4b5f023e8d [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>
25
Patrick Venture9b534f02018-12-13 16:10:02 -080026namespace host_tool
27{
28
Patrick Venture2bc23fe2018-12-13 10:16:36 -080029void updaterMain(BlobInterface* blob, DataInterface* handler,
30 const std::string& imagePath, const std::string& signaturePath)
Patrick Venturebf58cd62018-12-11 09:05:46 -080031{
Patrick Ventureaf696252018-12-11 10:22:14 -080032 /* TODO(venture): Add optional parameter to specify the flash type, default
33 * to legacy for now.
34 */
Patrick Venture00887592018-12-11 10:57:06 -080035 std::string goalFirmware = "/flash/image";
36
Patrick Venture0bf8bf02018-12-12 20:43:25 -080037 /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
38 * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
39 * will have in mind which they're sending and we need to verify it's
40 * available and use it.
41 */
Patrick Venture00887592018-12-11 10:57:06 -080042 std::vector<std::string> blobs = blob->getBlobList();
Patrick Venture339dece2018-12-14 18:32:04 -080043 auto blobInst = std::find_if(
44 blobs.begin(), blobs.end(), [&goalFirmware](const auto& iter) {
45 /* Running into weird scenarios where the string comparison doesn't
46 * work. TODO: revisit.
47 */
48 return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
49 goalFirmware.length()));
50 // return (goalFirmware.compare(iter));
51 });
Patrick Venture00887592018-12-11 10:57:06 -080052 if (blobInst == blobs.end())
53 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080054 throw ToolException(goalFirmware + " not found");
Patrick Venture00887592018-12-11 10:57:06 -080055 }
Patrick Ventureaf696252018-12-11 10:22:14 -080056
Patrick Ventureaf696252018-12-11 10:22:14 -080057 /* Call stat on /flash/image (or /flash/tarball) and check if data interface
Patrick Venture00887592018-12-11 10:57:06 -080058 * is supported.
59 */
Patrick Venture339dece2018-12-14 18:32:04 -080060 StatResponse stat;
61 try
62 {
63 stat = blob->getStat(goalFirmware);
64 }
65 catch (const BlobException& b)
66 {
67 throw ToolException("blob exception received: " +
68 std::string(b.what()));
69 }
70
Patrick Ventureaa32a362018-12-13 10:52:33 -080071 auto supported = handler->supportedType();
72 if ((stat.blob_state & supported) == 0)
Patrick Venture8a55dcb2018-12-12 21:12:58 -080073 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080074 throw ToolException("data interface selected not supported.");
Patrick Venture8a55dcb2018-12-12 21:12:58 -080075 }
Patrick Ventureaf696252018-12-11 10:22:14 -080076
Patrick Venture0533d0b2018-12-13 08:48:24 -080077 /* Yay, our data handler is supported. */
78 std::uint16_t session;
79 try
80 {
Patrick Ventureaa32a362018-12-13 10:52:33 -080081 session = blob->openBlob(goalFirmware, supported);
Patrick Venture0533d0b2018-12-13 08:48:24 -080082 }
83 catch (const BlobException& b)
84 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080085 throw ToolException("blob exception received: " +
86 std::string(b.what()));
Patrick Venture0533d0b2018-12-13 08:48:24 -080087 }
88
Patrick Venturefd6aaec2018-12-13 09:06:02 -080089 /* Send over the firmware image. */
90 if (!handler->sendContents(imagePath, session))
91 {
Patrick Venturef9566d82019-01-16 09:44:01 -080092 /* Need to close the session on failure, or it's stuck open (until the
93 * blob handler timeout is implemented, and even then, why make it wait.
94 */
95 blob->closeBlob(session);
Patrick Venture2bc23fe2018-12-13 10:16:36 -080096 throw ToolException("Failed to send contents of " + imagePath);
Patrick Venturefd6aaec2018-12-13 09:06:02 -080097 }
98
Patrick Venture9a5ce562018-12-14 18:56:04 -080099 blob->closeBlob(session);
100
Patrick Venturefd6aaec2018-12-13 09:06:02 -0800101 /* Send over the hash contents. */
102 /* Trigger the verification. */
103 /* Check the verification. */
104
Patrick Venture2bc23fe2018-12-13 10:16:36 -0800105 return;
Patrick Venturebf58cd62018-12-11 09:05:46 -0800106}
Patrick Venture9b534f02018-12-13 16:10:02 -0800107
108} // namespace host_tool