Skip to content

Add LZSS encoding/decoding for MKRNB 1500 SBU #551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions libraries/SBU/examples/SBU_LoadLZSS/SBU_LoadLZSS.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <MKRNB.h>
#include <SBU.h>

#include "lzssEncode.h"

static char const BINARY[] =
{
#include "Binary.h"
};

static char const CHECK_FILE[] =
{
"OK"
};

static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
const char * UPDATE_FILE_NAME_LZSS = "UPDATE.BIN.LZSS";

NBFileUtils fileUtils;
bool update_available = false;

void setup() {
Serial.begin(9600);
while (!Serial) { }

unsigned long const start = millis();
for (unsigned long now = millis(); !Serial && ((now - start) < 5000); now = millis()) { };

Serial.print("Accessing SARA Filesystem... ");
if (!fileUtils.begin(false)) {
Serial.println("failed.");
return;

}
Serial.println("OK");

uint32_t bytes_to_write = sizeof(BINARY);
Serial.print("Size of BINARY.H: ");
Serial.println(bytes_to_write);

Serial.print("Encoding \"BINARY.H\" into \"UPDATE.BIN.LZSS\" and writing it into the Sara-R410M module ... ");

//Encode into .lzss and write to the Sara modem
int bytes_written = lzss_encode(BINARY, bytes_to_write);

if (bytes_written == 0) {
Serial.println("something went wrong!");
} else {
Serial.println("OK!");
}

Serial.print("Size of UPDATE.BIN.LZSS: ");
Serial.println(bytes_written);

auto status = 0;
while (status != 2) {
status = fileUtils.createFile(CHECK_FILE_NAME, CHECK_FILE, 2);
delay(100);
}

Serial.println("Please type \"restart\" to apply the update");
update_available = true;
}


void loop() {
if (update_available == true) {
String command = Serial.readStringUntil('\n');
if (command.indexOf("restart") >= 0) {
NVIC_SystemReset();
}
}
}
182 changes: 182 additions & 0 deletions libraries/SBU/examples/SBU_LoadLZSS/lzssEncode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**************************************************************************************
INCLUDE
**************************************************************************************/

#include "lzssEncode.h"

#include <stdlib.h>
#include <stdint.h>

#include <MKRNB.h>

/**************************************************************************************
DEFINE
**************************************************************************************/

#define EI 11 /* typically 10..13 */
#define EJ 4 /* typically 4..5 */
#define P 1 /* If match length <= P then output one character */
#define N (1 << EI) /* buffer size */
#define F ((1 << EJ) + 1) /* lookahead buffer size */

#define LZSS_EOF (-1)

#define FPUTC_BUF_SIZE (512)
#define FGETC_BUF_SIZE (512)

/**************************************************************************************
GLOBAL VARIABLES
**************************************************************************************/

extern NBFileUtils fileUtils;
extern const char * UPDATE_FILE_NAME_LZSS;

int bit_buffer = 0, bit_mask = 128;
unsigned long textcount = 0;
unsigned char buffer[N * 2];

static char write_buf[FPUTC_BUF_SIZE];
static size_t write_buf_num_bytes = 0;
static size_t bytes_written_fputc = 0;

bool append = false;
bool endOfFile = false;

/**************************************************************************************
PUBLIC FUNCTIONS
**************************************************************************************/

void lzss_flush()
{
bytes_written_fputc += write_buf_num_bytes;

fileUtils.downloadFile(UPDATE_FILE_NAME_LZSS, write_buf, write_buf_num_bytes, append); //UPDATE.BIN.LZSS
append = true;

write_buf_num_bytes = 0;
}

/**************************************************************************************
PRIVATE FUNCTIONS
**************************************************************************************/

void lzss_fputc(int const c)
{
/* Buffer the compressed data into a buffer so
* we can perform block writes and don't need to
* write every byte singly on the modem
*/
write_buf[write_buf_num_bytes] = static_cast<char>(c);
write_buf_num_bytes++;

/* The write buffer is full of compressed
* data, write it to the modem now.
*/
if (write_buf_num_bytes == FPUTC_BUF_SIZE || endOfFile)
lzss_flush();
}

/**************************************************************************************
LZSS FUNCTIONS
**************************************************************************************/

void putbit1(void)
{
bit_buffer |= bit_mask;
if ((bit_mask >>= 1) == 0) {
lzss_fputc(bit_buffer);
bit_buffer = 0; bit_mask = 128;
}
}

void putbit0(void)
{
if ((bit_mask >>= 1) == 0) {
lzss_fputc(bit_buffer);
bit_buffer = 0; bit_mask = 128;
}
}

void flush_bit_buffer(void)
{
if (bit_mask != 128) {
lzss_fputc(bit_buffer);
}
}

void output1(int c)
{
int mask;

putbit1();
mask = 256;
while (mask >>= 1) {
if (c & mask) putbit1();
else putbit0();
}
}

void output2(int x, int y)
{
int mask;

putbit0();
mask = N;
while (mask >>= 1) {
if (x & mask) putbit1();
else putbit0();
}
mask = (1 << EJ);
while (mask >>= 1) {
if (y & mask) putbit1();
else putbit0();
}
}

int lzss_encode(const char buf_in[], uint32_t size)
{
int i, j, f1, x, y, r, s, bufferend, c;

for (i = 0; i < N - F; i++) buffer[i] = ' ';
for (i = N - F; i < N * 2; i++) {
if (textcount >= size) {
endOfFile = true;
break;
} else {
buffer[i] = buf_in[textcount];
textcount++;
}
}
bufferend = i; r = N - F; s = 0;
while (r < bufferend) {
f1 = (F <= bufferend - r) ? F : bufferend - r;
x = 0; y = 1; c = buffer[r];
for (i = r - 1; i >= s; i--)
if (buffer[i] == c) {
for (j = 1; j < f1; j++)
if (buffer[i + j] != buffer[r + j]) break;
if (j > y) {
x = i; y = j;
}
}
if (y <= P) { y = 1; output1(c); }
else output2(x & (N - 1), y - 2);
r += y; s += y;
if (r >= N * 2 - F) {
for (i = 0; i < N; i++) buffer[i] = buffer[i + N];
bufferend -= N; r -= N; s -= N;
while (bufferend < N * 2) {
if (textcount >= size) {
endOfFile = true;
break;
} else {
buffer[bufferend++] = buf_in[textcount];
textcount++;
}
}
}
}
flush_bit_buffer();

return bytes_written_fputc;
}
17 changes: 17 additions & 0 deletions libraries/SBU/examples/SBU_LoadLZSS/lzssEncode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SBU_LZSS_H_
#define SBU_LZSS_H_

/**************************************************************************************
INCLUDE
**************************************************************************************/

#include <stdint.h>

/**************************************************************************************
FUNCTION DEFINITION
**************************************************************************************/

void lzss_flush();
int lzss_encode(const char buf_in[], uint32_t size);

#endif /* SBU_LZSS_H_ */
101 changes: 67 additions & 34 deletions libraries/SBU/extras/SBUBoot/SBUBoot.ino
Original file line number Diff line number Diff line change
@@ -19,17 +19,20 @@
#include <FlashStorage.h>
#include <MKRNB.h>

#include "lzss.h"

#define SBU_START 0x2000
#define SBU_SIZE 0x8000

#define SKETCH_START (uint32_t*)(SBU_START + SBU_SIZE)

static constexpr char UPDATE_FILE_NAME[] = "UPDATE.BIN";
static constexpr char CHECK_FILE_NAME[] = "UPDATE.OK";
const char * UPDATE_FILE_NAME = "UPDATE.BIN";
const char * UPDATE_FILE_NAME_LZSS = "UPDATE.BIN.LZSS";
static const char * CHECK_FILE_NAME = "UPDATE.OK";

FlashClass mcu_flash;

NBFileUtils fileUtils(true);
NBFileUtils fileUtils(true);

extern "C" void __libc_init_array(void);

@@ -49,41 +52,71 @@ int main()
// Try to update only if update file
// has been download successfully.

if (fileUtils.listFile(CHECK_FILE_NAME)) {
uint32_t updateSize = fileUtils.listFile(UPDATE_FILE_NAME);
uint32_t tot_bytes = 0;
uint32_t read_bytes = 0;

if (updateSize > SBU_SIZE) {
updateSize = updateSize - SBU_SIZE;
size_t cycles = (updateSize / blockSize);
size_t spare_bytes = (updateSize % blockSize);
/* Erase the MCU flash */
uint32_t flash_address = (uint32_t)SKETCH_START;
mcu_flash.erase((void*)flash_address, updateSize);

for (auto i = 0; i < cycles; i++) {
uint8_t block[blockSize] { 0 };
digitalWrite(LED_BUILTIN, LOW);
read_bytes = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SBU_SIZE, blockSize, block);
digitalWrite(LED_BUILTIN, HIGH);
mcu_flash.write((void*)flash_address, block, read_bytes);
flash_address += read_bytes;
tot_bytes += read_bytes;
}

if (spare_bytes){
uint8_t block[spare_bytes] { 0 };
digitalWrite(LED_BUILTIN, LOW);
read_bytes = fileUtils.readBlock(UPDATE_FILE_NAME, tot_bytes + SBU_SIZE, spare_bytes, block);
digitalWrite(LED_BUILTIN, HIGH);
mcu_flash.write((void*)flash_address, block, read_bytes);
flash_address += read_bytes;
}
if (fileUtils.listFile(CHECK_FILE_NAME))
{
/*This is for LZSS compressed binaries. */
if (fileUtils.listFile(UPDATE_FILE_NAME_LZSS))
{
/* Erase the complete flash starting from the SBU forward
* because we've got no possibility of knowing how large
* the decompressed binary will finally be.
*/
mcu_flash.erase((void*)SKETCH_START, 0x40000 - (uint32_t)SKETCH_START);
/* Initialize the lzss module with the data which
* it requires.
*/
lzss_init((uint32_t)SKETCH_START);
/* During the process of decoding UPDATE.BIN.LZSS
* is decompressed and stored as UPDATE.BIN.
*/
lzss_decode();
/* Write the data remaining in the write buffer to
* the file.
*/
lzss_flush();
/* Signal a successul update. */
update_success = true;
}
/* This is for uncompressed binaries. */
else if (fileUtils.listFile(UPDATE_FILE_NAME) > 0)
{
uint32_t updateSize = fileUtils.listFile(UPDATE_FILE_NAME);
uint32_t tot_bytes = 0;
uint32_t read_bytes = 0;

if (updateSize > SBU_SIZE) {
updateSize = updateSize - SBU_SIZE;
size_t cycles = (updateSize / blockSize) + 1;
size_t spare_bytes = (updateSize % blockSize);

/* Erase the MCU flash */
uint32_t flash_address = (uint32_t)SKETCH_START;
mcu_flash.erase((void*)flash_address, updateSize);

for (auto i = 0; i < cycles; i++) {
uint8_t block[blockSize] { 0 };
digitalWrite(LED_BUILTIN, LOW);
read_bytes = fileUtils.readBlock(UPDATE_FILE_NAME, (i * blockSize) + SBU_SIZE, blockSize, block);
digitalWrite(LED_BUILTIN, HIGH);
mcu_flash.write((void*)flash_address, block, read_bytes);
flash_address += read_bytes;
tot_bytes += read_bytes;
}

if (spare_bytes){
uint8_t block[spare_bytes] { 0 };
digitalWrite(LED_BUILTIN, LOW);
read_bytes = fileUtils.readBlock(UPDATE_FILE_NAME, tot_bytes + SBU_SIZE, spare_bytes, block);
digitalWrite(LED_BUILTIN, HIGH);
mcu_flash.write((void*)flash_address, block, read_bytes);
flash_address += read_bytes;
}
update_success = true;
}
}
if (update_success) {
fileUtils.deleteFile(UPDATE_FILE_NAME);
fileUtils.deleteFile(UPDATE_FILE_NAME_LZSS);
fileUtils.deleteFile(CHECK_FILE_NAME);
}
}
219 changes: 219 additions & 0 deletions libraries/SBU/extras/SBUBoot/lzss.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/**************************************************************************************
INCLUDE
**************************************************************************************/

#include "lzss.h"

#include <stdlib.h>
#include <stdint.h>

#include <MKRNB.h>
#include <FlashStorage.h>

/**************************************************************************************
DEFINE
**************************************************************************************/

#define EI 11 /* typically 10..13 */
#define EJ 4 /* typically 4..5 */
#define P 1 /* If match length <= P then output one character */
#define N (1 << EI) /* buffer size */
#define F ((1 << EJ) + 1) /* lookahead buffer size */

#define LZSS_EOF (-1)

#define FPUTC_BUF_SIZE (512)
#define FGETC_BUF_SIZE (512)

/**************************************************************************************
GLOBAL VARIABLES
**************************************************************************************/

extern NBFileUtils fileUtils;
extern FlashClass mcu_flash;
extern const char * UPDATE_FILE_NAME_LZSS;

static uint32_t SKETCH_START = 0;
static uint32_t LZSS_FILE_SIZE = 0;

int bit_buffer = 0, bit_mask = 128;
unsigned char buffer[N * 2];

static char write_buf[FPUTC_BUF_SIZE];
static size_t write_buf_num_bytes = 0;
static size_t bytes_written_fputc = 0;
static size_t bytes_written_flash = 0;
static uint32_t flash_addr = 0;

/**************************************************************************************
PUBLIC FUNCTIONS
**************************************************************************************/

void lzss_init(uint32_t const sketch_start)
{
SKETCH_START = sketch_start;
flash_addr = sketch_start;
LZSS_FILE_SIZE = fileUtils.listFile(UPDATE_FILE_NAME_LZSS);
}

void lzss_flush()
{
bytes_written_fputc += write_buf_num_bytes;

/* Only write to the flash once we've surpassed
* the SBU in the update binary.
*/
if (bytes_written_fputc > (SKETCH_START - 0x2000))
{
mcu_flash.write((void*)flash_addr, write_buf, write_buf_num_bytes);
flash_addr += write_buf_num_bytes;
}

write_buf_num_bytes = 0;
}

/**************************************************************************************
PRIVATE FUNCTIONS
**************************************************************************************/

void lzss_fputc(int const c)
{
/* Buffer the decompressed data into a buffer so
* we can perform block writes and don't need to
* write every byte singly on the flash (which
* wouldn't be possible anyway).
*/
write_buf[write_buf_num_bytes] = static_cast<char>(c);
write_buf_num_bytes++;

/* The write buffer is full of decompressed
* data, write it to the flash now.
*/
if (write_buf_num_bytes == FPUTC_BUF_SIZE)
lzss_flush();
}

int lzss_fgetc()
{
static uint8_t read_buf[FGETC_BUF_SIZE];
static size_t read_buf_pos = FGETC_BUF_SIZE;
static size_t bytes_read_fgetc = 0;
static size_t bytes_read_from_modem = 0;

/* lzss_file_size is set within SBUBoot:main
* and contains the size of the LZSS file. Once
* all those bytes have been read its time to return
* LZSS_EOF in order to signal that the end of
* the file has been reached.
*/
if (bytes_read_fgetc == LZSS_FILE_SIZE)
return LZSS_EOF;

/* If there is no data left to be read from the read buffer
* than read a new block and store it into the read buffer.
*/
if (read_buf_pos == FGETC_BUF_SIZE)
{
/* Read the next block from the flash memory. */
bytes_read_from_modem += fileUtils.readBlock(UPDATE_FILE_NAME_LZSS, bytes_read_from_modem, FGETC_BUF_SIZE, read_buf);
/* Reset the read buffer position. */
read_buf_pos = 0;
}

uint8_t const c = read_buf[read_buf_pos];
read_buf_pos++;
bytes_read_fgetc++;

return c;
}

/**************************************************************************************
LZSS FUNCTIONS
**************************************************************************************/

void putbit1(void)
{
bit_buffer |= bit_mask;
if ((bit_mask >>= 1) == 0) {
lzss_fputc(bit_buffer);
bit_buffer = 0; bit_mask = 128;
}
}

void putbit0(void)
{
if ((bit_mask >>= 1) == 0) {
lzss_fputc(bit_buffer);
bit_buffer = 0; bit_mask = 128;
}
}

void output1(int c)
{
int mask;

putbit1();
mask = 256;
while (mask >>= 1) {
if (c & mask) putbit1();
else putbit0();
}
}

void output2(int x, int y)
{
int mask;

putbit0();
mask = N;
while (mask >>= 1) {
if (x & mask) putbit1();
else putbit0();
}
mask = (1 << EJ);
while (mask >>= 1) {
if (y & mask) putbit1();
else putbit0();
}
}

int getbit(int n) /* get n bits */
{
int i, x;
static int buf, mask = 0;

x = 0;
for (i = 0; i < n; i++) {
if (mask == 0) {
if ((buf = lzss_fgetc()) == LZSS_EOF) return LZSS_EOF;
mask = 128;
}
x <<= 1;
if (buf & mask) x++;
mask >>= 1;
}
return x;
}

void lzss_decode(void)
{
int i, j, k, r, c;

for (i = 0; i < N - F; i++) buffer[i] = ' ';
r = N - F;
while ((c = getbit(1)) != LZSS_EOF) {
if (c) {
if ((c = getbit(8)) == LZSS_EOF) break;
lzss_fputc(c);
buffer[r++] = c; r &= (N - 1);
} else {
if ((i = getbit(EI)) == LZSS_EOF) break;
if ((j = getbit(EJ)) == LZSS_EOF) break;
for (k = 0; k <= j + 1; k++) {
c = buffer[(i + k) & (N - 1)];
lzss_fputc(c);
buffer[r++] = c; r &= (N - 1);
}
}
}
}
18 changes: 18 additions & 0 deletions libraries/SBU/extras/SBUBoot/lzss.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef SBU_LZSS_H_
#define SBU_LZSS_H_

/**************************************************************************************
INCLUDE
**************************************************************************************/

#include <stdint.h>

/**************************************************************************************
FUNCTION DEFINITION
**************************************************************************************/

void lzss_init(uint32_t const sketch_start);
void lzss_decode();
void lzss_flush();

#endif /* SBU_LZSS_H_ */
4,658 changes: 2,362 additions & 2,296 deletions libraries/SBU/src/boot/mkrnb1500.h

Large diffs are not rendered by default.