Aktualizr
C++ SOTA Client
legacysecondary.cc
1 #include "uptane/legacysecondary.h"
2 
3 #include <sstream>
4 
5 #include <boost/algorithm/hex.hpp>
6 #include <boost/filesystem.hpp>
7 
8 #include "crypto/crypto.h"
9 #include "logging/logging.h"
10 #include "utilities/utils.h"
11 
12 namespace Uptane {
13 LegacySecondary::LegacySecondary(const SecondaryConfig& sconfig_in) : ManagedSecondary(sconfig_in) {}
14 
15 bool LegacySecondary::storeFirmware(const std::string& target_name, const std::string& content) {
16  // reading target hash back is not currently supported, so primary needs to save the firmware file locally
17  Utils::writeFile(sconfig.target_name_path, target_name);
18  Utils::writeFile(sconfig.firmware_path, content);
19  sync();
20 
21  std::stringstream command;
22  std::string output;
23  command << sconfig.flasher.string() << " install-software --hardware-identifier " << sconfig.ecu_hardware_id
24  << " --ecu-identifier " << getSerial() << " --firmware " << sconfig.firmware_path.string() << " --loglevel "
25  << loggerGetSeverity();
26  int rs = Utils::shell(command.str(), &output);
27 
28  if (rs != 0) {
29  // TODO: Should we do anything with the return value?
30  // 1 - The firmware image is invalid.
31  // 2 - Installation failure. The previous firmware was not modified.
32  // 3 - Installation failure. The previous firmware was partially overwritten or erased.
33  LOG_ERROR << "Legacy external flasher install-software command failed: " << output;
34  }
35  return (rs == 0);
36 }
37 
38 bool LegacySecondary::getFirmwareInfo(std::string* target_name, size_t& target_len, std::string* sha256hash) {
39  std::string content;
40 
41  // reading target hash back is not currently supported, just use the saved file
42  if (!boost::filesystem::exists(sconfig.target_name_path) || !boost::filesystem::exists(sconfig.firmware_path)) {
43  *target_name = std::string("noimage");
44  content = "";
45  } else {
46  *target_name = Utils::readFile(sconfig.target_name_path.string());
47  content = Utils::readFile(sconfig.firmware_path.string());
48  }
49  *sha256hash = boost::algorithm::to_lower_copy(boost::algorithm::hex(Crypto::sha256digest(content)));
50  target_len = content.size();
51 
52  return true;
53 }
54 } // namespace Uptane
Base data types that are used in The Update Framework (TUF), part of UPTANE.