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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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., 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. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <errno.h>
  28. #include <ipxe/image.h>
  29. #include <ipxe/downloader.h>
  30. #include <ipxe/monojob.h>
  31. #include <ipxe/open.h>
  32. #include <ipxe/uri.h>
  33. #include <usr/imgmgmt.h>
  34. /** @file
  35. *
  36. * Image management
  37. *
  38. */
  39. /**
  40. * Download a new image
  41. *
  42. * @v uri URI
  43. * @v timeout Download timeout
  44. * @v image Image to fill in
  45. * @ret rc Return status code
  46. */
  47. int imgdownload ( struct uri *uri, unsigned long timeout,
  48. struct image **image ) {
  49. const char *password;
  50. char *uri_string_redacted;
  51. int rc;
  52. /* Construct redacted URI */
  53. password = uri->password;
  54. if ( password )
  55. uri->password = "***";
  56. uri_string_redacted = format_uri_alloc ( uri );
  57. uri->password = password;
  58. if ( ! uri_string_redacted ) {
  59. rc = -ENOMEM;
  60. goto err_uri_string;
  61. }
  62. /* Resolve URI */
  63. uri = resolve_uri ( cwuri, uri );
  64. if ( ! uri ) {
  65. rc = -ENOMEM;
  66. goto err_resolve_uri;
  67. }
  68. /* Allocate image */
  69. *image = alloc_image ( uri );
  70. if ( ! *image ) {
  71. rc = -ENOMEM;
  72. goto err_alloc_image;
  73. }
  74. /* Create downloader */
  75. if ( ( rc = create_downloader ( &monojob, *image ) ) != 0 ) {
  76. printf ( "Could not start download: %s\n", strerror ( rc ) );
  77. goto err_create_downloader;
  78. }
  79. /* Wait for download to complete */
  80. if ( ( rc = monojob_wait ( uri_string_redacted, timeout ) ) != 0 )
  81. goto err_monojob_wait;
  82. /* Register image */
  83. if ( ( rc = register_image ( *image ) ) != 0 ) {
  84. printf ( "Could not register image: %s\n", strerror ( rc ) );
  85. goto err_register_image;
  86. }
  87. err_register_image:
  88. err_monojob_wait:
  89. err_create_downloader:
  90. image_put ( *image );
  91. err_alloc_image:
  92. uri_put ( uri );
  93. err_resolve_uri:
  94. free ( uri_string_redacted );
  95. err_uri_string:
  96. return rc;
  97. }
  98. /**
  99. * Download a new image
  100. *
  101. * @v uri_string URI string
  102. * @v timeout Download timeout
  103. * @v image Image to fill in
  104. * @ret rc Return status code
  105. */
  106. int imgdownload_string ( const char *uri_string, unsigned long timeout,
  107. struct image **image ) {
  108. struct uri *uri;
  109. int rc;
  110. if ( ! ( uri = parse_uri ( uri_string ) ) )
  111. return -ENOMEM;
  112. rc = imgdownload ( uri, timeout, image );
  113. uri_put ( uri );
  114. return rc;
  115. }
  116. /**
  117. * Acquire an image
  118. *
  119. * @v name_uri Name or URI string
  120. * @v timeout Download timeout
  121. * @v image Image to fill in
  122. * @ret rc Return status code
  123. */
  124. int imgacquire ( const char *name_uri, unsigned long timeout,
  125. struct image **image ) {
  126. /* If we already have an image with the specified name, use it */
  127. *image = find_image ( name_uri );
  128. if ( *image )
  129. return 0;
  130. /* Otherwise, download a new image */
  131. return imgdownload_string ( name_uri, timeout, image );
  132. }
  133. /**
  134. * Display status of an image
  135. *
  136. * @v image Executable/loadable image
  137. */
  138. void imgstat ( struct image *image ) {
  139. printf ( "%s : %zd bytes", image->name, image->len );
  140. if ( image->type )
  141. printf ( " [%s]", image->type->name );
  142. if ( image->flags & IMAGE_TRUSTED )
  143. printf ( " [TRUSTED]" );
  144. if ( image->flags & IMAGE_SELECTED )
  145. printf ( " [SELECTED]" );
  146. if ( image->flags & IMAGE_AUTO_UNREGISTER )
  147. printf ( " [AUTOFREE]" );
  148. if ( image->cmdline )
  149. printf ( " \"%s\"", image->cmdline );
  150. printf ( "\n" );
  151. }