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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include <stdint.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <gpxe/image.h>
  23. #include <gpxe/downloader.h>
  24. #include <gpxe/monojob.h>
  25. #include <gpxe/open.h>
  26. #include <usr/imgmgmt.h>
  27. /** @file
  28. *
  29. * Image management
  30. *
  31. */
  32. static int imgfetch_autoload ( struct image *image ) {
  33. int rc;
  34. if ( ( rc = register_image ( image ) ) != 0 )
  35. return rc;
  36. if ( ( rc = image_autoload ( image ) ) != 0 )
  37. return rc;
  38. return 0;
  39. }
  40. /**
  41. * Fetch an image
  42. *
  43. * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
  44. * @v name Name for image, or NULL
  45. * @ret new_image Newly created image
  46. * @ret rc Return status code
  47. */
  48. int imgfetch ( struct image *image, const char *uri_string, int load ) {
  49. int rc;
  50. if ( ( rc = create_downloader ( &monojob, image,
  51. ( load ? imgfetch_autoload :
  52. register_image ),
  53. LOCATION_URI_STRING,
  54. uri_string ) ) == 0 )
  55. rc = monojob_wait();
  56. return rc;
  57. }
  58. /**
  59. * Load an image
  60. *
  61. * @v image Image
  62. * @ret rc Return status code
  63. */
  64. int imgload ( struct image *image ) {
  65. int rc;
  66. /* Try to load image */
  67. if ( ( rc = image_autoload ( image ) ) != 0 )
  68. return rc;
  69. return 0;
  70. }
  71. /**
  72. * Execute an image
  73. *
  74. * @v image Image
  75. * @ret rc Return status code
  76. */
  77. int imgexec ( struct image *image ) {
  78. return image_exec ( image );
  79. }
  80. /**
  81. * Identify the first loaded image
  82. *
  83. * @ret image Image, or NULL
  84. */
  85. struct image * imgautoselect ( void ) {
  86. struct image *image;
  87. for_each_image ( image ) {
  88. if ( image->flags & IMAGE_LOADED )
  89. return image;
  90. }
  91. return NULL;
  92. }
  93. /**
  94. * Display status of an image
  95. *
  96. * @v image Executable/loadable image
  97. */
  98. void imgstat ( struct image *image ) {
  99. printf ( "%s: %zd bytes", image->name, image->len );
  100. if ( image->type )
  101. printf ( " [%s]", image->type->name );
  102. if ( image->flags & IMAGE_LOADED )
  103. printf ( " [LOADED]" );
  104. if ( image->cmdline[0] )
  105. printf ( " \"%s\"", image->cmdline );
  106. printf ( "\n" );
  107. }
  108. /**
  109. * Free an image
  110. *
  111. * @v image Executable/loadable image
  112. */
  113. void imgfree ( struct image *image ) {
  114. unregister_image ( image );
  115. }