David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 1 | /* RomFS storage access routines |
| 2 | * |
| 3 | * Copyright © 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 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. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/fs.h> |
| 13 | #include <linux/mtd/super.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include "internal.h" |
| 16 | |
| 17 | #if !defined(CONFIG_ROMFS_ON_MTD) && !defined(CONFIG_ROMFS_ON_BLOCK) |
| 18 | #error no ROMFS backing store interface configured |
| 19 | #endif |
| 20 | |
| 21 | #ifdef CONFIG_ROMFS_ON_MTD |
| 22 | #define ROMFS_MTD_READ(sb, ...) ((sb)->s_mtd->read((sb)->s_mtd, ##__VA_ARGS__)) |
| 23 | |
| 24 | /* |
| 25 | * read data from an romfs image on an MTD device |
| 26 | */ |
| 27 | static int romfs_mtd_read(struct super_block *sb, unsigned long pos, |
| 28 | void *buf, size_t buflen) |
| 29 | { |
| 30 | size_t rlen; |
| 31 | int ret; |
| 32 | |
| 33 | ret = ROMFS_MTD_READ(sb, pos, buflen, &rlen, buf); |
| 34 | return (ret < 0 || rlen != buflen) ? -EIO : 0; |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * determine the length of a string in a romfs image on an MTD device |
| 39 | */ |
| 40 | static ssize_t romfs_mtd_strnlen(struct super_block *sb, |
| 41 | unsigned long pos, size_t maxlen) |
| 42 | { |
| 43 | ssize_t n = 0; |
| 44 | size_t segment; |
| 45 | u_char buf[16], *p; |
| 46 | size_t len; |
| 47 | int ret; |
| 48 | |
| 49 | /* scan the string up to 16 bytes at a time */ |
| 50 | while (maxlen > 0) { |
| 51 | segment = min_t(size_t, maxlen, 16); |
| 52 | ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf); |
| 53 | if (ret < 0) |
| 54 | return ret; |
| 55 | p = memchr(buf, 0, len); |
| 56 | if (p) |
| 57 | return n + (p - buf); |
| 58 | maxlen -= len; |
| 59 | pos += len; |
| 60 | n += len; |
| 61 | } |
| 62 | |
| 63 | return n; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * compare a string to one in a romfs image on MTD |
| 68 | * - return 1 if matched, 0 if differ, -ve if error |
| 69 | */ |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 70 | static int romfs_mtd_strcmp(struct super_block *sb, unsigned long pos, |
| 71 | const char *str, size_t size) |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 72 | { |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 73 | u_char buf[17]; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 74 | size_t len, segment; |
| 75 | int ret; |
| 76 | |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 77 | /* scan the string up to 16 bytes at a time, and attempt to grab the |
| 78 | * trailing NUL whilst we're at it */ |
| 79 | buf[0] = 0xff; |
| 80 | |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 81 | while (size > 0) { |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 82 | segment = min_t(size_t, size + 1, 17); |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 83 | ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf); |
| 84 | if (ret < 0) |
| 85 | return ret; |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 86 | len--; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 87 | if (memcmp(buf, str, len) != 0) |
| 88 | return 0; |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 89 | buf[0] = buf[len]; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 90 | size -= len; |
| 91 | pos += len; |
| 92 | str += len; |
| 93 | } |
| 94 | |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 95 | /* check the trailing NUL was */ |
| 96 | if (buf[0]) |
| 97 | return 0; |
| 98 | |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | #endif /* CONFIG_ROMFS_ON_MTD */ |
| 102 | |
| 103 | #ifdef CONFIG_ROMFS_ON_BLOCK |
| 104 | /* |
| 105 | * read data from an romfs image on a block device |
| 106 | */ |
| 107 | static int romfs_blk_read(struct super_block *sb, unsigned long pos, |
| 108 | void *buf, size_t buflen) |
| 109 | { |
| 110 | struct buffer_head *bh; |
| 111 | unsigned long offset; |
| 112 | size_t segment; |
| 113 | |
| 114 | /* copy the string up to blocksize bytes at a time */ |
| 115 | while (buflen > 0) { |
| 116 | offset = pos & (ROMBSIZE - 1); |
| 117 | segment = min_t(size_t, buflen, ROMBSIZE - offset); |
| 118 | bh = sb_bread(sb, pos >> ROMBSBITS); |
| 119 | if (!bh) |
| 120 | return -EIO; |
| 121 | memcpy(buf, bh->b_data + offset, segment); |
| 122 | brelse(bh); |
| 123 | buflen -= segment; |
| 124 | pos += segment; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * determine the length of a string in romfs on a block device |
| 132 | */ |
| 133 | static ssize_t romfs_blk_strnlen(struct super_block *sb, |
| 134 | unsigned long pos, size_t limit) |
| 135 | { |
| 136 | struct buffer_head *bh; |
| 137 | unsigned long offset; |
| 138 | ssize_t n = 0; |
| 139 | size_t segment; |
| 140 | u_char *buf, *p; |
| 141 | |
| 142 | /* scan the string up to blocksize bytes at a time */ |
| 143 | while (limit > 0) { |
| 144 | offset = pos & (ROMBSIZE - 1); |
| 145 | segment = min_t(size_t, limit, ROMBSIZE - offset); |
| 146 | bh = sb_bread(sb, pos >> ROMBSBITS); |
| 147 | if (!bh) |
| 148 | return -EIO; |
| 149 | buf = bh->b_data + offset; |
| 150 | p = memchr(buf, 0, segment); |
| 151 | brelse(bh); |
| 152 | if (p) |
| 153 | return n + (p - buf); |
| 154 | limit -= segment; |
| 155 | pos += segment; |
| 156 | n += segment; |
| 157 | } |
| 158 | |
| 159 | return n; |
| 160 | } |
| 161 | |
| 162 | /* |
| 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 |
| 165 | */ |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 166 | static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos, |
| 167 | const char *str, size_t size) |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 168 | { |
| 169 | struct buffer_head *bh; |
| 170 | unsigned long offset; |
| 171 | size_t segment; |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 172 | bool matched, terminated = false; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 173 | |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 174 | /* compare string up to a block at a time */ |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 175 | while (size > 0) { |
| 176 | offset = pos & (ROMBSIZE - 1); |
| 177 | segment = min_t(size_t, size, ROMBSIZE - offset); |
| 178 | bh = sb_bread(sb, pos >> ROMBSBITS); |
| 179 | if (!bh) |
| 180 | return -EIO; |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 181 | matched = (memcmp(bh->b_data + offset, str, segment) == 0); |
| 182 | |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 183 | size -= segment; |
| 184 | pos += segment; |
| 185 | str += segment; |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 186 | if (matched && size == 0 && offset + segment < ROMBSIZE) { |
| 187 | if (!bh->b_data[offset + segment]) |
| 188 | terminated = true; |
| 189 | else |
| 190 | matched = false; |
| 191 | } |
| 192 | brelse(bh); |
| 193 | if (!matched) |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | if (!terminated) { |
| 198 | /* the terminating NUL must be on the first byte of the next |
| 199 | * block */ |
| 200 | BUG_ON((pos & (ROMBSIZE - 1)) != 0); |
| 201 | bh = sb_bread(sb, pos >> ROMBSBITS); |
| 202 | if (!bh) |
| 203 | return -EIO; |
| 204 | matched = !bh->b_data[0]; |
| 205 | brelse(bh); |
| 206 | if (!matched) |
| 207 | return 0; |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | return 1; |
| 211 | } |
| 212 | #endif /* CONFIG_ROMFS_ON_BLOCK */ |
| 213 | |
| 214 | /* |
| 215 | * read data from the romfs image |
| 216 | */ |
| 217 | int romfs_dev_read(struct super_block *sb, unsigned long pos, |
| 218 | void *buf, size_t buflen) |
| 219 | { |
| 220 | size_t limit; |
| 221 | |
| 222 | limit = romfs_maxsize(sb); |
| 223 | if (pos >= limit) |
| 224 | return -EIO; |
| 225 | if (buflen > limit - pos) |
| 226 | buflen = limit - pos; |
| 227 | |
| 228 | #ifdef CONFIG_ROMFS_ON_MTD |
| 229 | if (sb->s_mtd) |
| 230 | return romfs_mtd_read(sb, pos, buf, buflen); |
| 231 | #endif |
| 232 | #ifdef CONFIG_ROMFS_ON_BLOCK |
| 233 | if (sb->s_bdev) |
| 234 | return romfs_blk_read(sb, pos, buf, buflen); |
| 235 | #endif |
| 236 | return -EIO; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * determine the length of a string in romfs |
| 241 | */ |
| 242 | ssize_t romfs_dev_strnlen(struct super_block *sb, |
| 243 | unsigned long pos, size_t maxlen) |
| 244 | { |
| 245 | size_t limit; |
| 246 | |
| 247 | limit = romfs_maxsize(sb); |
| 248 | if (pos >= limit) |
| 249 | return -EIO; |
| 250 | if (maxlen > limit - pos) |
| 251 | maxlen = limit - pos; |
| 252 | |
| 253 | #ifdef CONFIG_ROMFS_ON_MTD |
| 254 | if (sb->s_mtd) |
| 255 | return romfs_mtd_strnlen(sb, pos, limit); |
| 256 | #endif |
| 257 | #ifdef CONFIG_ROMFS_ON_BLOCK |
| 258 | if (sb->s_bdev) |
| 259 | return romfs_blk_strnlen(sb, pos, limit); |
| 260 | #endif |
| 261 | return -EIO; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * compare a string to one in romfs |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 266 | * - the string to be compared to, str, may not be NUL-terminated; instead the |
| 267 | * string is of the specified size |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 268 | * - return 1 if matched, 0 if differ, -ve if error |
| 269 | */ |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 270 | int romfs_dev_strcmp(struct super_block *sb, unsigned long pos, |
| 271 | const char *str, size_t size) |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 272 | { |
| 273 | size_t limit; |
| 274 | |
| 275 | limit = romfs_maxsize(sb); |
| 276 | if (pos >= limit) |
| 277 | return -EIO; |
| 278 | if (size > ROMFS_MAXFN) |
| 279 | return -ENAMETOOLONG; |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 280 | if (size + 1 > limit - pos) |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 281 | return -EIO; |
| 282 | |
| 283 | #ifdef CONFIG_ROMFS_ON_MTD |
| 284 | if (sb->s_mtd) |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 285 | return romfs_mtd_strcmp(sb, pos, str, size); |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 286 | #endif |
| 287 | #ifdef CONFIG_ROMFS_ON_BLOCK |
| 288 | if (sb->s_bdev) |
David Howells | 84baf74 | 2009-04-23 16:41:13 +0100 | [diff] [blame^] | 289 | return romfs_blk_strcmp(sb, pos, str, size); |
David Howells | da4458b | 2009-02-12 10:40:10 +0000 | [diff] [blame] | 290 | #endif |
| 291 | return -EIO; |
| 292 | } |