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" 202bc23fe1SPatrick Venture #include "tool_errors.hpp" 210533d0b0SPatrick Venture 2200887597SPatrick Venture #include <algorithm> 23*339dece8SPatrick Venture #include <cstring> 24af69625fSPatrick Venture #include <memory> 25af69625fSPatrick Venture 262bc23fe1SPatrick Venture void updaterMain(BlobInterface* blob, DataInterface* handler, 2700887597SPatrick Venture const std::string& imagePath, const std::string& signaturePath) 28bf58cd64SPatrick Venture { 29af69625fSPatrick Venture /* TODO(venture): Add optional parameter to specify the flash type, default 30af69625fSPatrick Venture * to legacy for now. 31af69625fSPatrick Venture */ 3200887597SPatrick Venture std::string goalFirmware = "/flash/image"; 3300887597SPatrick Venture 340bf8bf0cSPatrick Venture /* Get list of blob_ids, check for /flash/image, or /flash/tarball. 350bf8bf0cSPatrick Venture * TODO(venture) the mechanism doesn't care, but the caller of burn_my_bmc 360bf8bf0cSPatrick Venture * will have in mind which they're sending and we need to verify it's 370bf8bf0cSPatrick Venture * available and use it. 380bf8bf0cSPatrick Venture */ 3900887597SPatrick Venture std::vector<std::string> blobs = blob->getBlobList(); 40*339dece8SPatrick Venture auto blobInst = std::find_if( 41*339dece8SPatrick Venture blobs.begin(), blobs.end(), [&goalFirmware](const auto& iter) { 42*339dece8SPatrick Venture /* Running into weird scenarios where the string comparison doesn't 43*339dece8SPatrick Venture * work. TODO: revisit. 44*339dece8SPatrick Venture */ 45*339dece8SPatrick Venture return (0 == std::memcmp(goalFirmware.c_str(), iter.c_str(), 46*339dece8SPatrick Venture goalFirmware.length())); 47*339dece8SPatrick Venture // return (goalFirmware.compare(iter)); 48*339dece8SPatrick Venture }); 4900887597SPatrick Venture if (blobInst == blobs.end()) 5000887597SPatrick Venture { 512bc23fe1SPatrick Venture throw ToolException(goalFirmware + " not found"); 5200887597SPatrick Venture } 53af69625fSPatrick Venture 54af69625fSPatrick Venture /* Call stat on /flash/image (or /flash/tarball) and check if data interface 5500887597SPatrick Venture * is supported. 5600887597SPatrick Venture */ 57*339dece8SPatrick Venture StatResponse stat; 58*339dece8SPatrick Venture try 59*339dece8SPatrick Venture { 60*339dece8SPatrick Venture stat = blob->getStat(goalFirmware); 61*339dece8SPatrick Venture } 62*339dece8SPatrick Venture catch (const BlobException& b) 63*339dece8SPatrick Venture { 64*339dece8SPatrick Venture throw ToolException("blob exception received: " + 65*339dece8SPatrick Venture std::string(b.what())); 66*339dece8SPatrick Venture } 67*339dece8SPatrick Venture 68aa32a36aSPatrick Venture auto supported = handler->supportedType(); 69aa32a36aSPatrick Venture if ((stat.blob_state & supported) == 0) 708a55dcbdSPatrick Venture { 712bc23fe1SPatrick Venture throw ToolException("data interface selected not supported."); 728a55dcbdSPatrick Venture } 73af69625fSPatrick Venture 740533d0b0SPatrick Venture /* Yay, our data handler is supported. */ 750533d0b0SPatrick Venture std::uint16_t session; 760533d0b0SPatrick Venture try 770533d0b0SPatrick Venture { 78aa32a36aSPatrick Venture session = blob->openBlob(goalFirmware, supported); 790533d0b0SPatrick Venture } 800533d0b0SPatrick Venture catch (const BlobException& b) 810533d0b0SPatrick Venture { 822bc23fe1SPatrick Venture throw ToolException("blob exception received: " + 832bc23fe1SPatrick Venture std::string(b.what())); 840533d0b0SPatrick Venture } 850533d0b0SPatrick Venture 86fd6aaec8SPatrick Venture /* Send over the firmware image. */ 87fd6aaec8SPatrick Venture if (!handler->sendContents(imagePath, session)) 88fd6aaec8SPatrick Venture { 892bc23fe1SPatrick Venture throw ToolException("Failed to send contents of " + imagePath); 90fd6aaec8SPatrick Venture } 91fd6aaec8SPatrick Venture 92fd6aaec8SPatrick Venture /* Send over the hash contents. */ 93fd6aaec8SPatrick Venture /* Trigger the verification. */ 94fd6aaec8SPatrick Venture /* Check the verification. */ 95fd6aaec8SPatrick Venture 962bc23fe1SPatrick Venture return; 97bf58cd64SPatrick Venture } 98