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

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