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