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 
192bc23fe1SPatrick Venture #include "tool_errors.hpp"
200533d0b0SPatrick Venture 
2100887597SPatrick Venture #include <algorithm>
22664c5bc7SPatrick Venture #include <blobs-ipmid/blobs.hpp>
23339dece8SPatrick Venture #include <cstring>
24664c5bc7SPatrick Venture #include <ipmiblob/blob_errors.hpp>
25af69625fSPatrick Venture #include <memory>
262a927e87SPatrick Venture #include <string>
27af69625fSPatrick Venture 
289b534f06SPatrick Venture namespace host_tool
299b534f06SPatrick Venture {
309b534f06SPatrick Venture 
31664c5bc7SPatrick Venture void updaterMain(ipmiblob::BlobInterface* blob, DataInterface* handler,
3200887597SPatrick Venture                  const std::string& imagePath, const std::string& signaturePath)
33bf58cd64SPatrick Venture {
34af69625fSPatrick Venture     /* TODO(venture): Add optional parameter to specify the flash type, default
35af69625fSPatrick Venture      * to legacy for now.
36af69625fSPatrick Venture      */
3700887597SPatrick Venture     std::string goalFirmware = "/flash/image";
38*73528388SPatrick Venture     std::string hashFilename = "/flash/hash";
3900887597SPatrick Venture 
400bf8bf0cSPatrick Venture     /* Get list of blob_ids, check for /flash/image, or /flash/tarball.
410bf8bf0cSPatrick Venture      * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc
420bf8bf0cSPatrick Venture      * will have in mind which they're sending and we need to verify it's
430bf8bf0cSPatrick Venture      * available and use it.
440bf8bf0cSPatrick Venture      */
4500887597SPatrick Venture     std::vector<std::string> blobs = blob->getBlobList();
46339dece8SPatrick Venture     auto blobInst = std::find_if(
472a927e87SPatrick Venture         blobs.begin(), blobs.end(), [&goalFirmware](const std::string& iter) {
48339dece8SPatrick Venture             /* Running into weird scenarios where the string comparison doesn't
49339dece8SPatrick Venture              * work.  TODO: revisit.
50339dece8SPatrick Venture              */
51339dece8SPatrick Venture             return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(),
52339dece8SPatrick Venture                                      goalFirmware.length()));
53339dece8SPatrick Venture             // return (goalFirmware.compare(iter));
54339dece8SPatrick Venture         });
5500887597SPatrick Venture     if (blobInst == blobs.end())
5600887597SPatrick Venture     {
572bc23fe1SPatrick Venture         throw ToolException(goalFirmware + " not found");
5800887597SPatrick Venture     }
59af69625fSPatrick Venture 
60af69625fSPatrick Venture     /* Call stat on /flash/image (or /flash/tarball) and check if data interface
6100887597SPatrick Venture      * is supported.
6200887597SPatrick Venture      */
63664c5bc7SPatrick Venture     ipmiblob::StatResponse stat;
64339dece8SPatrick Venture     try
65339dece8SPatrick Venture     {
66339dece8SPatrick Venture         stat = blob->getStat(goalFirmware);
67339dece8SPatrick Venture     }
68664c5bc7SPatrick Venture     catch (const ipmiblob::BlobException& b)
69339dece8SPatrick Venture     {
70339dece8SPatrick Venture         throw ToolException("blob exception received: " +
71339dece8SPatrick Venture                             std::string(b.what()));
72339dece8SPatrick Venture     }
73339dece8SPatrick Venture 
74aa32a36aSPatrick Venture     auto supported = handler->supportedType();
75aa32a36aSPatrick Venture     if ((stat.blob_state & supported) == 0)
768a55dcbdSPatrick Venture     {
772bc23fe1SPatrick Venture         throw ToolException("data interface selected not supported.");
788a55dcbdSPatrick Venture     }
79af69625fSPatrick Venture 
800533d0b0SPatrick Venture     /* Yay, our data handler is supported. */
81*73528388SPatrick Venture 
82*73528388SPatrick Venture     /* Send over the firmware image. */
83*73528388SPatrick Venture     std::fprintf(stderr, "Sending over the firmware image.\n");
840533d0b0SPatrick Venture     std::uint16_t session;
850533d0b0SPatrick Venture     try
860533d0b0SPatrick Venture     {
87664c5bc7SPatrick Venture         session = blob->openBlob(
88664c5bc7SPatrick Venture             goalFirmware,
89664c5bc7SPatrick Venture             static_cast<std::uint16_t>(supported) |
90664c5bc7SPatrick Venture                 static_cast<std::uint16_t>(blobs::OpenFlags::write));
910533d0b0SPatrick Venture     }
92664c5bc7SPatrick Venture     catch (const ipmiblob::BlobException& b)
930533d0b0SPatrick Venture     {
942bc23fe1SPatrick Venture         throw ToolException("blob exception received: " +
952bc23fe1SPatrick Venture                             std::string(b.what()));
960533d0b0SPatrick Venture     }
970533d0b0SPatrick Venture 
98fd6aaec8SPatrick Venture     if (!handler->sendContents(imagePath, session))
99fd6aaec8SPatrick Venture     {
100f9566d88SPatrick Venture         /* Need to close the session on failure, or it's stuck open (until the
101f9566d88SPatrick Venture          * blob handler timeout is implemented, and even then, why make it wait.
102f9566d88SPatrick Venture          */
103f9566d88SPatrick Venture         blob->closeBlob(session);
1042bc23fe1SPatrick Venture         throw ToolException("Failed to send contents of " + imagePath);
105fd6aaec8SPatrick Venture     }
106fd6aaec8SPatrick Venture 
1079a5ce561SPatrick Venture     blob->closeBlob(session);
1089a5ce561SPatrick Venture 
109fd6aaec8SPatrick Venture     /* Send over the hash contents. */
110*73528388SPatrick Venture     std::fprintf(stderr, "Sending over the hash file.\n");
111*73528388SPatrick Venture     try
112*73528388SPatrick Venture     {
113*73528388SPatrick Venture         session = blob->openBlob(
114*73528388SPatrick Venture             hashFilename,
115*73528388SPatrick Venture             static_cast<std::uint16_t>(supported) |
116*73528388SPatrick Venture                 static_cast<std::uint16_t>(blobs::OpenFlags::write));
117*73528388SPatrick Venture     }
118*73528388SPatrick Venture     catch (const ipmiblob::BlobException& b)
119*73528388SPatrick Venture     {
120*73528388SPatrick Venture         throw ToolException("blob exception received: " +
121*73528388SPatrick Venture                             std::string(b.what()));
122*73528388SPatrick Venture     }
123*73528388SPatrick Venture 
124*73528388SPatrick Venture     if (!handler->sendContents(signaturePath, session))
125*73528388SPatrick Venture     {
126*73528388SPatrick Venture         blob->closeBlob(session);
127*73528388SPatrick Venture         throw ToolException("Failed to send contents of " + signaturePath);
128*73528388SPatrick Venture     }
129*73528388SPatrick Venture 
130*73528388SPatrick Venture     blob->closeBlob(session);
131*73528388SPatrick Venture 
132fd6aaec8SPatrick Venture     /* Trigger the verification. */
133fd6aaec8SPatrick Venture     /* Check the verification. */
134fd6aaec8SPatrick Venture 
1352bc23fe1SPatrick Venture     return;
136bf58cd64SPatrick Venture }
1379b534f06SPatrick Venture 
1389b534f06SPatrick Venture } // namespace host_tool
139