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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. * @v flags Flags
  233. * @ret drive Drive number, or negative error
  234. */
  235. static int efi_block_hook ( unsigned int drive, struct uri **uris,
  236. unsigned int count, unsigned int flags ) {
  237. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  238. EFI_DEVICE_PATH_PROTOCOL *end;
  239. struct efi_block_vendor_path *vendor;
  240. struct efi_snp_device *snpdev;
  241. struct san_device *sandev;
  242. struct efi_block_data *block;
  243. size_t prefix_len;
  244. size_t uri_len;
  245. size_t vendor_len;
  246. size_t len;
  247. char *uri_buf;
  248. EFI_STATUS efirc;
  249. int rc;
  250. /* Sanity check */
  251. if ( ! count ) {
  252. DBG ( "EFIBLK has no URIs\n" );
  253. rc = -ENOTTY;
  254. goto err_no_uris;
  255. }
  256. /* Find an appropriate parent device handle */
  257. snpdev = last_opened_snpdev();
  258. if ( ! snpdev ) {
  259. DBG ( "EFIBLK could not identify SNP device\n" );
  260. rc = -ENODEV;
  261. goto err_no_snpdev;
  262. }
  263. /* Calculate length of private data */
  264. prefix_len = efi_devpath_len ( snpdev->path );
  265. uri_len = format_uri ( uris[0], NULL, 0 );
  266. vendor_len = ( sizeof ( *vendor ) +
  267. ( ( uri_len + 1 /* NUL */ ) * sizeof ( wchar_t ) ) );
  268. len = ( sizeof ( *block ) + uri_len + 1 /* NUL */ + prefix_len +
  269. vendor_len + sizeof ( *end ) );
  270. /* Allocate and initialise structure */
  271. sandev = alloc_sandev ( uris, count, len );
  272. if ( ! sandev ) {
  273. rc = -ENOMEM;
  274. goto err_alloc;
  275. }
  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. drive, efi_devpath_text ( block->path ) );
  305. /* Register SAN device */
  306. if ( ( rc = register_sandev ( sandev, drive, flags ) ) != 0 ) {
  307. DBGC ( sandev, "EFIBLK %#02x could not register: %s\n",
  308. 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. /** An installed ACPI table */
  370. struct efi_acpi_table {
  371. /** List of installed tables */
  372. struct list_head list;
  373. /** Table key */
  374. UINTN key;
  375. };
  376. /** List of installed ACPI tables */
  377. static LIST_HEAD ( efi_acpi_tables );
  378. /**
  379. * Install ACPI table
  380. *
  381. * @v hdr ACPI description header
  382. * @ret rc Return status code
  383. */
  384. static int efi_block_install ( struct acpi_header *hdr ) {
  385. size_t len = le32_to_cpu ( hdr->length );
  386. struct efi_acpi_table *installed;
  387. EFI_STATUS efirc;
  388. int rc;
  389. /* Allocate installed table record */
  390. installed = zalloc ( sizeof ( *installed ) );
  391. if ( ! installed ) {
  392. rc = -ENOMEM;
  393. goto err_alloc;
  394. }
  395. /* Fill in common parameters */
  396. strncpy ( hdr->oem_id, "FENSYS", sizeof ( hdr->oem_id ) );
  397. strncpy ( hdr->oem_table_id, "iPXE", sizeof ( hdr->oem_table_id ) );
  398. /* Fix up ACPI checksum */
  399. acpi_fix_checksum ( hdr );
  400. /* Install table */
  401. if ( ( efirc = acpi->InstallAcpiTable ( acpi, hdr, len,
  402. &installed->key ) ) != 0 ){
  403. rc = -EEFI ( efirc );
  404. DBGC ( acpi, "EFIBLK could not install %s: %s\n",
  405. acpi_name ( hdr->signature ), strerror ( rc ) );
  406. DBGC_HDA ( acpi, 0, hdr, len );
  407. goto err_install;
  408. }
  409. /* Add to list of installed tables */
  410. list_add_tail ( &installed->list, &efi_acpi_tables );
  411. DBGC ( acpi, "EFIBLK installed %s as ACPI table %#lx:\n",
  412. acpi_name ( hdr->signature ),
  413. ( ( unsigned long ) installed->key ) );
  414. DBGC_HDA ( acpi, 0, hdr, len );
  415. return 0;
  416. list_del ( &installed->list );
  417. err_install:
  418. free ( installed );
  419. err_alloc:
  420. return rc;
  421. }
  422. /**
  423. * Describe EFI block devices
  424. *
  425. * @ret rc Return status code
  426. */
  427. static int efi_block_describe ( void ) {
  428. struct efi_acpi_table *installed;
  429. struct efi_acpi_table *tmp;
  430. UINTN key;
  431. EFI_STATUS efirc;
  432. int rc;
  433. /* Sanity check */
  434. if ( ! acpi ) {
  435. DBG ( "EFIBLK has no ACPI table protocol\n" );
  436. return -ENOTSUP;
  437. }
  438. /* Uninstall any existing ACPI tables */
  439. list_for_each_entry_safe ( installed, tmp, &efi_acpi_tables, list ) {
  440. key = installed->key;
  441. if ( ( efirc = acpi->UninstallAcpiTable ( acpi, key ) ) != 0 ) {
  442. rc = -EEFI ( efirc );
  443. DBGC ( acpi, "EFIBLK could not uninstall ACPI table "
  444. "%#lx: %s\n", ( ( unsigned long ) key ),
  445. strerror ( rc ) );
  446. /* Continue anyway */
  447. }
  448. list_del ( &installed->list );
  449. free ( installed );
  450. }
  451. /* Install ACPI tables */
  452. if ( ( rc = acpi_install ( efi_block_install ) ) != 0 ) {
  453. DBGC ( acpi, "EFIBLK could not install ACPI tables: %s\n",
  454. strerror ( rc ) );
  455. return rc;
  456. }
  457. return 0;
  458. }
  459. /**
  460. * Try booting from child device of EFI block device
  461. *
  462. * @v sandev SAN device
  463. * @v handle EFI handle
  464. * @v filename Filename (or NULL to use default)
  465. * @v image Image handle to fill in
  466. * @ret rc Return status code
  467. */
  468. static int efi_block_boot_image ( struct san_device *sandev, EFI_HANDLE handle,
  469. const char *filename, EFI_HANDLE *image ) {
  470. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  471. struct efi_block_data *block = sandev->priv;
  472. union {
  473. EFI_DEVICE_PATH_PROTOCOL *path;
  474. void *interface;
  475. } path;
  476. EFI_DEVICE_PATH_PROTOCOL *boot_path;
  477. FILEPATH_DEVICE_PATH *filepath;
  478. EFI_DEVICE_PATH_PROTOCOL *end;
  479. size_t prefix_len;
  480. size_t filepath_len;
  481. size_t boot_path_len;
  482. EFI_STATUS efirc;
  483. int rc;
  484. /* Identify device path */
  485. if ( ( efirc = bs->OpenProtocol ( handle,
  486. &efi_device_path_protocol_guid,
  487. &path.interface, efi_image_handle,
  488. handle,
  489. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  490. DBGC ( sandev, "EFIBLK %#02x found filesystem with no device "
  491. "path??", sandev->drive );
  492. rc = -EEFI ( efirc );
  493. goto err_open_device_path;
  494. }
  495. /* Check if this device is a child of our block device */
  496. prefix_len = efi_devpath_len ( block->path );
  497. if ( memcmp ( path.path, block->path, prefix_len ) != 0 ) {
  498. /* Not a child device */
  499. rc = -ENOTTY;
  500. goto err_not_child;
  501. }
  502. DBGC ( sandev, "EFIBLK %#02x found child device %s\n",
  503. sandev->drive, efi_devpath_text ( path.path ) );
  504. /* Construct device path for boot image */
  505. end = efi_devpath_end ( path.path );
  506. prefix_len = ( ( ( void * ) end ) - ( ( void * ) path.path ) );
  507. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  508. ( filename ?
  509. ( ( strlen ( filename ) + 1 /* NUL */ ) *
  510. sizeof ( filepath->PathName[0] ) ) :
  511. sizeof ( efi_block_boot_filename ) ) );
  512. boot_path_len = ( prefix_len + filepath_len + sizeof ( *end ) );
  513. boot_path = zalloc ( boot_path_len );
  514. if ( ! boot_path ) {
  515. rc = -ENOMEM;
  516. goto err_alloc_path;
  517. }
  518. memcpy ( boot_path, path.path, prefix_len );
  519. filepath = ( ( ( void * ) boot_path ) + prefix_len );
  520. filepath->Header.Type = MEDIA_DEVICE_PATH;
  521. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  522. filepath->Header.Length[0] = ( filepath_len & 0xff );
  523. filepath->Header.Length[1] = ( filepath_len >> 8 );
  524. if ( filename ) {
  525. efi_sprintf ( filepath->PathName, "%s", filename );
  526. } else {
  527. memcpy ( filepath->PathName, efi_block_boot_filename,
  528. sizeof ( efi_block_boot_filename ) );
  529. }
  530. end = ( ( ( void * ) filepath ) + filepath_len );
  531. end->Type = END_DEVICE_PATH_TYPE;
  532. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  533. end->Length[0] = sizeof ( *end );
  534. DBGC ( sandev, "EFIBLK %#02x trying to load %s\n",
  535. sandev->drive, efi_devpath_text ( boot_path ) );
  536. /* Try loading boot image from this device */
  537. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, boot_path,
  538. NULL, 0, image ) ) != 0 ) {
  539. rc = -EEFI ( efirc );
  540. DBGC ( sandev, "EFIBLK %#02x could not load image: %s\n",
  541. sandev->drive, strerror ( rc ) );
  542. goto err_load_image;
  543. }
  544. /* Success */
  545. rc = 0;
  546. err_load_image:
  547. free ( boot_path );
  548. err_alloc_path:
  549. err_not_child:
  550. err_open_device_path:
  551. return rc;
  552. }
  553. /**
  554. * Boot from EFI block device
  555. *
  556. * @v drive Drive number
  557. * @v filename Filename (or NULL to use default)
  558. * @ret rc Return status code
  559. */
  560. static int efi_block_boot ( unsigned int drive, const char *filename ) {
  561. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  562. struct san_device *sandev;
  563. EFI_HANDLE *handles;
  564. EFI_HANDLE image = NULL;
  565. UINTN count;
  566. unsigned int i;
  567. EFI_STATUS efirc;
  568. int rc;
  569. /* Find SAN device */
  570. sandev = sandev_find ( drive );
  571. if ( ! sandev ) {
  572. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  573. rc = -ENODEV;
  574. goto err_sandev_find;
  575. }
  576. /* Connect all possible protocols */
  577. efi_block_connect ( sandev );
  578. /* Locate all handles supporting the Simple File System protocol */
  579. if ( ( efirc = bs->LocateHandleBuffer (
  580. ByProtocol, &efi_simple_file_system_protocol_guid,
  581. NULL, &count, &handles ) ) != 0 ) {
  582. rc = -EEFI ( efirc );
  583. DBGC ( sandev, "EFIBLK %#02x cannot locate file systems: %s\n",
  584. sandev->drive, strerror ( rc ) );
  585. goto err_locate_file_systems;
  586. }
  587. /* Try booting from any available child device containing a
  588. * suitable boot image. This is something of a wild stab in
  589. * the dark, but should end up conforming to user expectations
  590. * most of the time.
  591. */
  592. rc = -ENOENT;
  593. for ( i = 0 ; i < count ; i++ ) {
  594. if ( ( rc = efi_block_boot_image ( sandev, handles[i], filename,
  595. &image ) ) != 0 )
  596. continue;
  597. DBGC ( sandev, "EFIBLK %#02x found boot image\n",
  598. sandev->drive );
  599. efirc = bs->StartImage ( image, NULL, NULL );
  600. rc = ( efirc ? -EEFI ( efirc ) : 0 );
  601. bs->UnloadImage ( image );
  602. DBGC ( sandev, "EFIBLK %#02x boot image returned: %s\n",
  603. sandev->drive, strerror ( rc ) );
  604. break;
  605. }
  606. bs->FreePool ( handles );
  607. err_locate_file_systems:
  608. err_sandev_find:
  609. return rc;
  610. }
  611. PROVIDE_SANBOOT ( efi, san_hook, efi_block_hook );
  612. PROVIDE_SANBOOT ( efi, san_unhook, efi_block_unhook );
  613. PROVIDE_SANBOOT ( efi, san_describe, efi_block_describe );
  614. PROVIDE_SANBOOT ( efi, san_boot, efi_block_boot );