* [PATCH] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off
@ 2025-09-03 8:49 Christian Thießen via B4 Relay
2025-09-03 10:11 ` Ahmad Fatoum
2025-09-03 11:05 ` Marco Felsch
0 siblings, 2 replies; 3+ messages in thread
From: Christian Thießen via B4 Relay @ 2025-09-03 8:49 UTC (permalink / raw)
To: BAREBOX; +Cc: Christian Thießen
From: Christian Thießen <christian.thiessen@airbus.com>
When CONFIG_MTD_WRITE is unset, the mtd->_erase member of partition
devices does not get initialized, so it remains NULL.
However when running ubiattach on an UBI partition, mtd_erase() still
gets called, tries to call mtd->_erase and crashes. Make it fail
gracefully by returning ENOSYS ("Function not implemented") instead.
---
As discussed in https://github.com/barebox/barebox/issues/39 this patch
enables ubiattach-ing a NAND partition even if CONFIG_MTD_WRITE is
turned off, by making mtd_erase fail gracefully.
Signed-off-by: Christian Thießen <christian.thiessen@airbus.com>
---
drivers/mtd/core.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
index 6162ec450fc1c3a0fa6339042c4d627e0f9567d0..7ca8fc900af4666070b03ef5f945748d60e6a6e7 100644
--- a/drivers/mtd/core.c
+++ b/drivers/mtd/core.c
@@ -436,6 +436,7 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
{
if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
return -EINVAL;
+#ifdef CONFIG_MTD_WRITE
if (!(mtd->flags & MTD_WRITEABLE))
return -EROFS;
instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
@@ -443,6 +444,9 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
return 0;
return mtd->_erase(mtd, instr);
+#else
+ return -ENOSYS;
+#endif
}
int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
---
base-commit: b5561f3bdd4845b478e5b35aab04f1d8e71ea93b
change-id: 20250902-mtd-erase-enosys-2d80e4bc77c8
Best regards,
--
Christian Thießen <christian.thiessen@airbus.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off
2025-09-03 8:49 [PATCH] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off Christian Thießen via B4 Relay
@ 2025-09-03 10:11 ` Ahmad Fatoum
2025-09-03 11:05 ` Marco Felsch
1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2025-09-03 10:11 UTC (permalink / raw)
To: christian.thiessen, BAREBOX
Hi Christian,
Thanks for your patch!
On 9/3/25 10:49 AM, Christian Thießen via B4 Relay wrote:
> From: Christian Thießen <christian.thiessen@airbus.com>
>
> When CONFIG_MTD_WRITE is unset, the mtd->_erase member of partition
> devices does not get initialized, so it remains NULL.
> However when running ubiattach on an UBI partition, mtd_erase() still
> gets called, tries to call mtd->_erase and crashes. Make it fail
> gracefully by returning ENOSYS ("Function not implemented") instead.
Reviewed-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
with two minor comments below.
>
> ---
> As discussed in https://github.com/barebox/barebox/issues/39 this patch
> enables ubiattach-ing a NAND partition even if CONFIG_MTD_WRITE is
> turned off, by making mtd_erase fail gracefully.
>
> Signed-off-by: Christian Thießen <christian.thiessen@airbus.com>
Stuff between the --- is thrown away by git am, so you will want to move
the S-o-b above the ---. The rest is okay.
> ---
> drivers/mtd/core.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
> index 6162ec450fc1c3a0fa6339042c4d627e0f9567d0..7ca8fc900af4666070b03ef5f945748d60e6a6e7 100644
> --- a/drivers/mtd/core.c
> +++ b/drivers/mtd/core.c
> @@ -436,6 +436,7 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
> {
> if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
> return -EINVAL;
> +#ifdef CONFIG_MTD_WRITE
Please replace with the following at the start of the function:
if (!IS_ENABLED(CONFIG_MTD_WRITE))
return -ENOSYS;
This results in the same code as IS_ENABLED() is evaluated at
compile-time. but the compiler can still catch compile errors in the
disabled parts, which makes maintenance easier.
Cheers,
Ahmad
> if (!(mtd->flags & MTD_WRITEABLE))
> return -EROFS;
> instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
> @@ -443,6 +444,9 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
> return 0;
>
> return mtd->_erase(mtd, instr);
> +#else
> + return -ENOSYS;
> +#endif
> }
>
> int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
>
> ---
> base-commit: b5561f3bdd4845b478e5b35aab04f1d8e71ea93b
> change-id: 20250902-mtd-erase-enosys-2d80e4bc77c8
>
> Best regards,
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off
2025-09-03 8:49 [PATCH] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off Christian Thießen via B4 Relay
2025-09-03 10:11 ` Ahmad Fatoum
@ 2025-09-03 11:05 ` Marco Felsch
1 sibling, 0 replies; 3+ messages in thread
From: Marco Felsch @ 2025-09-03 11:05 UTC (permalink / raw)
To: christian.thiessen; +Cc: BAREBOX
Hi Christian,
thank you for your patch, please see below.
On 25-09-03, Christian Thießen via B4 Relay wrote:
> From: Christian Thießen <christian.thiessen@airbus.com>
>
> When CONFIG_MTD_WRITE is unset, the mtd->_erase member of partition
> devices does not get initialized, so it remains NULL.
> However when running ubiattach on an UBI partition, mtd_erase() still
> gets called, tries to call mtd->_erase and crashes. Make it fail
> gracefully by returning ENOSYS ("Function not implemented") instead.
>
> ---
> As discussed in https://github.com/barebox/barebox/issues/39 this patch
> enables ubiattach-ing a NAND partition even if CONFIG_MTD_WRITE is
> turned off, by making mtd_erase fail gracefully.
>
> Signed-off-by: Christian Thießen <christian.thiessen@airbus.com>
> ---
> drivers/mtd/core.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/mtd/core.c b/drivers/mtd/core.c
> index 6162ec450fc1c3a0fa6339042c4d627e0f9567d0..7ca8fc900af4666070b03ef5f945748d60e6a6e7 100644
> --- a/drivers/mtd/core.c
> +++ b/drivers/mtd/core.c
> @@ -436,6 +436,7 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
> {
> if (instr->addr >= mtd->size || instr->len > mtd->size - instr->addr)
> return -EINVAL;
> +#ifdef CONFIG_MTD_WRITE
> if (!(mtd->flags & MTD_WRITEABLE))
> return -EROFS;
> instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
> @@ -443,6 +444,9 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
> return 0;
>
> return mtd->_erase(mtd, instr);
> +#else
> + return -ENOSYS;
> +#endif
Albeit the patch is fine, I wouldn't do it via #ifdef's. Instead could
we use something like:
if (!IS_ENABLED(CONFIG_MTD_WRITE))
return -ENOSYS;
Regards,
Marco
> }
>
> int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
>
> ---
> base-commit: b5561f3bdd4845b478e5b35aab04f1d8e71ea93b
> change-id: 20250902-mtd-erase-enosys-2d80e4bc77c8
>
> Best regards,
> --
> Christian Thießen <christian.thiessen@airbus.com>
>
>
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-09-03 12:16 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-03 8:49 [PATCH] mtd: Make mtd_erase fail gracefully if CONFIG_MTD_WRITE is off Christian Thießen via B4 Relay
2025-09-03 10:11 ` Ahmad Fatoum
2025-09-03 11:05 ` Marco Felsch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox