blob: 35c81091ae5580ad7bbcc0fd1b3f3f52b4acfa0b [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 Mills3a482f62017-04-20 16:07:20 -050058 // Check if IMAGE DIR exists and create if necessary.
59 fs::path imgDirPath(IMG_UPLOAD_DIR);
60 if (!fs::is_directory(imgDirPath))
61 {
62 fs::create_directory(imgDirPath);
63 }
64
Gunnar Millsa927de42017-04-06 21:40:50 -050065 pid_t pid = fork();
66
67 if (pid == 0)
68 {
69 // child process
70 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(),
Gunnar Millsc7e2d242017-08-31 14:50:17 -050071 serverAddress.c_str(), "-l",
72 (std::string{IMG_UPLOAD_DIR} + '/' + fileName).c_str(),
73 (char*)0);
Gunnar Millsa927de42017-04-06 21:40:50 -050074 // execl only returns on fail
75 log<level::ERR>("Error occurred during the TFTP call");
Gunnar Millsa733df52017-04-11 13:05:52 -050076 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -050077 }
78 else if (pid < 0)
79 {
80 log<level::ERR>("Error occurred during fork");
Gunnar Millsa733df52017-04-11 13:05:52 -050081 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -050082 }
83
Gunnar Mills701e0212017-04-03 11:21:27 -050084 return;
85}
86
87} // namespace manager
88} // namespace software
89} // namespace phosphor
90