/* should be the first function */ void entry(unsigned long icache_size, unsigned long icache_lsize, unsigned long dcache_size, unsigned long dcache_lsize, unsigned long fw_arg0, unsigned long fw_arg1, unsigned long fw_arg2, unsigned long fw_arg3) { CLzmaDec state; ISzAlloc dummy; ISeqInStream stream; ELzmaStatus status; unsigned int i; /* temp value */ unsigned int osize; /* uncompressed size */ dummy.Alloc = dummy_alloc; dummy.Free = dummy_free; stream.Read = read_bytes; /* look for trx header, 32-bit data access */ for (data = ((unsigned char *)KSEG1ADDR(BCM4710_FLASH)); ((struct trx_header *)data)->magic != TRX_MAGIC; data += 65536); /* compressed kernel is in the partition 1 */ data += ((struct trx_header *)data)->offsets[1]; offset = 0; /* read lzma stream header */ SeqInStream_Read(&stream, workspace, LZMA_PROPS_SIZE + 8); /* read the lower half of uncompressed size in the header */ osize = (workspace[LZMA_PROPS_SIZE + 0]) + (workspace[LZMA_PROPS_SIZE + 1] << 8) + (workspace[LZMA_PROPS_SIZE + 2] << 16) + (workspace[LZMA_PROPS_SIZE + 3] << 24); LzmaDec_Construct(&state); LzmaDec_AllocateProbs(&state, workspace, LZMA_PROPS_SIZE, &dummy); state.dic = (unsigned char *)LOADADDR; state.dicBufSize = osize; /* decompress kernel */ LzmaDec_Init(&state); do { i = LZMA_REQUIRED_INPUT_MAX; SeqInStream_Read(&stream, workspace, i); if (LzmaDec_DecodeToDic(&state, osize, workspace, &i, LZMA_FINISH_ANY, &status) != SZ_OK) { /* something went wrong */ return; } } while (status == LZMA_STATUS_NEEDS_MORE_INPUT); blast_dcache(dcache_size, dcache_lsize); blast_icache(icache_size, icache_lsize); /* jump to load address */ ((void (*)(unsigned long, unsigned long, unsigned long, unsigned long))LOADADDR)(fw_arg0, fw_arg1, fw_arg2, fw_arg3); }
static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream) { UInt64 unpackSize; int i; SRes res = 0; CLzmaDec state; /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */ unsigned char header[LZMA_PROPS_SIZE + 8]; /* Read and parse header */ RINOK(SeqInStream_Read(inStream, header, sizeof(header))); unpackSize = 0; for (i = 0; i < 8; i++) unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8); LzmaDec_Construct(&state); RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); res = Decode2(&state, outStream, inStream, unpackSize); LzmaDec_Free(&state, &g_Alloc); return res; }
SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, Bool *isIndex, UInt32 *headerSizeRes) { Byte header[XZ_BLOCK_HEADER_SIZE_MAX]; unsigned headerSize; *headerSizeRes = 0; RINOK(SeqInStream_ReadByte(inStream, &header[0])); headerSize = ((unsigned)header[0] << 2) + 4; if (headerSize == 0) { *headerSizeRes = 1; *isIndex = True; return SZ_OK; } *isIndex = False; *headerSizeRes = headerSize; RINOK(SeqInStream_Read(inStream, header + 1, headerSize - 1)); return XzBlock_Parse(p, header); }
static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize) { SRes res; size_t inSize = (size_t)fileSize; Byte *inBuffer = 0; Byte *outBuffer = 0; Byte *filteredStream = 0; size_t outSize; CLzmaEncProps props; LzmaEncProps_Init(&props); LzmaEncProps_Normalize(&props); if (inSize != 0) { inBuffer = (Byte *)MyAlloc(inSize); if (inBuffer == 0) return SZ_ERROR_MEM; } else { return SZ_ERROR_INPUT_EOF; } if (SeqInStream_Read(inStream, inBuffer, inSize) != SZ_OK) { res = SZ_ERROR_READ; goto Done; } // we allocate 105% of original size + 64KB for output buffer outSize = (size_t)fileSize / 20 * 21 + (1 << 16); outBuffer = (Byte *)MyAlloc(outSize); if (outBuffer == 0) { res = SZ_ERROR_MEM; goto Done; } { int i; for (i = 0; i < 8; i++) outBuffer[i + LZMA_PROPS_SIZE] = (Byte)(fileSize >> (8 * i)); } if (mConType != NoConverter) { filteredStream = (Byte *)MyAlloc(inSize); if (filteredStream == 0) { res = SZ_ERROR_MEM; goto Done; } memcpy(filteredStream, inBuffer, inSize); if (mConType == X86Converter) { { UInt32 x86State; x86_Convert_Init(x86State); x86_Convert(filteredStream, (SizeT) inSize, 0, &x86State, 1); } } } { size_t outSizeProcessed = outSize - LZMA_HEADER_SIZE; size_t outPropsSize = LZMA_PROPS_SIZE; res = LzmaEncode(outBuffer + LZMA_HEADER_SIZE, &outSizeProcessed, mConType != NoConverter ? filteredStream : inBuffer, inSize, &props, outBuffer, &outPropsSize, 0, NULL, &g_Alloc, &g_Alloc); if (res != SZ_OK) goto Done; outSize = LZMA_HEADER_SIZE + outSizeProcessed; } if (outStream->Write(outStream, outBuffer, outSize) != outSize) res = SZ_ERROR_WRITE; Done: MyFree(outBuffer); MyFree(inBuffer); MyFree(filteredStream); return res; }
static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize) { SRes res; size_t inSize = (size_t)fileSize; Byte *inBuffer = 0; Byte *outBuffer = 0; size_t outSize = 0; size_t inSizePure; ELzmaStatus status; UInt64 outSize64 = 0; int i; if (inSize < LZMA_HEADER_SIZE) return SZ_ERROR_INPUT_EOF; inBuffer = (Byte *)MyAlloc(inSize); if (inBuffer == 0) return SZ_ERROR_MEM; if (SeqInStream_Read(inStream, inBuffer, inSize) != SZ_OK) { res = SZ_ERROR_READ; goto Done; } for (i = 0; i < 8; i++) outSize64 += ((UInt64)inBuffer[LZMA_PROPS_SIZE + i]) << (i * 8); outSize = (size_t)outSize64; if (outSize != 0) { outBuffer = (Byte *)MyAlloc(outSize); if (outBuffer == 0) { res = SZ_ERROR_MEM; goto Done; } } else { res = SZ_OK; goto Done; } inSizePure = inSize - LZMA_HEADER_SIZE; res = LzmaDecode(outBuffer, &outSize, inBuffer + LZMA_HEADER_SIZE, &inSizePure, inBuffer, LZMA_PROPS_SIZE, LZMA_FINISH_END, &status, &g_Alloc); if (res != SZ_OK) goto Done; if (mConType == X86Converter) { UInt32 x86State; x86_Convert_Init(x86State); x86_Convert(outBuffer, (SizeT) outSize, 0, &x86State, 0); } if (outStream->Write(outStream, outBuffer, outSize) != outSize) res = SZ_ERROR_WRITE; Done: MyFree(outBuffer); MyFree(inBuffer); return res; }