blob: e6f7728dd02236ca506c417ee74b0c78407f0306 [file] [log] [blame]
Gunnar Millsa927de42017-04-06 21:40:50 -05001#include <iostream>
2#include <string>
3#include <unistd.h>
4#include <sys/wait.h>
5#include <phosphor-logging/log.hpp>
Gunnar Mills3a482f62017-04-20 16:07:20 -05006#include <experimental/filesystem>
Gunnar Mills49739432017-04-21 11:03:34 -05007#include <algorithm>
Gunnar Millsa927de42017-04-06 21:40:50 -05008#include "config.h"
Gunnar Millsa733df52017-04-11 13:05:52 -05009#include <phosphor-logging/elog.hpp>
10#include <phosphor-logging/elog-errors.hpp>
11#include "xyz/openbmc_project/Common/error.hpp"
Gunnar Mills701e0212017-04-03 11:21:27 -050012#include "download_manager.hpp"
13
14namespace phosphor
15{
16namespace software
17{
18namespace manager
19{
20
Gunnar Millsa733df52017-04-11 13:05:52 -050021using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Millsa927de42017-04-06 21:40:50 -050022using namespace phosphor::logging;
Gunnar Mills3a482f62017-04-20 16:07:20 -050023namespace fs = std::experimental::filesystem;
Gunnar Millsa927de42017-04-06 21:40:50 -050024
Gunnar Mills49739432017-04-21 11:03:34 -050025void Download::downloadViaTFTP(std::string fileName,
26 std::string serverAddress)
Gunnar Mills701e0212017-04-03 11:21:27 -050027{
Gunnar Mills49739432017-04-21 11:03:34 -050028
29 // Sanitize the fileName string
Brad Bishop8a24dda2017-08-31 11:25:20 -040030 if (!fileName.empty())
31 {
32 fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'),
33 fileName.end());
34 fileName = fileName.substr(fileName.find_first_not_of('.'));
35 }
Gunnar Mills49739432017-04-21 11:03:34 -050036
Gunnar Millsa927de42017-04-06 21:40:50 -050037 if (fileName.empty())
38 {
39 log<level::ERR>("Error FileName is empty");
Gunnar Millsa733df52017-04-11 13:05:52 -050040 elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument::
41 ARGUMENT_NAME("FileName"),
42 xyz::openbmc_project::Common::InvalidArgument::
43 ARGUMENT_VALUE(fileName.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050044 return;
45 }
46
47 if (serverAddress.empty())
48 {
49 log<level::ERR>("Error ServerAddress is empty");
Gunnar Millsa733df52017-04-11 13:05:52 -050050 elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument::
51 ARGUMENT_NAME("ServerAddress"),
52 xyz::openbmc_project::Common::InvalidArgument::
53 ARGUMENT_VALUE(serverAddress.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050054 return;
55 }
56
57 log<level::INFO>("Downloading via TFTP",
58 entry("FILENAME=%s", fileName),
59 entry("SERVERADDRESS=%s", serverAddress));
60
Gunnar Mills3a482f62017-04-20 16:07:20 -050061 // Check if IMAGE DIR exists and create if necessary.
62 fs::path imgDirPath(IMG_UPLOAD_DIR);
63 if (!fs::is_directory(imgDirPath))
64 {
65 fs::create_directory(imgDirPath);
66 }
67
Gunnar Millsa927de42017-04-06 21:40:50 -050068 pid_t pid = fork();
69
70 if (pid == 0)
71 {
72 // child process
73 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(),
74 serverAddress.c_str(), "-l", (std::string{IMG_UPLOAD_DIR} + '/' +
75 fileName).c_str(), (char*)0);
76 // execl only returns on fail
77 log<level::ERR>("Error occurred during the TFTP call");
Gunnar Millsa733df52017-04-11 13:05:52 -050078 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -050079 }
80 else if (pid < 0)
81 {
82 log<level::ERR>("Error occurred during fork");
Gunnar Millsa733df52017-04-11 13:05:52 -050083 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -050084 }
85
Gunnar Mills701e0212017-04-03 11:21:27 -050086 return;
87}
88
89} // namespace manager
90} // namespace software
91} // namespace phosphor
92