Skip to content

Commit

Permalink
Minimum device os version to downgrade on Protected Devices is 6.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
keeramis committed Aug 26, 2024
1 parent 9dfe271 commit 24e0961
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 49 deletions.
42 changes: 3 additions & 39 deletions src/lib/flash-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,9 @@ const ensureError = utilities.ensureError;
const FLASH_TIMEOUT = 4 * 60000;

// Minimum version of Device OS that supports protected modules
const PROTECTED_MINIMUM_VERSION = '6.0.0';
const PROTECTED_MINIMUM_SYSTEM_VERSION = 6000;
const PROTECTED_MINIMUM_BOOTLOADER_VERSION = 3000;

// If a Protected Device has a system version of 6101, it can't be downgraded to 6100 or 6000
const INCOMPATIBLE_PROTECTED_SYSTEM_VERSIONS = {
6101: [6100, 6000],
6100: [6000],
};

// If a Protected Device has a bootloader version of 3001, it can't be downgraded to 3000
const INCOMPATIBLE_PROTECTED_BOOTLOADER_VERSIONS = {
3001: [3000]
};
const PROTECTED_MINIMUM_VERSION = '6.1.1';
const PROTECTED_MINIMUM_SYSTEM_VERSION = 6101;
const PROTECTED_MINIMUM_BOOTLOADER_VERSION = 3001;

async function flashFiles({ device, flashSteps, resetAfterFlash = true, ui, verbose=true }) {
let progress = null;
Expand Down Expand Up @@ -394,31 +383,6 @@ async function maintainDeviceProtection({ modules, device }) {
throw new Error(`Cannot downgrade Device OS below version ${PROTECTED_MINIMUM_VERSION} on a Protected Device`);
}

// Check for compatibility with current Device OS version for Protected Devices
// Firmware module info is only available when the device is not in DFU mode
// If the device was in DFU mode, allow the flashing to continue and fail midway if the device is not compatible
if (!device.isInDfuMode) {
let currentModules, incompatSystem, incompatBootloader, currentSystem, currentBootloader;
try {
currentModules = await device.getFirmwareModuleInfo({ timeout: 5000 });
currentSystem = currentModules.find(m => m.type === 'SYSTEM_PART');
currentBootloader = currentModules.find(m => m.type === 'BOOTLOADER' && m.index === 0);
incompatSystem = currentSystem && INCOMPATIBLE_PROTECTED_SYSTEM_VERSIONS[currentSystem.version] &&
INCOMPATIBLE_PROTECTED_SYSTEM_VERSIONS[currentSystem.version].includes(moduleVersion);
incompatBootloader = currentBootloader && INCOMPATIBLE_PROTECTED_BOOTLOADER_VERSIONS[currentBootloader.version] &&
INCOMPATIBLE_PROTECTED_BOOTLOADER_VERSIONS[currentBootloader.version].includes(moduleVersion);
} catch (error) {
// ignore error, as it may simply mean the module info couldn't be retrieved
}

if (incompatSystem) {
throw new Error(`Device OS system version ${currentSystem.version} cannot be downgraded to ${moduleVersion} on a Protected Device. Please refer to https://docs.particle.io/ for more details.`);
}
if (incompatBootloader) {
throw new Error(`Device OS bootloader version ${currentBootloader.version} cannot be downgraded to ${moduleVersion} on a Protected Device. Please refer to https://docs.particle.io/ for more details.`);
}
}

// Enable Device Protection on the bootloader when flashing a Protected Device
if (moduleFunction === ModuleInfo.FunctionType.BOOTLOADER && moduleIndex === 0) {
const protectedBuffer = await createProtectedModule(module.fileBuffer);
Expand Down
15 changes: 5 additions & 10 deletions src/lib/flash-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,12 +674,7 @@ describe('flash-helper', () => {

beforeEach(async () => {
device = {
getProtectionState: sinon.stub(),
isInDfuMode: false,
getFirmwareModuleInfo: sinon.stub().resolves([
{ type: 'BOOTLOADER', index: 0, version: 3000 },
{ type: 'SYSTEM_PART', index: 1, version: 6000 },
])
getProtectionState: sinon.stub()
};

const oldBootloaderBuffer = await firmwareTestHelper.createFirmwareBinary({
Expand All @@ -704,13 +699,13 @@ describe('flash-helper', () => {
moduleFunction: ModuleInfo.FunctionType.BOOTLOADER,
platformId: 12,
moduleIndex: 0,
moduleVersion: 3000
moduleVersion: 3001
});
const newSystemPartBuffer = await firmwareTestHelper.createFirmwareBinary({
moduleFunction: ModuleInfo.FunctionType.SYSTEM_PART,
platformId: 12,
moduleIndex: 0,
moduleVersion: 6000
moduleVersion: 6101
});
newBootloader = await new HalModuleParser().parseBuffer({ fileBuffer: newBootloaderBuffer });
newSystemPart = await new HalModuleParser().parseBuffer({ fileBuffer: newSystemPartBuffer });
Expand Down Expand Up @@ -762,7 +757,7 @@ describe('flash-helper', () => {
} catch (_error) {
error = _error;
}
expect(error).to.have.property('message').that.eql('Cannot downgrade Device OS below version 6.0.0 on a Protected Device');
expect(error).to.have.property('message').that.eql('Cannot downgrade Device OS below version 6.1.1 on a Protected Device');
});

it('throws an exception if the system part is too old', async () => {
Expand All @@ -772,7 +767,7 @@ describe('flash-helper', () => {
} catch (_error) {
error = _error;
}
expect(error).to.have.property('message').that.eql('Cannot downgrade Device OS below version 6.0.0 on a Protected Device');
expect(error).to.have.property('message').that.eql('Cannot downgrade Device OS below version 6.1.1 on a Protected Device');
});

it('does does not reject new modules', async () => {
Expand Down

0 comments on commit 24e0961

Please sign in to comment.