From patchwork Tue Aug 19 18:09:41 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 35650 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-oa0-f72.google.com (mail-oa0-f72.google.com [209.85.219.72]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 244842034C for ; Tue, 19 Aug 2014 18:12:30 +0000 (UTC) Received: by mail-oa0-f72.google.com with SMTP id m1sf45222296oag.11 for ; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:date:message-id:in-reply-to :references:mime-version:subject:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:errors-to:sender :x-original-sender:x-original-authentication-results:mailing-list :content-type:content-transfer-encoding; bh=uSmqc9CZxexK7U/IL/yJQzh0KrswDcyWdUWqiOV76+8=; b=PmLOSqdbj1XhSRC/04tnGludvtJ/Tw6Dm9H9I5yaYQnwLVOJbJd+L8V92fulhdrADC AsTA/tbsowV3Hr+pHC7VWnNH50vjk48djSwultPqBrslCxKr3QagROtkGYatrld0Itai jofJrKyYyEyb6+bzC7Z6lpMqA69ojINTfT/Zbh3raVwMHKvDw4pqhjNNYTqLyfgswNKN qA1dKDlR5nKhYCEZOL0iI9t9vNm9/5eAQkMz2uVnct6inpSXEpJB+TB2L8KRVeH547zZ POMJbpy48bIP70zIJk/cWqasB8yzQdqwkxeLe5HRm3fEAoasLnIM79v9hvsD6rjO+oSS e6cQ== X-Gm-Message-State: ALoCoQlJ8thh4hubzhgW26Amt2C8WsfUMrBPX+uARqDVtTvXXz5pTNyJjY1DdXuN+l4TzzeacrrY X-Received: by 10.182.68.17 with SMTP id r17mr21346318obt.26.1408471949781; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.101.210 with SMTP id u76ls2774994qge.62.gmail; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) X-Received: by 10.52.230.164 with SMTP id sz4mr1596795vdc.68.1408471949682; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) Received: from mail-vc0-f181.google.com (mail-vc0-f181.google.com [209.85.220.181]) by mx.google.com with ESMTPS id rt3si9280504vdb.70.2014.08.19.11.12.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Tue, 19 Aug 2014 11:12:29 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.181 as permitted sender) client-ip=209.85.220.181; Received: by mail-vc0-f181.google.com with SMTP id lf12so7734926vcb.26 for ; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) X-Received: by 10.220.15.8 with SMTP id i8mr2038302vca.45.1408471949605; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.37.5 with SMTP id tc5csp260158vcb; Tue, 19 Aug 2014 11:12:29 -0700 (PDT) X-Received: by 10.140.29.138 with SMTP id b10mr66031047qgb.15.1408471948715; Tue, 19 Aug 2014 11:12:28 -0700 (PDT) Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id b8si30100145qad.39.2014.08.19.11.12.28 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 19 Aug 2014 11:12:28 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Received: from localhost ([::1]:52401 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XJntU-0002Zs-4A for patch@linaro.org; Tue, 19 Aug 2014 14:12:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50467) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XJns7-00017e-19 for qemu-devel@nongnu.org; Tue, 19 Aug 2014 14:11:21 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XJnrz-00042q-ES for qemu-devel@nongnu.org; Tue, 19 Aug 2014 14:11:02 -0400 Received: from mnementh.archaic.org.uk ([81.2.115.146]:42051) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XJnrz-0003wv-8D for qemu-devel@nongnu.org; Tue, 19 Aug 2014 14:10:55 -0400 Received: from pm215 by mnementh.archaic.org.uk with local (Exim 4.80) (envelope-from ) id 1XJnqr-0000iW-5N for qemu-devel@nongnu.org; Tue, 19 Aug 2014 19:09:45 +0100 From: Peter Maydell To: qemu-devel@nongnu.org Date: Tue, 19 Aug 2014 19:09:41 +0100 Message-Id: <1408471784-2652-17-git-send-email-peter.maydell@linaro.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1408471784-2652-1-git-send-email-peter.maydell@linaro.org> References: <1408471784-2652-1-git-send-email-peter.maydell@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 81.2.115.146 Subject: [Qemu-devel] [PULL 16/19] loader: Add load_image_gzipped function. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: peter.maydell@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.181 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 From: "Richard W.M. Jones" 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 Reviewed-by: Alex Bennée Reviewed-by: Peter Crosthwaite Reviewed-by: Alex Bennée Message-id: 1407831259-2115-2-git-send-email-rjones@redhat.com [PMM: removed stray space before ')'] Signed-off-by: Peter Maydell --- hw/core/loader.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ include/hw/loader.h | 1 + 2 files changed, 49 insertions(+) diff --git a/hw/core/loader.c b/hw/core/loader.c index 1a53f0f..193f0f8 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] != 0x1f || + compressed_data[1] != 0x8b) { + 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