1bf58cd64SPatrick Venture /*
2bf58cd64SPatrick Venture  * Copyright 2018 Google Inc.
3bf58cd64SPatrick Venture  *
4bf58cd64SPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5bf58cd64SPatrick Venture  * you may not use this file except in compliance with the License.
6bf58cd64SPatrick Venture  * You may obtain a copy of the License at
7bf58cd64SPatrick Venture  *
8bf58cd64SPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9bf58cd64SPatrick Venture  *
10bf58cd64SPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11bf58cd64SPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12bf58cd64SPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf58cd64SPatrick Venture  * See the License for the specific language governing permissions and
14bf58cd64SPatrick Venture  * limitations under the License.
15bf58cd64SPatrick Venture  */
16bf58cd64SPatrick Venture 
17bf58cd64SPatrick Venture #include "updater.hpp"
18bf58cd64SPatrick Venture 
190533d0b0SPatrick Venture #include "blob_errors.hpp"
20*2bc23fe1SPatrick Venture #include "tool_errors.hpp"
210533d0b0SPatrick Venture 
2200887597SPatrick Venture #include <algorithm>
23af69625fSPatrick Venture #include <memory>
24af69625fSPatrick Venture 
25*2bc23fe1SPatrick Venture void updaterMain(BlobInterface* blob, DataInterface* handler,
2600887597SPatrick Venture                  const std::string& imagePath, const std::string& signaturePath)
27bf58cd64SPatrick Venture {
28af69625fSPatrick Venture     /* TODO(venture): Add optional parameter to specify the flash type, default
29af69625fSPatrick Venture      * to legacy for now.
30af69625fSPatrick Venture      */
3100887597SPatrick Venture     std::string goalFirmware = "/flash/image";
3200887597SPatrick Venture 
330bf8bf0cSPatrick Venture     /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
340bf8bf0cSPatrick Venture      * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
350bf8bf0cSPatrick Venture      * will have in mind which they're sending and we need to verify it's
360bf8bf0cSPatrick Venture      * available and use it.
370bf8bf0cSPatrick Venture      */
3800887597SPatrick Venture     std::vector<std::string> blobs = blob->getBlobList();
3900887597SPatrick Venture     auto blobInst = std::find(blobs.begin(), blobs.end(), goalFirmware);
4000887597SPatrick Venture     if (blobInst == blobs.end())
4100887597SPatrick Venture     {
42*2bc23fe1SPatrick Venture         throw ToolException(goalFirmware + " not found");
4300887597SPatrick Venture     }
44af69625fSPatrick Venture 
45af69625fSPatrick Venture     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
4600887597SPatrick Venture      * is supported.
4700887597SPatrick Venture      */
480bf8bf0cSPatrick Venture     auto stat = blob->getStat(goalFirmware);
498a55dcbdSPatrick Venture     if ((stat.blob_state & handler->supportedType()) == 0)
508a55dcbdSPatrick Venture     {
51*2bc23fe1SPatrick Venture         throw ToolException("data interface selected not supported.");
528a55dcbdSPatrick Venture     }
53af69625fSPatrick Venture 
540533d0b0SPatrick Venture     /* Yay, our data handler is supported. */
550533d0b0SPatrick Venture     std::uint16_t session;
560533d0b0SPatrick Venture     try
570533d0b0SPatrick Venture     {
580533d0b0SPatrick Venture         session = blob->openBlob(goalFirmware, handler->supportedType());
590533d0b0SPatrick Venture     }
600533d0b0SPatrick Venture     catch (const BlobException& b)
610533d0b0SPatrick Venture     {
62*2bc23fe1SPatrick Venture         throw ToolException("blob exception received: " +
63*2bc23fe1SPatrick Venture                             std::string(b.what()));
640533d0b0SPatrick Venture     }
650533d0b0SPatrick Venture 
660533d0b0SPatrick Venture     std::fprintf(stderr, "using session: %d\n", session);
670533d0b0SPatrick Venture 
68fd6aaec8SPatrick Venture     /* Send over the firmware image. */
69fd6aaec8SPatrick Venture     if (!handler->sendContents(imagePath, session))
70fd6aaec8SPatrick Venture     {
71*2bc23fe1SPatrick Venture         throw ToolException("Failed to send contents of " + imagePath);
72fd6aaec8SPatrick Venture     }
73fd6aaec8SPatrick Venture 
74fd6aaec8SPatrick Venture     /* Send over the hash contents. */
75fd6aaec8SPatrick Venture     /* Trigger the verification. */
76fd6aaec8SPatrick Venture     /* Check the verification. */
77fd6aaec8SPatrick Venture 
78*2bc23fe1SPatrick Venture     return;
79bf58cd64SPatrick Venture }
80