...modify a binary file as using CrossScript
Often you need to resize or modify a binary file output from a build as part of your manufacturing process. This example demonstrates how you can do this using CrossWork's CrossScript tool.
To resize a binary file:
- First, make sure you are generating a binary file by setting the Linker > Additional Output Format project property of your executable project to bin.
- Add a JavaScript file to your project called postlink.js containing the following code:
function postlink(filename) {
var size = 1024 * 16;
var pad = 0xFF;
BinaryFile.load(filename);
BinaryFile.resize(size, pad);
BinaryFile.save(filename);
} - Set the User Build Step Options > Post-Link Command project property of your executable project to:
"$(StudioDir)/bin/crossscript" -load "$(ProjectDir)/postlink.js" "postlink(\"$(OutDir)/$(ProjectName).bin\");"
Note that this example assumes the default filename for a binary file, you may need to change the value passed to the postlink function to point to your binary file if this has been changed.
If rather than resizing to a fixed size you want to align the size of the binary file (which you might want to do to make it match your Flash geometry) you could modify the resizeArray call as follows:
var alignment = 1024 * 4;
BinaryFile.resize(BinaryFile.length() + (alignment - (BinaryFile.length() % alignment)), pad);
You can also modify the data, so if for example you want to calculate a CRC and write it to the last word, you could modify the postlink function as follows:
function postlink(filename)
{
var size = 1024 * 16;
var pad = 0xFF;
BinaryFile.load(filename);
BinaryFile.resize(size, pad);
var crc32 = BinaryFile.crc32(0, size - 4);
WScript.Echo("crc32 = 0x" + crc32.toString(16) + "\n");
BinaryFile.pokeUint32(size - 4, crc32, true);
BinaryFile.save(filename);
}
CrossScript doesn't necessarily need to be run as part of a build, it can be run interactively or as part of your own manufacturing process. For example, you might want to do something like embed a serial number and include that in the CRC calculation, you could do this with the following script:
function postlink(inputFilename, outputFilename, serialNumber)
{
var size = 1024 * 16;
var pad = 0xFF;
BinaryFile.load(inputFilename);
BinaryFile.resize(size, pad);
BinaryFile.pokeUint32(size - 8, serialNumber, true);
var crc32 = BinaryFile.crc32(0, size - 4);
WScript.Echo("crc32 = 0x" + crc32.toString(16) + "\n");
BinaryFile.pokeUint32(size - 4, crc32, true);
BinaryFile.save(outputFilename);
}
This script could then be invoked from the command line or another tool as follows:
crossscript -load postlink.js "postlink('in.bin', 'out.bin', 0x12345678);"Please sign in to leave a comment.
Comments
0 comments