-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
fw-update-helper.cpp
663 lines (560 loc) · 23.5 KB
/
fw-update-helper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
#include "fw-update-helper.h"
#include "model-views.h"
#include "viewer.h"
#include "ux-window.h"
#include <rsutils/os/special-folder.h>
#include "os.h"
#include <rsutils/easylogging/easyloggingpp.h>
#include <map>
#include <vector>
#include <string>
#include <thread>
#include <condition_variable>
#ifdef INTERNAL_FW
#include "common/fw/D4XX_FW_Image.h"
#else
#define FW_D4XX_FW_IMAGE_VERSION ""
const char* fw_get_D4XX_FW_Image(int) { return NULL; }
#endif // INTERNAL_FW
constexpr const char* recommended_fw_url = "https://dev.intelrealsense.com/docs/firmware-updates";
namespace rs2
{
enum firmware_update_ui_state
{
RS2_FWU_STATE_INITIAL_PROMPT = 0,
RS2_FWU_STATE_IN_PROGRESS = 1,
RS2_FWU_STATE_COMPLETE = 2,
RS2_FWU_STATE_FAILED = 3,
};
bool is_recommended_fw_available(const std::string& product_line, const std::string& pid)
{
auto pl = parse_product_line(product_line);
auto fv = get_available_firmware_version(pl, pid);
return !(fv == "");
}
int parse_product_line(const std::string& product_line)
{
if (product_line == "D400") return RS2_PRODUCT_LINE_D400;
else return -1;
}
std::string get_available_firmware_version(int product_line, const std::string& pid)
{
if (product_line == RS2_PRODUCT_LINE_D400) return FW_D4XX_FW_IMAGE_VERSION;
else return "";
}
std::vector< uint8_t > get_default_fw_image( int product_line, const std::string & pid )
{
std::vector< uint8_t > image;
switch( product_line )
{
case RS2_PRODUCT_LINE_D400:
{
bool allow_rc_firmware = config_file::instance().get_or_default( configurations::update::allow_rc_firmware, false );
if( strlen( FW_D4XX_FW_IMAGE_VERSION ) && ! allow_rc_firmware )
{
int size = 0;
auto hex = fw_get_D4XX_FW_Image( size );
image = std::vector< uint8_t >( hex, hex + size );
}
}
break;
default:
break;
}
return image;
}
bool is_upgradeable(const std::string& curr, const std::string& available)
{
if (curr == "" || available == "") return false;
return rsutils::version( curr ) < rsutils::version( available );
}
bool firmware_update_manager::check_for(
std::function<bool()> action, std::function<void()> cleanup,
std::chrono::system_clock::duration delta)
{
using namespace std;
using namespace std::chrono;
auto start = system_clock::now();
auto now = system_clock::now();
do
{
try
{
if (action()) return true;
}
catch (const error& e)
{
fail(error_to_string(e));
cleanup();
return false;
}
catch (const std::exception& ex)
{
fail(ex.what());
cleanup();
return false;
}
catch (...)
{
fail("Unknown error during update.\nPlease reconnect the camera to exit recovery mode");
cleanup();
return false;
}
now = system_clock::now();
this_thread::sleep_for(milliseconds(100));
} while (now - start < delta);
return false;
}
void firmware_update_manager::process_mipi()
{
if (!_is_signed)
{
fail("Signed FW update for MIPI device - This FW file is not signed ");
return;
}
auto dev_updatable = _dev.as<updatable>();
if(!(dev_updatable && dev_updatable.check_firmware_compatibility(_fw)))
{
fail("Firmware Update failed - fw version must be newer than version 5.13.1.1");
return;
}
log("Burning Signed Firmware on MIPI device");
// Enter DFU mode
auto device_debug = _dev.as<rs2::debug_protocol>();
uint32_t dfu_opcode = 0x1e;
device_debug.build_command(dfu_opcode, 1);
_progress = 30;
// Write signed firmware to appropriate file descriptor
std::ofstream fw_path_in_device(_dev.get_info(RS2_CAMERA_INFO_DFU_DEVICE_PATH), std::ios::binary);
if (fw_path_in_device)
{
fw_path_in_device.write(reinterpret_cast<const char*>(_fw.data()), _fw.size());
}
else
{
fail("Firmware Update failed - wrong path or permissions missing");
return;
}
LOG_INFO("Firmware Update for MIPI device done.");
fw_path_in_device.close();
_progress = 100;
_done = true;
// need to find a way to update the fw version field in the viewer
}
void firmware_update_manager::process_flow(
std::function<void()> cleanup,
invoker invoke)
{
// if device is D457, and fw is signed - using mipi specific procedure
if (!strcmp(_dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID), "ABCD") && _is_signed)
{
process_mipi();
return;
}
std::string serial = "";
if (_dev.supports(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID))
serial = _dev.get_info(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID);
else
serial = _dev.query_sensors().front().get_info(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID);
// Clear FW update related notification to avoid dismissing the notification on ~device_model()
// We want the notification alive during the whole process.
_model.related_notifications.erase(
std::remove_if( _model.related_notifications.begin(),
_model.related_notifications.end(),
[]( std::shared_ptr< notification_model > n ) {
return n->is< fw_update_notification_model >();
} ) , end(_model.related_notifications));
for (auto&& n : _model.related_notifications)
{
if (n->is< fw_update_notification_model >()
|| n->is< sw_recommended_update_alert_model >())
n->dismiss(false);
}
_progress = 5;
int next_progress = 10;
update_device dfu{};
if (auto upd = _dev.as<updatable>())
{
// checking firmware version compatibility with device
if (_is_signed)
{
if (!upd.check_firmware_compatibility(_fw))
{
std::stringstream ss;
ss << "The firmware version is not compatible with ";
ss << _dev.get_info(RS2_CAMERA_INFO_NAME) << std::endl;
fail(ss.str());
return;
}
}
log( "Trying to back-up camera flash memory" );
std::string log_backup_status;
try
{
auto flash = upd.create_flash_backup( [&]( const float progress )
{
_progress = ( ( ceil( progress * 5 ) / 5 ) * ( 30 - next_progress ) ) + next_progress;
} );
// Not all cameras supports this feature
if( !flash.empty() )
{
auto temp = rsutils::os::get_special_folder( rsutils::os::special_folder::app_data );
temp += serial + "." + get_timestamped_file_name() + ".bin";
{
std::ofstream file( temp.c_str(), std::ios::binary );
file.write( (const char*)flash.data(), flash.size() );
log_backup_status = "Backup completed and saved as '" + temp + "'";
}
}
else
{
log_backup_status = "Backup flash is not supported";
}
}
catch( const std::exception& e )
{
if( auto not_model_protected = get_protected_notification_model() )
{
log_backup_status = "WARNING: backup failed; continuing without it...";
not_model_protected->output.add_log( RS2_LOG_SEVERITY_WARN,
__FILE__,
__LINE__,
log_backup_status + ", Error: " + e.what() );
}
}
catch( ... )
{
if( auto not_model_protected = get_protected_notification_model() )
{
log_backup_status = "WARNING: backup failed; continuing without it...";
not_model_protected->add_log( log_backup_status + ", Unknown error occurred" );
}
}
if ( !log_backup_status.empty() )
log(log_backup_status);
next_progress = 40;
if (_is_signed)
{
log("Requesting to switch to recovery mode");
// in order to update device to DFU state, it will be disconnected then switches to DFU state
// if querying devices is called while device still switching to DFU state, an exception will be thrown
// to prevent that, a blocking is added to make sure device is updated before continue to next step of querying device
upd.enter_update_state();
if (!check_for([this, serial, &dfu]() {
auto devs = _ctx.query_devices();
for (uint32_t j = 0; j < devs.size(); j++)
{
try
{
auto d = devs[j];
if (d.is<update_device>())
{
if (d.supports(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID))
{
if (serial == d.get_info(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID))
{
log( "DFU device '" + serial + "' found" );
dfu = d;
return true;
}
}
}
}
catch (std::exception &e) {
if (auto not_model_protected = get_protected_notification_model())
{
not_model_protected->output.add_log(RS2_LOG_SEVERITY_WARN,
__FILE__,
__LINE__,
rsutils::string::from() << "Exception caught in FW Update process-flow: " << e.what() << "; Retrying...");
}
}
catch (...) {}
}
return false;
}, cleanup, std::chrono::seconds(60)))
{
fail("Recovery device did not connect in time!");
return;
}
}
}
else
{
dfu = _dev.as<update_device>();
}
if (dfu)
{
_progress = float(next_progress);
log("Recovery device connected, starting update..\n"
"Internal write is in progress\n"
"Please DO NOT DISCONNECT the camera");
dfu.update(_fw, [&](const float progress)
{
_progress = ((ceil(progress * 10) / 10 * (90 - next_progress)) + next_progress);
});
log( "Firmware Download completed, await DFU transition event" );
std::this_thread::sleep_for( std::chrono::seconds( 3 ) );
}
else
{
auto upd = _dev.as<updatable>();
upd.update_unsigned(_fw, [&](const float progress)
{
_progress = (ceil(progress * 10) / 10 * (90 - next_progress)) + next_progress;
});
log("Firmware Update completed, waiting for device to reconnect");
}
if (!check_for([this, serial]() {
auto devs = _ctx.query_devices();
for (uint32_t j = 0; j < devs.size(); j++)
{
try
{
auto d = devs[j];
if (d.query_sensors().size() && d.query_sensors().front().supports(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID))
{
auto s = d.query_sensors().front().get_info(RS2_CAMERA_INFO_FIRMWARE_UPDATE_ID);
if (s == serial)
{
log("Discovered connection of the original device");
return true;
}
}
}
catch (...) {}
}
return false;
}, cleanup, std::chrono::seconds(60)))
{
fail("Original device did not reconnect in time!");
return;
}
log( "Device reconnected successfully!\n"
"FW update process completed successfully" );
_progress = 100;
_done = true;
}
void fw_update_notification_model::draw_content(ux_window& win, int x, int y, float t, std::string& error_message)
{
using namespace std;
using namespace chrono;
ImGui::SetCursorScreenPos({ float(x + 9), float(y + 4) });
ImVec4 shadow{ 1.f, 1.f, 1.f, 0.1f };
ImGui::GetWindowDrawList()->AddRectFilled({ float(x), float(y) },
{ float(x + width), float(y + 25) }, ImColor(shadow));
if (update_state != RS2_FWU_STATE_COMPLETE)
{
ImGui::Text("Firmware Update Recommended!");
ImGui::SetCursorScreenPos({ float(x + 5), float(y + 27) });
draw_text(get_title().c_str(), x, y, height - 50);
ImGui::SetCursorScreenPos({ float(x + 9), float(y + height - 67) });
ImGui::PushStyleColor(ImGuiCol_Text, alpha(light_grey, 1.f - t));
if (update_state == RS2_FWU_STATE_INITIAL_PROMPT)
ImGui::Text("Firmware updates offer critical bug fixes and\nunlock new camera capabilities.");
else
ImGui::Text("Firmware update is underway...\nPlease do not disconnect the device");
ImGui::PopStyleColor();
}
else
{
//ImGui::PushStyleColor(ImGuiCol_Text, alpha(light_blue, 1.f - t));
ImGui::Text("Update Completed");
//ImGui::PopStyleColor();
ImGui::SetCursorScreenPos({ float(x + 10), float(y + 35) });
ImGui::PushFont(win.get_large_font());
std::string txt = rsutils::string::from() << textual_icons::throphy;
ImGui::Text("%s", txt.c_str());
ImGui::PopFont();
ImGui::SetCursorScreenPos({ float(x + 40), float(y + 35) });
ImGui::Text("Camera Firmware Updated Successfully");
}
ImGui::SetCursorScreenPos({ float(x + 5), float(y + height - 25) });
const auto bar_width = width - 115;
if (update_manager)
{
if (update_state == RS2_FWU_STATE_INITIAL_PROMPT)
{
auto sat = 1.f + sin(duration_cast<milliseconds>(system_clock::now() - created_time).count() / 700.f) * 0.1f;
ImGui::PushStyleColor(ImGuiCol_Button, saturate(sensor_header_light_blue, sat));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, saturate(sensor_header_light_blue, 1.5f));
std::string button_name = rsutils::string::from() << "Install" << "##fwupdate" << index;
if (ImGui::Button(button_name.c_str(), { float(bar_width), 20.f }) || update_manager->started())
{
// stopping stream before starting fw update
auto fw_update_manager = dynamic_cast<firmware_update_manager*>(update_manager.get());
if( ! fw_update_manager )
throw std::runtime_error( "Cannot convert firmware_update_manager" );
std::for_each(fw_update_manager->get_device_model().subdevices.begin(),
fw_update_manager->get_device_model().subdevices.end(),
[&](const std::shared_ptr<subdevice_model>& sm)
{
if (sm->streaming)
{
try
{
sm->stop(fw_update_manager->get_protected_notification_model());
}
catch (...)
{
// avoiding exception that can be sent by stop method
// this could happen if the sensor is not streaming and the stop method is called - for example
}
}
});
auto _this = shared_from_this();
auto invoke = [_this](std::function<void()> action) {
_this->invoke(action);
};
if (!update_manager->started())
update_manager->start(invoke);
update_state = RS2_FWU_STATE_IN_PROGRESS;
enable_dismiss = false;
_progress_bar.last_progress_time = system_clock::now();
}
ImGui::PopStyleColor(2);
if (ImGui::IsItemHovered())
{
ImGui::SetTooltip("%s", "New firmware will be flashed to the device");
}
}
else if (update_state == RS2_FWU_STATE_IN_PROGRESS)
{
if (update_manager->done())
{
update_state = RS2_FWU_STATE_COMPLETE;
pinned = false;
_progress_bar.last_progress_time = last_interacted = system_clock::now();
}
if (!expanded)
{
if (update_manager->failed())
{
update_manager->check_error(error_message);
update_state = RS2_FWU_STATE_FAILED;
pinned = false;
dismiss(false);
}
draw_progress_bar(win, bar_width);
ImGui::SetCursorScreenPos({ float(x + width - 105), float(y + height - 25) });
string id = rsutils::string::from() << "Expand" << "##" << index;
ImGui::PushStyleColor(ImGuiCol_Text, light_grey);
if (ImGui::Button(id.c_str(), { 100, 20 }))
{
expanded = true;
}
ImGui::PopStyleColor();
}
}
}
else
{
std::string button_name = rsutils::string::from() << "Learn More..." << "##" << index;
if (ImGui::Button(button_name.c_str(), { float(bar_width), 20 }))
{
open_url(recommended_fw_url);
}
if (ImGui::IsItemHovered())
{
win.link_hovered();
ImGui::SetTooltip("%s", "Internet connection required");
}
}
}
void fw_update_notification_model::draw_expanded(ux_window& win, std::string& error_message)
{
if (update_manager->started() && update_state == RS2_FWU_STATE_INITIAL_PROMPT)
update_state = RS2_FWU_STATE_IN_PROGRESS;
auto flags = ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoCollapse;
ImGui::PushStyleColor(ImGuiCol_WindowBg, { 0, 0, 0, 0 });
ImGui::PushStyleColor(ImGuiCol_Text, light_grey);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, white);
ImGui::PushStyleColor(ImGuiCol_PopupBg, sensor_bg);
ImGui::PushStyleVar(ImGuiStyleVar_WindowMinSize, ImVec2(500, 100));
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(5, 5));
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0);
std::string title = "Firmware Update";
if (update_manager->failed()) title += " Failed";
ImGui::OpenPopup(title.c_str());
if (ImGui::BeginPopupModal(title.c_str(), nullptr, flags))
{
ImGui::SetCursorPosX(200);
std::string progress_str = rsutils::string::from() << "Progress: " << update_manager->get_progress() << "%";
ImGui::Text("%s", progress_str.c_str());
ImGui::SetCursorPosX(5);
draw_progress_bar(win, 490);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, regular_blue);
auto s = update_manager->get_log();
ImGui::InputTextMultiline("##fw_update_log", const_cast<char*>(s.c_str()),
s.size() + 1, { 490,100 }, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_ReadOnly);
ImGui::PopStyleColor();
ImGui::SetCursorPosX(190);
if (visible || update_manager->done() || update_manager->failed())
{
if (ImGui::Button("OK", ImVec2(120, 0)))
{
if (update_manager->done() || update_manager->failed())
{
update_state = RS2_FWU_STATE_FAILED;
pinned = false;
dismiss(false);
}
expanded = false;
ImGui::CloseCurrentPopup();
}
}
else
{
ImGui::PushStyleColor(ImGuiCol_Button, transparent);
ImGui::PushStyleColor(ImGuiCol_ButtonActive, transparent);
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, transparent);
ImGui::PushStyleColor(ImGuiCol_Text, transparent);
ImGui::PushStyleColor(ImGuiCol_TextSelectedBg, transparent);
ImGui::Button("OK", ImVec2(120, 0));
ImGui::PopStyleColor(5);
}
ImGui::EndPopup();
}
ImGui::PopStyleVar(3);
ImGui::PopStyleColor(4);
error_message = "";
}
int fw_update_notification_model::calc_height()
{
if (update_state != RS2_FWU_STATE_COMPLETE) return 150;
else return 65;
}
void fw_update_notification_model::set_color_scheme(float t) const
{
notification_model::set_color_scheme(t);
ImGui::PopStyleColor(1);
ImVec4 c;
if (update_state == RS2_FWU_STATE_COMPLETE)
{
c = alpha(saturate(light_blue, 0.7f), 1 - t);
ImGui::PushStyleColor(ImGuiCol_WindowBg, c);
}
else
{
c = alpha(sensor_bg, 1 - t);
ImGui::PushStyleColor(ImGuiCol_WindowBg, c);
}
}
fw_update_notification_model::fw_update_notification_model(std::string name,
std::shared_ptr<firmware_update_manager> manager, bool exp)
: process_notification_model(manager)
{
enable_expand = false;
expanded = exp;
if (expanded) visible = false;
message = name;
this->severity = RS2_LOG_SEVERITY_INFO;
this->category = RS2_NOTIFICATION_CATEGORY_FIRMWARE_UPDATE_RECOMMENDED;
pinned = true;
forced = true;
}
}