mirror of
https://github.com/AsahiLinux/u-boot
synced 2024-11-24 21:54:01 +00:00
disk/part.c: 'usb storage' avoiding overflow when output capacity
Before: Marvell>> usb storage Device 0: Vendor: StoreJet Rev: Prod: Transcend Type: Hard Disk Capacity: 28759.9 MB = 28.0 GB (488397168 x 512) After: Marvell>> usb storage Device 0: Vendor: StoreJet Rev: Prod: Transcend Type: Hard Disk Capacity: 238475.1 MB = 232.8 GB (488397168 x 512) Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
This commit is contained in:
parent
b9d51fbb18
commit
69a2a4d9a5
1 changed files with 24 additions and 6 deletions
30
disk/part.c
30
disk/part.c
|
@ -109,13 +109,30 @@ block_dev_desc_t *get_dev(char* ifname, int dev)
|
|||
/*
|
||||
* reports device info to the user
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_LBA48
|
||||
typedef uint64_t lba512_t;
|
||||
#else
|
||||
typedef lbaint_t lba512_t;
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Overflowless variant of (block_count * mul_by / div_by)
|
||||
* when div_by > mul_by
|
||||
*/
|
||||
static lba512_t lba512_muldiv (lba512_t block_count, lba512_t mul_by, lba512_t div_by)
|
||||
{
|
||||
lba512_t bc_quot, bc_rem;
|
||||
|
||||
/* x * m / d == x / d * m + (x % d) * m / d */
|
||||
bc_quot = block_count / div_by;
|
||||
bc_rem = block_count - div_by * bc_quot;
|
||||
return bc_quot * mul_by + (bc_rem * mul_by) / div_by;
|
||||
}
|
||||
|
||||
void dev_print (block_dev_desc_t *dev_desc)
|
||||
{
|
||||
#ifdef CONFIG_LBA48
|
||||
uint64_t lba512; /* number of blocks if 512bytes block size */
|
||||
#else
|
||||
lbaint_t lba512;
|
||||
#endif
|
||||
lba512_t lba512; /* number of blocks if 512bytes block size */
|
||||
|
||||
if (dev_desc->type == DEV_TYPE_UNKNOWN) {
|
||||
puts ("not available\n");
|
||||
|
@ -184,8 +201,9 @@ void dev_print (block_dev_desc_t *dev_desc)
|
|||
lba = dev_desc->lba;
|
||||
|
||||
lba512 = (lba * (dev_desc->blksz/512));
|
||||
mb = (10 * lba512) / 2048; /* 2048 = (1024 * 1024) / 512 MB */
|
||||
/* round to 1 digit */
|
||||
mb = lba512_muldiv(lba512, 10, 2048); /* 2048 = (1024 * 1024) / 512 MB */
|
||||
|
||||
mb_quot = mb / 10;
|
||||
mb_rem = mb - (10 * mb_quot);
|
||||
|
||||
|
|
Loading…
Reference in a new issue