Subject: Eliminated 'unused variable' compiler warning generated with -DLWS_WITH_NO_L...
[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 96
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, i_in;
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                 i_in = i;
132
133                 set_cache(i, sizeof(*i));
134                 next_be = ci->next;
135
136                 cp = (const char *)cache;
137                 set_cache((romfs_inode_t)n, RFS_STRING_MAX);
138
139                 while (*p && *p != '/' && *cp && *p == *cp && (p - path) < RFS_STRING_MAX) {
140                         p++;
141                         n++;
142                         cp++;
143                 }
144
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));
151                 }
152
153                 if (!*p && !*cp) {
154                         set_cache(i, sizeof(*i));
155                         if ((ntohl(ci->next) & 7) == RFST_SYMLINK) {
156                                 i = romfs_symlink(romfs, level, i);
157                                 continue;
158                         }
159                         return i;
160                 }
161
162                 if (!*p && *cp == '/')
163                         return NULL;
164
165                 if (*p == '/' && !*cp) {
166                         set_cache(i, sizeof(*i));
167                         switch (ntohl(ci->next) & 7) {
168                         case RFST_SYMLINK:
169                                 i = romfs_symlink(romfs, level, i);
170                                 if (!i)
171                                         return NULL;
172                                 i = dir_link(romfs, i);
173                                 while (*path != '/' && *path)
174                                         path++;
175                                 if (!*path)
176                                         return NULL;
177                                 path++;
178                                 continue;
179                         case RFST_DIR:
180                                 path = p + 1;
181                                 i = dir_link(romfs, i);
182                                 break;
183                         default:
184                                 path = p + 1;
185                                 i = skip_and_pad(i);
186                                 break;
187                         }
188                         level = i;
189                         continue;
190                 }
191
192                 set_cache(i, sizeof(*i));
193                 if (!(ntohl(ci->next) & ~15))
194                         return NULL;
195
196                 i = (romfs_inode_t)((const uint8_t *)romfs +
197                                     (ntohl(ci->next) & ~15));
198                 if (i == i_in)
199                         return NULL;
200         }
201
202         return NULL;
203 }
204
205 const void *
206 romfs_get_info(romfs_t romfs, const char *path, size_t *len, size_t *csum)
207 {
208         romfs_inode_t i;
209        
210         if (*path == '/')
211                 path++;
212
213         i = romfs_lookup(romfs, (romfs_inode_t)romfs, path);
214
215         if (!i)
216                 return NULL;
217
218         set_cache(i, sizeof(*i));
219         *len = ntohl(ci->size);
220         if (csum)
221                 *csum = ntohl(ci->checksum);
222
223         return (void *)skip_and_pad(i);
224 }