Skip to content

Commit

Permalink
Puck.js: Allow flash memory protection to be overwridden with E.setFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Nov 27, 2017
1 parent e9cb718 commit 5fedb5c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Expand Up @@ -35,6 +35,7 @@
nRF5x: jsvArrayPushAll memory leak fixes NRF.findDevices memory leak when services present
jsvNewIterator now has an option to iterate over sparse arrays as if they weren't sparse
Fixed some built-in functions that misbehaved when given sparse arrays
Puck.js: Allow flash memory protection to be overwridden with E.setFlags

1v94 : Allow Espruino boards to reset straight out of the DFU Bootloader
Improvements in handling errors in code running in IRQs
Expand Down
49 changes: 49 additions & 0 deletions scripts/hextojs.js
@@ -0,0 +1,49 @@
#!/usr/bin/nodejs
/* This is a big hack - we take a hex file and turn it into
Espruino commands that write the memory directly via JS. Can
be used to do bootloader updates/etc.
ASSUMES 4K PAGES
*/

var PAGESIZE = 4096;

if (process.argv.length!=3) {
console.error("USAGE: hextojs.js inputfile.hex");
process.exit(1);
}

var inputFile = process.argv[2];

var hex = require("fs").readFileSync(inputFile).toString().split("\n");
var addrHi = 0;
function parseLines(dataCallback) {
hex.forEach(function(hexline) {
var cmd = hexline.substr(1,2);
if (cmd=="02") {
var subcmd = hexline.substr(7,2);
if (subcmd=="02") addrHi = parseInt(hexline.substr(9,4),16) << 4; // Extended Segment Address
if (subcmd=="04") addrHi = parseInt(hexline.substr(9,4),16) << 16; // Extended Linear Address
} else if (cmd=="10") {
var addr = addrHi + parseInt(hexline.substr(3,4),16);
var data = [];
for (var i=0;i<16;i++) data.push(parseInt(hexline.substr(9+(i*2),2),16));
dataCallback(addr,data);
}
});
}

console.log('var f = require("Flash");');
var erasedPages = [];
parseLines(function(addr, data) {
var page = addr & ~(PAGESIZE-1);
if (erasedPages.indexOf(page) < 0) {
console.log('f.erasePage(0x'+page.toString(16)+');');
erasedPages.push(page);
}
});
parseLines(function(addr, data) {
// don't output stuff which is all 255
if (data.reduce((a,b)=>a+b) == data.length*255) return;
console.log('f.write(['+data+'],0x'+addr.toString(16)+');');
});
7 changes: 4 additions & 3 deletions src/jsflags.h
Expand Up @@ -18,11 +18,12 @@

typedef enum {
JSF_NONE,
JSF_DEEP_SLEEP = 1<<0, // Allow deep sleep modes (also set by setDeepSleep)
JSF_PRETOKENISE = 1<<1, // When adding functions, pre-minify them and tokenise reserved words
JSF_DEEP_SLEEP = 1<<0, ///< Allow deep sleep modes (also set by setDeepSleep)
JSF_PRETOKENISE = 1<<1, ///< When adding functions, pre-minify them and tokenise reserved words
JSF_UNSAFE_FLASH = 1<<2, ///< Some platforms stop writes/erases to interpreter memory to stop you bricking the device accidentally - this removes that protection
} PACKED_FLAGS JsFlags;

#define JSFLAG_NAMES "deepSleep\0pretokenise\0"
#define JSFLAG_NAMES "deepSleep\0pretokenise\0unsafeFlash\0"
// NOTE: \0 also added by compiler - two \0's are required!

extern volatile JsFlags jsFlags;
Expand Down
1 change: 1 addition & 0 deletions src/jswrap_espruino.c
Expand Up @@ -700,6 +700,7 @@ Get Espruino's interpreter flags that control the way it handles your JavaScript
* `deepSleep` - Allow deep sleep modes (also set by setDeepSleep)
* `pretokenise` - When adding functions, pre-minify them and tokenise reserved words
* `unsafeFlash` - Some platforms stop writes/erases to interpreter memory to stop you bricking the device accidentally - this removes that protection
*/
/*JSON{
"type" : "staticmethod",
Expand Down
3 changes: 3 additions & 0 deletions targets/nrf5x/jshardware.c
Expand Up @@ -24,6 +24,7 @@
#include "jsinteractive.h"
#include "jswrap_io.h"
#include "jswrap_date.h" // for non-F1 calendar -> days since 1970 conversion.
#include "jsflags.h"

#include "app_util_platform.h"
#ifdef BLUETOOTH
Expand Down Expand Up @@ -1082,6 +1083,8 @@ void jshI2CRead(IOEventFlags device, unsigned char address, int nBytes, unsigned


bool jshFlashWriteProtect(uint32_t addr) {
// allow protection to be overwritten
if (jsfGetFlag(JSF_UNSAFE_FLASH)) return false;
#if PUCKJS
/* It's vital we don't let anyone screw with the softdevice or bootloader.
* Recovering from changes would require soldering onto SWDIO and SWCLK pads!
Expand Down

0 comments on commit 5fedb5c

Please sign in to comment.