mipsnet incorrect device ID fix
PATCH 1/1 mipsnet incorrect device ID fix
The mipsnet device returns wrong values for device ID, since it returns the contents of the pointer rather that the contents of the device ID string. Also the contents of the string is returned such that the order is host endianess dependent. The patch fixes both these issues.
hw/mipsnet.c +15 -2
@@ -101,6 +101,19 @@mipsnet_update_irq (s); } +static uint32_t bytes_to_int32 (const unsigned char *arr) +{ +int i; +uint32_t ret =0 ; +int nbytes =sizeof (int32_t ); + +for (i =0 ; i < nbytes; i++) { + ret = ret <<8 | arr[nbytes -1 - i]; + } + +return ret; +} +static uint32_t mipsnet_ioport_read (void *opaque,uint32_t addr) {MIPSnetState *s = opaque;
@@ -110,10 +123,10 @@ addr &=0x3f ;switch (addr) {case MIPSNET_DEV_ID : - ret = *((uint32_t *)&devid); + ret =bytes_to_int32 (devid);break ;case MIPSNET_DEV_ID +4 : - ret = *((uint32_t *)(&devid +4 )); + ret =bytes_to_int32 (devid +4 );break ;case MIPSNET_BUSY : ret = s->busy;
Quoted 15 lines · Vijay Kumar
The mipsnet device returns wrong values for device ID, since it returns the contents of the pointer rather that the contents of the device ID string. Also the contents of the string is returned such that the order is host endianess dependent. The patch fixes both these issues.
Signed-off-by Vijay Kumar B. <[email protected]>hw/mipsnet.c +2
-0@@ -101,6 +101,19 @@mipsnet_update_irq (s); } +static uint32_t bytes_to_int32 (const unsigned char *arr) +{
hw/mipsnet.c line 115
Quoted 10 lines
hw/mipsnet.c +10
-0+int i; +uint32_t ret =0 ; +int nbytes =sizeof (int32_t ); + +for (i =0 ; i < nbytes; i++) { + ret = ret <<8 | arr[nbytes -1 - i]; + } + +return ret; +}
Why not use le32_to_cpu() which does the same, but in an optimized way?
hw/mipsnet.c line 126
Quoted 9 lines
hw/mipsnet.c +1
-1static uint32_t mipsnet_ioport_read (void *opaque,uint32_t addr) {MIPSnetState *s = opaque;@@ -110,10 +123,10 @@ addr &=0x3f ;switch (addr) {case MIPSNET_DEV_ID : - ret = *((uint32_t *)&devid); + ret =bytes_to_int32 (devid);
That's the '&' which is wrong here. The string can be accessed with *((uint32_t *)devid). So you can simply use:
ret = le32_to_cpu(*((uint32_t *)devid))
hw/mipsnet.c line 129
Quoted 4 lines
hw/mipsnet.c +1
-1break ;case MIPSNET_DEV_ID +4 : - ret = *((uint32_t *)(&devid +4 )); + ret =bytes_to_int32 (devid +4 );
ret = le32_to_cpu(*((uint32_t *)devid + 4));
Quoted 6 lines
hw/mipsnet.c +0
-0break ;case MIPSNET_BUSY : ret = s->busy;
Signature · 4 lines
.''`. Aurelien Jarno | GPG: 1024D/F1BCDB73 : :' : Debian developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net
Quoted 4 lines
That's the '&' which is wrong here. The string can be accessed with *((uint32_t *)devid). So you can simply use:
ret = le32_to_cpu(*((uint32_t *)devid))
No you can't. Even ignoring the aliasing violation, devid might not be sufficiently aligned.
Paul
Quoted 8 lines · Paul Brook
Quoted 4 lines
That's the '&' which is wrong here. The string can be accessed with *((uint32_t *)devid). So you can simply use:
ret = le32_to_cpu(*((uint32_t *)devid))No you can't. Even ignoring the aliasing violation, devid might not be sufficiently aligned.
Oops you are right, I have been troubled by the original code. As suggested by Paul on IRC, the best is most probably to hardcode the hex values and put the string on a comment.
Signature · 4 lines
.''`. Aurelien Jarno | GPG: 1024D/F1BCDB73 : :' : Debian developer | Electrical Engineer `. `' [email protected] | [email protected] `- people.debian.org/~aurel32 | www.aurel32.net