Patchwork [v10,2/4] use uimage_reset to reload uImage

login
register
mail settings
Submitter Olivia Yin
Date Jan. 24, 2013, 1:26 a.m.
Message ID <1358990783-23048-3-git-send-email-hong-hua.yin@freescale.com>
Download mbox | patch
Permalink /patch/215098/
State New
Headers show

Comments

Olivia Yin - Jan. 24, 2013, 1:26 a.m.
Signed-off-by: Olivia Yin <hong-hua.yin@freescale.com>
---
 hw/loader.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 51 insertions(+), 13 deletions(-)
Alexander Graf - Jan. 24, 2013, 4:59 p.m.
On 24.01.2013, at 02:26, Olivia Yin wrote:

> Signed-off-by: Olivia Yin <hong-hua.yin@freescale.com>
> ---
> hw/loader.c |   64 +++++++++++++++++++++++++++++++++++++++++++++++------------
> 1 files changed, 51 insertions(+), 13 deletions(-)
> 
> diff --git a/hw/loader.c b/hw/loader.c
> index 4fa9965..288a1ba 100644
> --- a/hw/loader.c
> +++ b/hw/loader.c
> @@ -458,15 +458,15 @@ static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
>     return dstbytes;
> }
> 
> -/* Load a U-Boot image.  */
> -int load_uimage(const char *filename, hwaddr *ep,
> -                hwaddr *loadaddr, int *is_linux)
> +/* write uimage into memory */
> +static int uimage_physical_loader(const char *filename, hwaddr *ep, 
> +                                  uint8_t **data, hwaddr *loadaddr,
> +                                  int *is_linux, int reset)
> {
>     int fd;
>     int size;
>     uboot_image_header_t h;
>     uboot_image_header_t *hdr = &h;
> -    uint8_t *data = NULL;
>     int ret = -1;
> 
>     fd = open(filename, O_RDONLY | O_BINARY);
> @@ -507,10 +507,12 @@ int load_uimage(const char *filename, hwaddr *ep,
>             *is_linux = 0;
>     }
> 
> -    *ep = hdr->ih_ep;
> -    data = g_malloc(hdr->ih_size);
> +    if (ep) {
> +        *ep = hdr->ih_ep;
> +    }

This is a separate patch.

> +    *data = g_malloc(hdr->ih_size);
> 
> -    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
> +    if (read(fd, *data, hdr->ih_size) != hdr->ih_size) {
>         fprintf(stderr, "Error reading file\n");
>         goto out;
>     }
> @@ -520,11 +522,11 @@ int load_uimage(const char *filename, hwaddr *ep,
>         size_t max_bytes;
>         ssize_t bytes;
> 
> -        compressed_data = data;
> +        compressed_data = *data;
>         max_bytes = UBOOT_MAX_GUNZIP_BYTES;
> -        data = g_malloc(max_bytes);
> +        *data = g_malloc(max_bytes);
> 
> -        bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
> +        bytes = gunzip(*data, max_bytes, compressed_data, hdr->ih_size);
>         g_free(compressed_data);
>         if (bytes < 0) {
>             fprintf(stderr, "Unable to decompress gzipped image!\n");
> @@ -533,7 +535,11 @@ int load_uimage(const char *filename, hwaddr *ep,
>         hdr->ih_size = bytes;
>     }
> 
> -    rom_add_blob_fixed(filename, data, hdr->ih_size, hdr->ih_load);
> +    if (!reset) {
> +        rom_add_blob_fixed(filename, *data, hdr->ih_size, hdr->ih_load);

And there I thought you wanted to not use the rom framework anymore...


Alex

> +    } else {
> +        cpu_physical_memory_write(hdr->ih_load, *data, hdr->ih_size);
> +    }
> 
>     if (loadaddr)
>         *loadaddr = hdr->ih_load;
> @@ -541,12 +547,44 @@ int load_uimage(const char *filename, hwaddr *ep,
>     ret = hdr->ih_size;
> 
> out:
> -    if (data)
> -        g_free(data);
>     close(fd);
>     return ret;
> }
> 
> +static void uimage_reset(void *opaque)
> +{
> +    ImageFile *image = opaque;
> +    uint8_t *data = NULL;
> +    ssize_t size;
> +
> +    size = uimage_physical_loader(image->name, NULL, &data, &image->loadaddr, NULL, 1);
> +    if (size >0) {
> +        g_free(data);
> +    } else {
> +       error_report("failed to read image file: %s\n", image->name);
> +    }
> +}
> +
> +/* Load a U-Boot image.  */
> +int load_uimage(const char *filename, hwaddr *ep,
> +                hwaddr *loadaddr, int *is_linux)
> +{
> +    int size;
> +    ImageFile *image;
> +    uint8_t *data = NULL;
> +
> +    size = uimage_physical_loader(filename, ep, &data, loadaddr, is_linux, 0);
> +    if (size > 0) {
> +        g_free(data);
> +        image = g_malloc0(sizeof(*image));
> +        image->name = g_strdup(filename);
> +        image->loadaddr = *loadaddr;
> +        image->size = size;
> +        qemu_register_reset(uimage_reset, image);
> +    }
> +    return size;
> +}
> +
> /*
>  * Functions for reboot-persistent memory regions.
>  *  - used for vga bios and option roms.
> -- 
> 1.7.1
> 
> 
>

Patch

diff --git a/hw/loader.c b/hw/loader.c
index 4fa9965..288a1ba 100644
--- a/hw/loader.c
+++ b/hw/loader.c
@@ -458,15 +458,15 @@  static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src,
     return dstbytes;
 }
 
-/* Load a U-Boot image.  */
-int load_uimage(const char *filename, hwaddr *ep,
-                hwaddr *loadaddr, int *is_linux)
+/* write uimage into memory */
+static int uimage_physical_loader(const char *filename, hwaddr *ep, 
+                                  uint8_t **data, hwaddr *loadaddr,
+                                  int *is_linux, int reset)
 {
     int fd;
     int size;
     uboot_image_header_t h;
     uboot_image_header_t *hdr = &h;
-    uint8_t *data = NULL;
     int ret = -1;
 
     fd = open(filename, O_RDONLY | O_BINARY);
@@ -507,10 +507,12 @@  int load_uimage(const char *filename, hwaddr *ep,
             *is_linux = 0;
     }
 
-    *ep = hdr->ih_ep;
-    data = g_malloc(hdr->ih_size);
+    if (ep) {
+        *ep = hdr->ih_ep;
+    }
+    *data = g_malloc(hdr->ih_size);
 
-    if (read(fd, data, hdr->ih_size) != hdr->ih_size) {
+    if (read(fd, *data, hdr->ih_size) != hdr->ih_size) {
         fprintf(stderr, "Error reading file\n");
         goto out;
     }
@@ -520,11 +522,11 @@  int load_uimage(const char *filename, hwaddr *ep,
         size_t max_bytes;
         ssize_t bytes;
 
-        compressed_data = data;
+        compressed_data = *data;
         max_bytes = UBOOT_MAX_GUNZIP_BYTES;
-        data = g_malloc(max_bytes);
+        *data = g_malloc(max_bytes);
 
-        bytes = gunzip(data, max_bytes, compressed_data, hdr->ih_size);
+        bytes = gunzip(*data, max_bytes, compressed_data, hdr->ih_size);
         g_free(compressed_data);
         if (bytes < 0) {
             fprintf(stderr, "Unable to decompress gzipped image!\n");
@@ -533,7 +535,11 @@  int load_uimage(const char *filename, hwaddr *ep,
         hdr->ih_size = bytes;
     }
 
-    rom_add_blob_fixed(filename, data, hdr->ih_size, hdr->ih_load);
+    if (!reset) {
+        rom_add_blob_fixed(filename, *data, hdr->ih_size, hdr->ih_load);
+    } else {
+        cpu_physical_memory_write(hdr->ih_load, *data, hdr->ih_size);
+    }
 
     if (loadaddr)
         *loadaddr = hdr->ih_load;
@@ -541,12 +547,44 @@  int load_uimage(const char *filename, hwaddr *ep,
     ret = hdr->ih_size;
 
 out:
-    if (data)
-        g_free(data);
     close(fd);
     return ret;
 }
 
+static void uimage_reset(void *opaque)
+{
+    ImageFile *image = opaque;
+    uint8_t *data = NULL;
+    ssize_t size;
+
+    size = uimage_physical_loader(image->name, NULL, &data, &image->loadaddr, NULL, 1);
+    if (size >0) {
+        g_free(data);
+    } else {
+       error_report("failed to read image file: %s\n", image->name);
+    }
+}
+
+/* Load a U-Boot image.  */
+int load_uimage(const char *filename, hwaddr *ep,
+                hwaddr *loadaddr, int *is_linux)
+{
+    int size;
+    ImageFile *image;
+    uint8_t *data = NULL;
+
+    size = uimage_physical_loader(filename, ep, &data, loadaddr, is_linux, 0);
+    if (size > 0) {
+        g_free(data);
+        image = g_malloc0(sizeof(*image));
+        image->name = g_strdup(filename);
+        image->loadaddr = *loadaddr;
+        image->size = size;
+        qemu_register_reset(uimage_reset, image);
+    }
+    return size;
+}
+
 /*
  * Functions for reboot-persistent memory regions.
  *  - used for vga bios and option roms.