esp32: move helper code into lws
[platform/upstream/libwebsockets.git] / lib / romfs.c
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
34 #include <string.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 #include "romfs.h"
38 #include "esp_spi_flash.h"
39
40 #define RFS_STRING_MAX 64
41
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
46 static void
47 set_cache(romfs_inode_t inode, size_t len)
48 {
49         spi_flash_read((uint32_t)inode, cache, len);
50 }
51
52 static uint32_t
53 ntohl(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
63 static int
64 plus_padding(const uint8_t *s)
65 {
66         int n;
67        
68         set_cache((romfs_inode_t)s, RFS_STRING_MAX);
69         n = strlen((const char *)cache);
70
71         if (!(n & 15))
72                 n += 0x10;
73
74         return (n + 15) & ~15;
75 }
76
77 static romfs_inode_t
78 skip_and_pad(romfs_inode_t ri)
79 {
80         const uint8_t *p = ((const uint8_t *)ri) + sizeof(*ri);
81
82         return (romfs_inode_t)(p + plus_padding(p));
83 }
84
85 size_t
86 romfs_mount_check(romfs_t romfs)
87 {
88         set_cache((romfs_inode_t)romfs, sizeof(*romfs));
89
90         if (cr->magic1 != 0x6d6f722d ||
91             cr->magic2 != 0x2d736631)
92                 return 0;
93
94         return ntohl(cr->size);
95 }
96
97 static romfs_inode_t
98 romfs_symlink(romfs_t romfs, romfs_inode_t level, romfs_inode_t i)
99 {
100         const char *p = (const char *)skip_and_pad(i);
101
102         if (*p == '/') {
103                 level = skip_and_pad((romfs_inode_t)romfs);
104                 p++;
105         }
106
107         return romfs_lookup(romfs, level, p);
108 }
109
110 static romfs_inode_t
111 dir_link(romfs_t romfs, romfs_inode_t i)
112 {
113         set_cache(i, sizeof(*i));
114         return (romfs_inode_t)((const uint8_t *)romfs +
115                                                 ntohl(ci->dir_start));
116 }
117
118 static romfs_inode_t
119 romfs_lookup(romfs_t romfs, romfs_inode_t start, const char *path)
120 {
121         romfs_inode_t level, i = start;
122         const char *p, *n, *cp;
123         uint32_t next_be;
124
125         if (start == (romfs_inode_t)romfs)
126                 i = skip_and_pad((romfs_inode_t)romfs);
127         level = i;
128         while (i != (romfs_inode_t)romfs) {
129                 p = path;
130                 n = ((const char *)i) + sizeof(*i);
131
132                 set_cache(i, sizeof(*i));
133                 next_be = ci->next;
134
135                 cp = (const char *)cache;
136                 set_cache((romfs_inode_t)n, RFS_STRING_MAX);
137
138                 while (*p && *p != '/' && *cp && *p == *cp) {
139                         p++;
140                         n++;
141                         cp++;
142                 }
143
144                 if (!*cp && (!*p || *p == '/') &&
145                     (ntohl(next_be) & 7) == RFST_HARDLINK) {
146                         set_cache(i, sizeof(*i));
147                         return (romfs_inode_t)
148                                ((const uint8_t *)romfs +
149                                 (ntohl(ci->dir_start) & ~15));
150                 }
151
152                 if (!*p && !*cp) {
153                         set_cache(i, sizeof(*i));
154                         if ((ntohl(ci->next) & 7) == RFST_SYMLINK) {
155                                 i = romfs_symlink(romfs, level, i);
156                                 continue;
157                         }
158                         return i;
159                 }
160
161                 if (*p == '/' && !*cp) {
162                         set_cache(i, sizeof(*i));
163                         switch (ntohl(ci->next) & 7) {
164                         case RFST_SYMLINK:
165                                 i = romfs_symlink(romfs, level, i);
166                                 if (!i)
167                                         return NULL;
168                                 i = dir_link(romfs, i);
169                                 while (*path != '/' && *path)
170                                         path++;
171                                 if (!*path)
172                                         return NULL;
173                                 path++;
174                                 continue;
175                         case RFST_DIR:
176                                 path = p + 1;
177                                 i = dir_link(romfs, i);
178                                 break;
179                         default:
180                                 path = p + 1;
181                                 i = skip_and_pad(i);
182                                 break;
183                         }
184                         level = i;
185                         continue;
186                 }
187
188                 set_cache(i, sizeof(*i));
189                 if (!(ntohl(ci->next) & ~15))
190                         return NULL;
191
192                 i = (romfs_inode_t)((const uint8_t *)romfs +
193                                     (ntohl(ci->next) & ~15));
194         }
195
196         return NULL;
197 }
198
199 const void *
200 romfs_get_info(romfs_t romfs, const char *path, size_t *len)
201 {
202         romfs_inode_t i;
203        
204         if (*path == '/')
205                 path++;
206
207         i = romfs_lookup(romfs, (romfs_inode_t)romfs, path);
208
209         if (!i)
210                 return NULL;
211
212         set_cache(i, sizeof(*i));
213         *len = ntohl(ci->size);
214
215         return (void *)skip_and_pad(i);
216 }