2 +++ b/arch/x86/boot/compressed/LzmaDecode.c
6 + LZMA Decoder (optimized for Speed version)
8 + LZMA SDK 4.17 Copyright (c) 1999-2005 Igor Pavlov (2005-04-05)
9 + http://www.7-zip.org/
11 + LZMA SDK is licensed under two licenses:
12 + 1) GNU Lesser General Public License (GNU LGPL)
13 + 2) Common Public License (CPL)
14 + It means that you can select one of these two licenses and
15 + follow rules of that license.
18 + Igor Pavlov, as the author of this Code, expressly permits you to
19 + statically or dynamically link your Code (or bind by name) to the
20 + interfaces of this file without subjecting your linked Code to the
21 + terms of the CPL or GNU LGPL. Any modifications or additions
22 + to this file, however, are subject to the LGPL or CPL terms.
25 +#include "LzmaDecode.h"
28 +#define Byte unsigned char
31 +#define kNumTopBits 24
32 +#define kTopValue ((UInt32)1 << kNumTopBits)
34 +#define kNumBitModelTotalBits 11
35 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
36 +#define kNumMoveBits 5
38 +#define RC_READ_BYTE (*Buffer++)
40 +#define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
41 + { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
45 +#define RC_TEST { if (Buffer == BufferLim) \
46 + { UInt32 size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
47 + BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
49 +#define RC_INIT Buffer = BufferLim = 0; RC_INIT2
53 +#define RC_TEST { if (Buffer == BufferLim) return LZMA_RESULT_DATA_ERROR; }
55 +#define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
59 +#define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
61 +#define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
62 +#define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
63 +#define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
65 +#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
66 + { UpdateBit0(p); mi <<= 1; A0; } else \
67 + { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
69 +#define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
71 +#define RangeDecoderBitTreeDecode(probs, numLevels, res) \
72 + { int i = numLevels; res = 1; \
73 + do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
74 + res -= (1 << numLevels); }
77 +#define kNumPosBitsMax 4
78 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
80 +#define kLenNumLowBits 3
81 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
82 +#define kLenNumMidBits 3
83 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
84 +#define kLenNumHighBits 8
85 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
88 +#define LenChoice2 (LenChoice + 1)
89 +#define LenLow (LenChoice2 + 1)
90 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
91 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
92 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols)
95 +#define kNumStates 12
96 +#define kNumLitStates 7
98 +#define kStartPosModelIndex 4
99 +#define kEndPosModelIndex 14
100 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
102 +#define kNumPosSlotBits 6
103 +#define kNumLenToPosStates 4
105 +#define kNumAlignBits 4
106 +#define kAlignTableSize (1 << kNumAlignBits)
108 +#define kMatchMinLen 2
111 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
112 +#define IsRepG0 (IsRep + kNumStates)
113 +#define IsRepG1 (IsRepG0 + kNumStates)
114 +#define IsRepG2 (IsRepG1 + kNumStates)
115 +#define IsRep0Long (IsRepG2 + kNumStates)
116 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
117 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
118 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
119 +#define LenCoder (Align + kAlignTableSize)
120 +#define RepLenCoder (LenCoder + kNumLenProbs)
121 +#define Literal (RepLenCoder + kNumLenProbs)
123 +#if Literal != LZMA_BASE_SIZE
127 +#ifdef _LZMA_OUT_READ
129 +typedef struct _LzmaVarState
136 + ILzmaInCallback *InCallback;
139 + UInt32 DictionarySize;
140 + UInt32 DictionaryPos;
148 + Byte TempDictionary[4];
151 +int LzmaDecoderInit(
152 + unsigned char *buffer, UInt32 bufferSize,
153 + int lc, int lp, int pb,
154 + unsigned char *dictionary, UInt32 dictionarySize,
156 + ILzmaInCallback *InCallback
158 + unsigned char *inStream, UInt32 inSize
166 + LzmaVarState *vs = (LzmaVarState *)buffer;
167 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
168 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
170 + if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
171 + return LZMA_RESULT_NOT_ENOUGH_MEM;
172 + vs->Dictionary = dictionary;
173 + vs->DictionarySize = dictionarySize;
174 + vs->DictionaryPos = 0;
176 + vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
182 + dictionary[dictionarySize - 1] = 0;
183 + for (i = 0; i < numProbs; i++)
184 + p[i] = kBitModelTotal >> 1;
189 + RC_INIT(inStream, inSize);
191 + vs->Buffer = Buffer;
192 + vs->BufferLim = BufferLim;
196 + vs->InCallback = InCallback;
199 + return LZMA_RESULT_OK;
202 +int LzmaDecode(unsigned char *buffer,
203 + unsigned char *outStream, UInt32 outSize,
204 + UInt32 *outSizeProcessed)
206 + LzmaVarState *vs = (LzmaVarState *)buffer;
207 + Byte *Buffer = vs->Buffer;
208 + Byte *BufferLim = vs->BufferLim;
209 + UInt32 Range = vs->Range;
210 + UInt32 Code = vs->Code;
212 + ILzmaInCallback *InCallback = vs->InCallback;
214 + CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
215 + int state = vs->State;
217 + UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
219 + UInt32 posStateMask = (1 << (vs->pb)) - 1;
220 + UInt32 literalPosMask = (1 << (vs->lp)) - 1;
222 + int len = vs->RemainLen;
223 + UInt32 globalPos = vs->GlobalPos;
225 + Byte *dictionary = vs->Dictionary;
226 + UInt32 dictionarySize = vs->DictionarySize;
227 + UInt32 dictionaryPos = vs->DictionaryPos;
229 + Byte tempDictionary[4];
230 + if (dictionarySize == 0)
232 + dictionary = tempDictionary;
233 + dictionarySize = 1;
234 + tempDictionary[0] = vs->TempDictionary[0];
239 + *outSizeProcessed = 0;
240 + return LZMA_RESULT_OK;
243 + while(len != 0 && nowPos < outSize)
245 + UInt32 pos = dictionaryPos - rep0;
246 + if (pos >= dictionarySize)
247 + pos += dictionarySize;
248 + outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
249 + if (++dictionaryPos == dictionarySize)
253 + if (dictionaryPos == 0)
254 + previousByte = dictionary[dictionarySize - 1];
256 + previousByte = dictionary[dictionaryPos - 1];
260 + Byte *buffer, UInt32 bufferSize,
261 + int lc, int lp, int pb,
263 + ILzmaInCallback *InCallback,
265 + unsigned char *inStream, UInt32 inSize,
267 + unsigned char *outStream, UInt32 outSize,
268 + UInt32 *outSizeProcessed)
270 + UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
271 + CProb *p = (CProb *)buffer;
275 + Byte previousByte = 0;
276 + UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
278 + UInt32 posStateMask = (1 << pb) - 1;
279 + UInt32 literalPosMask = (1 << lp) - 1;
287 + if (bufferSize < numProbs * sizeof(CProb))
288 + return LZMA_RESULT_NOT_ENOUGH_MEM;
289 + for (i = 0; i < numProbs; i++)
290 + p[i] = kBitModelTotal >> 1;
296 + RC_INIT(inStream, inSize);
300 + *outSizeProcessed = 0;
301 + while(nowPos < outSize)
305 + int posState = (int)(
307 + #ifdef _LZMA_OUT_READ
313 + prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
318 + prob = p + Literal + (LZMA_LIT_SIZE *
321 + #ifdef _LZMA_OUT_READ
325 + & literalPosMask) << lc) + (previousByte >> (8 - lc))));
327 + if (state >= kNumLitStates)
330 + #ifdef _LZMA_OUT_READ
331 + UInt32 pos = dictionaryPos - rep0;
332 + if (pos >= dictionarySize)
333 + pos += dictionarySize;
334 + matchByte = dictionary[pos];
336 + matchByte = outStream[nowPos - rep0];
343 + bit = (matchByte & 0x100);
344 + probLit = prob + 0x100 + bit + symbol;
345 + RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
347 + while (symbol < 0x100);
349 + while (symbol < 0x100)
351 + CProb *probLit = prob + symbol;
352 + RC_GET_BIT(probLit, symbol)
354 + previousByte = (Byte)symbol;
356 + outStream[nowPos++] = previousByte;
357 + #ifdef _LZMA_OUT_READ
358 + dictionary[dictionaryPos] = previousByte;
359 + if (++dictionaryPos == dictionarySize)
362 + if (state < 4) state = 0;
363 + else if (state < 10) state -= 3;
369 + prob = p + IsRep + state;
376 + state = state < kNumLitStates ? 0 : 3;
377 + prob = p + LenCoder;
382 + prob = p + IsRepG0 + state;
386 + prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
389 + #ifdef _LZMA_OUT_READ
394 + #ifdef _LZMA_OUT_READ
398 + return LZMA_RESULT_DATA_ERROR;
399 + state = state < kNumLitStates ? 9 : 11;
400 + #ifdef _LZMA_OUT_READ
401 + pos = dictionaryPos - rep0;
402 + if (pos >= dictionarySize)
403 + pos += dictionarySize;
404 + previousByte = dictionary[pos];
405 + dictionary[dictionaryPos] = previousByte;
406 + if (++dictionaryPos == dictionarySize)
409 + previousByte = outStream[nowPos - rep0];
411 + outStream[nowPos++] = previousByte;
423 + prob = p + IsRepG1 + state;
432 + prob = p + IsRepG2 + state;
449 + state = state < kNumLitStates ? 8 : 11;
450 + prob = p + RepLenCoder;
453 + int numBits, offset;
454 + CProb *probLen = prob + LenChoice;
457 + UpdateBit0(probLen);
458 + probLen = prob + LenLow + (posState << kLenNumLowBits);
460 + numBits = kLenNumLowBits;
464 + UpdateBit1(probLen);
465 + probLen = prob + LenChoice2;
468 + UpdateBit0(probLen);
469 + probLen = prob + LenMid + (posState << kLenNumMidBits);
470 + offset = kLenNumLowSymbols;
471 + numBits = kLenNumMidBits;
475 + UpdateBit1(probLen);
476 + probLen = prob + LenHigh;
477 + offset = kLenNumLowSymbols + kLenNumMidSymbols;
478 + numBits = kLenNumHighBits;
481 + RangeDecoderBitTreeDecode(probLen, numBits, len);
488 + state += kNumLitStates;
489 + prob = p + PosSlot +
490 + ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
492 + RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
493 + if (posSlot >= kStartPosModelIndex)
495 + int numDirectBits = ((posSlot >> 1) - 1);
496 + rep0 = (2 | ((UInt32)posSlot & 1));
497 + if (posSlot < kEndPosModelIndex)
499 + rep0 <<= numDirectBits;
500 + prob = p + SpecPos + rep0 - posSlot - 1;
504 + numDirectBits -= kNumAlignBits;
516 + while (--numDirectBits != 0);
518 + rep0 <<= kNumAlignBits;
519 + numDirectBits = kNumAlignBits;
526 + CProb *prob3 = prob + mi;
527 + RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
530 + while(--numDirectBits != 0);
535 + if (++rep0 == (UInt32)(0))
537 + /* it's for stream version */
543 + len += kMatchMinLen;
545 + #ifdef _LZMA_OUT_READ
546 + + globalPos || rep0 > dictionarySize
549 + return LZMA_RESULT_DATA_ERROR;
552 + #ifdef _LZMA_OUT_READ
553 + UInt32 pos = dictionaryPos - rep0;
554 + if (pos >= dictionarySize)
555 + pos += dictionarySize;
556 + previousByte = dictionary[pos];
557 + dictionary[dictionaryPos] = previousByte;
558 + if (++dictionaryPos == dictionarySize)
561 + previousByte = outStream[nowPos - rep0];
564 + outStream[nowPos++] = previousByte;
566 + while(len != 0 && nowPos < outSize);
571 + #ifdef _LZMA_OUT_READ
572 + vs->Buffer = Buffer;
573 + vs->BufferLim = BufferLim;
576 + vs->DictionaryPos = dictionaryPos;
577 + vs->GlobalPos = globalPos + nowPos;
578 + vs->Reps[0] = rep0;
579 + vs->Reps[1] = rep1;
580 + vs->Reps[2] = rep2;
581 + vs->Reps[3] = rep3;
583 + vs->RemainLen = len;
584 + vs->TempDictionary[0] = tempDictionary[0];
587 + *outSizeProcessed = nowPos;
588 + return LZMA_RESULT_OK;
591 +++ b/arch/x86/boot/compressed/LzmaDecode.h
595 + LZMA Decoder interface
597 + LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-03-18)
598 + http://www.7-zip.org/
600 + LZMA SDK is licensed under two licenses:
601 + 1) GNU Lesser General Public License (GNU LGPL)
602 + 2) Common Public License (CPL)
603 + It means that you can select one of these two licenses and
604 + follow rules of that license.
607 + Igor Pavlov, as the author of this code, expressly permits you to
608 + statically or dynamically link your code (or bind by name) to the
609 + interfaces of this file without subjecting your linked code to the
610 + terms of the CPL or GNU LGPL. Any modifications or additions
611 + to this file, however, are subject to the LGPL or CPL terms.
614 +#ifndef __LZMADECODE_H
615 +#define __LZMADECODE_H
617 +/* #define _LZMA_IN_CB */
618 +/* Use callback for input data */
620 +/* #define _LZMA_OUT_READ */
621 +/* Use read function for output data */
623 +/* #define _LZMA_PROB32 */
624 +/* It can increase speed on some 32-bit CPUs,
625 + but memory usage will be doubled in that case */
627 +/* #define _LZMA_LOC_OPT */
628 +/* Enable local speed optimizations inside code */
631 +#ifdef _LZMA_UINT32_IS_ULONG
632 +#define UInt32 unsigned long
634 +#define UInt32 unsigned int
639 +#define CProb UInt32
641 +#define CProb unsigned short
644 +#define LZMA_RESULT_OK 0
645 +#define LZMA_RESULT_DATA_ERROR 1
646 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
649 +typedef struct _ILzmaInCallback
651 + int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
655 +#define LZMA_BASE_SIZE 1846
656 +#define LZMA_LIT_SIZE 768
659 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
660 +bufferSize += 100 in case of _LZMA_OUT_READ
661 +by default CProb is unsigned short,
662 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
665 +#ifdef _LZMA_OUT_READ
666 +int LzmaDecoderInit(
667 + unsigned char *buffer, UInt32 bufferSize,
668 + int lc, int lp, int pb,
669 + unsigned char *dictionary, UInt32 dictionarySize,
671 + ILzmaInCallback *inCallback
673 + unsigned char *inStream, UInt32 inSize
679 + unsigned char *buffer,
680 + #ifndef _LZMA_OUT_READ
682 + int lc, int lp, int pb,
684 + ILzmaInCallback *inCallback,
686 + unsigned char *inStream, UInt32 inSize,
689 + unsigned char *outStream, UInt32 outSize,
690 + UInt32 *outSizeProcessed);
694 +++ b/arch/x86/boot/compressed/lzma_misc.c
699 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
700 + * puts by Nick Holloway 1993, better puts by Martin Mares 1995
701 + * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
703 + * Decompress LZMA compressed vmlinuz
704 + * Version 0.9 Copyright (c) Ming-Ching Tiew mctiew@yahoo.com
705 + * Program adapted from misc.c for 2.6 kernel
706 + * Forward ported to latest 2.6 version of misc.c by
707 + * Felix Fietkau <nbd@openwrt.org>
710 +#undef CONFIG_PARAVIRT
711 +#include <linux/linkage.h>
712 +#include <linux/vmalloc.h>
713 +#include <linux/screen_info.h>
715 +#include <asm/page.h>
716 +#include <asm/boot.h>
719 + * This code is compiled with -fPIC and it is relocated dynamically
720 + * at run time, but no relocation processing is performed.
721 + * This means that it is not safe to place pointers in static structures.
725 + * Getting to provable safe in place decompression is hard.
726 + * Worst case behaviours need to be analized.
727 + * Background information:
729 + * The file layout is:
736 + * compressed data blocks[N]
737 + * crc[4] orig_len[4]
739 + * resulting in 18 bytes of non compressed data overhead.
741 + * Files divided into blocks
742 + * 1 bit (last block flag)
743 + * 2 bits (block type)
745 + * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
746 + * The smallest block type encoding is always used.
749 + * 32 bits length in bytes.
752 + * magic fixed tree.
756 + * dynamic tree encoding.
760 + * The buffer for decompression in place is the length of the
761 + * uncompressed data, plus a small amount extra to keep the algorithm safe.
762 + * The compressed data is placed at the end of the buffer. The output
763 + * pointer is placed at the start of the buffer and the input pointer
764 + * is placed where the compressed data starts. Problems will occur
765 + * when the output pointer overruns the input pointer.
767 + * The output pointer can only overrun the input pointer if the input
768 + * pointer is moving faster than the output pointer. A condition only
769 + * triggered by data whose compressed form is larger than the uncompressed
772 + * The worst case at the block level is a growth of the compressed data
773 + * of 5 bytes per 32767 bytes.
775 + * The worst case internal to a compressed block is very hard to figure.
776 + * The worst case can at least be boundined by having one bit that represents
777 + * 32764 bytes and then all of the rest of the bytes representing the very
780 + * All of which is enough to compute an amount of extra data that is required
781 + * to be safe. To avoid problems at the block level allocating 5 extra bytes
782 + * per 32767 bytes of data is sufficient. To avoind problems internal to a block
783 + * adding an extra 32767 bytes (the worst case uncompressed block size) is
784 + * sufficient, to ensure that in the worst case the decompressed data for
785 + * block will stop the byte before the compressed data for a block begins.
786 + * To avoid problems with the compressed data's meta information an extra 18
787 + * bytes are needed. Leading to the formula:
789 + * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
791 + * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
792 + * Adding 32768 instead of 32767 just makes for round numbers.
793 + * Adding the decompressor_size is necessary as it musht live after all
794 + * of the data as well. Last I measured the decompressor is about 14K.
795 + * 10K of actuall data and 4K of bss.
800 + * gzip declarations
803 +#define OF(args) args
804 +#define STATIC static
808 +typedef unsigned char uch;
809 +typedef unsigned short ush;
810 +typedef unsigned long ulg;
812 +#define WSIZE 0x80000000 /* Window size must be at least 32k,
813 + * and a power of two
814 + * We don't actually have a window just
815 + * a huge output buffer so I report
816 + * a 2G windows size, as that should
817 + * always be larger than our output buffer.
820 +static uch *inbuf; /* input buffer */
821 +static uch *window; /* Sliding window buffer, (and final output buffer) */
823 +static unsigned insize; /* valid bytes in inbuf */
824 +static unsigned inptr; /* index of next byte to be processed in inbuf */
825 +static unsigned long workspace;
827 +#define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
829 +/* Diagnostic functions */
831 +# define Assert(cond,msg) {if(!(cond)) error(msg);}
832 +# define Trace(x) fprintf x
833 +# define Tracev(x) {if (verbose) fprintf x ;}
834 +# define Tracevv(x) {if (verbose>1) fprintf x ;}
835 +# define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
836 +# define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
838 +# define Assert(cond,msg)
842 +# define Tracec(c,x)
843 +# define Tracecv(c,x)
846 +static int fill_inbuf(void);
849 + * This is set up by the setup-routine at boot-time
851 +static unsigned char *real_mode; /* Pointer to real-mode data */
852 +extern unsigned char input_data[];
853 +extern int input_len;
855 +static void error(char *x);
856 +static void *memcpy(void *dest, const void *src, unsigned n);
858 +#ifdef CONFIG_X86_NUMAQ
862 +static void* memcpy(void* dest, const void* src, unsigned n)
865 + char *d = (char *)dest, *s = (char *)src;
867 + for (i=0;i<n;i++) d[i] = s[i];
871 +/* ===========================================================================
872 + * Fill the input buffer. This is called only when the buffer is empty
873 + * and at least one byte is really needed.
875 +static int fill_inbuf(void)
877 + error("ran out of input data");
882 +// When using LZMA in callback, the compressed length is not needed.
883 +// Otherwise you need a special version of lzma compression program
884 +// which will pad the compressed length in the header.
886 +#include "LzmaDecode.h"
887 +#include "LzmaDecode.c"
889 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize);
893 + * Do the lzma decompression
894 + * When using LZMA in callback, the end of input stream is automatically determined
896 +static int lzma_unzip(void)
899 + unsigned int i; /* temp value */
900 + unsigned int lc; /* literal context bits */
901 + unsigned int lp; /* literal pos state bits */
902 + unsigned int pb; /* pos state bits */
903 + unsigned int uncompressedSize = 0;
906 + ILzmaInCallback callback;
907 + callback.Read = read_byte;
911 + lc = i % 9, i = i / 9;
912 + lp = i % 5, pb = i / 5;
914 + /* skip dictionary size */
915 + for (i = 0; i < 4; i++)
917 + // get uncompressedSize
918 + p= (char*)&uncompressedSize;
919 + for (i = 0; i < 4; i++)
922 + //get compressedSize
923 + for (i = 0; i < 4; i++)
926 + // point it beyond uncompresedSize
927 + //workspace = window + uncompressedSize;
929 + /* decompress kernel */
930 + if (LzmaDecode((unsigned char*)workspace, ~0, lc, lp, pb, &callback,
931 + (unsigned char*)window, uncompressedSize, &i) == LZMA_RESULT_OK)
939 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
941 + static unsigned int i = 0;
942 + static unsigned char val;
946 + return LZMA_RESULT_OK;
950 +static void error(char *x)
952 + while(1); /* Halt */
955 +asmlinkage void decompress_kernel(void *rmode, unsigned long end,
956 + uch *input_data, unsigned long input_len, uch *output)
961 + inbuf = input_data; /* Input buffer */
962 + insize = input_len;
965 + if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
966 + error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
967 + if ((workspace = end) > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff))
968 + error("Destination address too large");
969 +#ifndef CONFIG_RELOCATABLE
970 + if ((u32)output != LOAD_PHYSICAL_ADDR)
971 + error("Wrong destination address");
977 --- a/scripts/Makefile.lib
978 +++ b/scripts/Makefile.lib
980 quiet_cmd_gzip = GZIP $@
981 cmd_gzip = gzip -f -9 < $< > $@
986 +quiet_cmd_lzma = LZMA $@
987 +cmd_lzma = bash -e scripts/lzma_kern $< $@ -lc7 -lp0 -pb0
989 +# cmd_lzma = lzmacomp $< 700 > $@
991 +++ b/scripts/lzma_kern
993 +get-size() { echo "$5" ;}
994 +printf -v len '%.8x' "$(get-size $(ls -l "$1"))"
996 +echo -ne "\x$(echo $len | cut -c 7,8)\x$(echo $len | cut -c 5,6)\x$(echo $len | cut -c 3,4)\x$(echo $len | cut -c 1,2)" >> "$2"
997 --- a/arch/x86/boot/compressed/Makefile
998 +++ b/arch/x86/boot/compressed/Makefile
1000 # create a compressed vmlinux image from the original vmlinux
1003 -targets := vmlinux vmlinux.bin vmlinux.bin.gz head_$(BITS).o misc.o piggy.o
1004 +targets := vmlinux vmlinux.bin vmlinux.bin.lzma head_$(BITS).o lzma_misc.o piggy.o
1006 KBUILD_CFLAGS := -m$(BITS) -D__KERNEL__ $(LINUX_INCLUDE) -O2
1007 KBUILD_CFLAGS += -fno-strict-aliasing -fPIC
1009 LDFLAGS := -m elf_$(UTS_MACHINE)
1010 LDFLAGS_vmlinux := -T
1012 -$(obj)/vmlinux: $(src)/vmlinux_$(BITS).lds $(obj)/head_$(BITS).o $(obj)/misc.o $(obj)/piggy.o FORCE
1013 +$(obj)/vmlinux: $(src)/vmlinux_$(BITS).lds $(obj)/head_$(BITS).o $(obj)/lzma_misc.o $(obj)/piggy.o FORCE
1014 $(call if_changed,ld)
1018 $(call if_changed,relocbin)
1020 ifdef CONFIG_RELOCATABLE
1021 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
1022 - $(call if_changed,gzip)
1023 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
1024 + $(call if_changed,lzma)
1026 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
1027 - $(call if_changed,gzip)
1028 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
1029 + $(call if_changed,lzma)
1031 LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
1037 -$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.gz FORCE
1038 +$(obj)/piggy.o: $(obj)/vmlinux.scr $(obj)/vmlinux.bin.lzma FORCE
1039 $(call if_changed,ld)