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.

imgmgmt.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <gpxe/image.h>
  23. #include <gpxe/downloader.h>
  24. #include <gpxe/monojob.h>
  25. #include <gpxe/open.h>
  26. #include <gpxe/uri.h>
  27. #include <usr/imgmgmt.h>
  28. /** @file
  29. *
  30. * Image management
  31. *
  32. */
  33. /**
  34. * Fetch an image
  35. *
  36. * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
  37. * @v name Name for image, or NULL
  38. * @v register_image Image registration routine
  39. * @ret rc Return status code
  40. */
  41. int imgfetch ( struct image *image, const char *uri_string,
  42. int ( * image_register ) ( struct image *image ) ) {
  43. char uri_string_redacted[ strlen ( uri_string ) + 3 /* "***" */
  44. + 1 /* NUL */ ];
  45. struct uri *uri;
  46. const char *password;
  47. int rc;
  48. if ( ! ( uri = parse_uri ( uri_string ) ) )
  49. return -ENOMEM;
  50. image_set_uri ( image, uri );
  51. /* Redact password portion of URI, if necessary */
  52. password = uri->password;
  53. if ( password )
  54. uri->password = "***";
  55. unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
  56. uri );
  57. uri->password = password;
  58. if ( ( rc = create_downloader ( &monojob, image, image_register,
  59. LOCATION_URI, uri ) ) == 0 )
  60. rc = monojob_wait ( uri_string_redacted );
  61. uri_put ( uri );
  62. return rc;
  63. }
  64. /**
  65. * Load an image
  66. *
  67. * @v image Image
  68. * @ret rc Return status code
  69. */
  70. int imgload ( struct image *image ) {
  71. int rc;
  72. /* Try to load image */
  73. if ( ( rc = image_autoload ( image ) ) != 0 )
  74. return rc;
  75. return 0;
  76. }
  77. /**
  78. * Execute an image
  79. *
  80. * @v image Image
  81. * @ret rc Return status code
  82. */
  83. int imgexec ( struct image *image ) {
  84. return image_exec ( image );
  85. }
  86. /**
  87. * Identify the only loaded image
  88. *
  89. * @ret image Image, or NULL if 0 or >1 images are loaded
  90. */
  91. struct image * imgautoselect ( void ) {
  92. struct image *image;
  93. struct image *selected_image = NULL;
  94. int flagged_images = 0;
  95. for_each_image ( image ) {
  96. if ( image->flags & IMAGE_LOADED ) {
  97. selected_image = image;
  98. flagged_images++;
  99. }
  100. }
  101. return ( ( flagged_images == 1 ) ? selected_image : NULL );
  102. }
  103. /**
  104. * Display status of an image
  105. *
  106. * @v image Executable/loadable image
  107. */
  108. void imgstat ( struct image *image ) {
  109. printf ( "%s: %zd bytes", image->name, image->len );
  110. if ( image->type )
  111. printf ( " [%s]", image->type->name );
  112. if ( image->flags & IMAGE_LOADED )
  113. printf ( " [LOADED]" );
  114. if ( image->cmdline )
  115. printf ( " \"%s\"", image->cmdline );
  116. printf ( "\n" );
  117. }
  118. /**
  119. * Free an image
  120. *
  121. * @v image Executable/loadable image
  122. */
  123. void imgfree ( struct image *image ) {
  124. unregister_image ( image );
  125. }