1 /* RomFS storage access routines
3 * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/mtd/super.h>
14 #include <linux/buffer_head.h>
17 #if !defined(CONFIG_ROMFS_ON_MTD) && !defined(CONFIG_ROMFS_ON_BLOCK)
18 #error no ROMFS backing store interface configured
21 #ifdef CONFIG_ROMFS_ON_MTD
22 #define ROMFS_MTD_READ(sb, ...) ((sb)->s_mtd->read((sb)->s_mtd, ##__VA_ARGS__))
25 * read data from an romfs image on an MTD device
27 static int romfs_mtd_read(struct super_block *sb, unsigned long pos,
28 void *buf, size_t buflen)
33 ret = ROMFS_MTD_READ(sb, pos, buflen, &rlen, buf);
34 return (ret < 0 || rlen != buflen) ? -EIO : 0;
38 * determine the length of a string in a romfs image on an MTD device
40 static ssize_t romfs_mtd_strnlen(struct super_block *sb,
41 unsigned long pos, size_t maxlen)
49 /* scan the string up to 16 bytes at a time */
51 segment = min_t(size_t, maxlen, 16);
52 ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
55 p = memchr(buf, 0, len);
67 * compare a string to one in a romfs image on MTD
68 * - return 1 if matched, 0 if differ, -ve if error
70 static int romfs_mtd_strcmp(struct super_block *sb, unsigned long pos,
71 const char *str, size_t size)
77 /* scan the string up to 16 bytes at a time, and attempt to grab the
78 * trailing NUL whilst we're at it */
82 segment = min_t(size_t, size + 1, 17);
83 ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
87 if (memcmp(buf, str, len) != 0)
95 /* check the trailing NUL was */
101 #endif /* CONFIG_ROMFS_ON_MTD */
103 #ifdef CONFIG_ROMFS_ON_BLOCK
105 * read data from an romfs image on a block device
107 static int romfs_blk_read(struct super_block *sb, unsigned long pos,
108 void *buf, size_t buflen)
110 struct buffer_head *bh;
111 unsigned long offset;
114 /* copy the string up to blocksize bytes at a time */
116 offset = pos & (ROMBSIZE - 1);
117 segment = min_t(size_t, buflen, ROMBSIZE - offset);
118 bh = sb_bread(sb, pos >> ROMBSBITS);
121 memcpy(buf, bh->b_data + offset, segment);
131 * determine the length of a string in romfs on a block device
133 static ssize_t romfs_blk_strnlen(struct super_block *sb,
134 unsigned long pos, size_t limit)
136 struct buffer_head *bh;
137 unsigned long offset;
142 /* scan the string up to blocksize bytes at a time */
144 offset = pos & (ROMBSIZE - 1);
145 segment = min_t(size_t, limit, ROMBSIZE - offset);
146 bh = sb_bread(sb, pos >> ROMBSBITS);
149 buf = bh->b_data + offset;
150 p = memchr(buf, 0, segment);
153 return n + (p - buf);
163 * compare a string to one in a romfs image on a block device
164 * - return 1 if matched, 0 if differ, -ve if error
166 static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos,
167 const char *str, size_t size)
169 struct buffer_head *bh;
170 unsigned long offset;
172 bool matched, terminated = false;
174 /* compare string up to a block at a time */
176 offset = pos & (ROMBSIZE - 1);
177 segment = min_t(size_t, size, ROMBSIZE - offset);
178 bh = sb_bread(sb, pos >> ROMBSBITS);
181 matched = (memcmp(bh->b_data + offset, str, segment) == 0);
186 if (matched && size == 0 && offset + segment < ROMBSIZE) {
187 if (!bh->b_data[offset + segment])
198 /* the terminating NUL must be on the first byte of the next
200 BUG_ON((pos & (ROMBSIZE - 1)) != 0);
201 bh = sb_bread(sb, pos >> ROMBSBITS);
204 matched = !bh->b_data[0];
212 #endif /* CONFIG_ROMFS_ON_BLOCK */
215 * read data from the romfs image
217 int romfs_dev_read(struct super_block *sb, unsigned long pos,
218 void *buf, size_t buflen)
222 limit = romfs_maxsize(sb);
225 if (buflen > limit - pos)
226 buflen = limit - pos;
228 #ifdef CONFIG_ROMFS_ON_MTD
230 return romfs_mtd_read(sb, pos, buf, buflen);
232 #ifdef CONFIG_ROMFS_ON_BLOCK
234 return romfs_blk_read(sb, pos, buf, buflen);
240 * determine the length of a string in romfs
242 ssize_t romfs_dev_strnlen(struct super_block *sb,
243 unsigned long pos, size_t maxlen)
247 limit = romfs_maxsize(sb);
250 if (maxlen > limit - pos)
251 maxlen = limit - pos;
253 #ifdef CONFIG_ROMFS_ON_MTD
255 return romfs_mtd_strnlen(sb, pos, limit);
257 #ifdef CONFIG_ROMFS_ON_BLOCK
259 return romfs_blk_strnlen(sb, pos, limit);
265 * compare a string to one in romfs
266 * - the string to be compared to, str, may not be NUL-terminated; instead the
267 * string is of the specified size
268 * - return 1 if matched, 0 if differ, -ve if error
270 int romfs_dev_strcmp(struct super_block *sb, unsigned long pos,
271 const char *str, size_t size)
275 limit = romfs_maxsize(sb);
278 if (size > ROMFS_MAXFN)
279 return -ENAMETOOLONG;
280 if (size + 1 > limit - pos)
283 #ifdef CONFIG_ROMFS_ON_MTD
285 return romfs_mtd_strcmp(sb, pos, str, size);
287 #ifdef CONFIG_ROMFS_ON_BLOCK
289 return romfs_blk_strcmp(sb, pos, str, size);