Message ID | 1407152873-16772-2-git-send-email-rjones@redhat.com |
---|---|
State | New |
Headers | show |
Richard W.M. Jones writes: > As the name suggests this lets you load a ROM/disk image that is > gzipped. It is uncompressed before storing it in guest memory. > <snip> > + > + /* Is it a gzip-compressed file? */ > + if (len < 2 || > + compressed_data[0] != '\x1f' || > + compressed_data[1] != '\x8b') { > + goto out; > + } I was looking to see if the zlib library provided the magic numbers or a verification routine here but I couldn't find it. > + > + if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) { > + max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES; > + } > + > + data = g_malloc(max_sz); > + bytes = gunzip(data, max_sz, compressed_data, len); > + if (bytes < 0) { > + fprintf(stderr, "%s: unable to decompress gzipped kernel file\n", > + filename); > + goto out; > + } > + > + rom_add_blob_fixed(filename, data, bytes, addr); > + ret = bytes; > + > + out: > + g_free(compressed_data); > + g_free(data); > + return ret; > +} > + > /* > * Functions for reboot-persistent memory regions. > * - used for vga bios and option roms. > diff --git a/include/hw/loader.h b/include/hw/loader.h > index 796cbf9..00c9117 100644 > --- a/include/hw/loader.h > +++ b/include/hw/loader.h > @@ -15,6 +15,7 @@ int get_image_size(const char *filename); > int load_image(const char *filename, uint8_t *addr); /* deprecated */ > int load_image_targphys(const char *filename, hwaddr, > uint64_t max_sz); > +int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz); > > #define ELF_LOAD_FAILED -1 > #define ELF_LOAD_NOT_ELF -2 Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
On Tue, Aug 05, 2014 at 10:57:26AM +0100, Alex Bennée wrote: > > Richard W.M. Jones writes: > > > As the name suggests this lets you load a ROM/disk image that is > > gzipped. It is uncompressed before storing it in guest memory. > > > > Signed-off-by: Richard W.M. Jones <rjones@redhat.com> > <snip> > > + /* Is it a gzip-compressed file? */ > > + if (len < 2 || > > + compressed_data[0] != '\x1f' || > > + compressed_data[1] != '\x8b') { > > + goto out; > > + } > <snip> > > Hmm serves me right for not compiling this first. I had to explicit > literals to get this to compile: > > Modified hw/core/loader.c > diff --git a/hw/core/loader.c b/hw/core/loader.c > index e773aab..83136e8 100644 > --- a/hw/core/loader.c > +++ b/hw/core/loader.c > @@ -599,8 +599,8 @@ int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz) > > /* Is it a gzip-compressed file? */ > if (len < 2 || > - compressed_data[0] != '\x1f' || > - compressed_data[1] != '\x8b') { > + compressed_data[0] != 0x1f || > + compressed_data[1] != 0x8b ) { > goto out; > } > > Otherwise I get: > hw/core/loader.c: In function ‘load_image_gzipped’: > hw/core/loader.c:603:9: error: comparison is always true due to limited range of data type [-Werror=type-limits] > compressed_data[1] != '\x8b') { This is probably because I only compiled and tested this on aarch64 where char == unsigned char (not signed char). I'll fix this in v6, thanks. Rich.
diff --git a/hw/core/loader.c b/hw/core/loader.c index 2bf6b8f..e773aab 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -577,6 +577,54 @@ int load_ramdisk(const char *filename, hwaddr addr, uint64_t max_sz) return load_uboot_image(filename, NULL, &addr, NULL, IH_TYPE_RAMDISK); } +/* This simply prevents g_malloc in the function below from allocating + * a huge amount of memory, by placing a limit on the maximum + * uncompressed image size that load_image_gzipped will read. + */ +#define LOAD_IMAGE_MAX_GUNZIP_BYTES (256 << 20) + +/* Load a gzip-compressed kernel. */ +int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz) +{ + uint8_t *compressed_data = NULL; + uint8_t *data = NULL; + gsize len; + ssize_t bytes; + int ret = -1; + + if (!g_file_get_contents(filename, (char **) &compressed_data, &len, + NULL)) { + goto out; + } + + /* Is it a gzip-compressed file? */ + if (len < 2 || + compressed_data[0] != '\x1f' || + compressed_data[1] != '\x8b') { + goto out; + } + + if (max_sz > LOAD_IMAGE_MAX_GUNZIP_BYTES) { + max_sz = LOAD_IMAGE_MAX_GUNZIP_BYTES; + } + + data = g_malloc(max_sz); + bytes = gunzip(data, max_sz, compressed_data, len); + if (bytes < 0) { + fprintf(stderr, "%s: unable to decompress gzipped kernel file\n", + filename); + goto out; + } + + rom_add_blob_fixed(filename, data, bytes, addr); + ret = bytes; + + out: + g_free(compressed_data); + g_free(data); + return ret; +} + /* * Functions for reboot-persistent memory regions. * - used for vga bios and option roms. diff --git a/include/hw/loader.h b/include/hw/loader.h index 796cbf9..00c9117 100644 --- a/include/hw/loader.h +++ b/include/hw/loader.h @@ -15,6 +15,7 @@ int get_image_size(const char *filename); int load_image(const char *filename, uint8_t *addr); /* deprecated */ int load_image_targphys(const char *filename, hwaddr, uint64_t max_sz); +int load_image_gzipped(const char *filename, hwaddr addr, uint64_t max_sz); #define ELF_LOAD_FAILED -1 #define ELF_LOAD_NOT_ELF -2
As the name suggests this lets you load a ROM/disk image that is gzipped. It is uncompressed before storing it in guest memory. Signed-off-by: Richard W.M. Jones <rjones@redhat.com> --- hw/core/loader.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/loader.h | 1 + 2 files changed, 49 insertions(+)