os/kernelhwsrv/brdbootldr/ubootldr/loadzip.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of the License "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 // ubootldr\loadzip.cpp
    15 // 
    16 //
    17 
    18 #define FILE_ID	0x4C5A4950
    19 
    20 #include <e32std.h>
    21 #include <e32std_private.h>
    22 #include "bootldr.h"
    23 #include "unzip.h"
    24 
    25 TUint8 Buffer[1024];
    26 
    27 GLDEF_C void AcceptUnzippedBlock(TZipInfo& aInfo, TUint8*& aOutPtr, TInt aError)
    28 	{
    29 	if (aError!=KErrNone)
    30 		{
    31 		PrintToScreen(_L("Error!\r\n"));
    32 		BOOT_FAULT();
    33 		}
    34 #ifdef __SUPPORT_FLASH_REPRO__
    35 	if (LoadToFlash)
    36 		{
    37 		// signal flash programming thread
    38 		TInt got=(TInt)((TLinAddr)aOutPtr-(TLinAddr)DestinationAddress());
    39 		NotifyDataAvailable(got);
    40 		}
    41 #endif
    42 	}
    43 
    44 GLDEF_C TInt UnzipComplete(TZipInfo& a, TUint8* aOutPtr, TInt aError)
    45 	{
    46 #ifdef __SUPPORT_FLASH_REPRO__
    47 	if (LoadToFlash)
    48 		NotifyDownloadComplete();
    49 #endif
    50 	return KErrNone;
    51 	}
    52 
    53 _LIT(KLitThreadName,"Unzip");
    54 TInt Initialise(TZipInfo& a)
    55 	{
    56 	TInt r=InitInfo(a);
    57 	if (r!=KErrNone)
    58 		return r;
    59 	a.iFileBufSize=4*a.iInBufSize;
    60 	TAny* pFileBuf=MALLOC(a.iFileBufSize);
    61 	if (!pFileBuf)
    62 		return KErrNoMemory;
    63 	a.iFileBuf=(TUint8*)pFileBuf;
    64 	RThread t;
    65 	r=t.Create(KLitThreadName,UnzipThread,0x2000,NULL,&a);
    66 	if (r!=KErrNone)
    67 		{
    68 		delete pFileBuf;
    69 		a.iFileBuf=NULL;
    70 		return r;
    71 		}
    72 	t.SetPriority(EPriorityLess);
    73 	t.Logon(a.iThreadStatus);
    74 	t.Resume();
    75 	a.iThreadHandle=t.Handle();
    76 	return KErrNone;
    77 	}
    78 
    79 void ProcessHeader(TZipInfo& a)
    80 	{
    81 	FileName=a.iName;
    82 	LoadSize=a.iUncompressedSize;
    83 	TInt size_mod_4k=LoadSize & 0xfff;
    84 	if (size_mod_4k==0)
    85 		ImageHeaderPresent=EFalse;
    86 	else if (size_mod_4k==256)
    87 		ImageHeaderPresent=ETrue;
    88 	else
    89 		{
    90 		PrintToScreen(_L("\r\n\r\nInvalid size\r\n"));
    91 		BOOT_FAULT();
    92 		}
    93 	ImageSize=ImageHeaderPresent ? LoadSize-256 : LoadSize;
    94 
    95 	PrintToScreen(_L("Unzip %lS, size %d\r\n"),&FileName,LoadSize);
    96 
    97 #ifdef __SUPPORT_FLASH_REPRO__
    98 	if (LoadToFlash)
    99 		{
   100 		TInt r=InitFlashWrite();
   101 		if (r!=KErrNone)
   102 			{
   103 			PrintToScreen(_L("InitFlashWrite returned %d\r\n"),r);
   104 			BOOT_FAULT();
   105 			}
   106 		}
   107 #endif
   108 
   109 	a.iOutBuf=(TUint8*)DestinationAddress();
   110 
   111 	a.iHeaderDone=2;
   112 	TRequestStatus* pS=&a.iProcessedHeader;
   113 
   114 	
   115 	RThread t;
   116 	t.SetHandle(a.iThreadHandle);
   117 	t.RequestComplete(pS,0);
   118 	}
   119 
   120 void Cleanup(TZipInfo& a)
   121 	{
   122 	delete a.iFileBuf;
   123 	a.iFileBuf=NULL;
   124 	a.iOutBuf=NULL;
   125 	RThread& t=*(RThread*)&a.iThreadHandle;
   126 	t.Close();
   127 	}
   128 
   129 TInt DoZipDownload(RFile &aBootFile)
   130 	{
   131 	TZipInfo z;
   132 	z.iRemain=FileSize;
   133 	InitProgressBar(0,(TUint)FileSize,_L("LOAD"));
   134 	TInt r=Initialise(z);
   135 	CHECK(r);
   136 	RThread t;
   137 	t.SetHandle(z.iThreadHandle);
   138 
   139 	while (z.iRemain && z.iThreadStatus==KRequestPending)
   140 		{
   141 		TRequestStatus dummy;
   142 		TRequestStatus* pS=&dummy;
   143 
   144 		r=ReadBlockToBuffer(z, aBootFile);
   145 		if (r != KErrNone)
   146 			{
   147 			PrintToScreen(_L("FAULT: Unzip Error %d\r\n"),r);
   148 			if (z.iFileBufW-z.iFileBufR==z.iFileBufSize)
   149 				{
   150 				PrintToScreen(_L("Check there is only one image\n\rin the zip file.\r\n"));
   151 				}
   152 			CHECK(r);
   153 			}
   154 
   155 		UpdateProgressBar(0,(TUint)(FileSize-z.iRemain));
   156 		t.RequestComplete(pS,0);		// same process
   157 		while(z.iHeaderDone==0 && z.iThreadStatus==KRequestPending)
   158 			{
   159 			DELAY(20000);
   160 			}
   161 		if (z.iHeaderDone==1 && z.iThreadStatus==KRequestPending)
   162 			{
   163 			// after reading first block, process the header
   164 			ProcessHeader(z);
   165 			}
   166 		}	// while
   167 
   168 	User::WaitForRequest(z.iThreadStatus);
   169 
   170 	TInt exitType=t.ExitType();
   171 	TInt exitReason=t.ExitReason();
   172 	if (z.iRemain || exitType!=EExitKill || exitReason!=KErrNone)
   173 		{
   174 		TBuf<32> exitCat=t.ExitCategory();
   175 		PrintToScreen(_L("Exit code %d,%d,%S\n"),exitType,exitReason,&exitCat);
   176 		TEST(0);
   177 		}
   178 
   179 	PrintToScreen(_L("Unzip complete\r\n"));
   180 	
   181 	TUint8* pD=Buffer;
   182 	TInt len=1024;
   183 
   184 	r=ReadInputData(pD,len);
   185 	TEST(r==KErrEof);
   186 
   187 
   188 	DELAY(20000);
   189 
   190 	Cleanup(z);
   191 	return KErrNone;
   192 	}
   193