code_decompression_hal.c 15.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2001
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   code_decompression_hal.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file perform the ZIMAGE and BOOT_ZIMAGE decompression processes.
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

#if defined(__ZIMAGE_SUPPORT__)

/*****************************************************************************
 * Includes
 *****************************************************************************/
#include "kal_general_types.h"
#include "kal_public_api.h"
#include "simple_memory_manager.h"
#include "cache_sw_int.h"
#include "code_decompression.h"
#include "custom_jump_tbl.h"
#include "viva.h"
#include "LzmaDec.h"

#define CPU_CACHE_LINE_SIZE_MASK 0x0000001F
#if defined(__EMPTY_MMI__)
#if (defined(__MAUI_BASIC__))
#pragma arm section rwdata = "CACHED_EXTSRAM_RW", zidata = "CACHED_EXTSRAM_ZI"
kal_uint8 g_zimage_working_buffer[(35*1024)];
#pragma arm section rwdata, zidata
#endif
#else
#define USE_MMI_MEMORY_AS_ZIMAGE_WORKING_MEMORY
#endif
#if defined(__MTK_TARGET__)
__weak extern kal_uint32 Image$$DYNAMIC_CACHEABLE_EXTSRAM_DEFAULT_CACHEABLE_ZI_MMIPOOL$$Base;
__weak extern kal_uint32 Image$$DYNAMIC_CACHEABLE_EXTSRAM_DEFAULT_CACHEABLE_ZI_MMIPOOL$$Length;
#endif
extern void *kal_tmp_mem_alloc(kal_uint32 size);
extern void kal_tmp_mem_free(void* mem_ptr);

extern SRes Private_LzmaFastDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
                            const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
                            ELzmaStatus *status, ISzAlloc *alloc, kal_uint32 *handle);
extern SRes Private_LzmaCheckDecode(ELzmaStatus *status, kal_uint32 handle);

/*****************************************************************************
 * Macros
 *****************************************************************************/
//#define ZIMAGE_MEASURE_ENABLE

#if defined (ZIMAGE_MEASURE_ENABLE)
extern kal_uint32 drv_get_current_time(void);
extern kal_uint32 drv_get_duration_tick(kal_uint32 previous_time, kal_uint32 current_time);

#define ZIMAGE_PARTITION_NUMBER  10

kal_uint32 __zimage_duration[ZIMAGE_PARTITION_NUMBER];
kal_uint32 __zimage_duration_index = 0;

#define ZIMAGE_MEASURE_START     do { __zimage_duration[__zimage_duration_index] = drv_get_current_time(); } while (0)
#define ZIMAGE_MEASURE_END       do { __zimage_duration[__zimage_duration_index++] = drv_get_duration_tick(__zimage_duration[i], drv_get_current_time()); } while (0)

#else

#define ZIMAGE_MEASURE_START
#define ZIMAGE_MEASURE_END

#endif

/*****************************************************************************
 * Data Structtures
 *****************************************************************************/
/* Use comment instead of enum intentionally
 * Type:
 * 0: SW LZMA
 * 1: SW LZMA with progress
 * 2: HW LZMA
*  3: SW LZMA with PCE-1
 */
typedef struct
{
   kal_uint32 type;
   kal_uint32 sourceAddress;
   kal_uint32 sourceSize;
   kal_uint32 destinationAddress;
   kal_uint32 destinationSize;
} ZIMAGEPartition;

typedef struct
{
   kal_uint32 partitionNumber;
   ZIMAGEPartition firstPartition;
} ZIMAGEHeader;

/*****************************************************************************
 * Prototypes
 *****************************************************************************/

/*****************************************************************************
 * Global Variables
 *****************************************************************************/
static STSMM _resource_decompress_smm;

/*****************************************************************************
 * Private Functions
 *****************************************************************************/
static void *zimage_malloc(void *p, unsigned int size)
{
   return smmMallocAligned(&_resource_decompress_smm, size, 4);
}

static void zimage_free(void *p, void *address)
{
   return;
}

/*****************************************************************************
 * Internal API Functions
 *****************************************************************************/
void ZImageDecompressByPartitionWithBuffer
         (kal_uint32 source, kal_uint32 destination,
          kal_uint32 notifyLength, kal_uint32 notifyTotalLength, kal_uint32 *notifyCurrentLength, ZImageNotifyFunction notifyFunction,
          kal_uint8 *workingBuffer, kal_uint32 workingBufferSize)
{
#if defined(__MTK_TARGET__)

   const ZIMAGEHeader *header = (const ZIMAGEHeader *)source;
   const ZIMAGEPartition *partitions = &header->firstPartition;
   kal_uint32 i;
   kal_uint32 handle;
   kal_uint32 fastDecode = 0;
   SRes ret;
   ELzmaStatus status;

   ISzAlloc alloc = { zimage_malloc, zimage_free };
   smmInit(&_resource_decompress_smm, workingBuffer, workingBufferSize);

   /* Foreach partition, perform the corresponding action */
   for (i = 0; i < header->partitionNumber; ++i)
   {
      /* Adjust the address from relative to absolute */
      kal_uint8 *sourceAddress = (kal_uint8 *)(source + partitions[i].sourceAddress);
      kal_uint8 *destinationAddress = (kal_uint8 *)(destination + partitions[i].destinationAddress);
      kal_uint32 sourceSize = partitions[i].sourceSize - (LZMA_PROPS_SIZE + 8);
      kal_uint32 destinationSize = partitions[i].destinationSize;

      if (0 == partitions[i].type)
      {
         ZIMAGE_MEASURE_START;

         ret = LzmaDecodeWithOption(destinationAddress, &destinationSize,
                                    sourceAddress + LZMA_PROPS_SIZE + 8, &sourceSize,
                                    sourceAddress, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &alloc,
                          LZMA_ADDON_OPTION_T);
         if(SZ_OK != ret)
         {
             ASSERT(0);
         }

         ZIMAGE_MEASURE_END;
      }
      else if (1 == partitions[i].type)
      {
         CLzmaDec state;
         kal_uint32 decompressLength;
         kal_uint32 sourceSizeCurrent;

         ZIMAGE_MEASURE_START;

         LzmaDec_Construct(&state);

         ret = LzmaDec_AllocateProbs(&state, sourceAddress, LZMA_PROPS_SIZE, &alloc);
         if(SZ_OK != ret)
         {
             ASSERT(0);
         }

         sourceAddress += LZMA_PROPS_SIZE + 8;

         state.dic = destinationAddress;
         state.dicBufSize = destinationSize;

         LzmaDec_Init(&state);

         for(decompressLength = 0; decompressLength < destinationSize; )
         {
            notifyFunction(*notifyCurrentLength + decompressLength, notifyTotalLength);

            sourceSizeCurrent = sourceSize;

            decompressLength += notifyLength;
            if(decompressLength > destinationSize)
            {
               decompressLength = destinationSize;
            }

            ret = LzmaDec_DecodeToDicWithOption(&state, decompressLength, sourceAddress,
                                                &sourceSizeCurrent, LZMA_FINISH_ANY, &status,
                                                LZMA_ADDON_OPTION_T);

            sourceAddress += sourceSizeCurrent;
            sourceSize -= sourceSizeCurrent;
         }
         notifyFunction(*notifyCurrentLength + decompressLength, notifyTotalLength);
         *notifyCurrentLength += destinationSize;

         if(SZ_OK != ret)
         {
             ASSERT(0);
         }

         ret = LzmaDec_DecodeFinishWithOption(&state, destinationAddress, &destinationSize,
                                              sourceAddress, &sourceSize, LZMA_FINISH_ANY, &status,
                                              LZMA_ADDON_OPTION_T);

         if (SZ_OK != ret)
         {
            ASSERT(0);
         }

         ZIMAGE_MEASURE_END;
      }
      else if (2 == partitions[i].type)
      {
         ZIMAGE_MEASURE_START;

         ret = Private_LzmaFastDecode
                         (destinationAddress, &destinationSize,
                          sourceAddress + LZMA_PROPS_SIZE + 8, &sourceSize,
                          sourceAddress, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &alloc, &handle);

         fastDecode = 1;

         if(SZ_OK != ret)
         {
             ASSERT(0);
         }

#if defined(ZIMAGE_MEASURE_ENABLE)
         /* Check decompress */
         if (fastDecode)
         {
            ret = Private_LzmaCheckDecode(&status, handle);

            if (SZ_OK != ret)
            {
               ASSERT(0);
            }
         }

         ZIMAGE_MEASURE_END;
#endif
      }
      else if (3 == partitions[i].type)
      {
         ZIMAGE_MEASURE_START;

         {
#if defined(__ALICE_SUPPORT__)
            extern kal_uint32 Image$$ALICE$$RO$$Base;
            extern kal_uint32 Image$$ALICE$$RO$$Limit;
            kal_uint32 dictBuffer = (kal_uint32)&Image$$ALICE$$RO$$Base;
            kal_uint32 dictSize = ((kal_uint32)&Image$$ALICE$$RO$$Limit) - dictBuffer;
            kal_uint32 dictInfo[2];
            dictSize = (0x400000 < dictSize) ? 0x400000 : dictSize;
            dictInfo[0] = dictBuffer;
            dictInfo[1] = dictSize;
            status = (ELzmaStatus)dictInfo;
            ret = LzmaDecodeWithOption(destinationAddress, &destinationSize,
                                       sourceAddress + LZMA_PROPS_SIZE + 8, &sourceSize,
                                       sourceAddress, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &alloc,
                                       LZMA_ADDON_OPTION_T_P);
#endif
         }
         if(SZ_OK != ret)
         {
             ASSERT(0);
         }

         ZIMAGE_MEASURE_END;
      }
      else
      {
         /* Unknown type */
         ASSERT(0);
      }
   }

#if !defined(ZIMAGE_MEASURE_ENABLE)
   /* Check decompress */
   if (fastDecode)
   {
      ret = Private_LzmaCheckDecode(&status, handle);

      if (SZ_OK != ret)
      {
         ASSERT(0);
      }
   }
#endif

   MTKL1CACHE_CleanAllCacheLines();

#endif /* __MTK_TARGET__ */
}

void ZImageDecompressByPartition(kal_uint32 source, kal_uint32 destination,
                                 kal_uint32 notifyLength, kal_uint32 notifyTotalLength,
                                 kal_uint32 *notifyCurrentLength, ZImageNotifyFunction notifyFunction)
{
#if defined(__MTK_TARGET__)
#if defined(USE_MMI_MEMORY_AS_ZIMAGE_WORKING_MEMORY)
#define ZIMAGE_DECOMPRESS_BUFFER_SIZE    (40*1024)
   kal_uint32 workingSize = ((kal_uint32)(&Image$$DYNAMIC_CACHEABLE_EXTSRAM_DEFAULT_CACHEABLE_ZI_MMIPOOL$$Length) - (CPU_CACHE_LINE_SIZE_MASK + 1));
   kal_uint32 mmimemAddr = (((kal_uint32)(&Image$$DYNAMIC_CACHEABLE_EXTSRAM_DEFAULT_CACHEABLE_ZI_MMIPOOL$$Base) + CPU_CACHE_LINE_SIZE_MASK) & ~CPU_CACHE_LINE_SIZE_MASK);
   kal_uint8 *workingBuffer = (kal_uint8 *)mmimemAddr;
   if (workingSize < ZIMAGE_DECOMPRESS_BUFFER_SIZE)
   {
      ASSERT(0);
   }
#else
#define ZIMAGE_DECOMPRESS_BUFFER_SIZE    (35*1024)
#if (defined(__MAUI_BASIC__))
	 kal_uint8 *workingBuffer = &g_zimage_working_buffer[0];
#else
   kal_uint8 *workingBuffer = (kal_uint8 *)kal_tmp_mem_alloc(ZIMAGE_DECOMPRESS_BUFFER_SIZE);
#endif
#endif

   ZImageDecompressByPartitionWithBuffer(source, destination,
                                         notifyLength, notifyTotalLength, notifyCurrentLength, notifyFunction,
                                         workingBuffer, ZIMAGE_DECOMPRESS_BUFFER_SIZE);

#if defined(USE_MMI_MEMORY_AS_ZIMAGE_WORKING_MEMORY)
   kal_mem_set((void*) mmimemAddr, 0x0, ZIMAGE_DECOMPRESS_BUFFER_SIZE);
#else
#if (defined(__MAUI_BASIC__))
#else
   kal_tmp_mem_free(workingBuffer);
#endif //#if (defined(__NOR_FLASH_HARDWARE_TEST__) && defined(__MAUI_BASIC__))
#endif
#endif
}

kal_uint32 ZImageGetNotifyLength(kal_uint32 source)
{
   const ZIMAGEHeader *header = (const ZIMAGEHeader *)source;
   const ZIMAGEPartition *partitions = &header->firstPartition;
   kal_uint32 i;
   kal_uint32 result = 0;

   /* Foreach partition, get the notify length */
   for (i = 0; i < header->partitionNumber; ++i)
   {
      if (1 == partitions[i].type)
      {
         result += partitions[i].destinationSize;
      }
   }

   return result;
}

/*****************************************************************************
 * Public API Functions
 *****************************************************************************/
extern unsigned int Image$$ZIMAGE_ER$$Base;
extern unsigned int Image$$ZIMAGE_ER$$Length;
extern unsigned int Image$$BOOT_ZIMAGE_ER$$Base;

#pragma arm section rwdata="ZIMAGE_RWDATA", zidata="ZIMAGE_ZIDATA"
int ZIMAGEDummyFlag = 0xFFFFFFFF;
#pragma arm section rwdata, zidata

kal_int32 ZImageDecompressHal(kal_uint32 notifyLength, ZImageNotifyFunction notifyFunction)
{
   kal_uint32 notifyTotalLength = 0;
   kal_uint32 notifyCurrentLength = 0;
   kal_uint32 count;

   notifyTotalLength += ZImageGetNotifyLength(vivaInfo.zimage_base);
   for(count = 0; count < MAX_COMPRESSED_BIN_SUPPORT; count++){
   	  if(GET_Res_IsExisted(count) && GET_Res_IsCompressed(count)){
         notifyTotalLength += ZImageGetNotifyLength(((kal_uint32)GET_Res_LoadAddr(count) + GET_Res_TBLSize(count)));
      }
   }

   ZImageDecompressByPartition(vivaInfo.zimage_base, (kal_uint32)&Image$$ZIMAGE_ER$$Base,
                               notifyLength, notifyTotalLength, &notifyCurrentLength, notifyFunction);
   /*For MBA with compression Feature*/
   for(count = 0; count < MAX_COMPRESSED_BIN_SUPPORT; count++){
      if(GET_Res_IsExisted(count) && GET_Res_IsCompressed(count)){
         ZImageDecompressByPartition(((kal_uint32)GET_Res_LoadAddr(count) + GET_Res_TBLSize(count)), (kal_uint32)GET_Res_ExecAddr(count),
               notifyLength, notifyTotalLength, &notifyCurrentLength, notifyFunction);
      }
   }

   ZIMAGEDummyFlag = 0;

   return 0;
}

void BootZImageDecompress(void)
{
#if defined(__BOOT_ZIMAGE_SUPPORT__)

#define BOOT_ZIMAGE_DECOMPRESS_BUFFER_SIZE    (35*1024)

   kal_uint8 *workingBuffer = (kal_uint8 *)&Image$$ZIMAGE_ER$$Base;
   kal_uint32 length = 0;

   ASSERT(BOOT_ZIMAGE_DECOMPRESS_BUFFER_SIZE <= (kal_uint32)&Image$$ZIMAGE_ER$$Length);

   ZImageDecompressByPartitionWithBuffer(vivaInfo.boot_zimage_base, (kal_uint32)&Image$$BOOT_ZIMAGE_ER$$Base,
                                         0, 0, &length, 0, workingBuffer, BOOT_ZIMAGE_DECOMPRESS_BUFFER_SIZE);

#endif
}

#else /* __ZIMAGE_SUPPORT__ */

#include "kal_general_types.h"
#include "code_decompression.h"

//kal_int32 ZImageDecompressHal(kal_uint32 notifyLength, ZImageNotifyFunction notifyFunction) { return 0; }
void BootZImageDecompress(void) { /* Do nothing */ }

#endif /* __ZIMAGE_SUPPORT__ */