Skip to contentSkip to conversation

cmd: ufetch: avoid truncating storage sizes on 32-bit systems

PATCH Applied Han Gao 5 participants 5 messages Sep 19 – Oct 28

lore
Share & tools
lore

Han Gao

author
#0 lore raw

PATCH 1/1 cmd: ufetch: avoid truncating storage sizes on 32-bit systems

Storage capacities above 4 GiB are truncated when stored in size_t on 32-bit systems. Use a 64-bit accumulator and promote the block count before multiplication so 32-bit LBA configurations are also handled correctly.

Fixes 86d462c05d57 ("cmd: add a fetch utility")
---
 cmd/ufetch.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
cmd/ufetch.c +2 -2
index d4b54f77b7b2..29ccbf654a4e 100644
@@ -95,7 +95,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 	const char *model, *compatible;
 	char *ipaddr;
 	int n_cmds, n_cpus = 0, compatlen;
-	size_t size = 0;
+	u64 size = 0;
 	ofnode np;
 	bool skip_ascii = false;
 
@@ -221,7 +221,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 			ret = uclass_find_device_by_seq(UCLASS_BLK, line - STORAGE, &dev);
 			if (!ret && dev) {
 				desc = dev_get_uclass_plat(dev);
-				size = desc->lba * desc->blksz;
+				size = (u64)desc->lba * desc->blksz;
 				printf("%4s %d: " RESET, blk_get_uclass_name(desc->uclass_id),
 					desc->lun);
 				if (size)

---
base-commit: 211de43d0f954a00a490220c1aac9db298287c40
change-id: 20260919-fixfetch-d7aa5b94d9b7

Best regards,

Signature · 1 line
Han Gao <[email protected]>
#1 lore raw

Am 18. September 2026 22:35:33 MESZ schrieb Han Gao <[email protected]>:

Quoted 26 lines

Storage capacities above 4 GiB are truncated when stored in size_t on 32-bit systems. Use a 64-bit accumulator and promote the block count before multiplication so 32-bit LBA configurations are also handled correctly.

Fixes 86d462c05d57 ("cmd: add a fetch utility")

---
cmd/ufetch.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
cmd/ufetch.c +1 -1
index d4b54f77b7b2..29ccbf654a4e 100644
@@ -95,7 +95,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 	const char *model, *compatible;
 	char *ipaddr;
 	int n_cmds, n_cpus = 0, compatlen;
-	size_t size = 0;
+	u64 size = 0;
 	ofnode np;
 	bool skip_ascii = false;
 

cmd/ufetch.c static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc, · line 224

Quoted 5 lines
cmd/ufetch.c +1 -1
@@ -221,7 +221,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 			ret = uclass_find_device_by_seq(UCLASS_BLK, line - STORAGE, &dev);
 			if (!ret && dev) {
 				desc = dev_get_uclass_plat(dev);
-				size = desc->lba * desc->blksz;
+				size = (u64)desc->lba * desc->blksz;

Shouldn't desc->lba itself be 64 bit to allow for drives larger than 2 TiB.

Why do we allow CONFIG_SYS_64BIT_LBA=n?

Best regards

Heinrich

Quoted 12 lines
cmd/ufetch.c +0 -1
 				printf("%4s %d: " RESET, blk_get_uclass_name(desc->uclass_id),
 					desc->lun);
 				if (size)
 
---

base-commit: 211de43d0f954a00a490220c1aac9db298287c40
change-id: 20260919-fixfetch-d7aa5b94d9b7

Best regards,

Signature · 1 line
Han Gao <[email protected]>
#2 lore raw
Quoted 26 lines · Heinrich Schuchardt

Am 18. September 2026 22:35:33 MESZ schrieb Han Gao <[email protected]>:

Quoted 25 lines

Storage capacities above 4 GiB are truncated when stored in size_t on 32-bit systems. Use a 64-bit accumulator and promote the block count before multiplication so 32-bit LBA configurations are also handled correctly.

Fixes 86d462c05d57 ("cmd: add a fetch utility")

---
cmd/ufetch.c | 4 ++--

1 file changed, 2 insertions(+), 2 deletions(-)
cmd/ufetch.c +1 -1
index d4b54f77b7b2..29ccbf654a4e 100644
@@ -95,7 +95,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 	const char *model, *compatible;
 	char *ipaddr;
 	int n_cmds, n_cpus = 0, compatlen;
-	size_t size = 0;
+	u64 size = 0;
 	ofnode np;
 	bool skip_ascii = false;
 
Quoted 8 lines

cmd/ufetch.c static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc, · line 224

Quoted 6 lines
cmd/ufetch.c +1 -1
@@ -221,7 +221,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 			ret = uclass_find_device_by_seq(UCLASS_BLK, line - STORAGE, &dev);
 			if (!ret && dev) {
 				desc = dev_get_uclass_plat(dev);
-				size = desc->lba * desc->blksz;
+				size = (u64)desc->lba * desc->blksz;

Shouldn't desc->lba itself be 64 bit to allow for drives larger than 2 TiB.

Likely so, yes.

Quoted 1 line

Why do we allow CONFIG_SYS_64BIT_LBA=n?

It'd be interesting to see the size growth from that change.

Signature · 1 line
Tom
#3 lore raw
In reply to Han Gao

Hi Han,

Quoted 7 lines · Han Gao

Storage capacities above 4 GiB are truncated when stored in size_t on 32-bit systems. Use a 64-bit accumulator and promote the block count before multiplication so 32-bit LBA configurations are also handled correctly.

Fixes 86d462c05d57 ("cmd: add a fetch utility")

No need blank line here.

Quoted 10 lines
---
 cmd/ufetch.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
cmd/ufetch.c +0 -0
index d4b54f77b7b2..29ccbf654a4e 100644

cmd/ufetch.c static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc, · line 98

Quoted 5 lines
cmd/ufetch.c +1 -1
@@ -95,7 +95,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 	const char *model, *compatible;
 	char *ipaddr;
 	int n_cmds, n_cpus = 0, compatlen;
-	size_t size = 0;
+	u64 size = 0;

Although I don't like using 'u64' directly, but checked include/api_public.h:92 lbasize_t is 'unsigned long' no matter 32bit or 64bit platform, so seems we have
no choice but have to stick with it..

cmd/ufetch.c static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc, · line 224

Quoted 9 lines
cmd/ufetch.c +1 -1
 	ofnode np;
 	bool skip_ascii = false;
 
@@ -221,7 +221,7 @@ static int do_ufetch(struct cmd_tbl *cmdtp, int flag, int argc,
 			ret = uclass_find_device_by_seq(UCLASS_BLK, line - STORAGE, &dev);
 			if (!ret && dev) {
 				desc = dev_get_uclass_plat(dev);
-				size = desc->lba * desc->blksz;
+				size = (u64)desc->lba * desc->blksz;

you can use CONFIG_SYS_64BIT_LBA instead of manually cast

Quoted 12 lines
cmd/ufetch.c +0 -1
 				printf("%4s %d: " RESET, blk_get_uclass_name(desc->uclass_id),
 					desc->lun);
 				if (size)
 
---

base-commit: 211de43d0f954a00a490220c1aac9db298287c40
change-id: 20260919-fixfetch-d7aa5b94d9b7

Best regards,

Signature · 1 line
Han Gao <[email protected]>
Signature · 1 line
Yixun Lan (dlan)
#4 lore raw
In reply to Han Gao
Quoted 8 lines · Han Gao

Storage capacities above 4 GiB are truncated when stored in size_t on 32-bit systems. Use a 64-bit accumulator and promote the block count before multiplication so 32-bit LBA configurations are also handled correctly.

Fixes 86d462c05d57 ("cmd: add a fetch utility")

[...]

Applied, thanks!

[1/1] cmd: ufetch: avoid truncating storage sizes on 32-bit systems

      https://source.denx.de/u-boot/custodians/u-boot-snapdragon/-/commit/7aa9fac8587a

Best regards,

Signature · 1 line
// Casey (she/they)