decompress.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * LZMA compressed kernel decompressor for bcm947xx boards
  3. *
  4. * Copyright (C) 2005 by Oleg I. Vdovikin <oleg@cs.msu.su>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. *
  21. * Please note, this was code based on the bunzip2 decompressor code
  22. * by Manuel Novoa III (mjn3@codepoet.org), although the only thing left
  23. * is an idea and part of original vendor code
  24. *
  25. *
  26. * 12-Mar-2005 Mineharu Takahara <mtakahar@yahoo.com>
  27. * pass actual output size to decoder (stream mode
  28. * compressed input is not a requirement anymore)
  29. *
  30. * 24-Apr-2005 Oleg I. Vdovikin
  31. * reordered functions using lds script, removed forward decl
  32. *
  33. */
  34. #include "LzmaDecode.h"
  35. #define BCM4710_FLASH 0x1fc00000 /* Flash */
  36. #define KSEG0 0x80000000
  37. #define KSEG1 0xa0000000
  38. #define KSEG1ADDR(a) ((((unsigned)(a)) & 0x1fffffffU) | KSEG1)
  39. #define Index_Invalidate_I 0x00
  40. #define Index_Writeback_Inv_D 0x01
  41. #define cache_unroll(base,op) \
  42. __asm__ __volatile__( \
  43. ".set noreorder;\n" \
  44. ".set mips3;\n" \
  45. "cache %1, (%0);\n" \
  46. ".set mips0;\n" \
  47. ".set reorder\n" \
  48. : \
  49. : "r" (base), \
  50. "i" (op));
  51. static __inline__ void blast_icache(unsigned long size, unsigned long lsize)
  52. {
  53. unsigned long start = KSEG0;
  54. unsigned long end = (start + size);
  55. while(start < end) {
  56. cache_unroll(start,Index_Invalidate_I);
  57. start += lsize;
  58. }
  59. }
  60. static __inline__ void blast_dcache(unsigned long size, unsigned long lsize)
  61. {
  62. unsigned long start = KSEG0;
  63. unsigned long end = (start + size);
  64. while(start < end) {
  65. cache_unroll(start,Index_Writeback_Inv_D);
  66. start += lsize;
  67. }
  68. }
  69. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  70. struct trx_header {
  71. unsigned int magic; /* "HDR0" */
  72. unsigned int len; /* Length of file including header */
  73. unsigned int crc32; /* 32-bit CRC from flag_version to end of file */
  74. unsigned int flag_version; /* 0:15 flags, 16:31 version */
  75. unsigned int offsets[3]; /* Offsets of partitions from start of header */
  76. };
  77. #define EDIMAX_PS_HEADER_MAGIC 0x36315350 /* "PS16" */
  78. #define EDIMAX_PS_HEADER_LEN 0xc /* 12 bytes long for edimax header */
  79. /* beyound the image end, size not known in advance */
  80. extern unsigned char workspace[];
  81. unsigned int offset;
  82. unsigned char *data;
  83. /* flash access should be aligned, so wrapper is used */
  84. /* read byte from the flash, all accesses are 32-bit aligned */
  85. static int read_byte(void *object, unsigned char **buffer, UInt32 *bufferSize)
  86. {
  87. static unsigned int val;
  88. if (((unsigned int)offset % 4) == 0) {
  89. val = *(unsigned int *)data;
  90. data += 4;
  91. }
  92. *bufferSize = 1;
  93. *buffer = ((unsigned char *)&val) + (offset++ & 3);
  94. return LZMA_RESULT_OK;
  95. }
  96. static __inline__ unsigned char get_byte(void)
  97. {
  98. unsigned char *buffer;
  99. UInt32 fake;
  100. return read_byte(0, &buffer, &fake), *buffer;
  101. }
  102. /* should be the first function */
  103. void entry(unsigned long icache_size, unsigned long icache_lsize,
  104. unsigned long dcache_size, unsigned long dcache_lsize,
  105. unsigned long fw_arg0, unsigned long fw_arg1,
  106. unsigned long fw_arg2, unsigned long fw_arg3)
  107. {
  108. unsigned int i; /* temp value */
  109. unsigned int lc; /* literal context bits */
  110. unsigned int lp; /* literal pos state bits */
  111. unsigned int pb; /* pos state bits */
  112. unsigned int osize; /* uncompressed size */
  113. ILzmaInCallback callback;
  114. callback.Read = read_byte;
  115. /* look for trx header, 32-bit data access */
  116. for (data = ((unsigned char *) KSEG1ADDR(BCM4710_FLASH));
  117. ((struct trx_header *)data)->magic != TRX_MAGIC &&
  118. ((struct trx_header *)data)->magic != EDIMAX_PS_HEADER_MAGIC;
  119. data += 65536);
  120. if (((struct trx_header *)data)->magic == EDIMAX_PS_HEADER_MAGIC)
  121. data += EDIMAX_PS_HEADER_LEN;
  122. /* compressed kernel is in the partition 0 or 1 */
  123. if (((struct trx_header *)data)->offsets[1] > 65536)
  124. data += ((struct trx_header *)data)->offsets[0];
  125. else
  126. data += ((struct trx_header *)data)->offsets[1];
  127. offset = 0;
  128. /* lzma args */
  129. i = get_byte();
  130. lc = i % 9, i = i / 9;
  131. lp = i % 5, pb = i / 5;
  132. /* skip rest of the LZMA coder property */
  133. for (i = 0; i < 4; i++)
  134. get_byte();
  135. /* read the lower half of uncompressed size in the header */
  136. osize = ((unsigned int)get_byte()) +
  137. ((unsigned int)get_byte() << 8) +
  138. ((unsigned int)get_byte() << 16) +
  139. ((unsigned int)get_byte() << 24);
  140. /* skip rest of the header (upper half of uncompressed size) */
  141. for (i = 0; i < 4; i++)
  142. get_byte();
  143. /* decompress kernel */
  144. if (LzmaDecode(workspace, ~0, lc, lp, pb, &callback,
  145. (unsigned char*)LOADADDR, osize, &i) == LZMA_RESULT_OK)
  146. {
  147. blast_dcache(dcache_size, dcache_lsize);
  148. blast_icache(icache_size, icache_lsize);
  149. /* Jump to load address */
  150. ((void (*)(unsigned long, unsigned long, unsigned long,
  151. unsigned long)) LOADADDR)(fw_arg0, fw_arg1, fw_arg2,
  152. fw_arg3);
  153. }
  154. }