From d8afde3cc10d67b9998acb8dc161ec4d034a0e0d Mon Sep 17 00:00:00 2001 From: Jindra Dolezy Date: Thu, 9 Apr 2015 23:11:33 +0200 Subject: [PATCH] Added espFsInit call --- espfs/espfs.c | 29 ++++++++++++++++++++++------- espfs/espfs.h | 10 ++++++---- espfs/espfsformat.h | 1 + espfs/espfstest/main.c | 7 +++++++ espfs/espfstest/style.css | 17 +++++++++++++++++ user/user_main.c | 7 ++++++- 6 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 espfs/espfstest/style.css diff --git a/espfs/espfs.c b/espfs/espfs.c index 06ae135..7536a41 100644 --- a/espfs/espfs.c +++ b/espfs/espfs.c @@ -35,7 +35,6 @@ It's written for use with httpd, but doesn't need to be used as such. #define os_strcpy strcpy #define os_printf printf #define ICACHE_FLASH_ATTR -extern char* espFsData; #endif #include "espfsformat.h" @@ -46,7 +45,7 @@ extern char* espFsData; #include "heatshrink_decoder.h" #endif - +static char* espFsData = NULL; struct EspFsFile { @@ -74,6 +73,22 @@ Accessing the flash through the mem emulation at 0x40200000 is a bit hairy: All a memory exception, crashing the program. */ +EspFsInitResult espFsInit(void *flashAddress) { + // 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; +} //Copies len bytes over from dst to src, but does it using *only* //aligned 32-bit reads. Yes, it's no too optimized but it's short and sweet and it works. @@ -100,11 +115,11 @@ void ICACHE_FLASH_ATTR memcpyAligned(char *dst, char *src, int len) { //Open a file and return a pointer to the file desc struct. EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { -#ifdef __ets__ - char *p=(char *)(ESPFS_POS+0x40200000); -#else + if (espFsData == NULL) { + os_printf("Call espFsInit first!\n"); + return NULL; + } char *p=espFsData; -#endif char *hpos; char namebuf[256]; EspFsHeader h; @@ -116,7 +131,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { hpos=p; //Grab the next file header. os_memcpy(&h, p, sizeof(EspFsHeader)); - if (h.magic!=0x73665345) { + if (h.magic!=ESPFS_MAGIC) { os_printf("Magic mismatch. EspFS image broken.\n"); return NULL; } diff --git a/espfs/espfs.h b/espfs/espfs.h index 98e8aaa..4d7e1ff 100644 --- a/espfs/espfs.h +++ b/espfs/espfs.h @@ -4,13 +4,15 @@ //Define this if you want to be able to use Heatshrink-compressed espfs images. #define ESPFS_HEATSHRINK -//Pos of esp fs in flash -#define ESPFS_POS 0x12000 -#define ESPFS_SIZE 0x2E000 - +typedef enum { + ESPFS_INIT_RESULT_OK, + ESPFS_INIT_RESULT_NO_IMAGE, + ESPFS_INIT_RESULT_BAD_ALIGN, +} EspFsInitResult; typedef struct EspFsFile EspFsFile; +EspFsInitResult espFsInit(void *flashAddress); EspFsFile *espFsOpen(char *fileName); int espFsRead(EspFsFile *fh, char *buff, int len); void espFsClose(EspFsFile *fh); diff --git a/espfs/espfsformat.h b/espfs/espfsformat.h index d937f99..325cad5 100644 --- a/espfs/espfsformat.h +++ b/espfs/espfsformat.h @@ -18,6 +18,7 @@ with the FLAG_LASTFILE flag set. #define FLAG_LASTFILE (1<<0) #define COMPRESS_NONE 0 #define COMPRESS_HEATSHRINK 1 +#define ESPFS_MAGIC 0x73665345 typedef struct { int32_t magic; diff --git a/espfs/espfstest/main.c b/espfs/espfstest/main.c index 26143ee..16a6287 100644 --- a/espfs/espfstest/main.c +++ b/espfs/espfstest/main.c @@ -22,6 +22,7 @@ int main(int argc, char **argv) { char buff[128]; EspFsFile *ef; off_t size; + EspFsInitResult ir; if (argc!=3) { printf("Usage: %s espfs-image file\nExpands file from the espfs-image archive.\n", argv[0]); @@ -40,6 +41,12 @@ int main(int argc, char **argv) { exit(1); } + ir=espFsInit(espFsData); + if (ir != ESPFS_INIT_RESULT_OK) { + printf("Couldn't init espfs filesystem (code %d)\n", ir); + exit(1); + } + ef=espFsOpen(argv[2]); if (ef==NULL) { printf("Couldn't find %s in image.\n", argv[2]); diff --git a/espfs/espfstest/style.css b/espfs/espfstest/style.css new file mode 100644 index 0000000..2a2f758 --- /dev/null +++ b/espfs/espfstest/style.css @@ -0,0 +1,17 @@ + +body { + background-color: #404040; + font-family: sans-serif; +} + +#main { + background-color: #d0d0FF; + -moz-border-radius: 5px; + -webkit-border-radius: 5px; + border-radius: 5px; + border: 2px solid #000000; + width: 800px; + margin: 0 auto; + padding: 20px +} + diff --git a/user/user_main.c b/user/user_main.c index e4f9058..70af276 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -19,6 +19,7 @@ #include "cgiflash.h" #include "stdout.h" #include "auth.h" +#include "espfs.h" //Function that tells the authentication system what users/passwords live on the system. //This is disabled in the default build; if you want to try it, enable the authBasic line in @@ -73,10 +74,14 @@ HttpdBuiltInUrl builtInUrls[]={ }; -//Main routine. Initialize stdout, the I/O and the webserver and we're done. +//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done. void user_init(void) { stdoutInit(); ioInit(); + + // 0x40200000 is the base address for spi flash memory mapping, 0x12000 is the position + // where image is written in flash + espFsInit((void*)(0x40200000 + 0x12000)); httpdInit(builtInUrls, 80); os_printf("\nReady\n"); }