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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 sandev_rw SAN device 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 ( * sandev_rw ) ( struct san_device *sandev,
  101. uint64_t lba, unsigned int count,
  102. userptr_t buffer ) ) {
  103. struct efi_block_data *block = sandev->priv;
  104. unsigned int count;
  105. int rc;
  106. /* Sanity check */
  107. count = ( len / block->media.BlockSize );
  108. if ( ( count * block->media.BlockSize ) != len ) {
  109. DBGC ( sandev, "EFIBLK %#02x impossible length %#zx\n",
  110. sandev->drive, len );
  111. return -EINVAL;
  112. }
  113. /* Read from / write to block device */
  114. if ( ( rc = sandev_rw ( sandev, lba, count,
  115. virt_to_user ( data ) ) ) != 0 ) {
  116. DBGC ( sandev, "EFIBLK %#02x I/O failed: %s\n",
  117. sandev->drive, strerror ( rc ) );
  118. return rc;
  119. }
  120. return 0;
  121. }
  122. /**
  123. * Reset EFI block device
  124. *
  125. * @v block_io Block I/O protocol
  126. * @v verify Perform extended verification
  127. * @ret efirc EFI status code
  128. */
  129. static EFI_STATUS EFIAPI efi_block_io_reset ( EFI_BLOCK_IO_PROTOCOL *block_io,
  130. BOOLEAN verify __unused ) {
  131. struct efi_block_data *block =
  132. container_of ( block_io, struct efi_block_data, block_io );
  133. struct san_device *sandev = block->sandev;
  134. int rc;
  135. DBGC2 ( sandev, "EFIBLK %#02x reset\n", sandev->drive );
  136. efi_snp_claim();
  137. rc = sandev_reset ( sandev );
  138. efi_snp_release();
  139. return EFIRC ( rc );
  140. }
  141. /**
  142. * Read from EFI block device
  143. *
  144. * @v block_io Block I/O protocol
  145. * @v media Media identifier
  146. * @v lba Starting LBA
  147. * @v len Size of buffer
  148. * @v data Data buffer
  149. * @ret efirc EFI status code
  150. */
  151. static EFI_STATUS EFIAPI
  152. efi_block_io_read ( EFI_BLOCK_IO_PROTOCOL *block_io, UINT32 media __unused,
  153. EFI_LBA lba, UINTN len, VOID *data ) {
  154. struct efi_block_data *block =
  155. container_of ( block_io, struct efi_block_data, block_io );
  156. struct san_device *sandev = block->sandev;
  157. int rc;
  158. DBGC2 ( sandev, "EFIBLK %#02x read LBA %#08llx to %p+%#08zx\n",
  159. sandev->drive, lba, data, ( ( size_t ) len ) );
  160. efi_snp_claim();
  161. rc = efi_block_rw ( sandev, lba, data, len, sandev_read );
  162. efi_snp_release();
  163. return EFIRC ( rc );
  164. }
  165. /**
  166. * Write to EFI block device
  167. *
  168. * @v block_io Block I/O protocol
  169. * @v media Media identifier
  170. * @v lba Starting LBA
  171. * @v len Size of buffer
  172. * @v data Data buffer
  173. * @ret efirc EFI status code
  174. */
  175. static EFI_STATUS EFIAPI
  176. efi_block_io_write ( EFI_BLOCK_IO_PROTOCOL *block_io, UINT32 media __unused,
  177. EFI_LBA lba, UINTN len, VOID *data ) {
  178. struct efi_block_data *block =
  179. container_of ( block_io, struct efi_block_data, block_io );
  180. struct san_device *sandev = block->sandev;
  181. int rc;
  182. DBGC2 ( sandev, "EFIBLK %#02x write LBA %#08llx from %p+%#08zx\n",
  183. sandev->drive, lba, data, ( ( size_t ) len ) );
  184. efi_snp_claim();
  185. rc = efi_block_rw ( sandev, lba, data, len, sandev_write );
  186. efi_snp_release();
  187. return EFIRC ( rc );
  188. }
  189. /**
  190. * Flush data to EFI block device
  191. *
  192. * @v block_io Block I/O protocol
  193. * @ret efirc EFI status code
  194. */
  195. static EFI_STATUS EFIAPI
  196. efi_block_io_flush ( EFI_BLOCK_IO_PROTOCOL *block_io ) {
  197. struct efi_block_data *block =
  198. container_of ( block_io, struct efi_block_data, block_io );
  199. struct san_device *sandev = block->sandev;
  200. DBGC2 ( sandev, "EFIBLK %#02x flush\n", sandev->drive );
  201. /* Nothing to do */
  202. return 0;
  203. }
  204. /**
  205. * Connect all possible drivers to EFI block device
  206. *
  207. * @v sandev SAN device
  208. */
  209. static void efi_block_connect ( struct san_device *sandev ) {
  210. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  211. struct efi_block_data *block = sandev->priv;
  212. EFI_STATUS efirc;
  213. int rc;
  214. /* Try to connect all possible drivers to this block device */
  215. if ( ( efirc = bs->ConnectController ( block->handle, NULL,
  216. NULL, 1 ) ) != 0 ) {
  217. rc = -EEFI ( efirc );
  218. DBGC ( sandev, "EFIBLK %#02x could not connect drivers: %s\n",
  219. sandev->drive, strerror ( rc ) );
  220. /* May not be an error; may already be connected */
  221. }
  222. DBGC2 ( sandev, "EFIBLK %#02x supports protocols:\n", sandev->drive );
  223. DBGC2_EFI_PROTOCOLS ( sandev, block->handle );
  224. }
  225. /**
  226. * Hook EFI block device
  227. *
  228. * @v drive Drive number
  229. * @v uris List of URIs
  230. * @v count Number of URIs
  231. * @v flags Flags
  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, unsigned int flags ) {
  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. block = sandev->priv;
  276. block->sandev = sandev;
  277. block->media.MediaPresent = 1;
  278. block->media.LogicalBlocksPerPhysicalBlock = 1;
  279. block->block_io.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION3;
  280. block->block_io.Media = &block->media;
  281. block->block_io.Reset = efi_block_io_reset;
  282. block->block_io.ReadBlocks = efi_block_io_read;
  283. block->block_io.WriteBlocks = efi_block_io_write;
  284. block->block_io.FlushBlocks = efi_block_io_flush;
  285. uri_buf = ( ( ( void * ) block ) + sizeof ( *block ) );
  286. block->path = ( ( ( void * ) uri_buf ) + uri_len + 1 /* NUL */ );
  287. /* Construct device path */
  288. memcpy ( block->path, snpdev->path, prefix_len );
  289. vendor = ( ( ( void * ) block->path ) + prefix_len );
  290. vendor->vendor.Header.Type = HARDWARE_DEVICE_PATH;
  291. vendor->vendor.Header.SubType = HW_VENDOR_DP;
  292. vendor->vendor.Header.Length[0] = ( vendor_len & 0xff );
  293. vendor->vendor.Header.Length[1] = ( vendor_len >> 8 );
  294. memcpy ( &vendor->vendor.Guid, &ipxe_block_device_path_guid,
  295. sizeof ( vendor->vendor.Guid ) );
  296. format_uri ( uris[0], uri_buf, ( uri_len + 1 /* NUL */ ) );
  297. efi_snprintf ( vendor->uri, ( uri_len + 1 /* NUL */ ), "%s", uri_buf );
  298. end = ( ( ( void * ) vendor ) + vendor_len );
  299. end->Type = END_DEVICE_PATH_TYPE;
  300. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  301. end->Length[0] = sizeof ( *end );
  302. DBGC ( sandev, "EFIBLK %#02x has device path %s\n",
  303. drive, efi_devpath_text ( block->path ) );
  304. /* Register SAN device */
  305. if ( ( rc = register_sandev ( sandev, drive, flags ) ) != 0 ) {
  306. DBGC ( sandev, "EFIBLK %#02x could not register: %s\n",
  307. drive, strerror ( rc ) );
  308. goto err_register;
  309. }
  310. /* Update media descriptor */
  311. block->media.BlockSize =
  312. ( sandev->capacity.blksize << sandev->blksize_shift );
  313. block->media.LastBlock =
  314. ( ( sandev->capacity.blocks >> sandev->blksize_shift ) - 1 );
  315. /* Install protocols */
  316. if ( ( efirc = bs->InstallMultipleProtocolInterfaces (
  317. &block->handle,
  318. &efi_block_io_protocol_guid, &block->block_io,
  319. &efi_device_path_protocol_guid, block->path,
  320. NULL ) ) != 0 ) {
  321. rc = -EEFI ( efirc );
  322. DBGC ( sandev, "EFIBLK %#02x could not install protocols: %s\n",
  323. sandev->drive, strerror ( rc ) );
  324. goto err_install;
  325. }
  326. /* Connect all possible protocols */
  327. efi_block_connect ( sandev );
  328. return drive;
  329. bs->UninstallMultipleProtocolInterfaces (
  330. block->handle,
  331. &efi_block_io_protocol_guid, &block->block_io,
  332. &efi_device_path_protocol_guid, block->path, NULL );
  333. err_install:
  334. unregister_sandev ( sandev );
  335. err_register:
  336. sandev_put ( sandev );
  337. err_alloc:
  338. err_no_snpdev:
  339. err_no_uris:
  340. return rc;
  341. }
  342. /**
  343. * Unhook EFI block device
  344. *
  345. * @v drive Drive number
  346. */
  347. static void efi_block_unhook ( unsigned int drive ) {
  348. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  349. struct san_device *sandev;
  350. struct efi_block_data *block;
  351. /* Find SAN device */
  352. sandev = sandev_find ( drive );
  353. if ( ! sandev ) {
  354. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  355. return;
  356. }
  357. block = sandev->priv;
  358. /* Uninstall protocols */
  359. bs->UninstallMultipleProtocolInterfaces (
  360. block->handle,
  361. &efi_block_io_protocol_guid, &block->block_io,
  362. &efi_device_path_protocol_guid, block->path, NULL );
  363. /* Unregister SAN device */
  364. unregister_sandev ( sandev );
  365. /* Drop reference to drive */
  366. sandev_put ( sandev );
  367. }
  368. /** An installed ACPI table */
  369. struct efi_acpi_table {
  370. /** List of installed tables */
  371. struct list_head list;
  372. /** Table key */
  373. UINTN key;
  374. };
  375. /** List of installed ACPI tables */
  376. static LIST_HEAD ( efi_acpi_tables );
  377. /**
  378. * Install ACPI table
  379. *
  380. * @v hdr ACPI description header
  381. * @ret rc Return status code
  382. */
  383. static int efi_block_install ( struct acpi_header *hdr ) {
  384. size_t len = le32_to_cpu ( hdr->length );
  385. struct efi_acpi_table *installed;
  386. EFI_STATUS efirc;
  387. int rc;
  388. /* Allocate installed table record */
  389. installed = zalloc ( sizeof ( *installed ) );
  390. if ( ! installed ) {
  391. rc = -ENOMEM;
  392. goto err_alloc;
  393. }
  394. /* Fill in common parameters */
  395. strncpy ( hdr->oem_id, "FENSYS", sizeof ( hdr->oem_id ) );
  396. strncpy ( hdr->oem_table_id, "iPXE", sizeof ( hdr->oem_table_id ) );
  397. /* Fix up ACPI checksum */
  398. acpi_fix_checksum ( hdr );
  399. /* Install table */
  400. if ( ( efirc = acpi->InstallAcpiTable ( acpi, hdr, len,
  401. &installed->key ) ) != 0 ){
  402. rc = -EEFI ( efirc );
  403. DBGC ( acpi, "EFIBLK could not install %s: %s\n",
  404. acpi_name ( hdr->signature ), strerror ( rc ) );
  405. DBGC_HDA ( acpi, 0, hdr, len );
  406. goto err_install;
  407. }
  408. /* Add to list of installed tables */
  409. list_add_tail ( &installed->list, &efi_acpi_tables );
  410. DBGC ( acpi, "EFIBLK installed %s as ACPI table %#lx:\n",
  411. acpi_name ( hdr->signature ),
  412. ( ( unsigned long ) installed->key ) );
  413. DBGC_HDA ( acpi, 0, hdr, len );
  414. return 0;
  415. list_del ( &installed->list );
  416. err_install:
  417. free ( installed );
  418. err_alloc:
  419. return rc;
  420. }
  421. /**
  422. * Describe EFI block devices
  423. *
  424. * @ret rc Return status code
  425. */
  426. static int efi_block_describe ( void ) {
  427. struct efi_acpi_table *installed;
  428. struct efi_acpi_table *tmp;
  429. UINTN key;
  430. EFI_STATUS efirc;
  431. int rc;
  432. /* Sanity check */
  433. if ( ! acpi ) {
  434. DBG ( "EFIBLK has no ACPI table protocol\n" );
  435. return -ENOTSUP;
  436. }
  437. /* Uninstall any existing ACPI tables */
  438. list_for_each_entry_safe ( installed, tmp, &efi_acpi_tables, list ) {
  439. key = installed->key;
  440. if ( ( efirc = acpi->UninstallAcpiTable ( acpi, key ) ) != 0 ) {
  441. rc = -EEFI ( efirc );
  442. DBGC ( acpi, "EFIBLK could not uninstall ACPI table "
  443. "%#lx: %s\n", ( ( unsigned long ) key ),
  444. strerror ( rc ) );
  445. /* Continue anyway */
  446. }
  447. list_del ( &installed->list );
  448. free ( installed );
  449. }
  450. /* Install ACPI tables */
  451. if ( ( rc = acpi_install ( efi_block_install ) ) != 0 ) {
  452. DBGC ( acpi, "EFIBLK could not install ACPI tables: %s\n",
  453. strerror ( rc ) );
  454. return rc;
  455. }
  456. return 0;
  457. }
  458. /**
  459. * Try booting from child device of EFI block device
  460. *
  461. * @v sandev SAN device
  462. * @v handle EFI handle
  463. * @v filename Filename (or NULL to use default)
  464. * @v image Image handle to fill in
  465. * @ret rc Return status code
  466. */
  467. static int efi_block_boot_image ( struct san_device *sandev, EFI_HANDLE handle,
  468. const char *filename, EFI_HANDLE *image ) {
  469. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  470. struct efi_block_data *block = sandev->priv;
  471. union {
  472. EFI_DEVICE_PATH_PROTOCOL *path;
  473. void *interface;
  474. } path;
  475. EFI_DEVICE_PATH_PROTOCOL *boot_path;
  476. FILEPATH_DEVICE_PATH *filepath;
  477. EFI_DEVICE_PATH_PROTOCOL *end;
  478. size_t prefix_len;
  479. size_t filepath_len;
  480. size_t boot_path_len;
  481. EFI_STATUS efirc;
  482. int rc;
  483. /* Identify device path */
  484. if ( ( efirc = bs->OpenProtocol ( handle,
  485. &efi_device_path_protocol_guid,
  486. &path.interface, efi_image_handle,
  487. handle,
  488. EFI_OPEN_PROTOCOL_GET_PROTOCOL ))!=0){
  489. DBGC ( sandev, "EFIBLK %#02x found filesystem with no device "
  490. "path??", sandev->drive );
  491. rc = -EEFI ( efirc );
  492. goto err_open_device_path;
  493. }
  494. /* Check if this device is a child of our block device */
  495. prefix_len = efi_devpath_len ( block->path );
  496. if ( memcmp ( path.path, block->path, prefix_len ) != 0 ) {
  497. /* Not a child device */
  498. rc = -ENOTTY;
  499. goto err_not_child;
  500. }
  501. DBGC ( sandev, "EFIBLK %#02x found child device %s\n",
  502. sandev->drive, efi_devpath_text ( path.path ) );
  503. /* Construct device path for boot image */
  504. end = efi_devpath_end ( path.path );
  505. prefix_len = ( ( ( void * ) end ) - ( ( void * ) path.path ) );
  506. filepath_len = ( SIZE_OF_FILEPATH_DEVICE_PATH +
  507. ( filename ?
  508. ( ( strlen ( filename ) + 1 /* NUL */ ) *
  509. sizeof ( filepath->PathName[0] ) ) :
  510. sizeof ( efi_block_boot_filename ) ) );
  511. boot_path_len = ( prefix_len + filepath_len + sizeof ( *end ) );
  512. boot_path = zalloc ( boot_path_len );
  513. if ( ! boot_path ) {
  514. rc = -ENOMEM;
  515. goto err_alloc_path;
  516. }
  517. memcpy ( boot_path, path.path, prefix_len );
  518. filepath = ( ( ( void * ) boot_path ) + prefix_len );
  519. filepath->Header.Type = MEDIA_DEVICE_PATH;
  520. filepath->Header.SubType = MEDIA_FILEPATH_DP;
  521. filepath->Header.Length[0] = ( filepath_len & 0xff );
  522. filepath->Header.Length[1] = ( filepath_len >> 8 );
  523. if ( filename ) {
  524. efi_sprintf ( filepath->PathName, "%s", filename );
  525. } else {
  526. memcpy ( filepath->PathName, efi_block_boot_filename,
  527. sizeof ( efi_block_boot_filename ) );
  528. }
  529. end = ( ( ( void * ) filepath ) + filepath_len );
  530. end->Type = END_DEVICE_PATH_TYPE;
  531. end->SubType = END_ENTIRE_DEVICE_PATH_SUBTYPE;
  532. end->Length[0] = sizeof ( *end );
  533. DBGC ( sandev, "EFIBLK %#02x trying to load %s\n",
  534. sandev->drive, efi_devpath_text ( boot_path ) );
  535. /* Try loading boot image from this device */
  536. if ( ( efirc = bs->LoadImage ( FALSE, efi_image_handle, boot_path,
  537. NULL, 0, image ) ) != 0 ) {
  538. rc = -EEFI ( efirc );
  539. DBGC ( sandev, "EFIBLK %#02x could not load image: %s\n",
  540. sandev->drive, strerror ( rc ) );
  541. goto err_load_image;
  542. }
  543. /* Success */
  544. rc = 0;
  545. err_load_image:
  546. free ( boot_path );
  547. err_alloc_path:
  548. err_not_child:
  549. err_open_device_path:
  550. return rc;
  551. }
  552. /**
  553. * Boot from EFI block device
  554. *
  555. * @v drive Drive number
  556. * @v filename Filename (or NULL to use default)
  557. * @ret rc Return status code
  558. */
  559. static int efi_block_boot ( unsigned int drive, const char *filename ) {
  560. EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
  561. struct san_device *sandev;
  562. EFI_HANDLE *handles;
  563. EFI_HANDLE image = NULL;
  564. UINTN count;
  565. unsigned int i;
  566. EFI_STATUS efirc;
  567. int rc;
  568. /* Find SAN device */
  569. sandev = sandev_find ( drive );
  570. if ( ! sandev ) {
  571. DBG ( "EFIBLK cannot find drive %#02x\n", drive );
  572. rc = -ENODEV;
  573. goto err_sandev_find;
  574. }
  575. /* Release SNP devices */
  576. efi_snp_release();
  577. /* Connect all possible protocols */
  578. efi_block_connect ( sandev );
  579. /* Locate all handles supporting the Simple File System protocol */
  580. if ( ( efirc = bs->LocateHandleBuffer (
  581. ByProtocol, &efi_simple_file_system_protocol_guid,
  582. NULL, &count, &handles ) ) != 0 ) {
  583. rc = -EEFI ( efirc );
  584. DBGC ( sandev, "EFIBLK %#02x cannot locate file systems: %s\n",
  585. sandev->drive, strerror ( rc ) );
  586. goto err_locate_file_systems;
  587. }
  588. /* Try booting from any available child device containing a
  589. * suitable boot image. This is something of a wild stab in
  590. * the dark, but should end up conforming to user expectations
  591. * most of the time.
  592. */
  593. rc = -ENOENT;
  594. for ( i = 0 ; i < count ; i++ ) {
  595. if ( ( rc = efi_block_boot_image ( sandev, handles[i], filename,
  596. &image ) ) != 0 )
  597. continue;
  598. DBGC ( sandev, "EFIBLK %#02x found boot image\n",
  599. sandev->drive );
  600. efirc = bs->StartImage ( image, NULL, NULL );
  601. rc = ( efirc ? -EEFI ( efirc ) : 0 );
  602. bs->UnloadImage ( image );
  603. DBGC ( sandev, "EFIBLK %#02x boot image returned: %s\n",
  604. sandev->drive, strerror ( rc ) );
  605. break;
  606. }
  607. bs->FreePool ( handles );
  608. err_locate_file_systems:
  609. efi_snp_claim();
  610. err_sandev_find:
  611. return rc;
  612. }
  613. PROVIDE_SANBOOT ( efi, san_hook, efi_block_hook );
  614. PROVIDE_SANBOOT ( efi, san_unhook, efi_block_unhook );
  615. PROVIDE_SANBOOT ( efi, san_describe, efi_block_describe );
  616. PROVIDE_SANBOOT ( efi, san_boot, efi_block_boot );