Skip to content

Commit

Permalink
PR #12463 from Eran: move extract_firmware_version_string() into ds::…
Browse files Browse the repository at this point in the history
…, d400-private
  • Loading branch information
maloel authored Dec 3, 2023
2 parents 3be0596 + 8296e55 commit 02e24f4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/ds/d400/d400-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace librealsense
throw librealsense::invalid_value_exception(
rsutils::string::from() << "Unsupported firmware binary image provided - " << image.size() << " bytes" );

std::string fw_version = firmware_check_interface::extract_firmware_version_string( image );
std::string fw_version = ds::extract_firmware_version_string( image );

auto it = ds::d400_device_to_fw_min_version.find( _pid );
if( it == ds::d400_device_to_fw_min_version.end() )
Expand Down
2 changes: 1 addition & 1 deletion src/ds/d400/d400-fw-update-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ds_d400_update_device::ds_d400_update_device(
throw librealsense::invalid_value_exception(
rsutils::string::from() << "Unsupported firmware binary image provided - " << image.size() << " bytes" );

std::string fw_version = extract_firmware_version_string(image);
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);
if (it == ds::d400_device_to_fw_min_version.end())
throw librealsense::invalid_value_exception(
Expand Down
17 changes: 17 additions & 0 deletions src/ds/d400/d400-private.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@ namespace librealsense
return results;
}

std::string extract_firmware_version_string( const std::vector< uint8_t > & fw_image )
{
auto version_offset = offsetof( platform::dfu_header, bcdDevice );
if( fw_image.size() < ( version_offset + sizeof( size_t ) ) )
throw std::runtime_error( "Firmware binary image might be corrupted - size is only: "
+ std::to_string( fw_image.size() ) );

auto version = fw_image.data() + version_offset;
uint8_t major = *( version + 3 );
uint8_t minor = *( version + 2 );
uint8_t patch = *( version + 1 );
uint8_t build = *( version );

return std::to_string( major ) + "." + std::to_string( minor ) + "." + std::to_string( patch ) + "."
+ std::to_string( build );
}

rs2_intrinsics get_d400_intrinsic_by_resolution(const vector<uint8_t>& raw_data, d400_calibration_table_id table_id, uint32_t width, uint32_t height)
{
switch (table_id)
Expand Down
2 changes: 2 additions & 0 deletions src/ds/d400/d400-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ namespace librealsense
return min_gvd_version <= cur_gvd_version;
}

std::string extract_firmware_version_string( const std::vector< uint8_t > & fw_image );

enum class d400_calibration_table_id
{
coefficients_table_id = 25,
Expand Down
8 changes: 1 addition & 7 deletions src/ds/d500/d500-fw-update-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ ds_d500_update_device::ds_d500_update_device( std::shared_ptr< const device_info
bool ds_d500_update_device::check_fw_compatibility(const std::vector<uint8_t>& image) const
{
// Currently we cannot extract FW version from HKR FW image
bool result = true;

// TODO::: Once verified we can check against the minimal FW version map
std::string fw_version = extract_firmware_version_string(image);
LOG_INFO( "FW version extracted from the FW image is" + fw_version );

return result;
return true;
}

std::string ds_d500_update_device::parse_serial_number(const std::vector<uint8_t>& buffer) const
Expand Down
43 changes: 21 additions & 22 deletions src/fw-update/fw-update-device-interface.h
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2019 Intel Corporation. All Rights Reserved.

// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
#pragma once

#include "../types.h"
#include <src/core/device-interface.h>
#include "../usb/usb-types.h"
#include <cstddef>
#include <librealsense2/hpp/rs_types.hpp>
#include <cstdint>
#include <vector>


namespace librealsense
{
class firmware_check_interface
{
public:
virtual bool check_fw_compatibility(const std::vector<uint8_t>& image) const = 0;
static std::string extract_firmware_version_string(const std::vector<uint8_t>& fw_image)
{
auto version_offset = offsetof(platform::dfu_header, bcdDevice);
if (fw_image.size() < (version_offset + sizeof(size_t)))
throw std::runtime_error("Firmware binary image might be corrupted - size is only: " + std::to_string( fw_image.size() ));

auto version = fw_image.data() + version_offset;
uint8_t major = *(version + 3);
uint8_t minor = *(version + 2);
uint8_t patch = *(version + 1);
uint8_t build = *(version);

return std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(patch) + "." + std::to_string(build);
}
};

// Regular devices inherit to enable entering DFU state or implementing unsigned FW update.
class updatable : public firmware_check_interface
{
public:
// Places the device in DFU (recovery) mode, where the DFU process can continue with update_device_interface.
// Restarts the device!
virtual void enter_update_state() const = 0;
virtual std::vector<uint8_t> backup_flash( rs2_update_progress_callback_sptr callback) = 0;
virtual void update_flash(const std::vector<uint8_t>& image, rs2_update_progress_callback_sptr callback, int update_mode) = 0;

// Returns a backup of the current flash image. Optional: return an empty buffer if unsupported
virtual std::vector< uint8_t > backup_flash( rs2_update_progress_callback_sptr callback ) = 0;

// Unsigned FW update. When this is called, we're not in an "update state".
virtual void update_flash( const std::vector< uint8_t > & image,
rs2_update_progress_callback_sptr callback,
int update_mode )
= 0;
};

MAP_EXTENSION( RS2_EXTENSION_UPDATABLE, updatable );

// Recovery devices implement this to perform DFU with signed FW.
class update_device_interface : public device_interface, public firmware_check_interface
{
public:
virtual void update(const void* fw_image, int fw_image_size, rs2_update_progress_callback_sptr = nullptr) const = 0;
// Signed FW update
virtual void update( const void * fw_image, int fw_image_size, rs2_update_progress_callback_sptr = nullptr ) const = 0;

protected:
virtual const std::string& get_name() const = 0;
virtual const std::string& get_product_line() const = 0;
Expand Down

0 comments on commit 02e24f4

Please sign in to comment.