Skip to content

Commit

Permalink
DFU for D457 recovery device
Browse files Browse the repository at this point in the history
  • Loading branch information
dmipx committed May 20, 2024
1 parent 43133ff commit b9cb41c
Show file tree
Hide file tree
Showing 17 changed files with 420 additions and 21 deletions.
2 changes: 2 additions & 0 deletions include/librealsense2/hpp/rs_device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ namespace rs2
std::string pid = get_info( RS2_CAMERA_INFO_PRODUCT_ID );
if( pid == "ABCD" ) // Specific for D457
return "GMSL";
if( pid == "BBCD" ) // Specific for D457 Recovery DFU
return "GMSL";
return pid; // for DDS devices, this will be "DDS"
}
return {};
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ target_sources(${LRS_TARGET}
"${CMAKE_CURRENT_LIST_DIR}/platform/hid-data.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/hid-device.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/hid-device-info.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/mipi-device-info.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/playback-device-info.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/platform-utils.h"
"${CMAKE_CURRENT_LIST_DIR}/platform/platform-utils.cpp"
Expand Down
10 changes: 9 additions & 1 deletion src/backend-device-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ std::vector< std::shared_ptr< device_info > > backend_device_factory::query_devi
auto backend = _device_watcher->get_backend();
platform::backend_device_group group( backend->query_uvc_devices(),
backend->query_usb_devices(),
backend->query_mipi_devices(),
backend->query_hid_devices() );
auto devices = create_devices_from_group( group, requested_mask );
return { devices.begin(), devices.end() };
Expand Down Expand Up @@ -214,6 +215,13 @@ backend_device_factory::create_devices_from_group( platform::backend_device_grou
std::copy( begin( recovery_devices ), end( recovery_devices ), std::back_inserter( list ) );
}

// Supported mipi recovery devices
{
auto recovery_devices
= fw_update_info::pick_recovery_devices( ctx, devices.mipi_devices, mask );
std::copy( begin( recovery_devices ), end( recovery_devices ), std::back_inserter( list ) );
}

if( mask & RS2_PRODUCT_LINE_NON_INTEL )
{
auto uvc_devices
Expand All @@ -225,4 +233,4 @@ backend_device_factory::create_devices_from_group( platform::backend_device_grou
}


} // namespace librealsense
} // namespace librealsense
5 changes: 5 additions & 0 deletions src/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "platform/uvc-device-info.h"
#include "platform/hid-device-info.h"
#include "platform/mipi-device-info.h"
#include "platform/stream-profile.h"
#include "platform/frame-object.h"

Expand All @@ -27,6 +28,7 @@ namespace librealsense
class device_watcher;
class hid_device;
class uvc_device;
class mipi_device;
class command_transfer;


Expand All @@ -42,6 +44,9 @@ namespace librealsense
virtual std::shared_ptr<hid_device> create_hid_device(hid_device_info info) const = 0;
virtual std::vector<hid_device_info> query_hid_devices() const = 0;

virtual std::shared_ptr<mipi_device> create_mipi_device(mipi_device_info info) const = 0;
virtual std::vector<mipi_device_info> query_mipi_devices() const = 0;

virtual std::shared_ptr<device_watcher> create_device_watcher() const = 0;

virtual std::string get_device_serial(uint16_t device_vid, uint16_t device_pid, const std::string& device_uid) const
Expand Down
17 changes: 16 additions & 1 deletion src/ds/d400/d400-fw-update-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ ds_d400_update_device::ds_d400_update_device(
_serial_number = parse_serial_number(_serial_number_buffer);
}

ds_d400_update_device::ds_d400_update_device(
std::shared_ptr< const device_info > const & dev_info,
std::shared_ptr< platform::mipi_device > const & mipi_device )
: update_device( dev_info, mipi_device, "D400" )
{
auto info = mipi_device->get_info();
_name = ds::rs400_sku_names.find(info.pid) != ds::rs400_sku_names.end() ? ds::rs400_sku_names.at(info.pid) : "unknown";
_serial_number = info.serial_number;
}


bool ds_d400_update_device::check_fw_compatibility(const std::vector<uint8_t>& image) const
{
Expand All @@ -27,7 +37,12 @@ ds_d400_update_device::ds_d400_update_device(
rsutils::string::from() << "Unsupported firmware binary image provided - " << image.size() << " bytes" );

std::string fw_version = ds::extract_firmware_version_string(image);
auto it = ds::d400_device_to_fw_min_version.find(_usb_device->get_info().pid);
uint16_t pid;
if (_usb_device != nullptr )
pid = _usb_device->get_info().pid;
else if (_mipi_device != nullptr )
pid = _mipi_device->get_info().pid;
auto it = ds::d400_device_to_fw_min_version.find(pid);
if (it == ds::d400_device_to_fw_min_version.end())
throw librealsense::invalid_value_exception(
rsutils::string::from() << "Min and Max firmware versions have not been defined for this device: "
Expand Down
2 changes: 2 additions & 0 deletions src/ds/d400/d400-fw-update-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace librealsense
public:
ds_d400_update_device( std::shared_ptr< const device_info > const &,
std::shared_ptr< platform::usb_device > const & usb_device );
ds_d400_update_device( std::shared_ptr< const device_info > const &,
std::shared_ptr< platform::mipi_device > const & mipi_device );
virtual ~ds_d400_update_device() = default;

virtual bool check_fw_compatibility(const std::vector<uint8_t>& image) const override;
Expand Down
7 changes: 5 additions & 2 deletions src/ds/d400/d400-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace librealsense
const uint16_t RS405_PID = 0x0B5B; // D405
const uint16_t RS455_PID = 0x0B5C; // D455
const uint16_t RS457_PID = 0xabcd; // D457
const uint16_t RS457_RECOVERY_PID = 0xbbcd; // D457 DFU Recovery

// d400 Devices supported by the current version
static const std::set<std::uint16_t> rs400_sku_pid = {
Expand Down Expand Up @@ -124,6 +125,7 @@ namespace librealsense
{ RS405_PID, "Intel RealSense D405" },
{ RS455_PID, "Intel RealSense D455" },
{ RS457_PID, "Intel RealSense D457" },
{ RS457_RECOVERY_PID, "Intel RealSense D457 Recovery"},
};

static std::map<uint16_t, std::string> d400_device_to_fw_min_version = {
Expand All @@ -150,7 +152,8 @@ namespace librealsense
{RS416_RGB_PID, "5.8.15.0" },
{RS405_PID, "5.12.11.8" },
{RS455_PID, "5.13.0.50" },
{RS457_PID, "5.13.1.1" }
{RS457_PID, "5.13.1.1" },
{RS457_RECOVERY_PID, "5.13.1.1" }
};

std::vector<platform::uvc_device_info> filter_d400_device_by_capability(
Expand Down Expand Up @@ -330,4 +333,4 @@ namespace librealsense
};

} // namespace ds
} // namespace librealsense
} // namespace librealsense
80 changes: 78 additions & 2 deletions src/fw-update/fw-update-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include <stdexcept>
#include <algorithm>
#include <thread>

#include <string>
#include <regex>
#define DEFAULT_TIMEOUT 100
#define FW_UPDATE_INTERFACE_NUMBER 0
namespace librealsense
Expand Down Expand Up @@ -140,12 +141,31 @@ namespace librealsense
}
}

update_device::update_device( std::shared_ptr< const device_info > const & dev_info,
std::shared_ptr< platform::mipi_device > const & mipi_device,
const std::string & product_line )
: _dev_info( dev_info )
, _mipi_device( mipi_device )
, _physical_port( mipi_device->get_info().dfu_device_path )
, _pid( rsutils::string::from() << std::uppercase << rsutils::string::hexdump( mipi_device->get_info().pid ))
, _product_line( product_line )
, _serial_number( mipi_device->get_info().serial_number )
{
std::ifstream fw_path_in_device(_physical_port);
if (!fw_path_in_device)
{
throw std::runtime_error("Firmware Update failed - wrong path or permissions missing");
return;
}
fw_path_in_device.close();
}

update_device::~update_device()
{

}

void update_device::update(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr update_progress_callback) const
void update_device::update_usb(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr update_progress_callback) const
{
// checking fw compatibility (covering the case of recovery device with wrong product line fw )
std::vector<uint8_t> buffer((uint8_t*)fw_image, (uint8_t*)fw_image + fw_image_size);
Expand Down Expand Up @@ -223,6 +243,62 @@ namespace librealsense
throw std::runtime_error("Firmware manifest failed");
}

void update_device::update_mipi(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr update_progress_callback) const
{
// checking fw compatibility (covering the case of recovery device with wrong product line fw )
std::vector<uint8_t> buffer((uint8_t*)fw_image, (uint8_t*)fw_image + fw_image_size);
const size_t transfer_size = 1024;

size_t remaining_bytes = fw_image_size;
uint16_t blocks_count = uint16_t( fw_image_size / transfer_size );
uint16_t block_number = 0;

size_t offset = 0;
uint32_t transferred = 0;

if (!check_fw_compatibility(buffer))
throw librealsense::invalid_value_exception("Device: " + get_serial_number() + " failed to update firmware\nImage is unsupported for this device or corrupted");
// Write signed firmware to appropriate file descriptor

std::ofstream fw_path_in_device(_physical_port, std::ios::binary);
if (!fw_path_in_device)
{
throw std::runtime_error("Firmware Update failed - wrong path or permissions missing");
return;
}
while (remaining_bytes > 0)
{
size_t chunk_size = std::min(transfer_size, remaining_bytes);

auto curr_block = ((uint8_t*)fw_image + offset);

fw_path_in_device.write(reinterpret_cast<const char*>(curr_block), chunk_size);

block_number++;
remaining_bytes -= chunk_size;
offset += chunk_size;

float progress = (float)block_number / (float)blocks_count;
LOG_DEBUG("fw update progress: " << progress);
if (update_progress_callback)
update_progress_callback->on_update_progress(progress);
}
LOG_INFO("Firmware Update for MIPI device done.");
fw_path_in_device.close();
}

void update_device::update(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr update_progress_callback) const
{
if(_pid == "ABCD" || _pid == "BBCD")
{
update_mipi(fw_image, fw_image_size, update_progress_callback);
}
else
{
update_usb(fw_image, fw_image_size, update_progress_callback);
}
}

sensor_interface& update_device::get_sensor(size_t i)
{
throw std::runtime_error("try to get sensor from fw loader device");
Expand Down
11 changes: 10 additions & 1 deletion src/fw-update/fw-update-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <src/core/device-interface.h>
#include "fw-update-device-interface.h"
#include "usb/usb-device.h"
#include "src/platform/mipi-device-info.h"

namespace librealsense
{
Expand Down Expand Up @@ -102,10 +103,15 @@ namespace librealsense
update_device( std::shared_ptr< const device_info > const &,
std::shared_ptr< platform::usb_device > const & usb_device,
const std::string & _product_line );

update_device( std::shared_ptr< const device_info > const &,
std::shared_ptr< platform::mipi_device > const & mipi_device,
const std::string & _product_line );

virtual ~update_device();

virtual void update(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr = nullptr) const override;

virtual sensor_interface& get_sensor(size_t i) override;

virtual const sensor_interface& get_sensor(size_t i) const override;
Expand Down Expand Up @@ -143,6 +149,8 @@ namespace librealsense
virtual void enable_recording(std::function<void(const info_interface&)> recording_function) override;

protected:
void update_usb(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr = nullptr) const;
void update_mipi(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr = nullptr) const;
rs2_dfu_state get_dfu_state(std::shared_ptr<platform::usb_messenger> messenger) const;
void detach(std::shared_ptr<platform::usb_messenger> messenger) const;
bool wait_for_state(std::shared_ptr<platform::usb_messenger> messenger, const rs2_dfu_state state, size_t timeout = 1000) const;
Expand All @@ -155,6 +163,7 @@ namespace librealsense

const std::shared_ptr< const device_info > _dev_info;
const platform::rs_usb_device _usb_device;
const platform::rs_mipi_device _mipi_device;
std::vector<uint8_t> _serial_number_buffer;
std::string _highest_fw_version;
std::string _last_fw_version;
Expand Down
55 changes: 44 additions & 11 deletions src/fw-update/fw-update-factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,57 @@ namespace librealsense
return list;
}

std::vector< std::shared_ptr< fw_update_info > > fw_update_info::pick_recovery_devices(
std::shared_ptr< context > ctx, const std::vector< platform::mipi_device_info > & mipi_devices, int mask )
{
std::vector< std::shared_ptr< fw_update_info > > list;
for (auto&& mipi : mipi_devices)
{
list.push_back(std::make_shared<fw_update_info>(ctx, mipi));
}
return list;
}


std::shared_ptr<device_interface> fw_update_info::create_device()
{
auto devices = platform::usb_enumerator::query_devices_info();
auto const & dfu_id = get_group().usb_devices.front().id;
for (auto&& info : devices)
{
if( info.id == dfu_id )
auto & dfu_id = get_group().usb_devices.front().id;

auto const & mipi_id = get_group().mipi_devices.front().id;

if (&dfu_id != nullptr) {
for (auto&& info : devices)
{
if( info.id == dfu_id )
{
auto usb = platform::usb_enumerator::create_usb_device(info);
if (!usb)
continue;
switch( info.pid )
{
case ds::RS_D400_RECOVERY_PID:
case ds::RS_D400_USB2_RECOVERY_PID:
return std::make_shared< ds_d400_update_device >( shared_from_this(), usb );
case ds::D555E_RECOVERY_PID:
return std::make_shared< ds_d500_update_device >( shared_from_this(), usb );
default:
// Do nothing
break;
}
}
}
}
else {
for (auto&& info: get_group().mipi_devices)
{
auto usb = platform::usb_enumerator::create_usb_device(info);
if (!usb)
auto mipi = platform::mipi_device::create_mipi_device(info);
if (!mipi)
continue;
switch( info.pid )
{
case ds::RS_D400_RECOVERY_PID:
case ds::RS_D400_USB2_RECOVERY_PID:
return std::make_shared< ds_d400_update_device >( shared_from_this(), usb );
case ds::D555E_RECOVERY_PID:
return std::make_shared< ds_d500_update_device >( shared_from_this(), usb );
case 0xbbcd:
return std::make_shared< ds_d400_update_device >( shared_from_this(), mipi );
default:
// Do nothing
break;
Expand All @@ -70,4 +102,5 @@ namespace librealsense
throw std::runtime_error( rsutils::string::from()
<< "Failed to create FW update device, device id: " << dfu_id );
}

}
6 changes: 6 additions & 0 deletions src/fw-update/fw-update-factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ namespace librealsense
static std::vector< std::shared_ptr< fw_update_info > > pick_recovery_devices(
std::shared_ptr< context > ctx, const std::vector< platform::usb_device_info > & usb_devices, int mask );

static std::vector< std::shared_ptr< fw_update_info > > pick_recovery_devices(
std::shared_ptr< context > ctx, const std::vector< platform::mipi_device_info > & mipi_devices, int mask );

explicit fw_update_info(std::shared_ptr<context> ctx, platform::usb_device_info const & dfu)
: platform_device_info( ctx, { { dfu } } ) {}

explicit fw_update_info(std::shared_ptr<context> ctx, platform::mipi_device_info const & dfu)
: platform_device_info( ctx, { { dfu } } ) {}

std::string get_address() const override { return "recovery:" + super::get_address(); }

private:
Expand Down
Loading

0 comments on commit b9cb41c

Please sign in to comment.