Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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