romfs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <string.h>
  34. #include <stdint.h>
  35. #include <stdio.h>
  36. #include "romfs.h"
  37. #include "esp_spi_flash.h"
  38. #define RFS_STRING_MAX 64
  39. static u32_be_t cache[(RFS_STRING_MAX + 32) / 4];
  40. static romfs_inode_t ci = (romfs_inode_t)cache;
  41. static romfs_t cr = (romfs_t)cache;
  42. static void
  43. set_cache(romfs_inode_t inode, size_t len)
  44. {
  45. spi_flash_read((uint32_t)inode, cache, len);
  46. }
  47. static uint32_t
  48. ntohl(const u32_be_t be)
  49. {
  50. return ((be >> 24) & 0xff) |
  51. ((be >> 16) & 0xff) << 8 |
  52. ((be >> 8) & 0xff) << 16 |
  53. (be & 0xff) << 24;
  54. }
  55. static romfs_inode_t
  56. romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path);
  57. static int
  58. plus_padding(const uint8_t *s)
  59. {
  60. int n;
  61. set_cache((romfs_inode_t)s, RFS_STRING_MAX);
  62. n = strlen((const char *)cache);
  63. if (!(n & 15))
  64. n += 0x10;
  65. return (n + 15) & ~15;
  66. }
  67. static romfs_inode_t
  68. skip_and_pad(romfs_inode_t ri)
  69. {
  70. const uint8_t *p = ((const uint8_t *)ri) + sizeof(*ri);
  71. return (romfs_inode_t)(p + plus_padding(p));
  72. }
  73. size_t
  74. romfs_mount_check(romfs_t romfs)
  75. {
  76. set_cache((romfs_inode_t)romfs, sizeof(*romfs));
  77. if (cr->magic1 != 0x6d6f722d ||
  78. cr->magic2 != 0x2d736631)
  79. return 0;
  80. return ntohl(cr->size);
  81. }
  82. static romfs_inode_t
  83. romfs_symlink(romfs_t romfs, romfs_inode_t level, romfs_inode_t i)
  84. {
  85. const char *p = (const char *)skip_and_pad(i);
  86. if (*p == '/') {
  87. level = skip_and_pad((romfs_inode_t)romfs);
  88. p++;
  89. }
  90. return romfs_lookup(romfs, level, p);
  91. }
  92. static romfs_inode_t
  93. dir_link(romfs_t romfs, romfs_inode_t i)
  94. {
  95. set_cache(i, sizeof(*i));
  96. return (romfs_inode_t)((const uint8_t *)romfs +
  97. ntohl(ci->dir_start));
  98. }
  99. static romfs_inode_t
  100. romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path)
  101. {
  102. romfs_inode_t level, i = start;
  103. const char *p, *n, *cp;
  104. uint32_t next_be;
  105. if (start == (romfs_inode_t)romfs)
  106. i = skip_and_pad((romfs_inode_t)romfs);
  107. level = i;
  108. while (i != (romfs_inode_t)romfs) {
  109. p = path;
  110. n = ((const char *)i) + sizeof(*i);
  111. set_cache(i, sizeof(*i));
  112. next_be = ci->next;
  113. cp = (const char *)cache;
  114. set_cache((romfs_inode_t)n, RFS_STRING_MAX);
  115. while (*p && *p != '/' && *cp && *p == *cp) {
  116. p++;
  117. n++;
  118. cp++;
  119. }
  120. if (!*cp && (!*p || *p == '/') &&
  121. (ntohl(next_be) & 7) == RFST_HARDLINK) {
  122. set_cache(i, sizeof(*i));
  123. return (romfs_inode_t)
  124. ((const uint8_t *)romfs +
  125. (ntohl(ci->dir_start) & ~15));
  126. }
  127. if (!*p && !*cp) {
  128. set_cache(i, sizeof(*i));
  129. if ((ntohl(ci->next) & 7) == RFST_SYMLINK) {
  130. i = romfs_symlink(romfs, level, i);
  131. continue;
  132. }
  133. return i;
  134. }
  135. if (*p == '/' && !*cp) {
  136. set_cache(i, sizeof(*i));
  137. switch (ntohl(ci->next) & 7) {
  138. case RFST_SYMLINK:
  139. i = romfs_symlink(romfs, level, i);
  140. if (!i)
  141. return NULL;
  142. i = dir_link(romfs, i);
  143. while (*path != '/' && *path)
  144. path++;
  145. if (!*path)
  146. return NULL;
  147. path++;
  148. continue;
  149. case RFST_DIR:
  150. path = p + 1;
  151. i = dir_link(romfs, i);
  152. break;
  153. default:
  154. path = p + 1;
  155. i = skip_and_pad(i);
  156. break;
  157. }
  158. level = i;
  159. continue;
  160. }
  161. set_cache(i, sizeof(*i));
  162. if (!(ntohl(ci->next) & ~15))
  163. return NULL;
  164. i = (romfs_inode_t)((const uint8_t *)romfs +
  165. (ntohl(ci->next) & ~15));
  166. }
  167. return NULL;
  168. }
  169. const void *
  170. romfs_get_info(romfs_t romfs, const char *path, size_t *len)
  171. {
  172. romfs_inode_t i;
  173. if (*path == '/')
  174. path++;
  175. i = romfs_lookup(romfs, (romfs_inode_t)romfs, path);
  176. if (!i)
  177. return NULL;
  178. set_cache(i, sizeof(*i));
  179. *len = ntohl(ci->size);
  180. return (void *)skip_and_pad(i);
  181. }