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

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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <ipxe/image.h>
  24. #include <ipxe/downloader.h>
  25. #include <ipxe/monojob.h>
  26. #include <ipxe/open.h>
  27. #include <ipxe/uri.h>
  28. #include <usr/imgmgmt.h>
  29. /** @file
  30. *
  31. * Image management
  32. *
  33. */
  34. /**
  35. * Download a new image
  36. *
  37. * @v uri URI
  38. * @v image Image to fill in
  39. * @ret rc Return status code
  40. */
  41. int imgdownload ( struct uri *uri, struct image **image ) {
  42. size_t len = ( unparse_uri ( NULL, 0, uri, URI_ALL ) + 1 );
  43. char uri_string_redacted[len];
  44. const char *password;
  45. int rc;
  46. /* Allocate image */
  47. *image = alloc_image ( uri );
  48. if ( ! *image ) {
  49. rc = -ENOMEM;
  50. goto err_alloc_image;
  51. }
  52. /* Redact password portion of URI, if necessary */
  53. password = uri->password;
  54. if ( password )
  55. uri->password = "***";
  56. unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
  57. uri, URI_ALL );
  58. uri->password = password;
  59. /* Create downloader */
  60. if ( ( rc = create_downloader ( &monojob, *image, LOCATION_URI,
  61. uri ) ) != 0 ) {
  62. printf ( "Could not start download: %s\n", strerror ( rc ) );
  63. goto err_create_downloader;
  64. }
  65. /* Wait for download to complete */
  66. if ( ( rc = monojob_wait ( uri_string_redacted ) ) != 0 )
  67. goto err_monojob_wait;
  68. /* Register image */
  69. if ( ( rc = register_image ( *image ) ) != 0 ) {
  70. printf ( "Could not register image: %s\n", strerror ( rc ) );
  71. goto err_register_image;
  72. }
  73. /* Drop local reference to image. Image is guaranteed to
  74. * remain in scope since it is registered.
  75. */
  76. image_put ( *image );
  77. return 0;
  78. err_register_image:
  79. err_monojob_wait:
  80. err_create_downloader:
  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->cmdline )
  130. printf ( " \"%s\"", image->cmdline );
  131. printf ( "\n" );
  132. }