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

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