Skip to contentSkip to conversation

Misc eeprom_at24c clean ups

PATCH In review BALATON Zoltan 2 participants 13 messages Mar 1, 2025 – Mar 3, 2025

lore
Share & tools
lore
#0 lore raw

cover Misc eeprom_at24c clean ups

These are some small misc clean ups in hw/nvram/eeprom_at24c.

Regards,
BALATON Zoltan

BALATON Zoltan (4):
hw/nvram/eeprom_at24c: Use OBJECT_DECLARE_SIMPLE_TYPE hw/nvram/eeprom_at24c: Remove ERR macro that calls fprintf to stderr hw/nvram/eeprom_at24c: Remove memset after g_malloc0 hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

 hw/nvram/eeprom_at24c.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
Signature · 1 line
2.30.9
#7 lore raw

PATCH 4/4 hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

The init_rom can write values to the beginning of the memory but these are overwritten by values from a backing file that covers the whole memory. Do the init_rom handling only if it would not be overwritten.

---
 hw/nvram/eeprom_at24c.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
hw/nvram/eeprom_at24c.c +2 -4
index 78c81bea77..ff7a21eee7 100644
@@ -191,10 +191,6 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
 
     ee->mem = g_malloc0(ee->rsize);
 
-    if (ee->init_rom) {
-        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
-    }
-
     if (ee->blk) {
         int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
 
@@ -204,6 +200,8 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
             return;
         }
         DPRINTK("Reset read backing file\n");
+    } else if (ee->init_rom) {
+        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
     }
 
     /*
Signature · 1 line
2.30.9
#5 lore raw

PATCH 3/4 hw/nvram/eeprom_at24c: Remove memset after g_malloc0

In reply to BALATON Zoltan

Calling memset to zero memory is not needed after g_malloc0 which already clears memory. These used to be in separate functions but after some patches the memset ended up after g_malloc0 and thus can be dropped.

Fixes 4f2c6448c3 (hw/nvram/eeprom_at24c: Make reset behavior more like hardware)
---
 hw/nvram/eeprom_at24c.c | 1 -
 1 file changed, 1 deletion(-)
hw/nvram/eeprom_at24c.c +0 -1
index 9f606842eb..78c81bea77 100644
@@ -190,7 +190,6 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
     }
 
     ee->mem = g_malloc0(ee->rsize);
-    memset(ee->mem, 0, ee->rsize);
 
     if (ee->init_rom) {
         memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
Signature · 1 line
2.30.9
#1 lore raw

PATCH 1/4 hw/nvram/eeprom_at24c: Use OBJECT_DECLARE_SIMPLE_TYPE

In reply to BALATON Zoltan

No need to open code it so use the simple object type declaration.

---
 hw/nvram/eeprom_at24c.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
hw/nvram/eeprom_at24c.c +1 -3
index a40cc5dd15..2ae03935d4 100644
@@ -30,9 +30,7 @@
                             ## __VA_ARGS__)
 
 #define TYPE_AT24C_EE "at24c-eeprom"
-typedef struct EEPROMState EEPROMState;
-DECLARE_INSTANCE_CHECKER(EEPROMState, AT24C_EE,
-                         TYPE_AT24C_EE)
+OBJECT_DECLARE_SIMPLE_TYPE(EEPROMState, AT24C_EE)
 
 struct EEPROMState {
     I2CSlave parent_obj;
Signature · 1 line
2.30.9
#3 lore raw

PATCH 2/4 hw/nvram/eeprom_at24c: Remove ERR macro that calls fprintf to stderr

In reply to BALATON Zoltan

In the realize method error_setg can be used like other places there already do. The other usage can be replaced with error_report which is the preferred way instead of directly printing to stderr.

---
 hw/nvram/eeprom_at24c.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
hw/nvram/eeprom_at24c.c +5 -7
index 2ae03935d4..9f606842eb 100644
@@ -10,6 +10,7 @@
 #include "qemu/osdep.h"
 
 #include "qapi/error.h"
+#include "qemu/error-report.h"
 #include "qemu/module.h"
 #include "hw/i2c/i2c.h"
 #include "hw/nvram/eeprom_at24c.h"
@@ -26,9 +27,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"
 OBJECT_DECLARE_SIMPLE_TYPE(EEPROMState, AT24C_EE)
 
@@ -75,8 +73,7 @@ int at24c_eeprom_event(I2CSlave *s, enum i2c_event event)
         if (ee->blk && ee->changed) {
             int ret = blk_pwrite(ee->blk, 0, ee->rsize, ee->mem, 0);
             if (ret < 0) {
-                ERR(TYPE_AT24C_EE
-                        " : failed to write backing file\n");
+                error_report("%s: failed to write backing file", __func__);
             }
             DPRINTK("Wrote to backing file\n");
         }
@@ -203,8 +200,9 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
         int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
 
         if (ret < 0) {
-            ERR(TYPE_AT24C_EE
-                    " : Failed initial sync with backing file\n");
+            error_setg(errp, "%s: Failed initial sync with backing file",
+                       TYPE_AT24C_EE);
+            return;
         }
         DPRINTK("Reset read backing file\n");
     }
Signature · 1 line
2.30.9
Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] hw/nvram/eeprom_at24c: Remove memset after g_malloc0

In reply to BALATON Zoltan
Quoted 10 lines · BALATON Zoltan

Calling memset to zero memory is not needed after g_malloc0 which already clears memory. These used to be in separate functions but after some patches the memset ended up after g_malloc0 and thus can be dropped.

Fixes 4f2c6448c3 (hw/nvram/eeprom_at24c: Make reset behavior more like hardware)
---
  hw/nvram/eeprom_at24c.c | 1 -
  1 file changed, 1 deletion(-)
#8 lore raw

Re: [PATCH 4/4] hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

In reply to BALATON Zoltan
Quoted 25 lines · BALATON Zoltan

The init_rom can write values to the beginning of the memory but these are overwritten by values from a backing file that covers the whole memory. Do the init_rom handling only if it would not be overwritten.

---
  hw/nvram/eeprom_at24c.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
hw/nvram/eeprom_at24c.c +0 -4
index 78c81bea77..ff7a21eee7 100644
@@ -191,10 +191,6 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
 
      ee->mem = g_malloc0(ee->rsize);
 
-    if (ee->init_rom) {
-        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
-    }
-
      if (ee->blk) {
          int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
 

hw/nvram/eeprom_at24c.c static void at24c_eeprom_realize(DeviceState *dev, Error **errp) · line 203

Quoted 4 lines
hw/nvram/eeprom_at24c.c +1 -0
@@ -204,6 +200,8 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp)
              return;
          }
          DPRINTK("Reset read backing file\n");
+    } else if (ee->init_rom) {

Don't you want to keep overwritting the init_rom[] buffer?

IOW why not s/else//?

Quoted 4 lines
hw/nvram/eeprom_at24c.c +1 -0
+        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
      }
 
      /*
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] hw/nvram/eeprom_at24c: Remove ERR macro that calls fprintf to stderr

In reply to BALATON Zoltan
Quoted 8 lines · BALATON Zoltan

In the realize method error_setg can be used like other places there already do. The other usage can be replaced with error_report which is the preferred way instead of directly printing to stderr.

---
  hw/nvram/eeprom_at24c.c | 12 +++++-------
  1 file changed, 5 insertions(+), 7 deletions(-)
#9 lore raw

Re: [PATCH 4/4] hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

In reply to Philippe Mathieu-Daudé
Quoted 32 lines · Philippe Mathieu-Daudé
Quoted 24 lines · BALATON Zoltan

The init_rom can write values to the beginning of the memory but these are overwritten by values from a backing file that covers the whole memory. Do the init_rom handling only if it would not be overwritten.

---
  hw/nvram/eeprom_at24c.c | 6 ++----
  1 file changed, 2 insertions(+), 4 deletions(-)
hw/nvram/eeprom_at24c.c +0 -3
index 78c81bea77..ff7a21eee7 100644
@@ -191,10 +191,6 @@ static void at24c_eeprom_realize(DeviceState *dev, 
 Error **errp)
        ee->mem = g_malloc0(ee->rsize);
  -    if (ee->init_rom) {
-        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
-    }
-
      if (ee->blk) {
          int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
  @@ -204,6 +200,8 @@ static void at24c_eeprom_realize(DeviceState *dev, 

Error **errp)

Quoted 4 lines
              return;
          }
          DPRINTK("Reset read backing file\n");

+ } else if (ee->init_rom) {

Don't you want to keep overwritting the init_rom[] buffer?

Quoted 1 line

IOW why not s/else//?

I've tried to explain that in the commit message. Current behaviour is to use backing file content overwriting init_rom content. Removing else here would change that and init_rom would overwrite data read from backing file. I think normally init_rom is used only if there's no backing file (provides default content) but should not overwrite backing file content (especially leaving the file unchanged and only change it in memory). So I don't see why would that be useful.

Regards,
BALATON Zoltan

Quoted 5 lines
Quoted 3 lines
+        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee->rsize));
      }
        /*
#10 lore raw

Re: [PATCH 4/4] hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

Quoted 31 lines · BALATON Zoltan
Quoted 30 lines · Philippe Mathieu-Daudé
Quoted 28 lines · BALATON Zoltan

The init_rom can write values to the beginning of the memory but these are overwritten by values from a backing file that covers the whole memory. Do the init_rom handling only if it would not be overwritten.

---
  hw/nvram/eeprom_at24c.c | 6 ++----

  1 file changed, 2 insertions(+), 4 deletions(-)
hw/nvram/eeprom_at24c.c +0 -3
index 78c81bea77..ff7a21eee7 100644
@@ -191,10 +191,6 @@ static void at24c_eeprom_realize(DeviceState 
 *dev, Error **errp)
         ee->mem = g_malloc0(ee->rsize);
   -    if (ee->init_rom) {
-        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee- 
 >rsize));
-    }
-
       if (ee->blk) {
           int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);

  @@ -204,6 +200,8 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp) return;
          }
          DPRINTK("Reset read backing file\n");
+    } else if (ee->init_rom) {

Quoted 5 lines
Quoted 3 lines

Don't you want to keep overwritting the init_rom[] buffer?

IOW why not s/else//?

I've tried to explain that in the commit message. Current behaviour is to use backing file content overwriting init_rom content. Removing else here would change that and init_rom would overwrite data read from backing file. I think normally

OK, I'll amend in description:

---

Quoted 1 line

init_rom is used only if there's no backing file (provides default content) but should not overwrite backing file content (especially leaving the file unchanged and only change it in memory).

---

Quoted 11 lines

So I don't see why would that be useful.

Regards,
BALATON Zoltan

Quoted 6 lines
Quoted 4 lines

+        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee-

Quoted 1 line

rsize));

      }
        /*

#11 lore raw

Re: [PATCH 4/4] hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

Quoted 36 lines · Philippe Mathieu-Daudé
Quoted 35 lines · BALATON Zoltan
Quoted 33 lines · Philippe Mathieu-Daudé
Quoted 23 lines · BALATON Zoltan

The init_rom can write values to the beginning of the memory but these are overwritten by values from a backing file that covers the whole memory. Do the init_rom handling only if it would not be overwritten.

---
  hw/nvram/eeprom_at24c.c | 6 ++----

  1 file changed, 2 insertions(+), 4 deletions(-)
hw/nvram/eeprom_at24c.c +0 -3
index 78c81bea77..ff7a21eee7 100644
@@ -191,10 +191,6 @@ static void at24c_eeprom_realize(DeviceState 
 *dev, Error **errp)
         ee->mem = g_malloc0(ee->rsize);
   -    if (ee->init_rom) {
-        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee- 
 >rsize));
-    }
-
       if (ee->blk) {
           int ret = blk_pread(ee->blk, 0, ee->rsize, ee->mem, 0);
Quoted 5 lines

  @@ -204,6 +200,8 @@ static void at24c_eeprom_realize(DeviceState *dev, Error **errp) return;
          }
          DPRINTK("Reset read backing file\n");
+    } else if (ee->init_rom) {

Don't you want to keep overwritting the init_rom[] buffer?

IOW why not s/else//?

Quoted 7 lines
Quoted 1 line

I've tried to explain that in the commit message. Current behaviour is to use backing file content overwriting init_rom content. Removing else here would change that and init_rom would overwrite data read from backing file. I think normally

OK, I'll amend in description:

---

Quoted 1 line

init_rom is used only if there's no backing file (provides default content) but should not overwrite backing file content (especially leaving the file unchanged and only change it in memory).

---

Reworded as:

---
The init_rom[] can write values to the beginning of the memory but these are overwritten by values from a backing file that covers the whole memory.

init_rom[] is used only if there's no backing file (provides default content) but should not overwrite backing file content (especially leaving the file unchanged and only change it in memory). Do the init_rom[] handling only if it would not be overwritten.
---

Quoted 15 lines
Quoted 11 lines

So I don't see why would that be useful.

Regards,
BALATON Zoltan

Quoted 6 lines
Quoted 4 lines

+        memcpy(ee->mem, ee->init_rom, MIN(ee->init_rom_size, ee-

Quoted 1 line

rsize));

      }
        /*

#12 lore raw
In reply to BALATON Zoltan
Quoted 7 lines · BALATON Zoltan

These are some small misc clean ups in hw/nvram/eeprom_at24c.

BALATON Zoltan (4):
hw/nvram/eeprom_at24c: Use OBJECT_DECLARE_SIMPLE_TYPE hw/nvram/eeprom_at24c: Remove ERR macro that calls fprintf to stderr hw/nvram/eeprom_at24c: Remove memset after g_malloc0 hw/nvram/eeprom_at24c: Reorganise init to avoid overwriting values

Thanks, series queued.