2015-07-15 16:32:18 +00:00
|
|
|
//
|
|
|
|
|
// Copyright 2014-2015 Ettus Research LLC
|
2018-02-19 23:30:32 +00:00
|
|
|
// Copyright 2018 Ettus Research, a National Instruments Company
|
2015-07-15 16:32:18 +00:00
|
|
|
//
|
2018-02-19 23:30:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
2015-07-15 16:32:18 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <uhd/exception.hpp>
|
|
|
|
|
#include <uhd/image_loader.hpp>
|
|
|
|
|
#include <uhd/utils/log.hpp>
|
|
|
|
|
#include <uhd/utils/static.hpp>
|
2020-03-02 23:25:13 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
#include <boost/format.hpp>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <map>
|
|
|
|
|
#include <utility>
|
2015-07-15 16:32:18 +00:00
|
|
|
|
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
|
|
|
|
typedef std::map<std::string, uhd::image_loader::loader_fcn_t> loader_fcn_map_t;
|
|
|
|
|
typedef std::map<std::string, std::string> string_map_t;
|
|
|
|
|
|
|
|
|
|
// Nice typedefs for iterating over std::map
|
|
|
|
|
typedef std::pair<std::string, uhd::image_loader::loader_fcn_t> loader_fcn_pair_t;
|
|
|
|
|
typedef std::pair<std::string, std::string> string_pair_t;
|
|
|
|
|
|
|
|
|
|
UHD_SINGLETON_FCN(loader_fcn_map_t, get_image_loaders);
|
2020-03-02 23:25:13 +00:00
|
|
|
UHD_SINGLETON_FCN(string_map_t, get_recovery_strings);
|
2015-07-15 16:32:18 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Registration
|
|
|
|
|
*/
|
2020-03-02 23:25:13 +00:00
|
|
|
void uhd::image_loader::register_image_loader(const std::string& device_type,
|
|
|
|
|
const loader_fcn_t& loader_fcn,
|
|
|
|
|
const std::string& recovery_instructions)
|
|
|
|
|
{
|
|
|
|
|
// UHD_LOGGER_TRACE("UHD") << "Registering image loader and recovery instructions for
|
|
|
|
|
// "
|
2017-05-20 00:43:26 +00:00
|
|
|
// << device_type;
|
2015-07-15 16:32:18 +00:00
|
|
|
|
|
|
|
|
get_image_loaders().insert(loader_fcn_pair_t(device_type, loader_fcn));
|
|
|
|
|
get_recovery_strings().insert(string_pair_t(device_type, recovery_instructions));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Actual loading
|
|
|
|
|
*/
|
2020-03-02 23:25:13 +00:00
|
|
|
bool uhd::image_loader::load(
|
|
|
|
|
const uhd::image_loader::image_loader_args_t& image_loader_args)
|
|
|
|
|
{
|
2015-07-15 16:32:18 +00:00
|
|
|
// If "type=foo" given in args, see if we have an image loader for that
|
2020-03-02 23:25:13 +00:00
|
|
|
if (image_loader_args.args.has_key("type")) {
|
2015-07-15 16:32:18 +00:00
|
|
|
std::string type = image_loader_args.args.get("type");
|
2020-03-02 23:25:13 +00:00
|
|
|
if (get_image_loaders().find(type) == get_image_loaders().end()) {
|
|
|
|
|
throw uhd::runtime_error(
|
|
|
|
|
str(boost::format(
|
|
|
|
|
"There is no image loader registered for given type \"%s\".")
|
|
|
|
|
% type));
|
|
|
|
|
} else
|
|
|
|
|
return get_image_loaders().at(type)(image_loader_args);
|
|
|
|
|
} else {
|
2021-01-08 12:23:17 +00:00
|
|
|
for (const auto& loader_fcn_pair : get_image_loaders()) {
|
2020-03-02 23:25:13 +00:00
|
|
|
if (loader_fcn_pair.second(image_loader_args))
|
|
|
|
|
return true;
|
2015-07-15 16:32:18 +00:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Get recovery instructions for particular device
|
|
|
|
|
*/
|
2020-03-02 23:25:13 +00:00
|
|
|
std::string uhd::image_loader::get_recovery_instructions(const std::string& device_type)
|
|
|
|
|
{
|
|
|
|
|
if (get_recovery_strings().count(device_type) == 0) {
|
|
|
|
|
return "A firmware or FPGA loading process was interrupted by the user. This can "
|
|
|
|
|
"leave your device in a non-working state.";
|
|
|
|
|
} else
|
|
|
|
|
return get_recovery_strings().at(device_type);
|
2015-07-15 16:32:18 +00:00
|
|
|
}
|