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.3KB

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