Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

efi_block.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. /**
  25. * @file
  26. *
  27. * EFI block device protocols
  28. *
  29. */
  30. #include <stddef.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include <ipxe/refcnt.h>
  35. #include <ipxe/list.h>
  36. #include <ipxe/uri.h>
  37. #include <ipxe/interface.h>
  38. #include <ipxe/blockdev.h>
  39. #include <ipxe/xfer.h>
  40. #include <ipxe/open.h>
  41. #include <ipxe/retry.h>
  42. #include <ipxe/timer.h>
  43. #include <ipxe/process.h>
  44. #include <ipxe/sanboot.h>
  45. #include <ipxe/iso9660.h>
  46. #include <ipxe/acpi.h>
  47. #include <ipxe/efi/efi.h>
  48. #include <ipxe/efi/Protocol/BlockIo.h>
  49. #include <ipxe/efi/Protocol/SimpleFileSystem.h>
  50. #include <ipxe/efi/Protocol/AcpiTable.h>
  51. #include <ipxe/efi/efi_driver.h>
  52. #include <ipxe/efi/efi_strings.h>
  53. #include <ipxe/efi/efi_snp.h>
  54. #include <ipxe/efi/efi_utils.h>
  55. #include <ipxe/efi/efi_block.h>
  56. /** ACPI table protocol protocol */
  57. static EFI_ACPI_TABLE_PROTOCOL *acpi;
  58. EFI_REQUEST_PROTOCOL ( EFI_ACPI_TABLE_PROTOCOL, &acpi );
  59. /** Boot filename */
  60. static wchar_t efi_block_boot_filename[] = EFI_REMOVABLE_MEDIA_FILE_NAME;
  61. /** iPXE EFI block device vendor device path GUID */
  62. #define IPXE_BLOCK_DEVICE_PATH_GUID \
  63. { 0x8998b594, 0xf531, 0x4e87, \
  64. { 0x8b, 0xdf, 0x8f, 0x88, 0x54, 0x3e, 0x99, 0xd4 } }
  65. /** iPXE EFI block device vendor device path GUID */
  66. static EFI_GUID ipxe_block_device_path_guid
  67. = IPXE_BLOCK_DEVICE_PATH_GUID;
  68. /** An iPXE EFI block device vendor device path */
  69. struct efi_block_vendor_path {
  70. /** Generic vendor device path */
  71. VENDOR_DEVICE_PATH vendor;
  72. /** Block device URI */
  73. CHAR16 uri[0];
  74. } __attribute__ (( packed ));
  75. /** EFI SAN device private data */
  76. struct efi_block_data {
  77. /** SAN device */
  78. struct san_device *sandev;
  79. /** EFI handle */
  80. EFI_HANDLE handle;
  81. /** Media descriptor */
  82. EFI_BLOCK_IO_MEDIA media;
  83. /** Block I/O protocol */
  84. EFI_BLOCK_IO_PROTOCOL block_io;
  85. /** Device path protocol */
  86. EFI_DEVICE_PATH_PROTOCOL *path;
  87. };
  88. /**
  89. * Read from or write to EFI block device
  90. *
  91. * @v sandev SAN device
  92. * @v lba Starting LBA
  93. * @v data Data buffer
  94. * @v len Size of buffer
  95. * @v block_rw Block read/write method
  96. * @ret rc Return status code
  97. */
  98. static int efi_block_rw ( struct san_device *sandev, uint64_t lba,
  99. void *data, size_t len,
  100. int ( * block_rw ) ( struct interface *control,
  101. struct interface *data,
  102. uint64_t lba, unsigned int count,
  103. userptr_t buffer, size_t len ) ){
  104. struct efi_block_data *block = sandev->priv;
  105. unsigned int count;
  106. int rc;
  107. /* Sanity check */
  108. count = ( len / block->media.BlockSize );
  109. if ( ( count * block->media.BlockSize ) != len ) {
  110. DBGC ( sandev, "EFIBLK %#02x impossible length %#zx\n",
  111. sandev->drive, len );
  112. return -EINVAL;
  113. }
  114. /* Read from / write to block device */
  115. if ( ( rc = sandev_rw ( sandev, lba, count, virt_to_user ( data ),
  116. block_rw ) ) != 0 ) {
  117. DBGC ( sandev, "EFIBLK %#02x I/O failed: %s\n",
  118. sandev->drive, strerror ( rc ) );
  119. return rc;
  120. }
  121. return 0;
  122. }
  123. /**
  124. * Reset EFI block device
  125. *
  126. * @v block_io Block I/O protocol
  127. * @v verify Perform extended verification
  128. * @ret efirc EFI status code
  129. */
  130. static EFI_STATUS EFIAPI efi_block_io_reset ( EFI_BLOCK_IO_PROTOCOL *block_io,
  131. BOOLEAN verify __unused ) {
  132. struct efi_block_data *block =
  133. container_of ( block_io, struct efi_block_data, block_io );
  134. struct san_device *sandev = block->sandev;
  135. int rc;
  136. DBGC2 ( sandev, "EFIBLK %#02x reset\n", sandev->drive );
  137. efi_snp_claim();
  138. rc = sandev_reset ( sandev );
  139. efi_snp_release();
  140. return EFIRC ( rc );
  141. }
  142. /**
  143. * Read from EFI block device
  144. *
  145. * @v block_io Block I/O protocol
  146. * @v media Media identifier
  147. * @v lba Starting LBA
  148. * @v len Size of buffer
  149. * @v data Data buffer
  150. * @ret efirc EFI status code
  151. */
  152. static EFI_STATUS EFIAPI
  153. efi_block_io_read ( EFI_BLOCK_IO_PROTOCOL *block_io, UINT32 media __unused,
  154. EFI_LBA lba, UINTN len, VOID *data ) {
  155. struct efi_block_data *block =
  156. container_of ( block_io, struct efi_block_data, block_io );
  157. struct san_device *sandev = block->sandev;
  158. int rc;
  159. DBGC2 ( sandev, "EFIBLK %#02x read LBA %#08llx to %p+%#08zx\n",
  160. sandev->drive, lba, data, ( ( size_t ) len ) );
  161. efi_snp_claim();
  162. rc = efi_block_rw ( sandev, lba, data, len, block_read );
  163. efi_snp_release();
  164. return EFIRC ( rc );
  165. }
  166. /**
  167. * Write to EFI block device
  168. *
  169. * @v block_io Block I/O protocol
  170. * @v media Media identifier
  171. * @v lba Starting LBA
  172. * @v len Size of buffer
  173. * @v data Data buffer
  174. * @ret efirc EFI status code
  175. */
  176. static EFI_STATUS EFIAPI
  177. efi_block_io_write ( EFI_BLOCK_IO_PROTOCOL *block_io, UINT32 media __unused,
  178. EFI_LBA lba, UINTN len, VOID *data ) {
  179. struct efi_block_data *block =
  180. container_of ( block_io, struct efi_block_data, block_io );
  181. struct san_device *sandev = block->sandev;
  182. int rc;
  183. DBGC2 ( sandev, "EFIBLK %#02x write LBA %#08llx from %p+%#08zx\n",
  184. sandev->drive, lba, data, ( ( size_t ) len ) );
  185. efi_snp_claim();
  186. rc = efi_block_rw ( sandev, lba, data, len, block_write );
  187. efi_snp_release();
  188. return EFIRC ( rc );
  189. }
  190. /**
  191. * Flush data to EFI block device
  192. *
  193. * @v block_io Block I/O protocol
  194. * @ret efirc EFI status code
  195. */
  196. static EFI_STATUS EFIAPI
  197. efi_block_io_flush ( EFI_BLOCK_IO_PROTOCOL *block_io ) {
  198. struct efi_block_data *block =
  199. container_of ( block_io, struct efi_block_data, block_io );
  200. struct san_device *sandev = block->sandev;
  201. DBGC2 ( sandev, "EFIBLK %#02x flush\n", sandev->drive );
  202. /* Nothing to do */
  203. return 0;
  204. }
  205. /**
  206. * Connect all possible drivers to EFI block device
  207. *
  208. * @v sandev SAN device
  209. */
  210. static void efi_block_connect ( struct san_device *sandev ) {
  211. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  212. struct efi_block_data *block = sandev->priv;
  213. EFI_STATUS efirc;
  214. int rc;
  215. /* Try to connect all possible drivers to this block device */
  216. if ( ( efirc = bs->ConnectController ( block->handle, NULL,
  217. NULL, 1 ) ) != 0 ) {
  218. rc = -EEFI ( efirc );
  219. DBGC ( sandev, "EFIBLK %#02x could not connect drivers: %s\n",
  220. sandev->drive, strerror ( rc ) );
  221. /* May not be an error; may already be connected */
  222. }
  223. DBGC2 ( sandev, "EFIBLK %#02x supports protocols:\n", sandev->drive );
  224. DBGC2_EFI_PROTOCOLS ( sandev, block->handle );
  225. }
  226. /**
  227. * Hook EFI block device
  228. *
  229. * @v uri URI
  230. * @v drive Drive number
  231. * @ret drive Drive number, or negative error
  232. */
  233. static int efi_block_hook ( struct uri *uri, unsigned int drive ) {
  234. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  235. EFI_DEVICE_PATH_PROTOCOL *end;
  236. struct efi_block_vendor_path *vendor;
  237. struct efi_snp_device *snpdev;
  238. struct san_device *sandev;
  239. struct efi_block_data *block;
  240. size_t prefix_len;
  241. size_t uri_len;
  242. size_t vendor_len;
  243. size_t len;
  244. char *uri_buf;
  245. EFI_STATUS efirc;
  246. int rc;
  247. /* Find an appropriate parent device handle */
  248. snpdev = last_opened_snpdev();
  249. if ( ! snpdev ) {
  250. DBG ( "EFIBLK could not identify SNP device\n" );
  251. rc = -ENODEV;
  252. goto err_no_snpdev;
  253. }
  254. /* Calculate length of private data */
  255. prefix_len = efi_devpath_len ( snpdev->path );
  256. uri_len = format_uri ( uri, NULL, 0 );
  257. vendor_len = ( sizeof ( *vendor ) +
  258. ( ( uri_len + 1 /* NUL */ ) * sizeof ( wchar_t ) ) );
  259. len = ( sizeof ( *block ) + uri_len + 1 /* NUL */ + prefix_len +
  260. vendor_len + sizeof ( *end ) );
  261. /* Allocate and initialise structure */
  262. sandev = alloc_sandev ( uri, len );
  263. if ( ! sandev ) {
  264. rc = -ENOMEM;
  265. goto err_alloc;
  266. }
  267. sandev->drive = drive;
  268. block = sandev->priv;
  269. block->sandev = sandev;
  270. block->media.MediaPresent = 1;
  271. block->media.LogicalBlocksPerPhysicalBlock = 1;
  272. block->block_io.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
  273. block->block_io.Media = &block->media;
  274. block->block_io.Reset = efi_block_io_reset;
  275. block->block_io.ReadBlocks = efi_block_io_read;
  276. block->block_io.WriteBlocks = efi_block_io_write;
  277. block->block_io.FlushBlocks = efi_block_io_flush;
  278. uri_buf = ( ( ( void * ) block ) + sizeof ( *block ) );
  279. block->path = ( ( ( void * ) uri_buf ) + uri_len + 1 /* NUL */ );
  280. /* Construct device path */
  281. memcpy ( block->path, snpdev->path, prefix_len );
  282. vendor = ( ( ( void * ) block->path ) + prefix_len );
  283. vendor->vendor.Header.Type = HARDWARE_DEVICE_PATH;
  284. vendor->vendor.Header.SubType = HW_VENDOR_DP;
  285. vendor->vendor.Header.Length[0] = ( vendor_len & 0xff );
  286. vendor->vendor.Header.Length[1] = ( vendor_len >> 8 );
  287. memcpy ( &vendor->vendor.Guid, &ipxe_block_device_path_guid,
  288. sizeof ( vendor->vendor.Guid ) );
  289. format_uri ( uri, uri_buf, ( uri_len + 1 /* NUL */ ) );
  290. efi_snprintf ( vendor->uri, ( uri_len + 1 /* NUL */ ), "%s", uri_buf );
  291. end = ( ( ( void * ) vendor ) + vendor_len );
  292. end->Type = END_DEVICE_PATH_TYPE;
  293. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  294. end->Length[0] = sizeof ( *end );
  295. DBGC ( sandev, "EFIBLK %#02x has device path %s\n",
  296. sandev->drive, efi_devpath_text ( block->path ) );
  297. /* Register SAN device */
  298. if ( ( rc = register_sandev ( sandev ) ) != 0 ) {
  299. DBGC ( sandev, "EFIBLK %#02x could not register: %s\n",
  300. sandev->drive, strerror ( rc ) );
  301. goto err_register;
  302. }
  303. /* Update media descriptor */
  304. block->media.BlockSize =
  305. ( sandev->capacity.blksize << sandev->blksize_shift );
  306. block->media.LastBlock =
  307. ( ( sandev->capacity.blocks >> sandev->blksize_shift ) - 1 );
  308. /* Install protocols */
  309. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  310. &block->handle,
  311. &efi_block_io_protocol_guid, &block->block_io,
  312. &efi_device_path_protocol_guid, block->path,
  313. NULL ) ) != 0 ) {
  314. rc = -EEFI ( efirc );
  315. DBGC ( sandev, "EFIBLK %#02x could not install protocols: %s\n",
  316. sandev->drive, strerror ( rc ) );
  317. goto err_install;
  318. }
  319. /* Connect all possible protocols */
  320. efi_block_connect ( sandev );
  321. return drive;
  322. bs->UninstallMultipleProtocolInterfaces (
  323. block->handle,
  324. &efi_block_io_protocol_guid, &block->block_io,
  325. &efi_device_path_protocol_guid, block->path, NULL );
  326. err_install:
  327. unregister_sandev ( sandev );
  328. err_register:
  329. sandev_put ( sandev );
  330. err_alloc:
  331. err_no_snpdev:
  332. return rc;
  333. }
  334. /**
  335. * Unhook EFI block device
  336. *
  337. * @v drive Drive number
  338. */
  339. static void efi_block_unhook ( unsigned int drive ) {
  340. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  341. struct san_device *sandev;
  342. struct efi_block_data *block;
  343. /* Find SAN device */
  344. sandev = sandev_find ( drive );
  345. if ( ! sandev ) {
  346. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  347. return;
  348. }
  349. block = sandev->priv;
  350. /* Uninstall protocols */
  351. bs->UninstallMultipleProtocolInterfaces (
  352. block->handle,
  353. &efi_block_io_protocol_guid, &block->block_io,
  354. &efi_device_path_protocol_guid, block->path, NULL );
  355. /* Unregister SAN device */
  356. unregister_sandev ( sandev );
  357. /* Drop reference to drive */
  358. sandev_put ( sandev );
  359. }
  360. /**
  361. * Describe EFI block device
  362. *
  363. * @v drive Drive number
  364. * @ret rc Return status code
  365. */
  366. static int efi_block_describe ( unsigned int drive ) {
  367. static union {
  368. /** ACPI header */
  369. struct acpi_description_header acpi;
  370. /** Padding */
  371. char pad[768];
  372. } xbftab;
  373. static UINTN key;
  374. struct san_device *sandev;
  375. size_t len;
  376. EFI_STATUS efirc;
  377. int rc;
  378. /* Find SAN device */
  379. sandev = sandev_find ( drive );
  380. if ( ! sandev ) {
  381. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  382. return -ENODEV;
  383. }
  384. /* Sanity check */
  385. if ( ! acpi ) {
  386. DBGC ( sandev, "EFIBLK %#02x has no ACPI table protocol\n",
  387. sandev->drive );
  388. return -ENOTSUP;
  389. }
  390. /* Remove existing table, if any */
  391. if ( key ) {
  392. if ( ( efirc = acpi->UninstallAcpiTable ( acpi, key ) ) != 0 ) {
  393. rc = -EEFI ( efirc );
  394. DBGC ( sandev, "EFIBLK %#02x could not uninstall ACPI "
  395. "table: %s\n", sandev->drive, strerror ( rc ) );
  396. /* Continue anyway */
  397. }
  398. key = 0;
  399. }
  400. /* Reopen block device if necessary */
  401. if ( sandev_needs_reopen ( sandev ) &&
  402. ( ( rc = sandev_reopen ( sandev ) ) != 0 ) )
  403. return rc;
  404. /* Clear table */
  405. memset ( &xbftab, 0, sizeof ( xbftab ) );
  406. /* Fill in common parameters */
  407. strncpy ( xbftab.acpi.oem_id, "FENSYS",
  408. sizeof ( xbftab.acpi.oem_id ) );
  409. strncpy ( xbftab.acpi.oem_table_id, "iPXE",
  410. sizeof ( xbftab.acpi.oem_table_id ) );
  411. /* Fill in remaining parameters */
  412. if ( ( rc = acpi_describe ( &sandev->block, &xbftab.acpi,
  413. sizeof ( xbftab ) ) ) != 0 ) {
  414. DBGC ( sandev, "EFIBLK %#02x could not create ACPI "
  415. "description: %s\n", sandev->drive, strerror ( rc ) );
  416. return rc;
  417. }
  418. len = le32_to_cpu ( xbftab.acpi.length );
  419. /* Fix up ACPI checksum */
  420. acpi_fix_checksum ( &xbftab.acpi );
  421. /* Install table */
  422. if ( ( efirc = acpi->InstallAcpiTable ( acpi, &xbftab, len,
  423. &key ) ) != 0 ) {
  424. rc = -EEFI ( efirc );
  425. DBGC ( sandev, "EFIBLK %#02x could not install ACPI table: "
  426. "%s\n", sandev->drive, strerror ( rc ) );
  427. return rc;
  428. }
  429. return 0;
  430. }
  431. /**
  432. * Try booting from child device of EFI block device
  433. *
  434. * @v sandev SAN device
  435. * @v handle EFI handle
  436. * @ret rc Return status code
  437. */
  438. static int efi_block_boot_image ( struct san_device *sandev, EFI_HANDLE handle,
  439. EFI_HANDLE *image ) {
  440. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  441. struct efi_block_data *block = sandev->priv;
  442. union {
  443. EFI_DEVICE_PATH_PROTOCOL *path;
  444. void *interface;
  445. } path;
  446. EFI_DEVICE_PATH_PROTOCOL *boot_path;
  447. FILEPATH_DEVICE_PATH *filepath;
  448. EFI_DEVICE_PATH_PROTOCOL *end;
  449. size_t prefix_len;
  450. size_t filepath_len;
  451. size_t boot_path_len;
  452. EFI_STATUS efirc;
  453. int rc;
  454. /* Identify device path */
  455. if ( ( efirc = bs->OpenProtocol ( handle,
  456. &efi_device_path_protocol_guid,
  457. &path.interface, efi_image_handle,
  458. handle,
  459. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  460. DBGC ( sandev, "EFIBLK %#02x found filesystem with no device "
  461. "path??", sandev->drive );
  462. rc = -EEFI ( efirc );
  463. goto err_open_device_path;
  464. }
  465. /* Check if this device is a child of our block device */
  466. prefix_len = efi_devpath_len ( block->path );
  467. if ( memcmp ( path.path, block->path, prefix_len ) != 0 ) {
  468. /* Not a child device */
  469. rc = -ENOTTY;
  470. goto err_not_child;
  471. }
  472. DBGC ( sandev, "EFIBLK %#02x found child device %s\n",
  473. sandev->drive, efi_devpath_text ( path.path ) );
  474. /* Construct device path for boot image */
  475. end = efi_devpath_end ( path.path );
  476. prefix_len = ( ( ( void * ) end ) - ( ( void * ) path.path ) );
  477. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  478. sizeof ( efi_block_boot_filename ) );
  479. boot_path_len = ( prefix_len + filepath_len + sizeof ( *end ) );
  480. boot_path = zalloc ( boot_path_len );
  481. if ( ! boot_path ) {
  482. rc = -ENOMEM;
  483. goto err_alloc_path;
  484. }
  485. memcpy ( boot_path, path.path, prefix_len );
  486. filepath = ( ( ( void * ) boot_path ) + prefix_len );
  487. filepath->Header.Type = MEDIA_DEVICE_PATH;
  488. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  489. filepath->Header.Length[0] = ( filepath_len & 0xff );
  490. filepath->Header.Length[1] = ( filepath_len >> 8 );
  491. memcpy ( filepath->PathName, efi_block_boot_filename,
  492. sizeof ( efi_block_boot_filename ) );
  493. end = ( ( ( void * ) filepath ) + filepath_len );
  494. end->Type = END_DEVICE_PATH_TYPE;
  495. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  496. end->Length[0] = sizeof ( *end );
  497. DBGC ( sandev, "EFIBLK %#02x trying to load %s\n",
  498. sandev->drive, efi_devpath_text ( boot_path ) );
  499. /* Try loading boot image from this device */
  500. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, boot_path,
  501. NULL, 0, image ) ) != 0 ) {
  502. rc = -EEFI ( efirc );
  503. DBGC ( sandev, "EFIBLK %#02x could not load image: %s\n",
  504. sandev->drive, strerror ( rc ) );
  505. goto err_load_image;
  506. }
  507. /* Success */
  508. rc = 0;
  509. err_load_image:
  510. free ( boot_path );
  511. err_alloc_path:
  512. err_not_child:
  513. err_open_device_path:
  514. return rc;
  515. }
  516. /**
  517. * Boot from EFI block device
  518. *
  519. * @v drive Drive number
  520. * @ret rc Return status code
  521. */
  522. static int efi_block_boot ( unsigned int drive ) {
  523. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  524. struct san_device *sandev;
  525. EFI_HANDLE *handles;
  526. EFI_HANDLE image = NULL;
  527. UINTN count;
  528. unsigned int i;
  529. EFI_STATUS efirc;
  530. int rc;
  531. /* Find SAN device */
  532. sandev = sandev_find ( drive );
  533. if ( ! sandev ) {
  534. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  535. rc = -ENODEV;
  536. goto err_sandev_find;
  537. }
  538. /* Connect all possible protocols */
  539. efi_block_connect ( sandev );
  540. /* Locate all handles supporting the Simple File System protocol */
  541. if ( ( efirc = bs->LocateHandleBuffer (
  542. ByProtocol, &efi_simple_file_system_protocol_guid,
  543. NULL, &count, &handles ) ) != 0 ) {
  544. rc = -EEFI ( efirc );
  545. DBGC ( sandev, "EFIBLK %#02x cannot locate file systems: %s\n",
  546. sandev->drive, strerror ( rc ) );
  547. goto err_locate_file_systems;
  548. }
  549. /* Try booting from any available child device containing a
  550. * suitable boot image. This is something of a wild stab in
  551. * the dark, but should end up conforming to user expectations
  552. * most of the time.
  553. */
  554. rc = -ENOENT;
  555. for ( i = 0 ; i < count ; i++ ) {
  556. if ( ( rc = efi_block_boot_image ( sandev, handles[i],
  557. &image ) ) != 0 )
  558. continue;
  559. DBGC ( sandev, "EFIBLK %#02x found boot image\n",
  560. sandev->drive );
  561. efirc = bs->StartImage ( image, NULL, NULL );
  562. rc = ( efirc ? -EEFI ( efirc ) : 0 );
  563. bs->UnloadImage ( image );
  564. DBGC ( sandev, "EFIBLK %#02x boot image returned: %s\n",
  565. sandev->drive, strerror ( rc ) );
  566. break;
  567. }
  568. bs->FreePool ( handles );
  569. err_locate_file_systems:
  570. err_sandev_find:
  571. return rc;
  572. }
  573. PROVIDE_SANBOOT ( efi, san_hook, efi_block_hook );
  574. PROVIDE_SANBOOT ( efi, san_unhook, efi_block_unhook );
  575. PROVIDE_SANBOOT ( efi, san_describe, efi_block_describe );
  576. PROVIDE_SANBOOT ( efi, san_boot, efi_block_boot );