123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
- #include <string.h>
- #include <stdio.h>
- #include <errno.h>
- #include <ipxe/netdevice.h>
- #include <ipxe/dhcp.h>
- #include <ipxe/settings.h>
- #include <ipxe/image.h>
- #include <ipxe/sanboot.h>
- #include <ipxe/uri.h>
- #include <ipxe/init.h>
- #include <usr/ifmgmt.h>
- #include <usr/route.h>
- #include <usr/dhcpmgmt.h>
- #include <usr/imgmgmt.h>
- #include <usr/autoboot.h>
-
-
-
-
- int shutdown_exit_flags = 0;
-
-
- __weak int pxe_menu_boot ( struct net_device *netdev __unused ) {
- return -ENOTSUP;
- }
-
-
- static struct net_device * find_boot_netdev ( void ) {
- return NULL;
- }
-
-
- int boot_next_server_and_filename ( struct in_addr next_server,
- const char *filename ) {
- struct uri *uri;
- struct image *image;
- char buf[ 23 +
- ( 3 * strlen(filename) )
- + 1 ];
- int filename_is_absolute;
- int rc;
-
-
- uri = parse_uri ( filename );
- if ( ! uri ) {
- printf ( "Could not parse \"%s\"\n", filename );
- rc = -ENOMEM;
- goto err_parse_uri;
- }
- filename_is_absolute = uri_is_absolute ( uri );
- uri_put ( uri );
-
-
-
- if ( ! filename_is_absolute ) {
- snprintf ( buf, sizeof ( buf ), "tftp://%s/",
- inet_ntoa ( next_server ) );
- uri_encode ( filename, buf + strlen ( buf ),
- sizeof ( buf ) - strlen ( buf ), URI_PATH );
- filename = buf;
- }
-
-
- image = alloc_image();
- if ( ! image ) {
- printf ( "Could not allocate image\n" );
- rc = -ENOMEM;
- goto err_alloc_image;
- }
- if ( ( rc = imgfetch ( image, filename,
- register_and_autoload_image ) ) != 0 ) {
- printf ( "Could not fetch image: %s\n", strerror ( rc ) );
- goto err_imgfetch;
- }
- if ( ( rc = imgexec ( image ) ) != 0 ) {
- printf ( "Could not execute image: %s\n", strerror ( rc ) );
- goto err_imgexec;
- }
-
-
- image_put ( image );
- return 0;
-
- err_imgexec:
- err_imgfetch:
- image_put ( image );
- err_alloc_image:
- err_parse_uri:
- return rc;
- }
-
-
- struct setting keep_san_setting __setting = {
- .name = "keep-san",
- .description = "Preserve SAN connection",
- .tag = DHCP_EB_KEEP_SAN,
- .type = &setting_type_int8,
- };
-
-
- struct setting skip_san_boot_setting __setting = {
- .name = "skip-san-boot",
- .description = "Do not boot the SAN drive after connecting",
- .tag = DHCP_EB_SKIP_SAN_BOOT,
- .type = &setting_type_int8,
- };
-
-
- int boot_root_path ( const char *root_path ) {
- struct uri *uri;
- int drive;
- int rc;
-
-
- uri = parse_uri ( root_path );
- if ( ! uri ) {
- printf ( "Could not parse \"%s\"\n", root_path );
- rc = -ENOMEM;
- goto err_parse_uri;
- }
-
-
- if ( ( drive = san_hook ( uri, 0 ) ) < 0 ) {
- rc = drive;
- printf ( "Could not open SAN device: %s\n",
- strerror ( rc ) );
- goto err_open;
- }
- printf ( "Registered as SAN device %#02x\n", drive );
-
-
- if ( ( rc = san_describe ( drive ) ) != 0 ) {
- printf ( "Could not describe SAN device %#02x: %s\n",
- drive, strerror ( rc ) );
- goto err_describe;
- }
-
-
- if ( fetch_intz_setting ( NULL, &skip_san_boot_setting) != 0 ) {
- printf ( "Skipping boot from SAN device %#02x\n", drive );
- } else {
- printf ( "Booting from SAN device %#02x\n", drive );
- rc = san_boot ( drive );
- printf ( "Boot from SAN device %#02x failed: %s\n",
- drive, strerror ( rc ) );
- }
-
-
- if ( fetch_intz_setting ( NULL, &keep_san_setting ) != 0 ) {
- printf ( "Preserving connection to SAN device %#02x\n",
- drive );
- shutdown_exit_flags |= SHUTDOWN_KEEP_DEVICES;
- goto err_keep_san;
- }
-
-
- printf ( "Unregistering SAN device %#02x\n", drive );
- san_unhook ( drive );
-
-
- uri_put ( uri );
-
- return 0;
-
- err_keep_san:
- err_describe:
- err_open:
- uri_put ( uri );
- err_parse_uri:
- return rc;
- }
-
-
- int netboot ( struct net_device *netdev ) {
- struct setting vendor_class_id_setting
- = { .tag = DHCP_VENDOR_CLASS_ID };
- struct setting pxe_discovery_control_setting
- = { .tag = DHCP_PXE_DISCOVERY_CONTROL };
- struct setting pxe_boot_menu_setting
- = { .tag = DHCP_PXE_BOOT_MENU };
- char buf[256];
- struct in_addr next_server;
- unsigned int pxe_discovery_control;
- int rc;
-
-
- if ( ( rc = ifopen ( netdev ) ) != 0 )
- return rc;
- ifstat ( netdev );
-
-
- if ( ( rc = dhcp ( netdev ) ) != 0 )
- return rc;
- route();
-
-
- fetch_string_setting ( NULL, &vendor_class_id_setting,
- buf, sizeof ( buf ) );
- pxe_discovery_control =
- fetch_uintz_setting ( NULL, &pxe_discovery_control_setting );
- if ( ( strcmp ( buf, "PXEClient" ) == 0 ) &&
- setting_exists ( NULL, &pxe_boot_menu_setting ) &&
- ( ! ( ( pxe_discovery_control & PXEBS_SKIP ) &&
- setting_exists ( NULL, &filename_setting ) ) ) ) {
- printf ( "Booting from PXE menu\n" );
- return pxe_menu_boot ( netdev );
- }
-
-
- fetch_ipv4_setting ( NULL, &next_server_setting, &next_server );
- fetch_string_setting ( NULL, &filename_setting, buf, sizeof ( buf ) );
- if ( buf[0] ) {
- printf ( "Booting from filename \"%s\"\n", buf );
- return boot_next_server_and_filename ( next_server, buf );
- }
-
-
- fetch_string_setting ( NULL, &root_path_setting, buf, sizeof ( buf ) );
- if ( buf[0] ) {
- printf ( "Booting from root path \"%s\"\n", buf );
- return boot_root_path ( buf );
- }
-
- printf ( "No filename or root path specified\n" );
- return -ENOENT;
- }
-
-
- static void close_all_netdevs ( void ) {
- struct net_device *netdev;
-
- for_each_netdev ( netdev ) {
- ifclose ( netdev );
- }
- }
-
-
- void autoboot ( void ) {
- struct net_device *boot_netdev;
- struct net_device *netdev;
-
-
- close_all_netdevs();
- if ( ( boot_netdev = find_boot_netdev() ) )
- netboot ( boot_netdev );
-
-
- for_each_netdev ( netdev ) {
- if ( netdev == boot_netdev )
- continue;
- close_all_netdevs();
- netboot ( netdev );
- }
-
- printf ( "No more network devices\n" );
- }
|