Skip to content

Commit 2954da2

Browse files
committed
ccg3: Update only a single firmware image
We don't actually need to update both firmware images. It'll stay on the one we updated. So it's totally fine to keep the other one on the older version. Signed-off-by: Daniel Schaefer <[email protected]>
1 parent 70438f6 commit 2954da2

File tree

1 file changed

+73
-73
lines changed

1 file changed

+73
-73
lines changed

framework_lib/src/ccgx/hid.rs

Lines changed: 73 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use hidapi::{DeviceInfo, HidApi, HidDevice};
1+
use hidapi::{DeviceInfo, HidApi, HidDevice, HidError};
22

33
use crate::ccgx;
44
use crate::ccgx::device::{decode_flash_row_size, FwMode};
@@ -155,25 +155,6 @@ pub fn check_ccg_fw_version(device: &HidDevice) {
155155
print_fw_info(&info);
156156
}
157157

158-
// 0 .. 2 = HID header
159-
// 2 .. 4 = signature (CY)
160-
// 4 = Operating Mode
161-
// 5 = Bootloader (security, no-flashing, priority, row_size)
162-
// 6 = Boot mode reason, jump-bootloader, reserved, fw1 invalid, fw2 invalid
163-
// 7 = ??
164-
// 8 .. 12 = Silicon ID
165-
// 12 .. 16 = bootloader Version
166-
// 15 .. 20 = ??
167-
// 20 .. 24 = Image 1 Version
168-
// 24 .. 26 = Image 1 ??
169-
// 26 .. 28 = Image 1 ??
170-
// 28 .. 32 = Image 2 Version
171-
// 32 .. 34 = Image 2 ??
172-
// 34 .. 36 = Image 2 ??
173-
// 36 .. 40 = Image 1 Start Address
174-
// 40 .. 44 = Image 2 Start Address
175-
// 44 .. 52 = ?? [c9 d7 3e 02 23 19 0b 00]
176-
177158
fn decode_fw_info(buf: &[u8]) -> HidFirmwareInfo {
178159
let info_len = std::mem::size_of::<HidFirmwareInfo>();
179160
let info: HidFirmwareInfo = unsafe { std::ptr::read(buf[..info_len].as_ptr() as *const _) };
@@ -191,77 +172,84 @@ fn decode_fw_info(buf: &[u8]) -> HidFirmwareInfo {
191172
fn print_fw_info(info: &HidFirmwareInfo) {
192173
assert_eq!(info.report_id, ReportIdCmd::E0Read as u8);
193174

194-
debug!(" Signature: {:X?}", info.signature);
175+
info!(" Signature: {:X?}", info.signature);
195176
// Something's totally off if the signature is invalid
196-
assert_eq!(info.signature, [b'C', b'Y']);
177+
if info.signature != [b'C', b'Y'] {
178+
println!("Firmware Signature is invalid.");
179+
return;
180+
}
197181

198-
debug!(
199-
" Operating Mode: {:?} ({})",
200-
FwMode::try_from(info.operating_mode).unwrap(),
201-
info.operating_mode
202-
);
203-
debug!(" Bootloader Info");
204-
debug!(
182+
info!(" Bootloader Info");
183+
info!(
205184
" Security Support: {:?}",
206185
info.bootloader_info & 0b001 != 0
207186
);
208-
debug!(
187+
info!(
209188
" Flashing Support: {:?}",
210189
info.bootloader_info & 0b010 == 0
211190
);
212-
debug!(
213-
" App Priority: {:?}",
214-
info.bootloader_info & 0b100 != 0
215-
);
216-
debug!(
217-
" Flash Row Size: {:?}",
191+
// App Priority means you can configure whether the main or backup firmware
192+
// has priority. This can either be configured in the flash image or by
193+
// sending a command. But the CCG3 SDK lets you disable support for this at
194+
// compile-time. If disabled, both images have the same priority.
195+
let app_priority_support = info.bootloader_info & 0b100 != 0;
196+
info!(" App Priority: {:?}", app_priority_support);
197+
info!(
198+
" Flash Row Size: {:?} B",
218199
decode_flash_row_size(info.bootloader_info)
219200
);
220-
debug!(" Boot Mode Reason");
221-
debug!(
201+
info!(" Boot Mode Reason");
202+
info!(
222203
" Jump to Bootloader: {:?}",
223204
info.bootmode_reason & 0b000001 != 0
224205
);
225206
let image_1_valid = info.bootmode_reason & 0b000100 == 0;
226207
let image_2_valid = info.bootmode_reason & 0b001000 == 0;
227-
debug!(" FW 1 valid: {:?}", image_1_valid);
228-
debug!(" FW 2 valid: {:?}", image_2_valid);
229-
debug!(
230-
" App Priority: {:?}",
231-
info.bootmode_reason & 0b110000
232-
);
233-
debug!(" UID: {:X?}", info.device_uid);
234-
debug!(" Silicon ID: {:X?}", info.silicon_id);
208+
info!(" FW 1 valid: {:?}", image_1_valid);
209+
info!(" FW 2 valid: {:?}", image_2_valid);
210+
if app_priority_support {
211+
info!(
212+
" App Priority: {:?}",
213+
info.bootmode_reason & 0b110000
214+
);
215+
}
216+
info!(" UID: {:X?}", info.device_uid);
217+
info!(" Silicon ID: {:X?}", info.silicon_id);
235218
let bl_ver = BaseVersion::from(info.bl_version.as_slice());
236219
let base_version_1 = BaseVersion::from(info.image_1_ver.as_slice());
237220
let base_version_2 = BaseVersion::from(info.image_2_ver.as_slice());
238-
debug!(
239-
" BL Version: {} Build {}",
240-
bl_ver, bl_ver.build_number
241-
);
242-
debug!(
221+
info!(" BL Version: {} ", bl_ver,);
222+
info!(
243223
" Image 1 start: 0x{:08X}",
244224
u32::from_le_bytes(info.image_1_row)
245225
);
246-
debug!(
226+
info!(
247227
" Image 2 start: 0x{:08X}",
248228
u32::from_le_bytes(info.image_2_row)
249229
);
250230

231+
let operating_mode = FwMode::try_from(info.operating_mode).unwrap();
232+
let (active_ver, active_valid, inactive_ver, inactive_valid) = match operating_mode {
233+
FwMode::MainFw | FwMode::BootLoader => {
234+
(base_version_2, image_2_valid, base_version_1, image_1_valid)
235+
}
236+
FwMode::BackupFw => (base_version_1, image_1_valid, base_version_2, image_2_valid),
237+
};
238+
251239
println!(
252-
" FW Image 1 Version: {:03} ({}){}",
253-
base_version_1.build_number,
254-
base_version_1,
255-
if image_1_valid { "" } else { " - INVALID!" }
240+
" Active Firmware: {:03} ({}){}",
241+
active_ver.build_number,
242+
active_ver,
243+
if active_valid { "" } else { " - INVALID!" }
256244
);
257245
println!(
258-
" FW Image 2 Version: {:03} ({}){}",
259-
base_version_2.build_number,
260-
base_version_2,
261-
if image_2_valid { "" } else { " - INVALID!" }
246+
" Inactive Firmware: {:03} ({}){}",
247+
inactive_ver.build_number,
248+
inactive_ver,
249+
if inactive_valid { "" } else { " - INVALID!" }
262250
);
263251
println!(
264-
" Currently running: {:?} ({})",
252+
" Operating Mode: {:?} (#{})",
265253
FwMode::try_from(info.operating_mode).unwrap(),
266254
info.operating_mode
267255
);
@@ -361,21 +349,25 @@ pub fn flash_firmware(fw_binary: &[u8]) {
361349
println!(" Updating Firmware Image 1");
362350
flash_firmware_image(&device, fw_binary, FW1_START, FW1_METADATA, fw1_rows, 1);
363351

364-
let (device, _) =
365-
wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
352+
// We don't actually need to update both firmware images.
353+
// It'll stay on the one we updated. So it's totally fine to
354+
// keep the other one on the older version.
355+
//let (device, _) =
356+
// wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
366357

367-
println!(" Updating Firmware Image 2");
368-
flash_firmware_image(&device, fw_binary, FW2_START, FW2_METADATA, fw2_rows, 2);
358+
//println!(" Updating Firmware Image 2");
359+
//flash_firmware_image(&device, fw_binary, FW2_START, FW2_METADATA, fw2_rows, 2);
369360
}
370361
1 => {
371362
println!(" Updating Firmware Image 2");
372363
flash_firmware_image(&device, fw_binary, FW2_START, FW2_METADATA, fw2_rows, 2);
373364

374-
let (device, _) =
375-
wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
365+
// See above
366+
//let (device, _) =
367+
// wait_to_reappear(&mut api, &filter_devs, sn).expect("Device did not reappear");
376368

377-
println!(" Updating Firmware Image 1");
378-
flash_firmware_image(&device, fw_binary, FW1_START, FW1_METADATA, fw1_rows, 1);
369+
//println!(" Updating Firmware Image 1");
370+
//flash_firmware_image(&device, fw_binary, FW1_START, FW1_METADATA, fw1_rows, 1);
379371
}
380372
_ => unreachable!(),
381373
}
@@ -414,13 +406,21 @@ fn flash_firmware_image(
414406
row
415407
);
416408
}
417-
write_row(device, (start_row + row_no) as u16, row);
409+
write_row(device, (start_row + row_no) as u16, row).unwrap_or_else(|err| {
410+
panic!(
411+
"Failed to write firmware row #{} (@{:X}): {:?}",
412+
row_no,
413+
start_row + row_no,
414+
err
415+
)
416+
});
418417
}
419418
info!(
420419
"Writing metadata row@{:X?}: {:X?}",
421420
metadata_row, metadata_slice
422421
);
423-
write_row(device, metadata_row as u16, metadata_slice);
422+
write_row(device, metadata_row as u16, metadata_slice)
423+
.expect("Failed to write firmware metadata");
424424

425425
// Not quite sure what this is. But on the first update it has
426426
// 0x01 and on the second it has 0x02. So I think this switches the boot order?
@@ -454,7 +454,7 @@ fn flash_firmware_image(
454454
.unwrap();
455455
}
456456

457-
fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) {
457+
fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) -> Result<usize, HidError> {
458458
let row_no_bytes = row_no.to_le_bytes();
459459
trace!("Writing row {:04X}. Data: {:X?}", row_no, row);
460460

@@ -476,7 +476,7 @@ fn write_row(device: &HidDevice, row_no: u16, row: &[u8]) {
476476
buffer[2] = row_no_bytes[0];
477477
buffer[3] = row_no_bytes[1];
478478
buffer[4..].copy_from_slice(row);
479-
device.write(&buffer).unwrap();
479+
device.write(&buffer)
480480
}
481481

482482
/// Wait for the specific card to reappear

0 commit comments

Comments
 (0)