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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 images
  33. *
  34. */
  35. /** List of registered images */
  36. struct list_head images = LIST_HEAD_INIT ( images );
  37. /** Currently-executing image */
  38. struct image *current_image;
  39. /**
  40. * Free executable 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. free ( image->cmdline );
  47. uri_put ( image->uri );
  48. ufree ( image->data );
  49. image_put ( image->replacement );
  50. free ( image );
  51. DBGC ( image, "IMAGE %s freed\n", image->name );
  52. }
  53. /**
  54. * Allocate executable image
  55. *
  56. * @ret image Executable image
  57. */
  58. struct image * alloc_image ( void ) {
  59. struct image *image;
  60. image = zalloc ( sizeof ( *image ) );
  61. if ( image ) {
  62. ref_init ( &image->refcnt, free_image );
  63. }
  64. return image;
  65. }
  66. /**
  67. * Set image URI
  68. *
  69. * @v image Image
  70. * @v URI New image URI
  71. *
  72. * If no name is set, the name will be updated to the base name of the
  73. * URI path (if any).
  74. */
  75. void image_set_uri ( struct image *image, struct uri *uri ) {
  76. const char *path = uri->path;
  77. /* Replace URI reference */
  78. uri_put ( image->uri );
  79. image->uri = uri_get ( uri );
  80. /* Set name if none already specified */
  81. if ( path && ( ! image->name[0] ) )
  82. image_set_name ( image, basename ( ( char * ) path ) );
  83. }
  84. /**
  85. * Set image command line
  86. *
  87. * @v image Image
  88. * @v cmdline New image command line, or NULL
  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 = NULL;
  94. if ( cmdline ) {
  95. image->cmdline = strdup ( cmdline );
  96. if ( ! image->cmdline )
  97. return -ENOMEM;
  98. }
  99. return 0;
  100. }
  101. /**
  102. * Register executable image
  103. *
  104. * @v image Executable image
  105. * @ret rc Return status code
  106. */
  107. int register_image ( struct image *image ) {
  108. static unsigned int imgindex = 0;
  109. /* Create image name if it doesn't already have one */
  110. if ( ! image->name[0] ) {
  111. snprintf ( image->name, sizeof ( image->name ), "img%d",
  112. imgindex++ );
  113. }
  114. /* Add to image list */
  115. image_get ( image );
  116. list_add_tail ( &image->list, &images );
  117. DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
  118. image->name, user_to_phys ( image->data, 0 ),
  119. user_to_phys ( image->data, image->len ) );
  120. return 0;
  121. }
  122. /**
  123. * Unregister executable image
  124. *
  125. * @v image Executable image
  126. */
  127. void unregister_image ( struct image *image ) {
  128. DBGC ( image, "IMAGE %s unregistered\n", image->name );
  129. list_del ( &image->list );
  130. image_put ( image );
  131. }
  132. /**
  133. * Find image by name
  134. *
  135. * @v name Image name
  136. * @ret image Executable image, or NULL
  137. */
  138. struct image * find_image ( const char *name ) {
  139. struct image *image;
  140. list_for_each_entry ( image, &images, list ) {
  141. if ( strcmp ( image->name, name ) == 0 )
  142. return image;
  143. }
  144. return NULL;
  145. }
  146. /**
  147. * Determine image type
  148. *
  149. * @v image Executable image
  150. * @ret rc Return status code
  151. */
  152. int image_probe ( struct image *image ) {
  153. struct image_type *type;
  154. int rc;
  155. /* Succeed if we already have a type */
  156. if ( image->type )
  157. return 0;
  158. /* Try each type in turn */
  159. for_each_table_entry ( type, IMAGE_TYPES ) {
  160. if ( ( rc = type->probe ( image ) ) == 0 ) {
  161. image->type = type;
  162. DBGC ( image, "IMAGE %s is %s\n",
  163. image->name, type->name );
  164. return 0;
  165. }
  166. DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
  167. type->name, strerror ( rc ) );
  168. }
  169. DBGC ( image, "IMAGE %s format not recognised\n", image->name );
  170. return -ENOEXEC;
  171. }
  172. /**
  173. * Execute image
  174. *
  175. * @v image Executable image
  176. * @ret rc Return status code
  177. */
  178. int image_exec ( struct image *image ) {
  179. struct image *saved_current_image;
  180. struct image *replacement;
  181. struct uri *old_cwuri;
  182. int rc;
  183. /* Check that this image can be executed */
  184. if ( ( rc = image_probe ( image ) ) != 0 )
  185. return rc;
  186. /* Switch current working directory to be that of the image itself */
  187. old_cwuri = uri_get ( cwuri );
  188. churi ( image->uri );
  189. /* Preserve record of any currently-running image */
  190. saved_current_image = current_image;
  191. /* Take out a temporary reference to the image. This allows
  192. * the image to unregister itself if necessary, without
  193. * automatically freeing itself.
  194. */
  195. current_image = image_get ( image );
  196. /* Try executing the image */
  197. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  198. DBGC ( image, "IMAGE %s could not execute: %s\n",
  199. image->name, strerror ( rc ) );
  200. /* Do not return yet; we still have clean-up to do */
  201. }
  202. /* Pick up replacement image before we drop the original
  203. * image's temporary reference.
  204. */
  205. replacement = image->replacement;
  206. /* Drop temporary reference to the original image */
  207. image_put ( image );
  208. /* Restore previous currently-running image */
  209. current_image = saved_current_image;
  210. /* Reset current working directory */
  211. churi ( old_cwuri );
  212. uri_put ( old_cwuri );
  213. /* Tail-recurse into replacement image, if one exists */
  214. if ( replacement ) {
  215. DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
  216. image->name, replacement->name );
  217. if ( ( rc = image_exec ( replacement ) ) != 0 )
  218. return rc;
  219. }
  220. return rc;
  221. }
  222. /**
  223. * Select image for execution
  224. *
  225. * @v image Executable image
  226. * @ret rc Return status code
  227. */
  228. int image_select ( struct image *image ) {
  229. struct image *tmp;
  230. int rc;
  231. /* Unselect all other images */
  232. for_each_image ( tmp )
  233. tmp->flags &= ~IMAGE_SELECTED;
  234. /* Check that this image can be executed */
  235. if ( ( rc = image_probe ( image ) ) != 0 )
  236. return rc;
  237. /* Mark image as selected */
  238. image->flags |= IMAGE_SELECTED;
  239. return 0;
  240. }
  241. /**
  242. * Find selected image
  243. *
  244. * @ret image Executable image, or NULL
  245. */
  246. struct image * image_find_selected ( void ) {
  247. struct image *image;
  248. for_each_image ( image ) {
  249. if ( image->flags & IMAGE_SELECTED )
  250. return image;
  251. }
  252. return NULL;
  253. }