From: Alexander Schmidt <alexs@linux.vnet.ibm.com>
To: "linux-mtd@lists.infradead.org" <linux-mtd@lists.infradead.org>
Cc: Frank Haverkamp <haver@linux.vnet.ibm.com>,
Andreas Arnez <arnez@linux.vnet.ibm.com>
Subject: [PATCH 1/5] ubi-utils: introduction of libubi_common
Date: Fri, 27 Jul 2007 17:28:48 +0200 [thread overview]
Message-ID: <200707271728.48560.alexs@linux.vnet.ibm.com> (raw)
In-Reply-To: <200707271723.38987.alexs@linux.vnet.ibm.com>
libubi_common provides helper functions for ubi utils.
Signed-off-by: Alexander Schmidt <alexs@linux.vnet.ibm.com>
---
ubi-utils/src/libubi_common.c | 93 ++++++++++++++++++++++++++++++++++++++++++
ubi-utils/src/libubi_common.h | 78 +++++++++++++++++++++++++++++++++++
2 files changed, 171 insertions(+)
--- /dev/null
+++ mtd-utils/ubi-utils/src/libubi_common.c
@@ -0,0 +1,93 @@
+/*
+ * Copyright International Business Machines Corp., 2007
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Author: Alexander Schmidt
+ *
+ * This library provides helper functions for accessing UBI volumes.
+ * The paths to UBI device nodes is hardcoded, see below.
+ *
+ * 1.0 Initial commit
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define DEFAULT_DEV_PATTERN "/dev/ubi%d"
+#define DEFAULT_VOL_PATTERN "/dev/ubi%d_%d"
+
+#define MAXPATH 1024
+
+int ubi_vol_open(int devn, int vol_id, int flags)
+{
+ char path[MAXPATH];
+ int fd;
+
+ snprintf(path, MAXPATH, DEFAULT_VOL_PATTERN, devn, vol_id);
+
+ fd = open(path, flags);
+
+ return fd;
+}
+
+int ubi_vol_close(int vol_fd)
+{
+ return close(vol_fd);
+}
+
+FILE* ubi_vol_fopen_read(int devn, int vol_id)
+{
+ FILE *fp;
+ int fd;
+
+ fd = ubi_vol_open(devn, vol_id, O_RDONLY);
+ if (fd == -1)
+ return NULL;
+
+ fp = fdopen(fd, "r");
+ if (fp == NULL)
+ ubi_vol_close(fd);
+
+ return fp;
+}
+
+int ubi_get_cdev_path(int devn, char *buf, size_t buf_size)
+{
+ int rc;
+
+ if (buf == NULL)
+ return -EINVAL;
+
+ rc = snprintf(buf, buf_size, DEFAULT_DEV_PATTERN, devn);
+
+ return rc;
+}
+
+
+int ubi_vol_get_used_bytes(int vol_fd, unsigned long long *bytes)
+{
+ off_t res;
+
+ res = lseek(vol_fd, 0, SEEK_END);
+ if (res == (off_t)-1)
+ return -1;
+ *bytes = (unsigned long long) res;
+ res = lseek(vol_fd, 0, SEEK_SET);
+ return res == (off_t)-1 ? -1 : 0;
+}
--- /dev/null
+++ mtd-utils/ubi-utils/src/libubi_common.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright International Business Machines Corp., 2007
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
+ * the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Author: Alexander Schmidt
+ *
+ * This library provides helper functions for accessing UBI volumes.
+ * The paths to UBI device nodes is hardcoded.
+ *
+ * 1.0 Initial commit
+ */
+
+
+/**
+ * ubi_vol_open - open a UBI volume
+ *
+ * @devn Number of UBI device on which to open the volume
+ * @vol_id Number of UBI device on which to open the volume
+ * @flags Flags to pass to open()
+ *
+ * This function opens a UBI volume on a given UBI device. It returns
+ * the file descriptor of the opened volume device. In case of an
+ * error %-1 is returned and errno is set appropriately.
+ */
+int ubi_vol_open(int devn, int vol_id, int flags);
+
+/**
+ * ubi_vol_close - close a UBI volume
+ *
+ * @vol_fd file descriptor of UBI volume to close
+ *
+ * This function closes the given UBI device.
+ */
+int ubi_vol_close(int vol_fd);
+
+/**
+ * ubi_vol_fopen_read - open a volume for reading, returning a FILE *
+ * @devn UBI device number
+ * @vol_id volume ID to read
+ *
+ * Opens a volume for reading. Reading itself can then be performed
+ * with fread(). The stream can be closed with fclose(). Returns a
+ * stream on success, else NULL.
+ */
+FILE *
+ubi_vol_fopen_read(int devn, uint32_t vol_id);
+
+/**
+ * ubi_get_cdev_path - get a device path for a given device number
+ *
+ * @devn UBI device number
+ * @buf buffer for device path
+ * @buf_size size of buffer
+ */
+int ubi_get_cdev_path(int devn, char *buf, size_t buf_size);
+
+/**
+ * ubi_vol_get_used_bytes - determine used bytes in a UBI volume
+ * @vol_fd File descriptor of UBI volume
+ * @bytes Pointer to result
+ *
+ * Returns 0 on success, else error.
+ */
+int ubi_vol_get_used_bytes(int vol_fd, unsigned long long *bytes);
+
next prev parent reply other threads:[~2007-07-27 15:30 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-07-27 15:23 [PATCH 0/5] ubi-utils: migrate to new libubi Alexander Schmidt
2007-07-27 15:28 ` Alexander Schmidt [this message]
2007-07-28 12:51 ` [PATCH 1/5] ubi-utils: introduction of libubi_common Artem Bityutskiy
2007-08-02 8:33 ` Alexander Schmidt
2007-08-04 9:03 ` Artem Bityutskiy
2007-07-27 15:29 ` [PATCH 2/5] ubi-utils: migrate pddcustomize Alexander Schmidt
2007-07-27 15:30 ` [PATCH 3/5] ubi-utils: migrate ubimirror Alexander Schmidt
2007-07-27 15:31 ` [PATCH 4/5] ubi-utils: migrate pfiflash Alexander Schmidt
2007-07-27 15:31 ` [PATCH 5/5] ubi-utils: remove dead code Alexander Schmidt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=200707271728.48560.alexs@linux.vnet.ibm.com \
--to=alexs@linux.vnet.ibm.com \
--cc=arnez@linux.vnet.ibm.com \
--cc=haver@linux.vnet.ibm.com \
--cc=linux-mtd@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox