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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 <libgen.h>
  25. #include <gpxe/list.h>
  26. #include <gpxe/umalloc.h>
  27. #include <gpxe/uri.h>
  28. #include <gpxe/image.h>
  29. /** @file
  30. *
  31. * Executable/loadable images
  32. *
  33. */
  34. /** List of registered images */
  35. struct list_head images = LIST_HEAD_INIT ( images );
  36. /** List of image types */
  37. static struct image_type image_types[0]
  38. __table_start ( struct image_type, image_types );
  39. static struct image_type image_types_end[0]
  40. __table_end ( struct image_type, image_types );
  41. /**
  42. * Free executable/loadable image
  43. *
  44. * @v refcnt Reference counter
  45. */
  46. static void free_image ( struct refcnt *refcnt ) {
  47. struct image *image = container_of ( refcnt, struct image, refcnt );
  48. uri_put ( image->uri );
  49. ufree ( image->data );
  50. free ( image );
  51. DBGC ( image, "IMAGE %p freed\n", image );
  52. }
  53. /**
  54. * Allocate executable/loadable image
  55. *
  56. * @ret image Executable/loadable image
  57. */
  58. struct image * alloc_image ( void ) {
  59. struct image *image;
  60. image = zalloc ( sizeof ( *image ) );
  61. if ( image ) {
  62. image->refcnt.free = free_image;
  63. }
  64. return image;
  65. }
  66. /**
  67. * Set image URI
  68. *
  69. * @v image Image
  70. * @v URI New image URI
  71. * @ret rc Return status code
  72. *
  73. * If no name is set, the name will be updated to the base name of the
  74. * URI path (if any).
  75. */
  76. int image_set_uri ( struct image *image, struct uri *uri ) {
  77. const char *path = uri->path;
  78. /* Replace URI reference */
  79. uri_put ( image->uri );
  80. image->uri = uri_get ( uri );
  81. /* Set name if none already specified */
  82. if ( path && ( ! image->name[0] ) )
  83. image_set_name ( image, basename ( ( char * ) path ) );
  84. return 0;
  85. }
  86. /**
  87. * Set image command line
  88. *
  89. * @v image Image
  90. * @v cmdline New image command line
  91. * @ret rc Return status code
  92. */
  93. int image_set_cmdline ( struct image *image, const char *cmdline ) {
  94. free ( image->cmdline );
  95. image->cmdline = strdup ( cmdline );
  96. if ( ! image->cmdline )
  97. return -ENOMEM;
  98. return 0;
  99. }
  100. /**
  101. * Register executable/loadable image
  102. *
  103. * @v image Executable/loadable image
  104. * @ret rc Return status code
  105. */
  106. int register_image ( struct image *image ) {
  107. static unsigned int imgindex = 0;
  108. /* Create image name if it doesn't already have one */
  109. if ( ! image->name[0] ) {
  110. snprintf ( image->name, sizeof ( image->name ), "img%d",
  111. imgindex++ );
  112. }
  113. /* Add to image list */
  114. image_get ( image );
  115. list_add_tail ( &image->list, &images );
  116. DBGC ( image, "IMAGE %p at [%lx,%lx) registered as %s\n",
  117. image, user_to_phys ( image->data, 0 ),
  118. user_to_phys ( image->data, image->len ), image->name );
  119. return 0;
  120. }
  121. /**
  122. * Unregister executable/loadable image
  123. *
  124. * @v image Executable/loadable image
  125. */
  126. void unregister_image ( struct image *image ) {
  127. list_del ( &image->list );
  128. image_put ( image );
  129. DBGC ( image, "IMAGE %p unregistered\n", image );
  130. }
  131. /**
  132. * Find image by name
  133. *
  134. * @v name Image name
  135. * @ret image Executable/loadable image, or NULL
  136. */
  137. struct image * find_image ( const char *name ) {
  138. struct image *image;
  139. list_for_each_entry ( image, &images, list ) {
  140. if ( strcmp ( image->name, name ) == 0 )
  141. return image;
  142. }
  143. return NULL;
  144. }
  145. /**
  146. * Load executable/loadable image into memory
  147. *
  148. * @v image Executable/loadable image
  149. * @v type Executable/loadable image type
  150. * @ret rc Return status code
  151. */
  152. static int image_load_type ( struct image *image, struct image_type *type ) {
  153. int rc;
  154. /* Check image is actually loadable */
  155. if ( ! type->load )
  156. return -ENOEXEC;
  157. /* Try the image loader */
  158. if ( ( rc = type->load ( image ) ) != 0 ) {
  159. DBGC ( image, "IMAGE %p could not load as %s: %s\n",
  160. image, type->name, strerror ( rc ) );
  161. return rc;
  162. }
  163. /* Flag as loaded */
  164. image->flags |= IMAGE_LOADED;
  165. return 0;
  166. }
  167. /**
  168. * Load executable/loadable image into memory
  169. *
  170. * @v image Executable/loadable image
  171. * @ret rc Return status code
  172. */
  173. int image_load ( struct image *image ) {
  174. assert ( image->type != NULL );
  175. return image_load_type ( image, image->type );
  176. }
  177. /**
  178. * Autodetect image type and load executable/loadable image into memory
  179. *
  180. * @v image Executable/loadable image
  181. * @ret rc Return status code
  182. */
  183. int image_autoload ( struct image *image ) {
  184. struct image_type *type;
  185. int rc;
  186. /* If image already has a type, use it */
  187. if ( image->type )
  188. return image_load ( image );
  189. /* Otherwise probe for a suitable type */
  190. for ( type = image_types ; type < image_types_end ; type++ ) {
  191. DBGC ( image, "IMAGE %p trying type %s\n", image, type->name );
  192. rc = image_load_type ( image, type );
  193. if ( image->type == NULL )
  194. continue;
  195. return rc;
  196. }
  197. DBGC ( image, "IMAGE %p format not recognised\n", image );
  198. return -ENOEXEC;
  199. }
  200. /**
  201. * Execute loaded image
  202. *
  203. * @v image Loaded image
  204. * @ret rc Return status code
  205. */
  206. int image_exec ( struct image *image ) {
  207. struct uri *old_cwuri;
  208. int rc;
  209. /* Image must be loaded first */
  210. if ( ! ( image->flags & IMAGE_LOADED ) ) {
  211. DBGC ( image, "IMAGE %p could not execute: not loaded\n",
  212. image );
  213. return -ENOTTY;
  214. }
  215. assert ( image->type != NULL );
  216. /* Check that image is actually executable */
  217. if ( ! image->type->exec )
  218. return -ENOEXEC;
  219. /* Switch current working directory to be that of the image itself */
  220. old_cwuri = uri_get ( cwuri );
  221. churi ( image->uri );
  222. /* Try executing the image */
  223. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  224. DBGC ( image, "IMAGE %p could not execute: %s\n",
  225. image, strerror ( rc ) );
  226. goto done;
  227. }
  228. done:
  229. /* Reset current working directory */
  230. churi ( old_cwuri );
  231. uri_put ( old_cwuri );
  232. return rc;
  233. }
  234. /**
  235. * Register and autoload an image
  236. *
  237. * @v image Image
  238. * @ret rc Return status code
  239. */
  240. int register_and_autoload_image ( struct image *image ) {
  241. int rc;
  242. if ( ( rc = register_image ( image ) ) != 0 )
  243. return rc;
  244. if ( ( rc = image_autoload ( image ) ) != 0 )
  245. return rc;
  246. return 0;
  247. }
  248. /**
  249. * Register and autoexec an image
  250. *
  251. * @v image Image
  252. * @ret rc Return status code
  253. */
  254. int register_and_autoexec_image ( struct image *image ) {
  255. int rc;
  256. if ( ( rc = register_and_autoload_image ( image ) ) != 0 )
  257. return rc;
  258. if ( ( rc = image_exec ( image ) ) != 0 )
  259. return rc;
  260. return 0;
  261. }