Update contrib.
1 // Copyright (c) 2005-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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // improvised boot loader mechanism
23 #include <e32const_private.h>
25 #include <e32std_private.h>
31 #include "bootloader_variantconfig.h"
32 #include <nkern/nk_trace.h>
35 #define FILE_ID 0x594D555D
38 void CloseAndDeleteFile();
41 // Extra stuff to determine inner compression from the ROM header
43 extern TInt memcmp1(const TUint8* aTrg, const TUint8* aSrc, TInt aLength);
45 // XXX FIXED DRIVE PATH
46 const TPtrC filePath=_L("d:\\");
48 #if defined(__SUPPORT_UNZIP__)
49 const TBool ZipIsSupported = ETrue;
51 const TBool ZipIsSupported = EFalse;
54 #if defined(__SUPPORT_FLASH_REPRO__)
55 const TBool FlashIsSupported = ETrue;
57 const TBool FlashIsSupported = EFalse;
63 TBool Zip; // set to ETrue if this file is a ZIP file
64 TBool Flash; // set to ETrue if this file is to be written to flash
65 TBool BootLoader; // set to ETrue if this file is to be flashed to the BootLoader address
66 TBool Delete; // set to ETrue if this file is to be deleted after loading into RAM
71 TFileTypes supportedFileTypes [] =
73 // Name Zip Flash BootLoader Delete
74 {_L("FLASHLDR.ZIP"), ETrue, ETrue, ETrue, ETrue },
75 {_L("FLASHLDR.BIN"), EFalse, ETrue, ETrue, ETrue },
77 {VARIANT_ZIP, ETrue, EFalse, EFalse, EFalse },
78 {VARIANT_BIN, EFalse, EFalse, EFalse, EFalse },
80 {_L("SYS$ROM.ZIP"), ETrue, EFalse, EFalse, EFalse },
81 {_L("SYS$ROM.BIN"), EFalse, EFalse, EFalse, EFalse },
83 {_L("FLASHIMG.ZIP"), ETrue, ETrue, EFalse, EFalse },
84 {_L("FLASHIMG.BIN"), EFalse, ETrue, EFalse, EFalse },
85 {_L("BOOTLDR.ZIP"), ETrue, ETrue, ETrue, EFalse },
86 {_L("BOOTLDR.BIN"), EFalse, ETrue, ETrue, EFalse },
88 {_L("COREIMG.BIN"), EFalse, EFalse, EFalse, ETrue },
90 {_L(""),0} // Last Entry - this empty row is used in code to detect table end
94 GLDEF_C TBool SearchDrivesRaw()
96 // Scan local drives directly (i.e. via TLocalDrv rather than RFS)
98 PrintToScreen(_L("Checking local drives directly.\r\n"));
100 TDriveInfoV1Buf diBuf;
101 UserHal::DriveInfo(diBuf);
102 TDriveInfoV1 &di=diBuf();
107 TInt LocalDriveNum = KErrNotFound;
110 for ( ; n<KMaxLocalDrives && LocalDriveNum == KErrNotFound; n++)
112 r = LocDrv.Connect(n, LocDrvChg);
116 RDebug::Print(_L("\nDrive %d: TBusLocalDrive::Connect() failed %d"), n, r);
120 TLocalDriveCapsV5Buf capsBuf;
121 TLocalDriveCapsV5& caps = capsBuf();
122 r = LocDrv.Caps(capsBuf);
125 RDebug::Print(_L("\nDrive %d: TBusLocalDrive::Caps() failed %d"), n, r);
130 PrintToScreen(_L("Found Drive %d OK\r\n"),n);
131 RDebug::Print(_L("\nDrive %d: %S"), n, &di.iDriveName[n]);
132 RDebug::Print(_L("PartitionType %X"), caps.iPartitionType);
133 RDebug::Print(_L("PartitionSize %ld"), caps.iSize);
135 // Check that drive is labelled as MMC or SDIO,
136 // note that this is a platform specific label...
137 if ((di.iDriveName[n].MatchF(_L("MultiMediaCard0")) == KErrNone) ||
138 (di.iDriveName[n].MatchF(_L("SDIOCard0")) == KErrNone))
140 if (caps.iPartitionType == KPartitionTypeROM)
142 RDebug::Print(_L("- ROM Partition"));
144 FileSize = caps.iSize;
153 if (LocalDriveNum == KErrNotFound)
155 RDebug::Print(_L("No ROM Partitions found"));
160 RDebug::Print(_L("Query ROM in drive %d"), LocalDriveNum);
163 InputFunction=ReadFromLocalDrive;
164 CloseInputFunction=CloseLocalDrive;
166 RDebug::Print(_L("Determine the compression"));
168 r = GetInnerCompression(ImageDeflated, RomLoaderHeaderExists);
172 PrintToScreen(_L("Unable to determine the compression!\r\n"));
177 // Put position back to start
185 TInt ReadFromLocalDrive(TUint8* aDest, TInt& aLength)
187 // construct as TPtr8(TUint8 *aBuf, TInt aMaxLength);
188 // .. because TBusLocalDrive.Read only understands descriptors
189 TPtr8 d(aDest, aLength);
191 TInt r = LocDrv.Read(LocDrvPos,aLength,d);
194 if (LocDrvPos >= FileSize)
196 // ROM read completely
204 void CloseLocalDrive()
210 GLDEF_C TBool SearchDrives()
212 // set up the list of files to look for -- this code will search through
213 // all available drives until it finds one of these files in a root
216 PrintToScreen(_L("Checking local drives.\r\n"));
219 TInt r = TheFs.Connect();
222 RDebug::Print(_L("FAULT: Connecting RFs returned %d\r\n"),r);
226 TFindFile finder(TheFs);
228 // for each file in the list
232 while ( (*(supportedFileTypes[NrChecked].filename.Ptr()) != 0) && (r == KErrNotFound))
234 if ( (!ZipIsSupported && (supportedFileTypes[NrChecked].Zip))
235 || (!FlashIsSupported && (supportedFileTypes[NrChecked].Flash))
238 //skip ZIP/FLASH file types if they aren't supported
242 TPtrC thisFile = supportedFileTypes[NrChecked].filename;
243 r = finder.FindByDir(thisFile, filePath);
246 PrintToScreen(_L("Found %s\r\n"), thisFile.Ptr());
255 LoadFile=--NrChecked; // predecrement since this was incremented at the end of the last loop
256 LoadDevice=ELoadDrive;
258 const TPtrC bootFileName = finder.File();
260 PrintToScreen(_L("Opening: %s\r\n"), supportedFileTypes[NrChecked].filename.Ptr());
262 r = bootFile.Open(TheFs, bootFileName, EFileRead);
266 PrintToScreen(_L("Bootfile failed to open - err %d.\r\n"),r);
271 ImageZip = supportedFileTypes[NrChecked].Zip;
272 LoadToFlash = supportedFileTypes[NrChecked].Flash;
273 FlashBootLoader = supportedFileTypes[NrChecked].BootLoader;
275 InputFunction=ReadFromFile;
276 CloseInputFunction = supportedFileTypes[NrChecked].Delete ? CloseAndDeleteFile : CloseFile;
280 r = GetInnerCompression(ImageDeflated, RomLoaderHeaderExists);
283 PrintToScreen(_L("Unable to determine the compression!\r\n"));
288 // Move file pos back to the beginning
290 r = bootFile.Seek(ESeekStart, pos);
294 r = bootFile.Size(FileSize);
298 PrintToScreen(_L("Opened, size: %d bytes.\r\n"), FileSize);
301 PrintToScreen(_L("ROM Image is deflated.\r\n"));
306 PrintToScreen(_L("Unable to read file size\r\n"));
310 // Found image - return true
318 TInt ReadFromFile(TUint8* aDest, TInt& aLength)
320 // construct as TPtr8(TUint8 *aBuf, TInt aMaxLength);
321 // .. because RFile.Read only understands descriptors
322 TPtr8 d(aDest, aLength);
324 TInt r = bootFile.Read(d);
326 if (d.Length() < aLength) // may happen at the end of a file
328 aLength = d.Length();
331 if (d.Length() == 0) // indicates end of file
333 if (FileSize-aLength == ImageReadProgress)
336 // this will drop through to the error code below and will fault
348 void CloseAndDeleteFile()
351 TInt r = bootFile.FullName(fileName);
353 PrintToScreen(_L("CloseAndDeleteFile() RFile::FullName returned %d"), r);
357 r = TheFs.Delete(fileName);
358 PrintToScreen(_L("Deleted file fileName %S"), &fileName);
361 //#define TROM_LOADER_HEADER_SIZE 0x100
362 #define BUFFER_SIZE (TROM_LOADER_HEADER_SIZE + sizeof(TRomHeader))
364 TInt GetInnerCompression(TBool &aImageDeflated, TBool &aRomLoaderHeaderExists )
368 TUint8 buffer[BUFFER_SIZE];
369 TInt bufferSize = BUFFER_SIZE;
371 const TUint8 * romLoaderSignature1 = (const TUint8*)"EPOC";
372 const TUint8 * romLoaderSignature2 = (const TUint8*)"ROM";
374 r = ReadInputData((TUint8*)&buffer, bufferSize);
377 PrintToScreen(_L("Unable to read loader headers... (size:%d)\r\n"), bufferSize);
383 TRomHeader* romHeader= (TRomHeader *) &buffer;
384 aRomLoaderHeaderExists = EFalse;
386 if( !memcmp1(buffer, romLoaderSignature1, 4) && !memcmp1(&buffer[8], romLoaderSignature2, 3) )
388 // We have TRomLoaderHeader skip it
389 romHeader = (TRomHeader *) (&buffer[TROM_LOADER_HEADER_SIZE]);
390 aRomLoaderHeaderExists = ETrue;
393 if(romHeader->iCompressionType == 0 )
395 RDebug::Print(_L("Image is NOT Compressed"));
396 aImageDeflated = EFalse;
397 FileSize = romHeader->iUncompressedSize;
398 PrintToScreen(_L("ROMSIZE:%d\r\n"), romHeader->iUncompressedSize);
399 RDebug::Print(_L("ROMSIZE:%d"), romHeader->iUncompressedSize);
401 if (romHeader->iPageableRomStart > 0)
403 PrintToScreen(_L("Paged ROM FOUND\r\n"));
404 RDebug::Print(_L("Paged ROM FOUND"));
405 FileSize = romHeader->iPageableRomStart;
406 RDebug::Print(_L("Unpaged ROMSIZE:%d"), romHeader->iPageableRomStart);
409 else if (romHeader->iCompressionType == KUidCompressionDeflate )
411 RDebug::Print(_L("Image is Compressed\r\n"));
412 aImageDeflated = ETrue;
413 FileSize = romHeader->iUnpagedUncompressedSize;
414 RDebug::Print(_L("Compressed ROMSIZE:%d\r\n"), romHeader->iUnpagedUncompressedSize);
418 RDebug::Print(_L("Not supported compression method:0x%08x\r\n"), romHeader->iCompressionType);
419 PrintToScreen(_L("Not supported compression method:0x%08x\r\n"), romHeader->iCompressionType);
420 r = KErrNotSupported;