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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <errno.h>
  29. #include <assert.h>
  30. #include <libgen.h>
  31. #include <syslog.h>
  32. #include <ipxe/list.h>
  33. #include <ipxe/umalloc.h>
  34. #include <ipxe/uri.h>
  35. #include <ipxe/image.h>
  36. /** @file
  37. *
  38. * Executable images
  39. *
  40. */
  41. /* Disambiguate the various error causes */
  42. #define EACCES_UNTRUSTED \
  43. __einfo_error ( EINFO_EACCES_UNTRUSTED )
  44. #define EINFO_EACCES_UNTRUSTED \
  45. __einfo_uniqify ( EINFO_EACCES, 0x01, "Untrusted image" )
  46. #define EACCES_PERMANENT \
  47. __einfo_error ( EINFO_EACCES_PERMANENT )
  48. #define EINFO_EACCES_PERMANENT \
  49. __einfo_uniqify ( EINFO_EACCES, 0x02, "Trust requirement is permanent" )
  50. /** List of registered images */
  51. struct list_head images = LIST_HEAD_INIT ( images );
  52. /** Currently-executing image */
  53. struct image *current_image;
  54. /** Current image trust requirement */
  55. static int require_trusted_images = 0;
  56. /** Prevent changes to image trust requirement */
  57. static int require_trusted_images_permanent = 0;
  58. /**
  59. * Free executable image
  60. *
  61. * @v refcnt Reference counter
  62. */
  63. static void free_image ( struct refcnt *refcnt ) {
  64. struct image *image = container_of ( refcnt, struct image, refcnt );
  65. DBGC ( image, "IMAGE %s freed\n", image->name );
  66. free ( image->name );
  67. free ( image->cmdline );
  68. uri_put ( image->uri );
  69. ufree ( image->data );
  70. image_put ( image->replacement );
  71. free ( image );
  72. }
  73. /**
  74. * Allocate executable image
  75. *
  76. * @v uri URI, or NULL
  77. * @ret image Executable image
  78. */
  79. struct image * alloc_image ( struct uri *uri ) {
  80. const char *name;
  81. struct image *image;
  82. int rc;
  83. /* Allocate image */
  84. image = zalloc ( sizeof ( *image ) );
  85. if ( ! image )
  86. goto err_alloc;
  87. /* Initialise image */
  88. ref_init ( &image->refcnt, free_image );
  89. if ( uri ) {
  90. image->uri = uri_get ( uri );
  91. if ( uri->path ) {
  92. name = basename ( ( char * ) uri->path );
  93. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  94. goto err_set_name;
  95. }
  96. }
  97. return image;
  98. err_set_name:
  99. image_put ( image );
  100. err_alloc:
  101. return NULL;
  102. }
  103. /**
  104. * Set image name
  105. *
  106. * @v image Image
  107. * @v name New image name
  108. * @ret rc Return status code
  109. */
  110. int image_set_name ( struct image *image, const char *name ) {
  111. char *name_copy;
  112. /* Duplicate name */
  113. name_copy = strdup ( name );
  114. if ( ! name_copy )
  115. return -ENOMEM;
  116. /* Replace existing name */
  117. free ( image->name );
  118. image->name = name_copy;
  119. return 0;
  120. }
  121. /**
  122. * Set image command line
  123. *
  124. * @v image Image
  125. * @v cmdline New image command line, or NULL
  126. * @ret rc Return status code
  127. */
  128. int image_set_cmdline ( struct image *image, const char *cmdline ) {
  129. free ( image->cmdline );
  130. image->cmdline = NULL;
  131. if ( cmdline ) {
  132. image->cmdline = strdup ( cmdline );
  133. if ( ! image->cmdline )
  134. return -ENOMEM;
  135. }
  136. return 0;
  137. }
  138. /**
  139. * Register executable image
  140. *
  141. * @v image Executable image
  142. * @ret rc Return status code
  143. */
  144. int register_image ( struct image *image ) {
  145. static unsigned int imgindex = 0;
  146. char name[8]; /* "imgXXXX" */
  147. int rc;
  148. /* Create image name if it doesn't already have one */
  149. if ( ! image->name ) {
  150. snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
  151. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  152. return rc;
  153. }
  154. /* Avoid ending up with multiple "selected" images on
  155. * re-registration
  156. */
  157. if ( image_find_selected() )
  158. image->flags &= ~IMAGE_SELECTED;
  159. /* Add to image list */
  160. image_get ( image );
  161. image->flags |= IMAGE_REGISTERED;
  162. list_add_tail ( &image->list, &images );
  163. DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
  164. image->name, user_to_phys ( image->data, 0 ),
  165. user_to_phys ( image->data, image->len ) );
  166. return 0;
  167. }
  168. /**
  169. * Unregister executable image
  170. *
  171. * @v image Executable image
  172. */
  173. void unregister_image ( struct image *image ) {
  174. /* Do nothing unless image is registered */
  175. if ( ! ( image->flags & IMAGE_REGISTERED ) )
  176. return;
  177. DBGC ( image, "IMAGE %s unregistered\n", image->name );
  178. list_del ( &image->list );
  179. image->flags &= ~IMAGE_REGISTERED;
  180. image_put ( image );
  181. }
  182. /**
  183. * Find image by name
  184. *
  185. * @v name Image name
  186. * @ret image Executable image, or NULL
  187. */
  188. struct image * find_image ( const char *name ) {
  189. struct image *image;
  190. list_for_each_entry ( image, &images, list ) {
  191. if ( strcmp ( image->name, name ) == 0 )
  192. return image;
  193. }
  194. return NULL;
  195. }
  196. /**
  197. * Determine image type
  198. *
  199. * @v image Executable image
  200. * @ret rc Return status code
  201. */
  202. int image_probe ( struct image *image ) {
  203. struct image_type *type;
  204. int rc;
  205. /* Succeed if we already have a type */
  206. if ( image->type )
  207. return 0;
  208. /* Try each type in turn */
  209. for_each_table_entry ( type, IMAGE_TYPES ) {
  210. if ( ( rc = type->probe ( image ) ) == 0 ) {
  211. image->type = type;
  212. DBGC ( image, "IMAGE %s is %s\n",
  213. image->name, type->name );
  214. return 0;
  215. }
  216. DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
  217. type->name, strerror ( rc ) );
  218. }
  219. DBGC ( image, "IMAGE %s format not recognised\n", image->name );
  220. return -ENOEXEC;
  221. }
  222. /**
  223. * Execute image
  224. *
  225. * @v image Executable image
  226. * @ret rc Return status code
  227. *
  228. * The image must already be registered. Note that executing an image
  229. * may cause it to unregister itself. The caller must therefore
  230. * assume that the image pointer becomes invalid.
  231. */
  232. int image_exec ( struct image *image ) {
  233. struct image *saved_current_image;
  234. struct image *replacement = NULL;
  235. struct uri *old_cwuri;
  236. int rc;
  237. /* Sanity check */
  238. assert ( image->flags & IMAGE_REGISTERED );
  239. /* Switch current working directory to be that of the image itself */
  240. old_cwuri = uri_get ( cwuri );
  241. churi ( image->uri );
  242. /* Preserve record of any currently-running image */
  243. saved_current_image = current_image;
  244. /* Take out a temporary reference to the image. This allows
  245. * the image to unregister itself if necessary, without
  246. * automatically freeing itself.
  247. */
  248. current_image = image_get ( image );
  249. /* Check that this image can be selected for execution */
  250. if ( ( rc = image_select ( image ) ) != 0 )
  251. goto err;
  252. /* Check that image is trusted (if applicable) */
  253. if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
  254. DBGC ( image, "IMAGE %s is not trusted\n", image->name );
  255. rc = -EACCES_UNTRUSTED;
  256. goto err;
  257. }
  258. /* Record boot attempt */
  259. syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
  260. /* Try executing the image */
  261. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  262. DBGC ( image, "IMAGE %s could not execute: %s\n",
  263. image->name, strerror ( rc ) );
  264. /* Do not return yet; we still have clean-up to do */
  265. }
  266. /* Record result of boot attempt */
  267. if ( rc == 0 ) {
  268. syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
  269. image->name );
  270. } else {
  271. syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
  272. image->name, strerror ( rc ) );
  273. }
  274. /* Pick up replacement image before we drop the original
  275. * image's temporary reference. The replacement image must
  276. * already be registered, so we don't need to hold a temporary
  277. * reference (which would complicate the tail-recursion).
  278. */
  279. replacement = image->replacement;
  280. if ( replacement )
  281. assert ( replacement->flags & IMAGE_REGISTERED );
  282. err:
  283. /* Unregister image if applicable */
  284. if ( image->flags & IMAGE_AUTO_UNREGISTER )
  285. unregister_image ( image );
  286. /* Debug message for tail-recursion. Placed here because the
  287. * image_put() may end up freeing the image.
  288. */
  289. if ( replacement ) {
  290. DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
  291. image->name, replacement->name );
  292. }
  293. /* Drop temporary reference to the original image */
  294. image_put ( image );
  295. /* Restore previous currently-running image */
  296. current_image = saved_current_image;
  297. /* Reset current working directory */
  298. churi ( old_cwuri );
  299. uri_put ( old_cwuri );
  300. /* Tail-recurse into replacement image, if one exists */
  301. if ( replacement )
  302. return image_exec ( replacement );
  303. return rc;
  304. }
  305. /**
  306. * Set replacement image
  307. *
  308. * @v replacement Replacement image
  309. * @ret rc Return status code
  310. *
  311. * The replacement image must already be registered, and must remain
  312. * registered until the currently-executing image returns.
  313. */
  314. int image_replace ( struct image *replacement ) {
  315. struct image *image = current_image;
  316. int rc;
  317. /* Sanity check */
  318. assert ( replacement->flags & IMAGE_REGISTERED );
  319. /* Fail unless there is a currently-executing image */
  320. if ( ! image ) {
  321. rc = -ENOTTY;
  322. DBGC ( replacement, "IMAGE %s cannot replace non-existent "
  323. "image: %s\n", replacement->name, strerror ( rc ) );
  324. return rc;
  325. }
  326. /* Check that the replacement image can be executed */
  327. if ( ( rc = image_probe ( replacement ) ) != 0 )
  328. return rc;
  329. /* Clear any existing replacement */
  330. image_put ( image->replacement );
  331. /* Set replacement */
  332. image->replacement = image_get ( replacement );
  333. DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
  334. image->name, replacement->name );
  335. return 0;
  336. }
  337. /**
  338. * Select image for execution
  339. *
  340. * @v image Executable image
  341. * @ret rc Return status code
  342. */
  343. int image_select ( struct image *image ) {
  344. struct image *tmp;
  345. int rc;
  346. /* Unselect all other images */
  347. for_each_image ( tmp )
  348. tmp->flags &= ~IMAGE_SELECTED;
  349. /* Check that this image can be executed */
  350. if ( ( rc = image_probe ( image ) ) != 0 )
  351. return rc;
  352. if ( ! image->type->exec )
  353. return -ENOEXEC;
  354. /* Mark image as selected */
  355. image->flags |= IMAGE_SELECTED;
  356. return 0;
  357. }
  358. /**
  359. * Find selected image
  360. *
  361. * @ret image Executable image, or NULL
  362. */
  363. struct image * image_find_selected ( void ) {
  364. struct image *image;
  365. for_each_image ( image ) {
  366. if ( image->flags & IMAGE_SELECTED )
  367. return image;
  368. }
  369. return NULL;
  370. }
  371. /**
  372. * Change image trust requirement
  373. *
  374. * @v require_trusted Require trusted images
  375. * @v permanent Make trust requirement permanent
  376. * @ret rc Return status code
  377. */
  378. int image_set_trust ( int require_trusted, int permanent ) {
  379. /* Update trust requirement, if permitted to do so */
  380. if ( ! require_trusted_images_permanent ) {
  381. require_trusted_images = require_trusted;
  382. require_trusted_images_permanent = permanent;
  383. }
  384. /* Fail if we attempted to change the trust requirement but
  385. * were not permitted to do so.
  386. */
  387. if ( require_trusted_images != require_trusted )
  388. return -EACCES_PERMANENT;
  389. return 0;
  390. }
  391. /**
  392. * Create pixel buffer from image
  393. *
  394. * @v image Image
  395. * @v pixbuf Pixel buffer to fill in
  396. * @ret rc Return status code
  397. */
  398. int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
  399. int rc;
  400. /* Check that this image can be used to create a pixel buffer */
  401. if ( ( rc = image_probe ( image ) ) != 0 )
  402. return rc;
  403. if ( ! image->type->pixbuf )
  404. return -ENOTSUP;
  405. /* Try creating pixel buffer */
  406. if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
  407. DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
  408. image->name, strerror ( rc ) );
  409. return rc;
  410. }
  411. return 0;
  412. }