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_strncmp(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 */
79 segment = min_t(size_t, size, 16);
80 ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
83 if (memcmp(buf, str, len) != 0)
92 #endif /* CONFIG_ROMFS_ON_MTD */
94 #ifdef CONFIG_ROMFS_ON_BLOCK
96 * read data from an romfs image on a block device
98 static int romfs_blk_read(struct super_block *sb, unsigned long pos,
99 void *buf, size_t buflen)
101 struct buffer_head *bh;
102 unsigned long offset;
105 /* copy the string up to blocksize bytes at a time */
107 offset = pos & (ROMBSIZE - 1);
108 segment = min_t(size_t, buflen, ROMBSIZE - offset);
109 bh = sb_bread(sb, pos >> ROMBSBITS);
112 memcpy(buf, bh->b_data + offset, segment);
122 * determine the length of a string in romfs on a block device
124 static ssize_t romfs_blk_strnlen(struct super_block *sb,
125 unsigned long pos, size_t limit)
127 struct buffer_head *bh;
128 unsigned long offset;
133 /* scan the string up to blocksize bytes at a time */
135 offset = pos & (ROMBSIZE - 1);
136 segment = min_t(size_t, limit, ROMBSIZE - offset);
137 bh = sb_bread(sb, pos >> ROMBSBITS);
140 buf = bh->b_data + offset;
141 p = memchr(buf, 0, segment);
144 return n + (p - buf);
154 * compare a string to one in a romfs image on a block device
155 * - return 1 if matched, 0 if differ, -ve if error
157 static int romfs_blk_strncmp(struct super_block *sb, unsigned long pos,
158 const char *str, size_t size)
160 struct buffer_head *bh;
161 unsigned long offset;
165 /* scan the string up to 16 bytes at a time */
167 offset = pos & (ROMBSIZE - 1);
168 segment = min_t(size_t, size, ROMBSIZE - offset);
169 bh = sb_bread(sb, pos >> ROMBSBITS);
172 x = (memcmp(bh->b_data + offset, str, segment) != 0);
183 #endif /* CONFIG_ROMFS_ON_BLOCK */
186 * read data from the romfs image
188 int romfs_dev_read(struct super_block *sb, unsigned long pos,
189 void *buf, size_t buflen)
193 limit = romfs_maxsize(sb);
196 if (buflen > limit - pos)
197 buflen = limit - pos;
199 #ifdef CONFIG_ROMFS_ON_MTD
201 return romfs_mtd_read(sb, pos, buf, buflen);
203 #ifdef CONFIG_ROMFS_ON_BLOCK
205 return romfs_blk_read(sb, pos, buf, buflen);
211 * determine the length of a string in romfs
213 ssize_t romfs_dev_strnlen(struct super_block *sb,
214 unsigned long pos, size_t maxlen)
218 limit = romfs_maxsize(sb);
221 if (maxlen > limit - pos)
222 maxlen = limit - pos;
224 #ifdef CONFIG_ROMFS_ON_MTD
226 return romfs_mtd_strnlen(sb, pos, limit);
228 #ifdef CONFIG_ROMFS_ON_BLOCK
230 return romfs_blk_strnlen(sb, pos, limit);
236 * compare a string to one in romfs
237 * - return 1 if matched, 0 if differ, -ve if error
239 int romfs_dev_strncmp(struct super_block *sb, unsigned long pos,
240 const char *str, size_t size)
244 limit = romfs_maxsize(sb);
247 if (size > ROMFS_MAXFN)
248 return -ENAMETOOLONG;
249 if (size > limit - pos)
252 #ifdef CONFIG_ROMFS_ON_MTD
254 return romfs_mtd_strncmp(sb, pos, str, size);
256 #ifdef CONFIG_ROMFS_ON_BLOCK
258 return romfs_blk_strncmp(sb, pos, str, size);