[Skiboot] [RFC PATCH 2/2] libflash/file: add file abstraction for libflash

Alistair Popple alistair at popple.id.au
Thu Mar 26 13:45:37 AEDT 2015


Hi Cyril,

Is libflash providing any useful functionality here? Or are you creating this backend just 
to use the libffs API?

Regards,

Alistair

On Thu, 19 Mar 2015 18:18:21 Cyril Bur wrote:
> The functionality provided by libflash and libffs are useful and it would be
> good to use them on dumps of flash or even access flash from userland
> through MTD devices which are presented as files.
> 
> Signed-off-by: Cyril Bur <cyril.bur at au1.ibm.com>
> ---
>  libflash/file_flash.c    | 224
> +++++++++++++++++++++++++++++++++++++++++++++++ libflash/file_flash.h    | 
> 11 +++
>  libflash/libflash-priv.h |   2 +
>  libflash/libflash.c      |   1 +
>  4 files changed, 238 insertions(+)
>  create mode 100644 libflash/file_flash.c
>  create mode 100644 libflash/file_flash.h
> 
> diff --git a/libflash/file_flash.c b/libflash/file_flash.c
> new file mode 100644
> index 0000000..d902672
> --- /dev/null
> +++ b/libflash/file_flash.c
> @@ -0,0 +1,224 @@
> +#include <unistd.h>
> +#include <sys/types.h>
> +#include <sys/stat.h>
> +#include <stdbool.h>
> +#include <stdlib.h>
> +#include <sys/ioctl.h>
> +#include <sys/mman.h>
> +#include <string.h>
> +
> +#include <mtd/mtd-abi.h>
> +
> +#include "file_flash.h"
> +
> +/* The caller is going to have to supply this */
> +struct file_flash_priv {
> +	int fd;
> +};
> +
> +/*
> + * Unfortunately not all file descriptors are created equal...
> + * Here we check to see if the file descriptor is to an MTD device, in
> which + * case we have to get the size of it differently.
> + */
> +int file_setup(struct spi_flash_ctrl *ctrl, uint32_t *tsize)
> +{
> +	struct mtd_info_user mtd_info;
> +	struct file_flash_priv *file_flash_data;
> +	struct stat sbuf;
> +
> +	if (!ctrl || !ctrl->priv)
> +		return -1;
> +
> +	file_flash_data = (struct file_flash_priv *)ctrl->priv;
> +
> +	if (fstat(file_flash_data->fd, &sbuf) == -1)
> +		return -1;
> +
> +	if (S_ISCHR(sbuf.st_mode)) {
> +		if (ioctl(file_flash_data->fd, MEMGETINFO, &mtd_info) == 
-1)
> +			return -1;
> +
> +		ctrl->finfo->size = mtd_info.size;
> +
> +	} else if (S_ISREG(sbuf.st_mode)) {
> +		ctrl->finfo->size = sbuf.st_size;
> +
> +	} else {
> +		/* Not going to be able to work with anything else */
> +		return -1;
> +	}
> +
> +	if (tsize)
> +		*tsize = ctrl->finfo->size;
> +
> +	return 0;
> +}
> +
> +int file_set4b(struct spi_flash_ctrl *ctrl, bool enable)
> +{
> +	/* Always report success no matter what, this isn't relevent for files */
> +	return 0;
> +}
> +
> +int file_chipid(struct spi_flash_ctrl *ctrl, uint8_t *id_buf,
> +		uint32_t *id_size)
> +{
> +	if (!ctrl || !ctrl->priv || !id_size || *id_size < 3)
> +		return -1;
> +
> +	id_buf[0] = 'M';
> +	id_buf[1] = 'T';
> +	id_buf[2] = 'D';
> +
> +	*id_size = 3;
> +	return 0;
> +}
> +
> +int file_read(struct spi_flash_ctrl *ctrl, uint32_t addr, void *buf,
> +		uint32_t size)
> +{
> +	int rc;
> +	struct file_flash_priv *file_flash_data;
> +
> +	if (!ctrl || !ctrl->priv)
> +		return -1;
> +
> +	file_flash_data = (struct file_flash_priv *)ctrl->priv;
> +
> +	rc = lseek(file_flash_data->fd, addr, SEEK_SET);
> +	if ((off_t )rc == (off_t )-1)
> +		return -1;
> +
> +	rc = read(file_flash_data->fd, buf, size);
> +	if (rc == -1)
> +		return -1;
> +	/* TODO Perhaps deal with short reads */
> +
> +	return 0;
> +}
> +
> +int file_write(struct spi_flash_ctrl *ctrl, uint32_t addr,
> +		const void *buf, uint32_t size)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ozlabs.org/pipermail/skiboot/attachments/20150326/7db764d2/attachment-0001.html>


More information about the Skiboot mailing list