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

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