Skip to content

Commit

Permalink
hw/block/pflash_cfi0{1, 2}: Error out if device length isn't a power …
Browse files Browse the repository at this point in the history
…of two

According to the JEDEC standard the device length is communicated to an
OS as an exponent (power of two).

Signed-off-by: Bernhard Beschow <[email protected]>
Reviewed-by: Bin Meng <[email protected]>
Reviewed-by: Philippe Mathieu-Daudé <[email protected]>
Message-Id: <[email protected]>
Signed-off-by: Daniel Henrique Barboza <[email protected]>
  • Loading branch information
shentok authored and danielhb committed Oct 28, 2022
1 parent c593d1c commit 334c388
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions hw/block/pflash_cfi01.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ static const MemoryRegionOps pflash_cfi01_ops = {
.endianness = DEVICE_NATIVE_ENDIAN,
};

static void pflash_cfi01_fill_cfi_table(PFlashCFI01 *pfl)
static void pflash_cfi01_fill_cfi_table(PFlashCFI01 *pfl, Error **errp)
{
uint64_t blocks_per_device, sector_len_per_device, device_len;
int num_devices;
Expand All @@ -708,6 +708,10 @@ static void pflash_cfi01_fill_cfi_table(PFlashCFI01 *pfl)
sector_len_per_device = pfl->sector_len / num_devices;
}
device_len = sector_len_per_device * blocks_per_device;
if (!is_power_of_2(device_len)) {
error_setg(errp, "Device size must be a power of two.");
return;
}

/* Hardcoded CFI table */
/* Standard "QRY" string */
Expand Down Expand Up @@ -865,7 +869,7 @@ static void pflash_cfi01_realize(DeviceState *dev, Error **errp)
*/
pfl->cmd = 0x00;
pfl->status = 0x80; /* WSM ready */
pflash_cfi01_fill_cfi_table(pfl);
pflash_cfi01_fill_cfi_table(pfl, errp);
}

static void pflash_cfi01_system_reset(DeviceState *dev)
Expand Down
5 changes: 5 additions & 0 deletions hw/block/pflash_cfi02.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,11 @@ static void pflash_cfi02_realize(DeviceState *dev, Error **errp)
return;
}

if (!is_power_of_2(pfl->chip_len)) {
error_setg(errp, "Device size must be a power of two.");
return;
}

memory_region_init_rom_device(&pfl->orig_mem, OBJECT(pfl),
&pflash_cfi02_ops, pfl, pfl->name,
pfl->chip_len, errp);
Expand Down

0 comments on commit 334c388

Please sign in to comment.