diff mbox

[1/2] hw/nand: reject read-only drives

Message ID 1319007353-16041-2-git-send-email-juha.riihimaki@nokia.com
State New
Headers show

Commit Message

Juha.Riihimaki@nokia.com Oct. 19, 2011, 6:55 a.m. UTC
From: Juha Riihimäki <juha.riihimaki@nokia.com>

Signed-off-by: Juha Riihimäki <juha.riihimaki@nokia.com>
---
 hw/nand.c |   23 +++++++++++++++--------
 1 files changed, 15 insertions(+), 8 deletions(-)

Comments

Markus Armbruster Oct. 19, 2011, 4:56 p.m. UTC | #1
juha.riihimaki@nokia.com writes:

> From: Juha Riihimäki <juha.riihimaki@nokia.com>
>
> Signed-off-by: Juha Riihimäki <juha.riihimaki@nokia.com>
> ---
>  hw/nand.c |   23 +++++++++++++++--------
>  1 files changed, 15 insertions(+), 8 deletions(-)
>
> diff --git a/hw/nand.c b/hw/nand.c
> index c27783e..da6529d 100644
> --- a/hw/nand.c
> +++ b/hw/nand.c
> @@ -19,6 +19,7 @@
>  # include "flash.h"
>  # include "blockdev.h"
>  # include "sysbus.h"
> +#include "qemu-error.h"
>  
>  # define NAND_CMD_READ0		0x00
>  # define NAND_CMD_READ1		0x01
> @@ -384,18 +385,24 @@ static int nand_device_init(SysBusDevice *dev)
>          nand_init_2048(s);
>          break;
>      default:
> -        hw_error("%s: Unsupported NAND block size.\n", __func__);
> +        error_report("Unsupported NAND block size");
> +        return -1;

Not mentioned in commit message.  Separate patch?

>      }
>  
> -    pagesize = 1 << s->oob_shift;
>      s->mem_oob = 1;
> -    if (s->bdrv && bdrv_getlength(s->bdrv) >=
> +    if (s->bdrv) {
> +        if (bdrv_is_read_only(s->bdrv)) {
> +            error_report("Can't use a read-only drive");
> +            return -1;
> +        }
> +        if (bdrv_getlength(s->bdrv) >=
>              (s->pages << s->page_shift) + (s->pages << s->oob_shift)) {
> -        pagesize = 0;
> -        s->mem_oob = 0;
> -    }
> -
> -    if (!s->bdrv) {
> +            pagesize = 0;
> +            s->mem_oob = 0;
> +        } else {
> +            pagesize = 1 << s->oob_shift;
> +        }
> +    } else {
>          pagesize += 1 << s->page_shift;

Doesn't this use pagesize uninitialized?

>      }
>      if (pagesize) {
diff mbox

Patch

diff --git a/hw/nand.c b/hw/nand.c
index c27783e..da6529d 100644
--- a/hw/nand.c
+++ b/hw/nand.c
@@ -19,6 +19,7 @@ 
 # include "flash.h"
 # include "blockdev.h"
 # include "sysbus.h"
+#include "qemu-error.h"
 
 # define NAND_CMD_READ0		0x00
 # define NAND_CMD_READ1		0x01
@@ -384,18 +385,24 @@  static int nand_device_init(SysBusDevice *dev)
         nand_init_2048(s);
         break;
     default:
-        hw_error("%s: Unsupported NAND block size.\n", __func__);
+        error_report("Unsupported NAND block size");
+        return -1;
     }
 
-    pagesize = 1 << s->oob_shift;
     s->mem_oob = 1;
-    if (s->bdrv && bdrv_getlength(s->bdrv) >=
+    if (s->bdrv) {
+        if (bdrv_is_read_only(s->bdrv)) {
+            error_report("Can't use a read-only drive");
+            return -1;
+        }
+        if (bdrv_getlength(s->bdrv) >=
             (s->pages << s->page_shift) + (s->pages << s->oob_shift)) {
-        pagesize = 0;
-        s->mem_oob = 0;
-    }
-
-    if (!s->bdrv) {
+            pagesize = 0;
+            s->mem_oob = 0;
+        } else {
+            pagesize = 1 << s->oob_shift;
+        }
+    } else {
         pagesize += 1 << s->page_shift;
     }
     if (pagesize) {