blob: aa084e1edbbbccc71ece28314ee3bc0064cf6821 [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"
20
Patrick Venture00887592018-12-11 10:57:06 -080021#include <algorithm>
Patrick Ventureaf696252018-12-11 10:22:14 -080022#include <memory>
23
Patrick Venturea6586362018-12-11 18:47:13 -080024int updaterMain(BlobInterface* blob, DataInterface* handler,
Patrick Venture00887592018-12-11 10:57:06 -080025 const std::string& imagePath, const std::string& signaturePath)
Patrick Venturebf58cd62018-12-11 09:05:46 -080026{
Patrick Ventureaf696252018-12-11 10:22:14 -080027 /* TODO(venture): Add optional parameter to specify the flash type, default
28 * to legacy for now.
29 */
Patrick Venture00887592018-12-11 10:57:06 -080030 std::string goalFirmware = "/flash/image";
31
Patrick Venture0bf8bf02018-12-12 20:43:25 -080032 /* 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 Venture00887592018-12-11 10:57:06 -080037 std::vector<std::string> blobs = blob->getBlobList();
Patrick Venture00887592018-12-11 10:57:06 -080038 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 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 {
51 std::fprintf(stderr, "data interface selected not supported.\n");
52 return -1; /* throw custom exception. */
53 }
Patrick Ventureaf696252018-12-11 10:22:14 -080054
Patrick Venture0533d0b2018-12-13 08:48:24 -080055 /* 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 Venturefd6aaec2018-12-13 09:06:02 -080069 /* 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 Venturebf58cd62018-12-11 09:05:46 -080081 return 0;
82}