1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* RomFS storage access routines
4 * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/mtd/super.h>
10 #include <linux/buffer_head.h>
13 #if !defined(CONFIG_ROMFS_ON_MTD) && !defined(CONFIG_ROMFS_ON_BLOCK)
14 #error no ROMFS backing store interface configured
17 #ifdef CONFIG_ROMFS_ON_MTD
18 #define ROMFS_MTD_READ(sb, ...) mtd_read((sb)->s_mtd, ##__VA_ARGS__)
21 * read data from an romfs image on an MTD device
23 static int romfs_mtd_read(struct super_block *sb, unsigned long pos,
24 void *buf, size_t buflen)
29 ret = ROMFS_MTD_READ(sb, pos, buflen, &rlen, buf);
30 return (ret < 0 || rlen != buflen) ? -EIO : 0;
34 * determine the length of a string in a romfs image on an MTD device
36 static ssize_t romfs_mtd_strnlen(struct super_block *sb,
37 unsigned long pos, size_t maxlen)
45 /* scan the string up to 16 bytes at a time */
47 segment = min_t(size_t, maxlen, 16);
48 ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
51 p = memchr(buf, 0, len);
63 * compare a string to one in a romfs image on MTD
64 * - return 1 if matched, 0 if differ, -ve if error
66 static int romfs_mtd_strcmp(struct super_block *sb, unsigned long pos,
67 const char *str, size_t size)
73 /* scan the string up to 16 bytes at a time, and attempt to grab the
74 * trailing NUL whilst we're at it */
78 segment = min_t(size_t, size + 1, 17);
79 ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
83 if (memcmp(buf, str, len) != 0)
91 /* check the trailing NUL was */
97 #endif /* CONFIG_ROMFS_ON_MTD */
99 #ifdef CONFIG_ROMFS_ON_BLOCK
101 * read data from an romfs image on a block device
103 static int romfs_blk_read(struct super_block *sb, unsigned long pos,
104 void *buf, size_t buflen)
106 struct buffer_head *bh;
107 unsigned long offset;
110 /* copy the string up to blocksize bytes at a time */
112 offset = pos & (ROMBSIZE - 1);
113 segment = min_t(size_t, buflen, ROMBSIZE - offset);
114 bh = sb_bread(sb, pos >> ROMBSBITS);
117 memcpy(buf, bh->b_data + offset, segment);
128 * determine the length of a string in romfs on a block device
130 static ssize_t romfs_blk_strnlen(struct super_block *sb,
131 unsigned long pos, size_t limit)
133 struct buffer_head *bh;
134 unsigned long offset;
139 /* scan the string up to blocksize bytes at a time */
141 offset = pos & (ROMBSIZE - 1);
142 segment = min_t(size_t, limit, ROMBSIZE - offset);
143 bh = sb_bread(sb, pos >> ROMBSBITS);
146 buf = bh->b_data + offset;
147 p = memchr(buf, 0, segment);
150 return n + (p - buf);
160 * compare a string to one in a romfs image on a block device
161 * - return 1 if matched, 0 if differ, -ve if error
163 static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos,
164 const char *str, size_t size)
166 struct buffer_head *bh;
167 unsigned long offset;
169 bool matched, terminated = false;
171 /* compare string up to a block at a time */
173 offset = pos & (ROMBSIZE - 1);
174 segment = min_t(size_t, size, ROMBSIZE - offset);
175 bh = sb_bread(sb, pos >> ROMBSBITS);
178 matched = (memcmp(bh->b_data + offset, str, segment) == 0);
183 if (matched && size == 0 && offset + segment < ROMBSIZE) {
184 if (!bh->b_data[offset + segment])
195 /* the terminating NUL must be on the first byte of the next
197 BUG_ON((pos & (ROMBSIZE - 1)) != 0);
198 bh = sb_bread(sb, pos >> ROMBSBITS);
201 matched = !bh->b_data[0];
209 #endif /* CONFIG_ROMFS_ON_BLOCK */
212 * read data from the romfs image
214 int romfs_dev_read(struct super_block *sb, unsigned long pos,
215 void *buf, size_t buflen)
219 limit = romfs_maxsize(sb);
220 if (pos >= limit || buflen > limit - pos)
223 #ifdef CONFIG_ROMFS_ON_MTD
225 return romfs_mtd_read(sb, pos, buf, buflen);
227 #ifdef CONFIG_ROMFS_ON_BLOCK
229 return romfs_blk_read(sb, pos, buf, buflen);
235 * determine the length of a string in romfs
237 ssize_t romfs_dev_strnlen(struct super_block *sb,
238 unsigned long pos, size_t maxlen)
242 limit = romfs_maxsize(sb);
245 if (maxlen > limit - pos)
246 maxlen = limit - pos;
248 #ifdef CONFIG_ROMFS_ON_MTD
250 return romfs_mtd_strnlen(sb, pos, maxlen);
252 #ifdef CONFIG_ROMFS_ON_BLOCK
254 return romfs_blk_strnlen(sb, pos, maxlen);
260 * compare a string to one in romfs
261 * - the string to be compared to, str, may not be NUL-terminated; instead the
262 * string is of the specified size
263 * - return 1 if matched, 0 if differ, -ve if error
265 int romfs_dev_strcmp(struct super_block *sb, unsigned long pos,
266 const char *str, size_t size)
270 limit = romfs_maxsize(sb);
273 if (size > ROMFS_MAXFN)
274 return -ENAMETOOLONG;
275 if (size + 1 > limit - pos)
278 #ifdef CONFIG_ROMFS_ON_MTD
280 return romfs_mtd_strcmp(sb, pos, str, size);
282 #ifdef CONFIG_ROMFS_ON_BLOCK
284 return romfs_blk_strcmp(sb, pos, str, size);