Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * Copyright (C) 2017 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. * SAN booting
  28. *
  29. */
  30. #include <stdint.h>
  31. #include <stdlib.h>
  32. #include <errno.h>
  33. #include <assert.h>
  34. #include <ipxe/xfer.h>
  35. #include <ipxe/open.h>
  36. #include <ipxe/timer.h>
  37. #include <ipxe/process.h>
  38. #include <ipxe/iso9660.h>
  39. #include <ipxe/dhcp.h>
  40. #include <ipxe/settings.h>
  41. #include <ipxe/sanboot.h>
  42. /**
  43. * Default SAN drive number
  44. *
  45. * The drive number is a meaningful concept only in a BIOS
  46. * environment, where it represents the INT13 drive number (0x80 for
  47. * the first hard disk). We retain it in other environments to allow
  48. * for a simple way for iPXE commands to refer to SAN drives.
  49. */
  50. #define SAN_DEFAULT_DRIVE 0x80
  51. /**
  52. * Timeout for block device commands (in ticks)
  53. *
  54. * Underlying devices should ideally never become totally stuck.
  55. * However, if they do, then the blocking SAN APIs provide no means
  56. * for the caller to cancel the operation, and the machine appears to
  57. * hang. Use an overall timeout for all commands to avoid this
  58. * problem and bounce timeout failures to the caller.
  59. */
  60. #define SAN_COMMAND_TIMEOUT ( 15 * TICKS_PER_SEC )
  61. /**
  62. * Number of times to retry commands
  63. *
  64. * We may need to retry commands. For example, the underlying
  65. * connection may be closed by the SAN target due to an inactivity
  66. * timeout, or the SAN target may return pointless "error" messages
  67. * such as "SCSI power-on occurred".
  68. */
  69. #define SAN_COMMAND_MAX_RETRIES 10
  70. /** List of SAN devices */
  71. LIST_HEAD ( san_devices );
  72. /**
  73. * Find SAN device by drive number
  74. *
  75. * @v drive Drive number
  76. * @ret sandev SAN device, or NULL
  77. */
  78. struct san_device * sandev_find ( unsigned int drive ) {
  79. struct san_device *sandev;
  80. list_for_each_entry ( sandev, &san_devices, list ) {
  81. if ( sandev->drive == drive )
  82. return sandev;
  83. }
  84. return NULL;
  85. }
  86. /**
  87. * Free SAN device
  88. *
  89. * @v refcnt Reference count
  90. */
  91. static void sandev_free ( struct refcnt *refcnt ) {
  92. struct san_device *sandev =
  93. container_of ( refcnt, struct san_device, refcnt );
  94. assert ( ! timer_running ( &sandev->timer ) );
  95. uri_put ( sandev->uri );
  96. free ( sandev );
  97. }
  98. /**
  99. * Close SAN device command
  100. *
  101. * @v sandev SAN device
  102. * @v rc Reason for close
  103. */
  104. static void sandev_command_close ( struct san_device *sandev, int rc ) {
  105. /* Stop timer */
  106. stop_timer ( &sandev->timer );
  107. /* Restart interface */
  108. intf_restart ( &sandev->command, rc );
  109. /* Record command status */
  110. sandev->command_rc = rc;
  111. }
  112. /**
  113. * Record SAN device capacity
  114. *
  115. * @v sandev SAN device
  116. * @v capacity SAN device capacity
  117. */
  118. static void sandev_command_capacity ( struct san_device *sandev,
  119. struct block_device_capacity *capacity ) {
  120. /* Record raw capacity information */
  121. memcpy ( &sandev->capacity, capacity, sizeof ( sandev->capacity ) );
  122. }
  123. /** SAN device command interface operations */
  124. static struct interface_operation sandev_command_op[] = {
  125. INTF_OP ( intf_close, struct san_device *, sandev_command_close ),
  126. INTF_OP ( block_capacity, struct san_device *,
  127. sandev_command_capacity ),
  128. };
  129. /** SAN device command interface descriptor */
  130. static struct interface_descriptor sandev_command_desc =
  131. INTF_DESC ( struct san_device, command, sandev_command_op );
  132. /**
  133. * Handle SAN device command timeout
  134. *
  135. * @v retry Retry timer
  136. */
  137. static void sandev_command_expired ( struct retry_timer *timer,
  138. int over __unused ) {
  139. struct san_device *sandev =
  140. container_of ( timer, struct san_device, timer );
  141. sandev_command_close ( sandev, -ETIMEDOUT );
  142. }
  143. /**
  144. * Restart SAN device interface
  145. *
  146. * @v sandev SAN device
  147. * @v rc Reason for restart
  148. */
  149. static void sandev_restart ( struct san_device *sandev, int rc ) {
  150. /* Restart block device interface */
  151. intf_nullify ( &sandev->command ); /* avoid potential loops */
  152. intf_restart ( &sandev->block, rc );
  153. /* Close any outstanding command */
  154. sandev_command_close ( sandev, rc );
  155. /* Record device error */
  156. sandev->block_rc = rc;
  157. }
  158. /**
  159. * (Re)open SAN device
  160. *
  161. * @v sandev SAN device
  162. * @ret rc Return status code
  163. *
  164. * This function will block until the device is available.
  165. */
  166. int sandev_reopen ( struct san_device *sandev ) {
  167. int rc;
  168. /* Close any outstanding command and restart interface */
  169. sandev_restart ( sandev, -ECONNRESET );
  170. /* Mark device as being not yet open */
  171. sandev->block_rc = -EINPROGRESS;
  172. /* Open block device interface */
  173. if ( ( rc = xfer_open_uri ( &sandev->block, sandev->uri ) ) != 0 ) {
  174. DBGC ( sandev, "SAN %#02x could not (re)open URI: %s\n",
  175. sandev->drive, strerror ( rc ) );
  176. return rc;
  177. }
  178. /* Wait for device to become available */
  179. while ( sandev->block_rc == -EINPROGRESS ) {
  180. step();
  181. if ( xfer_window ( &sandev->block ) != 0 ) {
  182. sandev->block_rc = 0;
  183. return 0;
  184. }
  185. }
  186. DBGC ( sandev, "SAN %#02x never became available: %s\n",
  187. sandev->drive, strerror ( sandev->block_rc ) );
  188. return sandev->block_rc;
  189. }
  190. /**
  191. * Handle closure of underlying block device interface
  192. *
  193. * @v sandev SAN device
  194. * @ret rc Reason for close
  195. */
  196. static void sandev_block_close ( struct san_device *sandev, int rc ) {
  197. /* Any closure is an error from our point of view */
  198. if ( rc == 0 )
  199. rc = -ENOTCONN;
  200. DBGC ( sandev, "SAN %#02x went away: %s\n",
  201. sandev->drive, strerror ( rc ) );
  202. /* Close any outstanding command and restart interface */
  203. sandev_restart ( sandev, rc );
  204. }
  205. /**
  206. * Check SAN device flow control window
  207. *
  208. * @v sandev SAN device
  209. */
  210. static size_t sandev_block_window ( struct san_device *sandev __unused ) {
  211. /* We are never ready to receive data via this interface.
  212. * This prevents objects that support both block and stream
  213. * interfaces from attempting to send us stream data.
  214. */
  215. return 0;
  216. }
  217. /** SAN device block interface operations */
  218. static struct interface_operation sandev_block_op[] = {
  219. INTF_OP ( intf_close, struct san_device *, sandev_block_close ),
  220. INTF_OP ( xfer_window, struct san_device *, sandev_block_window ),
  221. };
  222. /** SAN device block interface descriptor */
  223. static struct interface_descriptor sandev_block_desc =
  224. INTF_DESC ( struct san_device, block, sandev_block_op );
  225. /** SAN device read/write command parameters */
  226. struct san_command_rw_params {
  227. /** SAN device read/write operation */
  228. int ( * block_rw ) ( struct interface *control, struct interface *data,
  229. uint64_t lba, unsigned int count,
  230. userptr_t buffer, size_t len );
  231. /** Data buffer */
  232. userptr_t buffer;
  233. /** Starting LBA */
  234. uint64_t lba;
  235. /** Block count */
  236. unsigned int count;
  237. };
  238. /** SAN device command parameters */
  239. union san_command_params {
  240. /** Read/write command parameters */
  241. struct san_command_rw_params rw;
  242. };
  243. /**
  244. * Initiate SAN device read/write command
  245. *
  246. * @v sandev SAN device
  247. * @v params Command parameters
  248. * @ret rc Return status code
  249. */
  250. static int sandev_command_rw ( struct san_device *sandev,
  251. const union san_command_params *params ) {
  252. size_t len = ( params->rw.count * sandev->capacity.blksize );
  253. int rc;
  254. /* Initiate read/write command */
  255. if ( ( rc = params->rw.block_rw ( &sandev->block, &sandev->command,
  256. params->rw.lba, params->rw.count,
  257. params->rw.buffer, len ) ) != 0 ) {
  258. DBGC ( sandev, "SAN %#02x could not initiate read/write: "
  259. "%s\n", sandev->drive, strerror ( rc ) );
  260. return rc;
  261. }
  262. return 0;
  263. }
  264. /**
  265. * Initiate SAN device read capacity command
  266. *
  267. * @v sandev SAN device
  268. * @v params Command parameters
  269. * @ret rc Return status code
  270. */
  271. static int
  272. sandev_command_read_capacity ( struct san_device *sandev,
  273. const union san_command_params *params __unused){
  274. int rc;
  275. /* Initiate read capacity command */
  276. if ( ( rc = block_read_capacity ( &sandev->block,
  277. &sandev->command ) ) != 0 ) {
  278. DBGC ( sandev, "SAN %#02x could not initiate read capacity: "
  279. "%s\n", sandev->drive, strerror ( rc ) );
  280. return rc;
  281. }
  282. return 0;
  283. }
  284. /**
  285. * Execute a single SAN device command and wait for completion
  286. *
  287. * @v sandev SAN device
  288. * @v command Command
  289. * @v params Command parameters (if required)
  290. * @ret rc Return status code
  291. */
  292. static int
  293. sandev_command ( struct san_device *sandev,
  294. int ( * command ) ( struct san_device *sandev,
  295. const union san_command_params *params ),
  296. const union san_command_params *params ) {
  297. unsigned int retries;
  298. int rc;
  299. /* Sanity check */
  300. assert ( ! timer_running ( &sandev->timer ) );
  301. /* (Re)try command */
  302. for ( retries = 0 ; retries < SAN_COMMAND_MAX_RETRIES ; retries++ ) {
  303. /* Reopen block device if applicable */
  304. if ( sandev_needs_reopen ( sandev ) &&
  305. ( ( rc = sandev_reopen ( sandev ) ) != 0 ) ) {
  306. continue;
  307. }
  308. /* Start expiry timer */
  309. start_timer_fixed ( &sandev->timer, SAN_COMMAND_TIMEOUT );
  310. /* Initiate command */
  311. if ( ( rc = command ( sandev, params ) ) != 0 ) {
  312. stop_timer ( &sandev->timer );
  313. continue;
  314. }
  315. /* Wait for command to complete */
  316. while ( timer_running ( &sandev->timer ) )
  317. step();
  318. /* Exit on success */
  319. if ( ( rc = sandev->command_rc ) == 0 )
  320. return 0;
  321. }
  322. /* Sanity check */
  323. assert ( ! timer_running ( &sandev->timer ) );
  324. return rc;
  325. }
  326. /**
  327. * Reset SAN device
  328. *
  329. * @v sandev SAN device
  330. * @ret rc Return status code
  331. */
  332. int sandev_reset ( struct san_device *sandev ) {
  333. int rc;
  334. DBGC ( sandev, "SAN %#02x reset\n", sandev->drive );
  335. /* Close and reopen underlying block device */
  336. if ( ( rc = sandev_reopen ( sandev ) ) != 0 )
  337. return rc;
  338. return 0;
  339. }
  340. /**
  341. * Read from or write to SAN device
  342. *
  343. * @v sandev SAN device
  344. * @v lba Starting logical block address
  345. * @v count Number of logical blocks
  346. * @v buffer Data buffer
  347. * @v block_rw Block read/write method
  348. * @ret rc Return status code
  349. */
  350. int sandev_rw ( struct san_device *sandev, uint64_t lba,
  351. unsigned int count, userptr_t buffer,
  352. int ( * block_rw ) ( struct interface *control,
  353. struct interface *data,
  354. uint64_t lba, unsigned int count,
  355. userptr_t buffer, size_t len ) ) {
  356. union san_command_params params;
  357. unsigned int remaining;
  358. size_t frag_len;
  359. int rc;
  360. /* Initialise command parameters */
  361. params.rw.block_rw = block_rw;
  362. params.rw.buffer = buffer;
  363. params.rw.lba = ( lba << sandev->blksize_shift );
  364. params.rw.count = sandev->capacity.max_count;
  365. remaining = ( count << sandev->blksize_shift );
  366. /* Read/write fragments */
  367. while ( remaining ) {
  368. /* Determine fragment length */
  369. if ( params.rw.count > remaining )
  370. params.rw.count = remaining;
  371. /* Execute command */
  372. if ( ( rc = sandev_command ( sandev, sandev_command_rw,
  373. &params ) ) != 0 )
  374. return rc;
  375. /* Move to next fragment */
  376. frag_len = ( sandev->capacity.blksize * params.rw.count );
  377. params.rw.buffer = userptr_add ( params.rw.buffer, frag_len );
  378. params.rw.lba += params.rw.count;
  379. remaining -= params.rw.count;
  380. }
  381. return 0;
  382. }
  383. /**
  384. * Configure SAN device as a CD-ROM, if applicable
  385. *
  386. * @v sandev SAN device
  387. * @ret rc Return status code
  388. *
  389. * Both BIOS and UEFI require SAN devices to be accessed with a block
  390. * size of 2048. While we could require the user to configure the
  391. * block size appropriately, this is non-trivial and would impose a
  392. * substantial learning effort on the user. Instead, we check for the
  393. * presence of the ISO9660 primary volume descriptor and, if found,
  394. * then we force a block size of 2048 and map read/write requests
  395. * appropriately.
  396. */
  397. static int sandev_parse_iso9660 ( struct san_device *sandev ) {
  398. static const struct iso9660_primary_descriptor_fixed primary_check = {
  399. .type = ISO9660_TYPE_PRIMARY,
  400. .id = ISO9660_ID,
  401. };
  402. struct iso9660_primary_descriptor *primary;
  403. unsigned int blksize;
  404. unsigned int blksize_shift;
  405. unsigned int lba;
  406. unsigned int count;
  407. int rc;
  408. /* Calculate required blocksize shift for potential CD-ROM access */
  409. blksize = sandev->capacity.blksize;
  410. blksize_shift = 0;
  411. while ( blksize < ISO9660_BLKSIZE ) {
  412. blksize <<= 1;
  413. blksize_shift++;
  414. }
  415. if ( blksize > ISO9660_BLKSIZE ) {
  416. /* Cannot be a CD-ROM. This is not an error. */
  417. rc = 0;
  418. goto invalid_blksize;
  419. }
  420. lba = ( ISO9660_PRIMARY_LBA << blksize_shift );
  421. count = ( 1 << blksize_shift );
  422. /* Allocate scratch area */
  423. primary = malloc ( ISO9660_BLKSIZE );
  424. if ( ! primary ) {
  425. rc = -ENOMEM;
  426. goto err_alloc;
  427. }
  428. /* Read primary volume descriptor */
  429. if ( ( rc = sandev_rw ( sandev, lba, count, virt_to_user ( primary ),
  430. block_read ) ) != 0 ) {
  431. DBGC ( sandev, "SAN %#02x could not read ISO9660 primary"
  432. "volume descriptor: %s\n",
  433. sandev->drive, strerror ( rc ) );
  434. goto err_rw;
  435. }
  436. /* Configure as CD-ROM if applicable */
  437. if ( memcmp ( primary, &primary_check, sizeof ( primary_check ) ) == 0){
  438. DBGC ( sandev, "SAN %#02x contains an ISO9660 filesystem; "
  439. "treating as CD-ROM\n", sandev->drive );
  440. sandev->blksize_shift = blksize_shift;
  441. sandev->is_cdrom = 1;
  442. }
  443. err_rw:
  444. free ( primary );
  445. err_alloc:
  446. invalid_blksize:
  447. return rc;
  448. }
  449. /**
  450. * Allocate SAN device
  451. *
  452. * @ret sandev SAN device, or NULL
  453. */
  454. struct san_device * alloc_sandev ( struct uri *uri, size_t priv_size ) {
  455. struct san_device *sandev;
  456. /* Allocate and initialise structure */
  457. sandev = zalloc ( sizeof ( *sandev ) + priv_size );
  458. if ( ! sandev )
  459. return NULL;
  460. ref_init ( &sandev->refcnt, sandev_free );
  461. sandev->uri = uri_get ( uri );
  462. intf_init ( &sandev->block, &sandev_block_desc, &sandev->refcnt );
  463. sandev->block_rc = -EINPROGRESS;
  464. intf_init ( &sandev->command, &sandev_command_desc, &sandev->refcnt );
  465. timer_init ( &sandev->timer, sandev_command_expired, &sandev->refcnt );
  466. sandev->priv = ( ( ( void * ) sandev ) + sizeof ( *sandev ) );
  467. return sandev;
  468. }
  469. /**
  470. * Register SAN device
  471. *
  472. * @v sandev SAN device
  473. * @ret rc Return status code
  474. */
  475. int register_sandev ( struct san_device *sandev ) {
  476. int rc;
  477. /* Check that drive number is not in use */
  478. if ( sandev_find ( sandev->drive ) != NULL ) {
  479. DBGC ( sandev, "SAN %#02x is already in use\n", sandev->drive );
  480. return -EADDRINUSE;
  481. }
  482. /* Read device capacity */
  483. if ( ( rc = sandev_command ( sandev, sandev_command_read_capacity,
  484. NULL ) ) != 0 )
  485. return rc;
  486. /* Configure as a CD-ROM, if applicable */
  487. if ( ( rc = sandev_parse_iso9660 ( sandev ) ) != 0 )
  488. return rc;
  489. /* Add to list of SAN devices */
  490. list_add_tail ( &sandev->list, &san_devices );
  491. DBGC ( sandev, "SAN %#02x registered\n", sandev->drive );
  492. return 0;
  493. }
  494. /**
  495. * Unregister SAN device
  496. *
  497. * @v sandev SAN device
  498. */
  499. void unregister_sandev ( struct san_device *sandev ) {
  500. /* Sanity check */
  501. assert ( ! timer_running ( &sandev->timer ) );
  502. /* Shut down interfaces */
  503. intfs_shutdown ( 0, &sandev->block, &sandev->command, NULL );
  504. /* Remove from list of SAN devices */
  505. list_del ( &sandev->list );
  506. DBGC ( sandev, "SAN %#02x unregistered\n", sandev->drive );
  507. }
  508. /** The "san-drive" setting */
  509. const struct setting san_drive_setting __setting ( SETTING_SANBOOT_EXTRA,
  510. san-drive ) = {
  511. .name = "san-drive",
  512. .description = "SAN drive number",
  513. .tag = DHCP_EB_SAN_DRIVE,
  514. .type = &setting_type_uint8,
  515. };
  516. /**
  517. * Get default SAN drive number
  518. *
  519. * @ret drive Default drive number
  520. */
  521. unsigned int san_default_drive ( void ) {
  522. unsigned long drive;
  523. /* Use "san-drive" setting, if specified */
  524. if ( fetch_uint_setting ( NULL, &san_drive_setting, &drive ) >= 0 )
  525. return drive;
  526. /* Otherwise, default to booting from first hard disk */
  527. return SAN_DEFAULT_DRIVE;
  528. }