refresh kernel patches
[oweals/openwrt.git] / target / linux / rdc / patches / 600-x86_lzma.patch
1 Index: linux-2.6.24.7/arch/x86/boot/compressed/LzmaDecode.c
2 ===================================================================
3 --- /dev/null
4 +++ linux-2.6.24.7/arch/x86/boot/compressed/LzmaDecode.c
5 @@ -0,0 +1,586 @@
6 +/*
7 +  LzmaDecode.c
8 +  LZMA Decoder (optimized for Speed version)
9 +  
10 +  LZMA SDK 4.17 Copyright (c) 1999-2005 Igor Pavlov (2005-04-05)
11 +  http://www.7-zip.org/
12 +
13 +  LZMA SDK is licensed under two licenses:
14 +  1) GNU Lesser General Public License (GNU LGPL)
15 +  2) Common Public License (CPL)
16 +  It means that you can select one of these two licenses and 
17 +  follow rules of that license.
18 +
19 +  SPECIAL EXCEPTION:
20 +  Igor Pavlov, as the author of this Code, expressly permits you to 
21 +  statically or dynamically link your Code (or bind by name) to the 
22 +  interfaces of this file without subjecting your linked Code to the 
23 +  terms of the CPL or GNU LGPL. Any modifications or additions 
24 +  to this file, however, are subject to the LGPL or CPL terms.
25 +*/
26 +
27 +#include "LzmaDecode.h"
28 +
29 +#ifndef Byte
30 +#define Byte unsigned char
31 +#endif
32 +
33 +#define kNumTopBits 24
34 +#define kTopValue ((UInt32)1 << kNumTopBits)
35 +
36 +#define kNumBitModelTotalBits 11
37 +#define kBitModelTotal (1 << kNumBitModelTotalBits)
38 +#define kNumMoveBits 5
39 +
40 +#define RC_READ_BYTE (*Buffer++)
41 +
42 +#define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
43 +  { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
44 +
45 +#ifdef _LZMA_IN_CB
46 +
47 +#define RC_TEST { if (Buffer == BufferLim) \
48 +  { UInt32 size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
49 +  BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
50 +
51 +#define RC_INIT Buffer = BufferLim = 0; RC_INIT2
52 +
53 +#else
54 +
55 +#define RC_TEST { if (Buffer == BufferLim) return LZMA_RESULT_DATA_ERROR; }
56 +
57 +#define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
58
59 +#endif
60 +
61 +#define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
62 +
63 +#define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
64 +#define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
65 +#define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
66 +
67 +#define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
68 +  { UpdateBit0(p); mi <<= 1; A0; } else \
69 +  { UpdateBit1(p); mi = (mi + mi) + 1; A1; } 
70 +  
71 +#define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)               
72 +
73 +#define RangeDecoderBitTreeDecode(probs, numLevels, res) \
74 +  { int i = numLevels; res = 1; \
75 +  do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
76 +  res -= (1 << numLevels); }
77 +
78 +
79 +#define kNumPosBitsMax 4
80 +#define kNumPosStatesMax (1 << kNumPosBitsMax)
81 +
82 +#define kLenNumLowBits 3
83 +#define kLenNumLowSymbols (1 << kLenNumLowBits)
84 +#define kLenNumMidBits 3
85 +#define kLenNumMidSymbols (1 << kLenNumMidBits)
86 +#define kLenNumHighBits 8
87 +#define kLenNumHighSymbols (1 << kLenNumHighBits)
88 +
89 +#define LenChoice 0
90 +#define LenChoice2 (LenChoice + 1)
91 +#define LenLow (LenChoice2 + 1)
92 +#define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
93 +#define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
94 +#define kNumLenProbs (LenHigh + kLenNumHighSymbols) 
95 +
96 +
97 +#define kNumStates 12
98 +#define kNumLitStates 7
99 +
100 +#define kStartPosModelIndex 4
101 +#define kEndPosModelIndex 14
102 +#define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
103 +
104 +#define kNumPosSlotBits 6
105 +#define kNumLenToPosStates 4
106 +
107 +#define kNumAlignBits 4
108 +#define kAlignTableSize (1 << kNumAlignBits)
109 +
110 +#define kMatchMinLen 2
111 +
112 +#define IsMatch 0
113 +#define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
114 +#define IsRepG0 (IsRep + kNumStates)
115 +#define IsRepG1 (IsRepG0 + kNumStates)
116 +#define IsRepG2 (IsRepG1 + kNumStates)
117 +#define IsRep0Long (IsRepG2 + kNumStates)
118 +#define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
119 +#define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
120 +#define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
121 +#define LenCoder (Align + kAlignTableSize)
122 +#define RepLenCoder (LenCoder + kNumLenProbs)
123 +#define Literal (RepLenCoder + kNumLenProbs)
124 +
125 +#if Literal != LZMA_BASE_SIZE
126 +StopCompilingDueBUG
127 +#endif
128 +
129 +#ifdef _LZMA_OUT_READ
130 +
131 +typedef struct _LzmaVarState
132 +{
133 +  Byte *Buffer;
134 +  Byte *BufferLim;
135 +  UInt32 Range;
136 +  UInt32 Code;
137 +  #ifdef _LZMA_IN_CB
138 +  ILzmaInCallback *InCallback;
139 +  #endif
140 +  Byte *Dictionary;
141 +  UInt32 DictionarySize;
142 +  UInt32 DictionaryPos;
143 +  UInt32 GlobalPos;
144 +  UInt32 Reps[4];
145 +  int lc;
146 +  int lp;
147 +  int pb;
148 +  int State;
149 +  int RemainLen;
150 +  Byte TempDictionary[4];
151 +} LzmaVarState;
152 +
153 +int LzmaDecoderInit(
154 +    unsigned char *buffer, UInt32 bufferSize,
155 +    int lc, int lp, int pb,
156 +    unsigned char *dictionary, UInt32 dictionarySize,
157 +    #ifdef _LZMA_IN_CB
158 +    ILzmaInCallback *InCallback
159 +    #else
160 +    unsigned char *inStream, UInt32 inSize
161 +    #endif
162 +    )
163 +{
164 +  Byte *Buffer;
165 +  Byte *BufferLim;
166 +  UInt32 Range;
167 +  UInt32 Code;
168 +  LzmaVarState *vs = (LzmaVarState *)buffer;
169 +  CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
170 +  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
171 +  UInt32 i;
172 +  if (bufferSize < numProbs * sizeof(CProb) + sizeof(LzmaVarState))
173 +    return LZMA_RESULT_NOT_ENOUGH_MEM;
174 +  vs->Dictionary = dictionary;
175 +  vs->DictionarySize = dictionarySize;
176 +  vs->DictionaryPos = 0;
177 +  vs->GlobalPos = 0;
178 +  vs->Reps[0] = vs->Reps[1] = vs->Reps[2] = vs->Reps[3] = 1;
179 +  vs->lc = lc;
180 +  vs->lp = lp;
181 +  vs->pb = pb;
182 +  vs->State = 0;
183 +  vs->RemainLen = 0;
184 +  dictionary[dictionarySize - 1] = 0;
185 +  for (i = 0; i < numProbs; i++)
186 +    p[i] = kBitModelTotal >> 1; 
187 +
188 +  #ifdef _LZMA_IN_CB
189 +  RC_INIT;
190 +  #else
191 +  RC_INIT(inStream, inSize);
192 +  #endif
193 +  vs->Buffer = Buffer;
194 +  vs->BufferLim = BufferLim;
195 +  vs->Range = Range;
196 +  vs->Code = Code;
197 +  #ifdef _LZMA_IN_CB
198 +  vs->InCallback = InCallback;
199 +  #endif
200 +
201 +  return LZMA_RESULT_OK;
202 +}
203 +
204 +int LzmaDecode(unsigned char *buffer, 
205 +    unsigned char *outStream, UInt32 outSize,
206 +    UInt32 *outSizeProcessed)
207 +{
208 +  LzmaVarState *vs = (LzmaVarState *)buffer;
209 +  Byte *Buffer = vs->Buffer;
210 +  Byte *BufferLim = vs->BufferLim;
211 +  UInt32 Range = vs->Range;
212 +  UInt32 Code = vs->Code;
213 +  #ifdef _LZMA_IN_CB
214 +  ILzmaInCallback *InCallback = vs->InCallback;
215 +  #endif
216 +  CProb *p = (CProb *)(buffer + sizeof(LzmaVarState));
217 +  int state = vs->State;
218 +  Byte previousByte;
219 +  UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
220 +  UInt32 nowPos = 0;
221 +  UInt32 posStateMask = (1 << (vs->pb)) - 1;
222 +  UInt32 literalPosMask = (1 << (vs->lp)) - 1;
223 +  int lc = vs->lc;
224 +  int len = vs->RemainLen;
225 +  UInt32 globalPos = vs->GlobalPos;
226 +
227 +  Byte *dictionary = vs->Dictionary;
228 +  UInt32 dictionarySize = vs->DictionarySize;
229 +  UInt32 dictionaryPos = vs->DictionaryPos;
230 +
231 +  Byte tempDictionary[4];
232 +  if (dictionarySize == 0)
233 +  {
234 +    dictionary = tempDictionary;
235 +    dictionarySize = 1;
236 +    tempDictionary[0] = vs->TempDictionary[0];
237 +  }
238 +
239 +  if (len == -1)
240 +  {
241 +    *outSizeProcessed = 0;
242 +    return LZMA_RESULT_OK;
243 +  }
244 +
245 +  while(len != 0 && nowPos < outSize)
246 +  {
247 +    UInt32 pos = dictionaryPos - rep0;
248 +    if (pos >= dictionarySize)
249 +      pos += dictionarySize;
250 +    outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
251 +    if (++dictionaryPos == dictionarySize)
252 +      dictionaryPos = 0;
253 +    len--;
254 +  }
255 +  if (dictionaryPos == 0)
256 +    previousByte = dictionary[dictionarySize - 1];
257 +  else
258 +    previousByte = dictionary[dictionaryPos - 1];
259 +#else
260 +
261 +int LzmaDecode(
262 +    Byte *buffer, UInt32 bufferSize,
263 +    int lc, int lp, int pb,
264 +    #ifdef _LZMA_IN_CB
265 +    ILzmaInCallback *InCallback,
266 +    #else
267 +    unsigned char *inStream, UInt32 inSize,
268 +    #endif
269 +    unsigned char *outStream, UInt32 outSize,
270 +    UInt32 *outSizeProcessed)
271 +{
272 +  UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + lp));
273 +  CProb *p = (CProb *)buffer;
274 +
275 +  UInt32 i;
276 +  int state = 0;
277 +  Byte previousByte = 0;
278 +  UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
279 +  UInt32 nowPos = 0;
280 +  UInt32 posStateMask = (1 << pb) - 1;
281 +  UInt32 literalPosMask = (1 << lp) - 1;
282 +  int len = 0;
283 +  
284 +  Byte *Buffer;
285 +  Byte *BufferLim;
286 +  UInt32 Range;
287 +  UInt32 Code;
288 +  
289 +  if (bufferSize < numProbs * sizeof(CProb))
290 +    return LZMA_RESULT_NOT_ENOUGH_MEM;
291 +  for (i = 0; i < numProbs; i++)
292 +    p[i] = kBitModelTotal >> 1;
293 +  
294 +
295 +  #ifdef _LZMA_IN_CB
296 +  RC_INIT;
297 +  #else
298 +  RC_INIT(inStream, inSize);
299 +  #endif
300 +#endif
301 +
302 +  *outSizeProcessed = 0;
303 +  while(nowPos < outSize)
304 +  {
305 +    CProb *prob;
306 +    UInt32 bound;
307 +    int posState = (int)(
308 +        (nowPos 
309 +        #ifdef _LZMA_OUT_READ
310 +        + globalPos
311 +        #endif
312 +        )
313 +        & posStateMask);
314 +
315 +    prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
316 +    IfBit0(prob)
317 +    {
318 +      int symbol = 1;
319 +      UpdateBit0(prob)
320 +      prob = p + Literal + (LZMA_LIT_SIZE * 
321 +        (((
322 +        (nowPos 
323 +        #ifdef _LZMA_OUT_READ
324 +        + globalPos
325 +        #endif
326 +        )
327 +        & literalPosMask) << lc) + (previousByte >> (8 - lc))));
328 +
329 +      if (state >= kNumLitStates)
330 +      {
331 +        int matchByte;
332 +        #ifdef _LZMA_OUT_READ
333 +        UInt32 pos = dictionaryPos - rep0;
334 +        if (pos >= dictionarySize)
335 +          pos += dictionarySize;
336 +        matchByte = dictionary[pos];
337 +        #else
338 +        matchByte = outStream[nowPos - rep0];
339 +        #endif
340 +        do
341 +        {
342 +          int bit;
343 +          CProb *probLit;
344 +          matchByte <<= 1;
345 +          bit = (matchByte & 0x100);
346 +          probLit = prob + 0x100 + bit + symbol;
347 +          RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
348 +        }
349 +        while (symbol < 0x100);
350 +      }
351 +      while (symbol < 0x100)
352 +      {
353 +        CProb *probLit = prob + symbol;
354 +        RC_GET_BIT(probLit, symbol)
355 +      }
356 +      previousByte = (Byte)symbol;
357 +
358 +      outStream[nowPos++] = previousByte;
359 +      #ifdef _LZMA_OUT_READ
360 +      dictionary[dictionaryPos] = previousByte;
361 +      if (++dictionaryPos == dictionarySize)
362 +        dictionaryPos = 0;
363 +      #endif
364 +      if (state < 4) state = 0;
365 +      else if (state < 10) state -= 3;
366 +      else state -= 6;
367 +    }
368 +    else             
369 +    {
370 +      UpdateBit1(prob);
371 +      prob = p + IsRep + state;
372 +      IfBit0(prob)
373 +      {
374 +        UpdateBit0(prob);
375 +        rep3 = rep2;
376 +        rep2 = rep1;
377 +        rep1 = rep0;
378 +        state = state < kNumLitStates ? 0 : 3;
379 +        prob = p + LenCoder;
380 +      }
381 +      else
382 +      {
383 +        UpdateBit1(prob);
384 +        prob = p + IsRepG0 + state;
385 +        IfBit0(prob)
386 +        {
387 +          UpdateBit0(prob);
388 +          prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
389 +          IfBit0(prob)
390 +          {
391 +            #ifdef _LZMA_OUT_READ
392 +            UInt32 pos;
393 +            #endif
394 +            UpdateBit0(prob);
395 +            if (nowPos 
396 +                #ifdef _LZMA_OUT_READ
397 +                + globalPos
398 +                #endif
399 +                == 0)
400 +              return LZMA_RESULT_DATA_ERROR;
401 +            state = state < kNumLitStates ? 9 : 11;
402 +            #ifdef _LZMA_OUT_READ
403 +            pos = dictionaryPos - rep0;
404 +            if (pos >= dictionarySize)
405 +              pos += dictionarySize;
406 +            previousByte = dictionary[pos];
407 +            dictionary[dictionaryPos] = previousByte;
408 +            if (++dictionaryPos == dictionarySize)
409 +              dictionaryPos = 0;
410 +            #else
411 +            previousByte = outStream[nowPos - rep0];
412 +            #endif
413 +            outStream[nowPos++] = previousByte;
414 +            continue;
415 +          }
416 +          else
417 +          {
418 +            UpdateBit1(prob);
419 +          }
420 +        }
421 +        else
422 +        {
423 +          UInt32 distance;
424 +          UpdateBit1(prob);
425 +          prob = p + IsRepG1 + state;
426 +          IfBit0(prob)
427 +          {
428 +            UpdateBit0(prob);
429 +            distance = rep1;
430 +          }
431 +          else 
432 +          {
433 +            UpdateBit1(prob);
434 +            prob = p + IsRepG2 + state;
435 +            IfBit0(prob)
436 +            {
437 +              UpdateBit0(prob);
438 +              distance = rep2;
439 +            }
440 +            else
441 +            {
442 +              UpdateBit1(prob);
443 +              distance = rep3;
444 +              rep3 = rep2;
445 +            }
446 +            rep2 = rep1;
447 +          }
448 +          rep1 = rep0;
449 +          rep0 = distance;
450 +        }
451 +        state = state < kNumLitStates ? 8 : 11;
452 +        prob = p + RepLenCoder;
453 +      }
454 +      {
455 +        int numBits, offset;
456 +        CProb *probLen = prob + LenChoice;
457 +        IfBit0(probLen)
458 +        {
459 +          UpdateBit0(probLen);
460 +          probLen = prob + LenLow + (posState << kLenNumLowBits);
461 +          offset = 0;
462 +          numBits = kLenNumLowBits;
463 +        }
464 +        else
465 +        {
466 +          UpdateBit1(probLen);
467 +          probLen = prob + LenChoice2;
468 +          IfBit0(probLen)
469 +          {
470 +            UpdateBit0(probLen);
471 +            probLen = prob + LenMid + (posState << kLenNumMidBits);
472 +            offset = kLenNumLowSymbols;
473 +            numBits = kLenNumMidBits;
474 +          }
475 +          else
476 +          {
477 +            UpdateBit1(probLen);
478 +            probLen = prob + LenHigh;
479 +            offset = kLenNumLowSymbols + kLenNumMidSymbols;
480 +            numBits = kLenNumHighBits;
481 +          }
482 +        }
483 +        RangeDecoderBitTreeDecode(probLen, numBits, len);
484 +        len += offset;
485 +      }
486 +
487 +      if (state < 4)
488 +      {
489 +        int posSlot;
490 +        state += kNumLitStates;
491 +        prob = p + PosSlot +
492 +            ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << 
493 +            kNumPosSlotBits);
494 +        RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
495 +        if (posSlot >= kStartPosModelIndex)
496 +        {
497 +          int numDirectBits = ((posSlot >> 1) - 1);
498 +          rep0 = (2 | ((UInt32)posSlot & 1));
499 +          if (posSlot < kEndPosModelIndex)
500 +          {
501 +            rep0 <<= numDirectBits;
502 +            prob = p + SpecPos + rep0 - posSlot - 1;
503 +          }
504 +          else
505 +          {
506 +            numDirectBits -= kNumAlignBits;
507 +            do
508 +            {
509 +              RC_NORMALIZE
510 +              Range >>= 1;
511 +              rep0 <<= 1;
512 +              if (Code >= Range)
513 +              {
514 +                Code -= Range;
515 +                rep0 |= 1;
516 +              }
517 +            }
518 +            while (--numDirectBits != 0);
519 +            prob = p + Align;
520 +            rep0 <<= kNumAlignBits;
521 +            numDirectBits = kNumAlignBits;
522 +          }
523 +          {
524 +            int i = 1;
525 +            int mi = 1;
526 +            do
527 +            {
528 +              CProb *prob3 = prob + mi;
529 +              RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
530 +              i <<= 1;
531 +            }
532 +            while(--numDirectBits != 0);
533 +          }
534 +        }
535 +        else
536 +          rep0 = posSlot;
537 +        if (++rep0 == (UInt32)(0))
538 +        {
539 +          /* it's for stream version */
540 +          len = -1;
541 +          break;
542 +        }
543 +      }
544 +
545 +      len += kMatchMinLen;
546 +      if (rep0 > nowPos 
547 +        #ifdef _LZMA_OUT_READ
548 +        + globalPos || rep0 > dictionarySize
549 +        #endif
550 +        ) 
551 +        return LZMA_RESULT_DATA_ERROR;
552 +      do
553 +      {
554 +        #ifdef _LZMA_OUT_READ
555 +        UInt32 pos = dictionaryPos - rep0;
556 +        if (pos >= dictionarySize)
557 +          pos += dictionarySize;
558 +        previousByte = dictionary[pos];
559 +        dictionary[dictionaryPos] = previousByte;
560 +        if (++dictionaryPos == dictionarySize)
561 +          dictionaryPos = 0;
562 +        #else
563 +        previousByte = outStream[nowPos - rep0];
564 +        #endif
565 +        len--;
566 +        outStream[nowPos++] = previousByte;
567 +      }
568 +      while(len != 0 && nowPos < outSize);
569 +    }
570 +  }
571 +  RC_NORMALIZE;
572 +
573 +  #ifdef _LZMA_OUT_READ
574 +  vs->Buffer = Buffer;
575 +  vs->BufferLim = BufferLim;
576 +  vs->Range = Range;
577 +  vs->Code = Code;
578 +  vs->DictionaryPos = dictionaryPos;
579 +  vs->GlobalPos = globalPos + nowPos;
580 +  vs->Reps[0] = rep0;
581 +  vs->Reps[1] = rep1;
582 +  vs->Reps[2] = rep2;
583 +  vs->Reps[3] = rep3;
584 +  vs->State = state;
585 +  vs->RemainLen = len;
586 +  vs->TempDictionary[0] = tempDictionary[0];
587 +  #endif
588 +
589 +  *outSizeProcessed = nowPos;
590 +  return LZMA_RESULT_OK;
591 +}
592 Index: linux-2.6.24.7/arch/x86/boot/compressed/LzmaDecode.h
593 ===================================================================
594 --- /dev/null
595 +++ linux-2.6.24.7/arch/x86/boot/compressed/LzmaDecode.h
596 @@ -0,0 +1,100 @@
597 +/* 
598 +  LzmaDecode.h
599 +  LZMA Decoder interface
600 +
601 +  LZMA SDK 4.16 Copyright (c) 1999-2005 Igor Pavlov (2005-03-18)
602 +  http://www.7-zip.org/
603 +
604 +  LZMA SDK is licensed under two licenses:
605 +  1) GNU Lesser General Public License (GNU LGPL)
606 +  2) Common Public License (CPL)
607 +  It means that you can select one of these two licenses and 
608 +  follow rules of that license.
609 +
610 +  SPECIAL EXCEPTION:
611 +  Igor Pavlov, as the author of this code, expressly permits you to 
612 +  statically or dynamically link your code (or bind by name) to the 
613 +  interfaces of this file without subjecting your linked code to the 
614 +  terms of the CPL or GNU LGPL. Any modifications or additions 
615 +  to this file, however, are subject to the LGPL or CPL terms.
616 +*/
617 +
618 +#ifndef __LZMADECODE_H
619 +#define __LZMADECODE_H
620 +
621 +/* #define _LZMA_IN_CB */
622 +/* Use callback for input data */
623 +
624 +/* #define _LZMA_OUT_READ */
625 +/* Use read function for output data */
626 +
627 +/* #define _LZMA_PROB32 */
628 +/* It can increase speed on some 32-bit CPUs, 
629 +   but memory usage will be doubled in that case */
630 +
631 +/* #define _LZMA_LOC_OPT */
632 +/* Enable local speed optimizations inside code */
633 +
634 +#ifndef UInt32
635 +#ifdef _LZMA_UINT32_IS_ULONG
636 +#define UInt32 unsigned long
637 +#else
638 +#define UInt32 unsigned int
639 +#endif
640 +#endif
641 +
642 +#ifdef _LZMA_PROB32
643 +#define CProb UInt32
644 +#else
645 +#define CProb unsigned short
646 +#endif
647 +
648 +#define LZMA_RESULT_OK 0
649 +#define LZMA_RESULT_DATA_ERROR 1
650 +#define LZMA_RESULT_NOT_ENOUGH_MEM 2
651 +
652 +#ifdef _LZMA_IN_CB
653 +typedef struct _ILzmaInCallback
654 +{
655 +  int (*Read)(void *object, unsigned char **buffer, UInt32 *bufferSize);
656 +} ILzmaInCallback;
657 +#endif
658 +
659 +#define LZMA_BASE_SIZE 1846
660 +#define LZMA_LIT_SIZE 768
661 +
662 +/* 
663 +bufferSize = (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << (lc + lp)))* sizeof(CProb)
664 +bufferSize += 100 in case of _LZMA_OUT_READ
665 +by default CProb is unsigned short, 
666 +but if specify _LZMA_PROB_32, CProb will be UInt32(unsigned int)
667 +*/
668 +
669 +#ifdef _LZMA_OUT_READ
670 +int LzmaDecoderInit(
671 +    unsigned char *buffer, UInt32 bufferSize,
672 +    int lc, int lp, int pb,
673 +    unsigned char *dictionary, UInt32 dictionarySize,
674 +  #ifdef _LZMA_IN_CB
675 +    ILzmaInCallback *inCallback
676 +  #else
677 +    unsigned char *inStream, UInt32 inSize
678 +  #endif
679 +);
680 +#endif
681 +
682 +int LzmaDecode(
683 +    unsigned char *buffer, 
684 +  #ifndef _LZMA_OUT_READ
685 +    UInt32 bufferSize,
686 +    int lc, int lp, int pb,
687 +  #ifdef _LZMA_IN_CB
688 +    ILzmaInCallback *inCallback,
689 +  #else
690 +    unsigned char *inStream, UInt32 inSize,
691 +  #endif
692 +  #endif
693 +    unsigned char *outStream, UInt32 outSize,
694 +    UInt32 *outSizeProcessed);
695 +
696 +#endif
697 Index: linux-2.6.24.7/arch/x86/boot/compressed/lzma_misc.c
698 ===================================================================
699 --- /dev/null
700 +++ linux-2.6.24.7/arch/x86/boot/compressed/lzma_misc.c
701 @@ -0,0 +1,281 @@
702 +/*
703 + * lzma_misc.c
704 + * 
705 + * malloc by Hannu Savolainen 1993 and Matthias Urlichs 1994
706 + * puts by Nick Holloway 1993, better puts by Martin Mares 1995
707 + * High loaded stuff by Hans Lermen & Werner Almesberger, Feb. 1996
708 + * 
709 + * Decompress LZMA compressed vmlinuz 
710 + * Version 0.9 Copyright (c) Ming-Ching Tiew mctiew@yahoo.com
711 + * Program adapted from misc.c for 2.6 kernel
712 + * Forward ported to latest 2.6 version of misc.c by
713 + * Felix Fietkau <nbd@openwrt.org>
714 + */
715 +
716 +#undef CONFIG_PARAVIRT
717 +#include <linux/linkage.h>
718 +#include <linux/vmalloc.h>
719 +#include <linux/screen_info.h>
720 +#include <asm/io.h>
721 +#include <asm/page.h>
722 +#include <asm/boot.h>
723 +
724 +/* WARNING!!
725 + * This code is compiled with -fPIC and it is relocated dynamically
726 + * at run time, but no relocation processing is performed.
727 + * This means that it is not safe to place pointers in static structures.
728 + */
729 +
730 +/*
731 + * Getting to provable safe in place decompression is hard.
732 + * Worst case behaviours need to be analized.
733 + * Background information:
734 + *
735 + * The file layout is:
736 + *    magic[2]
737 + *    method[1]
738 + *    flags[1]
739 + *    timestamp[4]
740 + *    extraflags[1]
741 + *    os[1]
742 + *    compressed data blocks[N]
743 + *    crc[4] orig_len[4]
744 + *
745 + * resulting in 18 bytes of non compressed data overhead.
746 + *
747 + * Files divided into blocks
748 + * 1 bit (last block flag)
749 + * 2 bits (block type)
750 + *
751 + * 1 block occurs every 32K -1 bytes or when there 50% compression has been achieved.
752 + * The smallest block type encoding is always used.
753 + *
754 + * stored:
755 + *    32 bits length in bytes.
756 + *
757 + * fixed:
758 + *    magic fixed tree.
759 + *    symbols.
760 + *
761 + * dynamic:
762 + *    dynamic tree encoding.
763 + *    symbols.
764 + *
765 + *
766 + * The buffer for decompression in place is the length of the
767 + * uncompressed data, plus a small amount extra to keep the algorithm safe.
768 + * The compressed data is placed at the end of the buffer.  The output
769 + * pointer is placed at the start of the buffer and the input pointer
770 + * is placed where the compressed data starts.  Problems will occur
771 + * when the output pointer overruns the input pointer.
772 + *
773 + * The output pointer can only overrun the input pointer if the input
774 + * pointer is moving faster than the output pointer.  A condition only
775 + * triggered by data whose compressed form is larger than the uncompressed
776 + * form.
777 + *
778 + * The worst case at the block level is a growth of the compressed data
779 + * of 5 bytes per 32767 bytes.
780 + *
781 + * The worst case internal to a compressed block is very hard to figure.
782 + * The worst case can at least be boundined by having one bit that represents
783 + * 32764 bytes and then all of the rest of the bytes representing the very
784 + * very last byte.
785 + *
786 + * All of which is enough to compute an amount of extra data that is required
787 + * to be safe.  To avoid problems at the block level allocating 5 extra bytes
788 + * per 32767 bytes of data is sufficient.  To avoind problems internal to a block
789 + * adding an extra 32767 bytes (the worst case uncompressed block size) is
790 + * sufficient, to ensure that in the worst case the decompressed data for
791 + * block will stop the byte before the compressed data for a block begins.
792 + * To avoid problems with the compressed data's meta information an extra 18
793 + * bytes are needed.  Leading to the formula:
794 + *
795 + * extra_bytes = (uncompressed_size >> 12) + 32768 + 18 + decompressor_size.
796 + *
797 + * Adding 8 bytes per 32K is a bit excessive but much easier to calculate.
798 + * Adding 32768 instead of 32767 just makes for round numbers.
799 + * Adding the decompressor_size is necessary as it musht live after all
800 + * of the data as well.  Last I measured the decompressor is about 14K.
801 + * 10K of actuall data and 4K of bss.
802 + *
803 + */
804 +
805 +/*
806 + * gzip declarations
807 + */
808 +
809 +#define OF(args)  args
810 +#define STATIC static
811 +
812 +#undef memcpy
813 +
814 +typedef unsigned char  uch;
815 +typedef unsigned short ush;
816 +typedef unsigned long  ulg;
817 +
818 +#define WSIZE 0x80000000       /* Window size must be at least 32k,
819 +                                * and a power of two
820 +                                * We don't actually have a window just
821 +                                * a huge output buffer so I report
822 +                                * a 2G windows size, as that should
823 +                                * always be larger than our output buffer.
824 +                                */
825 +
826 +static uch *inbuf;     /* input buffer */
827 +static uch *window;    /* Sliding window buffer, (and final output buffer) */
828 +
829 +static unsigned insize;  /* valid bytes in inbuf */
830 +static unsigned inptr;   /* index of next byte to be processed in inbuf */
831 +static unsigned long workspace;
832 +
833 +#define get_byte()  (inptr < insize ? inbuf[inptr++] : fill_inbuf())
834 +               
835 +/* Diagnostic functions */
836 +#ifdef DEBUG
837 +#  define Assert(cond,msg) {if(!(cond)) error(msg);}
838 +#  define Trace(x) fprintf x
839 +#  define Tracev(x) {if (verbose) fprintf x ;}
840 +#  define Tracevv(x) {if (verbose>1) fprintf x ;}
841 +#  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
842 +#  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
843 +#else
844 +#  define Assert(cond,msg)
845 +#  define Trace(x)
846 +#  define Tracev(x)
847 +#  define Tracevv(x)
848 +#  define Tracec(c,x)
849 +#  define Tracecv(c,x)
850 +#endif
851 +
852 +static int  fill_inbuf(void);
853 +  
854 +/*
855 + * This is set up by the setup-routine at boot-time
856 + */
857 +static unsigned char *real_mode; /* Pointer to real-mode data */
858 +extern unsigned char input_data[];
859 +extern int input_len;
860 +
861 +static void error(char *x);
862 +static void *memcpy(void *dest, const void *src, unsigned n);
863 +
864 +#ifdef CONFIG_X86_NUMAQ
865 +void *xquad_portio;
866 +#endif
867 +
868 +static void* memcpy(void* dest, const void* src, unsigned n)
869 +{
870 +       int i;
871 +       char *d = (char *)dest, *s = (char *)src;
872 +
873 +       for (i=0;i<n;i++) d[i] = s[i];
874 +       return dest;
875 +}
876 +
877 +/* ===========================================================================
878 + * Fill the input buffer. This is called only when the buffer is empty
879 + * and at least one byte is really needed.
880 + */
881 +static int fill_inbuf(void)
882 +{
883 +       error("ran out of input data");
884 +       return 0;
885 +}
886 +
887 +
888 +// When using LZMA in callback, the compressed length is not needed.
889 +// Otherwise you need a special version of lzma compression program
890 +// which will pad the compressed length in the header.
891 +#define _LZMA_IN_CB
892 +#include "LzmaDecode.h"
893 +#include "LzmaDecode.c"
894 +
895 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize);
896 +
897 +
898 +/*
899 + * Do the lzma decompression
900 + * When using LZMA in callback, the end of input stream is automatically determined
901 + */
902 +static int lzma_unzip(void)
903 +{
904 +
905 +       unsigned int i;  /* temp value */
906 +       unsigned int lc; /* literal context bits */
907 +       unsigned int lp; /* literal pos state bits */
908 +       unsigned int pb; /* pos state bits */
909 +       unsigned int uncompressedSize = 0;
910 +       unsigned char* p;
911 +       
912 +       ILzmaInCallback callback;
913 +       callback.Read = read_byte;
914 +
915 +       /* lzma args */
916 +       i = get_byte();
917 +       lc = i % 9, i = i / 9;
918 +       lp = i % 5, pb = i / 5;
919 +       
920 +       /* skip dictionary size */
921 +       for (i = 0; i < 4; i++) 
922 +               get_byte();
923 +       // get uncompressedSize         
924 +       p= (char*)&uncompressedSize;    
925 +       for (i = 0; i < 4; i++) 
926 +           *p++ = get_byte();
927 +           
928 +       //get compressedSize 
929 +       for (i = 0; i < 4; i++) 
930 +               get_byte();
931 +       
932 +       // point it beyond uncompresedSize
933 +       //workspace = window + uncompressedSize;
934 +       
935 +       /* decompress kernel */
936 +       if (LzmaDecode((unsigned char*)workspace, ~0, lc, lp, pb, &callback,
937 +               (unsigned char*)window, uncompressedSize, &i) == LZMA_RESULT_OK)
938 +               return 0;
939 +       else
940 +               return 1;
941 +}
942 +
943 +
944 +#ifdef  _LZMA_IN_CB
945 +static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
946 +{
947 +       static unsigned int i = 0;
948 +       static unsigned char val;
949 +       *bufferSize = 1;
950 +       val = get_byte();
951 +       *buffer = &val;
952 +       return LZMA_RESULT_OK;
953 +}      
954 +#endif
955 +
956 +static void error(char *x)
957 +{
958 +       while(1);       /* Halt */
959 +}
960 +
961 +asmlinkage void decompress_kernel(void *rmode, unsigned long end,
962 +                       uch *input_data, unsigned long input_len, uch *output)
963 +{
964 +       real_mode = rmode;
965 +
966 +       window = output;
967 +       inbuf  = input_data;    /* Input buffer */
968 +       insize = input_len;
969 +       inptr  = 0;
970 +
971 +       if ((u32)output & (CONFIG_PHYSICAL_ALIGN -1))
972 +               error("Destination address not CONFIG_PHYSICAL_ALIGN aligned");
973 +       if ((workspace = end) > ((-__PAGE_OFFSET-(512 <<20)-1) & 0x7fffffff))
974 +               error("Destination address too large");
975 +#ifndef CONFIG_RELOCATABLE
976 +       if ((u32)output != LOAD_PHYSICAL_ADDR)
977 +               error("Wrong destination address");
978 +#endif
979 +
980 +       lzma_unzip();
981 +       return;
982 +}
983 Index: linux-2.6.24.7/scripts/Makefile.lib
984 ===================================================================
985 --- linux-2.6.24.7.orig/scripts/Makefile.lib
986 +++ linux-2.6.24.7/scripts/Makefile.lib
987 @@ -166,4 +166,9 @@ cmd_objcopy = $(OBJCOPY) $(OBJCOPYFLAGS)
988  quiet_cmd_gzip = GZIP    $@
989  cmd_gzip = gzip -f -9 < $< > $@
990  
991 -
992 +# LZMA
993 +#
994 +quiet_cmd_lzma = LZMA $@
995 +cmd_lzma = bash -e scripts/lzma_kern $< $@ -lc7 -lp0 -pb0
996 +# to use lzmacomp,
997 +# cmd_lzma = lzmacomp $< 700 > $@
998 Index: linux-2.6.24.7/scripts/lzma_kern
999 ===================================================================
1000 --- /dev/null
1001 +++ linux-2.6.24.7/scripts/lzma_kern
1002 @@ -0,0 +1,4 @@
1003 +get-size() { echo "$5" ;}
1004 +printf -v len '%.8x' "$(get-size $(ls -l "$1"))"
1005 +lzma e "$@"
1006 +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"
1007 Index: linux-2.6.24.7/arch/x86/boot/compressed/Makefile_32
1008 ===================================================================
1009 --- linux-2.6.24.7.orig/arch/x86/boot/compressed/Makefile_32
1010 +++ linux-2.6.24.7/arch/x86/boot/compressed/Makefile_32
1011 @@ -4,8 +4,8 @@
1012  # create a compressed vmlinux image from the original vmlinux
1013  #
1014  
1015 -targets                := vmlinux vmlinux.bin vmlinux.bin.gz head_32.o misc_32.o piggy.o \
1016 -                       vmlinux.bin.all vmlinux.relocs
1017 +targets                := vmlinux vmlinux.bin vmlinux.bin.lzma head_32.o piggy.o \
1018 +                       vmlinux.bin.all vmlinux.relocs lzma_misc.o
1019  EXTRA_AFLAGS   := -traditional
1020  
1021  LDFLAGS_vmlinux := -T
1022 @@ -17,7 +17,7 @@ KBUILD_CFLAGS  := -m32 -D__KERNEL__ $(LI
1023            $(call cc-option,-fno-stack-protector)
1024  LDFLAGS := -m elf_i386
1025  
1026 -$(obj)/vmlinux: $(src)/vmlinux_32.lds $(obj)/head_32.o $(obj)/misc_32.o $(obj)/piggy.o FORCE
1027 +$(obj)/vmlinux: $(src)/vmlinux_32.lds $(obj)/head_32.o $(obj)/lzma_misc.o $(obj)/piggy.o FORCE
1028         $(call if_changed,ld)
1029         @:
1030  
1031 @@ -37,14 +37,14 @@ $(obj)/vmlinux.bin.all: $(vmlinux.bin.al
1032         $(call if_changed,relocbin)
1033  
1034  ifdef CONFIG_RELOCATABLE
1035 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin.all FORCE
1036 -       $(call if_changed,gzip)
1037 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin.all FORCE
1038 +       $(call if_changed,lzma)
1039  else
1040 -$(obj)/vmlinux.bin.gz: $(obj)/vmlinux.bin FORCE
1041 -       $(call if_changed,gzip)
1042 +$(obj)/vmlinux.bin.lzma: $(obj)/vmlinux.bin FORCE
1043 +       $(call if_changed,lzma)
1044  endif
1045  
1046  LDFLAGS_piggy.o := -r --format binary --oformat elf32-i386 -T
1047  
1048 -$(obj)/piggy.o: $(src)/vmlinux_32.scr $(obj)/vmlinux.bin.gz FORCE
1049 +$(obj)/piggy.o: $(src)/vmlinux_32.scr $(obj)/vmlinux.bin.lzma FORCE
1050         $(call if_changed,ld)