//Open a file and return a pointer to the file desc struct. EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { if (espFsData == NULL) { os_printf("Call espFsInit first!\n"); return NULL; } char *p=espFsData; char *hpos; char namebuf[256]; EspFsHeader h; EspFsFile *r; //Strip initial slashes while(fileName[0]=='/') fileName++; //Go find that file! while(1) { hpos=p; //Grab the next file header. os_memcpy(&h, p, sizeof(EspFsHeader)); if (h.magic!=ESPFS_MAGIC) { os_printf("Magic mismatch. EspFS image broken.\n"); return NULL; } if (h.flags&FLAG_LASTFILE) { os_printf("End of image.\n"); return NULL; } //Grab the name of the file. p+=sizeof(EspFsHeader); os_memcpy(namebuf, p, sizeof(namebuf)); // 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); if (os_strcmp(namebuf, fileName)==0) { //Yay, this is the file we need! p+=h.nameLen; //Skip to content. r=(EspFsFile *)os_malloc(sizeof(EspFsFile)); //Alloc file desc mem // os_printf("Alloc %p\n", r); if (r==NULL) return NULL; r->header=(EspFsHeader *)hpos; r->decompressor=h.compression; r->posComp=p; r->posStart=p; r->posDecomp=0; if (h.compression==COMPRESS_NONE) { r->decompData=NULL; #ifdef ESPFS_HEATSHRINK } else if (h.compression==COMPRESS_HEATSHRINK) { //File is compressed with Heatshrink. char parm; heatshrink_decoder *dec; //Decoder params are stored in 1st byte. memcpyAligned(&parm, r->posComp, 1); r->posComp++; os_printf("Heatshrink compressed file; decode parms = %x\n", parm); dec=heatshrink_decoder_alloc(16, (parm>>4)&0xf, parm&0xf); r->decompData=dec; #endif } else {
// 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; }
//Read len bytes from the given file into buff. Returns the actual amount of bytes read. int ICACHE_FLASH_ATTR espFsRead(EspFsFile *fh, char *buff, int len) { int flen, fdlen; if (fh==NULL) return 0; //Cache file length. memcpyAligned((char*)&flen, (char*)&fh->header->fileLenComp, 4); memcpyAligned((char*)&fdlen, (char*)&fh->header->fileLenDecomp, 4); //Do stuff depending on the way the file is compressed. if (fh->decompressor==COMPRESS_NONE) { int toRead; toRead=flen-(fh->posComp-fh->posStart); if (len>toRead) len=toRead; // os_printf("Reading %d bytes from %x\n", len, (unsigned int)fh->posComp); 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; } return 0; }
int f_read(RO_FILE *f,char * buff,int len){ if(f==NULL) return 0; int bytesToRead = len; int remLen = f->file->size - f->readPos; if(remLen < len) bytesToRead=remLen; NODE_DBG("Reading %d bytes from %s",bytesToRead,f->file->name); memcpyAligned(buff,rofs_data+f->file->offset+f->readPos,bytesToRead); //platform_flash_read(buff,(uint32_t)(rofs_data+f->file->offset+f->readPos),bytesToRead); f->readPos+=bytesToRead; f->eof= (f->readPos == f->file->size); return bytesToRead; }
/* espFsOpen */ EspFsFile ICACHE_FLASH_ATTR * espFsOpen(char * pszFilename) { /* initialization */ char * pbHeaderOffset = NULL; char * pbTmpFile = NULL; EspFsFile * ptEspFile = NULL; char pszFileNameBuffer[256] = { 0 }; EspFsHeader tEspFileHeader = { 0 }; /* acquire file system beginning */ #ifndef ESPFS_TEST int iEspFsOffset = 0; if (0 == system_upgrade_userbin_check()) { iEspFsOffset = partition[ESPFS_PART].iOffset; } else { iEspFsOffset = partition[ESPFS_PART2].iOffset; } pbTmpFile = (char *)(iEspFsOffset + ESP_FLASH_OFFSET); #else pbTmpFile = espFsData; #endif /* strip file name slashes */ while (SLASH_SYMBOL_STRING == pszFilename[0]) { ++pszFilename; } /* locate file */ while (1) { pbHeaderOffset = pbTmpFile; /* retrieve file header */ os_memcpy(&tEspFileHeader, pbTmpFile, sizeof(EspFsHeader)); if (ESPFS_MAGIC_HEADER != tEspFileHeader.magic) { #ifdef ESPFS_DEBUG os_printf("espFsOpen: magic mismatch - file system image broken. found 0x%x instead\n", tEspFileHeader.magic); #endif ptEspFile = NULL; goto lblCleanup; } if (tEspFileHeader.flags & FLAG_LASTFILE) { #ifdef ESPFS_DEBUG os_printf("espFsOpen: end of image reached\n"); #endif ptEspFile = NULL; goto lblCleanup; } /* acquire file name */ pbTmpFile += sizeof(EspFsHeader); os_memcpy(pszFileNameBuffer, pbTmpFile, sizeof(pszFileNameBuffer)); #ifdef ESPFS_DEBUG os_printf("espFsOpen: found file '%s'\nname length = %x, file length compressed = %x, compression = %d flags = %d\n", pszFileNameBuffer, (unsigned int)tEspFileHeader.nameLen, (unsigned int)tEspFileHeader.fileLenComp, tEspFileHeader.compression, tEspFileHeader.flags); #endif if (0 == os_strcmp(pszFileNameBuffer, pszFilename)) { /* desired file */ /* skip to content */ pbTmpFile += tEspFileHeader.nameLen; /* allocate file descriptor */ ptEspFile = (EspFsFile *)os_malloc(sizeof(EspFsFile)); #ifdef ESPFS_DEBUG os_printf("espFsOpen: file descriptor allocated at %p\n", ptEspFile); #endif if (NULL == ptEspFile) { goto lblCleanup; } /* fill file descriptor */ ptEspFile->header = (EspFsHeader *)pbHeaderOffset; ptEspFile->decompressor = tEspFileHeader.compression; ptEspFile->posComp = pbTmpFile; ptEspFile->posStart = pbTmpFile; ptEspFile->posDecomp = 0; if (COMPRESS_NONE == tEspFileHeader.compression) { ptEspFile->decompData = NULL; #ifdef EFS_HEATSHRINK } else if (COMPRESS_HEATSHRINK == tEspFileHeader.compression) { /* compression used */ char bDecoderParameter = { 0 }; heatshrink_decoder * ptDecoder = NULL; /* acquire decoder parameters - 1st byte */ memcpyAligned(&bDecoderParameter, ptEspFile->posComp, 1); ++ptEspFile->posComp; #ifdef HEATSHRINK_DEBUG os_printf("espFsOpen: heatshrink compressed file, decoder parameters = %x\n", bDecoderParameter); #endif ptDecoder = heatshrink_decoder_alloc(16, (bDecoderParameter >> 4) & 0xf, bDecoderParameter & 0xf); ptEspFile->decompData = ptDecoder; #endif } else {