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.

image.c 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2006 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 <stddef.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <assert.h>
  24. #include <gpxe/list.h>
  25. #include <gpxe/umalloc.h>
  26. #include <gpxe/image.h>
  27. /** @file
  28. *
  29. * Executable/loadable images
  30. *
  31. */
  32. /** List of registered images */
  33. struct list_head images = LIST_HEAD_INIT ( images );
  34. /** List of image types */
  35. static struct image_type image_types[0]
  36. __table_start ( struct image_type, image_types );
  37. static struct image_type image_types_end[0]
  38. __table_end ( struct image_type, image_types );
  39. /**
  40. * Free executable/loadable image
  41. *
  42. * @v refcnt Reference counter
  43. */
  44. static void free_image ( struct refcnt *refcnt ) {
  45. struct image *image = container_of ( refcnt, struct image, refcnt );
  46. ufree ( image->data );
  47. free ( image );
  48. DBGC ( image, "IMAGE %p freed\n", image );
  49. }
  50. /**
  51. * Allocate executable/loadable image
  52. *
  53. * @ret image Executable/loadable image
  54. */
  55. struct image * alloc_image ( void ) {
  56. struct image *image;
  57. image = zalloc ( sizeof ( *image ) );
  58. if ( image ) {
  59. image->refcnt.free = free_image;
  60. }
  61. return image;
  62. }
  63. /**
  64. * Register executable/loadable image
  65. *
  66. * @v image Executable/loadable image
  67. * @ret rc Return status code
  68. */
  69. int register_image ( struct image *image ) {
  70. static unsigned int imgindex = 0;
  71. /* Create image name if it doesn't already have one */
  72. if ( ! image->name[0] ) {
  73. snprintf ( image->name, sizeof ( image->name ), "img%d",
  74. imgindex++ );
  75. }
  76. /* Add to image list */
  77. image_get ( image );
  78. list_add_tail ( &image->list, &images );
  79. DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
  80. image, user_to_phys ( image->data, 0 ),
  81. user_to_phys ( image->data, image->len ), image->name );
  82. return 0;
  83. }
  84. /**
  85. * Unregister executable/loadable image
  86. *
  87. * @v image Executable/loadable image
  88. */
  89. void unregister_image ( struct image *image ) {
  90. list_del ( &image->list );
  91. image_put ( image );
  92. DBGC ( image, "IMAGE %p unregistered\n", image );
  93. }
  94. /**
  95. * Find image by name
  96. *
  97. * @v name Image name
  98. * @ret image Executable/loadable image, or NULL
  99. */
  100. struct image * find_image ( const char *name ) {
  101. struct image *image;
  102. list_for_each_entry ( image, &images, list ) {
  103. if ( strcmp ( image->name, name ) == 0 )
  104. return image;
  105. }
  106. return NULL;
  107. }
  108. /**
  109. * Load executable/loadable image into memory
  110. *
  111. * @v image Executable/loadable image
  112. * @v type Executable/loadable image type
  113. * @ret rc Return status code
  114. */
  115. static int image_load_type ( struct image *image, struct image_type *type ) {
  116. int rc;
  117. /* Check image is actually loadable */
  118. if ( ! type->load )
  119. return -ENOEXEC;
  120. /* Try the image loader */
  121. if ( ( rc = type->load ( image ) ) != 0 ) {
  122. DBGC ( image, "IMAGE %p could not load as %s: %s\n",
  123. image, type->name, strerror ( rc ) );
  124. return rc;
  125. }
  126. /* Flag as loaded */
  127. image->flags |= IMAGE_LOADED;
  128. return 0;
  129. }
  130. /**
  131. * Load executable/loadable image into memory
  132. *
  133. * @v image Executable/loadable image
  134. * @ret rc Return status code
  135. */
  136. int image_load ( struct image *image ) {
  137. assert ( image->type != NULL );
  138. return image_load_type ( image, image->type );
  139. }
  140. /**
  141. * Autodetect image type and load executable/loadable image into memory
  142. *
  143. * @v image Executable/loadable image
  144. * @ret rc Return status code
  145. */
  146. int image_autoload ( struct image *image ) {
  147. struct image_type *type;
  148. int rc;
  149. /* If image already has a type, use it */
  150. if ( image->type )
  151. return image_load ( image );
  152. /* Otherwise probe for a suitable type */
  153. for ( type = image_types ; type < image_types_end ; type++ ) {
  154. DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
  155. rc = image_load_type ( image, type );
  156. if ( image->type == NULL )
  157. continue;
  158. return rc;
  159. }
  160. DBGC ( image, "IMAGE %p format not recognised\n", image );
  161. return -ENOEXEC;
  162. }
  163. /**
  164. * Execute loaded image
  165. *
  166. * @v image Loaded image
  167. * @ret rc Return status code
  168. */
  169. int image_exec ( struct image *image ) {
  170. int rc;
  171. /* Image must be loaded first */
  172. if ( ! ( image->flags & IMAGE_LOADED ) ) {
  173. DBGC ( image, "IMAGE %p could not execute: not loaded\n",
  174. image );
  175. return -ENOTTY;
  176. }
  177. assert ( image->type != NULL );
  178. /* Check that image is actually executable */
  179. if ( ! image->type->exec )
  180. return -ENOEXEC;
  181. /* Try executing the image */
  182. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  183. DBGC ( image, "IMAGE %p could not execute: %s\n",
  184. image, strerror ( rc ) );
  185. return rc;
  186. }
  187. /* Well, some formats might return... */
  188. return 0;
  189. }