blob: b89ac22c6c87decc175339d6bc664fa50b40c992 [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 Millsc7e2d242017-08-31 14:50:17 -050028 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Gunnar Mills49739432017-04-21 11:03:34 -050029
30 // Sanitize the fileName string
Brad Bishop8a24dda2017-08-31 11:25:20 -040031 if (!fileName.empty())
32 {
33 fileName.erase(std::remove(fileName.begin(), fileName.end(), '/'),
34 fileName.end());
35 fileName = fileName.substr(fileName.find_first_not_of('.'));
36 }
Gunnar Mills49739432017-04-21 11:03:34 -050037
Gunnar Millsa927de42017-04-06 21:40:50 -050038 if (fileName.empty())
39 {
40 log<level::ERR>("Error FileName is empty");
Gunnar Millsc7e2d242017-08-31 14:50:17 -050041 elog<InvalidArgument>(Argument::ARGUMENT_NAME("FileName"),
42 Argument::ARGUMENT_VALUE(fileName.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050043 return;
44 }
45
46 if (serverAddress.empty())
47 {
48 log<level::ERR>("Error ServerAddress is empty");
Gunnar Millsc7e2d242017-08-31 14:50:17 -050049 elog<InvalidArgument>(Argument::ARGUMENT_NAME("ServerAddress"),
50 Argument::ARGUMENT_VALUE(serverAddress.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050051 return;
52 }
53
54 log<level::INFO>("Downloading via TFTP",
55 entry("FILENAME=%s", fileName),
56 entry("SERVERADDRESS=%s", serverAddress));
57
Gunnar Mills7e1abfc2017-11-09 15:43:50 -060058 // Check if IMAGE DIR exists
Gunnar Mills3a482f62017-04-20 16:07:20 -050059 fs::path imgDirPath(IMG_UPLOAD_DIR);
60 if (!fs::is_directory(imgDirPath))
61 {
Gunnar Mills7e1abfc2017-11-09 15:43:50 -060062 log<level::ERR>("Error Image Dir does not exist");
63 elog<InternalFailure>();
64 return;
Gunnar Mills3a482f62017-04-20 16:07:20 -050065 }
66
Gunnar Millsa927de42017-04-06 21:40:50 -050067 pid_t pid = fork();
68
69 if (pid == 0)
70 {
71 // child process
72 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(),
Gunnar Millsc7e2d242017-08-31 14:50:17 -050073 serverAddress.c_str(), "-l",
74 (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(),
75 (char*)0);
Gunnar Millsa927de42017-04-06 21:40:50 -050076 // 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