Detalhes do pacote

blockmap

balena-io-modules17.6kMIT4.0.3

Tizen's block map format

tizen, bmap, block, map

readme (leia-me)

Blockmap

npm npm license npm downloads

This module implements Tizen's block map format, which maps non-empty blocks or block ranges from a raw image file, making it possible to quickly & efficiently flash the image to the target block device by only reading & writing the necessary blocks.

Install via npm

$ npm install --save blockmap

Usage

For detailed API documentation, see /doc.

const { BlockMap } = require('blockmap')

Parsing a Block Map

const blockMap = BlockMap.parse(xml)
BlockMap {
  version: '2.0',
  imageSize: 821752,
  blockSize: 4096,
  blocksCount: 201,
  mappedBlocksCount: 117,
  checksum: '44e9d58de533d5eb94f8232cff22b2e6d71b15d369c2ac2af461c63164cce324',
  checksumType: 'sha256',
  ranges: [{
    checksum: '9eaf19215d55d23de1be1fe4bed4a95bfe620a404352fd06e782738fff58e500',
    start: 0,
    end: 1
  }, {
    checksum: 'e8a26f49a71262870f8294a73f40f122d622fd70fb82bef01c0322785e9fd6b2',
    start: 3,
    end: 5
  },
  // More ranges omitted for brevity
  {
    checksum: 'cb732fc3f3a0f81f6a761a534201c05549c8efe4a92630ccd24241f72d7d618c',
    start: 198,
    end: 199
  }]
}

Creating a Block Map

Render a .bmap file from a parsed or otherwise constructed BlockMap:

const blockMap = BlockMap.parse(value)
const xml = blockMap.toString()

Where xml would look like the following, given the block map from above:

<?xml version="1.0" encoding="UTF-8"?>
<bmap version="2.0">
  <ImageSize>821752</ImageSize>
  <BlockSize>4096</BlockSize>
  <BlocksCount>201</BlocksCount>
  <MappedBlocksCount>117</MappedBlocksCount>
  <ChecksumType>sha256</ChecksumType>
  <BmapFileChecksum>44e9d58de533d5eb94f8232cff22b2e6d71b15d369c2ac2af461c63164cce324</BmapFileChecksum>
  <BlockMap>
    <Range chksum="9eaf19215d55d23de1be1fe4bed4a95bfe620a404352fd06e782738fff58e500">0-1</Range>
    <Range chksum="e8a26f49a71262870f8294a73f40f122d622fd70fb82bef01c0322785e9fd6b2">3-5</Range>
    <!-- More ranges omitted for brevity -->
    <Range chksum="cb732fc3f3a0f81f6a761a534201c05549c8efe4a92630ccd24241f72d7d618c">198-199</Range>
  </BlockMap>
</bmap>

NOTE: Regardless of input version, blockMap.toString() will always create a .bmap in the format of the latest version (currently 2.0).


Block Map Checksum Verification

By default, checksums for mapped ranges and the bmap file itself (only version 1.3+) will be verified when parsing or streaming. If you need to disable verification, pass false as verify parameter.

// Disable verification of the bmap file checksum:
const blockMap = BlockMap.parse(bmap, false)
const { ReadStream } = require('blockmap')
// Disable range checksum verification:
const blockReadStream = new ReadStream(fileDescriptor, blockMap, false)
const { FilterStream } = require('blockmap')
// Same for filter streams:
const filterStream = new FilterStream(blockMap, false)

Reading Mapped Blocks


NOTE: These examples just use fs.writeSync() in .on('readable') for brevity; of course this should be implemented properly in a writable stream, which the readable side (i.e. the ReadStream or FilterStream) is piped to.


Use a parsed block map to read only mapped regions:

const blockMap = BlockMap.parse(fs.readFileSync('/path/to/balena-os.bmap'))
const blockReadStream = new ReadStream(fileDescriptor, blockMap)

// The chunk emitted will have two properties set;
// 1) chunk.buffer – the data buffer
// 2) chunk.position – the chunk's offset (or address) in bytes
// Which can then be used to write only those blocks to the target:
blockReadStream.on('readable', function() {
  len chunk = null
  while(chunk = this.read()) {
    fs.writeSync(fd, chunk.buffer, 0, chunk.buffer.length, chunk.position)
  }
})

blockReadStream.once('end', function() {
  console.log('Read', blockReadStream.blocksRead, 'mapped blocks')
  console.log('Read', blockReadStream.bytesRead, 'mapped bytes')
  console.log('Read', blockReadStream.rangesRead, 'mapped ranges')
})

Filtering Unmapped Blocks

Use a filter transform to filter out unmapped blocks from a stream:

const blockMap = BlockMap.parse(fs.readFileSync('/path/to/balena-os.bmap'))
const readStream = fs.createReadStream('/path/to/balena-os.img')
const filterStream = new FilterStream(blockMap)

// The chunk emitted will have two properties set;
// 1) chunk.buffer – the data buffer
// 2) chunk.position – the chunk's offset (or address) in bytes
// Which can then be used to write only those blocks to the target:
filterStream.on('readable', function() {
  let buffer = null
  while(chunk = this.read()) {
    fs.writeSync(fd, chunk.buffer, 0, chunk.buffer.length, chunk.position)
  }
})

// Pipe the readable stream into the block filter:
readStream.pipe(filterStream)

Verifying a Flashed Device

Use a ReadStream to verify a flashed device image:

const { ReadStream } = require('blockmap');

function verify(fileDescriptor, blockMap, callback) {
  new ReadStream(fileDescriptor, blockMap).resume()
    .once('error', callback)
    .once('end', callback)
}

const blockMap = BlockMap.parse(fs.readFileSync('/path/to/balena-os.bmap'))

verify(fileDescriptor, blockMap, function(error) {
  if(error != null) {
    // The image didn't verify...
  }
})

Handling Errors

Parsing

BlockMap.parse() and blockMap.parse() will throw when encountering invalid input, or if the checksum doesn't verify:

try {
  blockMap = BlockMap.parse(value)
} catch(error) {
  // ...
}

Streams

If the error is due to a checksum mismatch, the error will have a .checksum and .range property, denoting the calculated checksum, and the range for which it occured:

const blockReadStream = new ReadStream(fileDescriptor, blockMap)

blockReadStream.on('error', function(error) {
  if(error.checksum) {
    console.log(`Checksum mismatch for range [${error.range.start},${error.range.end}]:`)
    console.log(`${error.checksum} != ${error.range.checksum}`)
  }
  // ...
})

References

changelog (log de mudanças)

Change Log

All notable changes to this project will be documented in this file automatically by Versionist. DO NOT EDIT THIS FILE MANUALLY! This project adheres to Semantic Versioning.

v4.0.3

(2020-11-30)

  • Update typescript to v4.1.2 [Alexis Svinartchouk]
  • Add versionbot changelog [Alexis Svinartchouk]

v4.0.2

(2020-07-14)

  • Update generated docs [Alexis Svinartchouk]
  • Use tslib [Alexis Svinartchouk]
  • Use strict typescript flag [Alexis Svinartchouk]
  • Update dependencies [Alexis Svinartchouk]
  • Update resin-lint -> balena-lint [Alexis Svinartchouk]

v4.0.1

(2020-03-31)

  • Update generated docs [Alexis Svinartchouk]
  • Update resin-lint [Alexis Svinartchouk]
  • Transform._flush is public [Alexis Svinartchouk]

4.0.0 - 2019-04-04

  • Remove travis and appveyor configs [Alexis Svinartchouk]
  • Don't test on node 4 [Alexis Svinartchouk]
  • Replace jsdoc with typedoc, rename resin -> balena [Alexis Svinartchouk]
  • Change licence MIT -> Apache [Alexis Svinartchouk]
  • Add licence in each ts file [Alexis Svinartchouk]
  • Convert to typescript [Alexis Svinartchouk]

v3.4.3 - 2018-06-01

  • Upgrade(package): Bump dev dependencies #53 [Jonas Hermsmeier]

v3.4.2 - 2018-06-01

  • Fix(read-stream): Read the last range entirely in ReadStream #52 [Alexis Svinartchouk]
  • Test(read-stream) Add a test case with the last block larger than chunk size #52 [Alexis Svinartchouk]
  • Fix(test) Fix typo in ReadStream tests #52 [Alexis Svinartchouk]

v3.4.1 - 2018-05-30

  • Test(ci): Add Node v10 to CI test matrix #50 [Jonas Hermsmeier]

v3.4.0 - 2018-05-30

  • Feat(read-stream): Add a generateChecksums option #51 [Alexis Svinartchouk]

v3.3.0 - 2018-05-30

  • Fix(read-stream): Verify on end #49 [Jonas Hermsmeier]
  • Feat(filter-stream): Add .rangesVerified counter #49 [Jonas Hermsmeier]
  • Feat(read-stream): Add .rangesVerified counter #49 [Jonas Hermsmeier]

v3.2.1 - 2018-05-30

  • Fix(test): Fix ERR_AMBIGUOUS_ARGUMENT in assert with Node 10 #48 [Jonas Hermsmeier]

v3.2.0 - 2018-05-29

  • Feat(filter-stream): Add a generateChecksums option #47 [Alexis Svinartchouk]

v3.1.0 - 2018-05-29

  • Feat(read-stream): Accept a custom fs implementation in ReadStream constructor options. #46 [Alexis Svinartchouk]

v3.0.0 - 2018-02-23

  • Doc(README): Update examples with new chunk format #41 [Jonas Hermsmeier]
  • Feat(blockmap): Move ReadStream.ReadStream -> BlockMap.ReadStream #41 [Jonas Hermsmeier]
  • Feat(read-stream): Make ReadStream emit BlockMap.Chunk #41 [Jonas Hermsmeier]
  • Feat(lib): Add BlockMap.Range & ReadStream.ReadRange #41 [Jonas Hermsmeier]
  • Test(filter-stream): Remove checks for block.address #41 [Jonas Hermsmeier]
  • Feat(filter-stream): Emit Chunk objects instead of Buffers #41 [Jonas Hermsmeier]

v2.0.5 - 2018-02-20

  • Test(filter-stream): Add assertions for bytes read #44 [Jonas Hermsmeier]

v2.0.4 - 2018-02-16

  • Chore(package): Update dependencies #42 [Jonas Hermsmeier]

v2.0.3 - 2018-02-15

  • Doc(README): Add reference links #43 [Jonas Hermsmeier]

v2.0.2 - 2017-11-03

  • Upgrade(package): Bump dependencies #39 [Jonas Hermsmeier]

v2.0.1 - 2017-08-04

  • fix(package): Fix nanobench not being a dev dependency
  • chore(package): Update mocha 3.4.2 -> 3.5.0

v2.0.0 - 2017-06-02

  • refactor(filter-stream): Optimize performance
  • feat(read-stream): Refactor & optimize BlockMap.ReadStream
    • BREAKING: Removed .blockInRange property
    • BREAKING: Rewrote open(), close() and destroy() to implement the same behavior as fs.ReadStream
    • BREAKING: start & end options will now cause only partially covered regions to NOT be read
    • BREAKING: Removed .options, add .verify instead
    • feat: Added .closed & .destroyed properties
    • feat: Added .highWaterMark getter/setter
    • feat: Support autoClose option
  • chore(package): Add benchmarks
  • chore(package): Update mocha 3.2.0 -> 3.4.2

v1.2.0 - 2017-03-08

  • fix(filter-stream): Prevent emitting too small blocks

v1.1.0 - 2017-03-02

  • fix(read-stream): Throw if [start,end] are OOB
  • feat(blockmap): Add .createFilterStream()
  • feat(readstream): Add fd option