Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2603 lines
107 KiB

  1. /*
  2. * $Log: V:/Flite/archives/TrueFFS5/Src/DOCBDK.C_V $
  3. *
  4. * Rev 1.32 Apr 15 2002 20:14:30 oris
  5. * Bug fix - No longer ignore the last binary block of high capacity (512MB and up) INFTL formatted device. This bug caused the placeExbByBuffer routine to fail.
  6. * Bug fix - Physical size of INFTL formatted binary partition was 1 unit smaller then the real size.
  7. *
  8. * Rev 1.31 Apr 15 2002 07:35:38 oris
  9. * Bug fix - bdkErase routine did not made sure not to erase OTP and DPS
  10. * units of floors > 0.
  11. *
  12. * Rev 1.30 Feb 19 2002 20:58:44 oris
  13. * Changed debug print.
  14. * Bug fix - binary partition physical length was not reported properly , due to bad casting.
  15. *
  16. * Rev 1.29 Jan 20 2002 11:09:42 oris
  17. * Added debug print.
  18. *
  19. * Rev 1.28 Jan 17 2002 22:58:56 oris
  20. * Force calling bdkInit if not called.
  21. * Moved declaration of internal variables to mtdsa.c
  22. * Bug fix NO_INFTL_FAMILY_SUPPORT and NO_NFTL_FAMILY_SUPPORT where changed to NO_INFTL_SUPPORT and NO_INFTL_SUPPORT to comply with header file.
  23. * Prevent creating binary partition with signature 0xffff ffff
  24. * bdkInit uses tffsset instead of zeroing all the structure fields.
  25. * Bug fix - retrieveHeader routine used a word variable instead of dword.
  26. * freePointer call uses DOC_WIN_SIZE instead of DOC_WIN (part of new docsys ,mechanism).
  27. * BDK now take socket and flash records using flSocketOf() and flFlashOf(). The records themselves are defined in mtdsa.c
  28. * Use PROTECTABLE definition instead of 1 in calls to protectionSet.
  29. * Added support for flVerifyWrite runtime variable for binary partitions
  30. *
  31. * Rev 1.27 Nov 16 2001 00:19:48 oris
  32. * Bug fix - TrueFFS with verify write calling erase and then update block without erase flag, will fail.
  33. *
  34. * Rev 1.26 Nov 08 2001 10:45:04 oris
  35. * Removed warnings.
  36. * Bug fix - DiskOnChip with different number of blocks in the last floor.
  37. *
  38. * Rev 1.25 Oct 18 2001 22:17:02 oris
  39. * Bug fix - Missing support for binary partiiton on M+ that spans over a single floor.
  40. *
  41. * Rev 1.24 Oct 10 2001 19:48:14 oris
  42. * Bug fix - missing return statment caused the bdkGetProtectionType routine to call bdkSetProtectionType and therfore return a failing status.
  43. *
  44. * Rev 1.23 Sep 25 2001 17:37:00 oris
  45. * Bug fix - bdkIdentifyProtection routine did not update after change protection call.
  46. *
  47. * Rev 1.22 Sep 15 2001 23:45:04 oris
  48. * Bug fix - Changeable protection type was reported by the MTD even if user did not ask for it.
  49. * Bug fix - Big endian casting caused wrong protection type to be returned.
  50. *
  51. * Rev 1.21 Jul 13 2001 01:00:48 oris
  52. * Bug fix - when skipping bad blocks we no longer mark the bad block with the signature.
  53. * Send default key before any protection change operation.
  54. *
  55. * Rev 1.20 May 30 2001 21:10:22 oris
  56. * Bug fix - meida header was converted from little indien twice therefore suppling wrong data.
  57. *
  58. * Rev 1.19 May 17 2001 21:17:50 oris
  59. * Improoved documentation of error codes.
  60. *
  61. * Rev 1.18 May 17 2001 16:51:08 oris
  62. * Removed warnings.
  63. *
  64. * Rev 1.17 May 16 2001 21:17:04 oris
  65. * Bug fix - One of the "ifndef" statement of NO_DOCPLUS_FAMILY_SUPPORT was coded as "ifdef".
  66. * Bug fix - bdkEraseBootArea routine.
  67. * Changed bdkCopyBootAreaFile and bdkUpdateBootAreaFile interface. The signature is not an unsigned char pointer and not signed.
  68. * Removed warnings.
  69. * Added arguments check in bdkSetProtectionType routine and forced the presence of PROTECTABLE flag.
  70. * Compilation problems for MTD_STANDALONE were fixed.
  71. * Changed DATA definition to FL_DATA.
  72. *
  73. * Rev 1.16 May 09 2001 00:32:02 oris
  74. * Removed the DOC2000_FAMILY and DOCPLUS_FAMILY defintion and replaced it with NO_DOC2000_FAMILY_SUPPORT, NO_DOCPLUS_FAMILY_SUPPORT, NO_NFTL_SUPPORT and NO_INFTL_SUPPORT.
  75. * Bug fix - bdkEraseBootArea did not erase more then a single unit.
  76. * Bug fix - protection routine did not support floors properly.
  77. * Added OTP and unique ID routines for Millennium Plus .
  78. *
  79. * Rev 1.15 May 06 2001 22:41:42 oris
  80. * Removed warnings.
  81. *
  82. * Rev 1.14 May 02 2001 06:43:56 oris
  83. * Bug fix - bdkRetrieveHeader routine with cascaded floors.
  84. *
  85. * Rev 1.13 May 01 2001 14:23:28 oris
  86. * Removed warrnings.
  87. *
  88. * Rev 1.12 Apr 30 2001 17:59:04 oris
  89. * Removed misleading debug massage when calling bdkCheckSignOffset.
  90. * Reviced bdkRetrieveHeader routine not to use the MTD readBBT routine.
  91. * Added initialization of the erasbleBlockSizeBits variable in order to simplify multiplications.
  92. * Changed bdkSetBootPartitonNo, bdkGetProtectionType, bdkSetProtection prototypes.
  93. * Bug fix - bdkGetPartitionType routine. Missing case caused type to be 0 at all times.
  94. *
  95. * Rev 1.11 Apr 16 2001 13:30:54 oris
  96. * Bug fix - bad comparison with bdk flag.
  97. * Bug fix - proection prevented read binary area since the dps were read protected.
  98. * Removed warrnings.
  99. *
  100. * Rev 1.10 Apr 12 2001 06:49:50 oris
  101. * Added forceDownload routine
  102. * Changed checkWinForDoc routine to be under ifndef MTD_STANDALONE.
  103. *
  104. * Rev 1.9 Apr 10 2001 16:40:56 oris
  105. * bug fixed for big_endien in bdkmount routine.
  106. *
  107. * Rev 1.8 Apr 09 2001 14:59:24 oris
  108. * Bug fix in retreave header routine - header was searched on the first floor only.
  109. * End with an empty line.
  110. *
  111. * Rev 1.7 Apr 01 2001 07:50:18 oris
  112. * Updated copywrite notice.
  113. * Removed nested comments.
  114. * Removed static type from bdkVol for placinf exb file and standlone applications.
  115. * Changed readBBT function call since prototype was changed.
  116. * Bug fix in bdkretreave header routine - added casting to bbt pointer and iUnit.
  117. * Fix for Big endien compilation problems.
  118. * Remove unneeded variables.
  119. * Bad spelling of "..changable..".
  120. * Changed h\w to h/w.
  121. * Changed 2400 family to doc plus family.
  122. * Adde casting in calls to protection routine.
  123. *
  124. * Rev 1.6 Feb 20 2001 15:55:28 oris
  125. * Bug fix - global partiton and socket variables were redaclared in bdcall routine.
  126. *
  127. * Rev 1.5 Feb 14 2001 02:36:44 oris
  128. * Changed FLFlash and FLScoket records (used for the BDK) from static to global for the putImage utility.
  129. *
  130. * Rev 1.4 Feb 13 2001 01:41:50 oris
  131. * bdkPartitionInfo returns the number of binary partitions in flags field of the bdkStruct and not irFlags
  132. *
  133. * Rev 1.3 Feb 07 2001 18:15:24 oris
  134. * Changed else in bdkRetreaveHeader routine otherwise would not compiling bdk with a single doc family.
  135. * Changed the socketTable and mtdTable so BDK package can compile.
  136. *
  137. * Rev 1.2 Feb 05 2001 20:46:30 oris
  138. * Read the changable protection flag in protectionChangeInit routine
  139. * from the media header and not from the MTD.
  140. *
  141. * Rev 1.1 Feb 05 2001 20:10:56 oris
  142. * Removed // comments and added missing ;
  143. *
  144. * Rev 1.0 Feb 02 2001 13:19:44 oris
  145. * Initial revision.
  146. *
  147. */
  148. /***********************************************************************************/
  149. /* M-Systems Confidential */
  150. /* Copyright (C) M-Systems Flash Disk Pioneers Ltd. 1995-2001 */
  151. /* All Rights Reserved */
  152. /***********************************************************************************/
  153. /* NOTICE OF M-SYSTEMS OEM */
  154. /* SOFTWARE LICENSE AGREEMENT */
  155. /* */
  156. /* THE USE OF THIS SOFTWARE IS GOVERNED BY A SEPARATE LICENSE */
  157. /* AGREEMENT BETWEEN THE OEM AND M-SYSTEMS. REFER TO THAT AGREEMENT */
  158. /* FOR THE SPECIFIC TERMS AND CONDITIONS OF USE, */
  159. /* OR CONTACT M-SYSTEMS FOR LICENSE ASSISTANCE: */
  160. /* E-MAIL = [email protected] */
  161. /***********************************************************************************/
  162. /************************************************/
  163. /* B i n a r y D e v e l o p m e n t K i t */
  164. /* ------------------------------------------- */
  165. /************************************************/
  166. /*****************************************************************************
  167. * File Header *
  168. * ----------- *
  169. * Name : docbdk.c *
  170. * *
  171. * Description : This file contains the binary partition handling routines. *
  172. * *
  173. * Note : The file has 2 interfaces each under its own compilation flag: *
  174. * *
  175. * BDK package - Standalone package that exports routines for binary *
  176. * partitions handling(MTD_STANDALONE compilation flag). *
  177. * OSAK module - Separated module of the OSAK package that exports a *
  178. * common entry point to the same routines. (BDK_ACCESS *
  179. * compilation flag). *
  180. * *
  181. * Warning : Do not use this file with the BDK_ACCESS compilation flag unless *
  182. * you own the full OSAK package. *
  183. *****************************************************************************/
  184. #include "docbdk.h"
  185. #ifndef MTD_STANDALONE
  186. #include "doc2exb.h"
  187. #include "blockdev.h"
  188. #endif /* MTD_STANDALONE */
  189. #if (defined(MTD_STANDALONE) && defined(ACCESS_BDK_IMAGE)) || defined(BDK_ACCESS)
  190. /*********************** Global Variables Start******************************/
  191. /* conversion table from OSAK handles to binary partition handles */
  192. static byte handleTable[SOCKETS][MAX_BINARY_PARTITIONS_PER_DRIVE];
  193. static byte noOfPartitions=0; /* number of mounted binary partition */
  194. static byte globalPartitionNo=0; /* The current partition number */
  195. static byte globalSocketNo = 0;
  196. BDKVol bdkVols[BINARY_PARTITIONS]; /* binary partitions records */
  197. BDKVol* bdkVol=bdkVols; /* pointer to current binary partition */
  198. #ifdef MTD_STANDALONE
  199. FLBoolean globalInitStatus = FALSE;
  200. #endif /* MTD_STANDALONE */
  201. /*********************** Internal Function Protoype *************************/
  202. static FLStatus bdkMount (void);
  203. static FLStatus getBootAreaInfo (word startUnit , byte FAR2* signature);
  204. static CardAddress getPhysAddressOfUnit(word startUnit);
  205. /*--------------------------------------------------------------------------*
  206. * b d k I n i t
  207. *
  208. * Initialize all binary partition global variables
  209. *
  210. * Note : This function is called automaticly by bdkFindDiskOnChip.
  211. *
  212. * Parameters: None
  213. *
  214. * global variable output:
  215. * bdkVols - initialized array of binary partitions records
  216. * bdkVol - current binary partition record set to the first
  217. *
  218. * Return: Nothing
  219. *
  220. * Routine for both OSAK and the BDK stand alone package.
  221. *--------------------------------------------------------------------------*/
  222. void bdkInit(void)
  223. {
  224. byte index;
  225. /* initialize binary partitions records with defaultive values */
  226. tffsset(bdkVols,0,sizeof(bdkVols));
  227. tffsset(handleTable,BDK_INVALID_VOLUME_HANDLE,sizeof(handleTable));
  228. for (bdkVol=bdkVols,index=0;index<BINARY_PARTITIONS;bdkVol++,index++)
  229. {
  230. bdkVol->bdkGlobalStatus = BDK_S_INIT;
  231. bdkVol->bdkSignOffset = BDK_SIGN_OFFSET;
  232. #ifdef EDC_MODE
  233. bdkVol->bdkEDC = EDC;
  234. #endif /* EDC_MODE */
  235. #ifdef UPDATE_BDK_IMAGE
  236. bdkVol->updateImageFlag = BDK_COMPLETE_IMAGE_UPDATE;
  237. #endif /* UPDATE_BDK_IMAGE */
  238. }
  239. /* Initialize binary handles conversion table with invalid values */
  240. #ifdef MTD_STANDALONE
  241. noOfMTDs = 0;
  242. globalPartitionNo=0;
  243. globalSocketNo = 0;
  244. globalInitStatus = TRUE;
  245. #endif /* MTD_STANDALONE */
  246. noOfPartitions=0;
  247. bdkVol=bdkVols;
  248. }
  249. /*--------------------------------------------------------------------------*
  250. * g e t P h y s A d d r e s s O f U n i t
  251. *
  252. * Return the physical address of a unit according to its number
  253. * in the binary sub partition.
  254. *
  255. * Note : The sub partition is assumed to be mounted (BDK_S_INFO_FOUND is on).
  256. *
  257. * Parameters :
  258. * startUnit : unit number in the binary sub partition.
  259. *
  260. * global variable input:
  261. * bdkVol - current binary partition record
  262. * flash - flash record enabling media I\O
  263. * startImageBlock - physical unit number of the first and
  264. * endImageBlock last units marked with the signature
  265. * signBuffer - the sub partition signature
  266. * bdkSignOffset - current signature offset
  267. * erasableBlockBits - number of bits representing a unit
  268. *
  269. * Return :
  270. * Physical address of the unit in the BDK partition or 0 if failed
  271. *
  272. * Routine for both OSAK and the BDK stand alone package.
  273. *-------------------------------------------------------------------*/
  274. static CardAddress getPhysAddressOfUnit(word startUnit)
  275. {
  276. word iBlock;
  277. byte signRead[BDK_SIGNATURE_NAME];
  278. FLFlash * flash=bdkVol->flash;
  279. FLStatus status;
  280. for(iBlock=bdkVol->startImageBlock;
  281. ((startUnit > 0) && (iBlock <= bdkVol->endImageBlock));iBlock++)
  282. {
  283. if ((dword)(iBlock % bdkVol->blockPerFloor) < bdkVol->flash->firstUsableBlock)
  284. {
  285. iBlock = (word)(iBlock + bdkVol->flash->firstUsableBlock);
  286. if (iBlock > bdkVol->endImageBlock)
  287. break;
  288. }
  289. status = flash->read( flash , bdkVol->bdkSignOffset +
  290. ((CardAddress)iBlock << bdkVol->erasableBlockBits) ,
  291. signRead , BDK_SIGNATURE_NAME , EXTRA);
  292. if(status!=flOK)
  293. {
  294. DEBUG_PRINT(("(EXTRA)read failed.\r\n"));
  295. return 0L;
  296. }
  297. if(tffscmp((void FAR1*)signRead,(void FAR1*)(bdkVol->signBuffer),BDK_SIGNATURE_NAME)==0)
  298. startUnit--;
  299. }
  300. return( (CardAddress)iBlock << bdkVol->erasableBlockBits );
  301. }
  302. /*--------------------------------------------------------------------------*
  303. * g e t B o o t A r e a I n f o
  304. *
  305. * Mount the binary sub partitions by reading the signature area of each
  306. * unit of the entire partition.
  307. *
  308. * Note : Assume that the DiskOnChip was already found (bdkFoundDiskOnChip).
  309. *
  310. * Parameters :
  311. * startUnit : unit number in the binary sub partition.
  312. * signature : signature of the binary sub partition.
  313. *
  314. * global variable input :
  315. * bdkVol - current binary partition record
  316. * bdkGlobalStatus - partition predsent status.
  317. * bdkSavedStartUnit - start unit of previous access
  318. * bdkSavedSignOffset - signature offset of previous access
  319. * bdkSignOffset - current signature offset
  320. * bdkDocWindow - explicitly sets the windows address
  321. *
  322. * global variable output :
  323. * flash - flash record enabling media I\O
  324. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  325. * signBuffer - initialize with given signature
  326. * bdkSavedStartUnit - save start unit for next access
  327. * bdkSavedSignOffset - save current signature for next access
  328. * startPartitionBlock - low physical boundry of the partition
  329. * endPartitionBlock - high physical boundry of the partition
  330. * startImageBlock - physical unit number of the first and
  331. * endImageBlock last units marked with the signature
  332. * bootImageSize - size of the entire sub partition in bytes
  333. * realBootImageSize - number of units writen in the sub partition
  334. *
  335. * Return :
  336. * flOK - success
  337. * flDriveNotAvailable - DiskOnChip ASIC was not found
  338. * flUnknownMedia - failed in Flash chips recognition
  339. * flBadFormat - TL format does not exists
  340. * flNoSpaceInVolume - there are 0 units marked with this signature
  341. * flDataError - MTD read fault.
  342. * flHWProtect - HW read protection was triggerd
  343. * flFeatureNotSupported - Not a DiskOnChip device.
  344. *
  345. * Routine for both OSAK and the BDK stand alone package.
  346. *-------------------------------------------------------------------*/
  347. static FLStatus getBootAreaInfo( word startUnit , byte FAR2* signature )
  348. {
  349. FLFlash * flash;
  350. word iBlock;
  351. word numBlock;
  352. #ifdef MTD_STANDALONE
  353. dword temp;
  354. checkStatus(bdkFindDiskOnChip((dword FAR2 *)&temp,(dword FAR2 *)&temp));
  355. DEBUG_PRINT(("Debug: getBootAreaInfo() - DiskOnChip found.\r\n"));
  356. #endif /* MTD_STANDALONE */
  357. /* set bdkVol pointer to the proper binary partition */
  358. checkStatus(bdkMount());
  359. DEBUG_PRINT(("Debug: getBootAreaInfo() - BDK mount succeed.\r\n"));
  360. /* Check if this sub-partition was already analized */
  361. if ((!(bdkVol->bdkGlobalStatus & BDK_S_INFO_FOUND)) ||
  362. (tffscmp((void FAR1 *)(bdkVol->signBuffer),
  363. (void FAR1 *)signature,BDK_SIGNATURE_NAME) != 0) ||
  364. (bdkVol->bdkSignOffset!=bdkVol->bdkSavedSignOffset) ||
  365. (startUnit<bdkVol->bdkSavedStartUnit) ||
  366. (bdkVol->bdkSavedStartUnit + (bdkVol->realBootImageSize >>
  367. bdkVol->erasableBlockBits)<= startUnit))
  368. {
  369. /* The partition needs mounting */
  370. tffscpy((void FAR1 *)(bdkVol->signBuffer),(void FAR1 *)signature,
  371. BDK_SIGNATURE_NAME);
  372. bdkVol->bdkSavedSignOffset = bdkVol->bdkSignOffset;
  373. bdkVol->bdkSavedStartUnit = startUnit;
  374. bdkVol->startImageBlock = 0;
  375. bdkVol->bootImageSize = 0L;
  376. bdkVol->realBootImageSize = 0L;
  377. /* Find the boundries and number of the units marked with the signature */
  378. DEBUG_PRINT(("Debug: searching for signtured blocks.\r\n"));
  379. flash = bdkVol->flash;
  380. numBlock = 0;
  381. for (numBlock = 0 , iBlock = bdkVol->startPartitionBlock;
  382. iBlock<=bdkVol->endPartitionBlock;iBlock++)
  383. {
  384. if ((dword)(iBlock % bdkVol->blockPerFloor) < bdkVol->flash->firstUsableBlock)
  385. {
  386. iBlock = (word)(iBlock + bdkVol->flash->firstUsableBlock);
  387. if (iBlock > bdkVol->endPartitionBlock)
  388. break;
  389. }
  390. /* check for unit signature */
  391. DEBUG_PRINT(("Debug: getBootAreaInfo() - Reading unit signature...\r\n"));
  392. checkStatus(flash->read(flash,bdkVol->bdkSignOffset +
  393. ((CardAddress)iBlock << bdkVol->erasableBlockBits),
  394. bdkVol->signBuffer,SIGNATURE_LEN,EXTRA));
  395. DEBUG_PRINT(("Debug: getBootAreaInfo() - Signature read done.\r\n"));
  396. if(tffscmp( (void FAR1 *)signature , (void FAR1 *)bdkVol->signBuffer,
  397. BDK_SIGNATURE_NAME ) == 0 )
  398. {
  399. if( numBlock == 0 )
  400. bdkVol->startImageBlock = iBlock;
  401. numBlock++;
  402. bdkVol->endImageBlock = iBlock;
  403. if(( bdkVol->realBootImageSize == 0L ) && (numBlock>startUnit) &&
  404. (tffscmp((void FAR1 *)&bdkVol->signBuffer[BDK_SIGNATURE_NAME],
  405. (void FAR1 *)"FFFF", SIGNATURE_NUM) == 0 ))
  406. {
  407. bdkVol->realBootImageSize =
  408. ((dword)(numBlock - startUnit) << bdkVol->erasableBlockBits);
  409. }
  410. }
  411. }
  412. if (numBlock<=startUnit)
  413. {
  414. DEBUG_PRINT(("Debug: getBootAreaInfo() - No space in volume.\r\n"));
  415. return( flNoSpaceInVolume );
  416. }
  417. bdkVol->bootImageSize = (dword)numBlock << bdkVol->erasableBlockBits;
  418. if( bdkVol->realBootImageSize == 0L ) /* In case the image without FFFF */
  419. bdkVol->realBootImageSize =
  420. ((dword)(numBlock - startUnit) << bdkVol->erasableBlockBits);
  421. tffscpy((void FAR1 *)(bdkVol->signBuffer),(void FAR1 *)signature,
  422. BDK_SIGNATURE_NAME);
  423. bdkVol->bdkGlobalStatus |= BDK_S_INFO_FOUND;
  424. }
  425. return( flOK );
  426. }
  427. #ifndef NO_INFTL_SUPPORT
  428. /*----------------------------------------------------------------------*/
  429. /* b d k R e t r i e v e H e a d e r */
  430. /* */
  431. /* Retreave media header by oring the headers of each floor */
  432. /* */
  433. /* Note: The header of each floor is read to the first half of the */
  434. /* buffer and then ORed to the second half therfore constructing the */
  435. /* real header in the upper half. The data is copied to the first half */
  436. /* while cast back from little endian */
  437. /* */
  438. /* Parameters: */
  439. /* headerBuffer - buffer returning the retieved header. */
  440. /* */
  441. /* Returns: */
  442. /* flOK on success any other value on error */
  443. /*----------------------------------------------------------------------*/
  444. FLStatus bdkRetrieveHeader (dword * headerBuffer)
  445. {
  446. word iUnit;
  447. dword endUnit; /* Might be larger then word */
  448. dword * bbt = (headerBuffer+BDK_HEADER_FIELDS);
  449. FLFlash * flash=bdkVol->flash;
  450. byte floorNo;
  451. FLBoolean flag;
  452. byte index;
  453. FLStatus status;
  454. tffsset(headerBuffer,0,BDK_HEADER_FIELDS * 2 * sizeof(LEmin));
  455. for (endUnit = 0,floorNo = 0 ; floorNo < flash->noOfFloors ; floorNo++)
  456. {
  457. iUnit = (word)(endUnit + flash->firstUsableBlock);
  458. endUnit += bdkVol->blockPerFloor;
  459. for (flag = FALSE;(iUnit<endUnit)&&(flag==FALSE);iUnit++)
  460. {
  461. for (index=0;index<BDK_NO_OF_MEDIA_HEADERS;index++) /* all copies */
  462. {
  463. status = flash->read(flash,((CardAddress)iUnit<<flash->erasableBlockSizeBits)
  464. + index * BDK_HEADERS_SPACING,bbt,
  465. BDK_HEADER_FIELDS*sizeof(LEmin),FL_DATA);
  466. if (status != flOK)
  467. {
  468. DEBUG_PRINT(("Debug: ERROR reading original unit header.\r\n"));
  469. return flBadFormat;
  470. }
  471. if (tffscmp(bbt, "BNAND", sizeof("BNAND")) == 0)
  472. {
  473. flag=TRUE;
  474. break;
  475. }
  476. }
  477. }
  478. if (flag == FALSE) /* Header not found in all header copies */
  479. {
  480. DEBUG_PRINT(("Debug: binary partition data could not be found.\r\n"));
  481. return flBadFormat;
  482. }
  483. /* merge with previous headers */
  484. for (index = 0 ; index < BDK_HEADER_FIELDS; index++)
  485. {
  486. headerBuffer[index] |= bbt[index];
  487. }
  488. } /* loop of the floors */
  489. return flOK;
  490. }
  491. #endif /* NO_INFTL_SUPPORT */
  492. /*--------------------------------------------------------------------------*
  493. * b d k M o u n t
  494. *
  495. * Routine that finds the media header unit and initializes it. The header
  496. * supplies the number of binary partition in the DiskOnChip and their
  497. * boundries. If the header is valid the curent partition pointer is set to
  498. * the required partition.
  499. *
  500. * Note : 1) Drive argument is assumed to be O.K and DiskOnChip already found
  501. * 2) If there are no binary partitions O.K will be returned but the
  502. * bdkVol pointer will not be changed.
  503. *
  504. * Parameters : None
  505. *
  506. * global variable input :
  507. * globalSocketNo : DiskOnChip drive number (always 0 for BDK)
  508. * globalPartitionNo : Binary partition number in the DiskOnChip
  509. * bdkGlobalStatus - was this partition accessed before
  510. * bdkVol - current binary partition record
  511. * flash - flash record enabling media I\O
  512. * noOfPartitions - the current amount of partitions
  513. * bdkVols - array of the binary partitions records
  514. *
  515. * global variable output :
  516. * bdkVol - new current binary partition record
  517. * handleTable - partition to record converion table
  518. * bdkGlobalStatus - set to BDK_S_HEADER_FOUND
  519. * erasableBlockBits - number of bits representing a unit
  520. * startPartitionBlock - physical unit number of first unit
  521. * endPartitionBlock - and last units of the partition
  522. * noOfPartitions - increment with the new found partitions
  523. *
  524. * Return:
  525. * flOK - success
  526. * flBadFormat - TL format does not exists
  527. * flFeatureNotSupported - Not a DiskOnChip device.
  528. * flNoSpaceInVolume - No binary partition at all
  529. * flBadDriveHandle - No such binary partition on the media or number
  530. * binary partitions exceeds the cusomized limit.
  531. * flDataError - fail in buffer reading codes
  532. * flHWProtect - HW read protection was triggerd
  533. *
  534. * Routine for both OSAK and the BDK stand alone package.
  535. *--------------------------------------------------------------------------*/
  536. static FLStatus bdkMount(void)
  537. {
  538. FLFlash * flash = bdkVol->flash;
  539. dword iBlock;
  540. dword noOfBlocks;
  541. byte blockMultiplierBits;
  542. byte maxPartition = 0;
  543. byte buf1[TL_SIGNATURE];
  544. byte buf2[TL_SIGNATURE];
  545. #ifndef NO_INFTL_SUPPORT
  546. LEmin headerBuffer[BDK_HEADER_FIELDS*2]; /* assume big enough */
  547. VolumeRecord * volume;
  548. #endif /* NO_INFTL_SUPPORT */
  549. if ((bdkVol->bdkGlobalStatus & BDK_S_HEADER_FOUND)==0)/* header not found */
  550. {
  551. /* Find number of bits used to represent erasable block */
  552. bdkVol->erasableBlockBits = flash->erasableBlockSizeBits;
  553. noOfBlocks = (dword)(flash->chipSize * flash->noOfChips)
  554. >> bdkVol->erasableBlockBits;
  555. bdkVol->blockPerFloor = (word)
  556. (flash->chipSize >> flash->erasableBlockSizeBits) *
  557. ((flash->noOfChips + (flash->noOfChips % flash->noOfFloors)) /
  558. flash->noOfFloors);
  559. DEBUG_PRINT(("Debug: searching for TL media header.\r\n"));
  560. #ifndef NO_NFTL_SUPPORT
  561. if (flash->flags & NFTL_ENABLED)
  562. {
  563. tffscpy(buf1,"ANAND",TL_SIGNATURE);
  564. }
  565. #endif /* NO_NFTL_SUPPORT */
  566. #ifndef NO_INFTL_SUPPORT
  567. if (flash->flags & INFTL_ENABLED)
  568. {
  569. tffscpy(buf1,"BNAND",TL_SIGNATURE);
  570. }
  571. #endif /* NO_INFTL_SUPPORT */
  572. if ((flash->flags & (NFTL_ENABLED | INFTL_ENABLED))==0)
  573. {
  574. DEBUG_PRINT(("Debug: Not a DiskOnChip device therfore Binary partitions are not supported.\r\n"));
  575. return( flFeatureNotSupported );
  576. }
  577. /* Find the medium boot record in order to get partition boundries */
  578. for(iBlock=flash->firstUsableBlock;( iBlock < noOfBlocks );iBlock++)
  579. {
  580. checkStatus(flash->read(flash,(CardAddress)iBlock << bdkVol->erasableBlockBits,
  581. buf2,TL_SIGNATURE,0));
  582. if(tffscmp((void FAR1 *)buf2,(void FAR1 *)buf1,TL_SIGNATURE) == 0 )
  583. {
  584. break;
  585. }
  586. }
  587. if (iBlock==noOfBlocks)
  588. {
  589. DEBUG_PRINT(("Debug: TL format does not exists.\r\n"));
  590. return( flBadFormat );
  591. }
  592. /* Analize The media header */
  593. #ifndef NO_NFTL_SUPPORT
  594. if (flash->flags & NFTL_ENABLED) /* NFTL - only a single partition */
  595. {
  596. DEBUG_PRINT(("NFTL media header encounterd.\r\n"));
  597. bdkVol->startPartitionBlock = 0;
  598. bdkVol->endPartitionBlock = (word)((iBlock) ? iBlock-1 : 0);
  599. maxPartition = 1;
  600. }
  601. #endif /* NO_NFTL_SUPPORT */
  602. #ifndef NO_INFTL_SUPPORT
  603. if (flash->flags & INFTL_ENABLED) /* INFTL - parse media header */
  604. {
  605. DEBUG_PRINT(("INFTL media header encounterd.\r\n"));
  606. maxPartition = 0;
  607. checkStatus(bdkRetrieveHeader((dword *)headerBuffer));
  608. volume = (VolumeRecord *) (headerBuffer+BDK_FIELDS_BEFORE_HEADER);
  609. if (((LE4(volume->flags) & BDK_BINARY_FLAG)==0) && (globalPartitionNo == 0))
  610. {
  611. DEBUG_PRINT(("Device is not formated with a binary partition.\r\n"));
  612. return flNoSpaceInVolume;
  613. }
  614. blockMultiplierBits = (byte)LE4(headerBuffer[MULTIPLIER_OFFSET]);
  615. bdkVol = &bdkVols[noOfPartitions];
  616. for (;(BINARY_PARTITIONS >= maxPartition + noOfPartitions) &&
  617. (LE4(volume->flags) & BDK_BINARY_FLAG) ; bdkVol++,volume++)
  618. {
  619. maxPartition++;
  620. bdkVol->startPartitionBlock = (word)LE4(volume->firstUnit) << blockMultiplierBits;
  621. bdkVol->endPartitionBlock = (word)((LE4(volume->lastUnit)+1) << blockMultiplierBits) - 1;
  622. bdkVol->flash = bdkVols[noOfPartitions].flash;
  623. #ifdef PROTECT_BDK_IMAGE
  624. bdkVol->protectionArea = (byte)LE4(volume->protectionArea);
  625. bdkVol->protectionType = (word)LE4(volume->flags);
  626. #endif /* PROTECT_BDK_IMAGE */
  627. bdkVol->blockPerFloor = bdkVols[noOfPartitions].blockPerFloor;
  628. bdkVol->erasableBlockBits = bdkVols[noOfPartitions].erasableBlockBits;
  629. }
  630. }
  631. #endif /* NO_INFTL_SUPPORT */
  632. /* Initialize the partitions that had been found */
  633. for (blockMultiplierBits=0;blockMultiplierBits<maxPartition;
  634. blockMultiplierBits++) /* reused blockMultiplierBits as counter */
  635. {
  636. bdkVol = &bdkVols[noOfPartitions];
  637. handleTable[globalSocketNo][blockMultiplierBits] = noOfPartitions++;
  638. bdkVol->bdkGlobalStatus = BDK_S_DOC_FOUND |
  639. BDK_S_HEADER_FOUND;
  640. }
  641. }
  642. /* set the current partition by changing the bdkVol global pointer */
  643. if ((globalPartitionNo>MAX_BINARY_PARTITIONS_PER_DRIVE) ||
  644. (handleTable[globalSocketNo][globalPartitionNo] == BDK_INVALID_VOLUME_HANDLE))
  645. {
  646. DEBUG_PRINT(("Device is not formated the specified binary partition.\r\n"));
  647. return flBadDriveHandle;
  648. }
  649. else
  650. {
  651. bdkVol = &bdkVols[handleTable[globalSocketNo][globalPartitionNo]];
  652. }
  653. return flOK;
  654. }
  655. #ifdef MTD_STANDALONE
  656. /*-------------------------------------------------------------------
  657. * b d k E x i t
  658. *
  659. * Reset BDK variables and Free 'bdkWin' memory.
  660. *
  661. * Parameters: None
  662. *
  663. * Return: Nothing
  664. *-------------------------------------------------------------------*/
  665. void bdkExit( void )
  666. {
  667. if( bdkVol->bdkGlobalStatus & BDK_S_DOC_FOUND ) /* DiskOnChip was found */
  668. {
  669. freePointer(bdkWin,DOC_WIN);
  670. }
  671. bdkInit();
  672. }
  673. /*-------------------------------------------------------------------
  674. * b d k S e t D o c W i n d o w
  675. *
  676. * Set DiskOnChip window explicitly
  677. *
  678. * Note : This routine should not be used after a DiskOnChip was already
  679. * found since it does not initialize the Global binary variables.
  680. * In order to switch between DiskOnChip call bdkexists before
  681. * calling this routine.
  682. *
  683. * Parameters : 'docWindow' - DiskOnChip physical address
  684. *
  685. * global variable input :
  686. * bdkVol - current binary partition record
  687. *
  688. * global variable output :
  689. * bdkDocWindow - initialized with the given address
  690. *
  691. * Return: Nothing
  692. *
  693. * Routine for BDK stand alone package.
  694. *-------------------------------------------------------------------*/
  695. void bdkSetDocWindow( dword docWindow )
  696. {
  697. bdkVol->bdkDocWindow = docWindow;
  698. }
  699. /*--------------------------------------------------------------------------
  700. * b d k F i n d D i s k O n C h i p
  701. *
  702. * Find DiskOnChip in the specified memory range and identify the flash
  703. * media (initialize flash). Update 'docSize' and 'docAddress' according
  704. * to the DiskOnChip size and address respectively.
  705. *
  706. * Note : If the DiskOnChip location is known, specify the same address
  707. * for DOC_LOW_ADDRESS and DOC_HIGH_ADDRESS.
  708. *
  709. * Parameters : 'docAddress' - pointer to the DiskOnChip address
  710. * 'docSize' - pointer to the returned size
  711. *
  712. * global variable input :
  713. * bdkGlobalStatus - was this device found before
  714. * bdkVol - current binary partition record
  715. * bdkDocWindow - if > 0 only this address will be checked
  716. *
  717. * global variable output :
  718. * flash - flash record enabling media I\O
  719. * bdkGlobalStatus - set to BDK_S_DOC_FOUND
  720. * bdkDocWindow - save physical window address
  721. *
  722. * Return:
  723. * flOK - success
  724. * flDriveNotAvailable - DiskOnChip ASIC was not found
  725. * flUnknownMedia - failed in Flash chips recognition
  726. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  727. * protection violation. This device is unusable.
  728. *
  729. * Routine for BDK stand alone package.
  730. *------------------------------------------------------------------------*/
  731. FLStatus bdkFindDiskOnChip(dword FAR2 *docAddress, dword FAR2 *docSize )
  732. {
  733. FLStatus status;
  734. byte mtdIndex;
  735. dword blockSize;
  736. FLSocket *socket = flSocketOf(0);
  737. FLFlash *flash = flFlashOf(0);
  738. if(globalInitStatus==FALSE)
  739. bdkInit();
  740. if ((bdkVol->bdkGlobalStatus & BDK_S_DOC_FOUND) == 0) /* initialize MTD */
  741. {
  742. bdkVol = bdkVols;
  743. bdkVol->flash = flash;
  744. #ifndef NO_DOC2000_FAMILY_SUPPORT
  745. flRegisterDOC2000();
  746. #endif
  747. #ifndef NO_DOCPLUS_FAMILY_SUPPORT
  748. flRegisterDOCPLUS();
  749. #endif
  750. /* Search for ASIC in the given memory boundries */
  751. for(mtdIndex=0;mtdIndex < noOfMTDs;mtdIndex++)
  752. {
  753. if( bdkVol->bdkDocWindow > 0 ) /* Set range explicitely */
  754. {
  755. status = socketTable[mtdIndex](socket,
  756. bdkVol->bdkDocWindow,bdkVol->bdkDocWindow);
  757. }
  758. else
  759. {
  760. status = socketTable[mtdIndex](socket,
  761. DOC_LOW_ADDRESS,DOC_HIGH_ADDRESS);
  762. }
  763. if (status == flOK)
  764. {
  765. /* Identify flash connected to the ASIC */
  766. bdkVol->bdkDocWindow = pointerToPhysical(socket->base);
  767. flash->socket = socket;
  768. checkStatus(mtdTable[mtdIndex](flash));
  769. break;
  770. }
  771. }
  772. if (status != flOK)
  773. return status;
  774. bdkVol->bdkGlobalStatus |= BDK_S_DOC_FOUND;
  775. /* Calculate erasable Block Size Bits */
  776. for(blockSize = flash->erasableBlockSize>>1,flash->erasableBlockSizeBits = 0;
  777. blockSize>0; flash->erasableBlockSizeBits++,blockSize = blockSize >> 1);
  778. }
  779. *docAddress = pointerToPhysical(socket->base);
  780. *docSize = flash->chipSize * flash->noOfChips;
  781. return flOK;
  782. }
  783. /*--------------------------------------------------------------------------
  784. * b d k S e t B o o t P a r t i t i o n N o
  785. *
  786. * Set current binary partiton pointer to a specific binary partition
  787. *
  788. * Note : This routine is neccesay only for partitions > 0 .
  789. * This routine can replace the bdkFindDiskOnChip call.
  790. *
  791. * Parameters : 'partitionNo' - serial number of binary partition
  792. *
  793. * global variable output :
  794. * globalPartitionNo - changed to the current partition number.
  795. *
  796. * Return:
  797. * flOK on success
  798. * flBadParameter if BDK is not customized to support that many partitions.
  799. *
  800. * Routine for BDK stand alone package only.
  801. *------------------------------------------------------------------------*/
  802. FLStatus bdkSetBootPartitionNo(byte partitionNo)
  803. {
  804. if (partitionNo < TFFSMIN(BINARY_PARTITIONS,MAX_BINARY_PARTITIONS_PER_DRIVE))
  805. {
  806. globalPartitionNo = partitionNo;
  807. return flOK;
  808. }
  809. else
  810. {
  811. DEBUG_PRINT(("BDK is not customized to support the specified partition number.\r\n"));
  812. return flBadParameter;
  813. }
  814. }
  815. /*-------------------------------------------------------------------
  816. * b d k C h e c k S i g n O f f s e t
  817. *
  818. * Set BDK signature offset
  819. *
  820. * Note : Offset 0 does not support the use of EDC\ECC mechanizm and
  821. * therfore it reconmended not to be used.
  822. * The new found offset is stored in the bdkSignOffset field of the
  823. * partitions global record.
  824. *
  825. * Parameters : 'signature' - 4-character signature of storage units
  826. *
  827. * global variable input :
  828. * bdkGlobalStatus - was this partition accessed before
  829. * bdkVol - current binary partition record
  830. * bdkDocWindow - DiskOnChip window physical address
  831. * (used to narrow search for DiskOnChip)
  832. *
  833. * global variable output :
  834. * bdkSignOffset - the offset of the signature (either 0 or 8)
  835. *
  836. * bdkSavedStartUnit - save start unit for next access
  837. * bdkSavedSignOffset - save current signatur for next access
  838. * flash - flash record enabling media I\O
  839. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  840. * startPartitionBlock - low physical boundry of the partition
  841. * endPartitionBlock - high physical boundry of the partition
  842. * signBuffer - initialize with given signature
  843. * bootImageSize - size of the entire sub partition in bytes
  844. * realBootImageSize - number of units writen in the sub partition
  845. * startImageBlock - physical unit number of the first and
  846. * endImageBlock last units marked with the signature
  847. *
  848. * Return :
  849. * flOK - success
  850. * flDriveNotAvailable - DiskOnChip ASIC was not found
  851. * flUnknownMedia - failed in Flash chips recognition
  852. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  853. * protection violation. This device is unusable.
  854. * flBadFormat - TL format does not exists
  855. * flNoSpaceInVolume - there are 0 units marked with this signature
  856. * flDataError - MTD read fault.
  857. * flHWProtect - HW protection was triggerd
  858. *
  859. * Routine for BDK stand alone package only.
  860. *-------------------------------------------------------------------*/
  861. FLStatus bdkCheckSignOffset( byte FAR2 *signature )
  862. {
  863. FLStatus status;
  864. bdkVol->bdkSignOffset = 0; /* Look for signature units with offset 0 */
  865. status = getBootAreaInfo( 0 , signature );
  866. if (status!=flOK)
  867. {
  868. bdkVol->bdkSignOffset = BDK_SIGN_OFFSET; /* Now try with offset 8 */
  869. checkStatus(getBootAreaInfo( 0 , signature ));
  870. }
  871. return( flOK );
  872. }
  873. /*-------------------------------------------------------------------
  874. * b d k C o p y B o o t A r e a
  875. *
  876. * Copy the BDK Image from the DiskOnChip to a RAM area starting at
  877. * 'startAddress', with a size of 'areaLen' bytes.
  878. *
  879. * Note : Blocks in the DiskOnChip are marked with a 4-byteacter signature
  880. * followed by a 4-digit hexadecimal number.
  881. *
  882. * This routine simple calls bdkCopyBootAreaInit and loops over
  883. * bdkCopyBootAreaBlock.
  884. *
  885. * Parameters: 'startAddress' - pointer to the beginning of the RAM area
  886. * 'startUnit' - start block in image for reading
  887. * 'areaLen' - BDK image size
  888. * 'checkSum' - pointer to the checksum modulo 0x100
  889. * 'signature' - 4-byteacter signature of storage units
  890. *
  891. * global variable input :
  892. * bdkVol - current binary partition record
  893. * bdkGlobalStatus - partition predsent status.
  894. * bdkSavedStartUnit - start unit of previous access
  895. * bdkSavedSignOffset - signature offset of previous access
  896. * bdkSignOffset - current signature offset
  897. * bdkDocWindow - explicitly sets the windows address
  898. *
  899. * global variable output :
  900. * flash - flash record enabling media I\O
  901. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  902. * signBuffer - initialize with given signature
  903. * bdkSavedStartUnit - save start unit for next access
  904. * bdkSavedSignOffset - save current signature for next access
  905. * startPartitionBlock - low physical boundry of the partition
  906. * endPartitionBlock - high physical boundry of the partition
  907. * startImageBlock - physical unit number of the first and
  908. * endImageBlock last units marked with the signature
  909. * bootImageSize - size of the entire sub partition in bytes
  910. * realBootImageSize - number of units writen in the sub partition
  911. *
  912. * Return :
  913. * flOK - success
  914. * flDriveNotAvailable - DiskOnChip ASIC was not found
  915. * flUnknownMedia - failed in Flash chips recognition
  916. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  917. * protection violation. This device is unusable.
  918. * flBadFormat - TL format does not exists
  919. * flNoSpaceInVolume - there are 0 units marked with this signature
  920. * flDataError - MTD dependent.
  921. * flHWProtect - HW read protection was triggerd
  922. *
  923. * Routine for BDK stand alone package only.
  924. *-------------------------------------------------------------------*/
  925. FLStatus bdkCopyBootArea( byte FAR1 *startAddress,word startUnit,dword areaLen,
  926. byte FAR2 *checkSum, byte FAR2 *signature )
  927. {
  928. dword curLen;
  929. word copyLen;
  930. checkStatus(bdkCopyBootAreaInit( startUnit, areaLen, signature ));
  931. DEBUG_PRINT(("Debug: bdkCopyBootArea() - bdkCopyBootAreaInit was done succefully.\r\n"));
  932. #ifdef BDK_CHECK_SUM
  933. if (checkSum!=NULL)
  934. *checkSum=0;
  935. #endif /* BDK_CHECK_SUM */
  936. for(curLen=0L;( curLen < areaLen );curLen+=bdkVol->flash->erasableBlockSize)
  937. {
  938. copyLen = (word)BDK_MIN((areaLen - curLen), bdkVol->flash->erasableBlockSize);
  939. checkStatus(bdkCopyBootAreaBlock( (byte FAR1 *)startAddress, copyLen, checkSum ));
  940. startAddress = (byte FAR1 *)addToFarPointer( startAddress, copyLen);
  941. }
  942. return flOK;
  943. }
  944. #endif /* MTD_STANDALONE */
  945. /*-------------------------------------------------------------------
  946. * b d k G e t B o o t P a r t i t i o n I n f o
  947. *
  948. * Get DiskOnChip binary sub partition Information.
  949. *
  950. * Note : Blocks in the DiskOnChip are marked with a 4-byteacter signature
  951. * followed by a 4-digit hexadecimal number.
  952. *
  953. * Parameters: 'startUnit' - start Unit for Actual sub Partition Size
  954. * 'partitionSize' - pointer to return sub Partition Size parameter
  955. * 'realPartitionSize' - pointer to return Actual sub Partition Size
  956. * 'unitSize' - pointer to return Unit Size parameter
  957. * 'signature' - 4-byteacter signature of storage units
  958. *
  959. * global variable input :
  960. * bdkVol - current binary partition record
  961. * bdkGlobalStatus - partition predsent status.
  962. * bdkSavedStartUnit - start unit of previous access
  963. * bdkSavedSignOffset - signature offset of previous access
  964. * bdkSignOffset - current signature offset
  965. * bdkDocWindow - explicitly sets the windows address
  966. *
  967. * global variable output :
  968. * flash - flash record enabling media I\O
  969. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  970. * signBuffer - initialize with given signature
  971. * bdkSavedStartUnit - save start unit for next access
  972. * bdkSavedSignOffset - save current signature for next access
  973. * startPartitionBlock - low physical boundry of the partition
  974. * endPartitionBlock - high physical boundry of the partition
  975. * startImageBlock - physical unit number of the first and
  976. * endImageBlock last units marked with the signature
  977. * bootImageSize - size of the entire sub partition in bytes
  978. * realBootImageSize - number of units writen in the sub partition
  979. *
  980. * Return :
  981. * flOK - success
  982. * flDriveNotAvailable - DiskOnChip ASIC was not found
  983. * flUnknownMedia - failed in Flash chips recognition
  984. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  985. * protection violation. This device is unusable.
  986. * flBadFormat - TL format does not exists
  987. * flNoSpaceInVolume - there are 0 units marked with this signature
  988. * flDataError - MTD dependent.
  989. * flHWProtect - HW read protection was triggerd
  990. *
  991. * Routine for BDK stand alone package only.*
  992. *-------------------------------------------------------------------*/
  993. FLStatus bdkGetBootPartitionInfo( word startUnit, dword FAR2 *partitionSize,
  994. dword FAR2 *realPartitionSize,
  995. dword FAR2 *unitSize, byte FAR2 *signature)
  996. {
  997. FLStatus st = flOK;
  998. *partitionSize = 0L;
  999. *unitSize = 0L;
  1000. st = getBootAreaInfo( startUnit , signature );
  1001. *partitionSize = bdkVol->bootImageSize;
  1002. *realPartitionSize = bdkVol->realBootImageSize;
  1003. *realPartitionSize -= ((startUnit - bdkVol->bdkSavedStartUnit) << bdkVol->erasableBlockBits);
  1004. *unitSize = bdkVol->flash->erasableBlockSize;
  1005. return( st );
  1006. }
  1007. /*-------------------------------------------------------------------
  1008. * b d k C o p y B o o t A r e a I n i t
  1009. *
  1010. * Initialize read operations on the DiskOnChip starting at 'startUnit', with
  1011. * a size of 'areaLen' bytes and 'signature'.
  1012. *
  1013. * Note : Blocks in the DiskOnChip are marked with a 4-byteacter signature
  1014. * followed by a 4-digit hexadecimal number.
  1015. *
  1016. * Parameters: 'startUnit' - start block in image for reading
  1017. * 'areaLen' - BDK image size
  1018. * 'signature' - 4-byteacter signature of storage units
  1019. *
  1020. * global variable input :
  1021. * bdkVol - current binary partition record
  1022. * flash - flash record enabling media I\O
  1023. * bdkGlobalStatus - was this partition accessed before
  1024. * bdkSavedSignBuffer - signature offset of previous access
  1025. * bdkSignOffset - current signature offset
  1026. *
  1027. * global variable output :
  1028. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  1029. * signBuffer - initialize with given signature
  1030. * bdkSavedSignBuffer - save current signatur for next access
  1031. * bootImageSize - size of the entire sub partition in bytes
  1032. * realBootImageSize - number of units writen in the sub partition
  1033. * startPartitionBlock - low physical boundry of the partition
  1034. * endPartitionBlock - high physical boundry of the partition
  1035. * startImageBlock - physical unit number of the first and
  1036. * endImageBlock last units marked with the signature
  1037. * curReadImageAddress - current address to read from
  1038. * actualReadLen - length left to read
  1039. *
  1040. * Return :
  1041. * flOK - success
  1042. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1043. * flUnknownMedia - failed in Flash chips recognition
  1044. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1045. * protection violation. This device is unusable.
  1046. * flBadFormat - TL format does not exists
  1047. * flNoSpaceInVolume - there are 0 units marked with this signature
  1048. * flDataError - MTD read fault.
  1049. * flHWProtect - HW read protection was triggerd
  1050. *
  1051. * Routine for both OSAK and the BDK stand alone package.
  1052. *-------------------------------------------------------------------*/
  1053. FLStatus bdkCopyBootAreaInit(word startUnit ,
  1054. dword areaLen ,
  1055. byte FAR2 *signature)
  1056. {
  1057. dword realSize; /* remaining real size from start unit */
  1058. checkStatus(getBootAreaInfo( startUnit , signature ));
  1059. realSize = bdkVol->realBootImageSize;
  1060. realSize -= (startUnit - bdkVol->bdkSavedStartUnit) << bdkVol->erasableBlockBits;
  1061. if (areaLen>realSize)
  1062. {
  1063. DEBUG_PRINT(("got out of the partition.\r\n"));
  1064. return( flNoSpaceInVolume );
  1065. }
  1066. bdkVol->curReadImageBlock = startUnit;
  1067. bdkVol->actualReadLen = areaLen;
  1068. bdkVol->curReadImageAddress = getPhysAddressOfUnit( startUnit );
  1069. return( flOK );
  1070. }
  1071. /*-------------------------------------------------------------------
  1072. * bdkCopyBootAreaBlock - Read to 'buffer' from the DiskOnChip BDK Image area.
  1073. *
  1074. * Note : Before the first use of this function 'bdkCopyBootAreaInit'
  1075. * must be called
  1076. *
  1077. * Parameters: 'buf' - buffer to read into
  1078. * 'bufferLen' - buffer length in bytes
  1079. * 'checkSum' - pointer to the checksum modulo 0x100
  1080. *
  1081. * global variable input :
  1082. * bdkVol - current binary partition record
  1083. * flash - flash record enabling media I\O
  1084. * signBuffer - sub partition signature
  1085. * bdkSignOffset - current signature offset
  1086. * curReadImageAddress - current address to read from
  1087. * actualReadLen - length left to read
  1088. * endImageBlock - last signatured block in sub partition
  1089. * erasableBlockBits - no' of bits used to represent a block
  1090. *
  1091. * global variable output :
  1092. * curReadImageAddress - updated address to read from
  1093. * actualReadLen - updated length left to read
  1094. *
  1095. * Return :
  1096. * flOK - success
  1097. * flBadLength - required length will cause crossing unit boundry
  1098. * flNoSpaceInVolume - no more signatured units found
  1099. * flDataError - MTD read fault.
  1100. * flHWProtect - HW read protection was triggerd
  1101. *
  1102. * Routine for both OSAK and the BDK stand alone package.
  1103. *-------------------------------------------------------------------*/
  1104. FLStatus bdkCopyBootAreaBlock( byte FAR1 *buf, word bufferLen, byte FAR2 *checkSum )
  1105. {
  1106. word iBlock, readLen;
  1107. byte modes;
  1108. byte signRead[BDK_SIGNATURE_NAME];
  1109. FLFlash * flash = bdkVol->flash;
  1110. FLStatus status;
  1111. if( (bufferLen > flash->erasableBlockSize ) ||
  1112. (bufferLen > bdkVol->actualReadLen))
  1113. return( flBadLength );
  1114. /* find next good unit */
  1115. if( (bdkVol->curReadImageAddress & (flash->erasableBlockSize-1)) == 0 )
  1116. {
  1117. iBlock = (word)( bdkVol->curReadImageAddress >> bdkVol->erasableBlockBits );
  1118. for(;( iBlock <= bdkVol->endImageBlock ); iBlock++)
  1119. {
  1120. if ((dword)(iBlock % bdkVol->blockPerFloor) < bdkVol->flash->firstUsableBlock)
  1121. {
  1122. iBlock = (word)(iBlock + bdkVol->flash->firstUsableBlock);
  1123. if (iBlock > bdkVol->endImageBlock)
  1124. break;
  1125. }
  1126. status = flash->read (flash , bdkVol->bdkSignOffset +
  1127. ((CardAddress)iBlock << bdkVol->erasableBlockBits) ,
  1128. (byte FAR1 *)signRead, BDK_SIGNATURE_NAME,EXTRA);
  1129. if(status!=flOK)
  1130. {
  1131. DEBUG_PRINT(("(EXTRA)read failed.\r\n"));
  1132. return status;
  1133. }
  1134. if( tffscmp((void FAR1 *)signRead, (void FAR1 *)(bdkVol->signBuffer),
  1135. BDK_SIGNATURE_NAME ) == 0 )
  1136. break;
  1137. }
  1138. if( iBlock > bdkVol->endImageBlock )
  1139. return( flNoSpaceInVolume ); /* Finish (last block) */
  1140. bdkVol->curReadImageAddress = (CardAddress )iBlock << bdkVol->erasableBlockBits;
  1141. bdkVol->curReadImageBlock++;
  1142. }
  1143. /* read data */
  1144. readLen = (word)BDK_MIN(bdkVol->actualReadLen, (dword)bufferLen);
  1145. if((bdkVol->bdkEDC)&&(bdkVol->bdkSignOffset!=0))
  1146. {
  1147. modes=EDC;
  1148. }
  1149. else
  1150. {
  1151. modes=0;
  1152. }
  1153. status = (flash->read(flash, bdkVol->curReadImageAddress, buf, readLen, modes));
  1154. if(status!=flOK)
  1155. {
  1156. DEBUG_PRINT(("(EXTRA)read failed.\r\n"));
  1157. return status;
  1158. }
  1159. bdkVol->curReadImageAddress += (CardAddress )readLen;
  1160. bdkVol->actualReadLen -= (dword)readLen;
  1161. #ifdef BDK_CHECK_SUM
  1162. if (checkSum!=NULL)
  1163. {
  1164. while (readLen>0)
  1165. {
  1166. readLen--;
  1167. *checkSum+=buf[readLen];
  1168. }
  1169. }
  1170. #endif /* BDK_CHECK_SUM */
  1171. return( flOK );
  1172. }
  1173. #ifdef UPDATE_BDK_IMAGE
  1174. /*-------------------------------------------------------------------
  1175. * b d k U p d a t e B o o t A r e a I n i t
  1176. *
  1177. * Initialize update operations on the DiskOnChip starting at 'startUnit',
  1178. * with a size of 'areaLen' bytes and 'signature'.
  1179. *
  1180. * Note : Blocks in the DiskOnChip are marked with a 4-byteacter signature
  1181. * followed by a 4-digit hexadecimal number.
  1182. *
  1183. * Parameters: 'startUnit' - start unit in image for updating
  1184. * 'areaLen' - BDK image size
  1185. * 'updateFlag' - update whole image or part of it
  1186. * BDK_COMPLETE_IMAGE_UPDATE
  1187. * 'signature' - 4-byteacter signature of storage units
  1188. *
  1189. * global variable input :
  1190. * bdkVol - current binary partition record
  1191. * flash - flash record enabling media I\O
  1192. * bdkGlobalStatus - was this partition accessed before
  1193. * bdkSavedSignBuffer - signature offset of previous access
  1194. * bdkSignOffset - current signature offset
  1195. *
  1196. * global variable output :
  1197. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  1198. * signBuffer - initialize with given signature
  1199. * bdkSavedSignBuffer - save current signatur for next access
  1200. * bootImageSize - size of the entire sub partition in bytes
  1201. * realBootImageSize - number of units writen in the sub partition
  1202. * startPartitionBlock - low physical boundry of the partition
  1203. * endPartitionBlock - high physical boundry of the partition
  1204. * startImageBlock - physical unit number of the first and
  1205. * endImageBlock last units marked with the signature
  1206. * actualUpdateLen - length left to write
  1207. * curUpadateImageAddress - current address to write to
  1208. * Return :
  1209. * flOK - success
  1210. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1211. * flUnknownMedia - failed in Flash chips recognition
  1212. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1213. * protection violation. This device is unusable.
  1214. * flBadFormat - TL format does not exists
  1215. * flNoSpaceInVolume - there are 0 units marked with this signature
  1216. * flDataError - MTD read fault.
  1217. * flHWProtect - HW read protection was triggerd
  1218. *
  1219. * Routine for both OSAK and the BDK stand alone package.
  1220. *-------------------------------------------------------------------*/
  1221. FLStatus bdkUpdateBootAreaInit( word startUnit, dword areaLen,
  1222. byte updateFlag, byte FAR2 *signature )
  1223. {
  1224. word imageBlockSize;
  1225. checkStatus(getBootAreaInfo( startUnit , signature ));
  1226. imageBlockSize = (word)( bdkVol->bootImageSize >> bdkVol->erasableBlockBits);
  1227. if( ((dword)(imageBlockSize-startUnit) << bdkVol->erasableBlockBits) < areaLen )
  1228. return( flNoSpaceInVolume );
  1229. bdkVol->actualUpdateLen = areaLen;
  1230. bdkVol->curUpdateImageBlock = startUnit;
  1231. bdkVol->updateImageFlag = updateFlag;
  1232. bdkVol->curUpdateImageAddress = getPhysAddressOfUnit( startUnit );
  1233. bdkVol->bdkGlobalStatus &= ~BDK_S_INFO_FOUND;
  1234. return( flOK );
  1235. }
  1236. /*-------------------------------------------------------------------
  1237. * b d k U p d a t e B o o t A r e a B l o c k
  1238. *
  1239. * Write 'buffer' to the DiskOnChip BDK Image area.
  1240. *
  1241. * Note : Before the first use of this function 'bdkUpdateBootAreaInit'
  1242. * must be called
  1243. *
  1244. * Parameters: 'buf' - BDK image buffer
  1245. * 'bufferLen' - buffer length in bytes
  1246. *
  1247. * global variable input :
  1248. * bdkVol - current binary partition record
  1249. * flash - flash record enabling media I\O
  1250. * signBuffer - sub partition signature
  1251. * bdkSignOffset - current signature offset
  1252. * curUpdateImageAddress - current address to read from
  1253. * actualUpdateLen - length left to read
  1254. * endImageBlock - last signatured block in sub partition
  1255. * erasableBlockBits - no' of bits used to represent a block
  1256. *
  1257. * global variable output :
  1258. * curUpdateImageAddress - updated address to read from
  1259. * actualUpdateLen - updated length left to read
  1260. *
  1261. * Return :
  1262. * flOK - success
  1263. * flBadLength - required length will cause crossing unit boundry
  1264. * flNoSpaceInVolume - no more signatured units found
  1265. * flDataError - MTD read fault
  1266. * flHWProtect - HW protection was triggerd
  1267. * flWriteFault - MTD write fault
  1268. *
  1269. * Routine for both OSAK and the BDK stand alone package.
  1270. *-------------------------------------------------------------------*/
  1271. FLStatus bdkUpdateBootAreaBlock( byte FAR1 *buf, word bufferLen )
  1272. {
  1273. word iBlock, writeLen, i0, j;
  1274. FLStatus status;
  1275. FLFlash* flash = bdkVol->flash;
  1276. byte modes;
  1277. byte signRead[SIGNATURE_LEN];
  1278. if( (bufferLen > flash->erasableBlockSize) ||
  1279. (bufferLen > bdkVol->actualUpdateLen))
  1280. return( flBadLength );
  1281. /* find next good unit and prepare it for work */
  1282. if( (bdkVol->curUpdateImageAddress & (flash->erasableBlockSize-1)) == 0 )
  1283. {
  1284. /* find next signatured unit */
  1285. iBlock = (word)( bdkVol->curUpdateImageAddress >> bdkVol->erasableBlockBits );
  1286. for(;( iBlock <= bdkVol->endImageBlock ); iBlock++)
  1287. {
  1288. if ((dword)(iBlock % bdkVol->blockPerFloor) < bdkVol->flash->firstUsableBlock)
  1289. {
  1290. iBlock = (word)(iBlock + bdkVol->flash->firstUsableBlock);
  1291. if (iBlock > bdkVol->endImageBlock)
  1292. break;
  1293. }
  1294. status = flash->read(flash , bdkVol->bdkSignOffset +
  1295. ((CardAddress)iBlock << bdkVol->erasableBlockBits),
  1296. (byte FAR1 *)signRead, BDK_SIGNATURE_NAME ,EXTRA);
  1297. if(status!=flOK)
  1298. {
  1299. DEBUG_PRINT(("(EXTRA)read failed.\r\n"));
  1300. return status;
  1301. }
  1302. if( tffscmp( (void FAR1 *)signRead, (void FAR1 *)(bdkVol->signBuffer),
  1303. BDK_SIGNATURE_NAME ) == 0 )
  1304. break;
  1305. }
  1306. if( iBlock > bdkVol->endImageBlock )
  1307. return( flNoSpaceInVolume );
  1308. /* Erase the newly found unit */
  1309. #ifdef BDK_ACCESS
  1310. if(bdkVol->updateImageFlag & ERASE_BEFORE_WRITE)
  1311. #endif
  1312. checkStatus(flash->erase (flash, iBlock, 1 ));
  1313. /* Update signature number */
  1314. if( (bdkVol->actualUpdateLen <= flash->erasableBlockSize) &&
  1315. (bdkVol->updateImageFlag & BDK_COMPLETE_IMAGE_UPDATE) )
  1316. {
  1317. /* Last block FFFF */
  1318. tffscpy( (void FAR1 *)&bdkVol->signBuffer[BDK_SIGNATURE_NAME],
  1319. (void FAR1 *)"FFFF", SIGNATURE_NUM );
  1320. }
  1321. else
  1322. {
  1323. for(i0=bdkVol->curUpdateImageBlock,j=SIGNATURE_LEN;(j>BDK_SIGNATURE_NAME);j--)
  1324. {
  1325. bdkVol->signBuffer[j-1] = (i0 % 10) + '0';
  1326. i0 /= 10;
  1327. }
  1328. }
  1329. /* update internal pointers */
  1330. bdkVol->curUpdateImageAddress = (CardAddress )iBlock << bdkVol->erasableBlockBits;
  1331. bdkVol->curUpdateImageBlock++;
  1332. /* Mark new block */
  1333. status = flash->write(flash,
  1334. (bdkVol->bdkSignOffset + bdkVol->curUpdateImageAddress),
  1335. (byte FAR1 *)bdkVol->signBuffer, SIGNATURE_LEN, EXTRA);
  1336. #ifdef VERIFY_WRITE
  1337. if (status == flWriteFault) /* Check if failed due to erase operation */
  1338. {
  1339. /* Check signature is written properly */
  1340. flash->read(flash , (bdkVol->bdkSignOffset + bdkVol->curUpdateImageAddress),
  1341. (byte FAR1 *)signRead, SIGNATURE_LEN ,EXTRA);
  1342. if(tffscmp(signRead,bdkVol->signBuffer,BDK_SIGNATURE_NAME))
  1343. {
  1344. return flWriteFault;
  1345. }
  1346. /* Check unit number is not "FFFF" */
  1347. if(tffscmp(signRead+BDK_SIGNATURE_NAME,"FFFF",BDK_SIGNATURE_NAME))
  1348. {
  1349. dword tmp=0;
  1350. checkStatus(flash->write(flash,(bdkVol->bdkSignOffset +
  1351. bdkVol->curUpdateImageAddress), &tmp, sizeof (dword), EXTRA));
  1352. }
  1353. }
  1354. #endif /* VERIFY_WRITE */
  1355. }
  1356. /* Write the data to the flash and update internal pointers */
  1357. writeLen = (word)BDK_MIN(bdkVol->actualUpdateLen, (dword)bufferLen);
  1358. if((bdkVol->bdkEDC)&&(bdkVol->bdkSignOffset!=0))
  1359. {
  1360. modes=EDC;
  1361. }
  1362. else
  1363. {
  1364. modes=0;
  1365. }
  1366. checkStatus(flash->write(flash,bdkVol->curUpdateImageAddress,buf,writeLen,modes));
  1367. bdkVol->curUpdateImageAddress += (CardAddress)writeLen;
  1368. bdkVol->actualUpdateLen -= (dword)writeLen;
  1369. return( flOK );
  1370. }
  1371. #ifdef ERASE_BDK_IMAGE
  1372. /*-------------------------------------------------------------------
  1373. * b d k E r a s e A r e a
  1374. *
  1375. * Erase given number of blockds in the binary sub partition.
  1376. *
  1377. * Parameters: 'startUnit' - start unit in image for updating
  1378. * 'noOfBlocks' - number of blocks to erase
  1379. * 'signature' - 4-byteacter signature of storage units
  1380. *
  1381. * global variable input :
  1382. * bdkVol - current binary partition record
  1383. * flash - flash record enabling media I\O
  1384. * bdkGlobalStatus - was this partition accessed before
  1385. * bdkSavedSignBuffer - signature offset of previous access
  1386. * bdkSignOffset - current signature offset
  1387. *
  1388. * global variable output :
  1389. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  1390. * signBuffer - initialize with given signature
  1391. * bdkSavedSignBuffer - save current signatur for next access
  1392. * bootImageSize - size of the entire sub partition in bytes
  1393. * startPartitionBlock - low physical boundry of the partition
  1394. * endPartitionBlock - high physical boundry of the partition
  1395. * realBootImageSize - number of units writen in the sub partition
  1396. * startImageBlock - physical unit number of the first and
  1397. * endImageBlock last units marked with the signature
  1398. * actualUpdateLen - 0
  1399. * curUpadateImageAddress - address of first block to erase.
  1400. *
  1401. * Return :
  1402. * flOK - success
  1403. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1404. * flUnknownMedia - failed in Flash chips recognition
  1405. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1406. * protection violation. This device is unusable.
  1407. * flBadFormat - TL format does not exists
  1408. * flNoSpaceInVolume - there are 0 units marked with this signature
  1409. * flDataError - MTD read fault.
  1410. * flHWProtect - HW protection was triggerd
  1411. * flWriteFault - MTD write fault
  1412. *
  1413. * Routine for both OSAK and the BDK stand alone package.
  1414. *-------------------------------------------------------------------*/
  1415. FLStatus bdkEraseBootArea(word startUnit, word noOfBlocks, byte FAR2 * signature)
  1416. {
  1417. word iBlock,index;
  1418. FLStatus status;
  1419. FLFlash* flash;
  1420. flash = bdkVol->flash;
  1421. checkStatus(bdkUpdateBootAreaInit(startUnit,
  1422. (dword)noOfBlocks << bdkVol->erasableBlockBits,0,signature));
  1423. tffscpy( (void FAR1 *)&bdkVol->signBuffer, (void FAR1 *) signature, BDK_SIGNATURE_NAME);
  1424. tffscpy( (void FAR1 *)&bdkVol->signBuffer[BDK_SIGNATURE_NAME], /* Last block FFFF */
  1425. (void FAR1 *)"FFFF", SIGNATURE_NUM );
  1426. iBlock = (word)(bdkVol->curUpdateImageAddress >> bdkVol->erasableBlockBits);
  1427. for (index=0;index<noOfBlocks;index++,iBlock++)
  1428. /* find next good unit erase it and rewrite its signature */
  1429. {
  1430. do
  1431. {
  1432. if ((dword)(iBlock % bdkVol->blockPerFloor) < bdkVol->flash->firstUsableBlock)
  1433. {
  1434. /* Skip OTP and DPS if relevant */
  1435. iBlock = (word)(iBlock + bdkVol->flash->firstUsableBlock);
  1436. if (iBlock > bdkVol->endImageBlock)
  1437. return flGeneralFailure;
  1438. }
  1439. status = flash->read(flash , bdkVol->bdkSignOffset +
  1440. ((CardAddress)iBlock << bdkVol->erasableBlockBits),
  1441. (byte FAR1 *)bdkVol->signBuffer, BDK_SIGNATURE_NAME ,EXTRA);
  1442. if(status!=flOK)
  1443. {
  1444. DEBUG_PRINT(("(EXTRA)read failed.\r\n"));
  1445. return status;
  1446. }
  1447. if (tffscmp((void FAR1 *)bdkVol->signBuffer,
  1448. (void FAR1 *)signature, BDK_SIGNATURE_NAME ) == 0)
  1449. {
  1450. break;
  1451. }
  1452. else
  1453. {
  1454. iBlock++;
  1455. }
  1456. if (iBlock > bdkVol->endImageBlock)
  1457. return( flNoSpaceInVolume );
  1458. }while(1);
  1459. checkStatus(flash->erase(flash, iBlock, 1 ));
  1460. checkStatus(flash->write(flash, bdkVol->bdkSignOffset +
  1461. ((CardAddress)iBlock << bdkVol->erasableBlockBits),
  1462. (byte FAR1 *)bdkVol->signBuffer, SIGNATURE_LEN, EXTRA));
  1463. }
  1464. bdkVol->actualUpdateLen = 0L;
  1465. return( flOK );
  1466. }
  1467. #endif /* ERASE_BDK_IMAGE */
  1468. #ifdef CREATE_BDK_IMAGE
  1469. /*-------------------------------------------------------------------
  1470. * b d k C r e a t e B o o t A r e a
  1471. * Init create operations on the DiskOnChip starting at 'startUnit', with
  1472. * a # of 'units' and 'signature'.
  1473. *
  1474. * Note : Blocks in the DiskOnChip are marked with a 4-character signature
  1475. * followed by a 4-digit hexadecimal number.
  1476. *
  1477. * Parameters: 'noOfBlocks' - number of blocks to erase
  1478. * 'oldSign' - 4-byteacter signature of the source units
  1479. * 'newSign' - 4-byteacter signature of the new units
  1480. *
  1481. * global variable input :
  1482. * bdkVol - current binary partition record
  1483. * flash - flash record enabling media I\O
  1484. * bdkGlobalStatus - was this partition accessed before
  1485. * bdkSavedSignBuffer - signature offset of previous access
  1486. * bdkSignOffset - current signature offset
  1487. *
  1488. * global variable output :
  1489. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  1490. * signBuffer - initialize with given signature
  1491. * bdkSavedSignBuffer - save current signatur for next access
  1492. * bootImageSize - size of the entire sub partition in bytes
  1493. * startPartitionBlock - low physical boundry of the partition
  1494. * endPartitionBlock - high physical boundry of the partition
  1495. * realBootImageSize - number of units writen in the sub partition
  1496. * startImageBlock - physical unit number of the first and
  1497. * endImageBlock last units marked with the signature
  1498. * actualUpdateLen - 0
  1499. * curUpadateImageAddress - address of first block to erase.
  1500. *
  1501. * Return :
  1502. * flOK - success
  1503. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1504. * flUnknownMedia - failed in Flash chips recognition
  1505. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1506. * protection violation. This device is unusable.
  1507. * flBadFormat - TL format does not exists
  1508. * flNoSpaceInVolume - there are 0 units marked with this signature
  1509. * flDataError - MTD read fault.
  1510. * flHWProtect - HW protection was triggerd
  1511. * flWriteFault - MTD write fault
  1512. *
  1513. * Routine for both OSAK and the BDK stand alone package.
  1514. *-------------------------------------------------------------------*/
  1515. FLStatus bdkCreateBootArea(word noOfBlocks, byte FAR2 * oldSign,
  1516. byte FAR2 * newSign)
  1517. {
  1518. word iBlock, index;
  1519. byte signRead[BDK_SIGNATURE_NAME];
  1520. FLStatus status;
  1521. FLFlash* flash;
  1522. flash = bdkVol->flash;
  1523. /* FFFF is not a valid signature */
  1524. if(*((dword FAR2*)newSign)==0xffffffffL)
  1525. {
  1526. DEBUG_PRINT(("Debug: can not use 'FFFF' signature for Binary partition.\r\n"));
  1527. return flBadParameter;
  1528. }
  1529. checkStatus(bdkUpdateBootAreaInit(0,(dword)noOfBlocks<<flash->erasableBlockSizeBits,
  1530. BDK_PARTIAL_IMAGE_UPDATE,oldSign));
  1531. tffscpy( (void FAR1 *)&bdkVol->signBuffer, (void FAR1 *) newSign, BDK_SIGNATURE_NAME);
  1532. tffscpy( (void FAR1 *)&bdkVol->signBuffer[BDK_SIGNATURE_NAME], /* Last block FFFF */
  1533. (void FAR1 *)"FFFF", SIGNATURE_NUM );
  1534. iBlock = (word)(bdkVol->curUpdateImageAddress >> bdkVol->erasableBlockBits);
  1535. for (index=0;index<noOfBlocks;index++)
  1536. /* find next good unit erase it and write its new signature */
  1537. {
  1538. for(;( iBlock <= bdkVol->endImageBlock ); iBlock++)
  1539. {
  1540. if ((dword)(iBlock % bdkVol->blockPerFloor) < bdkVol->flash->firstUsableBlock)
  1541. {
  1542. iBlock = (word)(iBlock + bdkVol->flash->firstUsableBlock);
  1543. if (iBlock > bdkVol->endImageBlock)
  1544. break;
  1545. }
  1546. status = flash->read(flash , bdkVol->bdkSignOffset +
  1547. ((CardAddress)iBlock << bdkVol->erasableBlockBits),
  1548. (byte FAR1 *)signRead, BDK_SIGNATURE_NAME ,EXTRA);
  1549. if(status!=flOK)
  1550. {
  1551. DEBUG_PRINT(("(EXTRA)read failed.\r\n"));
  1552. return status;
  1553. }
  1554. if( tffscmp( (void FAR1 *)oldSign,
  1555. (void FAR1 *)(signRead), BDK_SIGNATURE_NAME ) == 0 )
  1556. break;
  1557. }
  1558. if( iBlock > bdkVol->endImageBlock )
  1559. return( flNoSpaceInVolume );
  1560. checkStatus(flash->erase(flash, iBlock, 1 ));
  1561. checkStatus(flash->write(flash, bdkVol->bdkSignOffset +
  1562. ((CardAddress)iBlock << bdkVol->erasableBlockBits),
  1563. (byte FAR1 *)bdkVol->signBuffer, SIGNATURE_LEN, EXTRA));
  1564. }
  1565. bdkVol->actualUpdateLen = 0L;
  1566. return( flOK );
  1567. }
  1568. #endif /* CREATE_BDK_IMAGE */
  1569. #endif /* UPDATE_BDK_IMAGE */
  1570. #ifdef BDK_IMAGE_TO_FILE
  1571. #include <stdio.h>
  1572. /*-------------------------------------------------------------------
  1573. * b d k C o p y B o o t A r e a F i l e
  1574. *
  1575. * Copy the BDK Image from the DiskOnChip to file 'fname' starting at
  1576. * 'startAddress', with a size of 'areaLen' bytes.
  1577. *
  1578. * Note : Blocks in the DiskOnChip are marked with a 4-character signature
  1579. * followed by a 4-digit hexadecimal number.
  1580. *
  1581. * Parameters: 'fname' - pointer to file name
  1582. * 'startUnit' - start block in image for reading
  1583. * 'areaLen' - BDK image size
  1584. * 'checkSum' - pointer to the checksum modulo 0x100
  1585. * 'signature' - 4-character signature of storage units
  1586. *
  1587. * global variable input :
  1588. * bdkVol - current binary partition record
  1589. * flash - flash record enabling media I\O
  1590. * bdkGlobalStatus - was this partition accessed before
  1591. * bdkSavedSignBuffer - signature offset of previous access
  1592. * bdkSignOffset - current signature offset
  1593. *
  1594. * global variable output :
  1595. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  1596. * signBuffer - initialize with given signature
  1597. * bdkSavedSignBuffer - save current signatur for next access
  1598. * bootImageSize - size of the entire sub partition in bytes
  1599. * realBootImageSize - number of units writen in the sub partition
  1600. * startPartitionBlock - low physical boundry of the partition
  1601. * endPartitionBlock - high physical boundry of the partition
  1602. * startImageBlock - physical unit number of the first and
  1603. * endImageBlock last units marked with the signature
  1604. * curReadImageAddress - address of last unit read
  1605. * actualReadLen - 0
  1606. *
  1607. * Return :
  1608. * flOK - success
  1609. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1610. * flGeneralFailure - could not open file
  1611. * flUnknownMedia - failed in Flash chips recognition
  1612. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1613. * protection violation. This device is unusable.
  1614. * flBadFormat - TL format does not exists
  1615. * flNoSpaceInVolume - there are 0 units marked with this signature
  1616. * flDataError - MTD read fault.
  1617. * flHWProtect - HW read protection was triggerd
  1618. *
  1619. * Routine for BDK stand alone package.
  1620. *-------------------------------------------------------------------*/
  1621. FLStatus bdkCopyBootAreaFile( Sbyte FAR2 *fname, word startUnit, dword areaLen,
  1622. byte FAR2 *checkSum, byte FAR2 *signature )
  1623. {
  1624. dword curLen;
  1625. word copyLen;
  1626. FILE *fout;
  1627. byte buf[BLOCK];
  1628. checkStatus(bdkCopyBootAreaInit( startUnit, areaLen, signature ));
  1629. if( (fout = fopen(fname,"wb")) == NULL )
  1630. return( flGeneralFailure );
  1631. #ifdef BDK_CHECK_SUM
  1632. *checkSum = 0;
  1633. #endif /* BDK_CHECK_SUM */
  1634. for(curLen=0L;( curLen < areaLen );curLen+=BLOCK)
  1635. {
  1636. copyLen = (word)BDK_MIN((areaLen - curLen), BLOCK);
  1637. checkStatus(bdkCopyBootAreaBlock( (byte FAR1 *)buf, copyLen, checkSum ));
  1638. fwrite( buf, 1, copyLen, fout );
  1639. }
  1640. fclose(fout);
  1641. return( flOK );
  1642. }
  1643. #ifdef UPDATE_BDK_IMAGE
  1644. /*-------------------------------------------------------------------
  1645. * b d k U p d a t e B o o t A r e a F i l e
  1646. *
  1647. * Copy the BDK Image to the DiskOnChip from the file 'fname' starting at
  1648. * 'startUnit', with a size of 'areaLen' bytes and 'signature'.
  1649. *
  1650. * Note : Blocks in the DiskOnChip are marked with a 4-character signature
  1651. * followed by a 4-digit hexadecimal number.
  1652. *
  1653. * Parameters: 'fname' - pointer to file name
  1654. * 'startUnit' - start block in image for reading
  1655. * 'areaLen' - BDK image size
  1656. * 'signature' - 4-character signature of storage units
  1657. *
  1658. * global variable input :
  1659. * bdkVol - current binary partition record
  1660. * flash - flash record enabling media I\O
  1661. * bdkGlobalStatus - was this partition accessed before
  1662. * bdkSavedSignBuffer - signature offset of previous access
  1663. * bdkSignOffset - current signature offset
  1664. *
  1665. * global variable output :
  1666. * bdkGlobalStatus - set to BDK_S_INFO_FOUND
  1667. * signBuffer - initialize with given signature
  1668. * bdkSavedSignBuffer - save current signatur for next access
  1669. * bootImageSize - size of the entire sub partition in bytes
  1670. * realBootImageSize - number of units writen in the sub partition
  1671. * startPartitionBlock - low physical boundry of the partition
  1672. * endPartitionBlock - high physical boundry of the partition
  1673. * startImageBlock - physical unit number of the first and
  1674. * endImageBlock last units marked with the signature
  1675. * actualUpdateLen - 0
  1676. * curUpadateImageAddress - address of last written unit
  1677. * Return :
  1678. * flOK - success
  1679. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1680. * flGeneralFailure - could not open file
  1681. * flUnknownMedia - failed in Flash chips recognition
  1682. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1683. * protection violation. This device is unusable.
  1684. * flBadFormat - TL format does not exists
  1685. * flNoSpaceInVolume - there are 0 units marked with this signature
  1686. * flDataError - MTD read fault.
  1687. * flHWProtect - HW protection was triggerd
  1688. * flWriteFault - MTD write fault
  1689. *
  1690. * Routine for BDK stand alone package.
  1691. *-------------------------------------------------------------------*/
  1692. FLStatus bdkUpdateBootAreaFile( Sbyte FAR2 *fname, word startUnit,
  1693. dword areaLen , byte FAR2 *signature )
  1694. {
  1695. dword curLen;
  1696. word copyLen;
  1697. FILE *fout;
  1698. byte buf[BLOCK];
  1699. checkStatus(bdkUpdateBootAreaInit( startUnit, areaLen,
  1700. BDK_COMPLETE_IMAGE_UPDATE, signature ));
  1701. if( (fout = fopen(fname,"rb")) == NULL )
  1702. return(flGeneralFailure);
  1703. for(curLen=0L;( curLen < areaLen );curLen+=BLOCK)
  1704. {
  1705. copyLen = (word)BDK_MIN((areaLen - curLen), BLOCK);
  1706. fread( buf, 1, copyLen, fout );
  1707. checkStatus(bdkUpdateBootAreaBlock( (byte FAR1 *)buf, copyLen ));
  1708. }
  1709. fclose(fout);
  1710. return( flOK );
  1711. }
  1712. #endif /* UPDATE_BDK_IMAGE */
  1713. #endif /* BDK_IMAGE_TO_FILE */
  1714. #ifdef PROTECT_BDK_IMAGE
  1715. /*--------------------------------------------------------------------------
  1716. * p r o t e c t i o n I n i t
  1717. *
  1718. * Makes suer the volume is mounted before calling protection calls.
  1719. *
  1720. * Note this routine is called directly by each of the protection routines
  1721. *
  1722. * return
  1723. * FLStatus flOK otherwise respective error code.
  1724. *
  1725. *--------------------------------------------------------------------------*/
  1726. static FLStatus protectionInit(void)
  1727. {
  1728. /* Check that the partition is mounted */
  1729. #ifdef MTD_STANDALONE
  1730. dword temp;
  1731. checkStatus(bdkFindDiskOnChip((dword FAR2 *)&temp,(dword FAR2 *)&temp));
  1732. #endif /* MTD_STANDALONE */
  1733. checkStatus(bdkMount());
  1734. if (bdkVol->flash->flags & NFTL_ENABLED)
  1735. {
  1736. DEBUG_PRINT(("NFTL does not support protection.\r\n"));
  1737. return flFeatureNotSupported;
  1738. }
  1739. if ((bdkVol->protectionType & PROTECTABLE) == 0)
  1740. {
  1741. DEBUG_PRINT(("Not a protectable partition.\r\n"));
  1742. return flNotProtected;
  1743. }
  1744. return flOK;
  1745. }
  1746. /*--------------------------------------------------------------------------
  1747. * p r o t e c t i o n C h a n g e I n i t
  1748. *
  1749. * Makes sure the volume is mounted before calling protection calls.
  1750. * Makes sure the protection can be updated and functions arae vailable
  1751. *
  1752. * Note this routine is called directly by each of the protection routines
  1753. *
  1754. * return
  1755. * FLStatus flOK on success non zero otherwise
  1756. *
  1757. *--------------------------------------------------------------------------*/
  1758. static FLStatus protectionChangeInit(word * type)
  1759. {
  1760. checkStatus(protectionInit());
  1761. if (!(bdkVol->protectionType & CHANGEABLE_PROTECTION))
  1762. {
  1763. DEBUG_PRINT(("Uncheangable protection.\r\n"));
  1764. return flFeatureNotSupported;
  1765. }
  1766. if ((bdkVol->flash->protectionBoundries == NULL) ||
  1767. (bdkVol->flash->protectionSet == NULL) ||
  1768. (bdkVol->flash->protectionKeyInsert == NULL) ||
  1769. (bdkVol->flash->protectionType == NULL))
  1770. {
  1771. DEBUG_PRINT(("H/W does not support protection.\r\n"));
  1772. return flFeatureNotSupported;
  1773. }
  1774. /* get the protection type */
  1775. checkStatus(bdkVol->flash->protectionType(bdkVol->flash,
  1776. bdkVol->protectionArea, (word *)type));
  1777. return (bdkVol->flash->protectionKeyInsert(bdkVol->flash,
  1778. bdkVol->protectionArea, (byte *)DEFAULT_KEY));
  1779. }
  1780. /*--------------------------------------------------------------------------
  1781. * b d k G e t P r o t e c t i o n T y p e
  1782. *
  1783. * Return the current partition protection type.
  1784. *
  1785. *
  1786. * Parameters : 'protectionType' - return the protection type as a
  1787. * combination of the following flags:
  1788. *
  1789. * PROTECTABLE - A protection area is allocated for this volume
  1790. * LOCK_ENABLED - HW Lock signal is enabled.
  1791. * LOCK_ASSERTED - HW Lock signal is asserted.
  1792. * KEY_INSERTED - Key is currently inserted (protection is down).
  1793. * READ_PROTECTED - Area is protected against read operations.
  1794. * WRITE_PROTECTED - Area is protected against write operations.
  1795. * CHANGEABLE_PROTECTION - The area is protected against write operations.
  1796. *
  1797. * global variable input :
  1798. * bdkGlobalStatus - was this device found before
  1799. * bdkVol - current binary partition record
  1800. * bdkDocWindow - if > 0 only this address will be checked
  1801. *
  1802. * global variable output :
  1803. * bdkVol - current binary partition record
  1804. * bdkGlobalStatus - both BDK_S_DOC_FOUND and BDK_S_HEADER_FOUND
  1805. * flash - identify flash media and its socket
  1806. * bdkDocWindow - DiskOnChip window physical address
  1807. *
  1808. * Return:
  1809. * flOK - success
  1810. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1811. * flUnknownMedia - fail in Flash chips recognition
  1812. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1813. * protection violation. This device is unusable.
  1814. * flBadFormat - TL format does not exists
  1815. * flDataError - MTD read fault.
  1816. * flHWProtect - HW read protection was triggerd
  1817. * flFeatureNotSupported - The HW protection feature is not supported
  1818. *
  1819. * Routine for both OSAK and the BDK stand alone package.
  1820. *------------------------------------------------------------------------*/
  1821. FLStatus bdkGetProtectionType (word * protectionType)
  1822. {
  1823. FLStatus status;
  1824. status = protectionInit();
  1825. switch (status)
  1826. {
  1827. case flOK:
  1828. if (bdkVol->flash->protectionType==NULL)
  1829. return flFeatureNotSupported;
  1830. checkStatus (bdkVol->flash->protectionType(bdkVol->flash,
  1831. bdkVol->protectionArea, (word *)protectionType));
  1832. *protectionType &= ~((word)(PROTECTABLE | CHANGEABLE_PROTECTION));
  1833. *protectionType |= (word)(bdkVol->protectionType & (word)(PROTECTABLE | CHANGEABLE_PROTECTION));
  1834. return status;
  1835. case flNotProtected:
  1836. *protectionType = 0;
  1837. default:
  1838. return status;
  1839. }
  1840. }
  1841. /*--------------------------------------------------------------------------
  1842. * b d k I n s e r t K e y
  1843. *
  1844. * Insert the protection key to disable the HW protection.
  1845. *
  1846. * Parameters : 'key' - The key to send
  1847. *
  1848. * global variable input :
  1849. * bdkGlobalStatus - was this device found before
  1850. * bdkVol - current binary partition record
  1851. * bdkDocWindow - if > 0 only this address will be checked
  1852. *
  1853. * global variable output :
  1854. * bdkVol - current binary partition record
  1855. * bdkGlobalStatus - both BDK_S_DOC_FOUND and BDK_S_HEADER_FOUND
  1856. * flash - identify flash media and its socket
  1857. * bdkDocWindow - DiskOnChip window physical address
  1858. *
  1859. * Return:
  1860. * flOK - success
  1861. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1862. * flUnknownMedia - fail in Flash chips recognition
  1863. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1864. * protection violation. This device is unusable.
  1865. * flBadFormat - TL format does not exists
  1866. * flDataError - MTD read fault.
  1867. * flHWProtect - HW read protection was triggerd
  1868. * flFeatureNotSupported - The HW protection feature is not supported
  1869. *
  1870. * Routine for both OSAK and the BDK stand alone package.
  1871. *------------------------------------------------------------------------*/
  1872. FLStatus bdkInsertKey (byte FAR1* key)
  1873. {
  1874. checkStatus(protectionInit());
  1875. if (bdkVol->flash->protectionKeyInsert==NULL)
  1876. return flFeatureNotSupported;
  1877. return (bdkVol->flash->protectionKeyInsert(bdkVol->flash,
  1878. bdkVol->protectionArea, key));
  1879. }
  1880. /*--------------------------------------------------------------------------
  1881. * b d k R e m o v e K e y
  1882. *
  1883. * Return the current partition protection type.
  1884. *
  1885. * Parameters : None
  1886. *
  1887. * global variable input :
  1888. * bdkGlobalStatus - was this device found before
  1889. * bdkVol - current binary partition record
  1890. * bdkDocWindow - if > 0 only this address will be checked
  1891. *
  1892. * global variable output :
  1893. * bdkVol - current binary partition record
  1894. * bdkGlobalStatus - both BDK_S_DOC_FOUND and BDK_S_HEADER_FOUND
  1895. * flash - identify flash media and its socket
  1896. * bdkDocWindow - DiskOnChip window physical address
  1897. *
  1898. * Return:
  1899. * flOK - success
  1900. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1901. * flUnknownMedia - fail in Flash chips recognition
  1902. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1903. * protection violation. This device is unusable.
  1904. * flBadFormat - TL format does not exists
  1905. * flDataError - MTD read fault.
  1906. * flHWProtect - HW read protection was triggerd
  1907. * flFeatureNotSupported - The HW protection feature is not supported
  1908. *
  1909. * Routine for both OSAK and the BDK stand alone package.
  1910. *------------------------------------------------------------------------*/
  1911. FLStatus bdkRemoveKey (void)
  1912. {
  1913. checkStatus(protectionInit());
  1914. if ((bdkVol->flash->flags & NFTL_ENABLED) ||
  1915. (bdkVol->flash->protectionKeyRemove==NULL))
  1916. return flFeatureNotSupported;
  1917. return (bdkVol->flash->protectionKeyRemove(bdkVol->flash,
  1918. bdkVol->protectionArea));
  1919. }
  1920. /*--------------------------------------------------------------------------
  1921. * b d k L o c k E n a b l e
  1922. *
  1923. * Enable or disable the HW LOCK signal.
  1924. *
  1925. * Note : The protction key must be off before calling this routine.
  1926. * While asserted the HW LOCK signal can not be disabled.
  1927. *
  1928. * Parameters : 'enabled' - LOCK_ENABLED - enables the HW LOCK
  1929. * otherwise disabled
  1930. *
  1931. * global variable input :
  1932. * bdkGlobalStatus - was this device found before
  1933. * bdkVol - current binary partition record
  1934. * bdkDocWindow - if > 0 only this address will be checked
  1935. *
  1936. * global variable output :
  1937. * bdkVol - current binary partition record
  1938. * bdkGlobalStatus - both BDK_S_DOC_FOUND and BDK_S_HEADER_FOUND
  1939. * flash - identify flash media and its socket
  1940. * bdkDocWindow - DiskOnChip window physical address
  1941. *
  1942. * Return:
  1943. * flOK - success
  1944. * flDriveNotAvailable - DiskOnChip ASIC was not found
  1945. * flUnknownMedia - fail in Flash chips recognition
  1946. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  1947. * protection violation. This device is unusable.
  1948. * flBadFormat - TL format does not exists
  1949. * flDataError - MTD read fault.
  1950. * flHWProtect - HW protection was triggerd
  1951. * flWriteFault - MTD write fault
  1952. * flFeatureNotSupported - The HW protection feature is not supported
  1953. *
  1954. * Routine for both OSAK and the BDK stand alone package.
  1955. *------------------------------------------------------------------------*/
  1956. FLStatus bdkLockEnable (byte enabled)
  1957. {
  1958. CardAddress low,high;
  1959. word type;
  1960. byte floorNo;
  1961. checkStatus(protectionChangeInit(&type));
  1962. if (enabled == LOCK_ENABLED)
  1963. {
  1964. type |= LOCK_ENABLED;
  1965. }
  1966. else
  1967. {
  1968. type &= ~LOCK_ENABLED;
  1969. }
  1970. for (floorNo=0;floorNo<bdkVol->flash->noOfFloors;floorNo++)
  1971. {
  1972. /* Find boundries */
  1973. checkStatus(bdkVol->flash->protectionBoundries(bdkVol->flash,
  1974. bdkVol->protectionArea,&low,&high,floorNo));
  1975. /* Set new protection values */
  1976. checkStatus(bdkVol->flash->protectionSet(bdkVol->flash,
  1977. bdkVol->protectionArea,(word)((high == 0) ? PROTECTABLE : type),low,high,
  1978. NULL,(byte)((floorNo == bdkVol->flash->noOfFloors - 1) ?
  1979. COMMIT_PROTECTION : DO_NOT_COMMIT_PROTECTION),floorNo));
  1980. }
  1981. return flOK;
  1982. }
  1983. /*--------------------------------------------------------------------------
  1984. * b d k C h a n g e K e y
  1985. *
  1986. * Change the protection key of a protected binary partition..
  1987. *
  1988. * Note : The protction key must be off before calling this routine.
  1989. *
  1990. * Parameters : 'key' - The new protection key.
  1991. *
  1992. * global variable input :
  1993. * bdkGlobalStatus - was this device found before
  1994. * bdkVol - current binary partition record
  1995. * bdkDocWindow - if > 0 only this address will be checked
  1996. *
  1997. * global variable output :
  1998. * bdkVol - current binary partition record
  1999. * bdkGlobalStatus - both BDK_S_DOC_FOUND and BDK_S_HEADER_FOUND
  2000. * flash - identify flash media and its socket
  2001. * bdkDocWindow - DiskOnChip window physical address
  2002. *
  2003. * Return:
  2004. * flOK - success
  2005. * flDriveNotAvailable - DiskOnChip ASIC was not found
  2006. * flUnknownMedia - fail in Flash chips recognition
  2007. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  2008. * protection violation. This device is unusable.
  2009. * flBadFormat - TL format does not exists
  2010. * flDataError - MTD read fault.
  2011. * flHWProtect - HW protection was triggerd
  2012. * flWriteFault - MTD write fault
  2013. * flFeatureNotSupported - The HW protection feature is not supported
  2014. *
  2015. * Routine for both OSAK and the BDK stand alone package.
  2016. *------------------------------------------------------------------------*/
  2017. FLStatus bdkChangeKey(byte FAR1 * key)
  2018. {
  2019. CardAddress low,high;
  2020. word type;
  2021. byte floorNo;
  2022. checkStatus(protectionChangeInit(&type));
  2023. for (floorNo=0;floorNo<bdkVol->flash->noOfFloors;floorNo++)
  2024. {
  2025. /* Find boundries */
  2026. checkStatus(bdkVol->flash->protectionBoundries(bdkVol->flash,
  2027. bdkVol->protectionArea,&low,&high,floorNo));
  2028. /* Set new protection values */
  2029. checkStatus(bdkVol->flash->protectionSet(bdkVol->flash,
  2030. bdkVol->protectionArea,(word)((high == 0) ? PROTECTABLE : type),
  2031. low,high,key,(byte)((floorNo == bdkVol->flash->noOfFloors - 1)
  2032. ? COMMIT_PROTECTION : DO_NOT_COMMIT_PROTECTION),floorNo));
  2033. }
  2034. return flOK;
  2035. }
  2036. /*--------------------------------------------------------------------------
  2037. * b d k S e t P r o t e c t i o n T y p e
  2038. *
  2039. * Change the current partition protection type.
  2040. *
  2041. *
  2042. * Parameters : 'protectionType' - change the protection type as a
  2043. * combination of the following flags:
  2044. *
  2045. * PROTECTABLE - Must be added for the routine to work.
  2046. * READ_PROTECTED - Area is protected against read operations.
  2047. * WRITE_PROTECTED - Area is protected against write operations.
  2048. *
  2049. * Note - To add extra protection only a combination of the above 3 flags is
  2050. * aceptable and the PROTECTABLE flag must be on.
  2051. *
  2052. * global variable input :
  2053. * bdkGlobalStatus - was this device found before
  2054. * bdkVol - current binary partition record
  2055. * bdkDocWindow - if > 0 only this address will be checked
  2056. *
  2057. * global variable output :
  2058. * bdkVol - current binary partition record
  2059. * bdkGlobalStatus - both BDK_S_DOC_FOUND and BDK_S_HEADER_FOUND
  2060. * flash - identify flash media and its socket
  2061. * bdkDocWindow - DiskOnChip window physical address
  2062. *
  2063. * Return:
  2064. * flOK - success
  2065. * flDriveNotAvailable - DiskOnChip ASIC was not found
  2066. * flUnknownMedia - fail in Flash chips recognition
  2067. * flBadDownload - DiskOnChip Millennium Plus reported an uncorrectable
  2068. * protection violation. This device is unusable.
  2069. * flBadFormat - TL format does not exists
  2070. * flDataError - MTD read fault.
  2071. * flHWProtect - HW protection was triggerd
  2072. * flWriteFault - MTD write fault
  2073. * flFeatureNotSupported - The HW protection feature is not supported
  2074. *
  2075. * Routine for both OSAK and the BDK stand alone package.
  2076. *------------------------------------------------------------------------*/
  2077. FLStatus bdkSetProtectionType (word newType)
  2078. {
  2079. CardAddress low,high;
  2080. word type;
  2081. byte floorNo;
  2082. checkStatus(protectionChangeInit(&type));
  2083. if (((newType & (READ_PROTECTED | WRITE_PROTECTED | PROTECTABLE)) != newType) ||
  2084. ((newType & PROTECTABLE) == 0))
  2085. return flBadParameter;
  2086. newType |= type & ~(READ_PROTECTED | WRITE_PROTECTED);
  2087. for (floorNo=0;floorNo<bdkVol->flash->noOfFloors;floorNo++)
  2088. {
  2089. /* Find boundries */
  2090. checkStatus(bdkVol->flash->protectionBoundries(bdkVol->flash,
  2091. bdkVol->protectionArea,&low,&high,floorNo));
  2092. /* Set new protection values */
  2093. checkStatus(bdkVol->flash->protectionSet(bdkVol->flash,
  2094. bdkVol->protectionArea,(word)((high == 0) ? PROTECTABLE : newType),
  2095. low,high,NULL,(byte)((floorNo == bdkVol->flash->noOfFloors - 1)
  2096. ? COMMIT_PROTECTION : DO_NOT_COMMIT_PROTECTION),floorNo));
  2097. }
  2098. return flOK;
  2099. }
  2100. #endif /* PROTECT_BDK_IMAGE */
  2101. #if (defined(HW_OTP) && defined (MTD_STANDALONE))
  2102. /*----------------------------------------------------------------------*/
  2103. /* b d k G e t U n i q u e I d */
  2104. /* */
  2105. /* Retreave the device 16 bytes unique ID. */
  2106. /* */
  2107. /* Parameters: */
  2108. /* buffer : buffer to read into. */
  2109. /* */
  2110. /* Returns: */
  2111. /* flOK - success */
  2112. /* flDriveNotAvailable - DiskOnChip ASIC was not found */
  2113. /* flUnknownMedia - Failed in Flash chips recognition */
  2114. /* flBadDownload - MdocPlus device has unrecoverable */
  2115. /* protection violation. */
  2116. /* flHWProtection - HW read protection violation. */
  2117. /* flTimedOut - Flash delay not long enough. */
  2118. /*----------------------------------------------------------------------*/
  2119. FLStatus bdkGetUniqueID(byte FAR1* buf)
  2120. {
  2121. dword temp;
  2122. checkStatus(bdkFindDiskOnChip((dword FAR2 *)&temp,(dword FAR2 *)&temp));
  2123. if (bdkVol->flash->getUniqueId == NULL)
  2124. return flFeatureNotSupported;
  2125. return(bdkVol->flash->getUniqueId(bdkVol->flash,buf));
  2126. }
  2127. /*----------------------------------------------------------------------*/
  2128. /* b d k R e a d O T P */
  2129. /* */
  2130. /* Read data from the customer OTP. */
  2131. /* */
  2132. /* Parameters: */
  2133. /* offset : Offset from the beginning of OTP area to read from. */
  2134. /* buffer : buffer to read into. */
  2135. /* length : number of bytes to read. */
  2136. /* */
  2137. /* Returns: */
  2138. /* flOK - success */
  2139. /* flDriveNotAvailable - DiskOnChip ASIC was not found */
  2140. /* flUnknownMedia - Failed in Flash chips recognition */
  2141. /* flBadDownload - MdocPlus device has unrecoverable */
  2142. /* protection violation. */
  2143. /* flHWProtection - HW read protection violation. */
  2144. /* flTimedOut - Flash delay not long enough. */
  2145. /*----------------------------------------------------------------------*/
  2146. FLStatus bdkReadOtp(word offset,byte FAR1 * buffer,word length)
  2147. {
  2148. dword temp;
  2149. checkStatus(bdkFindDiskOnChip((dword FAR2 *)&temp,(dword FAR2 *)&temp));
  2150. if (bdkVol->flash->readOTP == NULL)
  2151. return flFeatureNotSupported;
  2152. return(bdkVol->flash->readOTP(bdkVol->flash,offset,buffer,length));
  2153. }
  2154. #ifndef FL_READ_ONLY
  2155. /*----------------------------------------------------------------------*/
  2156. /* b d k W r i t e A n d L o c k O T P */
  2157. /* */
  2158. /* Write and lock the customer OTP. */
  2159. /* */
  2160. /* Parameters: */
  2161. /* buffer : buffer to write from. */
  2162. /* length : number of bytes to write. */
  2163. /* */
  2164. /* Note - Once writen (even a single byte) the entire section is */
  2165. /* locked forever. The data is written with EDC. */
  2166. /* */
  2167. /* Returns: */
  2168. /* flOK - success */
  2169. /* flDriveNotAvailable - DiskOnChip ASIC was not found */
  2170. /* flUnknownMedia - Failed in Flash chips recognition */
  2171. /* flBadDownload - MdocPlus device has unrecoverable */
  2172. /* protection violation. */
  2173. /* flHWProtection - HW protection violation. */
  2174. /* flTimedOut - Flash delay not long enough. */
  2175. /*----------------------------------------------------------------------*/
  2176. FLStatus bdkWriteAndLockOtp(const byte FAR1 * buffer,word length)
  2177. {
  2178. dword temp;
  2179. checkStatus(bdkFindDiskOnChip((dword FAR2 *)&temp,(dword FAR2 *)&temp));
  2180. if (bdkVol->flash->writeOTP == NULL)
  2181. return flFeatureNotSupported;
  2182. return(bdkVol->flash->writeOTP(bdkVol->flash,buffer,length));
  2183. }
  2184. #endif /* FL_READ_ONLY */
  2185. /*----------------------------------------------------------------------*/
  2186. /* b d k G e t O t p S i z e */
  2187. /* */
  2188. /* Returns the size and state of the OTP area. */
  2189. /* */
  2190. /* Parameters: */
  2191. /* sectionSize : Total OTP size. */
  2192. /* usedSize : Used OTP size. */
  2193. /* locked : Lock state (LOCKED_OTP for locked). */
  2194. /* */
  2195. /* Returns: */
  2196. /* flOK - success */
  2197. /* flDriveNotAvailable - DiskOnChip ASIC was not found */
  2198. /* flUnknownMedia - Failed in Flash chips recognition */
  2199. /* flBadDownload - MdocPlus device has unrecoverable */
  2200. /* protection violation. */
  2201. /* flHWProtection - HW read protection violation. */
  2202. /* flTimedOut - Flash delay not long enough. */
  2203. /*----------------------------------------------------------------------*/
  2204. FLStatus bdkGetOtpSize(dword FAR2* sectionSize, dword FAR2* usedSize, word FAR2* locked)
  2205. {
  2206. dword temp;
  2207. checkStatus(bdkFindDiskOnChip((dword FAR2 *)&temp,(dword FAR2 *)&temp));
  2208. if (bdkVol->flash->otpSize == NULL)
  2209. return flFeatureNotSupported;
  2210. return(bdkVol->flash->otpSize(bdkVol->flash,sectionSize,usedSize,locked));
  2211. }
  2212. #endif /* HW_OTP */
  2213. #ifdef BDK_ACCESS
  2214. /*----------------------------------------------------------------------*/
  2215. /* b d k C a l l */
  2216. /* */
  2217. /* Common entry-point to all binary partition functions. */
  2218. /* */
  2219. /* Note : the error codes and global variable changed by this routin */
  2220. /* depened on the functionNo parameter and can be deduced from the */
  2221. /* coresponding routine in the BDK package or from the OSAK manual */
  2222. /* */
  2223. /* Parameters: */
  2224. /* functionNo : file-system function code (listed below) */
  2225. /* ioreq : IOreq structure */
  2226. /* flash : flash record supplied hardware information and */
  2227. /* flash access routines */
  2228. /* Returns: */
  2229. /* FLStatus : 0 on success, otherwise failed */
  2230. /* */
  2231. /* Routine for OSAK package. */
  2232. /*----------------------------------------------------------------------*/
  2233. FLStatus bdkCall(FLFunctionNo functionNo, IOreq FAR2 *ioreq, FLFlash* flash)
  2234. {
  2235. FLStatus status;
  2236. BDKStruct* bdkParam = (BDKStruct*)ioreq->irData;
  2237. byte volNo;
  2238. globalSocketNo = FL_GET_SOCKET_FROM_HANDLE(ioreq);
  2239. globalPartitionNo = FL_GET_PARTITION_FROM_HANDLE(ioreq);
  2240. /* convert drive handle to the appropriate binary partition record */
  2241. if (globalSocketNo>SOCKETS)
  2242. return flBadDriveHandle;
  2243. if (globalPartitionNo>MAX_BINARY_PARTITIONS_PER_DRIVE)
  2244. return flBadDriveHandle;
  2245. volNo = handleTable[globalSocketNo][globalPartitionNo];
  2246. if (volNo!=BDK_INVALID_VOLUME_HANDLE) /* not first access to this device */
  2247. {
  2248. bdkVol = &bdkVols[volNo];
  2249. }
  2250. else /* first access to this device */
  2251. {
  2252. bdkVol = &bdkVols[noOfPartitions];
  2253. bdkVol->flash = flash;
  2254. bdkVol->bdkGlobalStatus = BDK_S_DOC_FOUND;
  2255. }
  2256. #ifdef VERIFY_WRITE
  2257. /* Set binary partition verify write state */
  2258. flash->socket->verifyWrite = flVerifyWrite[globalSocketNo][globalPartitionNo+MAX_TL_PARTITIONS];
  2259. #endif /* VERIFY_WRITE */
  2260. /* Call the proper binary partition function */
  2261. switch (functionNo)
  2262. {
  2263. case FL_BINARY_READ_INIT:
  2264. bdkVol->bdkEDC = bdkParam->flags & EDC;
  2265. bdkVol->bdkSignOffset = bdkParam->signOffset;
  2266. return bdkCopyBootAreaInit((word)bdkParam->startingBlock,
  2267. bdkParam->length,bdkParam->oldSign);
  2268. case FL_BINARY_READ_BLOCK:
  2269. return bdkCopyBootAreaBlock(bdkParam->bdkBuffer,
  2270. (word)bdkParam->length,NULL);
  2271. case FL_BINARY_PARTITION_INFO:
  2272. {
  2273. dword unitSize;
  2274. #ifndef NT5PORT
  2275. int volNo;
  2276. #else /*NT5PORT*/
  2277. int nIndex;
  2278. #endif /*NT5PORT*/
  2279. status = bdkGetBootPartitionInfo((word)bdkParam->startingBlock,
  2280. &(bdkParam->startingBlock), &(bdkParam->length),&unitSize,
  2281. bdkParam->oldSign);
  2282. ioreq->irLength = (long)(bdkVol->endPartitionBlock -
  2283. bdkVol->startPartitionBlock + 1)<<(long)bdkVol->flash->erasableBlockSizeBits;
  2284. bdkParam->flags = 0;
  2285. #ifndef NT5PORT
  2286. for (volNo = 0;volNo < MAX_BINARY_PARTITIONS_PER_DRIVE;volNo++)
  2287. {
  2288. if (handleTable[globalSocketNo][volNo]!=
  2289. BDK_INVALID_VOLUME_HANDLE)
  2290. bdkParam->flags++;
  2291. }
  2292. #else /*NT5PORT*/
  2293. for (nIndex = 0;nIndex < MAX_BINARY_PARTITIONS_PER_DRIVE; nIndex++)
  2294. {
  2295. if (handleTable[globalSocketNo][nIndex]!=
  2296. BDK_INVALID_VOLUME_HANDLE)
  2297. bdkParam->flags++;
  2298. }
  2299. #endif /*NT5PORT*/
  2300. return status;
  2301. }
  2302. #ifdef UPDATE_BDK_IMAGE
  2303. case FL_BINARY_WRITE_INIT:
  2304. bdkVol->bdkEDC = bdkParam->flags & EDC;
  2305. bdkVol->bdkSignOffset = bdkParam->signOffset;
  2306. return bdkUpdateBootAreaInit((word)bdkParam->startingBlock, bdkParam->length,
  2307. bdkParam->flags , bdkParam->oldSign);
  2308. case FL_BINARY_WRITE_BLOCK:
  2309. if (bdkParam->flags & ERASE_BEFORE_WRITE)
  2310. {
  2311. bdkVol->updateImageFlag |= ERASE_BEFORE_WRITE;
  2312. }
  2313. else
  2314. {
  2315. bdkVol->updateImageFlag &= ~ ERASE_BEFORE_WRITE;
  2316. }
  2317. return bdkUpdateBootAreaBlock(bdkParam->bdkBuffer, (word)bdkParam->length);
  2318. #ifdef CREATE_BDK_IMAGE
  2319. case FL_BINARY_CREATE:
  2320. bdkVol->bdkSignOffset = bdkParam->signOffset;
  2321. return bdkCreateBootArea((word)bdkParam->length,bdkParam->oldSign,
  2322. bdkParam->newSign);
  2323. #endif /* CREATE_BDK_IMAGE */
  2324. #ifdef ERASE_BDK_IMAGE
  2325. case FL_BINARY_ERASE:
  2326. bdkVol->bdkSignOffset = bdkParam->signOffset;
  2327. return bdkEraseBootArea((word)bdkParam->startingBlock,(word)bdkParam->length,
  2328. bdkParam->oldSign);
  2329. #endif /* ERASE_BDK_IMAGE */
  2330. #endif /* UPDATE_BDK_IMAGE */
  2331. #ifdef PROTECT_BDK_IMAGE
  2332. case FL_BINARY_PROTECTION_GET_TYPE:
  2333. {
  2334. word tmpFlags;
  2335. checkStatus ( bdkGetProtectionType (&tmpFlags));
  2336. ioreq->irFlags = (unsigned) tmpFlags;
  2337. return flOK;
  2338. }
  2339. case FL_BINARY_PROTECTION_SET_TYPE:
  2340. return bdkSetProtectionType ((word)ioreq->irFlags);
  2341. case FL_BINARY_PROTECTION_INSERT_KEY:
  2342. return bdkInsertKey ((byte FAR1*)ioreq->irData);
  2343. case FL_BINARY_PROTECTION_REMOVE_KEY:
  2344. return bdkRemoveKey ();
  2345. case FL_BINARY_PROTECTION_CHANGE_KEY:
  2346. return bdkChangeKey ((byte FAR1*)ioreq->irData);
  2347. case FL_BINARY_PROTECTION_CHANGE_LOCK:
  2348. return bdkLockEnable ((byte)ioreq->irFlags);
  2349. #endif /* PROTECT_BDK_IMAGE */
  2350. default: /* not a binary partition routine */
  2351. return flBadFunction;
  2352. }
  2353. }
  2354. #endif /* BDK_ACCESS */
  2355. #endif /* BDK_ACCESS || MTD_STANDALONE || ACCESS_BDK_IMAGE */