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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 an image
  36. *
  37. * @v image Image
  38. * @v uri URI
  39. * @v action Action to take upon a successful download
  40. * @ret rc Return status code
  41. */
  42. int imgdownload ( struct image *image, struct uri *uri,
  43. int ( * action ) ( struct image *image ) ) {
  44. size_t len = ( unparse_uri ( NULL, 0, uri, URI_ALL ) + 1 );
  45. char uri_string_redacted[len];
  46. const char *password;
  47. int rc;
  48. /* Set image URI */
  49. image_set_uri ( image, uri );
  50. /* Redact password portion of URI, if necessary */
  51. password = uri->password;
  52. if ( password )
  53. uri->password = "***";
  54. unparse_uri ( uri_string_redacted, sizeof ( uri_string_redacted ),
  55. uri, URI_ALL );
  56. uri->password = password;
  57. /* Create downloader */
  58. if ( ( rc = create_downloader ( &monojob, image, LOCATION_URI,
  59. uri ) ) != 0 )
  60. return rc;
  61. /* Wait for download to complete */
  62. if ( ( rc = monojob_wait ( uri_string_redacted ) ) != 0 )
  63. return rc;
  64. /* Act upon downloaded image */
  65. if ( ( rc = action ( image ) ) != 0 )
  66. return rc;
  67. return 0;
  68. }
  69. /**
  70. * Fetch an image
  71. *
  72. * @v image Image
  73. * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
  74. * @v action Action to take upon a successful download
  75. * @ret rc Return status code
  76. */
  77. int imgfetch ( struct image *image, const char *uri_string,
  78. int ( * action ) ( struct image *image ) ) {
  79. struct uri *uri;
  80. int rc;
  81. if ( ! ( uri = parse_uri ( uri_string ) ) )
  82. return -ENOMEM;
  83. rc = imgdownload ( image, uri, action );
  84. uri_put ( uri );
  85. return rc;
  86. }
  87. /**
  88. * Display status of an image
  89. *
  90. * @v image Executable/loadable image
  91. */
  92. void imgstat ( struct image *image ) {
  93. printf ( "%s : %zd bytes", image->name, image->len );
  94. if ( image->type )
  95. printf ( " [%s]", image->type->name );
  96. if ( image->flags & IMAGE_SELECTED )
  97. printf ( " [SELECTED]" );
  98. if ( image->cmdline )
  99. printf ( " \"%s\"", image->cmdline );
  100. printf ( "\n" );
  101. }
  102. /**
  103. * Free an image
  104. *
  105. * @v image Executable/loadable image
  106. */
  107. void imgfree ( struct image *image ) {
  108. unregister_image ( image );
  109. }