Source code of Windows XP (NT5)
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.

534 lines
13 KiB

  1. //******************************************************************************/
  2. //* *
  3. //* tc6807af.c - *
  4. //* *
  5. //* Copyright (c) C-Cube Microsystems 1996 *
  6. //* All Rights Reserved. *
  7. //* *
  8. //* Use of C-Cube Microsystems code is governed by terms and conditions *
  9. //* stated in the accompanying licensing statement. *
  10. //* *
  11. //******************************************************************************/
  12. //
  13. // TC6907AF.C Digital Copy-Protection for DVD
  14. //
  15. /////////////////////////////////////////////////////////////////////
  16. #ifdef VTOOLSD
  17. #include <vtoolsc.h>
  18. #include "monovxd.h"
  19. #else
  20. #include "Headers.h"
  21. #pragma hdrstop
  22. #endif
  23. #include "cl6100.h"
  24. #include "tc6807af.h"
  25. #include "fpga.h"
  26. #include "bmaster.h"
  27. #include "boardio.h"
  28. //-------------------------------------------------------------------
  29. // TC6907AF REGISTERS DECLARATION
  30. //-------------------------------------------------------------------
  31. #define COM 0x00
  32. #define CNT_1 0x01
  33. #define CNT_2 0x02
  34. #define SD_STS 0x03
  35. #define DEPT_1 0x04
  36. #define DEPT_2 0x05
  37. #define ETKG_0 0x10
  38. #define ETKG_1 0x11
  39. #define ETKG_2 0x12
  40. #define ETKG_3 0x13
  41. #define ETKG_4 0x14
  42. #define ETKG_5 0x15
  43. #define CHGG_0 0x30
  44. #define CHGG_1 0x31
  45. #define CHGG_2 0x32
  46. #define CHGG_3 0x33
  47. #define CHGG_4 0x34
  48. #define CHGG_5 0x35
  49. #define CHGG_6 0x36
  50. #define CHGG_7 0x37
  51. #define CHGG_8 0x38
  52. #define CHGG_9 0x39
  53. #define RSPG_0 0x40
  54. #define RSPG_1 0x41
  55. #define RSPG_2 0x42
  56. #define RSPG_3 0x43
  57. #define RSPG_4 0x44
  58. // COM register bits
  59. #define END 0x80
  60. #define ERR 0x40
  61. // CNT_1 register bits
  62. #define RQ1 0x01
  63. #define RQ2 0x02
  64. #define ENBEND 0x04
  65. #define ENBERR 0x08
  66. #define CLINT 0x10
  67. // CNT_2 register bits
  68. #define THR 0x01
  69. #define EB1 0x02
  70. #define EB2 0x04
  71. #define CDV16 0x08
  72. #define AJSCK 0x10
  73. #define SCR1 0x20
  74. #define SCR2 0x40
  75. #define SCR3 0x80
  76. // Commands
  77. #define NOP 0x00
  78. #define DEC_RAND 0x12
  79. #define DEC_DKY 0x15
  80. #define DRV_AUTH 0x17
  81. #define DEC_AUTH 0x18
  82. #define DEC_DT 0x23
  83. #define DEC_DTK 0x25
  84. //-------------------------------------------------------------------
  85. // GLOBAL VARIABLES DECLARATION
  86. //-------------------------------------------------------------------
  87. DWORD gdwIndex = 0;
  88. DWORD gdwData = 0;
  89. //-------------------------------------------------------------------
  90. // STATIC FUNCTIONS DECLARATION
  91. //-------------------------------------------------------------------
  92. BOOL tc6807af_GetChallengeData( BYTE * CHG );
  93. BOOL tc6807af_SendChallengeData( BYTE * CHG );
  94. BOOL tc6807af_GetResponseData( BYTE * RSP );
  95. BOOL tc6807af_SendResponseData( BYTE * RSP );
  96. BOOL tc6807af_SendDiskKeyData( BYTE * pBuffer );
  97. BOOL tc6807af_SendTitleKeyData( BYTE * pBuffer );
  98. BOOL tc6807af_SetDecryptionMode( BYTE * SR_FLAG );
  99. BOOL tc6807af_NewCommand( BYTE Command );
  100. void tc6807af_WriteReg( BYTE byReg, BYTE byValue );
  101. BYTE tc6807af_ReadReg( BYTE byReg );
  102. //
  103. // TC6807AF_Initialize
  104. //
  105. /////////////////////////////////////////////////////////////////////
  106. BOOL TC6807AF_Initialize( DWORD dwBaseAddress )
  107. {
  108. MonoOutStr( " �� TC6807AF_Initialize " );
  109. MonoOutHex( dwBaseAddress );
  110. gdwIndex = dwBaseAddress;
  111. gdwData = dwBaseAddress + 1;
  112. //tc6807af_WriteReg( CNT_1, ENBERR | ENBEND | RQ1 | RQ2 );
  113. // Step 1.
  114. tc6807af_WriteReg( CNT_2, 0|CDV16 );
  115. // Step 2.
  116. //tc6807af_WriteReg( CNT_2, AJSCK );
  117. /*
  118. tc6807af_WriteReg( CNT_2, 0 );
  119. tc6807af_WriteReg( CNT_2, AJSCK );
  120. tc6807af_WriteReg( CNT_2, 0 );
  121. tc6807af_WriteReg( CNT_2, AJSCK );
  122. tc6807af_WriteReg( CNT_2, 0 );
  123. tc6807af_WriteReg( CNT_2, AJSCK );
  124. tc6807af_WriteReg( CNT_2, 0 );
  125. tc6807af_WriteReg( CNT_2, AJSCK );
  126. tc6807af_WriteReg( CNT_2, 0 );
  127. tc6807af_WriteReg( CNT_2, AJSCK );
  128. tc6807af_WriteReg( CNT_2, 0 );
  129. tc6807af_WriteReg( CNT_2, AJSCK );
  130. tc6807af_WriteReg( CNT_2, 0 );
  131. tc6807af_WriteReg( CNT_2, AJSCK );
  132. tc6807af_WriteReg( CNT_2, 0 );
  133. */
  134. // Step 3.
  135. tc6807af_WriteReg( CNT_1, CLINT );
  136. // Step 4.
  137. tc6807af_WriteReg( DEPT_1, 0 );
  138. tc6807af_WriteReg( DEPT_2, 0 );
  139. tc6807af_WriteReg( CNT_2, AJSCK|SCR1 );
  140. // Step 5.
  141. tc6807af_WriteReg( CNT_1, RQ1 | RQ2 );
  142. tc6807af_WriteReg( CNT_2, AJSCK|SCR1|EB2|EB1|THR );
  143. MonoOutStr( " Ķ " );
  144. return TRUE;
  145. }
  146. //
  147. // TC6807AF_Reset
  148. //
  149. /////////////////////////////////////////////////////////////////////
  150. BOOL TC6807AF_Reset()
  151. {
  152. return TRUE;
  153. }
  154. //
  155. // TC6807AF_Authenticate
  156. //
  157. /////////////////////////////////////////////////////////////////////
  158. BOOL TC6807AF_Authenticate( WORD wFunction, BYTE * pbyDATA )
  159. {
  160. switch ( wFunction )
  161. {
  162. case TC6807AF_GET_CHALLENGE:
  163. return tc6807af_GetChallengeData( pbyDATA );
  164. case TC6807AF_SEND_CHALLENGE:
  165. return tc6807af_SendChallengeData( pbyDATA );
  166. case TC6807AF_GET_RESPONSE:
  167. return tc6807af_GetResponseData( pbyDATA );
  168. case TC6807AF_SEND_RESPONSE:
  169. return tc6807af_SendResponseData( pbyDATA );
  170. case TC6807AF_SEND_DISK_KEY:
  171. return tc6807af_SendDiskKeyData( pbyDATA );
  172. case TC6807AF_SEND_TITLE_KEY:
  173. return tc6807af_SendTitleKeyData( pbyDATA );
  174. case TC6807AF_SET_DECRYPTION_MODE:
  175. return tc6807af_SetDecryptionMode( pbyDATA );
  176. }
  177. return FALSE;
  178. }
  179. /******************************************************************************/
  180. /******************* STATIC FUNCTIONS IMPLEMENTATION **************************/
  181. /******************************************************************************/
  182. //
  183. // tc6807af_GetChallengeData
  184. //
  185. /////////////////////////////////////////////////////////////////////
  186. BOOL tc6807af_GetChallengeData( BYTE * CHG )
  187. {
  188. MonoOutStr( " [DEC_RAND:" );
  189. if ( !tc6807af_NewCommand( DEC_RAND ) )
  190. return FALSE;
  191. CHG[0] = tc6807af_ReadReg( CHGG_0 );
  192. CHG[1] = tc6807af_ReadReg( CHGG_1 );
  193. CHG[2] = tc6807af_ReadReg( CHGG_2 );
  194. CHG[3] = tc6807af_ReadReg( CHGG_3 );
  195. CHG[4] = tc6807af_ReadReg( CHGG_4 );
  196. CHG[5] = tc6807af_ReadReg( CHGG_5 );
  197. CHG[6] = tc6807af_ReadReg( CHGG_6 );
  198. CHG[7] = tc6807af_ReadReg( CHGG_7 );
  199. CHG[8] = tc6807af_ReadReg( CHGG_8 );
  200. CHG[9] = tc6807af_ReadReg( CHGG_9 );
  201. MonoOutStr( "] " );
  202. return TRUE;
  203. }
  204. //
  205. // tc6807af_SendChallengeData
  206. //
  207. /////////////////////////////////////////////////////////////////////
  208. BOOL tc6807af_SendChallengeData( BYTE * CHG )
  209. {
  210. MonoOutStr( " [DEC_AUTH:" );
  211. tc6807af_WriteReg( CHGG_0, CHG[0] );
  212. tc6807af_WriteReg( CHGG_1, CHG[1] );
  213. tc6807af_WriteReg( CHGG_2, CHG[2] );
  214. tc6807af_WriteReg( CHGG_3, CHG[3] );
  215. tc6807af_WriteReg( CHGG_4, CHG[4] );
  216. tc6807af_WriteReg( CHGG_5, CHG[5] );
  217. tc6807af_WriteReg( CHGG_6, CHG[6] );
  218. tc6807af_WriteReg( CHGG_7, CHG[7] );
  219. tc6807af_WriteReg( CHGG_8, CHG[8] );
  220. tc6807af_WriteReg( CHGG_9, CHG[9] );
  221. if ( !tc6807af_NewCommand( DEC_AUTH ) )
  222. return FALSE;
  223. return TRUE;
  224. }
  225. //
  226. // tc6807af_GetResponseData
  227. //
  228. /////////////////////////////////////////////////////////////////////
  229. BOOL tc6807af_GetResponseData( BYTE * RSP )
  230. {
  231. MonoOutStr( " [GetResponseData" );
  232. RSP[0] = tc6807af_ReadReg( RSPG_0 );
  233. RSP[1] = tc6807af_ReadReg( RSPG_1 );
  234. RSP[2] = tc6807af_ReadReg( RSPG_2 );
  235. RSP[3] = tc6807af_ReadReg( RSPG_3 );
  236. RSP[4] = tc6807af_ReadReg( RSPG_4 );
  237. MonoOutStr( "] " );
  238. return TRUE;
  239. }
  240. //
  241. // tc6807af_SendResponseData
  242. //
  243. /////////////////////////////////////////////////////////////////////
  244. BOOL tc6807af_SendResponseData( BYTE * RSP )
  245. {
  246. MonoOutStr( " [DRV_AUTH:" );
  247. tc6807af_WriteReg( RSPG_0, RSP[0] );
  248. tc6807af_WriteReg( RSPG_1, RSP[1] );
  249. tc6807af_WriteReg( RSPG_2, RSP[2] );
  250. tc6807af_WriteReg( RSPG_3, RSP[3] );
  251. tc6807af_WriteReg( RSPG_4, RSP[4] );
  252. if ( !tc6807af_NewCommand( DRV_AUTH ) )
  253. return FALSE;
  254. return TRUE;
  255. }
  256. //
  257. // tc6807af_SendDiskKeyData
  258. //
  259. /////////////////////////////////////////////////////////////////////
  260. BOOL tc6807af_SendDiskKeyData( BYTE * pBuffer )
  261. {
  262. DWORD physAddress;
  263. DWORD dwTimeout = 10000;
  264. BYTE byValue;
  265. int i;
  266. MonoOutStr( " [DEC_DKY:" );
  267. //tc6807af_WriteReg( CNT_1, ENBERR | ENBEND | RQ1 );
  268. tc6807af_WriteReg( CNT_1, RQ1 );
  269. tc6807af_WriteReg( CNT_2, SCR1 | EB2 );
  270. tc6807af_WriteReg( COM, DEC_DKY );
  271. tc6807af_WriteReg( CNT_1, RQ2|RQ1 );
  272. tc6807af_WriteReg( CNT_2, SCR1 | EB2|EB1 );
  273. MonoOutStr( "DiskKey:" );
  274. MonoOutULongHex( *((DWORD *)pBuffer) );
  275. MonoOutStr( " pBuffer:" );
  276. MonoOutULongHex( (DWORD)pBuffer );
  277. // Send one sector
  278. #ifdef VTOOLSD
  279. CopyPageTable( (DWORD)pBuffer >> 12, 1, (PVOID*)&physAddress, 0 );
  280. physAddress = (physAddress & 0xfffff000) + (((DWORD)pBuffer) & 0xfff);
  281. #else
  282. physAddress = (DWORD)pBuffer;
  283. #endif
  284. FPGA_Set( FPGA_SECTOR_START );
  285. for ( i=0; i<32; i++ )
  286. {
  287. if ( !BMA_Send( (DWORD *) (physAddress+i*64), 64 ) )
  288. return FALSE;
  289. dwTimeout = 10000;
  290. while ( !BMA_Complete() )
  291. {
  292. //dvd_SetRequestEnable();
  293. if ( !(--dwTimeout) )
  294. {
  295. MonoOutStr( " BMA did not complete " );
  296. return FALSE;
  297. }
  298. }
  299. dvd_SetRequestEnable();
  300. }
  301. FPGA_Clear( FPGA_SECTOR_START );
  302. //tc6807af_WriteReg( CNT_1, CLINT );
  303. dwTimeout = 400000;
  304. while ( --dwTimeout )
  305. {
  306. byValue = tc6807af_ReadReg( COM );
  307. if ( byValue & END )
  308. {
  309. //tc6807af_WriteReg( CNT_1, CLINT | RQ1 );
  310. if ( byValue & ERR )
  311. {
  312. //tc6807af_WriteReg( CNT_1, CLINT | RQ1 );
  313. MonoOutStr( "ERR] " );
  314. return FALSE;
  315. }
  316. MonoOutStr( "End] " );
  317. return TRUE;
  318. }
  319. }
  320. //tc6807af_WriteReg( CNT_1, CLINT | RQ1 );
  321. MonoOutStr( "Timeout] " );
  322. return FALSE;
  323. }
  324. //
  325. // tc6807af_SendTilteKeyData
  326. //
  327. /////////////////////////////////////////////////////////////////////
  328. BOOL tc6807af_SendTitleKeyData( BYTE * ETK )
  329. {
  330. DWORD dwTimeout = 100000;
  331. BYTE byValue;
  332. MonoOutStr( " [DEC_DTK:" );
  333. tc6807af_WriteReg( ETKG_0, ETK[0] );
  334. tc6807af_WriteReg( ETKG_1, ETK[1] );
  335. tc6807af_WriteReg( ETKG_2, ETK[2] );
  336. tc6807af_WriteReg( ETKG_3, ETK[3] );
  337. tc6807af_WriteReg( ETKG_4, ETK[4] );
  338. tc6807af_WriteReg( ETKG_5, ETK[5] );
  339. tc6807af_WriteReg( COM, NOP );
  340. tc6807af_WriteReg( COM, DEC_DTK );
  341. while ( --dwTimeout )
  342. {
  343. byValue = tc6807af_ReadReg( COM );
  344. if ( byValue & END )
  345. {
  346. if ( byValue & ERR )
  347. {
  348. MonoOutStr( "ERR] " );
  349. return FALSE;
  350. }
  351. MonoOutStr( "End] " );
  352. return TRUE;
  353. }
  354. }
  355. MonoOutStr( "Timeout] " );
  356. return FALSE;
  357. }
  358. //
  359. // tc6807af_SetDecryptionMode
  360. //
  361. /////////////////////////////////////////////////////////////////////
  362. BOOL tc6807af_SetDecryptionMode( BYTE * SR_FLAG )
  363. {
  364. DWORD dwTimeout = 100000;
  365. BYTE byValue;
  366. MonoOutStr( " [DEC_DT:" );
  367. if ( *SR_FLAG )
  368. {
  369. //tc6807af_WriteReg( CNT_2, EB2 );
  370. tc6807af_WriteReg( COM, NOP );
  371. tc6807af_WriteReg( COM, DEC_DT );
  372. MonoOutStr( "] " );
  373. return TRUE;
  374. while ( --dwTimeout )
  375. {
  376. byValue = tc6807af_ReadReg( COM );
  377. if ( byValue & END )
  378. {
  379. if ( byValue & ERR )
  380. {
  381. MonoOutStr( "ERR] " );
  382. return FALSE;
  383. }
  384. MonoOutStr( "End] " );
  385. return TRUE;
  386. }
  387. }
  388. MonoOutStr( "Timeout] " );
  389. return FALSE;
  390. }
  391. else
  392. {
  393. tc6807af_WriteReg( CNT_2, SCR1 | EB2 | EB1 | THR );
  394. MonoOutStr( "Pass Through] " );
  395. return TRUE;
  396. }
  397. }
  398. /******************************************************************************/
  399. /******************* LOW LEVEL FUNCTIONS IMPLEMENTATION ***********************/
  400. /******************************************************************************/
  401. //
  402. // tc6807af_NewCommand
  403. //
  404. /////////////////////////////////////////////////////////////////////
  405. BOOL tc6807af_NewCommand( BYTE Command )
  406. {
  407. DWORD dwTimeout = 10000;
  408. BYTE byValue;
  409. tc6807af_WriteReg( COM, Command );
  410. if ( (Command == NOP) || (Command == DEC_RAND) )
  411. return TRUE;
  412. while ( --dwTimeout )
  413. {
  414. byValue = tc6807af_ReadReg( COM );
  415. if ( byValue & END )
  416. {
  417. if ( byValue & ERR )
  418. {
  419. MonoOutStr( "ERR] " );
  420. return FALSE;
  421. }
  422. MonoOutStr( "End] " );
  423. return TRUE;
  424. }
  425. }
  426. MonoOutStr( "Timeout] " );
  427. return FALSE;
  428. }
  429. //
  430. // tc6807af_WriteReg
  431. //
  432. /////////////////////////////////////////////////////////////////////
  433. void tc6807af_WriteReg( BYTE byReg, BYTE byValue )
  434. {
  435. BRD_WriteByte( gdwIndex, byReg );
  436. BRD_WriteByte( gdwData, byValue );
  437. }
  438. //
  439. // tc6807af_ReadReg
  440. //
  441. /////////////////////////////////////////////////////////////////////
  442. BYTE tc6807af_ReadReg( BYTE byReg )
  443. {
  444. BRD_WriteByte( gdwIndex, byReg );
  445. return BRD_ReadByte( gdwData );
  446. }