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
);
132 * determine the length of a string in romfs on a block device
134 static ssize_t
romfs_blk_strnlen(struct super_block
*sb
,
135 unsigned long pos
, size_t limit
)
137 struct buffer_head
*bh
;
138 unsigned long offset
;
143 /* scan the string up to blocksize bytes at a time */
145 offset
= pos
& (ROMBSIZE
- 1);
146 segment
= min_t(size_t, limit
, ROMBSIZE
- offset
);
147 bh
= sb_bread(sb
, pos
>> ROMBSBITS
);
150 buf
= bh
->b_data
+ offset
;
151 p
= memchr(buf
, 0, segment
);
154 return n
+ (p
- buf
);
164 * compare a string to one in a romfs image on a block device
165 * - return 1 if matched, 0 if differ, -ve if error
167 static int romfs_blk_strcmp(struct super_block
*sb
, unsigned long pos
,
168 const char *str
, size_t size
)
170 struct buffer_head
*bh
;
171 unsigned long offset
;
173 bool matched
, terminated
= false;
175 /* compare string up to a block at a time */
177 offset
= pos
& (ROMBSIZE
- 1);
178 segment
= min_t(size_t, size
, ROMBSIZE
- offset
);
179 bh
= sb_bread(sb
, pos
>> ROMBSBITS
);
182 matched
= (memcmp(bh
->b_data
+ offset
, str
, segment
) == 0);
187 if (matched
&& size
== 0 && offset
+ segment
< ROMBSIZE
) {
188 if (!bh
->b_data
[offset
+ segment
])
199 /* the terminating NUL must be on the first byte of the next
201 BUG_ON((pos
& (ROMBSIZE
- 1)) != 0);
202 bh
= sb_bread(sb
, pos
>> ROMBSBITS
);
205 matched
= !bh
->b_data
[0];
213 #endif /* CONFIG_ROMFS_ON_BLOCK */
216 * read data from the romfs image
218 int romfs_dev_read(struct super_block
*sb
, unsigned long pos
,
219 void *buf
, size_t buflen
)
223 limit
= romfs_maxsize(sb
);
226 if (buflen
> limit
- pos
)
227 buflen
= limit
- pos
;
229 #ifdef CONFIG_ROMFS_ON_MTD
231 return romfs_mtd_read(sb
, pos
, buf
, buflen
);
233 #ifdef CONFIG_ROMFS_ON_BLOCK
235 return romfs_blk_read(sb
, pos
, buf
, buflen
);
241 * determine the length of a string in romfs
243 ssize_t
romfs_dev_strnlen(struct super_block
*sb
,
244 unsigned long pos
, size_t maxlen
)
248 limit
= romfs_maxsize(sb
);
251 if (maxlen
> limit
- pos
)
252 maxlen
= limit
- pos
;
254 #ifdef CONFIG_ROMFS_ON_MTD
256 return romfs_mtd_strnlen(sb
, pos
, limit
);
258 #ifdef CONFIG_ROMFS_ON_BLOCK
260 return romfs_blk_strnlen(sb
, pos
, limit
);
266 * compare a string to one in romfs
267 * - the string to be compared to, str, may not be NUL-terminated; instead the
268 * string is of the specified size
269 * - return 1 if matched, 0 if differ, -ve if error
271 int romfs_dev_strcmp(struct super_block
*sb
, unsigned long pos
,
272 const char *str
, size_t size
)
276 limit
= romfs_maxsize(sb
);
279 if (size
> ROMFS_MAXFN
)
280 return -ENAMETOOLONG
;
281 if (size
+ 1 > limit
- pos
)
284 #ifdef CONFIG_ROMFS_ON_MTD
286 return romfs_mtd_strcmp(sb
, pos
, str
, size
);
288 #ifdef CONFIG_ROMFS_ON_BLOCK
290 return romfs_blk_strcmp(sb
, pos
, str
, size
);