From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60038) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XH3bm-0006h3-AF for qemu-devel@nongnu.org; Tue, 12 Aug 2014 00:22:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XH3bg-00019P-9q for qemu-devel@nongnu.org; Tue, 12 Aug 2014 00:22:50 -0400 From: Max Filippov Date: Tue, 12 Aug 2014 08:22:20 +0400 Message-Id: <1407817342-1373-2-git-send-email-jcmvbkbc@gmail.com> In-Reply-To: <1407817342-1373-1-git-send-email-jcmvbkbc@gmail.com> References: <1407817342-1373-1-git-send-email-jcmvbkbc@gmail.com> Subject: [Qemu-devel] [PATCH 1/3] hw/core/loader: implement load_uboot_image_header List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Max Filippov , Waldemar Brodkorb , qemu-stable@nongnu.org Extract uImage header loader and allow using it from the rest of the code. Cc: qemu-stable@nongnu.org Signed-off-by: Max Filippov --- hw/core/loader.c | 37 +++++++++++++++++++++++++++++-------- include/hw/loader.h | 2 ++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/hw/core/loader.c b/hw/core/loader.c index 2bf6b8f..5bf7f9b 100644 --- a/hw/core/loader.c +++ b/hw/core/loader.c @@ -454,12 +454,25 @@ static ssize_t gunzip(void *dst, size_t dstlen, uint8_t *src, return dstbytes; } +static int load_uboot_image_header_fd(int fd, uboot_image_header_t *hdr) +{ + int size = read(fd, hdr, sizeof(uboot_image_header_t)); + + if (size < 0) { + return -1; + } + bswap_uboot_header(hdr); + if (hdr->ih_magic != IH_MAGIC) { + return -1; + } + return 0; +} + /* Load a U-Boot image. */ 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; @@ -471,14 +484,9 @@ static int load_uboot_image(const char *filename, hwaddr *ep, hwaddr *loadaddr, if (fd < 0) return -1; - size = read(fd, hdr, sizeof(uboot_image_header_t)); - if (size < 0) - goto out; - - bswap_uboot_header(hdr); - - if (hdr->ih_magic != IH_MAGIC) + if (load_uboot_image_header_fd(fd, hdr) < 0) { goto out; + } if (hdr->ih_type != image_type) { fprintf(stderr, "Wrong image type %d, expected %d\n", hdr->ih_type, @@ -565,6 +573,19 @@ out: return ret; } +int load_uboot_image_header(const char *filename, uboot_image_header_t *hdr) +{ + int rc; + int fd = open(filename, O_RDONLY | O_BINARY); + + if (fd < 0) { + return -1; + } + rc = load_uboot_image_header_fd(fd, hdr); + close(fd); + return rc; +} + int load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr, int *is_linux) { diff --git a/include/hw/loader.h b/include/hw/loader.h index 796cbf9..a8cd0cf 100644 --- a/include/hw/loader.h +++ b/include/hw/loader.h @@ -1,6 +1,7 @@ #ifndef LOADER_H #define LOADER_H #include "qapi/qmp/qdict.h" +#include "hw/core/uboot_image.h" #include "hw/nvram/fw_cfg.h" /* loader.c */ @@ -27,6 +28,7 @@ int load_elf(const char *filename, uint64_t (*translate_fn)(void *, uint64_t), int clear_lsb); int load_aout(const char *filename, hwaddr addr, int max_sz, int bswap_needed, hwaddr target_page_size); +int load_uboot_image_header(const char *filename, uboot_image_header_t *hdr); int load_uimage(const char *filename, hwaddr *ep, hwaddr *loadaddr, int *is_linux); -- 1.8.1.4