mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed
@ 2020-09-14 15:27 Yann Sionneau
  2020-09-14 15:51 ` Ahmad Fatoum
  0 siblings, 1 reply; 5+ messages in thread
From: Yann Sionneau @ 2020-09-14 15:27 UTC (permalink / raw)
  To: barebox; +Cc: Yann Sionneau

char type can be either signed or unsigned according to C standard.
If your arch has signed char, this kind of computation will end up wrong
because of sign extension:

https://git.pengutronix.de/cgit/barebox/tree/drivers/mci/mci-core.c#n869

mci->capacity = mci->ext_csd[EXT_CSD_SEC_COUNT] << 0 |
		mci->ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 |
		mci->ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 |
		mci->ext_csd[EXT_CSD_SEC_COUNT + 3] << 24;

Turning the ext_csd field into u8 * fixes the issue.
---
 include/mci.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/mci.h b/include/mci.h
index 96547fb39..f6d845440 100644
--- a/include/mci.h
+++ b/include/mci.h
@@ -456,7 +456,7 @@ struct mci {
 	uint64_t capacity;	/**< Card's data capacity in bytes */
 	int ready_for_use;	/** true if already probed */
 	int dsr_imp;		/**< DSR implementation state from CSD */
-	char *ext_csd;
+	u8 *ext_csd;
 	int probe;
 	struct param_d *param_boot;
 	int bootpart;
-- 
2.17.1



_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed
  2020-09-14 15:27 [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed Yann Sionneau
@ 2020-09-14 15:51 ` Ahmad Fatoum
  2020-09-14 16:03   ` Yann Sionneau
  0 siblings, 1 reply; 5+ messages in thread
From: Ahmad Fatoum @ 2020-09-14 15:51 UTC (permalink / raw)
  To: Yann Sionneau, barebox

Hello Yann,

On 9/14/20 5:27 PM, Yann Sionneau wrote:
> char type can be either signed or unsigned according to C standard.
> If your arch has signed char, this kind of computation will end up wrong
> because of sign extension:
> 
> https://git.pengutronix.de/cgit/barebox/tree/drivers/mci/mci-core.c#n869
> 
> mci->capacity = mci->ext_csd[EXT_CSD_SEC_COUNT] << 0 |
> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 |
> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 |
> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 3] << 24;

Good catch! I wondered why this wasn't noticed before,
so I looked at the OSELAS.Toolchain`s we have on our build
server. Only x86 and MIPS have signed chars by default.

On x86, we usually do EFI, so barebox mci-core is not involved
and MIPS apparently doesn't have any MCI drivers yet.

(Of course, you can compile do -fsigned-char or compile
 your own toolchain with a different default, but I guess
 not many do that)

Thanks for fixing it.

> Turning the ext_csd field into u8 * fixes the issue.

Your Signed-off-by is required.
Please check https://developercertificate.org/

> ---
>  include/mci.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/include/mci.h b/include/mci.h
> index 96547fb39..f6d845440 100644
> --- a/include/mci.h
> +++ b/include/mci.h
> @@ -456,7 +456,7 @@ struct mci {
>  	uint64_t capacity;	/**< Card's data capacity in bytes */
>  	int ready_for_use;	/** true if already probed */
>  	int dsr_imp;		/**< DSR implementation state from CSD */
> -	char *ext_csd;
> +	u8 *ext_csd;
>  	int probe;
>  	struct param_d *param_boot;
>  	int bootpart;
> 

-- 
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 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed
  2020-09-14 15:51 ` Ahmad Fatoum
@ 2020-09-14 16:03   ` Yann Sionneau
  0 siblings, 0 replies; 5+ messages in thread
From: Yann Sionneau @ 2020-09-14 16:03 UTC (permalink / raw)
  To: Ahmad Fatoum, barebox

Woops, forgot about the Signed-off-by. Sorry!

On 9/14/20 5:51 PM, Ahmad Fatoum wrote:
> Hello Yann,
>
> On 9/14/20 5:27 PM, Yann Sionneau wrote:
>> char type can be either signed or unsigned according to C standard.
>> If your arch has signed char, this kind of computation will end up wrong
>> because of sign extension:
>>
>> https://git.pengutronix.de/cgit/barebox/tree/drivers/mci/mci-core.c#n869
>>
>> mci->capacity = mci->ext_csd[EXT_CSD_SEC_COUNT] << 0 |
>> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 |
>> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 |
>> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 3] << 24;
> Good catch! I wondered why this wasn't noticed before,
> so I looked at the OSELAS.Toolchain`s we have on our build
> server. Only x86 and MIPS have signed chars by default.
>
> On x86, we usually do EFI, so barebox mci-core is not involved
> and MIPS apparently doesn't have any MCI drivers yet.
>
> (Of course, you can compile do -fsigned-char or compile
>   your own toolchain with a different default, but I guess
>   not many do that)
>
> Thanks for fixing it.
>
>> Turning the ext_csd field into u8 * fixes the issue.
> Your Signed-off-by is required.
> Please check https://developercertificate.org/
>
>> ---
>>   include/mci.h | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/include/mci.h b/include/mci.h
>> index 96547fb39..f6d845440 100644
>> --- a/include/mci.h
>> +++ b/include/mci.h
>> @@ -456,7 +456,7 @@ struct mci {
>>   	uint64_t capacity;	/**< Card's data capacity in bytes */
>>   	int ready_for_use;	/** true if already probed */
>>   	int dsr_imp;		/**< DSR implementation state from CSD */
>> -	char *ext_csd;
>> +	u8 *ext_csd;
>>   	int probe;
>>   	struct param_d *param_boot;
>>   	int bootpart;
>>


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed
  2020-09-14 16:02 Yann Sionneau
@ 2020-09-15  7:33 ` Sascha Hauer
  0 siblings, 0 replies; 5+ messages in thread
From: Sascha Hauer @ 2020-09-15  7:33 UTC (permalink / raw)
  To: Yann Sionneau; +Cc: barebox

On Mon, Sep 14, 2020 at 06:02:08PM +0200, Yann Sionneau wrote:
> char type can be either signed or unsigned according to C standard.
> If your arch has signed char, this kind of computation will end up wrong
> because of sign extension:
> 
> https://git.pengutronix.de/cgit/barebox/tree/drivers/mci/mci-core.c#n869
> 
> mci->capacity = mci->ext_csd[EXT_CSD_SEC_COUNT] << 0 |
> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 |
> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 |
> 		mci->ext_csd[EXT_CSD_SEC_COUNT + 3] << 24;
> 
> Turning the ext_csd field into u8 * fixes the issue.
> 
> Signed-off-by: Yann Sionneau <ysionneau@kalray.eu>
> ---
>  include/mci.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

Applied, thanks

Sascha

-- 
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 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed
@ 2020-09-14 16:02 Yann Sionneau
  2020-09-15  7:33 ` Sascha Hauer
  0 siblings, 1 reply; 5+ messages in thread
From: Yann Sionneau @ 2020-09-14 16:02 UTC (permalink / raw)
  To: barebox; +Cc: Yann Sionneau

char type can be either signed or unsigned according to C standard.
If your arch has signed char, this kind of computation will end up wrong
because of sign extension:

https://git.pengutronix.de/cgit/barebox/tree/drivers/mci/mci-core.c#n869

mci->capacity = mci->ext_csd[EXT_CSD_SEC_COUNT] << 0 |
		mci->ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 |
		mci->ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 |
		mci->ext_csd[EXT_CSD_SEC_COUNT + 3] << 24;

Turning the ext_csd field into u8 * fixes the issue.

Signed-off-by: Yann Sionneau <ysionneau@kalray.eu>
---
 include/mci.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/mci.h b/include/mci.h
index 96547fb39..f6d845440 100644
--- a/include/mci.h
+++ b/include/mci.h
@@ -456,7 +456,7 @@ struct mci {
 	uint64_t capacity;	/**< Card's data capacity in bytes */
 	int ready_for_use;	/** true if already probed */
 	int dsr_imp;		/**< DSR implementation state from CSD */
-	char *ext_csd;
+	u8 *ext_csd;
 	int probe;
 	struct param_d *param_boot;
 	int bootpart;
-- 
2.17.1



_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-09-15  7:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-14 15:27 [PATCH] mci: fix wrong sd/mmc/emmc card size computation for arch where char is signed Yann Sionneau
2020-09-14 15:51 ` Ahmad Fatoum
2020-09-14 16:03   ` Yann Sionneau
2020-09-14 16:02 Yann Sionneau
2020-09-15  7:33 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox