Skip to content

Commit

Permalink
Fix binding table too large corrupting NVM3 on tokens restore
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerivec committed Dec 31, 2024
1 parent 15fbc2a commit 1ba07ef
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $ npm install -g ember-zli
$ ember-zli COMMAND
running command...
$ ember-zli (--version)
ember-zli/2.9.3 win32-x64 node-v22.11.0
ember-zli/2.9.4 win32-x64 node-v22.11.0
$ ember-zli --help [COMMAND]
USAGE
$ ember-zli COMMAND
Expand Down Expand Up @@ -61,7 +61,7 @@ EXAMPLES
$ ember-zli bootloader
```

_See code: [src/commands/bootloader/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.3/src/commands/bootloader/index.ts)_
_See code: [src/commands/bootloader/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.4/src/commands/bootloader/index.ts)_

## `ember-zli help [COMMAND]`

Expand Down Expand Up @@ -98,7 +98,7 @@ EXAMPLES
$ ember-zli monitor
```

_See code: [src/commands/monitor/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.3/src/commands/monitor/index.ts)_
_See code: [src/commands/monitor/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.4/src/commands/monitor/index.ts)_

## `ember-zli router`

Expand All @@ -115,7 +115,7 @@ EXAMPLES
$ ember-zli router
```

_See code: [src/commands/router/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.3/src/commands/router/index.ts)_
_See code: [src/commands/router/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.4/src/commands/router/index.ts)_

## `ember-zli sniff`

Expand All @@ -132,7 +132,7 @@ EXAMPLES
$ ember-zli sniff
```

_See code: [src/commands/sniff/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.3/src/commands/sniff/index.ts)_
_See code: [src/commands/sniff/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.4/src/commands/sniff/index.ts)_

## `ember-zli stack`

Expand All @@ -149,7 +149,7 @@ EXAMPLES
$ ember-zli stack
```

_See code: [src/commands/stack/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.3/src/commands/stack/index.ts)_
_See code: [src/commands/stack/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.4/src/commands/stack/index.ts)_

## `ember-zli utils`

Expand All @@ -166,7 +166,7 @@ EXAMPLES
$ ember-zli utils
```

_See code: [src/commands/utils/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.3/src/commands/utils/index.ts)_
_See code: [src/commands/utils/index.ts](https://github.com/Nerivec/ember-zli/blob/v2.9.4/src/commands/utils/index.ts)_

## `ember-zli version`

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ember-zli",
"description": "Interact with EmberZNet-based adapters using zigbee-herdsman 'ember' driver",
"version": "2.9.3",
"version": "2.9.4",
"author": "Nerivec",
"bin": {
"ember-zli": "bin/run.js"
Expand Down
42 changes: 41 additions & 1 deletion src/commands/stack/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,47 @@ export default class Stack extends Command {

private async menuTokensRestore(ezsp: Ezsp): Promise<boolean> {
const backupFile = await browseToFile('Tokens backup file location', DEFAULT_TOKENS_BACKUP_PATH)
const tokensBuf = Buffer.from(readFileSync(backupFile, 'utf8'), 'hex')
let tokensBuf = Buffer.from(readFileSync(backupFile, 'utf8'), 'hex')

{
// check for binding table corrupting NVM3 if size is too large (32 tested as "safe")
let bindingTableMod: undefined | { arraySizeOffset: number; clipStartOffset: number; clipEndOffset: number }
let readOffset: number = 0
const inTokenCount = tokensBuf.readUInt8(readOffset++)

for (let i = 0; i < inTokenCount; i++) {
const nvm3Key = tokensBuf.readUInt32LE(readOffset) // 4 bytes Token Key/Creator
readOffset += 4
const size = tokensBuf.readUInt8(readOffset++) // 1 byte token size
const arraySize = tokensBuf.readUInt8(readOffset++) // 1 byte array size.

if (nvm3Key === NVM3ObjectKey.STACK_BINDING_TABLE && arraySize > 32) {
logger.warning(`Binding table is too large, which is known to corrupt NVM3, keeping only first 32 entries.`)

bindingTableMod = {
arraySizeOffset: readOffset - 1,
clipStartOffset: readOffset + 32 * size,
clipEndOffset: readOffset + arraySize * size,
}
}

readOffset += arraySize * size
}

if (bindingTableMod) {
tokensBuf[bindingTableMod.arraySizeOffset] = 32

const tokensBufStart = tokensBuf.subarray(0, bindingTableMod.clipStartOffset)
const tokensBufEnd = tokensBuf.subarray(bindingTableMod.clipEndOffset)
tokensBuf = Buffer.concat([tokensBufStart, tokensBufEnd])
const saveFile = `${backupFile}-fixed.nvm3`

writeFileSync(saveFile, tokensBuf.toString('hex'), 'utf8')

logger.info(`Fixed tokens backup written to '${saveFile}'.`)
}
}

const status = await EmberTokensManager.restoreTokens(ezsp, tokensBuf)

if (status === SLStatus.OK) {
Expand Down

0 comments on commit 1ba07ef

Please sign in to comment.