blob: fc217f02f63223a8fffae5caa3d8990a9ef47cca [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 Millsa927de42017-04-06 21:40:50 -05007#include "config.h"
Gunnar Millsa733df52017-04-11 13:05:52 -05008#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/elog-errors.hpp>
10#include "xyz/openbmc_project/Common/error.hpp"
Gunnar Mills701e0212017-04-03 11:21:27 -050011#include "download_manager.hpp"
12
13namespace phosphor
14{
15namespace software
16{
17namespace manager
18{
19
Gunnar Millsa733df52017-04-11 13:05:52 -050020using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Gunnar Millsa927de42017-04-06 21:40:50 -050021using namespace phosphor::logging;
Gunnar Mills3a482f62017-04-20 16:07:20 -050022namespace fs = std::experimental::filesystem;
Gunnar Millsa927de42017-04-06 21:40:50 -050023
Gunnar Mills701e0212017-04-03 11:21:27 -050024void Download::downloadViaTFTP(const std::string fileName,
25 const std::string serverAddress)
26{
Gunnar Millsa927de42017-04-06 21:40:50 -050027 if (fileName.empty())
28 {
29 log<level::ERR>("Error FileName is empty");
Gunnar Millsa733df52017-04-11 13:05:52 -050030 elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument::
31 ARGUMENT_NAME("FileName"),
32 xyz::openbmc_project::Common::InvalidArgument::
33 ARGUMENT_VALUE(fileName.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050034 return;
35 }
36
37 if (serverAddress.empty())
38 {
39 log<level::ERR>("Error ServerAddress is empty");
Gunnar Millsa733df52017-04-11 13:05:52 -050040 elog<InvalidArgument>(xyz::openbmc_project::Common::InvalidArgument::
41 ARGUMENT_NAME("ServerAddress"),
42 xyz::openbmc_project::Common::InvalidArgument::
43 ARGUMENT_VALUE(serverAddress.c_str()));
Gunnar Millsa927de42017-04-06 21:40:50 -050044 return;
45 }
46
47 log<level::INFO>("Downloading via TFTP",
48 entry("FILENAME=%s", fileName),
49 entry("SERVERADDRESS=%s", serverAddress));
50
Gunnar Mills3a482f62017-04-20 16:07:20 -050051 // Check if IMAGE DIR exists and create if necessary.
52 fs::path imgDirPath(IMG_UPLOAD_DIR);
53 if (!fs::is_directory(imgDirPath))
54 {
55 fs::create_directory(imgDirPath);
56 }
57
Gunnar Millsa927de42017-04-06 21:40:50 -050058 pid_t pid = fork();
59
60 if (pid == 0)
61 {
62 // child process
63 execl("/usr/bin/tftp", "tftp", "-g", "-r", fileName.c_str(),
64 serverAddress.c_str(), "-l", (std::string{IMG_UPLOAD_DIR} + '/' +
65 fileName).c_str(), (char*)0);
66 // execl only returns on fail
67 log<level::ERR>("Error occurred during the TFTP call");
Gunnar Millsa733df52017-04-11 13:05:52 -050068 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -050069 }
70 else if (pid < 0)
71 {
72 log<level::ERR>("Error occurred during fork");
Gunnar Millsa733df52017-04-11 13:05:52 -050073 elog<InternalFailure>();
Gunnar Millsa927de42017-04-06 21:40:50 -050074 }
75
Gunnar Mills701e0212017-04-03 11:21:27 -050076 return;
77}
78
79} // namespace manager
80} // namespace software
81} // namespace phosphor
82