Support ramdisks with U-Boot header
v2 cover Support ramdisks with U-Boot header
Updated with Peter's comments. The detailed changelog can be found in the following emails with the patches.
Sören
Soren Brinkmann (2):
hw/loader: Support ramdisk with u-boot header hw/arm: Use 'load_ramdisk()' for loading ramdisk
hw/arm/boot.c | 8 ++--- hw/core/loader.c | 90 ++++++++++++++++++++++++++++++++++++++--------------- include/hw/loader.h | 13 ++++++++ 3 files changed, 82 insertions(+), 29 deletions(-)
Signature · 1 line
1.8.3.1
PATCH v2 1/2 hw/loader: Support ramdisk with u-boot header
Introduce 'load_ramdisk()' which can load "normal" ramdisks and ramdisks with a u-boot header.
To enable this and leverage synergies 'load_uimage()' is refactored to accomodate this additional use case.
---
v2:
- document the new 'load_ramdisk()' function
- print the image type byte in the error messages for unsupported u-boot images
- load_uimage() and load_ramdisk() both use load_uboot_image() to load images. Let the callers pass the expected image type to load_uboot_image to allow error checking for wrong image types. E.g. passing a ramdisk image to the load_uimage routine.
hw/core/loader.c | 90 ++++++++++++++++++++++++++++++++++++++--------------- include/hw/loader.h | 13 ++++++++ 2 files changed, 78 insertions(+), 25 deletions(-)
hw/core/loader.c +65 -25
@@ -434,15 +434,17 @@ static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, }/* Load a U-Boot image. */ -int load_uimage (const char *filename,hwaddr *ep, -hwaddr *loadaddr,int *is_linux) +static int load_uboot_image (const char *filename,hwaddr *ep,hwaddr *loadaddr, +int *is_linux,uint8_t image_type) {int fd;int size; +hwaddr address;uboot_image_header_t h;uboot_image_header_t *hdr = &h;uint8_t *data =NULL ;int ret =-1 ; +int do_uncompress =0 ; fd =open (filename,O_RDONLY |O_BINARY );if (fd <0 )
@@ -457,32 +459,55 @@ int load_uimage(const char *filename, hwaddr *ep,if (hdr->ih_magic !=IH_MAGIC ) goto out; -/* TODO: Implement other image types. */ -if (hdr->ih_type !=IH_TYPE_KERNEL ) { -fprintf (stderr,"Can only load u-boot image type \"kernel\"\n" ); +if (hdr->ih_type != image_type) { +fprintf (stderr,"Wrong image type %d, expected %d\n" , hdr->ih_type, + image_type); goto out; } -switch (hdr->ih_comp) { -case IH_COMP_NONE : -case IH_COMP_GZIP : +/* TODO: Implement other image types. */ +switch (hdr->ih_type) { +case IH_TYPE_KERNEL : + address = hdr->ih_load; +if (loadaddr) { + *loadaddr = hdr->ih_load; + } + +switch (hdr->ih_comp) { +case IH_COMP_NONE : +break ; +case IH_COMP_GZIP : + do_uncompress =1 ; +break ; +default : +fprintf (stderr, +"Unable to load u-boot images with compression type %d\n" , + hdr->ih_comp); + goto out; + } + +if (ep) { + *ep = hdr->ih_ep; + } + +/* TODO: Check CPU type. */ +if (is_linux) { +if (hdr->ih_os ==IH_OS_LINUX ) { + *is_linux =1 ; + }else { + *is_linux =0 ; + } + } + +break ; +case IH_TYPE_RAMDISK : + address = *loadaddr;break ;default : -fprintf (stderr, -"Unable to load u-boot images with compression type %d\n" , - hdr->ih_comp); +fprintf (stderr,"Unsupported u-boot image type %d\n" , hdr->ih_type); goto out; } -/* TODO: Check CPU type. */ -if (is_linux) { -if (hdr->ih_os ==IH_OS_LINUX ) - *is_linux =1 ; -else - *is_linux =0 ; - } - - *ep = hdr->ih_ep; data =g_malloc (hdr->ih_size);if (read (fd, data, hdr->ih_size) != hdr->ih_size) {
@@ -490,7 +515,7 @@ int load_uimage(const char *filename, hwaddr *ep, goto out; } -if (hdr->ih_comp ==IH_COMP_GZIP ) { +if (do_uncompress) {uint8_t *compressed_data;size_t max_bytes;ssize_t bytes;
@@ -508,10 +533,7 @@ 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 (loadaddr) - *loadaddr = hdr->ih_load; +rom_add_blob_fixed (filename, data, hdr->ih_size, address); ret = hdr->ih_size;
@@ -522,6 +544,24 @@ out:return ret; } +int load_uimage (const char *filename,hwaddr *ep,hwaddr *loadaddr, +int *is_linux) +{ +return load_uboot_image (filename, ep, loadaddr, is_linux,IH_TYPE_KERNEL ); +} + +/* Load a ramdisk. */ +int load_ramdisk (const char *filename,hwaddr addr,uint64_t max_sz) +{ +int size =load_uboot_image (filename,NULL , &addr,NULL ,IH_TYPE_RAMDISK ); + +if (size <0 ) { + size =load_image_targphys (filename, addr, max_sz); + } + +return size; +} +/* * Functions for reboot-persistent memory regions. * - used for vga bios and option roms.
include/hw/loader.h +13 -0
@@ -16,6 +16,19 @@ int load_aout(const char *filename, hwaddr addr, int max_sz,int load_uimage (const char *filename,hwaddr *ep,hwaddr *loadaddr,int *is_linux); +/** +* load_ramdisk: +* @filename: Path to the ramdisk image +* @addr: Memory address to load the ramdisk to +* @max_sz: Maximum allowed ramdisk size (for non-u-boot ramdisks) +* +* Load a ramdisk image - optional with U-Boot header - to the specified memory +* address. +* +* Returns the size of the loaded image on success, -1 otherwise. +*/ +int load_ramdisk (const char *filename,hwaddr addr,uint64_t max_sz); +ssize_t read_targphys (const char *name,int fd,hwaddr dst_addr,size_t nbytes); void pstrcpy_targphys(const char *name,
Signature · 1 line
1.8.3.1
PATCH v2 2/2 hw/arm: Use 'load_ramdisk()' for loading ramdisk
In reply to Soren BrinkmannThe load_ramdisk function takes over loading traditional ramdisks images and does also load ramdisks with u-boot header.
--- hw/arm/boot.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/arm/boot.c +4 -4
@@ -436,10 +436,10 @@ void arm_load_kernel(ARMCPU *cpu, struct arm_boot_info *info) info->entry = entry;if (is_linux) {if (info->initrd_filename) { - initrd_size =load_image_targphys (info->initrd_filename, - info->initrd_start, - info->ram_size - - info->initrd_start); + initrd_size =load_ramdisk (info->initrd_filename, + info->initrd_start, + info->ram_size - + info->initrd_start);if (initrd_size <0 ) {fprintf (stderr,"qemu: could not load initrd '%s'\n" , info->initrd_filename);
Signature · 1 line
1.8.3.1