Skip to content

Commit 7b09acd

Browse files
troglobitmiquelraynal
authored andcommitted
mtd: block2mtd: add support for an optional custom MTD label
This patch adds support for an optional MTD label for mtd2block emulated MTD devices. Useful when, e.g., testing device images using Qemu. The following line in /etc/fstab can then be used to mount a file system regardless if running on an embedded system, or emulated with block2mtd: mtd:Config /mnt jffs2 noatime,nodiratime 0 0 Kernel command line syntax in the emulated case: block2mtd.block2mtd=/dev/sda,,Config Notice the ',,' it is the optional erase_size, which like before this patch, defaults to PAGE_SIZE when omitted. Hence the strlen() check. Signed-off-by: Joachim Wiberg <[email protected]> Signed-off-by: Miquel Raynal <[email protected]> Link: https://lore.kernel.org/linux-mtd/[email protected]
1 parent a04e965 commit 7b09acd

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

drivers/mtd/devices/block2mtd.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <linux/major.h>
3333

3434
/* Maximum number of comma-separated items in the 'block2mtd=' parameter */
35-
#define BLOCK2MTD_PARAM_MAX_COUNT 2
35+
#define BLOCK2MTD_PARAM_MAX_COUNT 3
3636

3737
/* Info for the block device */
3838
struct block2mtd_dev {
@@ -217,7 +217,7 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
217217

218218

219219
static struct block2mtd_dev *add_device(char *devname, int erase_size,
220-
int timeout)
220+
char *label, int timeout)
221221
{
222222
#ifndef MODULE
223223
int i;
@@ -281,7 +281,10 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size,
281281

282282
/* Setup the MTD structure */
283283
/* make the name contain the block device in */
284-
name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
284+
if (!label)
285+
name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname);
286+
else
287+
name = kstrdup(label, GFP_KERNEL);
285288
if (!name)
286289
goto err_destroy_mutex;
287290

@@ -308,7 +311,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size,
308311
list_add(&dev->list, &blkmtd_device_list);
309312
pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n",
310313
dev->mtd.index,
311-
dev->mtd.name + strlen("block2mtd: "),
314+
label ? label : dev->mtd.name + strlen("block2mtd: "),
312315
dev->mtd.erasesize >> 10, dev->mtd.erasesize);
313316
return dev;
314317

@@ -386,6 +389,7 @@ static int block2mtd_setup2(const char *val)
386389
char *str = buf;
387390
char *token[BLOCK2MTD_PARAM_MAX_COUNT];
388391
char *name;
392+
char *label = NULL;
389393
size_t erase_size = PAGE_SIZE;
390394
unsigned long timeout = MTD_DEFAULT_TIMEOUT;
391395
int i, ret;
@@ -417,15 +421,21 @@ static int block2mtd_setup2(const char *val)
417421
return 0;
418422
}
419423

420-
if (token[1]) {
424+
/* Optional argument when custom label is used */
425+
if (token[1] && strlen(token[1])) {
421426
ret = parse_num(&erase_size, token[1]);
422427
if (ret) {
423428
pr_err("illegal erase size\n");
424429
return 0;
425430
}
426431
}
427432

428-
add_device(name, erase_size, timeout);
433+
if (token[2]) {
434+
label = token[2];
435+
pr_info("Using custom MTD label '%s' for dev %s\n", label, name);
436+
}
437+
438+
add_device(name, erase_size, label, timeout);
429439

430440
return 0;
431441
}
@@ -459,7 +469,7 @@ static int block2mtd_setup(const char *val, const struct kernel_param *kp)
459469

460470

461471
module_param_call(block2mtd, block2mtd_setup, NULL, NULL, 0200);
462-
MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,<erasesize>]\"");
472+
MODULE_PARM_DESC(block2mtd, "Device to use. \"block2mtd=<dev>[,[<erasesize>][,<label>]]\"");
463473

464474
static int __init block2mtd_init(void)
465475
{

0 commit comments

Comments
 (0)