romfs.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (C) 2017 National Institute of Advanced Industrial Science
  3. * and Technology (AIST)
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * Redistributions of source code must retain the above copyright notice, this
  11. * list of conditions and the following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. *
  17. * Neither the name of AIST nor the names of its contributors may be used
  18. * to endorse or promote products derived from this software without specific
  19. * prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <libwebsockets.h>
  34. #include <string.h>
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include "romfs.h"
  38. #if defined(LWS_WITH_ESP32)
  39. #include "esp_spi_flash.h"
  40. #endif
  41. #define RFS_STRING_MAX 96
  42. static u32_be_t cache[(RFS_STRING_MAX + 32) / 4];
  43. static romfs_inode_t ci = (romfs_inode_t)cache;
  44. static romfs_t cr = (romfs_t)cache;
  45. static void
  46. set_cache(romfs_inode_t inode, size_t len)
  47. {
  48. #if defined(LWS_WITH_ESP32)
  49. spi_flash_read((uint32_t)inode, cache, len);
  50. #endif
  51. }
  52. static uint32_t
  53. untohl(const u32_be_t be)
  54. {
  55. return ((be >> 24) & 0xff) |
  56. ((be >> 16) & 0xff) << 8 |
  57. ((be >> 8) & 0xff) << 16 |
  58. (be & 0xff) << 24;
  59. }
  60. static romfs_inode_t
  61. romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path);
  62. static int
  63. plus_padding(const uint8_t *s)
  64. {
  65. int n;
  66. set_cache((romfs_inode_t)s, RFS_STRING_MAX);
  67. n = strlen((const char *)cache);
  68. if (!(n & 15))
  69. n += 0x10;
  70. return (n + 15) & ~15;
  71. }
  72. static romfs_inode_t
  73. skip_and_pad(romfs_inode_t ri)
  74. {
  75. const uint8_t *p = ((const uint8_t *)ri) + sizeof(*ri);
  76. return (romfs_inode_t)(p + plus_padding(p));
  77. }
  78. size_t
  79. romfs_mount_check(romfs_t romfs)
  80. {
  81. set_cache((romfs_inode_t)romfs, sizeof(*romfs));
  82. if (cr->magic1 != 0x6d6f722d ||
  83. cr->magic2 != 0x2d736631)
  84. return 0;
  85. return untohl(cr->size);
  86. }
  87. static romfs_inode_t
  88. romfs_symlink(romfs_t romfs, romfs_inode_t level, romfs_inode_t i)
  89. {
  90. const char *p = (const char *)skip_and_pad(i);
  91. if (*p == '/') {
  92. level = skip_and_pad((romfs_inode_t)romfs);
  93. p++;
  94. }
  95. return romfs_lookup(romfs, level, p);
  96. }
  97. static romfs_inode_t
  98. dir_link(romfs_t romfs, romfs_inode_t i)
  99. {
  100. set_cache(i, sizeof(*i));
  101. return (romfs_inode_t)((const uint8_t *)romfs +
  102. untohl(ci->dir_start));
  103. }
  104. static romfs_inode_t
  105. romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path)
  106. {
  107. romfs_inode_t level, i = start, i_in;
  108. const char *p, *cp;
  109. uint32_t next_be;
  110. if (start == (romfs_inode_t)romfs)
  111. i = skip_and_pad((romfs_inode_t)romfs);
  112. level = i;
  113. while (i != (romfs_inode_t)romfs) {
  114. const char *n = ((const char *)i) + sizeof(*i);
  115. p = path;
  116. i_in = i;
  117. set_cache(i, sizeof(*i));
  118. next_be = ci->next;
  119. cp = (const char *)cache;
  120. set_cache((romfs_inode_t)n, RFS_STRING_MAX);
  121. while (*p && *p != '/' && *cp && *p == *cp &&
  122. (p - path) < RFS_STRING_MAX) {
  123. p++;
  124. n++;
  125. cp++;
  126. }
  127. while (*p == '/' && p[1] == '/')
  128. p++;
  129. if (!*cp && (!*p || *p == '/') &&
  130. (untohl(next_be) & 7) == RFST_HARDLINK) {
  131. set_cache(i, sizeof(*i));
  132. return (romfs_inode_t)
  133. ((const uint8_t *)romfs +
  134. (untohl(ci->dir_start) & ~15));
  135. }
  136. if (!*p && !*cp) {
  137. set_cache(i, sizeof(*i));
  138. if ((untohl(ci->next) & 7) == RFST_SYMLINK) {
  139. i = romfs_symlink(romfs, level, i);
  140. continue;
  141. }
  142. return i;
  143. }
  144. if (!*p && *cp == '/')
  145. return NULL;
  146. while (*p == '/' && p[1] == '/')
  147. p++;
  148. if (*p == '/' && !*cp) {
  149. set_cache(i, sizeof(*i));
  150. switch (untohl(ci->next) & 7) {
  151. case RFST_SYMLINK:
  152. i = romfs_symlink(romfs, level, i);
  153. if (!i)
  154. return NULL;
  155. i = dir_link(romfs, i);
  156. while (*path != '/' && *path)
  157. path++;
  158. if (!*path)
  159. return NULL;
  160. path++;
  161. continue;
  162. case RFST_DIR:
  163. path = p + 1;
  164. i = dir_link(romfs, i);
  165. break;
  166. default:
  167. path = p + 1;
  168. i = skip_and_pad(i);
  169. break;
  170. }
  171. level = i;
  172. continue;
  173. }
  174. set_cache(i, sizeof(*i));
  175. if (!(untohl(ci->next) & ~15))
  176. return NULL;
  177. i = (romfs_inode_t)((const uint8_t *)romfs +
  178. (untohl(ci->next) & ~15));
  179. if (i == i_in)
  180. return NULL;
  181. }
  182. return NULL;
  183. }
  184. const void *
  185. romfs_get_info(romfs_t romfs, const char *path, size_t *len, size_t *csum)
  186. {
  187. romfs_inode_t i;
  188. if (*path == '/')
  189. path++;
  190. i = romfs_lookup(romfs, (romfs_inode_t)romfs, path);
  191. if (!i)
  192. return NULL;
  193. set_cache(i, sizeof(*i));
  194. *len = untohl(ci->size);
  195. if (csum)
  196. *csum = untohl(ci->checksum);
  197. return (void *)skip_and_pad(i);
  198. }