mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH v2 2/2] file-list: support special 'auto', 'block' specifiers
Date: Fri,  9 Jun 2023 09:52:29 +0200	[thread overview]
Message-ID: <20230609075229.1093397-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230609075229.1093397-1-a.fatoum@pengutronix.de>

Best practice is for each board to populate $global.system.partitions
or $global.fastboot.partitions with a string exporting its flashable
devices in a descriptive manner, e.g. "/dev/mmc0(eMMC),/dev/mmc1(SD)".

This often goes into BSPs though, so upstream boards are left without
default partitions, making use a bit cumbersome. Make this easier
by providing three new magic specifiers:

  - block: exports all registered block devices (e.g. eMMC and SD)
  - auto:  currently equivalent to "block". May be extended
           to EEPROMs, raw MTD and UBI in future

This makes it easy to export devices on any board:

  usbgadget -A auto -b

or

  usbgadget -S auto,/tmp/fitimage(fitimage)c

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v1 -> 2:
  - dropped nvmem specifier (Sascha). Can add it back later
    limited to only EEPROMs
  - reordered code in file_list_handle_spec to make future extension
    easier (Sascha)
---
 Documentation/user/usb.rst | 16 ++++++++++++++++
 common/block.c             | 16 ++++++++++++++++
 common/file-list.c         | 24 ++++++++++++++++++++++--
 include/block.h            |  6 ++++++
 4 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/Documentation/user/usb.rst b/Documentation/user/usb.rst
index f2f57ead98d4..eaee342956f9 100644
--- a/Documentation/user/usb.rst
+++ b/Documentation/user/usb.rst
@@ -73,6 +73,22 @@ Example:
 
   /dev/nand0.barebox.bb(barebox)sr,/kernel(kernel)rc
 
+Board code authors are encouraged to provide a default environment containing
+partitions with descriptive names. For boards where this is not specified,
+there exist a number of **partition** specifiers for automatically generating entries:
+
+* ``block`` exports all registered block devices (e.g. eMMC and SD)
+* ``auto``  currently equivalent to ``block``. May be extended to other flashable
+            devices, like EEPROMs, MTD or UBI volumes in future
+
+Example usage of exporting registered block devices, barebox update
+handlers and a single file that is created on flashing:
+
+.. code-block:: sh
+
+     detect -a # optional. Detects everything, so auto can register it
+     usbgadget -A auto,/tmp/fitimage(fitimage)c -b
+
 DFU
 ^^^
 
diff --git a/common/block.c b/common/block.c
index f6eeb7f9c85f..3a4a9fb73149 100644
--- a/common/block.c
+++ b/common/block.c
@@ -11,6 +11,7 @@
 #include <linux/err.h>
 #include <linux/list.h>
 #include <dma.h>
+#include <file-list.h>
 
 #define BLOCKSIZE(blk)	(1 << blk->blockbits)
 
@@ -458,3 +459,18 @@ int block_write(struct block_device *blk, void *buf, sector_t block, blkcnt_t nu
 
 	return ret < 0 ? ret : 0;
 }
+
+unsigned file_list_add_blockdevs(struct file_list *files)
+{
+	struct block_device *blk;
+	unsigned count = 0;
+	int err;
+
+	list_for_each_entry(blk, &block_device_list, list) {
+		err = file_list_add_cdev_entry(files, &blk->cdev, 0);
+		if (!err)
+			count++;
+	}
+
+	return count;
+}
diff --git a/common/file-list.c b/common/file-list.c
index 5c7020111145..7ecc8d00bb9e 100644
--- a/common/file-list.c
+++ b/common/file-list.c
@@ -9,6 +9,7 @@
 #include <stringlist.h>
 #include <linux/err.h>
 #include <driver.h>
+#include <block.h>
 
 #define PARSE_DEVICE	0
 #define PARSE_NAME	1
@@ -59,12 +60,28 @@ int file_list_add_cdev_entry(struct file_list *files, struct cdev *cdev,
 				     xasprintf("/dev/%s", cdev->name), flags);
 }
 
+static bool file_list_handle_spec(struct file_list *files, const char *spec)
+{
+	unsigned count = 0;
+	bool autoadd;
+
+	autoadd = !strcmp(spec, "auto");
+	if (autoadd || !strcmp(spec, "block"))
+		count += file_list_add_blockdevs(files);
+	else
+		return false;
+
+	pr_debug("'%s' spcifier resulted in %u entries\n", spec, count);
+	return true;
+}
+
 static int file_list_parse_one(struct file_list *files, const char *partstr, const char **endstr)
 {
 	int i = 0, state = PARSE_DEVICE;
 	char filename[PATH_MAX];
 	char name[PATH_MAX];
 	unsigned long flags = 0;
+	bool special = false;
 
 	memset(filename, 0, sizeof(filename));
 	memset(name, 0, sizeof(name));
@@ -115,7 +132,10 @@ static int file_list_parse_one(struct file_list *files, const char *partstr, con
 		partstr++;
 	}
 
-	if (state != PARSE_FLAGS) {
+	if (state == PARSE_DEVICE)
+		special = file_list_handle_spec(files, filename);
+
+	if (!special && state != PARSE_FLAGS) {
 		pr_err("Missing ')'\n");
 		return -EINVAL;
 	}
@@ -124,7 +144,7 @@ static int file_list_parse_one(struct file_list *files, const char *partstr, con
 		partstr++;
 	*endstr = partstr;
 
-	return file_list_add_entry(files, name, filename, flags);
+	return special ? 0 : file_list_add_entry(files, name, filename, flags);
 }
 
 static const char *flags_to_str(int flags)
diff --git a/include/block.h b/include/block.h
index da258f509b41..44037bd74c61 100644
--- a/include/block.h
+++ b/include/block.h
@@ -7,6 +7,7 @@
 #include <linux/types.h>
 
 struct block_device;
+struct file_list;
 
 struct block_device_ops {
 	int (*read)(struct block_device *, void *buf, sector_t block, blkcnt_t num_blocks);
@@ -51,11 +52,16 @@ static inline int block_flush(struct block_device *blk)
 
 #ifdef CONFIG_BLOCK
 struct block_device *cdev_get_block_device(const struct cdev *cdev);
+unsigned file_list_add_blockdevs(struct file_list *files);
 #else
 static inline struct block_device *cdev_get_block_device(const struct cdev *cdev)
 {
 	return NULL;
 }
+static inline unsigned file_list_add_blockdevs(struct file_list *files)
+{
+	return 0;
+}
 #endif
 
 static inline bool cdev_is_block_device(const struct cdev *cdev)
-- 
2.39.2




  reply	other threads:[~2023-06-09  7:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09  7:52 [PATCH v2 1/2] file_list: implement file_list_add_cdev_entry Ahmad Fatoum
2023-06-09  7:52 ` Ahmad Fatoum [this message]
2023-06-09  7:55   ` [PATCH v2 2/2] file-list: support special 'auto', 'block' specifiers Ahmad Fatoum

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=20230609075229.1093397-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@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