particle-iot / particle-usb

A library for accessing Particle USB devices
Apache License 2.0
5 stars 1 forks source link

Feature/dfu impl #89

Closed keeramis closed 1 year ago

keeramis commented 1 year ago

Description

This pull request introduces new functionality to enable firmware download to a module using the Device Firmware Update (DFU) mechanism. The flashWithDfu method is designed to enable the flashing of any USB device in DFU mode.

To utilize this functionality, users are required to provide the following required arguments:

How to test

Flash system parts and user parts

  1. Checkout this branch
  2. Point your test script to the folder path of this branch
    
    const usb = require('/path/to/local/particle-usb');
    const fs = require('fs-extra');
    const { HalModuleParser } = require('binary-version-reader');

async function main() { const devices = await usb.getDevices(); if (devices.length === 0) { throw new Error('No devices found'); } const device = devices[0]; console.log('Device : ', device); await device.open(); await device.enterDfuMode();

const buf = await fs.readFile('/path/to/binaries/5.4.0/p2/p2-system-part1@5.4.0.bin'); const reader = new HalModuleParser(); const parsed = await reader.parseBuffer({ fileBuffer: buf }); const addr = parseInt(parsed.prefixInfo.moduleStartAddy, 16); await device.writeOverDfu(0, parsed.fileBuffer, addr, {}); // the 0 is obtained from particle-cli

const bufTinker = await fs.readFile('/path/to/binaries/5.4.0/p2/p2-tinker@5.4.0.bin'); const parsedTinker = await reader.parseBuffer({ fileBuffer: bufTinker }); const addrTinker = parseInt(parsedTinker.prefixInfo.moduleStartAddy, 16); await device.writeOverDfu(0, parsedTinker.fileBuffer, addrTinker, { doManifestation: true }); }

main().catch((error) => { console.error('Error:', error.message); });


### Flash DCT to download device keys, for example
1. First obtain keys for the given device using `particle keys new deviceID`
2. The key is saved in a local folder. Use that folder path to read the data with binary encoding

const usb = require('/path/to/local/particle-usb'); const fs = require('fs-extra'); const { HalModuleParser } = require('binary-version-reader');

async function main() { const devices = await usb.getDevices(); if (devices.length === 0) { throw new Error('No devices found'); } const device = devices[0]; console.log('Device : ', device); await device.open(); await device.enterDfuMode();

const keysStr = await fs.readFile('/path/to/module/key', 'binary'); const keysBuf = Buffer.from(keysStr, 'binary'); await device.flashWithDfu(1, keysBuf, 3106, { doManifestation: true }); }

main().catch((error) => { console.error('Error:', error.message); });

keeramis commented 1 year ago

Just a quick update regarding the progress on the DFU upload functionality. I've encountered some challenges when implementing the upload feature on nodeusb perhaps/apparently due to differences in how webdfu and nodeusb handle the upload process. It might take some time to look at it so to avoid delaying the release of the current functionality, I'll proceed with creating this PR without the upload feature. Rest assured, I'll continue working on the upload functionality in a separate PR, but for the upcoming RC, it's not an essential requirement.

hugomontero commented 1 year ago

Tested with argon: I'm getting an sos issue after flashing system-part and user-part I'll try again tomorrow. I'm assuming my implementation is broken hehe

Update: It was my device hehe 😅