/* main function returns non-zero if any function fails. */ int __cdecl wmain( int argc, wchar_t *argv[ ], wchar_t *envp[ ] ) { BOOL bResult; DWORD dwError = 0; DWORD i; DWORD dwVersion; HNDAS hNDAS = NULL; NDASCOMM_CONNECTION_INFO ci; BYTE DeviceID[6]; DWORD dwUnitNo; PBYTE Buffer; DWORD dwBufferLen; BYTE data[512]; /* 1 sector sized buffer */ INT64 i64Location; UINT ui64SectorCount; NDASCOMM_IDE_REGISTER IdeRegister; BYTE pbData[8]; DWORD cbData = sizeof(pbData); NDASCOMM_VCMD_PARAM param_vcmd; UINT32 uiTimeValue; BOOL bEnableTimer; NDAS_DEVICE_HARDWARE_INFO dinfo; BYTE vid; /* simple check parameter */ if(2 != argc) { wprintf( L"usage : apitest.exe ID-KEY\n" L"\n" L"ID-KEY : 20 chars of id and 5 chars of key of the NDAS Device ex(01234ABCDE56789FGHIJ13579)\n" L"ex : apitest.exe 01234ABCDE56789FGHIJ13579\n" ); wprintf(L"\n"); return -1; } wprintf(L"\n\n\n* Start NDASComm API test on the NDAS Device : %s\n", argv[1]); wprintf(L"* Initialize NdasComm library : NdasCommInitialize()\n"); API_CALL(NdasCommInitialize()); wprintf(L"* Get API Version : NdasCommGetAPIVersion()\n"); API_CALL_JMP(dwVersion = NdasCommGetAPIVersion(), out); wprintf(L"- Version : Major %d, Minor %d\n", (int)LOWORD(dwVersion), (int)HIWORD(dwVersion)); ////////////////////////////////////////////////////////////// //#define TEST_SET_PASSWORD #ifdef TEST_SET_PASSWORD wprintf(L"### TESTING 'Set user password' ###\n"); ZeroMemory(&ci, sizeof(ci)); ci.Size = sizeof(NDASCOMM_CONNECTION_INFO); ci.AddressType = NDASCOMM_CIT_NDAS_IDW; /* wide char set */ ci.UnitNo = 0; /* Use first Unit Device */ ci.WriteAccess = TRUE; /* Connect with read-write privilege */ ci.Protocol = NDASCOMM_TRANSPORT_LPX; /* Use LPX protocol */ CopyMemory( ci.OEMCode.Bytes, NdasOemCodeX1, sizeof(NdasOemCodeX1)); /* HASH_KEY_USER_X1 */ /* Use default password */ CopyMemory( ci.PrivilegedOEMCode.Bytes, NdasPrivOemCodeX1, sizeof(NdasPrivOemCodeX1)); /* HASH_KEY_SUPER_X1 */ /* Log in as super user */ ci.LoginType = NDASCOMM_LOGIN_TYPE_NORMAL; /* Normal operations */ wcsncpy(ci.NdasIdW.Id, argv[1], 20); /* ID */ wcsncpy(ci.NdasIdW.Key, argv[1] +20, 5); /* Key */ wprintf(L"* Connect to the NDAS Device : NdasCommConnect()\n"); API_CALL_JMP( hNDAS = NdasCommConnect( &ci, 0 /* synchronous mode */, NULL /* no connection hint */ ), out); wprintf(L"* Setting user password : NdasCommVendorCommand()\n"); *(UINT64 *)param_vcmd.SET_SUPERVISOR_PW.SupervisorPassword = HASH_KEY_SUPER_X1; bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_supervisor_pw, ¶m_vcmd, NULL, 0, NULL, 0); if(!bResult) { if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError()) { API_CALL_JMP(FALSE && "NdasCommVendorCommand", out); } wprintf(L"- Not supported for this Hardware version\n"); } *(UINT64 *)param_vcmd.SET_USER_PW.UserPassword = HASH_KEY_USER_X1; bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_user_pw, ¶m_vcmd, NULL, 0, NULL, 0); if(!bResult) { if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError()) { API_CALL_JMP(FALSE && "NdasCommVendorCommand", out); } wprintf(L"- Not supported for this Hardware version\n"); } #define TEST_SET_PASSWORD_RESET #ifdef TEST_SET_PASSWORD_RESET wprintf(L"* Resetting : NdasCommVendorCommand()\n"); bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_reset, ¶m_vcmd, NULL, 0, NULL, 0); if(!bResult) { if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError()) { API_CALL_JMP(FALSE && "NdasCommVendorCommand", out); } wprintf(L"- Not supported for this Hardware version\n"); } #else wprintf(L"* Disconnect the connection from the NDAS Device : NdasCommDisconnect()\n"); API_CALL_JMP(NdasCommDisconnect(hNDAS), out); #endif //TEST_SET_PASSWORD_RESET wprintf(L"### TESTED 'Set user passoword' ###\n"); #endif //TEST_SET_PASSWORD ////////////////////////////////////////////////////////////// wprintf(L"* Initialize connection info to create connection to the NDAS Device\n"); ZeroMemory(&ci, sizeof(ci)); ci.Size = sizeof(NDASCOMM_CONNECTION_INFO); ci.AddressType = NDASCOMM_CIT_NDAS_IDW; /* wide char set */ ci.UnitNo = 0; /* Use first Unit Device */ ci.WriteAccess = TRUE; /* Connect with read-write privilege */ ci.Protocol = NDASCOMM_TRANSPORT_LPX; /* Use LPX protocol */ ci.OEMCode.UI64Value = 0; /* Use default password */ ci.PrivilegedOEMCode.UI64Value = 0; /* Log in as normal user */ ci.LoginType = NDASCOMM_LOGIN_TYPE_NORMAL; /* Normal operations */ wcsncpy(ci.Address.NdasIdW.Id, argv[1], 20); /* ID */ wcsncpy(ci.Address.NdasIdW.Key, argv[1] + 20, 5); /* Key */ wprintf(L"* Connect to the NDAS Device : NdasCommConnect()\n"); API_CALL_JMP( hNDAS = NdasCommConnect(&ci), out ); wprintf(L"* Retrieve NDAS Device ID & unit number : NdasCommGetDeviceID()\n"); API_CALL_JMP(NdasCommGetDeviceID(hNDAS, NULL, DeviceID, &dwUnitNo, &vid), out); wprintf(L"- DeviceID : %02X%02X%02X%02X%02X%02X, Unit No. : %d\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5], (int)dwUnitNo); wprintf(L"* Retrieve the address of the host attached to the NDAS Device : NdasCommGetHostAddress()\n"); API_CALL_JMP(NdasCommGetHostAddress(hNDAS, NULL, &dwBufferLen), out); wprintf(L"- buffer length : %d\n", dwBufferLen); Buffer = malloc(dwBufferLen); API_CALL_JMP(NdasCommGetHostAddress(hNDAS, Buffer, &dwBufferLen), out); wprintf(L"- Host Address : "); for(i = 0 ; i < dwBufferLen; i++) { wprintf(L"%02X", (UINT)Buffer[i]); } wprintf(L"\n"); free(Buffer); ui64SectorCount = 1; i64Location = 0; wprintf(L"* Read %d sector(s) of data from Address %d : NdasCommBlockDeviceRead()\n", ui64SectorCount, i64Location); API_CALL_JMP(NdasCommBlockDeviceRead(hNDAS, i64Location, ui64SectorCount, data), out); i64Location = 1; wprintf(L"* Write %d sector(s) of data to Address %d : NdasCommBlockDeviceWriteSafeBuffer()\n", ui64SectorCount, i64Location); API_CALL_JMP(NdasCommBlockDeviceWriteSafeBuffer(hNDAS, i64Location, ui64SectorCount, data), out); ui64SectorCount = 2; i64Location = 2; wprintf(L"* Verify %d sector(s) from Address %d : NdasCommBlockDeviceVerify()\n", ui64SectorCount, i64Location); API_CALL_JMP(NdasCommBlockDeviceVerify(hNDAS, i64Location, ui64SectorCount), out); IdeRegister.command.command = 0xEC; /* WIN_IDENTIFY */ IdeRegister.device.dev = 0; wprintf(L"* Identify the NDAS Unit Device : NdasCommIdeCommand()\n"); API_CALL_JMP(NdasCommIdeCommand(hNDAS, &IdeRegister, NULL, 0, data, 512), out); /* data[] now has 512 bytes of identified data as per ANSI NCITS ATA6 rev.1b spec */ ZeroMemory(&dinfo, sizeof(NDAS_DEVICE_HARDWARE_INFO)); dinfo.Size = sizeof(NDAS_DEVICE_HARDWARE_INFO); API_CALL_JMP(NdasCommGetDeviceHardwareInfo(hNDAS,&dinfo), out); wprintf(L"Hardware Version : %d\n", dinfo.HardwareVersion); wprintf(L"* get standby timer : NdasCommVendorCommand()\n"); bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_get_ret_time, ¶m_vcmd, NULL, 0, NULL, 0); if(!bResult) { if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != GetLastError()) { API_CALL_JMP(FALSE && "NdasCommVendorCommand", out); } wprintf(L"- Not supported for this Hardware version\n"); } else { uiTimeValue = param_vcmd.GET_STANDBY_TIMER.TimeValue; bEnableTimer = param_vcmd.GET_STANDBY_TIMER.EnableTimer; wprintf(L"- standby timer : %d, enable : %d\n", uiTimeValue, bEnableTimer); param_vcmd.SET_STANDBY_TIMER.TimeValue = uiTimeValue; param_vcmd.SET_STANDBY_TIMER.EnableTimer = bEnableTimer ? 0 : 1; wprintf(L"* set standby timer : NdasCommVendorCommand()\n"); API_CALL_JMP(NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_ret_time, ¶m_vcmd, NULL, 0, NULL, 0), out); uiTimeValue = param_vcmd.SET_STANDBY_TIMER.TimeValue; bEnableTimer = param_vcmd.SET_STANDBY_TIMER.EnableTimer; wprintf(L"- standby timer : %d, enable : %d\n", uiTimeValue, bEnableTimer); } wprintf(L"* Disconnect the connection from the NDAS Device : NdasCommDisconnect()\n"); API_CALL_JMP(NdasCommDisconnect(hNDAS), out); wprintf(L"* Uninitialize NDASComm API : NdasCommUninitialize()\n"); out: if(hNDAS) { NdasCommDisconnect(hNDAS); hNDAS = NULL; } API_CALL(NdasCommUninitialize()); return GetLastError(); }
BOOL print_ndas_unitdevice_info( const NDASCOMM_CONNECTION_INFO* pci) { BOOL success; int i; HNDAS hNdas = NULL; NDAS_UNITDEVICE_HARDWARE_INFO uinfo; NDASCOMM_IDE_REGISTER idereg; struct hd_driveid ideInfo; UINT64 sectors; TCHAR szBuffer[100]; const DWORD cchBuffer = sizeof(szBuffer) / sizeof(szBuffer[0]); /* NdasCommGetUnitDeviceStat requires NORMAL login type */ _ASSERTE(NDASCOMM_LOGIN_TYPE_NORMAL == pci->LoginType); API_CALL_JMP(fail, hNdas = NdasCommConnect(pci)); /* Unit Device Information */ ZeroMemory(&uinfo, sizeof(NDAS_UNITDEVICE_HARDWARE_INFO)); uinfo.Size = sizeof(NDAS_UNITDEVICE_HARDWARE_INFO); API_CALL_JMP(fail, NdasCommGetUnitDeviceHardwareInfo(hNdas, &uinfo)); sectors = _tprintf(_T(" Sector count : %I64d\n"), uinfo.SectorCount.QuadPart); _tprintf(_T(" Supports LBA : %s\n"), bool_string(uinfo.LBA)); _tprintf(_T(" Supports LBA48 : %s\n"), bool_string(uinfo.LBA48)); _tprintf(_T(" Supports PIO : %s\n"), bool_string(uinfo.PIO)); _tprintf(_T(" Supports DMA : %s\n"), bool_string(uinfo.DMA)); _tprintf(_T(" Supports UDMA : %s\n"), bool_string(uinfo.UDMA)); _tprintf(_T(" Model : %s\n"), uinfo.Model); _tprintf(_T(" Firmware Rev : %s\n"), uinfo.FirmwareRevision); _tprintf(_T(" Serial number : %s\n"), uinfo.SerialNumber); _tprintf(_T(" Media type : %s\n"), media_type_string(uinfo.MediaType)); _tprintf(_T("\n")); /* Additional IDE information using WIN_IDENTIFY command */ idereg.device.lba_head_nr = 0; idereg.device.dev = (0 == pci->UnitNo) ? 0 : 1; idereg.device.lba = 0; idereg.command.command = WIN_IDENTIFY; API_CALL_JMP(fail, NdasCommIdeCommand(hNdas, &idereg, NULL, 0, (PBYTE)&ideInfo, sizeof(ideInfo))); _tprintf(_T(" FLUSH CACHE : Supports - %s, Enabled - %s\n"), bool_string(ideInfo.command_set_2 & 0x1000), bool_string(ideInfo.cfs_enable_2 & 0x1000)); _tprintf(_T(" FLUSH CACHE EXT : Supports - %s, Enabled - %s\n"), bool_string(ideInfo.command_set_2 & 0x2000), bool_string(ideInfo.cfs_enable_2 & 0x2000)); /* Check Ultra DMA mode */ for (i = 7; i >= 0; i--) { if (ideInfo.dma_ultra & (0x01 << i)) { _tprintf(_T(" Ultra DMA mode: supports up to UDMA mode %d\n"), i); break; } } for (i = 7; i >= 0; i--) { if (ideInfo.dma_ultra & (0x01 << (i + 8))) { _tprintf(_T(" Current Ultra DMA mode: %d\n"), i); break; } } if (i < 0) { _tprintf(_T(" Ultra DMA mode is not selected\n")); } /* Check DMA mode */ for (i = 2; i >= 0; i--) { if (ideInfo.dma_mword & (0x01 << i)) { _tprintf(_T(" DMA mode: supports up to DMA mode %d\n"), i); break; } } for (i = 2; i >= 0; i--) { if (ideInfo.dma_mword & (0x01 << (i + 8))) { _tprintf(_T(" DMA mode %d selected\n"), i); break; } } if (i < 0) { _tprintf(_T(" DMA mode is not selected\n")); } _tprintf(_T("\n")); success = TRUE; fail: if (hNdas) { API_CALL(NdasCommDisconnect(hNdas)); } return success; }
int CpDeviceStandby(int argc, _TCHAR* argv[]) { HNDAS hNDAS = NULL; NDASCOMM_CONNECTION_INFO ci; BYTE DeviceID[6]; DWORD dwUnitNo; NDASCOMM_IDE_REGISTER IdeRegister; // // Get arguments. // if(argc < 2) { _ftprintf(stderr, _T("ERROR: More parameter needed.\n")); return -1; } _ftprintf(stdout, _T("Starting the operation...\n")); SetLastError(0); API_CALL(NdasCommInitialize()); // // Connect and login // ZeroMemory(&ci, sizeof(ci)); ci.Size = sizeof(NDASCOMM_CONNECTION_INFO); ci.AddressType = NDASCOMM_CIT_NDAS_ID; /* Use NDAS ID*/ ci.UnitNo = 0; /* Use first Unit Device */ ci.WriteAccess = TRUE; /* Connect with read-write privilege */ ci.Protocol = NDASCOMM_TRANSPORT_LPX; /* Use LPX protocol */ ci.OEMCode.UI64Value = 0; /* Use default password */ ci.PrivilegedOEMCode.UI64Value = 0; /* Log in as normal user */ ci.LoginType = NDASCOMM_LOGIN_TYPE_NORMAL; /* Normal operations */ _tcsncpy(ci.Address.NdasId.Id, argv[0], 20); /* ID */ _tcsncpy(ci.Address.NdasId.Key, argv[1], 5); /* Key */ API_CALL_JMP( hNDAS = NdasCommConnect(&ci), out); // // Display NDAS device info // API_CALL_JMP(NdasCommGetDeviceID(hNDAS, DeviceID, &dwUnitNo), out); _tprintf(L"DeviceID : %02X%02X%02X%02X%02X%02X, Unit No. : %d\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5], (int)dwUnitNo); // // Request the NDAS device to enter Standby mode // ZeroMemory(&IdeRegister, sizeof(NDASCOMM_IDE_REGISTER)); IdeRegister.command.command = 0xE0; /* WIN_STANDBYNOW1 */ IdeRegister.device.dev = 0; API_CALL_JMP(NdasCommIdeCommand(hNDAS, &IdeRegister, NULL, 0, NULL, 0), out); out: if(GetLastError()) { _ftprintf(stdout, _T("Error! Code:%08lx\n"), GetLastError()); } if(hNDAS) { NdasCommDisconnect(hNDAS); hNDAS = NULL; } NdasCommUninitialize(); _ftprintf(stdout, _T("Finished the operation.\n")); return GetLastError(); }
/* main function returns non-zero if any function fails. */ int __cdecl main(int argc, char *argv[]) { DWORD dwError = 0; BOOL bResult; HNDAS hNdas; BYTE data[512]; // 1 sector sized buffer // simple check parameter if(2 != argc) { printf( "usage : apitest.exe ID-KEY\n" "\n" "ID-KEY : 20 chars of id and 5 chars of key of the NDAS Device ex(01234ABCDE56789FGHIJ13579)\n" "ex : apitest.exe 01234ABCDE56789FGHIJ13579\n" ); printf("\n"); return -1; } printf("\n\n\n* Start NDASComm API test on the NDAS Device : %s\n", argv[1]); printf("* Initialize NdasComm library : NdasCommInitialize()\n"); API_CALL(NdasCommInitialize()); DWORD dwVersion; printf("* Get API Version : NdasCommGetAPIVersion()\n"); API_CALL(dwVersion = NdasCommGetAPIVersion()); printf("- Version : Major %d, Minor %d\n", (int)LOWORD(dwVersion), (int)HIWORD(dwVersion)); printf("* Initialize connection info to create connection to the NDAS Device\n"); NDASCOMM_CONNECTION_INFO ci; ZeroMemory(&ci, sizeof(ci)); ci.address_type = NDASCOMM_CONNECTION_INFO_TYPE_ID_A; // ASCII char set ci.UnitNo = 0; // Use first Unit Device ci.bWriteAccess = TRUE; // Connect with read-write privilege ci.protocol = NDASCOMM_TRANSPORT_LPX; // Use LPX protocol ci.ui64OEMCode = 0; // Use default password ci.bSupervisor = FALSE; // Log in as normal user ci.login_type = NDASCOMM_LOGIN_TYPE_NORMAL; // Normal operations strncpy(ci.DeviceIDA.szDeviceStringId, argv[1], 20); // ID strncpy(ci.DeviceIDA.szDeviceStringKey, argv[1] +20, 5); // Key HNDAS hNDAS; printf("* Connect to the NDAS Device : NdasCommConnect()\n"); API_CALL( hNDAS = NdasCommConnect( &ci, 0 /* synchronous mode */, NULL /* no connection hint */ ) ); BYTE DeviceID[6]; DWORD UnitNo; printf("* Retrieve NDAS Device ID & unit number : NdasCommGetDeviceID()\n"); API_CALL(NdasCommGetDeviceID(hNDAS, DeviceID, &UnitNo)); printf("- DeviceID : %02X%02X%02X%02X%02X%02X, Unit No. : %d\n", DeviceID[0], DeviceID[1], DeviceID[2], DeviceID[3], DeviceID[4], DeviceID[5], (int)UnitNo); PBYTE Buffer; DWORD BufferLen; printf("* Retrieve the address of the host attached to the NDAS Device : NdasCommGetHostAddress()\n"); API_CALL(NdasCommGetHostAddress(hNDAS, NULL, &BufferLen)); printf("- buffer length : %d\n", BufferLen); Buffer = new BYTE[BufferLen]; API_CALL(NdasCommGetHostAddress(hNDAS, Buffer, &BufferLen)); printf("- Host Address : "); for(DWORD i = 0 ; i < BufferLen; i++) { printf("%02X", (UINT)Buffer[i]); } printf("\n"); delete [] Buffer; INT64 i64Location; UINT ui64SectorCount; ui64SectorCount = 1; i64Location = 0; printf("* Read %d sector(s) of data from Address %d : NdasCommBlockDeviceRead()\n", ui64SectorCount, i64Location); API_CALL(NdasCommBlockDeviceRead(hNDAS, i64Location, ui64SectorCount, data)); i64Location = 1; printf("* Write %d sector(s) of data to Address %d : NdasCommBlockDeviceWriteSafeBuffer()\n", ui64SectorCount, i64Location); API_CALL(NdasCommBlockDeviceWriteSafeBuffer(hNDAS, i64Location, ui64SectorCount, data)); ui64SectorCount = 2; i64Location = 2; printf("* Verify %d sector(s) from Address %d : NdasCommBlockDeviceVerify()\n", ui64SectorCount, i64Location); API_CALL(NdasCommBlockDeviceVerify(hNDAS, i64Location, ui64SectorCount)); NDASCOMM_IDE_REGISTER IdeRegister; IdeRegister.command.command = 0xEC; // WIN_IDENTIFY printf("* Identify the NDAS Unit Device : NdasCommIdeCommand()\n"); API_CALL(NdasCommIdeCommand(hNDAS, &IdeRegister, NULL, 0, data, 512)); // data[] now has 512 bytes of identified data as per ANSI NCITS ATA6 rev.1b spec BYTE pbData[8]; const DWORD cbData = sizeof(pbData); API_CALL(NdasCommGetDeviceInfo(hNDAS,ndascomm_handle_info_hw_version, pbData, cbData)); printf("Hardware Version : %d\n", *(BYTE*)pbData); NDASCOMM_VCMD_PARAM param_vcmd; printf("* get standby timer : NdasCommVendorCommand()\n"); bResult = NdasCommVendorCommand(hNDAS, ndascomm_vcmd_get_ret_time, ¶m_vcmd, NULL, 0, NULL, 0); if(!bResult) { if(NDASCOMM_ERROR_HARDWARE_UNSUPPORTED != ::GetLastError()) { API_CALL(FALSE && "NdasCommVendorCommand"); } printf("- Not supported for this Hardware version\n"); } else { UINT32 TimeValue = param_vcmd.GET_STANDBY_TIMER.TimeValue; BOOL EnableTimer = param_vcmd.GET_STANDBY_TIMER.EnableTimer; printf("- standby timer : %d, enable : %d\n", TimeValue, EnableTimer); param_vcmd.SET_STANDBY_TIMER.TimeValue = TimeValue; param_vcmd.SET_STANDBY_TIMER.EnableTimer = EnableTimer ? 0 : 1; printf("* set standby timer : NdasCommVendorCommand()\n"); API_CALL(NdasCommVendorCommand(hNDAS, ndascomm_vcmd_set_ret_time, ¶m_vcmd, NULL, 0, NULL, 0)); TimeValue = param_vcmd.SET_STANDBY_TIMER.TimeValue; EnableTimer = param_vcmd.SET_STANDBY_TIMER.EnableTimer; printf("- standby timer : %d, enable : %d\n", TimeValue, EnableTimer); } printf("* Disconnect the connection from the NDAS Device : NdasCommDisconnect()\n"); API_CALL(NdasCommDisconnect(hNDAS)); printf("* Uninitialize NDASComm API : NdasCommUninitialize()\n"); API_CALL(NdasCommUninitialize()); return 0; }