Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

image.c 7.3KB

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