選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

image.c 7.0KB

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