linux/fs/romfs/storage.c

// SPDX-License-Identifier: GPL-2.0-or-later
/* RomFS storage access routines
 *
 * Copyright © 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells ([email protected])
 */

#include <linux/fs.h>
#include <linux/mtd/super.h>
#include <linux/buffer_head.h>
#include "internal.h"

#if !defined(CONFIG_ROMFS_ON_MTD) && !defined(CONFIG_ROMFS_ON_BLOCK)
#error no ROMFS backing store interface configured
#endif

#ifdef CONFIG_ROMFS_ON_MTD
#define ROMFS_MTD_READ

/*
 * read data from an romfs image on an MTD device
 */
static int romfs_mtd_read(struct super_block *sb, unsigned long pos,
			  void *buf, size_t buflen)
{
	size_t rlen;
	int ret;

	ret = ROMFS_MTD_READ(sb, pos, buflen, &rlen, buf);
	return (ret < 0 || rlen != buflen) ? -EIO : 0;
}

/*
 * determine the length of a string in a romfs image on an MTD device
 */
static ssize_t romfs_mtd_strnlen(struct super_block *sb,
				 unsigned long pos, size_t maxlen)
{
	ssize_t n = 0;
	size_t segment;
	u_char buf[16], *p;
	size_t len;
	int ret;

	/* scan the string up to 16 bytes at a time */
	while (maxlen > 0) {
		segment = min_t(size_t, maxlen, 16);
		ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
		if (ret < 0)
			return ret;
		p = memchr(buf, 0, len);
		if (p)
			return n + (p - buf);
		maxlen -= len;
		pos += len;
		n += len;
	}

	return n;
}

/*
 * compare a string to one in a romfs image on MTD
 * - return 1 if matched, 0 if differ, -ve if error
 */
static int romfs_mtd_strcmp(struct super_block *sb, unsigned long pos,
			    const char *str, size_t size)
{
	u_char buf[17];
	size_t len, segment;
	int ret;

	/* scan the string up to 16 bytes at a time, and attempt to grab the
	 * trailing NUL whilst we're at it */
	buf[0] = 0xff;

	while (size > 0) {
		segment = min_t(size_t, size + 1, 17);
		ret = ROMFS_MTD_READ(sb, pos, segment, &len, buf);
		if (ret < 0)
			return ret;
		len--;
		if (memcmp(buf, str, len) != 0)
			return 0;
		buf[0] = buf[len];
		size -= len;
		pos += len;
		str += len;
	}

	/* check the trailing NUL was */
	if (buf[0])
		return 0;

	return 1;
}
#endif /* CONFIG_ROMFS_ON_MTD */

#ifdef CONFIG_ROMFS_ON_BLOCK
/*
 * read data from an romfs image on a block device
 */
static int romfs_blk_read(struct super_block *sb, unsigned long pos,
			  void *buf, size_t buflen)
{}

/*
 * determine the length of a string in romfs on a block device
 */
static ssize_t romfs_blk_strnlen(struct super_block *sb,
				 unsigned long pos, size_t limit)
{}

/*
 * compare a string to one in a romfs image on a block device
 * - return 1 if matched, 0 if differ, -ve if error
 */
static int romfs_blk_strcmp(struct super_block *sb, unsigned long pos,
			    const char *str, size_t size)
{}
#endif /* CONFIG_ROMFS_ON_BLOCK */

/*
 * read data from the romfs image
 */
int romfs_dev_read(struct super_block *sb, unsigned long pos,
		   void *buf, size_t buflen)
{}

/*
 * determine the length of a string in romfs
 */
ssize_t romfs_dev_strnlen(struct super_block *sb,
			  unsigned long pos, size_t maxlen)
{}

/*
 * compare a string to one in romfs
 * - the string to be compared to, str, may not be NUL-terminated; instead the
 *   string is of the specified size
 * - return 1 if matched, 0 if differ, -ve if error
 */
int romfs_dev_strcmp(struct super_block *sb, unsigned long pos,
		     const char *str, size_t size)
{}