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 
19 #include "blob_errors.hpp"
20 
21 #include <algorithm>
22 #include <memory>
23 
24 int updaterMain(BlobInterface* blob, DataInterface* handler,
25                 const std::string& imagePath, const std::string& signaturePath)
26 {
27     /* TODO(venture): Add optional parameter to specify the flash type, default
28      * to legacy for now.
29      */
30     std::string goalFirmware = "/flash/image";
31 
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      */
37     std::vector<std::string> blobs = blob->getBlobList();
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     }
44 
45     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
46      * is supported.
47      */
48     auto stat = blob->getStat(goalFirmware);
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     }
54 
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 
69     return 0;
70 }
71