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.

iscsiboot.c 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdint.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <errno.h>
  6. #include <gpxe/iscsi.h>
  7. #include <gpxe/settings.h>
  8. #include <gpxe/dhcp.h>
  9. #include <gpxe/netdevice.h>
  10. #include <gpxe/ibft.h>
  11. #include <gpxe/init.h>
  12. #include <int13.h>
  13. #include <usr/autoboot.h>
  14. #include <usr/iscsiboot.h>
  15. struct setting keep_san_setting __setting = {
  16. .name = "keep-san",
  17. .description = "Preserve SAN connection",
  18. .tag = DHCP_EB_KEEP_SAN,
  19. .type = &setting_type_int8,
  20. };
  21. /**
  22. * Guess boot network device
  23. *
  24. * @ret netdev Boot network device
  25. */
  26. static struct net_device * guess_boot_netdev ( void ) {
  27. struct net_device *boot_netdev;
  28. /* Just use the first network device */
  29. for_each_netdev ( boot_netdev ) {
  30. return boot_netdev;
  31. }
  32. return NULL;
  33. }
  34. int iscsiboot ( const char *root_path ) {
  35. struct scsi_device *scsi;
  36. struct int13_drive *drive;
  37. int keep_san;
  38. int rc;
  39. scsi = zalloc ( sizeof ( *scsi ) );
  40. if ( ! scsi ) {
  41. rc = -ENOMEM;
  42. goto err_alloc_scsi;
  43. }
  44. drive = zalloc ( sizeof ( *drive ) );
  45. if ( ! drive ) {
  46. rc = -ENOMEM;
  47. goto err_alloc_drive;
  48. }
  49. printf ( "iSCSI booting from %s\n", root_path );
  50. if ( ( rc = iscsi_attach ( scsi, root_path ) ) != 0 ) {
  51. printf ( "Could not attach iSCSI device: %s\n",
  52. strerror ( rc ) );
  53. goto err_attach;
  54. }
  55. if ( ( rc = init_scsidev ( scsi ) ) != 0 ) {
  56. printf ( "Could not initialise iSCSI device: %s\n",
  57. strerror ( rc ) );
  58. goto err_init;
  59. }
  60. drive->blockdev = &scsi->blockdev;
  61. /* FIXME: ugly, ugly hack */
  62. struct net_device *netdev = guess_boot_netdev();
  63. struct iscsi_session *iscsi =
  64. container_of ( scsi->backend, struct iscsi_session, refcnt );
  65. ibft_fill_data ( netdev, iscsi );
  66. register_int13_drive ( drive );
  67. printf ( "Registered as BIOS drive %#02x\n", drive->drive );
  68. printf ( "Booting from BIOS drive %#02x\n", drive->drive );
  69. rc = int13_boot ( drive->drive );
  70. printf ( "Boot failed\n" );
  71. /* Leave drive registered, if instructed to do so */
  72. keep_san = fetch_intz_setting ( NULL, &keep_san_setting );
  73. if ( keep_san ) {
  74. printf ( "Preserving connection to SAN disk\n" );
  75. shutdown_exit_flags |= SHUTDOWN_KEEP_DEVICES;
  76. return rc;
  77. }
  78. printf ( "Unregistering BIOS drive %#02x\n", drive->drive );
  79. unregister_int13_drive ( drive );
  80. err_init:
  81. iscsi_detach ( scsi );
  82. err_attach:
  83. free ( drive );
  84. err_alloc_drive:
  85. free ( scsi );
  86. err_alloc_scsi:
  87. return rc;
  88. }