improvements in uImage loading
PATCH v2 1/1 improvements in uImage loading
These are fixes and enhancements for loading uImages, a kernel format defined by the u-boot bootloader. These are needed in order to merge PowerPC 440 KVM support.
There are a few existing users of the uImage loader in qemu, but while I cannot test these patches, they shouldn't cause any impact (other than adding gzip loading capability).
The first two patches were modified based on comments from François Revol and Anthony Liguori.
-Hollis
PATCH 1/5 uImage: don't leak file data or file descriptor
---
Changes from v1:
- Combine the failure and success return paths to avoid leaking the file descriptor as well.
--- loader.c | 19 ++++++++++--------- 1 files changed, 10 insertions(+), 9 deletions(-)
loader.c +10 -9
@@ -354,6 +354,7 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux)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 );if (fd <0 )
@@ -361,23 +362,23 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux) size =read (fd, hdr,sizeof (uboot_image_header_t));if (size <0 ) - goto fail; + goto out;bswap_uboot_header (hdr);if (hdr->ih_magic !=IH_MAGIC ) - goto fail; + goto out;/* TODO: Implement Multi-File images. */ if (hdr->ih_type ==IH_TYPE_MULTI ) {fprintf (stderr,"Unable to load multi-file u-boot images\n" ); - goto fail; + goto out; }/* TODO: Implement compressed images. */ if (hdr->ih_comp !=IH_COMP_NONE ) {fprintf (stderr,"Unable to load compressed u-boot images\n" ); - goto fail; + goto out; }/* TODO: Check CPU type. */
@@ -391,20 +392,20 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux) *ep = hdr->ih_ep; data =qemu_malloc (hdr->ih_size);if (!data) - goto fail; + goto out;if (read (fd, data, hdr->ih_size) != hdr->ih_size) {fprintf (stderr,"Error reading file\n" ); - goto fail; + goto out; }cpu_physical_memory_write_rom (hdr->ih_load, data, hdr->ih_size); -return hdr->ih_size; + ret = hdr->ih_size; -fail: +out:if (data)qemu_free (data);close (fd); -return -1 ; +return ret; }
Signature · 1 line
1.5.6.5
PATCH 2/5 uImage: implement gzip support
In reply to Hollis BlanchardBased on gzip uImage loading code from u-boot.
---
Changes from v1:
- Include u-boot copyright notice and GPL license.
- Use a fixed size buffer for uncompressed data.
- Invert ih_comp test to properly handle LZMA compressed images.
--- loader.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 136 insertions(+), 4 deletions(-)
loader.c +136 -4
@@ -20,12 +20,37 @@* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. +* +* Gunzip functionality in this file is derived from u-boot: +* +* (C) Copyright 2008 Semihalf +* +* (C) Copyright 2000-2005 +* Wolfgang Denk, DENX Software Engineering, [email protected]. +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License as +* published by the Free Software Foundation; either version 2 of +* the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place, Suite 330, Boston, +* MA 02111-1307 USA */ +#include "qemu-common.h" #include "disas.h" #include "sysemu.h" #include "uboot_image.h" +#include <zlib.h> +/* return the size or -1 if error */ int get_image_size (const char *filename) {
@@ -345,10 +370,94 @@ static void bswap_uboot_header(uboot_image_header_t *hdr)#endif } + +#define ZALLOC_ALIGNMENT 16 + +static void *zalloc (void *x,unsigned items,unsigned size) +{ +void *p; + + size *= items; + size = (size +ZALLOC_ALIGNMENT -1 ) & ~(ZALLOC_ALIGNMENT -1 ); + + p =qemu_malloc (size); + +return (p); +} + +static void zfree (void *x,void *addr,unsigned nb) +{ +qemu_free (addr); +} + + +#define HEAD_CRC 2 +#define EXTRA_FIELD 4 +#define ORIG_NAME 8 +#define COMMENT 0x10 +#define RESERVED 0xe0 + +#define DEFLATED 8 + +/* This is the maximum in uboot, so if a uImage overflows this, it would +* overflow on real hardware too. */ +#define UBOOT_MAX_GUNZIP_BYTES 0x800000 + +static ssize_t gunzip (void *dst,size_t dstlen,uint8_t *src, +size_t srclen) +{ +z_stream s; +ssize_t dstbytes; +int r, i, flags; + +/* skip header */ + i =10 ; + flags = src[3 ]; +if (src[2 ] !=DEFLATED || (flags &RESERVED ) !=0 ) { +puts ("Error: Bad gzipped data\n" ); +return -1 ; + } +if ((flags &EXTRA_FIELD ) !=0 ) + i =12 + src[10 ] + (src[11 ] <<8 ); +if ((flags &ORIG_NAME ) !=0 ) +while (src[i++] !=0 ) + ; +if ((flags &COMMENT ) !=0 ) +while (src[i++] !=0 ) + ; +if ((flags &HEAD_CRC ) !=0 ) + i +=2 ; +if (i >= srclen) { +puts ("Error: gunzip out of data in header\n" ); +return -1 ; + } + + s.zalloc = zalloc; + s.zfree = (free_func )zfree; + + r =inflateInit2 (&s, -MAX_WBITS ); +if (r !=Z_OK ) { +printf ("Error: inflateInit2() returned %d\n" , r); +return (-1 ); + } + s.next_in = src + i; + s.avail_in = srclen - i; + s.next_out = dst; + s.avail_out = dstlen; + r =inflate (&s,Z_FINISH ); +if (r !=Z_OK && r !=Z_STREAM_END ) { +printf ("Error: inflate() returned %d\n" , r); +return -1 ; + } + dstbytes = s.next_out - (unsigned char *) dst; +inflateEnd (&s); + +return dstbytes; +} +/* Load a U-Boot image. */ int load_uboot (const char *filename,target_ulong *ep,int *is_linux) { -int fd;int size;uboot_image_header_t h;
@@ -375,9 +484,14 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux) goto out; } -/* TODO: Implement compressed images. */ -if (hdr->ih_comp !=IH_COMP_NONE ) { -fprintf (stderr,"Unable to load compressed u-boot images\n" ); +switch (hdr->ih_comp) { +case IH_COMP_NONE : +case IH_COMP_GZIP : +break ; +default : +fprintf (stderr, +"Unable to load u-boot images with compression type %d\n" , + hdr->ih_comp); goto out; }
@@ -399,6 +513,24 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux) goto out; } +if (hdr->ih_comp ==IH_COMP_GZIP ) { +uint8_t *compressed_data; +size_t max_bytes; +ssize_t bytes; + + compressed_data = data; + max_bytes =UBOOT_MAX_GUNZIP_BYTES ; + data =qemu_malloc (max_bytes); + + bytes =gunzip (data, max_bytes, compressed_data, hdr->ih_size); +qemu_free (compressed_data); +if (bytes <0 ) { +fprintf (stderr,"Unable to decompress gzipped image!\n" ); + goto out; + } + hdr->ih_size = bytes; + } +cpu_physical_memory_write_rom (hdr->ih_load, data, hdr->ih_size); ret = hdr->ih_size;
Signature · 1 line
1.5.6.5
PATCH 3/5 uImage: return base load address
In reply to Hollis BlanchardReturn the base address at which the image was loaded so that callers may keep track of currently occupied guest memory.
--- hw/an5206.c | 2 +- hw/arm_boot.c | 3 ++- hw/dummy_m68k.c | 2 +- hw/mcf5208.c | 2 +- loader.c | 6 +++++- sysemu.h | 3 ++- 6 files changed, 12 insertions(+), 6 deletions(-)
hw/an5206.c +1 -1
@@ -68,7 +68,7 @@ static void an5206_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ); + kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size = load_image(kernel_filename,
hw/arm_boot.c +2 -1
@@ -205,7 +205,8 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info) kernel_size =load_elf (info->kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (info->kernel_filename, &entry, &is_linux); + kernel_size =load_uboot (info->kernel_filename, &entry,NULL , + &is_linux); }if (kernel_size <0 ) { kernel_size = load_image(info->kernel_filename,
hw/dummy_m68k.c +1 -1
@@ -44,7 +44,7 @@ static void dummy_m68k_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ); + kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size = load_image(kernel_filename,
hw/mcf5208.c +1 -1
@@ -290,7 +290,7 @@ static void mcf5208evb_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ); + kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size =load_image (kernel_filename, phys_ram_base);
loader.c +5 -1
@@ -456,7 +456,8 @@ static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, }/* Load a U-Boot image. */ -int load_uboot (const char *filename,target_ulong *ep,int *is_linux) +int load_uboot (const char *filename,target_ulong *ep,target_ulong *loadaddr, +int *is_linux) {int fd;int size;
@@ -533,6 +534,9 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux)cpu_physical_memory_write_rom (hdr->ih_load, data, hdr->ih_size); +if (loadaddr) + *loadaddr = hdr->ih_load; + ret = hdr->ih_size; out:
sysemu.h +2 -1
@@ -165,7 +165,8 @@ int load_image_targphys(const char *filename, target_phys_addr_t, int max_sz);int load_elf (const char *filename,int64_t address_offset,uint64_t *pentry,uint64_t *lowaddr,uint64_t *highaddr);int load_aout (const char *filename,target_phys_addr_t addr,int max_sz); -int load_uboot (const char *filename,target_ulong *ep,int *is_linux); +int load_uboot (const char *filename,target_ulong *ep,target_ulong *loadaddr, +int *is_linux);int fread_targphys (target_phys_addr_t dst_addr,size_t nbytes,FILE *f);int fread_targphys_ok (target_phys_addr_t dst_addr,size_t nbytes,FILE *f);
Signature · 1 line
1.5.6.5
Re: [Qemu-devel] [PATCH 3/5] uImage: return base load address
Quoted 3 lines · Hollis Blanchard
Return the base address at which the image was loaded so that callers may keep track of currently occupied guest memory.
Why is it that you need this information if no other callers have needed it before?
I'm not suggesting this patch isn't correct, it's just not obvious from the description why this is now needed.
Regards,
Anthony Liguori
Quoted 102 lines
Signed-off-by Hollis Blanchard <[email protected]>--- hw/an5206.c | 2 +- hw/arm_boot.c | 3 ++- hw/dummy_m68k.c | 2 +- hw/mcf5208.c | 2 +- loader.c | 6 +++++- sysemu.h | 3 ++- 6 files changed, 12 insertions(+), 6 deletions(-)hw/an5206.c +1
-1@@ -68,7 +68,7 @@ static void an5206_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ); + kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size = load_image(kernel_filename,hw/arm_boot.c +2
-1@@ -205,7 +205,8 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info) kernel_size =load_elf (info->kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (info->kernel_filename, &entry, &is_linux); + kernel_size =load_uboot (info->kernel_filename, &entry,NULL , + &is_linux); }if (kernel_size <0 ) { kernel_size = load_image(info->kernel_filename,hw/dummy_m68k.c +1
-1@@ -44,7 +44,7 @@ static void dummy_m68k_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ); + kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size = load_image(kernel_filename,hw/mcf5208.c +1
-1@@ -290,7 +290,7 @@ static void mcf5208evb_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ); + kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size =load_image (kernel_filename, phys_ram_base);loader.c +5
-1@@ -456,7 +456,8 @@ static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, }/* Load a U-Boot image. */ -int load_uboot (const char *filename,target_ulong *ep,int *is_linux) +int load_uboot (const char *filename,target_ulong *ep,target_ulong *loadaddr, +int *is_linux) {int fd;int size;@@ -533,6 +534,9 @@ int load_uboot(const char *filename, target_ulong *ep, int *is_linux)cpu_physical_memory_write_rom (hdr->ih_load, data, hdr->ih_size); +if (loadaddr) + *loadaddr = hdr->ih_load; + ret = hdr->ih_size; out:sysemu.h +2
-1@@ -165,7 +165,8 @@ int load_image_targphys(const char *filename, target_phys_addr_t, int max_sz);int load_elf (const char *filename,int64_t address_offset,uint64_t *pentry,uint64_t *lowaddr,uint64_t *highaddr);int load_aout (const char *filename,target_phys_addr_t addr,int max_sz); -int load_uboot (const char *filename,target_ulong *ep,int *is_linux); +int load_uboot (const char *filename,target_ulong *ep,target_ulong *loadaddr, +int *is_linux);int fread_targphys (target_phys_addr_t dst_addr,size_t nbytes,FILE *f);int fread_targphys_ok (target_phys_addr_t dst_addr,size_t nbytes,FILE *f);
Re: [Qemu-devel] [PATCH 3/5] uImage: return base load address
Quoted 8 lines · Anthony Liguori
Quoted 1 line · Hollis Blanchard
Return the base address at which the image was loaded so that
callers may keep
Quoted 2 lines
track of currently occupied guest memory.
Why is it that you need this information if no other callers have needed it before?
As far as I can see, other users are making assumptions about maximum kernel size and simply hardcoding some addresses.
For example arm_load_kernel() seems to always load the initrd at 8MB, regardless of where the ELF or uImage kernel was loaded. (In the case of a "flat" kernel, that's loaded at 0x1000, and probably isn't bigger than 8MB...)
Signature · 2 lines
Hollis Blanchard IBM Linux Technology Center
PATCH 4/5 uImage: rename load_uboot() to load_uimage()
In reply to Hollis Blanchardu-boot is a firmware. uImage is an executable file format.
--- hw/an5206.c | 2 +- hw/arm_boot.c | 4 ++-- hw/dummy_m68k.c | 2 +- hw/mcf5208.c | 2 +- loader.c | 4 ++-- patches | 1 + sysemu.h | 4 ++-- 7 files changed, 10 insertions(+), 9 deletions(-) create mode 120000 patches
hw/an5206.c +1 -1
@@ -68,7 +68,7 @@ static void an5206_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); + kernel_size =load_uimage (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size = load_image(kernel_filename,
hw/arm_boot.c +2 -2
@@ -205,8 +205,8 @@ void arm_load_kernel(CPUState *env, struct arm_boot_info *info) kernel_size =load_elf (info->kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (info->kernel_filename, &entry,NULL , - &is_linux); + kernel_size =load_uimage (info->kernel_filename, &entry,NULL , + &is_linux); }if (kernel_size <0 ) { kernel_size = load_image(info->kernel_filename,
hw/dummy_m68k.c +1 -1
@@ -44,7 +44,7 @@ static void dummy_m68k_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); + kernel_size =load_uimage (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size = load_image(kernel_filename,
hw/mcf5208.c +1 -1
@@ -290,7 +290,7 @@ static void mcf5208evb_init(ram_addr_t ram_size, int vga_ram_size, kernel_size =load_elf (kernel_filename,0 , &elf_entry,NULL ,NULL ); entry = elf_entry;if (kernel_size <0 ) { - kernel_size =load_uboot (kernel_filename, &entry,NULL ,NULL ); + kernel_size =load_uimage (kernel_filename, &entry,NULL ,NULL ); }if (kernel_size <0 ) { kernel_size =load_image (kernel_filename, phys_ram_base);
loader.c +2 -2
@@ -456,8 +456,8 @@ static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, }/* Load a U-Boot image. */ -int load_uboot (const char *filename,target_ulong *ep,target_ulong *loadaddr, -int *is_linux) +int load_uimage (const char *filename,target_ulong *ep,target_ulong *loadaddr, +int *is_linux) {int fd;int size;
patches new file +1 -0
@@ -0,0 +1 @@ +.git/patches/master
sysemu.h +2 -2
@@ -165,8 +165,8 @@ int load_image_targphys(const char *filename, target_phys_addr_t, int max_sz);int load_elf (const char *filename,int64_t address_offset,uint64_t *pentry,uint64_t *lowaddr,uint64_t *highaddr);int load_aout (const char *filename,target_phys_addr_t addr,int max_sz); -int load_uboot (const char *filename,target_ulong *ep,target_ulong *loadaddr, -int *is_linux); +int load_uimage (const char *filename,target_ulong *ep,target_ulong *loadaddr, +int *is_linux);int fread_targphys (target_phys_addr_t dst_addr,size_t nbytes,FILE *f);int fread_targphys_ok (target_phys_addr_t dst_addr,size_t nbytes,FILE *f);
Signature · 1 line
1.5.6.5
PATCH 5/5 uImage: only try to load 'kernel' images
In reply to Hollis BlanchardLoading other image types (e.g. IH_TYPE_MULTI, IH_TYPE_FLATDT) is not implemented.
IH_TYPE_STANDALONE images could be loaded, but would unexpectedly fail if they tried to use any uboot services.
--- loader.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
loader.c +4 -4
@@ -479,9 +479,9 @@ int load_uimage(const char *filename, target_ulong *ep, target_ulong *loadaddr,if (hdr->ih_magic !=IH_MAGIC ) goto out; -/* TODO: Implement Multi-File images. */ -if (hdr->ih_type ==IH_TYPE_MULTI ) { -fprintf (stderr,"Unable to load multi-file u-boot images\n" ); +/* TODO: Implement other image types. */ +if (hdr->ih_type !=IH_TYPE_KERNEL ) { +fprintf (stderr,"Can only load u-boot image type \"kernel\"\n" ); goto out; }
@@ -498,7 +498,7 @@ int load_uimage(const char *filename, target_ulong *ep, target_ulong *loadaddr,/* TODO: Check CPU type. */ if (is_linux) { -if (hdr->ih_type ==IH_TYPE_KERNEL && hdr->ih_os ==IH_OS_LINUX ) +if (hdr->ih_os ==IH_OS_LINUX ) *is_linux =1 ;else *is_linux =0 ;
Signature · 1 line
1.5.6.5
Quoted 4 lines · Hollis Blanchard
These are fixes and enhancements for loading uImages, a kernel format defined by the u-boot bootloader. These are needed in order to merge PowerPC 440 KVM support.
Quoted 8 lines
There are a few existing users of the uImage loader in qemu, but while I cannot test these patches, they shouldn't cause any impact (other than adding gzip loading capability).
The first two patches were modified based on comments from François Revol and Anthony Liguori.
-Hollis
Best Regards,
J.
Anthony Liguori
AppliedQuoted 11 lines · Hollis Blanchard
These are fixes and enhancements for loading uImages, a kernel format defined by the u-boot bootloader. These are needed in order to merge PowerPC 440 KVM support.
There are a few existing users of the uImage loader in qemu, but while I cannot test these patches, they shouldn't cause any impact (other than adding gzip loading capability).
The first two patches were modified based on comments from François Revol and Anthony Liguori.
Applied all. Thanks.
Regards,
Anthony Liguori
Quoted 6 lines
-Hollis