AtaBus.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712
  1. /** @file
  2. This file implements protocol interfaces for ATA bus driver.
  3. This file implements protocol interfaces: Driver Binding protocol,
  4. Block IO protocol and DiskInfo protocol.
  5. Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
  6. SPDX-License-Identifier: BSD-2-Clause-Patent
  7. **/
  8. #include "AtaBus.h"
  9. //
  10. // ATA Bus Driver Binding Protocol Instance
  11. //
  12. EFI_DRIVER_BINDING_PROTOCOL gAtaBusDriverBinding = {
  13. AtaBusDriverBindingSupported,
  14. AtaBusDriverBindingStart,
  15. AtaBusDriverBindingStop,
  16. 0x10,
  17. NULL,
  18. NULL
  19. };
  20. //
  21. // Template for ATA Child Device.
  22. //
  23. ATA_DEVICE gAtaDeviceTemplate = {
  24. ATA_DEVICE_SIGNATURE, // Signature
  25. NULL, // Handle
  26. { // BlockIo
  27. EFI_BLOCK_IO_PROTOCOL_REVISION,
  28. NULL,
  29. AtaBlockIoReset,
  30. AtaBlockIoReadBlocks,
  31. AtaBlockIoWriteBlocks,
  32. AtaBlockIoFlushBlocks
  33. },
  34. { // BlockIo2
  35. NULL,
  36. AtaBlockIoResetEx,
  37. AtaBlockIoReadBlocksEx,
  38. AtaBlockIoWriteBlocksEx,
  39. AtaBlockIoFlushBlocksEx
  40. },
  41. { // BlockMedia
  42. 0, // MediaId
  43. FALSE, // RemovableMedia
  44. TRUE, // MediaPresent
  45. FALSE, // LogicPartition
  46. FALSE, // ReadOnly
  47. FALSE, // WritingCache
  48. 0x200, // BlockSize
  49. 0, // IoAlign
  50. 0, // LastBlock
  51. 0, // LowestAlignedLba
  52. 1 // LogicalBlocksPerPhysicalBlock
  53. },
  54. { // DiskInfo
  55. EFI_DISK_INFO_IDE_INTERFACE_GUID,
  56. AtaDiskInfoInquiry,
  57. AtaDiskInfoIdentify,
  58. AtaDiskInfoSenseData,
  59. AtaDiskInfoWhichIde
  60. },
  61. NULL, // DevicePath
  62. {
  63. AtaStorageSecurityReceiveData,
  64. AtaStorageSecuritySendData
  65. },
  66. NULL, // AtaBusDriverData
  67. 0, // Port
  68. 0, // PortMultiplierPort
  69. { 0, }, // Packet
  70. {{ 0}, }, // Acb
  71. NULL, // Asb
  72. FALSE, // UdmaValid
  73. FALSE, // Lba48Bit
  74. NULL, // IdentifyData
  75. NULL, // ControllerNameTable
  76. {L'\0', }, // ModelName
  77. {NULL, NULL}, // AtaTaskList
  78. {NULL, NULL}, // AtaSubTaskList
  79. FALSE // Abort
  80. };
  81. /**
  82. Allocates an aligned buffer for ATA device.
  83. This function allocates an aligned buffer for the ATA device to perform
  84. ATA pass through operations. The alignment requirement is from ATA pass
  85. through interface.
  86. @param AtaDevice The ATA child device involved for the operation.
  87. @param BufferSize The request buffer size.
  88. @return A pointer to the aligned buffer or NULL if the allocation fails.
  89. **/
  90. VOID *
  91. AllocateAlignedBuffer (
  92. IN ATA_DEVICE *AtaDevice,
  93. IN UINTN BufferSize
  94. )
  95. {
  96. return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign);
  97. }
  98. /**
  99. Frees an aligned buffer for ATA device.
  100. This function frees an aligned buffer for the ATA device to perform
  101. ATA pass through operations.
  102. @param Buffer The aligned buffer to be freed.
  103. @param BufferSize The request buffer size.
  104. **/
  105. VOID
  106. FreeAlignedBuffer (
  107. IN VOID *Buffer,
  108. IN UINTN BufferSize
  109. )
  110. {
  111. if (Buffer != NULL) {
  112. FreeAlignedPages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));
  113. }
  114. }
  115. /**
  116. Release all the resources allocated for the ATA device.
  117. This function releases all the resources allocated for the ATA device.
  118. @param AtaDevice The ATA child device involved for the operation.
  119. **/
  120. VOID
  121. ReleaseAtaResources (
  122. IN ATA_DEVICE *AtaDevice
  123. )
  124. {
  125. ATA_BUS_ASYN_SUB_TASK *SubTask;
  126. ATA_BUS_ASYN_TASK *AtaTask;
  127. LIST_ENTRY *Entry;
  128. LIST_ENTRY *DelEntry;
  129. EFI_TPL OldTpl;
  130. FreeUnicodeStringTable (AtaDevice->ControllerNameTable);
  131. FreeAlignedBuffer (AtaDevice->Asb, sizeof (EFI_ATA_STATUS_BLOCK));
  132. FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));
  133. if (AtaDevice->DevicePath != NULL) {
  134. FreePool (AtaDevice->DevicePath);
  135. }
  136. OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
  137. if (!IsListEmpty (&AtaDevice->AtaSubTaskList)) {
  138. //
  139. // Free the Subtask list.
  140. //
  141. for(Entry = AtaDevice->AtaSubTaskList.ForwardLink;
  142. Entry != (&AtaDevice->AtaSubTaskList);
  143. ) {
  144. DelEntry = Entry;
  145. Entry = Entry->ForwardLink;
  146. SubTask = ATA_ASYN_SUB_TASK_FROM_ENTRY (DelEntry);
  147. RemoveEntryList (DelEntry);
  148. FreeAtaSubTask (SubTask);
  149. }
  150. }
  151. if (!IsListEmpty (&AtaDevice->AtaTaskList)) {
  152. //
  153. // Free the Subtask list.
  154. //
  155. for(Entry = AtaDevice->AtaTaskList.ForwardLink;
  156. Entry != (&AtaDevice->AtaTaskList);
  157. ) {
  158. DelEntry = Entry;
  159. Entry = Entry->ForwardLink;
  160. AtaTask = ATA_ASYN_TASK_FROM_ENTRY (DelEntry);
  161. RemoveEntryList (DelEntry);
  162. FreePool (AtaTask);
  163. }
  164. }
  165. gBS->RestoreTPL (OldTpl);
  166. FreePool (AtaDevice);
  167. }
  168. /**
  169. Registers an ATA device.
  170. This function allocates an ATA device structure for the ATA device specified by
  171. Port and PortMultiplierPort if the ATA device is identified as a valid one.
  172. Then it will create child handle and install Block IO and Disk Info protocol on
  173. it.
  174. @param AtaBusDriverData The parent ATA bus driver data structure.
  175. @param Port The port number of the ATA device.
  176. @param PortMultiplierPort The port multiplier port number of the ATA device.
  177. @retval EFI_SUCCESS The ATA device is successfully registered.
  178. @retval EFI_OUT_OF_RESOURCES There is not enough memory to allocate the ATA device
  179. and related data structures.
  180. @return Others Some error occurs when registering the ATA device.
  181. **/
  182. EFI_STATUS
  183. RegisterAtaDevice (
  184. IN OUT ATA_BUS_DRIVER_DATA *AtaBusDriverData,
  185. IN UINT16 Port,
  186. IN UINT16 PortMultiplierPort
  187. )
  188. {
  189. EFI_STATUS Status;
  190. ATA_DEVICE *AtaDevice;
  191. EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
  192. EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode;
  193. EFI_DEVICE_PATH_PROTOCOL *DevicePath;
  194. EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
  195. EFI_HANDLE DeviceHandle;
  196. AtaDevice = NULL;
  197. NewDevicePathNode = NULL;
  198. DevicePath = NULL;
  199. RemainingDevicePath = NULL;
  200. //
  201. // Build device path
  202. //
  203. AtaPassThru = AtaBusDriverData->AtaPassThru;
  204. Status = AtaPassThru->BuildDevicePath (AtaPassThru, Port, PortMultiplierPort, &NewDevicePathNode);
  205. if (EFI_ERROR (Status)) {
  206. goto Done;
  207. }
  208. DevicePath = AppendDevicePathNode (AtaBusDriverData->ParentDevicePath, NewDevicePathNode);
  209. if (DevicePath == NULL) {
  210. Status = EFI_OUT_OF_RESOURCES;
  211. goto Done;
  212. }
  213. DeviceHandle = NULL;
  214. RemainingDevicePath = DevicePath;
  215. Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);
  216. if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(RemainingDevicePath)) {
  217. Status = EFI_ALREADY_STARTED;
  218. FreePool (DevicePath);
  219. goto Done;
  220. }
  221. //
  222. // Allocate ATA device from the template.
  223. //
  224. AtaDevice = AllocateCopyPool (sizeof (ATA_DEVICE), &gAtaDeviceTemplate);
  225. if (AtaDevice == NULL) {
  226. Status = EFI_OUT_OF_RESOURCES;
  227. goto Done;
  228. }
  229. //
  230. // Initializes ATA device structures and allocates the required buffer.
  231. //
  232. AtaDevice->BlockIo.Media = &AtaDevice->BlockMedia;
  233. AtaDevice->BlockIo2.Media = &AtaDevice->BlockMedia;
  234. AtaDevice->AtaBusDriverData = AtaBusDriverData;
  235. AtaDevice->DevicePath = DevicePath;
  236. AtaDevice->Port = Port;
  237. AtaDevice->PortMultiplierPort = PortMultiplierPort;
  238. AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (EFI_ATA_STATUS_BLOCK));
  239. if (AtaDevice->Asb == NULL) {
  240. Status = EFI_OUT_OF_RESOURCES;
  241. goto Done;
  242. }
  243. AtaDevice->IdentifyData = AllocateAlignedBuffer (AtaDevice, sizeof (ATA_IDENTIFY_DATA));
  244. if (AtaDevice->IdentifyData == NULL) {
  245. Status = EFI_OUT_OF_RESOURCES;
  246. goto Done;
  247. }
  248. //
  249. // Initial Ata Task List
  250. //
  251. InitializeListHead (&AtaDevice->AtaTaskList);
  252. InitializeListHead (&AtaDevice->AtaSubTaskList);
  253. //
  254. // Report Status Code to indicate the ATA device will be enabled
  255. //
  256. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  257. EFI_PROGRESS_CODE,
  258. (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_ENABLE),
  259. AtaBusDriverData->ParentDevicePath
  260. );
  261. //
  262. // Try to identify the ATA device via the ATA pass through command.
  263. //
  264. Status = DiscoverAtaDevice (AtaDevice);
  265. if (EFI_ERROR (Status)) {
  266. goto Done;
  267. }
  268. //
  269. // Build controller name for Component Name (2) protocol.
  270. //
  271. Status = AddUnicodeString2 (
  272. "eng",
  273. gAtaBusComponentName.SupportedLanguages,
  274. &AtaDevice->ControllerNameTable,
  275. AtaDevice->ModelName,
  276. TRUE
  277. );
  278. if (EFI_ERROR (Status)) {
  279. goto Done;
  280. }
  281. Status = AddUnicodeString2 (
  282. "en",
  283. gAtaBusComponentName2.SupportedLanguages,
  284. &AtaDevice->ControllerNameTable,
  285. AtaDevice->ModelName,
  286. FALSE
  287. );
  288. if (EFI_ERROR (Status)) {
  289. goto Done;
  290. }
  291. //
  292. // Update to AHCI interface GUID based on device path node. The default one
  293. // is IDE interface GUID copied from template.
  294. //
  295. if (NewDevicePathNode->SubType == MSG_SATA_DP) {
  296. CopyGuid (&AtaDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid);
  297. }
  298. Status = gBS->InstallMultipleProtocolInterfaces (
  299. &AtaDevice->Handle,
  300. &gEfiDevicePathProtocolGuid,
  301. AtaDevice->DevicePath,
  302. &gEfiBlockIoProtocolGuid,
  303. &AtaDevice->BlockIo,
  304. &gEfiBlockIo2ProtocolGuid,
  305. &AtaDevice->BlockIo2,
  306. &gEfiDiskInfoProtocolGuid,
  307. &AtaDevice->DiskInfo,
  308. NULL
  309. );
  310. if (EFI_ERROR (Status)) {
  311. goto Done;
  312. }
  313. //
  314. // See if the ata device support trust computing feature or not.
  315. // If yes, then install Storage Security Protocol at the ata device handle.
  316. //
  317. if ((AtaDevice->IdentifyData->trusted_computing_support & BIT0) != 0) {
  318. DEBUG ((EFI_D_INFO, "Found TCG support in Port %x PortMultiplierPort %x\n", Port, PortMultiplierPort));
  319. Status = gBS->InstallProtocolInterface (
  320. &AtaDevice->Handle,
  321. &gEfiStorageSecurityCommandProtocolGuid,
  322. EFI_NATIVE_INTERFACE,
  323. &AtaDevice->StorageSecurity
  324. );
  325. if (EFI_ERROR (Status)) {
  326. goto Done;
  327. }
  328. DEBUG ((EFI_D_INFO, "Successfully Install Storage Security Protocol on the ATA device\n"));
  329. }
  330. gBS->OpenProtocol (
  331. AtaBusDriverData->Controller,
  332. &gEfiAtaPassThruProtocolGuid,
  333. (VOID **) &AtaPassThru,
  334. AtaBusDriverData->DriverBindingHandle,
  335. AtaDevice->Handle,
  336. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  337. );
  338. Done:
  339. if (NewDevicePathNode != NULL) {
  340. FreePool (NewDevicePathNode);
  341. }
  342. if (EFI_ERROR (Status) && (AtaDevice != NULL)) {
  343. ReleaseAtaResources (AtaDevice);
  344. DEBUG ((EFI_D_ERROR | EFI_D_INIT, "Failed to initialize Port %x PortMultiplierPort %x, status = %r\n", Port, PortMultiplierPort, Status));
  345. }
  346. return Status;
  347. }
  348. /**
  349. Unregisters an ATA device.
  350. This function removes the protocols installed on the controller handle and
  351. frees the resources allocated for the ATA device.
  352. @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
  353. @param Controller The controller handle of the ATA device.
  354. @param Handle The child handle.
  355. @retval EFI_SUCCESS The ATA device is successfully unregistered.
  356. @return Others Some error occurs when unregistering the ATA device.
  357. **/
  358. EFI_STATUS
  359. UnregisterAtaDevice (
  360. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  361. IN EFI_HANDLE Controller,
  362. IN EFI_HANDLE Handle
  363. )
  364. {
  365. EFI_STATUS Status;
  366. EFI_BLOCK_IO_PROTOCOL *BlockIo;
  367. EFI_BLOCK_IO2_PROTOCOL *BlockIo2;
  368. ATA_DEVICE *AtaDevice;
  369. EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
  370. EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurity;
  371. BlockIo2 = NULL;
  372. BlockIo = NULL;
  373. Status = gBS->OpenProtocol (
  374. Handle,
  375. &gEfiBlockIoProtocolGuid,
  376. (VOID **) &BlockIo,
  377. This->DriverBindingHandle,
  378. Controller,
  379. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  380. );
  381. if (EFI_ERROR (Status)) {
  382. //
  383. // Locate BlockIo2 protocol
  384. //
  385. Status = gBS->OpenProtocol (
  386. Handle,
  387. &gEfiBlockIo2ProtocolGuid,
  388. (VOID **) &BlockIo2,
  389. This->DriverBindingHandle,
  390. Controller,
  391. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  392. );
  393. if (EFI_ERROR (Status)) {
  394. return Status;
  395. }
  396. }
  397. //
  398. // Get AtaDevice data.
  399. //
  400. if (BlockIo != NULL) {
  401. AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);
  402. } else {
  403. ASSERT (BlockIo2 != NULL);
  404. AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (BlockIo2);
  405. }
  406. //
  407. // Close the child handle
  408. //
  409. gBS->CloseProtocol (
  410. Controller,
  411. &gEfiAtaPassThruProtocolGuid,
  412. This->DriverBindingHandle,
  413. Handle
  414. );
  415. //
  416. // The Ata Bus driver installs the BlockIo and BlockIo2 in the DriverBindingStart().
  417. // Here should uninstall both of them.
  418. //
  419. Status = gBS->UninstallMultipleProtocolInterfaces (
  420. Handle,
  421. &gEfiDevicePathProtocolGuid,
  422. AtaDevice->DevicePath,
  423. &gEfiBlockIoProtocolGuid,
  424. &AtaDevice->BlockIo,
  425. &gEfiBlockIo2ProtocolGuid,
  426. &AtaDevice->BlockIo2,
  427. &gEfiDiskInfoProtocolGuid,
  428. &AtaDevice->DiskInfo,
  429. NULL
  430. );
  431. if (EFI_ERROR (Status)) {
  432. gBS->OpenProtocol (
  433. Controller,
  434. &gEfiAtaPassThruProtocolGuid,
  435. (VOID **) &AtaPassThru,
  436. This->DriverBindingHandle,
  437. Handle,
  438. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  439. );
  440. return Status;
  441. }
  442. //
  443. // If Storage Security Command Protocol is installed, then uninstall this protocol.
  444. //
  445. Status = gBS->OpenProtocol (
  446. Handle,
  447. &gEfiStorageSecurityCommandProtocolGuid,
  448. (VOID **) &StorageSecurity,
  449. This->DriverBindingHandle,
  450. Controller,
  451. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  452. );
  453. if (!EFI_ERROR (Status)) {
  454. Status = gBS->UninstallProtocolInterface (
  455. Handle,
  456. &gEfiStorageSecurityCommandProtocolGuid,
  457. &AtaDevice->StorageSecurity
  458. );
  459. if (EFI_ERROR (Status)) {
  460. gBS->OpenProtocol (
  461. Controller,
  462. &gEfiAtaPassThruProtocolGuid,
  463. (VOID **) &AtaPassThru,
  464. This->DriverBindingHandle,
  465. Handle,
  466. EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
  467. );
  468. return Status;
  469. }
  470. }
  471. ReleaseAtaResources (AtaDevice);
  472. return EFI_SUCCESS;
  473. }
  474. /**
  475. Tests to see if this driver supports a given controller. If a child device is provided,
  476. it further tests to see if this driver supports creating a handle for the specified child device.
  477. This function checks to see if the driver specified by This supports the device specified by
  478. ControllerHandle. Drivers will typically use the device path attached to
  479. ControllerHandle and/or the services from the bus I/O abstraction attached to
  480. ControllerHandle to determine if the driver supports ControllerHandle. This function
  481. may be called many times during platform initialization. In order to reduce boot times, the tests
  482. performed by this function must be very small, and take as little time as possible to execute. This
  483. function must not change the state of any hardware devices, and this function must be aware that the
  484. device specified by ControllerHandle may already be managed by the same driver or a
  485. different driver. This function must match its calls to AllocatePages() with FreePages(),
  486. AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
  487. Since ControllerHandle may have been previously started by the same driver, if a protocol is
  488. already in the opened state, then it must not be closed with CloseProtocol(). This is required
  489. to guarantee the state of ControllerHandle is not modified by this function.
  490. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  491. @param[in] ControllerHandle The handle of the controller to test. This handle
  492. must support a protocol interface that supplies
  493. an I/O abstraction to the driver.
  494. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  495. parameter is ignored by device drivers, and is optional for bus
  496. drivers. For bus drivers, if this parameter is not NULL, then
  497. the bus driver must determine if the bus controller specified
  498. by ControllerHandle and the child controller specified
  499. by RemainingDevicePath are both supported by this
  500. bus driver.
  501. @retval EFI_SUCCESS The device specified by ControllerHandle and
  502. RemainingDevicePath is supported by the driver specified by This.
  503. @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
  504. RemainingDevicePath is already being managed by the driver
  505. specified by This.
  506. @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
  507. RemainingDevicePath is already being managed by a different
  508. driver or an application that requires exclusive access.
  509. Currently not implemented.
  510. @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
  511. RemainingDevicePath is not supported by the driver specified by This.
  512. **/
  513. EFI_STATUS
  514. EFIAPI
  515. AtaBusDriverBindingSupported (
  516. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  517. IN EFI_HANDLE Controller,
  518. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  519. )
  520. {
  521. EFI_STATUS Status;
  522. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  523. EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
  524. UINT16 Port;
  525. UINT16 PortMultiplierPort;
  526. //
  527. // Test EFI_ATA_PASS_THRU_PROTOCOL on controller handle.
  528. //
  529. Status = gBS->OpenProtocol (
  530. Controller,
  531. &gEfiAtaPassThruProtocolGuid,
  532. (VOID **) &AtaPassThru,
  533. This->DriverBindingHandle,
  534. Controller,
  535. EFI_OPEN_PROTOCOL_BY_DRIVER
  536. );
  537. if (Status == EFI_ALREADY_STARTED) {
  538. return EFI_SUCCESS;
  539. }
  540. if (EFI_ERROR (Status)) {
  541. return Status;
  542. }
  543. //
  544. // Test to see if this ATA Pass Thru Protocol is for a LOGICAL channel
  545. //
  546. if ((AtaPassThru->Mode->Attributes & EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL) == 0) {
  547. //
  548. // Close the I/O Abstraction(s) used to perform the supported test
  549. //
  550. gBS->CloseProtocol (
  551. Controller,
  552. &gEfiAtaPassThruProtocolGuid,
  553. This->DriverBindingHandle,
  554. Controller
  555. );
  556. return EFI_UNSUPPORTED;
  557. }
  558. //
  559. // Test RemainingDevicePath is valid or not.
  560. //
  561. if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {
  562. Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
  563. if (EFI_ERROR (Status)) {
  564. //
  565. // Close the I/O Abstraction(s) used to perform the supported test
  566. //
  567. gBS->CloseProtocol (
  568. Controller,
  569. &gEfiAtaPassThruProtocolGuid,
  570. This->DriverBindingHandle,
  571. Controller
  572. );
  573. return Status;
  574. }
  575. }
  576. //
  577. // Close the I/O Abstraction(s) used to perform the supported test
  578. //
  579. gBS->CloseProtocol (
  580. Controller,
  581. &gEfiAtaPassThruProtocolGuid,
  582. This->DriverBindingHandle,
  583. Controller
  584. );
  585. //
  586. // Open the EFI Device Path protocol needed to perform the supported test
  587. //
  588. Status = gBS->OpenProtocol (
  589. Controller,
  590. &gEfiDevicePathProtocolGuid,
  591. (VOID **) &ParentDevicePath,
  592. This->DriverBindingHandle,
  593. Controller,
  594. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  595. );
  596. return Status;
  597. }
  598. /**
  599. Starts a device controller or a bus controller.
  600. The Start() function is designed to be invoked from the EFI boot service ConnectController().
  601. As a result, much of the error checking on the parameters to Start() has been moved into this
  602. common boot service. It is legal to call Start() from other locations,
  603. but the following calling restrictions must be followed or the system behavior will not be deterministic.
  604. 1. ControllerHandle must be a valid EFI_HANDLE.
  605. 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
  606. EFI_DEVICE_PATH_PROTOCOL.
  607. 3. Prior to calling Start(), the Supported() function for the driver specified by This must
  608. have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
  609. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  610. @param[in] ControllerHandle The handle of the controller to start. This handle
  611. must support a protocol interface that supplies
  612. an I/O abstraction to the driver.
  613. @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
  614. parameter is ignored by device drivers, and is optional for bus
  615. drivers. For a bus driver, if this parameter is NULL, then handles
  616. for all the children of Controller are created by this driver.
  617. If this parameter is not NULL and the first Device Path Node is
  618. not the End of Device Path Node, then only the handle for the
  619. child device specified by the first Device Path Node of
  620. RemainingDevicePath is created by this driver.
  621. If the first Device Path Node of RemainingDevicePath is
  622. the End of Device Path Node, no child handle is created by this
  623. driver.
  624. @retval EFI_SUCCESS The device was started.
  625. @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
  626. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
  627. @retval Others The driver failded to start the device.
  628. **/
  629. EFI_STATUS
  630. EFIAPI
  631. AtaBusDriverBindingStart (
  632. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  633. IN EFI_HANDLE Controller,
  634. IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
  635. )
  636. {
  637. EFI_STATUS Status;
  638. EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
  639. EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
  640. ATA_BUS_DRIVER_DATA *AtaBusDriverData;
  641. UINT16 Port;
  642. UINT16 PortMultiplierPort;
  643. AtaBusDriverData = NULL;
  644. Status = gBS->OpenProtocol (
  645. Controller,
  646. &gEfiDevicePathProtocolGuid,
  647. (VOID **) &ParentDevicePath,
  648. This->DriverBindingHandle,
  649. Controller,
  650. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  651. );
  652. if (EFI_ERROR (Status)) {
  653. return Status;
  654. }
  655. //
  656. // Report Status Code to indicate ATA bus starts
  657. //
  658. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  659. EFI_PROGRESS_CODE,
  660. (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_INIT),
  661. ParentDevicePath
  662. );
  663. Status = gBS->OpenProtocol (
  664. Controller,
  665. &gEfiAtaPassThruProtocolGuid,
  666. (VOID **) &AtaPassThru,
  667. This->DriverBindingHandle,
  668. Controller,
  669. EFI_OPEN_PROTOCOL_BY_DRIVER
  670. );
  671. if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
  672. goto ErrorExit;
  673. }
  674. //
  675. // Check EFI_ALREADY_STARTED to reuse the original ATA_BUS_DRIVER_DATA.
  676. //
  677. if (Status != EFI_ALREADY_STARTED) {
  678. AtaBusDriverData = AllocateZeroPool (sizeof (ATA_BUS_DRIVER_DATA));
  679. if (AtaBusDriverData == NULL) {
  680. Status = EFI_OUT_OF_RESOURCES;
  681. goto ErrorExit;
  682. }
  683. AtaBusDriverData->AtaPassThru = AtaPassThru;
  684. AtaBusDriverData->Controller = Controller;
  685. AtaBusDriverData->ParentDevicePath = ParentDevicePath;
  686. AtaBusDriverData->DriverBindingHandle = This->DriverBindingHandle;
  687. Status = gBS->InstallMultipleProtocolInterfaces (
  688. &Controller,
  689. &gEfiCallerIdGuid,
  690. AtaBusDriverData,
  691. NULL
  692. );
  693. if (EFI_ERROR (Status)) {
  694. goto ErrorExit;
  695. }
  696. } else {
  697. Status = gBS->OpenProtocol (
  698. Controller,
  699. &gEfiCallerIdGuid,
  700. (VOID **) &AtaBusDriverData,
  701. This->DriverBindingHandle,
  702. Controller,
  703. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  704. );
  705. if (EFI_ERROR (Status)) {
  706. AtaBusDriverData = NULL;
  707. goto ErrorExit;
  708. }
  709. }
  710. //
  711. // Report Status Code to indicate detecting devices on bus
  712. //
  713. REPORT_STATUS_CODE_WITH_DEVICE_PATH (
  714. EFI_PROGRESS_CODE,
  715. (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_DETECT),
  716. ParentDevicePath
  717. );
  718. if (RemainingDevicePath == NULL) {
  719. Port = 0xFFFF;
  720. while (TRUE) {
  721. Status = AtaPassThru->GetNextPort (AtaPassThru, &Port);
  722. if (EFI_ERROR (Status)) {
  723. //
  724. // We cannot find more legal port then we are done.
  725. //
  726. break;
  727. }
  728. PortMultiplierPort = 0xFFFF;
  729. while (TRUE) {
  730. Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);
  731. if (EFI_ERROR (Status)) {
  732. //
  733. // We cannot find more legal port multiplier port number for ATA device
  734. // on the port, then we are done.
  735. //
  736. break;
  737. }
  738. RegisterAtaDevice (AtaBusDriverData, Port, PortMultiplierPort);
  739. }
  740. }
  741. Status = EFI_SUCCESS;
  742. } else if (!IsDevicePathEnd (RemainingDevicePath)) {
  743. Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
  744. if (!EFI_ERROR (Status)) {
  745. Status = RegisterAtaDevice (AtaBusDriverData,Port, PortMultiplierPort);
  746. }
  747. }
  748. return Status;
  749. ErrorExit:
  750. if (AtaBusDriverData != NULL) {
  751. gBS->UninstallMultipleProtocolInterfaces (
  752. Controller,
  753. &gEfiCallerIdGuid,
  754. AtaBusDriverData,
  755. NULL
  756. );
  757. FreePool (AtaBusDriverData);
  758. }
  759. gBS->CloseProtocol (
  760. Controller,
  761. &gEfiAtaPassThruProtocolGuid,
  762. This->DriverBindingHandle,
  763. Controller
  764. );
  765. return Status;
  766. }
  767. /**
  768. Stops a device controller or a bus controller.
  769. The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
  770. As a result, much of the error checking on the parameters to Stop() has been moved
  771. into this common boot service. It is legal to call Stop() from other locations,
  772. but the following calling restrictions must be followed or the system behavior will not be deterministic.
  773. 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
  774. same driver's Start() function.
  775. 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
  776. EFI_HANDLE. In addition, all of these handles must have been created in this driver's
  777. Start() function, and the Start() function must have called OpenProtocol() on
  778. ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
  779. @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
  780. @param[in] ControllerHandle A handle to the device being stopped. The handle must
  781. support a bus specific I/O protocol for the driver
  782. to use to stop the device.
  783. @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
  784. @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
  785. if NumberOfChildren is 0.
  786. @retval EFI_SUCCESS The device was stopped.
  787. @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
  788. **/
  789. EFI_STATUS
  790. EFIAPI
  791. AtaBusDriverBindingStop (
  792. IN EFI_DRIVER_BINDING_PROTOCOL *This,
  793. IN EFI_HANDLE Controller,
  794. IN UINTN NumberOfChildren,
  795. IN EFI_HANDLE *ChildHandleBuffer
  796. )
  797. {
  798. EFI_STATUS Status;
  799. BOOLEAN AllChildrenStopped;
  800. UINTN Index;
  801. ATA_BUS_DRIVER_DATA *AtaBusDriverData;
  802. if (NumberOfChildren == 0) {
  803. Status = gBS->OpenProtocol (
  804. Controller,
  805. &gEfiCallerIdGuid,
  806. (VOID **) &AtaBusDriverData,
  807. This->DriverBindingHandle,
  808. Controller,
  809. EFI_OPEN_PROTOCOL_GET_PROTOCOL
  810. );
  811. if (!EFI_ERROR (Status)) {
  812. gBS->UninstallMultipleProtocolInterfaces (
  813. Controller,
  814. &gEfiCallerIdGuid,
  815. AtaBusDriverData,
  816. NULL
  817. );
  818. FreePool (AtaBusDriverData);
  819. }
  820. gBS->CloseProtocol (
  821. Controller,
  822. &gEfiAtaPassThruProtocolGuid,
  823. This->DriverBindingHandle,
  824. Controller
  825. );
  826. return EFI_SUCCESS;
  827. }
  828. AllChildrenStopped = TRUE;
  829. for (Index = 0; Index < NumberOfChildren; Index++) {
  830. Status = UnregisterAtaDevice (This, Controller, ChildHandleBuffer[Index]);
  831. if (EFI_ERROR (Status)) {
  832. AllChildrenStopped = FALSE;
  833. }
  834. }
  835. if (!AllChildrenStopped) {
  836. return EFI_DEVICE_ERROR;
  837. }
  838. return EFI_SUCCESS;
  839. }
  840. /**
  841. Reset the Block Device.
  842. @param This Indicates a pointer to the calling context.
  843. @param ExtendedVerification Driver may perform diagnostics on reset.
  844. @retval EFI_SUCCESS The device was reset.
  845. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  846. not be reset.
  847. **/
  848. EFI_STATUS
  849. EFIAPI
  850. AtaBlockIoReset (
  851. IN EFI_BLOCK_IO_PROTOCOL *This,
  852. IN BOOLEAN ExtendedVerification
  853. )
  854. {
  855. EFI_STATUS Status;
  856. ATA_DEVICE *AtaDevice;
  857. EFI_TPL OldTpl;
  858. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  859. AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
  860. Status = ResetAtaDevice (AtaDevice);
  861. if (EFI_ERROR (Status)) {
  862. Status = EFI_DEVICE_ERROR;
  863. }
  864. gBS->RestoreTPL (OldTpl);
  865. return Status;
  866. }
  867. /**
  868. Read/Write BufferSize bytes from Lba from/into Buffer.
  869. @param[in] This Indicates a pointer to the calling context. Either be
  870. block I/O or block I/O2.
  871. @param[in] MediaId The media ID that the read/write request is for.
  872. @param[in] Lba The starting logical block address to be read/written.
  873. The caller is responsible for reading/writing to only
  874. legitimate locations.
  875. @param[in, out] Token A pointer to the token associated with the transaction.
  876. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  877. @param[out] Buffer A pointer to the destination/source buffer for the data.
  878. @param[in] IsBlockIo2 Indicate the calling is from BlockIO or BlockIO2. TRUE is
  879. from BlockIO2, FALSE is for BlockIO.
  880. @param[in] IsWrite Indicates whether it is a write operation.
  881. @retval EFI_SUCCESS The data was read/written correctly to the device.
  882. @retval EFI_WRITE_PROTECTED The device can not be read/written to.
  883. @retval EFI_DEVICE_ERROR The device reported an error while performing the read/write.
  884. @retval EFI_NO_MEDIA There is no media in the device.
  885. @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
  886. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  887. @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,
  888. or the buffer is not on proper alignment.
  889. **/
  890. EFI_STATUS
  891. BlockIoReadWrite (
  892. IN VOID *This,
  893. IN UINT32 MediaId,
  894. IN EFI_LBA Lba,
  895. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  896. IN UINTN BufferSize,
  897. OUT VOID *Buffer,
  898. IN BOOLEAN IsBlockIo2,
  899. IN BOOLEAN IsWrite
  900. )
  901. {
  902. ATA_DEVICE *AtaDevice;
  903. EFI_STATUS Status;
  904. EFI_TPL OldTpl;
  905. EFI_BLOCK_IO_MEDIA *Media;
  906. UINTN BlockSize;
  907. UINTN NumberOfBlocks;
  908. UINTN IoAlign;
  909. if (IsBlockIo2) {
  910. Media = ((EFI_BLOCK_IO2_PROTOCOL *) This)->Media;
  911. AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);
  912. } else {
  913. Media = ((EFI_BLOCK_IO_PROTOCOL *) This)->Media;
  914. AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
  915. }
  916. if (MediaId != Media->MediaId) {
  917. return EFI_MEDIA_CHANGED;
  918. }
  919. //
  920. // Check parameters.
  921. //
  922. if (Buffer == NULL) {
  923. return EFI_INVALID_PARAMETER;
  924. }
  925. if (BufferSize == 0) {
  926. if ((Token != NULL) && (Token->Event != NULL)) {
  927. Token->TransactionStatus = EFI_SUCCESS;
  928. gBS->SignalEvent (Token->Event);
  929. }
  930. return EFI_SUCCESS;
  931. }
  932. BlockSize = Media->BlockSize;
  933. if ((BufferSize % BlockSize) != 0) {
  934. return EFI_BAD_BUFFER_SIZE;
  935. }
  936. NumberOfBlocks = BufferSize / BlockSize;
  937. if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
  938. return EFI_INVALID_PARAMETER;
  939. }
  940. IoAlign = Media->IoAlign;
  941. if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {
  942. return EFI_INVALID_PARAMETER;
  943. }
  944. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  945. //
  946. // Invoke low level AtaDevice Access Routine.
  947. //
  948. Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite, Token);
  949. gBS->RestoreTPL (OldTpl);
  950. return Status;
  951. }
  952. /**
  953. Read BufferSize bytes from Lba into Buffer.
  954. @param This Indicates a pointer to the calling context.
  955. @param MediaId Id of the media, changes every time the media is replaced.
  956. @param Lba The starting Logical Block Address to read from
  957. @param BufferSize Size of Buffer, must be a multiple of device block size.
  958. @param Buffer A pointer to the destination buffer for the data. The caller is
  959. responsible for either having implicit or explicit ownership of the buffer.
  960. @retval EFI_SUCCESS The data was read correctly from the device.
  961. @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
  962. @retval EFI_NO_MEDIA There is no media in the device.
  963. @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
  964. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  965. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  966. or the buffer is not on proper alignment.
  967. **/
  968. EFI_STATUS
  969. EFIAPI
  970. AtaBlockIoReadBlocks (
  971. IN EFI_BLOCK_IO_PROTOCOL *This,
  972. IN UINT32 MediaId,
  973. IN EFI_LBA Lba,
  974. IN UINTN BufferSize,
  975. OUT VOID *Buffer
  976. )
  977. {
  978. return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, FALSE);
  979. }
  980. /**
  981. Write BufferSize bytes from Lba into Buffer.
  982. @param This Indicates a pointer to the calling context.
  983. @param MediaId The media ID that the write request is for.
  984. @param Lba The starting logical block address to be written. The caller is
  985. responsible for writing to only legitimate locations.
  986. @param BufferSize Size of Buffer, must be a multiple of device block size.
  987. @param Buffer A pointer to the source buffer for the data.
  988. @retval EFI_SUCCESS The data was written correctly to the device.
  989. @retval EFI_WRITE_PROTECTED The device can not be written to.
  990. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  991. @retval EFI_NO_MEDIA There is no media in the device.
  992. @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
  993. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  994. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  995. or the buffer is not on proper alignment.
  996. **/
  997. EFI_STATUS
  998. EFIAPI
  999. AtaBlockIoWriteBlocks (
  1000. IN EFI_BLOCK_IO_PROTOCOL *This,
  1001. IN UINT32 MediaId,
  1002. IN EFI_LBA Lba,
  1003. IN UINTN BufferSize,
  1004. IN VOID *Buffer
  1005. )
  1006. {
  1007. return BlockIoReadWrite ((VOID *) This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, TRUE);
  1008. }
  1009. /**
  1010. Flush the Block Device.
  1011. @param This Indicates a pointer to the calling context.
  1012. @retval EFI_SUCCESS All outstanding data was written to the device
  1013. @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
  1014. @retval EFI_NO_MEDIA There is no media in the device.
  1015. **/
  1016. EFI_STATUS
  1017. EFIAPI
  1018. AtaBlockIoFlushBlocks (
  1019. IN EFI_BLOCK_IO_PROTOCOL *This
  1020. )
  1021. {
  1022. //
  1023. // return directly
  1024. //
  1025. return EFI_SUCCESS;
  1026. }
  1027. /**
  1028. Reset the Block Device.
  1029. @param[in] This Indicates a pointer to the calling context.
  1030. @param[in] ExtendedVerification Driver may perform diagnostics on reset.
  1031. @retval EFI_SUCCESS The device was reset.
  1032. @retval EFI_DEVICE_ERROR The device is not functioning properly and could
  1033. not be reset.
  1034. **/
  1035. EFI_STATUS
  1036. EFIAPI
  1037. AtaBlockIoResetEx (
  1038. IN EFI_BLOCK_IO2_PROTOCOL *This,
  1039. IN BOOLEAN ExtendedVerification
  1040. )
  1041. {
  1042. EFI_STATUS Status;
  1043. ATA_DEVICE *AtaDevice;
  1044. EFI_TPL OldTpl;
  1045. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1046. AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);
  1047. AtaTerminateNonBlockingTask (AtaDevice);
  1048. Status = ResetAtaDevice (AtaDevice);
  1049. if (EFI_ERROR (Status)) {
  1050. Status = EFI_DEVICE_ERROR;
  1051. }
  1052. gBS->RestoreTPL (OldTpl);
  1053. return Status;
  1054. }
  1055. /**
  1056. Read BufferSize bytes from Lba into Buffer.
  1057. @param[in] This Indicates a pointer to the calling context.
  1058. @param[in] MediaId Id of the media, changes every time the media is replaced.
  1059. @param[in] Lba The starting Logical Block Address to read from.
  1060. @param[in, out] Token A pointer to the token associated with the transaction.
  1061. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  1062. @param[out] Buffer A pointer to the destination buffer for the data. The caller is
  1063. responsible for either having implicit or explicit ownership of the buffer.
  1064. @retval EFI_SUCCESS The read request was queued if Event is not NULL.
  1065. The data was read correctly from the device if
  1066. the Event is NULL.
  1067. @retval EFI_DEVICE_ERROR The device reported an error while performing
  1068. the read.
  1069. @retval EFI_NO_MEDIA There is no media in the device.
  1070. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1071. @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
  1072. intrinsic block size of the device.
  1073. @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
  1074. or the buffer is not on proper alignment.
  1075. @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
  1076. of resources.
  1077. **/
  1078. EFI_STATUS
  1079. EFIAPI
  1080. AtaBlockIoReadBlocksEx (
  1081. IN EFI_BLOCK_IO2_PROTOCOL *This,
  1082. IN UINT32 MediaId,
  1083. IN EFI_LBA Lba,
  1084. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  1085. IN UINTN BufferSize,
  1086. OUT VOID *Buffer
  1087. )
  1088. {
  1089. return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, FALSE);
  1090. }
  1091. /**
  1092. Write BufferSize bytes from Lba into Buffer.
  1093. @param[in] This Indicates a pointer to the calling context.
  1094. @param[in] MediaId The media ID that the write request is for.
  1095. @param[in] Lba The starting logical block address to be written. The
  1096. caller is responsible for writing to only legitimate
  1097. locations.
  1098. @param[in, out] Token A pointer to the token associated with the transaction.
  1099. @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
  1100. @param[in] Buffer A pointer to the source buffer for the data.
  1101. @retval EFI_SUCCESS The data was written correctly to the device.
  1102. @retval EFI_WRITE_PROTECTED The device can not be written to.
  1103. @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
  1104. @retval EFI_NO_MEDIA There is no media in the device.
  1105. @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
  1106. @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
  1107. @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
  1108. or the buffer is not on proper alignment.
  1109. **/
  1110. EFI_STATUS
  1111. EFIAPI
  1112. AtaBlockIoWriteBlocksEx (
  1113. IN EFI_BLOCK_IO2_PROTOCOL *This,
  1114. IN UINT32 MediaId,
  1115. IN EFI_LBA Lba,
  1116. IN OUT EFI_BLOCK_IO2_TOKEN *Token,
  1117. IN UINTN BufferSize,
  1118. IN VOID *Buffer
  1119. )
  1120. {
  1121. return BlockIoReadWrite ((VOID *) This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, TRUE);
  1122. }
  1123. /**
  1124. Flush the Block Device.
  1125. @param[in] This Indicates a pointer to the calling context.
  1126. @param[in, out] Token A pointer to the token associated with the transaction.
  1127. @retval EFI_SUCCESS All outstanding data was written to the device
  1128. @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
  1129. @retval EFI_NO_MEDIA There is no media in the device.
  1130. **/
  1131. EFI_STATUS
  1132. EFIAPI
  1133. AtaBlockIoFlushBlocksEx (
  1134. IN EFI_BLOCK_IO2_PROTOCOL *This,
  1135. IN OUT EFI_BLOCK_IO2_TOKEN *Token
  1136. )
  1137. {
  1138. //
  1139. // Signal event and return directly.
  1140. //
  1141. if (Token != NULL && Token->Event != NULL) {
  1142. Token->TransactionStatus = EFI_SUCCESS;
  1143. gBS->SignalEvent (Token->Event);
  1144. }
  1145. return EFI_SUCCESS;
  1146. }
  1147. /**
  1148. Provides inquiry information for the controller type.
  1149. This function is used by the IDE bus driver to get inquiry data. Data format
  1150. of Identify data is defined by the Interface GUID.
  1151. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
  1152. @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
  1153. @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
  1154. @retval EFI_SUCCESS The command was accepted without any errors.
  1155. @retval EFI_NOT_FOUND Device does not support this data class
  1156. @retval EFI_DEVICE_ERROR Error reading InquiryData from device
  1157. @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
  1158. **/
  1159. EFI_STATUS
  1160. EFIAPI
  1161. AtaDiskInfoInquiry (
  1162. IN EFI_DISK_INFO_PROTOCOL *This,
  1163. IN OUT VOID *InquiryData,
  1164. IN OUT UINT32 *InquiryDataSize
  1165. )
  1166. {
  1167. return EFI_NOT_FOUND;
  1168. }
  1169. /**
  1170. Provides identify information for the controller type.
  1171. This function is used by the IDE bus driver to get identify data. Data format
  1172. of Identify data is defined by the Interface GUID.
  1173. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
  1174. instance.
  1175. @param[in, out] IdentifyData Pointer to a buffer for the identify data.
  1176. @param[in, out] IdentifyDataSize Pointer to the value for the identify data
  1177. size.
  1178. @retval EFI_SUCCESS The command was accepted without any errors.
  1179. @retval EFI_NOT_FOUND Device does not support this data class
  1180. @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
  1181. @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
  1182. **/
  1183. EFI_STATUS
  1184. EFIAPI
  1185. AtaDiskInfoIdentify (
  1186. IN EFI_DISK_INFO_PROTOCOL *This,
  1187. IN OUT VOID *IdentifyData,
  1188. IN OUT UINT32 *IdentifyDataSize
  1189. )
  1190. {
  1191. EFI_STATUS Status;
  1192. ATA_DEVICE *AtaDevice;
  1193. AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
  1194. Status = EFI_BUFFER_TOO_SMALL;
  1195. if (*IdentifyDataSize >= sizeof (ATA_IDENTIFY_DATA)) {
  1196. Status = EFI_SUCCESS;
  1197. CopyMem (IdentifyData, AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));
  1198. }
  1199. *IdentifyDataSize = sizeof (ATA_IDENTIFY_DATA);
  1200. return Status;
  1201. }
  1202. /**
  1203. Provides sense data information for the controller type.
  1204. This function is used by the IDE bus driver to get sense data.
  1205. Data format of Sense data is defined by the Interface GUID.
  1206. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
  1207. @param[in, out] SenseData Pointer to the SenseData.
  1208. @param[in, out] SenseDataSize Size of SenseData in bytes.
  1209. @param[out] SenseDataNumber Pointer to the value for the sense data size.
  1210. @retval EFI_SUCCESS The command was accepted without any errors.
  1211. @retval EFI_NOT_FOUND Device does not support this data class.
  1212. @retval EFI_DEVICE_ERROR Error reading SenseData from device.
  1213. @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
  1214. **/
  1215. EFI_STATUS
  1216. EFIAPI
  1217. AtaDiskInfoSenseData (
  1218. IN EFI_DISK_INFO_PROTOCOL *This,
  1219. IN OUT VOID *SenseData,
  1220. IN OUT UINT32 *SenseDataSize,
  1221. OUT UINT8 *SenseDataNumber
  1222. )
  1223. {
  1224. return EFI_NOT_FOUND;
  1225. }
  1226. /**
  1227. This function is used by the IDE bus driver to get controller information.
  1228. @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
  1229. @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
  1230. @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
  1231. @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
  1232. @retval EFI_UNSUPPORTED This is not an IDE device.
  1233. **/
  1234. EFI_STATUS
  1235. EFIAPI
  1236. AtaDiskInfoWhichIde (
  1237. IN EFI_DISK_INFO_PROTOCOL *This,
  1238. OUT UINT32 *IdeChannel,
  1239. OUT UINT32 *IdeDevice
  1240. )
  1241. {
  1242. ATA_DEVICE *AtaDevice;
  1243. AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
  1244. *IdeChannel = AtaDevice->Port;
  1245. *IdeDevice = AtaDevice->PortMultiplierPort;
  1246. return EFI_SUCCESS;
  1247. }
  1248. /**
  1249. Send a security protocol command to a device that receives data and/or the result
  1250. of one or more commands sent by SendData.
  1251. The ReceiveData function sends a security protocol command to the given MediaId.
  1252. The security protocol command sent is defined by SecurityProtocolId and contains
  1253. the security protocol specific data SecurityProtocolSpecificData. The function
  1254. returns the data from the security protocol command in PayloadBuffer.
  1255. For devices supporting the SCSI command set, the security protocol command is sent
  1256. using the SECURITY PROTOCOL IN command defined in SPC-4.
  1257. For devices supporting the ATA command set, the security protocol command is sent
  1258. using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
  1259. is non-zero.
  1260. If the PayloadBufferSize is zero, the security protocol command is sent using the
  1261. Trusted Non-Data command defined in ATA8-ACS.
  1262. If PayloadBufferSize is too small to store the available data from the security
  1263. protocol command, the function shall copy PayloadBufferSize bytes into the
  1264. PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
  1265. If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
  1266. the function shall return EFI_INVALID_PARAMETER.
  1267. If the given MediaId does not support security protocol commands, the function shall
  1268. return EFI_UNSUPPORTED. If there is no media in the device, the function returns
  1269. EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
  1270. the function returns EFI_MEDIA_CHANGED.
  1271. If the security protocol fails to complete within the Timeout period, the function
  1272. shall return EFI_TIMEOUT.
  1273. If the security protocol command completes without an error, the function shall
  1274. return EFI_SUCCESS. If the security protocol command completes with an error, the
  1275. function shall return EFI_DEVICE_ERROR.
  1276. @param This Indicates a pointer to the calling context.
  1277. @param MediaId ID of the medium to receive data from.
  1278. @param Timeout The timeout, in 100ns units, to use for the execution
  1279. of the security protocol command. A Timeout value of 0
  1280. means that this function will wait indefinitely for the
  1281. security protocol command to execute. If Timeout is greater
  1282. than zero, then this function will return EFI_TIMEOUT
  1283. if the time required to execute the receive data command
  1284. is greater than Timeout.
  1285. @param SecurityProtocolId The value of the "Security Protocol" parameter of
  1286. the security protocol command to be sent.
  1287. @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
  1288. of the security protocol command to be sent.
  1289. @param PayloadBufferSize Size in bytes of the payload data buffer.
  1290. @param PayloadBuffer A pointer to a destination buffer to store the security
  1291. protocol command specific payload data for the security
  1292. protocol command. The caller is responsible for having
  1293. either implicit or explicit ownership of the buffer.
  1294. @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
  1295. data written to the payload data buffer.
  1296. @retval EFI_SUCCESS The security protocol command completed successfully.
  1297. @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
  1298. data from the device. The PayloadBuffer contains the truncated data.
  1299. @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
  1300. @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
  1301. @retval EFI_NO_MEDIA There is no media in the device.
  1302. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1303. @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
  1304. PayloadBufferSize is non-zero.
  1305. @retval EFI_TIMEOUT A timeout occurred while waiting for the security
  1306. protocol command to execute.
  1307. **/
  1308. EFI_STATUS
  1309. EFIAPI
  1310. AtaStorageSecurityReceiveData (
  1311. IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
  1312. IN UINT32 MediaId,
  1313. IN UINT64 Timeout,
  1314. IN UINT8 SecurityProtocolId,
  1315. IN UINT16 SecurityProtocolSpecificData,
  1316. IN UINTN PayloadBufferSize,
  1317. OUT VOID *PayloadBuffer,
  1318. OUT UINTN *PayloadTransferSize
  1319. )
  1320. {
  1321. EFI_STATUS Status;
  1322. ATA_DEVICE *Private;
  1323. EFI_TPL OldTpl;
  1324. DEBUG ((EFI_D_INFO, "EFI Storage Security Protocol - Read\n"));
  1325. if ((PayloadBuffer == NULL || PayloadTransferSize == NULL) && PayloadBufferSize != 0) {
  1326. return EFI_INVALID_PARAMETER;
  1327. }
  1328. Status = EFI_SUCCESS;
  1329. Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);
  1330. if (MediaId != Private->BlockIo.Media->MediaId) {
  1331. return EFI_MEDIA_CHANGED;
  1332. }
  1333. if (!Private->BlockIo.Media->MediaPresent) {
  1334. return EFI_NO_MEDIA;
  1335. }
  1336. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1337. Status = TrustTransferAtaDevice (
  1338. Private,
  1339. PayloadBuffer,
  1340. SecurityProtocolId,
  1341. SecurityProtocolSpecificData,
  1342. PayloadBufferSize,
  1343. FALSE,
  1344. Timeout,
  1345. PayloadTransferSize
  1346. );
  1347. gBS->RestoreTPL (OldTpl);
  1348. return Status;
  1349. }
  1350. /**
  1351. Send a security protocol command to a device.
  1352. The SendData function sends a security protocol command containing the payload
  1353. PayloadBuffer to the given MediaId. The security protocol command sent is
  1354. defined by SecurityProtocolId and contains the security protocol specific data
  1355. SecurityProtocolSpecificData. If the underlying protocol command requires a
  1356. specific padding for the command payload, the SendData function shall add padding
  1357. bytes to the command payload to satisfy the padding requirements.
  1358. For devices supporting the SCSI command set, the security protocol command is sent
  1359. using the SECURITY PROTOCOL OUT command defined in SPC-4.
  1360. For devices supporting the ATA command set, the security protocol command is sent
  1361. using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
  1362. is non-zero. If the PayloadBufferSize is zero, the security protocol command is
  1363. sent using the Trusted Non-Data command defined in ATA8-ACS.
  1364. If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
  1365. return EFI_INVALID_PARAMETER.
  1366. If the given MediaId does not support security protocol commands, the function
  1367. shall return EFI_UNSUPPORTED. If there is no media in the device, the function
  1368. returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
  1369. device, the function returns EFI_MEDIA_CHANGED.
  1370. If the security protocol fails to complete within the Timeout period, the function
  1371. shall return EFI_TIMEOUT.
  1372. If the security protocol command completes without an error, the function shall return
  1373. EFI_SUCCESS. If the security protocol command completes with an error, the function
  1374. shall return EFI_DEVICE_ERROR.
  1375. @param This Indicates a pointer to the calling context.
  1376. @param MediaId ID of the medium to receive data from.
  1377. @param Timeout The timeout, in 100ns units, to use for the execution
  1378. of the security protocol command. A Timeout value of 0
  1379. means that this function will wait indefinitely for the
  1380. security protocol command to execute. If Timeout is greater
  1381. than zero, then this function will return EFI_TIMEOUT
  1382. if the time required to execute the receive data command
  1383. is greater than Timeout.
  1384. @param SecurityProtocolId The value of the "Security Protocol" parameter of
  1385. the security protocol command to be sent.
  1386. @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
  1387. of the security protocol command to be sent.
  1388. @param PayloadBufferSize Size in bytes of the payload data buffer.
  1389. @param PayloadBuffer A pointer to a destination buffer to store the security
  1390. protocol command specific payload data for the security
  1391. protocol command.
  1392. @retval EFI_SUCCESS The security protocol command completed successfully.
  1393. @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
  1394. @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
  1395. @retval EFI_NO_MEDIA There is no media in the device.
  1396. @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
  1397. @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
  1398. @retval EFI_TIMEOUT A timeout occurred while waiting for the security
  1399. protocol command to execute.
  1400. **/
  1401. EFI_STATUS
  1402. EFIAPI
  1403. AtaStorageSecuritySendData (
  1404. IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
  1405. IN UINT32 MediaId,
  1406. IN UINT64 Timeout,
  1407. IN UINT8 SecurityProtocolId,
  1408. IN UINT16 SecurityProtocolSpecificData,
  1409. IN UINTN PayloadBufferSize,
  1410. IN VOID *PayloadBuffer
  1411. )
  1412. {
  1413. EFI_STATUS Status;
  1414. ATA_DEVICE *Private;
  1415. EFI_TPL OldTpl;
  1416. DEBUG ((EFI_D_INFO, "EFI Storage Security Protocol - Send\n"));
  1417. if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {
  1418. return EFI_INVALID_PARAMETER;
  1419. }
  1420. Status = EFI_SUCCESS;
  1421. Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);
  1422. if (MediaId != Private->BlockIo.Media->MediaId) {
  1423. return EFI_MEDIA_CHANGED;
  1424. }
  1425. OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
  1426. Status = TrustTransferAtaDevice (
  1427. Private,
  1428. PayloadBuffer,
  1429. SecurityProtocolId,
  1430. SecurityProtocolSpecificData,
  1431. PayloadBufferSize,
  1432. TRUE,
  1433. Timeout,
  1434. NULL
  1435. );
  1436. gBS->RestoreTPL (OldTpl);
  1437. return Status;
  1438. }
  1439. /**
  1440. The user Entry Point for module AtaBus. The user code starts with this function.
  1441. @param[in] ImageHandle The firmware allocated handle for the EFI image.
  1442. @param[in] SystemTable A pointer to the EFI System Table.
  1443. @retval EFI_SUCCESS The entry point is executed successfully.
  1444. @retval other Some error occurs when executing this entry point.
  1445. **/
  1446. EFI_STATUS
  1447. EFIAPI
  1448. InitializeAtaBus(
  1449. IN EFI_HANDLE ImageHandle,
  1450. IN EFI_SYSTEM_TABLE *SystemTable
  1451. )
  1452. {
  1453. EFI_STATUS Status;
  1454. //
  1455. // Install driver model protocol(s).
  1456. //
  1457. Status = EfiLibInstallDriverBindingComponentName2 (
  1458. ImageHandle,
  1459. SystemTable,
  1460. &gAtaBusDriverBinding,
  1461. ImageHandle,
  1462. &gAtaBusComponentName,
  1463. &gAtaBusComponentName2
  1464. );
  1465. ASSERT_EFI_ERROR (Status);
  1466. return Status;
  1467. }