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

efi_block.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  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 drive Drive number
  230. * @v uris List of URIs
  231. * @v count Number of URIs
  232. * @ret drive Drive number, or negative error
  233. */
  234. static int efi_block_hook ( unsigned int drive, struct uri **uris,
  235. unsigned int count ) {
  236. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  237. EFI_DEVICE_PATH_PROTOCOL *end;
  238. struct efi_block_vendor_path *vendor;
  239. struct efi_snp_device *snpdev;
  240. struct san_device *sandev;
  241. struct efi_block_data *block;
  242. size_t prefix_len;
  243. size_t uri_len;
  244. size_t vendor_len;
  245. size_t len;
  246. char *uri_buf;
  247. EFI_STATUS efirc;
  248. int rc;
  249. /* Sanity check */
  250. if ( ! count ) {
  251. DBG ( "EFIBLK has no URIs\n" );
  252. rc = -ENOTTY;
  253. goto err_no_uris;
  254. }
  255. /* Find an appropriate parent device handle */
  256. snpdev = last_opened_snpdev();
  257. if ( ! snpdev ) {
  258. DBG ( "EFIBLK could not identify SNP device\n" );
  259. rc = -ENODEV;
  260. goto err_no_snpdev;
  261. }
  262. /* Calculate length of private data */
  263. prefix_len = efi_devpath_len ( snpdev->path );
  264. uri_len = format_uri ( uris[0], NULL, 0 );
  265. vendor_len = ( sizeof ( *vendor ) +
  266. ( ( uri_len + 1 /* NUL */ ) * sizeof ( wchar_t ) ) );
  267. len = ( sizeof ( *block ) + uri_len + 1 /* NUL */ + prefix_len +
  268. vendor_len + sizeof ( *end ) );
  269. /* Allocate and initialise structure */
  270. sandev = alloc_sandev ( uris, count, len );
  271. if ( ! sandev ) {
  272. rc = -ENOMEM;
  273. goto err_alloc;
  274. }
  275. sandev->drive = drive;
  276. block = sandev->priv;
  277. block->sandev = sandev;
  278. block->media.MediaPresent = 1;
  279. block->media.LogicalBlocksPerPhysicalBlock = 1;
  280. block->block_io.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
  281. block->block_io.Media = &block->media;
  282. block->block_io.Reset = efi_block_io_reset;
  283. block->block_io.ReadBlocks = efi_block_io_read;
  284. block->block_io.WriteBlocks = efi_block_io_write;
  285. block->block_io.FlushBlocks = efi_block_io_flush;
  286. uri_buf = ( ( ( void * ) block ) + sizeof ( *block ) );
  287. block->path = ( ( ( void * ) uri_buf ) + uri_len + 1 /* NUL */ );
  288. /* Construct device path */
  289. memcpy ( block->path, snpdev->path, prefix_len );
  290. vendor = ( ( ( void * ) block->path ) + prefix_len );
  291. vendor->vendor.Header.Type = HARDWARE_DEVICE_PATH;
  292. vendor->vendor.Header.SubType = HW_VENDOR_DP;
  293. vendor->vendor.Header.Length[0] = ( vendor_len & 0xff );
  294. vendor->vendor.Header.Length[1] = ( vendor_len >> 8 );
  295. memcpy ( &vendor->vendor.Guid, &ipxe_block_device_path_guid,
  296. sizeof ( vendor->vendor.Guid ) );
  297. format_uri ( uris[0], uri_buf, ( uri_len + 1 /* NUL */ ) );
  298. efi_snprintf ( vendor->uri, ( uri_len + 1 /* NUL */ ), "%s", uri_buf );
  299. end = ( ( ( void * ) vendor ) + vendor_len );
  300. end->Type = END_DEVICE_PATH_TYPE;
  301. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  302. end->Length[0] = sizeof ( *end );
  303. DBGC ( sandev, "EFIBLK %#02x has device path %s\n",
  304. sandev->drive, efi_devpath_text ( block->path ) );
  305. /* Register SAN device */
  306. if ( ( rc = register_sandev ( sandev ) ) != 0 ) {
  307. DBGC ( sandev, "EFIBLK %#02x could not register: %s\n",
  308. sandev->drive, strerror ( rc ) );
  309. goto err_register;
  310. }
  311. /* Update media descriptor */
  312. block->media.BlockSize =
  313. ( sandev->capacity.blksize << sandev->blksize_shift );
  314. block->media.LastBlock =
  315. ( ( sandev->capacity.blocks >> sandev->blksize_shift ) - 1 );
  316. /* Install protocols */
  317. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  318. &block->handle,
  319. &efi_block_io_protocol_guid, &block->block_io,
  320. &efi_device_path_protocol_guid, block->path,
  321. NULL ) ) != 0 ) {
  322. rc = -EEFI ( efirc );
  323. DBGC ( sandev, "EFIBLK %#02x could not install protocols: %s\n",
  324. sandev->drive, strerror ( rc ) );
  325. goto err_install;
  326. }
  327. /* Connect all possible protocols */
  328. efi_block_connect ( sandev );
  329. return drive;
  330. bs->UninstallMultipleProtocolInterfaces (
  331. block->handle,
  332. &efi_block_io_protocol_guid, &block->block_io,
  333. &efi_device_path_protocol_guid, block->path, NULL );
  334. err_install:
  335. unregister_sandev ( sandev );
  336. err_register:
  337. sandev_put ( sandev );
  338. err_alloc:
  339. err_no_snpdev:
  340. err_no_uris:
  341. return rc;
  342. }
  343. /**
  344. * Unhook EFI block device
  345. *
  346. * @v drive Drive number
  347. */
  348. static void efi_block_unhook ( unsigned int drive ) {
  349. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  350. struct san_device *sandev;
  351. struct efi_block_data *block;
  352. /* Find SAN device */
  353. sandev = sandev_find ( drive );
  354. if ( ! sandev ) {
  355. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  356. return;
  357. }
  358. block = sandev->priv;
  359. /* Uninstall protocols */
  360. bs->UninstallMultipleProtocolInterfaces (
  361. block->handle,
  362. &efi_block_io_protocol_guid, &block->block_io,
  363. &efi_device_path_protocol_guid, block->path, NULL );
  364. /* Unregister SAN device */
  365. unregister_sandev ( sandev );
  366. /* Drop reference to drive */
  367. sandev_put ( sandev );
  368. }
  369. /**
  370. * Describe EFI block device
  371. *
  372. * @v drive Drive number
  373. * @ret rc Return status code
  374. */
  375. static int efi_block_describe ( unsigned int drive ) {
  376. static union {
  377. /** ACPI header */
  378. struct acpi_description_header acpi;
  379. /** Padding */
  380. char pad[768];
  381. } xbftab;
  382. static UINTN key;
  383. struct san_device *sandev;
  384. struct san_path *sanpath;
  385. size_t len;
  386. EFI_STATUS efirc;
  387. int rc;
  388. /* Find SAN device */
  389. sandev = sandev_find ( drive );
  390. if ( ! sandev ) {
  391. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  392. return -ENODEV;
  393. }
  394. /* Sanity check */
  395. if ( ! acpi ) {
  396. DBGC ( sandev, "EFIBLK %#02x has no ACPI table protocol\n",
  397. sandev->drive );
  398. return -ENOTSUP;
  399. }
  400. /* Remove existing table, if any */
  401. if ( key ) {
  402. if ( ( efirc = acpi->UninstallAcpiTable ( acpi, key ) ) != 0 ) {
  403. rc = -EEFI ( efirc );
  404. DBGC ( sandev, "EFIBLK %#02x could not uninstall ACPI "
  405. "table: %s\n", sandev->drive, strerror ( rc ) );
  406. /* Continue anyway */
  407. }
  408. key = 0;
  409. }
  410. /* Reopen block device if necessary */
  411. if ( sandev_needs_reopen ( sandev ) &&
  412. ( ( rc = sandev_reopen ( sandev ) ) != 0 ) )
  413. return rc;
  414. sanpath = sandev->active;
  415. assert ( sanpath != NULL );
  416. /* Clear table */
  417. memset ( &xbftab, 0, sizeof ( xbftab ) );
  418. /* Fill in common parameters */
  419. strncpy ( xbftab.acpi.oem_id, "FENSYS",
  420. sizeof ( xbftab.acpi.oem_id ) );
  421. strncpy ( xbftab.acpi.oem_table_id, "iPXE",
  422. sizeof ( xbftab.acpi.oem_table_id ) );
  423. /* Fill in remaining parameters */
  424. if ( ( rc = acpi_describe ( &sanpath->block, &xbftab.acpi,
  425. sizeof ( xbftab ) ) ) != 0 ) {
  426. DBGC ( sandev, "EFIBLK %#02x could not create ACPI "
  427. "description: %s\n", sandev->drive, strerror ( rc ) );
  428. return rc;
  429. }
  430. len = le32_to_cpu ( xbftab.acpi.length );
  431. /* Fix up ACPI checksum */
  432. acpi_fix_checksum ( &xbftab.acpi );
  433. /* Install table */
  434. if ( ( efirc = acpi->InstallAcpiTable ( acpi, &xbftab, len,
  435. &key ) ) != 0 ) {
  436. rc = -EEFI ( efirc );
  437. DBGC ( sandev, "EFIBLK %#02x could not install ACPI table: "
  438. "%s\n", sandev->drive, strerror ( rc ) );
  439. return rc;
  440. }
  441. return 0;
  442. }
  443. /**
  444. * Try booting from child device of EFI block device
  445. *
  446. * @v sandev SAN device
  447. * @v handle EFI handle
  448. * @ret rc Return status code
  449. */
  450. static int efi_block_boot_image ( struct san_device *sandev, EFI_HANDLE handle,
  451. EFI_HANDLE *image ) {
  452. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  453. struct efi_block_data *block = sandev->priv;
  454. union {
  455. EFI_DEVICE_PATH_PROTOCOL *path;
  456. void *interface;
  457. } path;
  458. EFI_DEVICE_PATH_PROTOCOL *boot_path;
  459. FILEPATH_DEVICE_PATH *filepath;
  460. EFI_DEVICE_PATH_PROTOCOL *end;
  461. size_t prefix_len;
  462. size_t filepath_len;
  463. size_t boot_path_len;
  464. EFI_STATUS efirc;
  465. int rc;
  466. /* Identify device path */
  467. if ( ( efirc = bs->OpenProtocol ( handle,
  468. &efi_device_path_protocol_guid,
  469. &path.interface, efi_image_handle,
  470. handle,
  471. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  472. DBGC ( sandev, "EFIBLK %#02x found filesystem with no device "
  473. "path??", sandev->drive );
  474. rc = -EEFI ( efirc );
  475. goto err_open_device_path;
  476. }
  477. /* Check if this device is a child of our block device */
  478. prefix_len = efi_devpath_len ( block->path );
  479. if ( memcmp ( path.path, block->path, prefix_len ) != 0 ) {
  480. /* Not a child device */
  481. rc = -ENOTTY;
  482. goto err_not_child;
  483. }
  484. DBGC ( sandev, "EFIBLK %#02x found child device %s\n",
  485. sandev->drive, efi_devpath_text ( path.path ) );
  486. /* Construct device path for boot image */
  487. end = efi_devpath_end ( path.path );
  488. prefix_len = ( ( ( void * ) end ) - ( ( void * ) path.path ) );
  489. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  490. sizeof ( efi_block_boot_filename ) );
  491. boot_path_len = ( prefix_len + filepath_len + sizeof ( *end ) );
  492. boot_path = zalloc ( boot_path_len );
  493. if ( ! boot_path ) {
  494. rc = -ENOMEM;
  495. goto err_alloc_path;
  496. }
  497. memcpy ( boot_path, path.path, prefix_len );
  498. filepath = ( ( ( void * ) boot_path ) + prefix_len );
  499. filepath->Header.Type = MEDIA_DEVICE_PATH;
  500. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  501. filepath->Header.Length[0] = ( filepath_len & 0xff );
  502. filepath->Header.Length[1] = ( filepath_len >> 8 );
  503. memcpy ( filepath->PathName, efi_block_boot_filename,
  504. sizeof ( efi_block_boot_filename ) );
  505. end = ( ( ( void * ) filepath ) + filepath_len );
  506. end->Type = END_DEVICE_PATH_TYPE;
  507. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  508. end->Length[0] = sizeof ( *end );
  509. DBGC ( sandev, "EFIBLK %#02x trying to load %s\n",
  510. sandev->drive, efi_devpath_text ( boot_path ) );
  511. /* Try loading boot image from this device */
  512. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, boot_path,
  513. NULL, 0, image ) ) != 0 ) {
  514. rc = -EEFI ( efirc );
  515. DBGC ( sandev, "EFIBLK %#02x could not load image: %s\n",
  516. sandev->drive, strerror ( rc ) );
  517. goto err_load_image;
  518. }
  519. /* Success */
  520. rc = 0;
  521. err_load_image:
  522. free ( boot_path );
  523. err_alloc_path:
  524. err_not_child:
  525. err_open_device_path:
  526. return rc;
  527. }
  528. /**
  529. * Boot from EFI block device
  530. *
  531. * @v drive Drive number
  532. * @ret rc Return status code
  533. */
  534. static int efi_block_boot ( unsigned int drive ) {
  535. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  536. struct san_device *sandev;
  537. EFI_HANDLE *handles;
  538. EFI_HANDLE image = NULL;
  539. UINTN count;
  540. unsigned int i;
  541. EFI_STATUS efirc;
  542. int rc;
  543. /* Find SAN device */
  544. sandev = sandev_find ( drive );
  545. if ( ! sandev ) {
  546. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  547. rc = -ENODEV;
  548. goto err_sandev_find;
  549. }
  550. /* Connect all possible protocols */
  551. efi_block_connect ( sandev );
  552. /* Locate all handles supporting the Simple File System protocol */
  553. if ( ( efirc = bs->LocateHandleBuffer (
  554. ByProtocol, &efi_simple_file_system_protocol_guid,
  555. NULL, &count, &handles ) ) != 0 ) {
  556. rc = -EEFI ( efirc );
  557. DBGC ( sandev, "EFIBLK %#02x cannot locate file systems: %s\n",
  558. sandev->drive, strerror ( rc ) );
  559. goto err_locate_file_systems;
  560. }
  561. /* Try booting from any available child device containing a
  562. * suitable boot image. This is something of a wild stab in
  563. * the dark, but should end up conforming to user expectations
  564. * most of the time.
  565. */
  566. rc = -ENOENT;
  567. for ( i = 0 ; i < count ; i++ ) {
  568. if ( ( rc = efi_block_boot_image ( sandev, handles[i],
  569. &image ) ) != 0 )
  570. continue;
  571. DBGC ( sandev, "EFIBLK %#02x found boot image\n",
  572. sandev->drive );
  573. efirc = bs->StartImage ( image, NULL, NULL );
  574. rc = ( efirc ? -EEFI ( efirc ) : 0 );
  575. bs->UnloadImage ( image );
  576. DBGC ( sandev, "EFIBLK %#02x boot image returned: %s\n",
  577. sandev->drive, strerror ( rc ) );
  578. break;
  579. }
  580. bs->FreePool ( handles );
  581. err_locate_file_systems:
  582. err_sandev_find:
  583. return rc;
  584. }
  585. PROVIDE_SANBOOT ( efi, san_hook, efi_block_hook );
  586. PROVIDE_SANBOOT ( efi, san_unhook, efi_block_unhook );
  587. PROVIDE_SANBOOT ( efi, san_describe, efi_block_describe );
  588. PROVIDE_SANBOOT ( efi, san_boot, efi_block_boot );