blob: b79979c6f5d7d32e4364395fd7677f91e5e06317 [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 Ventureaf696252018-12-11 10:22:14 -080023#include <memory>
24
Patrick Venture2bc23fe2018-12-13 10:16:36 -080025void updaterMain(BlobInterface* blob, DataInterface* handler,
26 const std::string& imagePath, const std::string& signaturePath)
Patrick Venturebf58cd62018-12-11 09:05:46 -080027{
Patrick Ventureaf696252018-12-11 10:22:14 -080028 /* TODO(venture): Add optional parameter to specify the flash type, default
29 * to legacy for now.
30 */
Patrick Venture00887592018-12-11 10:57:06 -080031 std::string goalFirmware = "/flash/image";
32
Patrick Venture0bf8bf02018-12-12 20:43:25 -080033 /* 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 Venture00887592018-12-11 10:57:06 -080038 std::vector<std::string> blobs = blob->getBlobList();
Patrick Venture00887592018-12-11 10:57:06 -080039 auto blobInst = std::find(blobs.begin(), blobs.end(), goalFirmware);
40 if (blobInst == blobs.end())
41 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080042 throw ToolException(goalFirmware + " not found");
Patrick Venture00887592018-12-11 10:57:06 -080043 }
Patrick Ventureaf696252018-12-11 10:22:14 -080044
Patrick Ventureaf696252018-12-11 10:22:14 -080045 /* Call stat on /flash/image (or /flash/tarball) and check if data interface
Patrick Venture00887592018-12-11 10:57:06 -080046 * is supported.
47 */
Patrick Venture0bf8bf02018-12-12 20:43:25 -080048 auto stat = blob->getStat(goalFirmware);
Patrick Venture8a55dcb2018-12-12 21:12:58 -080049 if ((stat.blob_state & handler->supportedType()) == 0)
50 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080051 throw ToolException("data interface selected not supported.");
Patrick Venture8a55dcb2018-12-12 21:12:58 -080052 }
Patrick Ventureaf696252018-12-11 10:22:14 -080053
Patrick Venture0533d0b2018-12-13 08:48:24 -080054 /* Yay, our data handler is supported. */
55 std::uint16_t session;
56 try
57 {
58 session = blob->openBlob(goalFirmware, handler->supportedType());
59 }
60 catch (const BlobException& b)
61 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080062 throw ToolException("blob exception received: " +
63 std::string(b.what()));
Patrick Venture0533d0b2018-12-13 08:48:24 -080064 }
65
66 std::fprintf(stderr, "using session: %d\n", session);
67
Patrick Venturefd6aaec2018-12-13 09:06:02 -080068 /* Send over the firmware image. */
69 if (!handler->sendContents(imagePath, session))
70 {
Patrick Venture2bc23fe2018-12-13 10:16:36 -080071 throw ToolException("Failed to send contents of " + imagePath);
Patrick Venturefd6aaec2018-12-13 09:06:02 -080072 }
73
74 /* Send over the hash contents. */
75 /* Trigger the verification. */
76 /* Check the verification. */
77
Patrick Venture2bc23fe2018-12-13 10:16:36 -080078 return;
Patrick Venturebf58cd62018-12-11 09:05:46 -080079}