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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/umalloc.h>
  24. #include <gpxe/download.h>
  25. #include <usr/imgmgmt.h>
  26. /** @file
  27. *
  28. * Image management
  29. *
  30. */
  31. /**
  32. * Fetch an image
  33. *
  34. * @v uri_string URI as a string (e.g. "http://www.nowhere.com/vmlinuz")
  35. * @v name Name for image, or NULL
  36. * @ret new_image Newly created image
  37. * @ret rc Return status code
  38. */
  39. int imgfetch ( const char *uri_string, const char *name,
  40. struct image **new_image ) {
  41. struct image *image;
  42. struct async async;
  43. int rc;
  44. /* Allocate new image */
  45. image = malloc ( sizeof ( *image ) );
  46. if ( ! image )
  47. return -ENOMEM;
  48. memset ( image, 0, sizeof ( *image ) );
  49. /* Fill in image name */
  50. if ( name )
  51. strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
  52. /* Download the file */
  53. if ( ( rc = async_block_progress ( &async,
  54. start_download ( uri_string, &async,
  55. &image->data,
  56. &image->len )))!=0)
  57. goto err;
  58. /* Register the image */
  59. if ( ( rc = register_image ( image ) ) != 0 )
  60. goto err;
  61. *new_image = image;
  62. return 0;
  63. err:
  64. ufree ( image->data );
  65. free ( image );
  66. return rc;
  67. }
  68. /**
  69. * Load an image
  70. *
  71. * @v image Image
  72. * @ret rc Return status code
  73. */
  74. int imgload ( struct image *image ) {
  75. int rc;
  76. /* Try to load image */
  77. if ( ( rc = image_autoload ( image ) ) != 0 )
  78. return rc;
  79. /* If we succeed, move the image to the start of the list */
  80. promote_image ( image );
  81. return 0;
  82. }
  83. /**
  84. * Execute an image
  85. *
  86. * @v image Image
  87. * @ret rc Return status code
  88. */
  89. int imgexec ( struct image *image ) {
  90. return image_exec ( image );
  91. }
  92. /**
  93. * Identify the first loaded image
  94. *
  95. * @ret image Image, or NULL
  96. */
  97. struct image * imgautoselect ( void ) {
  98. struct image *image;
  99. for_each_image ( image ) {
  100. if ( image->flags & IMAGE_LOADED )
  101. return image;
  102. }
  103. return NULL;
  104. }
  105. /**
  106. * Display status of an image
  107. *
  108. * @v image Executable/loadable image
  109. */
  110. void imgstat ( struct image *image ) {
  111. printf ( "%s: %zd bytes", image->name, image->len );
  112. if ( image->type )
  113. printf ( " [%s]", image->type->name );
  114. if ( image->flags & IMAGE_LOADED )
  115. printf ( " [LOADED]" );
  116. if ( image->cmdline[0] )
  117. printf ( " \"%s\"", image->cmdline );
  118. printf ( "\n" );
  119. }
  120. /**
  121. * Free an image
  122. *
  123. * @v image Executable/loadable image
  124. */
  125. void imgfree ( struct image *image ) {
  126. unregister_image ( image );
  127. ufree ( image->data );
  128. free ( image );
  129. }