nvram: at24c: fix problems related to "rom-size"
v3 cover nvram: at24c: fix problems related to "rom-size"
I used this driver as a template for a custom one. While hacking on my own, I noticed some problems in this driver, too. This series fixes the first set of them, related to the "rom-size" parameter. It fixes a segfault.
I think the first patch is clearly suitable for stable. I think the second one, too, but not as clearly. The third one is a cleanup and not for stable. Still, I am open for opinions about these thoughts.
Thanks,
Wolfram
This is the same v3 as last time. Rebased to current master, but that produced no diff.
Changes since v2:
* removed '\n' from error_report-strings
* made sure checkpatch is happy
* added tags from Philippe (thanks!)
Changes since v1:
* reordered patches according to significance for stable
* use AT24C_ROMSIZE_DEFAULT instead of magic value
* patch 3 doesn't improve the ERR macro anymore but replaces it completely with error_report().
Wolfram Sang (3):
nvram: at24c: prevent segfault by checking "rom-size"
nvram: at24c: use a sane default for "rom-size"
nvram: at24c: use standard error reporting
hw/nvram/eeprom_at24c.c | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-)
Signature · 1 line
2.11.0
PATCH RESEND v3 1/3 nvram: at24c: prevent segfault by checking "rom-size"
The value for "rom-size" is used as a divisor, so it must not be 0 or it will segfault. A size of 0 wouldn't make sense anyhow.
--- hw/nvram/eeprom_at24c.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/nvram/eeprom_at24c.c +5 -0
@@ -121,6 +121,11 @@ int at24c_eeprom_init(I2CSlave *i2c) {EEPROMState *ee =AT24C_EE (i2c); +if (!ee->rsize) { +ERR ("rom-size not allowed to be 0\n" ); +exit (1 ); + } + ee->mem =g_malloc0 (ee->rsize);if (ee->blk) {
Signature · 1 line
2.11.0
PATCH RESEND v3 2/3 nvram: at24c: use a sane default for "rom-size"
In reply to Wolfram Sang0 as "rom-size" will lead to an error message. Let's use the size of a small 24c01 which has 128 byte.
--- hw/nvram/eeprom_at24c.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
hw/nvram/eeprom_at24c.c +4 -1
@@ -28,6 +28,9 @@#define TYPE_AT24C_EE "at24c-eeprom"#define AT24C_EE (obj) OBJECT_CHECK(EEPROMState, (obj), TYPE_AT24C_EE) +/* default is the size of a 24c01 EEPROM */ +#define AT24C_ROMSIZE_DEFAULT 128 + typedef struct EEPROMState { I2CSlave parent_obj;
@@ -171,7 +174,7 @@ void at24c_eeprom_reset(DeviceState *state) }static Property at24c_eeprom_props[] = { -DEFINE_PROP_UINT32 ("rom-size" , EEPROMState, rsize,0 ), +DEFINE_PROP_UINT32 ("rom-size" , EEPROMState, rsize,AT24C_ROMSIZE_DEFAULT ),DEFINE_PROP_BOOL ("writable" , EEPROMState, writable, true),DEFINE_PROP_DRIVE ("drive" , EEPROMState, blk),DEFINE_PROP_END_OF_LIST ()
Signature · 1 line
2.11.0
PATCH RESEND v3 3/3 nvram: at24c: use standard error reporting
In reply to Wolfram SangReplace the ERR macro with error_report() because fprintf is deprecated. This also fixes the prefix printed out twice.
--- hw/nvram/eeprom_at24c.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
hw/nvram/eeprom_at24c.c +6 -11
@@ -13,6 +13,7 @@#include "hw/hw.h" #include "hw/i2c/i2c.h" #include "sysemu/block-backend.h" +#include "qemu/error-report.h" /* #define DEBUG_AT24C */
@@ -22,9 +23,6 @@#define DPRINTK (FMT , ...) do {} while (0)#endif -#define ERR (FMT , ...) fprintf(stderr, TYPE_AT24C_EE " : " FMT, \ - ## __VA_ARGS__) -#define TYPE_AT24C_EE "at24c-eeprom"#define AT24C_EE (obj) OBJECT_CHECK(EEPROMState, (obj), TYPE_AT24C_EE)
@@ -63,8 +61,7 @@ int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)if (ee->blk && ee->changed) {int len =blk_pwrite (ee->blk,0 , ee->mem, ee->rsize,0 );if (len != ee->rsize) { -ERR (TYPE_AT24C_EE -" : failed to write backing file\n" ); +error_report ("failed to write backing file" ); }DPRINTK ("Wrote to backing file\n" ); }
@@ -125,7 +122,7 @@ int at24c_eeprom_init(I2CSlave *i2c)EEPROMState *ee =AT24C_EE (i2c);if (!ee->rsize) { -ERR ("rom-size not allowed to be 0\n" ); +error_report ("rom-size not allowed to be 0" );exit (1 ); }
@@ -135,7 +132,7 @@ int at24c_eeprom_init(I2CSlave *i2c)int64_t len =blk_getlength (ee->blk);if (len != ee->rsize) { -ERR (TYPE_AT24C_EE " : Backing file size %lu != %u\n" , +error_report ("Backing file size %lu != %u" , (unsigned long )len, (unsigned )ee->rsize);exit (1 ); }
@@ -143,8 +140,7 @@ int at24c_eeprom_init(I2CSlave *i2c)if (blk_set_perm (ee->blk,BLK_PERM_CONSISTENT_READ |BLK_PERM_WRITE ,BLK_PERM_ALL , &error_fatal) <0 ) { -ERR (TYPE_AT24C_EE -" : Backing file incorrect permission\n" ); +error_report ("Backing file incorrect permission" );exit (1 ); } }
@@ -166,8 +162,7 @@ void at24c_eeprom_reset(DeviceState *state)int len =blk_pread (ee->blk,0 , ee->mem, ee->rsize);if (len != ee->rsize) { -ERR (TYPE_AT24C_EE -" : Failed initial sync with backing file\n" ); +error_report ("Failed initial sync with backing file" ); }DPRINTK ("Reset read backing file\n" ); }
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH v3 RESEND 3/3] nvram: at24c: use standard error reporting
Quoted 19 lines · Wolfram Sang
Replace the ERR macro with
error_report()because fprintf is deprecated. This also fixes the prefix printed out twice.Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Wolfram Sang <[email protected]>--- hw/nvram/eeprom_at24c.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)hw/nvram/eeprom_at24c.c +1
-2@@ -63,8 +61,7 @@ int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)if (ee->blk && ee->changed) {int len =blk_pwrite (ee->blk,0 , ee->mem, ee->rsize,0 );if (len != ee->rsize) { -ERR (TYPE_AT24C_EE -" : failed to write backing file\n" ); +error_report ("failed to write backing file" ); }DPRINTK ("Wrote to backing file\n" ); }
hw/nvram/eeprom_at24c.c int at24c_eeprom_init(I2CSlave *i2c) · line 127
Quoted 7 lines
hw/nvram/eeprom_at24c.c +1
-1@@ -125,7 +122,7 @@ int at24c_eeprom_init(I2CSlave *i2c)EEPROMState *ee =AT24C_EE (i2c);if (!ee->rsize) { -ERR ("rom-size not allowed to be 0\n" ); +error_report ("rom-size not allowed to be 0" );exit (1 ); }
Hi; if we're going to overhaul the error handling for this device, I think we might as well do it properly, by moving the code in this init function which can fail into a new device realize method. The realize method takes an Error** and can report failures that way rather than by causing QEMU to exit.
hw/nvram/eeprom_at24c.c void at24c_eeprom_reset(DeviceState *state) · line 168
Quoted 10 lines
hw/nvram/eeprom_at24c.c +1
-2@@ -166,8 +162,7 @@ void at24c_eeprom_reset(DeviceState *state)int len =blk_pread (ee->blk,0 , ee->mem, ee->rsize);if (len != ee->rsize) { -ERR (TYPE_AT24C_EE -" : Failed initial sync with backing file\n" ); +error_report ("Failed initial sync with backing file" ); }DPRINTK ("Reset read backing file\n" ); }
Errors in reset and event methods are a bit more awkward; error_report is an improvement in those cases.
thanks
-- PMM