|
| 1 | +/* |
| 2 | +This is a simple read-only implementation of a file system. It uses a block of data coming from the |
| 3 | +mkespfsimg tool, and can use that block to do abstracted operations on the files that are in there. |
| 4 | +It's written for use with httpd, but doesn't need to be used as such. |
| 5 | +*/ |
| 6 | + |
| 7 | +/* |
| 8 | + * ---------------------------------------------------------------------------- |
| 9 | + * "THE BEER-WARE LICENSE" (Revision 42): |
| 10 | + * Jeroen Domburg <[email protected]> wrote this file. As long as you retain |
| 11 | + * this notice you can do whatever you want with this stuff. If we meet some day, |
| 12 | + * and you think this stuff is worth it, you can buy me a beer in return. |
| 13 | + * ---------------------------------------------------------------------------- |
| 14 | + */ |
| 15 | + |
| 16 | + |
| 17 | +//These routines can also be tested by comping them in with the espfstest tool. This |
| 18 | +//simplifies debugging, but needs some slightly different headers. The #ifdef takes |
| 19 | +//care of that. |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | +#include <stdint.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#include <esp_spi_flash.h> |
| 27 | +#include <esp_log.h> |
| 28 | +#include <esp_err.h> |
| 29 | + |
| 30 | +#include "espfsformat.h" |
| 31 | +#include "espfs.h" |
| 32 | +#include "sdkconfig.h" |
| 33 | + |
| 34 | +static char tag[] = "espfs"; |
| 35 | + |
| 36 | + |
| 37 | +struct EspFsFile { |
| 38 | + EspFsHeader *header; |
| 39 | + char decompressor; |
| 40 | + int32_t posDecomp; |
| 41 | + char *posStart; |
| 42 | + char *posComp; |
| 43 | + void *decompData; |
| 44 | +}; |
| 45 | + |
| 46 | +static spi_flash_mmap_handle_t handle; |
| 47 | +static void *espFlashPtr = NULL; |
| 48 | + |
| 49 | +EspFsInitResult espFsInit(void *flashAddress, size_t size) { |
| 50 | + |
| 51 | + spi_flash_init(); |
| 52 | + esp_err_t rc = spi_flash_mmap((uint32_t) flashAddress, 64*1024*2, SPI_FLASH_MMAP_DATA, (const void **)&espFlashPtr, &handle); |
| 53 | + if (rc != ESP_OK) { |
| 54 | + ESP_LOGD(tag, "rc from spi_flash_mmap: %d", rc); |
| 55 | + } |
| 56 | + |
| 57 | + // check if there is valid header at address |
| 58 | + EspFsHeader *testHeader = (EspFsHeader *)espFlashPtr; |
| 59 | + |
| 60 | + if (testHeader->magic != ESPFS_MAGIC) { |
| 61 | + ESP_LOGE(tag, "No valid header at flash address. Expected to find %x and found %x", ESPFS_MAGIC, testHeader->magic); |
| 62 | + return ESPFS_INIT_RESULT_NO_IMAGE; |
| 63 | + } |
| 64 | + |
| 65 | + return ESPFS_INIT_RESULT_OK; |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +// Returns flags of opened file. |
| 71 | +int espFsFlags(EspFsFile *fh) { |
| 72 | + if (fh == NULL) { |
| 73 | + ESP_LOGD(tag, "File handle not ready"); |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + int8_t flags; |
| 78 | + memcpy((char*)&flags, (char*)&fh->header->flags, 1); |
| 79 | + return (int)flags; |
| 80 | +} |
| 81 | + |
| 82 | +//Open a file and return a pointer to the file desc struct. |
| 83 | +EspFsFile *espFsOpen(char *fileName) { |
| 84 | + if (espFlashPtr == NULL) { |
| 85 | + ESP_LOGD(tag, "Call espFsInit first!"); |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + char *flashAddress = espFlashPtr; |
| 89 | + char *hpos; |
| 90 | + char *namebuf; |
| 91 | + EspFsHeader *header; |
| 92 | + EspFsFile *fileData; |
| 93 | + //Strip initial slashes |
| 94 | + while(fileName[0] == '/') { |
| 95 | + fileName++; |
| 96 | + } |
| 97 | + //Go find that file! |
| 98 | + while(1) { |
| 99 | + hpos=flashAddress; |
| 100 | + //Grab the next file header. |
| 101 | + header = (EspFsHeader *)flashAddress; |
| 102 | + |
| 103 | + if (header->magic != ESPFS_MAGIC) { |
| 104 | + ESP_LOGD(tag, "Magic mismatch. EspFS image broken."); |
| 105 | + return NULL; |
| 106 | + } |
| 107 | + if (header->flags & FLAG_LASTFILE) { |
| 108 | + ESP_LOGD(tag, "End of image. File not found."); |
| 109 | + return NULL; |
| 110 | + } |
| 111 | + //Grab the name of the file. |
| 112 | + flashAddress += sizeof(EspFsHeader); |
| 113 | + namebuf = (char *)flashAddress; |
| 114 | + if (strcmp(namebuf, fileName) == 0) { |
| 115 | + //Yay, this is the file we need! |
| 116 | + flashAddress += header->nameLen; //Skip to content. |
| 117 | + fileData = (EspFsFile *)malloc(sizeof(EspFsFile)); //Alloc file desc mem |
| 118 | + if (fileData==NULL) { |
| 119 | + return NULL; |
| 120 | + } |
| 121 | + fileData->header = (EspFsHeader *)hpos; |
| 122 | + fileData->decompressor = header->compression; |
| 123 | + fileData->posComp = flashAddress; |
| 124 | + fileData->posStart = flashAddress; |
| 125 | + fileData->posDecomp = 0; |
| 126 | + if (header->compression == COMPRESS_NONE) { |
| 127 | + fileData->decompData = NULL; |
| 128 | + } else { |
| 129 | + ESP_LOGD(tag, "Invalid compression: %d", header->compression); |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + return fileData; |
| 133 | + } |
| 134 | + //We don't need this file. Skip name and file |
| 135 | + flashAddress += header->nameLen+header->fileLenComp; |
| 136 | + if ((int)flashAddress&3) { |
| 137 | + flashAddress += 4-((int)flashAddress & 3); //align to next 32bit val |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +//Read len bytes from the given file into buff. Returns the actual amount of bytes read. |
| 143 | +int espFsRead(EspFsFile *fh, char *buff, int len) { |
| 144 | + int flen; |
| 145 | + if (fh==NULL) { |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + memcpy((char*)&flen, (char*)&fh->header->fileLenComp, 4); |
| 150 | + |
| 151 | + if (fh->decompressor == COMPRESS_NONE) { |
| 152 | + int toRead; |
| 153 | + toRead = flen-(fh->posComp-fh->posStart); |
| 154 | + if (len > toRead) { |
| 155 | + len = toRead; |
| 156 | + } |
| 157 | + ESP_LOGD(tag, "copying %d bytes from 0x%x to 0x%x", len, (uint32_t)fh->posComp, (uint32_t)buff); |
| 158 | + memcpy(buff, fh->posComp, len); |
| 159 | + fh->posDecomp += len; |
| 160 | + fh->posComp += len; |
| 161 | + return len; |
| 162 | + } |
| 163 | + return 0; |
| 164 | +} |
| 165 | + |
| 166 | +int espFsAccess(EspFsFile *fh, void **buf, size_t *len) { |
| 167 | + *buf = fh->posStart; |
| 168 | + *len = fh->header->fileLenComp; |
| 169 | + return *len; |
| 170 | +} |
| 171 | + |
| 172 | +//Close the file. |
| 173 | +void espFsClose(EspFsFile *fh) { |
| 174 | + if (fh == NULL) return; |
| 175 | + free(fh); |
| 176 | +} |
0 commit comments