From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Jan Beulich" Subject: [PATCH] x86: allow LZO compressed bzImage to be used as Dom0 kernel Date: Tue, 15 Jun 2010 12:54:05 +0100 Message-ID: <4C1785FD0200007800006791@vpn.id2.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__Part97BAC1CD.0__=" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel@lists.xensource.com List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__Part97BAC1CD.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline ... since recently Linux added this as another kernel compression method, and we already have LZO compression in the tree (from tmem), so that only glue logic is needed. Signed-off-by: Jan Beulich --- 2010-06-15.orig/xen/common/Makefile 2010-05-20 09:59:27.000000000 = +0200 +++ 2010-06-15/xen/common/Makefile 2010-06-15 09:50:53.000000000 = +0200 @@ -39,7 +39,7 @@ obj-y +=3D radix-tree.o obj-y +=3D rbtree.o obj-y +=3D lzo.o =20 -obj-$(CONFIG_X86) +=3D decompress.o bunzip2.o unlzma.o +obj-$(CONFIG_X86) +=3D decompress.o bunzip2.o unlzma.o unlzo.o =20 obj-$(perfc) +=3D perfc.o obj-$(crash_debug) +=3D gdbstub.o --- 2010-06-15.orig/xen/common/decompress.c 2009-11-09 09:45:09.0000000= 00 +0100 +++ 2010-06-15/xen/common/decompress.c 2010-06-15 09:50:53.000000000 = +0200 @@ -23,5 +23,8 @@ int __init decompress(void *inbuf, unsig if ( len >=3D 2 && !memcmp(inbuf, "\135\000", 2) ) return unlzma(inbuf, len, NULL, NULL, outbuf, NULL, error); =20 + if ( len >=3D 5 && !memcmp(inbuf, "\x89LZO", 5) ) + return unlzo(inbuf, len, NULL, NULL, outbuf, NULL, error); + return 1; } --- /dev/null 1970-01-01 00:00:00.000000000 +0000 +++ 2010-06-15/xen/common/unlzo.c 2010-06-15 09:50:53.000000000 = +0200 @@ -0,0 +1,229 @@ +/* + * LZO decompressor for the Linux kernel. Code borrowed from the lzo + * implementation by Markus Franz Xaver Johannes Oberhumer. + * + * Linux kernel adaptation: + * Copyright (C) 2009 + * Albin Tonnerre, Free Electrons + * + * Original code: + * Copyright (C) 1996-2005 Markus Franz Xaver Johannes Oberhumer + * All Rights Reserved. + * + * lzop and the LZO library are free software; you can redistribute them + * and/or modify them 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; see the file COPYING. + * If not, write to the Free Software Foundation, Inc., + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * Markus F.X.J. Oberhumer + * + * http://www.oberhumer.com/opensource/lzop/=20 + */ + +#include "decompress.h" +#include +#include + +#if 1 /* ndef CONFIG_??? */ +static inline u16 INIT get_unaligned_be16(void *p) +{ + return be16_to_cpup(p); +} + +static inline u32 INIT get_unaligned_be32(void *p) +{ + return be32_to_cpup(p); +} +#else +#include + +static inline u16 INIT get_unaligned_be16(void *p) +{ + return be16_to_cpu(__get_unaligned(p, 2)); +} + +static inline u32 INIT get_unaligned_be32(void *p) +{ + return be32_to_cpu(__get_unaligned(p, 4)); +} +#endif + +static const unsigned char lzop_magic[] =3D { + 0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a }; + +#define LZO_BLOCK_SIZE (256*1024l) +#define HEADER_HAS_FILTER 0x00000800L + +static int INIT parse_header(u8 *input, u8 *skip) +{ + int l; + u8 *parse =3D input; + u8 level =3D 0; + u16 version; + + /* read magic: 9 first bits */ + for (l =3D 0; l < 9; l++) { + if (*parse++ !=3D lzop_magic[l]) + return 0; + } + /* get version (2bytes), skip library version (2), + * 'need to be extracted' version (2) and + * method (1) */ + version =3D get_unaligned_be16(parse); + parse +=3D 7; + if (version >=3D 0x0940) + level =3D *parse++; + if (get_unaligned_be32(parse) & HEADER_HAS_FILTER) + parse +=3D 8; /* flags + filter info */ + else + parse +=3D 4; /* flags */ + + /* skip mode and mtime_low */ + parse +=3D 8; + if (version >=3D 0x0940) + parse +=3D 4; /* skip mtime_high */ + + l =3D *parse++; + /* don't care about the file name, and skip checksum */ + parse +=3D l + 4; + + *skip =3D parse - input; + return 1; +} + +STATIC int INIT unlzo(u8 *input, unsigned int in_len, + int (*fill) (void *, unsigned int), + int (*flush) (void *, unsigned int), + u8 *output, unsigned int *posp, + void (*error_fn) (const char *x)) +{ + u8 skip =3D 0, r =3D 0; + u32 src_len, dst_len; + size_t tmp; + u8 *in_buf, *in_buf_save, *out_buf; + int ret =3D -1; + + set_error_fn(error_fn); + + if (output) { + out_buf =3D output; + } else if (!flush) { + error("NULL output pointer and no flush function provided")= ; + goto exit; + } else { + out_buf =3D malloc(LZO_BLOCK_SIZE); + if (!out_buf) { + error("Could not allocate output buffer"); + goto exit; + } + } + + if (input && fill) { + error("Both input pointer and fill function provided, = don't know what to do"); + goto exit_1; + } else if (input) { + in_buf =3D input; + } else if (!fill || !posp) { + error("NULL input pointer and missing position pointer or = fill function"); + goto exit_1; + } else { + in_buf =3D malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE)); + if (!in_buf) { + error("Could not allocate input buffer"); + goto exit_1; + } + } + in_buf_save =3D in_buf; + + if (posp) + *posp =3D 0; + + if (fill) + fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE)); + + if (!parse_header(input, &skip)) { + error("invalid header"); + goto exit_2; + } + in_buf +=3D skip; + + if (posp) + *posp =3D skip; + + for (;;) { + /* read uncompressed block size */ + dst_len =3D get_unaligned_be32(in_buf); + in_buf +=3D 4; + + /* exit if last block */ + if (dst_len =3D=3D 0) { + if (posp) + *posp +=3D 4; + break; + } + + if (dst_len > LZO_BLOCK_SIZE) { + error("dest len longer than block size"); + goto exit_2; + } + + /* read compressed block size, and skip block checksum = info */ + src_len =3D get_unaligned_be32(in_buf); + in_buf +=3D 8; + + if (src_len <=3D 0 || src_len > dst_len) { + error("file corrupted"); + goto exit_2; + } + + /* decompress */ + tmp =3D dst_len; + + /* When the input data is not compressed at all, + * lzo1x_decompress_safe will fail, so call memcpy() + * instead */ + if (unlikely(dst_len =3D=3D src_len)) + memcpy(out_buf, in_buf, src_len); + else { + r =3D lzo1x_decompress_safe(in_buf, src_len, + out_buf, &tmp); + + if (r !=3D LZO_E_OK || dst_len !=3D tmp) { + error("Compressed data violation"); + goto exit_2; + } + } + + if (flush) + flush(out_buf, dst_len); + if (output) + out_buf +=3D dst_len; + if (posp) + *posp +=3D src_len + 12; + if (fill) { + in_buf =3D in_buf_save; + fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));= + } else + in_buf +=3D src_len; + } + + ret =3D 0; +exit_2: + if (!input) + free(in_buf); +exit_1: + if (!output) + free(out_buf); +exit: + return ret; +} --- 2010-06-15.orig/xen/include/xen/decompress.h 2009-11-09 = 09:45:09.000000000 +0100 +++ 2010-06-15/xen/include/xen/decompress.h 2010-06-15 09:50:53.0000000= 00 +0200 @@ -31,7 +31,7 @@ typedef int decompress_fn(unsigned char=20 * dependent). */ =20 -decompress_fn bunzip2, unlzma; +decompress_fn bunzip2, unlzma, unlzo; =20 int decompress(void *inbuf, unsigned int len, void *outbuf); =20 --=__Part97BAC1CD.0__= Content-Type: text/plain; name="x86-lzo-dom0.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="x86-lzo-dom0.patch" ... since recently Linux added this as another kernel compression=0Amethod,= and we already have LZO compression in the tree (from tmem),=0Aso that = only glue logic is needed.=0A=0ASigned-off-by: Jan Beulich =0A=0A--- 2010-06-15.orig/xen/common/Makefile 2010-05-20 = 09:59:27.000000000 +0200=0A+++ 2010-06-15/xen/common/Makefile 2010-06-15 = 09:50:53.000000000 +0200=0A@@ -39,7 +39,7 @@ obj-y +=3D radix-tree.o=0A = obj-y +=3D rbtree.o=0A obj-y +=3D lzo.o=0A =0A-obj-$(CONFIG_X86) +=3D = decompress.o bunzip2.o unlzma.o=0A+obj-$(CONFIG_X86) +=3D decompress.o = bunzip2.o unlzma.o unlzo.o=0A =0A obj-$(perfc) +=3D perfc.o=0A = obj-$(crash_debug) +=3D gdbstub.o=0A--- 2010-06-15.orig/xen/common/decompre= ss.c 2009-11-09 09:45:09.000000000 +0100=0A+++ 2010-06-15/xen/common/dec= ompress.c 2010-06-15 09:50:53.000000000 +0200=0A@@ -23,5 +23,8 @@ = int __init decompress(void *inbuf, unsig=0A if ( len >=3D 2 && = !memcmp(inbuf, "\135\000", 2) )=0A return unlzma(inbuf, len, NULL, = NULL, outbuf, NULL, error);=0A =0A+ if ( len >=3D 5 && !memcmp(inbuf, = "\x89LZO", 5) )=0A+ return unlzo(inbuf, len, NULL, NULL, outbuf, = NULL, error);=0A+=0A return 1;=0A }=0A--- /dev/null 1970-01-01 = 00:00:00.000000000 +0000=0A+++ 2010-06-15/xen/common/unlzo.c 2010-06-15 = 09:50:53.000000000 +0200=0A@@ -0,0 +1,229 @@=0A+/*=0A+ * LZO decompressor = for the Linux kernel. Code borrowed from the lzo=0A+ * implementation by = Markus Franz Xaver Johannes Oberhumer.=0A+ *=0A+ * Linux kernel adaptation:= =0A+ * Copyright (C) 2009=0A+ * Albin Tonnerre, Free Electrons =0A+ *=0A+ * Original code:=0A+ * Copyright (C) = 1996-2005 Markus Franz Xaver Johannes Oberhumer=0A+ * All Rights = Reserved.=0A+ *=0A+ * lzop and the LZO library are free software; you can = redistribute them=0A+ * and/or modify them under the terms of the GNU = General Public License as=0A+ * published by the Free Software Foundation; = either version 2 of=0A+ * the License, or (at your option) any later = version.=0A+ *=0A+ * This program is distributed in the hope that it will = be useful,=0A+ * but WITHOUT ANY WARRANTY; without even the implied = warranty of=0A+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. = See the=0A+ * GNU General Public License for more details.=0A+ *=0A+ * You = should have received a copy of the GNU General Public License=0A+ * along = with this program; see the file COPYING.=0A+ * If not, write to the Free = Software Foundation, Inc.,=0A+ * 59 Temple Place - Suite 330, Boston, MA = 02111-1307, USA.=0A+ *=0A+ * Markus F.X.J. Oberhumer=0A+ * =0A+ * http://www.oberhumer.com/opensource/lzop/=0A+ */=0A+=0A+#inclu= de "decompress.h"=0A+#include =0A+#include =0A+= =0A+#if 1 /* ndef CONFIG_??? */=0A+static inline u16 INIT get_unaligned_be1= 6(void *p)=0A+{=0A+ return be16_to_cpup(p);=0A+}=0A+=0A+static inline = u32 INIT get_unaligned_be32(void *p)=0A+{=0A+ return be32_to_cpup(p);=0A+= }=0A+#else=0A+#include =0A+=0A+static inline u16 INIT = get_unaligned_be16(void *p)=0A+{=0A+ return be16_to_cpu(__get_unaligned(= p, 2));=0A+}=0A+=0A+static inline u32 INIT get_unaligned_be32(void = *p)=0A+{=0A+ return be32_to_cpu(__get_unaligned(p, 4));=0A+}=0A+#endif= =0A+=0A+static const unsigned char lzop_magic[] =3D {=0A+ 0x89, = 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a };=0A+=0A+#define LZO_BLOCK_= SIZE (256*1024l)=0A+#define HEADER_HAS_FILTER 0x00000800L=0A+= =0A+static int INIT parse_header(u8 *input, u8 *skip)=0A+{=0A+ int l;=0A+ = u8 *parse =3D input;=0A+ u8 level =3D 0;=0A+ u16 version;=0A+=0A= + /* read magic: 9 first bits */=0A+ for (l =3D 0; l < 9; l++) = {=0A+ if (*parse++ !=3D lzop_magic[l])=0A+ = return 0;=0A+ }=0A+ /* get version (2bytes), skip library version = (2),=0A+ * 'need to be extracted' version (2) and=0A+ * method = (1) */=0A+ version =3D get_unaligned_be16(parse);=0A+ parse +=3D = 7;=0A+ if (version >=3D 0x0940)=0A+ level =3D *parse++;=0A+ if = (get_unaligned_be32(parse) & HEADER_HAS_FILTER)=0A+ parse +=3D = 8; /* flags + filter info */=0A+ else=0A+ parse +=3D = 4; /* flags */=0A+=0A+ /* skip mode and mtime_low */=0A+ parse +=3D = 8;=0A+ if (version >=3D 0x0940)=0A+ parse +=3D 4; /* skip = mtime_high */=0A+=0A+ l =3D *parse++;=0A+ /* don't care about the = file name, and skip checksum */=0A+ parse +=3D l + 4;=0A+=0A+ = *skip =3D parse - input;=0A+ return 1;=0A+}=0A+=0A+STATIC int INIT = unlzo(u8 *input, unsigned int in_len,=0A+ int (*fill) = (void *, unsigned int),=0A+ int (*flush) (void *, = unsigned int),=0A+ u8 *output, unsigned int *posp,=0A+ = void (*error_fn) (const char *x))=0A+{=0A+ u8 skip = =3D 0, r =3D 0;=0A+ u32 src_len, dst_len;=0A+ size_t tmp;=0A+ u8 = *in_buf, *in_buf_save, *out_buf;=0A+ int ret =3D -1;=0A+=0A+ set_error_f= n(error_fn);=0A+=0A+ if (output) {=0A+ out_buf =3D = output;=0A+ } else if (!flush) {=0A+ error("NULL output = pointer and no flush function provided");=0A+ goto exit;=0A+ } = else {=0A+ out_buf =3D malloc(LZO_BLOCK_SIZE);=0A+ if = (!out_buf) {=0A+ error("Could not allocate output = buffer");=0A+ goto exit;=0A+ }=0A+ }=0A+=0A+ = if (input && fill) {=0A+ error("Both input pointer and fill = function provided, don't know what to do");=0A+ goto exit_1;=0A+ = } else if (input) {=0A+ in_buf =3D input;=0A+ } else if (!fill = || !posp) {=0A+ error("NULL input pointer and missing position = pointer or fill function");=0A+ goto exit_1;=0A+ } else = {=0A+ in_buf =3D malloc(lzo1x_worst_compress(LZO_BLOCK_SIZE));=0A= + if (!in_buf) {=0A+ error("Could not = allocate input buffer");=0A+ goto exit_1;=0A+ = }=0A+ }=0A+ in_buf_save =3D in_buf;=0A+=0A+ if (posp)=0A+ = *posp =3D 0;=0A+=0A+ if (fill)=0A+ fill(in_buf, lzo1x_worst_co= mpress(LZO_BLOCK_SIZE));=0A+=0A+ if (!parse_header(input, &skip)) = {=0A+ error("invalid header");=0A+ goto exit_2;=0A+ = }=0A+ in_buf +=3D skip;=0A+=0A+ if (posp)=0A+ *posp =3D = skip;=0A+=0A+ for (;;) {=0A+ /* read uncompressed block size = */=0A+ dst_len =3D get_unaligned_be32(in_buf);=0A+ = in_buf +=3D 4;=0A+=0A+ /* exit if last block */=0A+ if = (dst_len =3D=3D 0) {=0A+ if (posp)=0A+ = *posp +=3D 4;=0A+ break;=0A+ = }=0A+=0A+ if (dst_len > LZO_BLOCK_SIZE) {=0A+ = error("dest len longer than block size");=0A+ goto = exit_2;=0A+ }=0A+=0A+ /* read compressed block = size, and skip block checksum info */=0A+ src_len =3D = get_unaligned_be32(in_buf);=0A+ in_buf +=3D 8;=0A+=0A+ if = (src_len <=3D 0 || src_len > dst_len) {=0A+ error("file= corrupted");=0A+ goto exit_2;=0A+ = }=0A+=0A+ /* decompress */=0A+ tmp =3D dst_len;=0A= +=0A+ /* When the input data is not compressed at all,=0A+ = * lzo1x_decompress_safe will fail, so call memcpy()=0A+ * = instead */=0A+ if (unlikely(dst_len =3D=3D src_len))=0A+ = memcpy(out_buf, in_buf, src_len);=0A+ else {=0A+ = r =3D lzo1x_decompress_safe(in_buf, src_len,=0A+ = out_buf, &tmp);=0A+=0A+ if = (r !=3D LZO_E_OK || dst_len !=3D tmp) {=0A+ = error("Compressed data violation");=0A+ goto = exit_2;=0A+ }=0A+ }=0A+=0A+ if = (flush)=0A+ flush(out_buf, dst_len);=0A+ if = (output)=0A+ out_buf +=3D dst_len;=0A+ if = (posp)=0A+ *posp +=3D src_len + 12;=0A+ if = (fill) {=0A+ in_buf =3D in_buf_save;=0A+ = fill(in_buf, lzo1x_worst_compress(LZO_BLOCK_SIZE));=0A+ } else=0A+ = in_buf +=3D src_len;=0A+ }=0A+=0A+ ret =3D = 0;=0A+exit_2:=0A+ if (!input)=0A+ free(in_buf);=0A+exit_1:=0A= + if (!output)=0A+ free(out_buf);=0A+exit:=0A+ = return ret;=0A+}=0A--- 2010-06-15.orig/xen/include/xen/decompress.h = 2009-11-09 09:45:09.000000000 +0100=0A+++ 2010-06-15/xen/include/xen/decomp= ress.h 2010-06-15 09:50:53.000000000 +0200=0A@@ -31,7 +31,7 @@ typedef = int decompress_fn(unsigned char =0A * dependent).=0A */=0A =0A-decompress= _fn bunzip2, unlzma;=0A+decompress_fn bunzip2, unlzma, unlzo;=0A =0A int = decompress(void *inbuf, unsigned int len, void *outbuf);=0A =0A --=__Part97BAC1CD.0__= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --=__Part97BAC1CD.0__=--