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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /* Avoid ending up with multiple "selected" images on
  115. * re-registration
  116. */
  117. if ( image_find_selected() )
  118. image->flags &= ~IMAGE_SELECTED;
  119. /* Add to image list */
  120. image_get ( image );
  121. image->flags |= IMAGE_REGISTERED;
  122. list_add_tail ( &image->list, &images );
  123. DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
  124. image->name, user_to_phys ( image->data, 0 ),
  125. user_to_phys ( image->data, image->len ) );
  126. return 0;
  127. }
  128. /**
  129. * Unregister executable image
  130. *
  131. * @v image Executable image
  132. */
  133. void unregister_image ( struct image *image ) {
  134. DBGC ( image, "IMAGE %s unregistered\n", image->name );
  135. list_del ( &image->list );
  136. image->flags &= ~IMAGE_REGISTERED;
  137. image_put ( image );
  138. }
  139. /**
  140. * Find image by name
  141. *
  142. * @v name Image name
  143. * @ret image Executable image, or NULL
  144. */
  145. struct image * find_image ( const char *name ) {
  146. struct image *image;
  147. list_for_each_entry ( image, &images, list ) {
  148. if ( strcmp ( image->name, name ) == 0 )
  149. return image;
  150. }
  151. return NULL;
  152. }
  153. /**
  154. * Determine image type
  155. *
  156. * @v image Executable image
  157. * @ret rc Return status code
  158. */
  159. int image_probe ( struct image *image ) {
  160. struct image_type *type;
  161. int rc;
  162. /* Succeed if we already have a type */
  163. if ( image->type )
  164. return 0;
  165. /* Try each type in turn */
  166. for_each_table_entry ( type, IMAGE_TYPES ) {
  167. if ( ( rc = type->probe ( image ) ) == 0 ) {
  168. image->type = type;
  169. DBGC ( image, "IMAGE %s is %s\n",
  170. image->name, type->name );
  171. return 0;
  172. }
  173. DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
  174. type->name, strerror ( rc ) );
  175. }
  176. DBGC ( image, "IMAGE %s format not recognised\n", image->name );
  177. return -ENOEXEC;
  178. }
  179. /**
  180. * Execute image
  181. *
  182. * @v image Executable image
  183. * @ret rc Return status code
  184. *
  185. * The image must already be registered. Note that executing an image
  186. * may cause it to unregister itself. The caller must therefore
  187. * assume that the image pointer becomes invalid.
  188. */
  189. int image_exec ( struct image *image ) {
  190. struct image *saved_current_image;
  191. struct image *replacement;
  192. struct uri *old_cwuri;
  193. int rc;
  194. /* Sanity check */
  195. assert ( image->flags & IMAGE_REGISTERED );
  196. /* Check that this image can be executed */
  197. if ( ( rc = image_probe ( image ) ) != 0 )
  198. return rc;
  199. /* Switch current working directory to be that of the image itself */
  200. old_cwuri = uri_get ( cwuri );
  201. churi ( image->uri );
  202. /* Preserve record of any currently-running image */
  203. saved_current_image = current_image;
  204. /* Take out a temporary reference to the image. This allows
  205. * the image to unregister itself if necessary, without
  206. * automatically freeing itself.
  207. */
  208. current_image = image_get ( image );
  209. /* Try executing the image */
  210. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  211. DBGC ( image, "IMAGE %s could not execute: %s\n",
  212. image->name, strerror ( rc ) );
  213. /* Do not return yet; we still have clean-up to do */
  214. }
  215. /* Pick up replacement image before we drop the original
  216. * image's temporary reference. The replacement image must
  217. * already be registered, so we don't need to hold a temporary
  218. * reference (which would complicate the tail-recursion).
  219. */
  220. replacement = image->replacement;
  221. if ( replacement )
  222. assert ( replacement->flags & IMAGE_REGISTERED );
  223. /* Drop temporary reference to the original image */
  224. image_put ( image );
  225. /* Restore previous currently-running image */
  226. current_image = saved_current_image;
  227. /* Reset current working directory */
  228. churi ( old_cwuri );
  229. uri_put ( old_cwuri );
  230. /* Tail-recurse into replacement image, if one exists */
  231. if ( replacement ) {
  232. DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
  233. image->name, replacement->name );
  234. if ( ( rc = image_exec ( replacement ) ) != 0 )
  235. return rc;
  236. }
  237. return rc;
  238. }
  239. /**
  240. * Set replacement image
  241. *
  242. * @v replacement Replacement image
  243. * @ret rc Return status code
  244. *
  245. * The replacement image must already be registered, and must remain
  246. * registered until the currently-executing image returns.
  247. */
  248. int image_replace ( struct image *replacement ) {
  249. struct image *image = current_image;
  250. int rc;
  251. /* Sanity check */
  252. assert ( replacement->flags & IMAGE_REGISTERED );
  253. /* Fail unless there is a currently-executing image */
  254. if ( ! image ) {
  255. rc = -ENOTTY;
  256. DBGC ( replacement, "IMAGE %s cannot replace non-existent "
  257. "image: %s\n", replacement->name, strerror ( rc ) );
  258. return rc;
  259. }
  260. /* Clear any existing replacement */
  261. image_put ( image->replacement );
  262. /* Set replacement */
  263. image->replacement = image_get ( replacement );
  264. DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
  265. image->name, replacement->name );
  266. return 0;
  267. }
  268. /**
  269. * Select image for execution
  270. *
  271. * @v image Executable image
  272. * @ret rc Return status code
  273. */
  274. int image_select ( struct image *image ) {
  275. struct image *tmp;
  276. int rc;
  277. /* Unselect all other images */
  278. for_each_image ( tmp )
  279. tmp->flags &= ~IMAGE_SELECTED;
  280. /* Check that this image can be executed */
  281. if ( ( rc = image_probe ( image ) ) != 0 )
  282. return rc;
  283. /* Mark image as selected */
  284. image->flags |= IMAGE_SELECTED;
  285. return 0;
  286. }
  287. /**
  288. * Find selected image
  289. *
  290. * @ret image Executable image, or NULL
  291. */
  292. struct image * image_find_selected ( void ) {
  293. struct image *image;
  294. for_each_image ( image ) {
  295. if ( image->flags & IMAGE_SELECTED )
  296. return image;
  297. }
  298. return NULL;
  299. }