2 * Copyright (C) 2017 National Institute of Advanced Industrial Science
3 * and Technology (AIST)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
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.
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.
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.
38 #include "esp_spi_flash.h"
40 #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;
47 set_cache(romfs_inode_t inode, size_t len)
49 spi_flash_read((uint32_t)inode, cache, len);
53 ntohl(const u32_be_t be)
55 return ((be >> 24) & 0xff) |
56 ((be >> 16) & 0xff) << 8 |
57 ((be >> 8) & 0xff) << 16 |
61 romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path);
64 plus_padding(const uint8_t *s)
68 set_cache((romfs_inode_t)s, RFS_STRING_MAX);
69 n = strlen((const char *)cache);
74 return (n + 15) & ~15;
78 skip_and_pad(romfs_inode_t ri)
80 const uint8_t *p = ((const uint8_t *)ri) + sizeof(*ri);
82 return (romfs_inode_t)(p + plus_padding(p));
86 romfs_mount_check(romfs_t romfs)
88 set_cache((romfs_inode_t)romfs, sizeof(*romfs));
90 if (cr->magic1 != 0x6d6f722d ||
91 cr->magic2 != 0x2d736631)
94 return ntohl(cr->size);
98 romfs_symlink(romfs_t romfs, romfs_inode_t level, romfs_inode_t i)
100 const char *p = (const char *)skip_and_pad(i);
103 level = skip_and_pad((romfs_inode_t)romfs);
107 return romfs_lookup(romfs, level, p);
111 dir_link(romfs_t romfs, romfs_inode_t i)
113 set_cache(i, sizeof(*i));
114 return (romfs_inode_t)((const uint8_t *)romfs +
115 ntohl(ci->dir_start));
119 romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path)
121 romfs_inode_t level, i = start, i_in;
122 const char *p, *n, *cp;
125 if (start == (romfs_inode_t)romfs)
126 i = skip_and_pad((romfs_inode_t)romfs);
128 while (i != (romfs_inode_t)romfs) {
130 n = ((const char *)i) + sizeof(*i);
133 set_cache(i, sizeof(*i));
136 cp = (const char *)cache;
137 set_cache((romfs_inode_t)n, RFS_STRING_MAX);
139 while (*p && *p != '/' && *cp && *p == *cp && (p - path) < RFS_STRING_MAX) {
145 if (!*cp && (!*p || *p == '/') &&
146 (ntohl(next_be) & 7) == RFST_HARDLINK) {
147 set_cache(i, sizeof(*i));
148 return (romfs_inode_t)
149 ((const uint8_t *)romfs +
150 (ntohl(ci->dir_start) & ~15));
154 set_cache(i, sizeof(*i));
155 if ((ntohl(ci->next) & 7) == RFST_SYMLINK) {
156 i = romfs_symlink(romfs, level, i);
162 if (!*p && *cp == '/')
165 if (*p == '/' && !*cp) {
166 set_cache(i, sizeof(*i));
167 switch (ntohl(ci->next) & 7) {
169 i = romfs_symlink(romfs, level, i);
172 i = dir_link(romfs, i);
173 while (*path != '/' && *path)
181 i = dir_link(romfs, i);
192 set_cache(i, sizeof(*i));
193 if (!(ntohl(ci->next) & ~15))
196 i = (romfs_inode_t)((const uint8_t *)romfs +
197 (ntohl(ci->next) & ~15));
206 romfs_get_info(romfs_t romfs, const char *path, size_t *len, size_t *csum)
213 i = romfs_lookup(romfs, (romfs_inode_t)romfs, path);
218 set_cache(i, sizeof(*i));
219 *len = ntohl(ci->size);
221 *csum = ntohl(ci->checksum);
223 return (void *)skip_and_pad(i);