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

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