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

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