You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
275 lines
7.8 KiB
275 lines
7.8 KiB
10 years ago
|
/*
|
||
|
This is a simple read-only implementation of a file system. It uses a block of data coming from the
|
||
|
mkespfsimg tool, and can use that block to do abstracted operations on the files that are in there.
|
||
|
It's written for use with httpd, but doesn't need to be used as such.
|
||
|
*/
|
||
|
|
||
10 years ago
|
/*
|
||
|
* ----------------------------------------------------------------------------
|
||
|
* "THE BEER-WARE LICENSE" (Revision 42):
|
||
|
* Jeroen Domburg <jeroen@spritesmods.com> wrote this file. As long as you retain
|
||
|
* this notice you can do whatever you want with this stuff. If we meet some day,
|
||
|
* and you think this stuff is worth it, you can buy me a beer in return.
|
||
|
* ----------------------------------------------------------------------------
|
||
|
*/
|
||
|
|
||
|
|
||
|
//These routines can also be tested by comping them in with the espfstest tool. This
|
||
|
//simplifies debugging, but needs some slightly different headers. The #ifdef takes
|
||
|
//care of that.
|
||
|
|
||
10 years ago
|
#ifdef __ets__
|
||
|
//esp build
|
||
10 years ago
|
#include <esp8266.h>
|
||
10 years ago
|
#else
|
||
|
//Test build
|
||
|
#include <stdio.h>
|
||
|
#include <stdint.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#define os_malloc malloc
|
||
|
#define os_free free
|
||
|
#define os_memcpy memcpy
|
||
|
#define os_strncmp strncmp
|
||
|
#define os_strcmp strcmp
|
||
|
#define os_strcpy strcpy
|
||
|
#define os_printf printf
|
||
|
#define ICACHE_FLASH_ATTR
|
||
|
#endif
|
||
|
|
||
10 years ago
|
#include "espfsformat.h"
|
||
10 years ago
|
#include "espfs.h"
|
||
10 years ago
|
|
||
|
#ifdef ESPFS_HEATSHRINK
|
||
|
#include "heatshrink_config_custom.h"
|
||
10 years ago
|
#include "heatshrink_decoder.h"
|
||
|
#endif
|
||
10 years ago
|
|
||
10 years ago
|
static char* espFsData = NULL;
|
||
10 years ago
|
|
||
|
|
||
10 years ago
|
struct EspFsFile {
|
||
|
EspFsHeader *header;
|
||
|
char decompressor;
|
||
|
int32_t posDecomp;
|
||
|
char *posStart;
|
||
|
char *posComp;
|
||
|
void *decompData;
|
||
|
};
|
||
|
|
||
|
/*
|
||
10 years ago
|
Available locations, at least in my flash, with boundaries partially guessed. This
|
||
10 years ago
|
is using 0.9.1/0.9.2 SDK on a not-too-new module.
|
||
10 years ago
|
0x00000 (0x10000): Code/data (RAM data?)
|
||
10 years ago
|
0x10000 (0x02000): Gets erased by something?
|
||
|
0x12000 (0x2E000): Free (filled with zeroes) (parts used by ESPCloud and maybe SSL)
|
||
10 years ago
|
0x40000 (0x20000): Code/data (ROM data?)
|
||
|
0x60000 (0x1C000): Free
|
||
|
0x7c000 (0x04000): Param store
|
||
|
0x80000 - end of flash
|
||
|
|
||
|
Accessing the flash through the mem emulation at 0x40200000 is a bit hairy: All accesses
|
||
|
*must* be aligned 32-bit accesses. Reading a short, byte or unaligned word will result in
|
||
|
a memory exception, crashing the program.
|
||
|
*/
|
||
|
|
||
10 years ago
|
EspFsInitResult ICACHE_FLASH_ATTR espFsInit(void *flashAddress) {
|
||
10 years ago
|
// base address must be aligned to 4 bytes
|
||
|
if (((int)flashAddress & 3) != 0) {
|
||
|
return ESPFS_INIT_RESULT_BAD_ALIGN;
|
||
|
}
|
||
|
|
||
|
// check if there is valid header at address
|
||
|
EspFsHeader testHeader;
|
||
|
os_memcpy(&testHeader, flashAddress, sizeof(EspFsHeader));
|
||
|
if (testHeader.magic != ESPFS_MAGIC) {
|
||
|
return ESPFS_INIT_RESULT_NO_IMAGE;
|
||
|
}
|
||
|
|
||
|
espFsData = (char *)flashAddress;
|
||
|
return ESPFS_INIT_RESULT_OK;
|
||
|
}
|
||
10 years ago
|
|
||
|
//Copies len bytes over from dst to src, but does it using *only*
|
||
10 years ago
|
//aligned 32-bit reads. Yes, it's no too optimized but it's short and sweet and it works.
|
||
10 years ago
|
|
||
|
//ToDo: perhaps os_memcpy also does unaligned accesses?
|
||
10 years ago
|
#ifdef __ets__
|
||
10 years ago
|
void ICACHE_FLASH_ATTR memcpyAligned(char *dst, char *src, int len) {
|
||
10 years ago
|
int x;
|
||
|
int w, b;
|
||
|
for (x=0; x<len; x++) {
|
||
|
b=((int)src&3);
|
||
|
w=*((int *)(src-b));
|
||
|
if (b==0) *dst=(w>>0);
|
||
|
if (b==1) *dst=(w>>8);
|
||
|
if (b==2) *dst=(w>>16);
|
||
|
if (b==3) *dst=(w>>24);
|
||
|
dst++; src++;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
#else
|
||
|
#define memcpyAligned memcpy
|
||
|
#endif
|
||
10 years ago
|
|
||
10 years ago
|
// Returns flags of opened file.
|
||
|
int ICACHE_FLASH_ATTR espFsFlags(EspFsFile *fh) {
|
||
|
if (fh == NULL) {
|
||
|
os_printf("File handle not ready\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
int8_t flags;
|
||
|
memcpyAligned((char*)&flags, (char*)&fh->header->flags, 1);
|
||
|
return (int)flags;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
//Open a file and return a pointer to the file desc struct.
|
||
10 years ago
|
EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) {
|
||
10 years ago
|
if (espFsData == NULL) {
|
||
|
os_printf("Call espFsInit first!\n");
|
||
|
return NULL;
|
||
|
}
|
||
10 years ago
|
char *p=espFsData;
|
||
10 years ago
|
char *hpos;
|
||
|
char namebuf[256];
|
||
|
EspFsHeader h;
|
||
|
EspFsFile *r;
|
||
10 years ago
|
//Strip initial slashes
|
||
10 years ago
|
while(fileName[0]=='/') fileName++;
|
||
|
//Go find that file!
|
||
|
while(1) {
|
||
|
hpos=p;
|
||
10 years ago
|
//Grab the next file header.
|
||
10 years ago
|
os_memcpy(&h, p, sizeof(EspFsHeader));
|
||
10 years ago
|
if (h.magic!=ESPFS_MAGIC) {
|
||
10 years ago
|
os_printf("Magic mismatch. EspFS image broken.\n");
|
||
|
return NULL;
|
||
|
}
|
||
10 years ago
|
if (h.flags&FLAG_LASTFILE) {
|
||
10 years ago
|
os_printf("End of image.\n");
|
||
10 years ago
|
return NULL;
|
||
|
}
|
||
10 years ago
|
//Grab the name of the file.
|
||
|
p+=sizeof(EspFsHeader);
|
||
10 years ago
|
os_memcpy(namebuf, p, sizeof(namebuf));
|
||
10 years ago
|
// os_printf("Found file '%s'. Namelen=%x fileLenComp=%x, compr=%d flags=%d\n",
|
||
|
// namebuf, (unsigned int)h.nameLen, (unsigned int)h.fileLenComp, h.compression, h.flags);
|
||
10 years ago
|
if (os_strcmp(namebuf, fileName)==0) {
|
||
10 years ago
|
//Yay, this is the file we need!
|
||
|
p+=h.nameLen; //Skip to content.
|
||
|
r=(EspFsFile *)os_malloc(sizeof(EspFsFile)); //Alloc file desc mem
|
||
10 years ago
|
// os_printf("Alloc %p\n", r);
|
||
10 years ago
|
if (r==NULL) return NULL;
|
||
|
r->header=(EspFsHeader *)hpos;
|
||
|
r->decompressor=h.compression;
|
||
|
r->posComp=p;
|
||
|
r->posStart=p;
|
||
|
r->posDecomp=0;
|
||
10 years ago
|
if (h.compression==COMPRESS_NONE) {
|
||
|
r->decompData=NULL;
|
||
10 years ago
|
#ifdef ESPFS_HEATSHRINK
|
||
10 years ago
|
} else if (h.compression==COMPRESS_HEATSHRINK) {
|
||
10 years ago
|
//File is compressed with Heatshrink.
|
||
10 years ago
|
char parm;
|
||
10 years ago
|
heatshrink_decoder *dec;
|
||
10 years ago
|
//Decoder params are stored in 1st byte.
|
||
|
memcpyAligned(&parm, r->posComp, 1);
|
||
|
r->posComp++;
|
||
|
os_printf("Heatshrink compressed file; decode parms = %x\n", parm);
|
||
10 years ago
|
dec=heatshrink_decoder_alloc(16, (parm>>4)&0xf, parm&0xf);
|
||
|
r->decompData=dec;
|
||
10 years ago
|
#endif
|
||
|
} else {
|
||
|
os_printf("Invalid compression: %d\n", h.compression);
|
||
|
return NULL;
|
||
|
}
|
||
10 years ago
|
return r;
|
||
|
}
|
||
10 years ago
|
//We don't need this file. Skip name and file
|
||
10 years ago
|
p+=h.nameLen+h.fileLenComp;
|
||
|
if ((int)p&3) p+=4-((int)p&3); //align to next 32bit val
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
//Read len bytes from the given file into buff. Returns the actual amount of bytes read.
|
||
10 years ago
|
int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) {
|
||
10 years ago
|
int flen, fdlen;
|
||
10 years ago
|
if (fh==NULL) return 0;
|
||
10 years ago
|
//Cache file length.
|
||
|
memcpyAligned((char*)&flen, (char*)&fh->header->fileLenComp, 4);
|
||
10 years ago
|
memcpyAligned((char*)&fdlen, (char*)&fh->header->fileLenDecomp, 4);
|
||
10 years ago
|
//Do stuff depending on the way the file is compressed.
|
||
10 years ago
|
if (fh->decompressor==COMPRESS_NONE) {
|
||
|
int toRead;
|
||
10 years ago
|
toRead=flen-(fh->posComp-fh->posStart);
|
||
10 years ago
|
if (len>toRead) len=toRead;
|
||
10 years ago
|
// os_printf("Reading %d bytes from %x\n", len, (unsigned int)fh->posComp);
|
||
10 years ago
|
memcpyAligned(buff, fh->posComp, len);
|
||
|
fh->posDecomp+=len;
|
||
|
fh->posComp+=len;
|
||
|
// os_printf("Done reading %d bytes, pos=%x\n", len, fh->posComp);
|
||
|
return len;
|
||
10 years ago
|
#ifdef ESPFS_HEATSHRINK
|
||
10 years ago
|
} else if (fh->decompressor==COMPRESS_HEATSHRINK) {
|
||
|
int decoded=0;
|
||
10 years ago
|
size_t elen, rlen;
|
||
10 years ago
|
char ebuff[16];
|
||
|
heatshrink_decoder *dec=(heatshrink_decoder *)fh->decompData;
|
||
10 years ago
|
// os_printf("Alloc %p\n", dec);
|
||
10 years ago
|
if (fh->posDecomp == fdlen) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
// We must ensure that whole file is decompressed and written to output buffer.
|
||
|
// This means even when there is no input data (elen==0) try to poll decoder until
|
||
|
// posDecomp equals decompressed file length
|
||
|
|
||
10 years ago
|
while(decoded<len) {
|
||
|
//Feed data into the decompressor
|
||
10 years ago
|
//ToDo: Check ret val of heatshrink fns for errors
|
||
10 years ago
|
elen=flen-(fh->posComp - fh->posStart);
|
||
|
if (elen>0) {
|
||
|
memcpyAligned(ebuff, fh->posComp, 16);
|
||
10 years ago
|
heatshrink_decoder_sink(dec, (uint8_t *)ebuff, (elen>16)?16:elen, &rlen);
|
||
10 years ago
|
fh->posComp+=rlen;
|
||
|
}
|
||
|
//Grab decompressed data and put into buff
|
||
10 years ago
|
heatshrink_decoder_poll(dec, (uint8_t *)buff, len-decoded, &rlen);
|
||
10 years ago
|
fh->posDecomp+=rlen;
|
||
|
buff+=rlen;
|
||
|
decoded+=rlen;
|
||
10 years ago
|
|
||
|
// os_printf("Elen %d rlen %d d %d pd %ld fdl %d\n",elen,rlen,decoded, fh->posDecomp, fdlen);
|
||
|
|
||
|
if (elen == 0) {
|
||
|
if (fh->posDecomp == fdlen) {
|
||
|
// os_printf("Decoder finish\n");
|
||
|
heatshrink_decoder_finish(dec);
|
||
|
}
|
||
|
return decoded;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
return len;
|
||
|
#endif
|
||
10 years ago
|
}
|
||
10 years ago
|
return 0;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
//Close the file.
|
||
10 years ago
|
void ICACHE_FLASH_ATTR espFsClose(EspFsFile *fh) {
|
||
10 years ago
|
if (fh==NULL) return;
|
||
10 years ago
|
#ifdef ESPFS_HEATSHRINK
|
||
10 years ago
|
if (fh->decompressor==COMPRESS_HEATSHRINK) {
|
||
10 years ago
|
heatshrink_decoder *dec=(heatshrink_decoder *)fh->decompData;
|
||
|
heatshrink_decoder_free(dec);
|
||
|
// os_printf("Freed %p\n", dec);
|
||
10 years ago
|
}
|
||
|
#endif
|
||
10 years ago
|
// os_printf("Freed %p\n", fh);
|
||
10 years ago
|
os_free(fh);
|
||
|
}
|
||
|
|
||
|
|
||
|
|