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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. * Determine image type
  140. *
  141. * @v image Executable image
  142. * @ret rc Return status code
  143. */
  144. static int image_probe ( struct image *image ) {
  145. struct image_type *type;
  146. int rc;
  147. /* Try each type in turn */
  148. for_each_table_entry ( type, IMAGE_TYPES ) {
  149. if ( ( rc = type->probe ( image ) ) == 0 ) {
  150. image->type = type;
  151. DBGC ( image, "IMAGE %s is %s\n",
  152. image->name, type->name );
  153. break;
  154. }
  155. DBGC ( image, "IMAGE %s is not %s: %s\n", image->name,
  156. type->name, strerror ( rc ) );
  157. }
  158. DBGC ( image, "IMAGE %s format not recognised\n", image->name );
  159. return -ENOTSUP;
  160. }
  161. /**
  162. * Register executable image
  163. *
  164. * @v image Executable image
  165. * @ret rc Return status code
  166. */
  167. int register_image ( struct image *image ) {
  168. static unsigned int imgindex = 0;
  169. char name[8]; /* "imgXXXX" */
  170. int rc;
  171. /* Create image name if it doesn't already have one */
  172. if ( ! image->name ) {
  173. snprintf ( name, sizeof ( name ), "img%d", imgindex++ );
  174. if ( ( rc = image_set_name ( image, name ) ) != 0 )
  175. return rc;
  176. }
  177. /* Avoid ending up with multiple "selected" images on
  178. * re-registration
  179. */
  180. if ( image_find_selected() )
  181. image->flags &= ~IMAGE_SELECTED;
  182. /* Add to image list */
  183. image_get ( image );
  184. image->flags |= IMAGE_REGISTERED;
  185. list_add_tail ( &image->list, &images );
  186. DBGC ( image, "IMAGE %s at [%lx,%lx) registered\n",
  187. image->name, user_to_phys ( image->data, 0 ),
  188. user_to_phys ( image->data, image->len ) );
  189. /* Try to detect image type, if applicable. Ignore failures,
  190. * since we expect to handle some unrecognised images
  191. * (e.g. kernel initrds, multiboot modules, random files
  192. * provided via our EFI virtual filesystem, etc).
  193. */
  194. if ( ! image->type )
  195. image_probe ( image );
  196. return 0;
  197. }
  198. /**
  199. * Unregister executable image
  200. *
  201. * @v image Executable image
  202. */
  203. void unregister_image ( struct image *image ) {
  204. /* Do nothing unless image is registered */
  205. if ( ! ( image->flags & IMAGE_REGISTERED ) )
  206. return;
  207. DBGC ( image, "IMAGE %s unregistered\n", image->name );
  208. list_del ( &image->list );
  209. image->flags &= ~IMAGE_REGISTERED;
  210. image_put ( image );
  211. }
  212. /**
  213. * Find image by name
  214. *
  215. * @v name Image name
  216. * @ret image Executable image, or NULL
  217. */
  218. struct image * find_image ( const char *name ) {
  219. struct image *image;
  220. list_for_each_entry ( image, &images, list ) {
  221. if ( strcmp ( image->name, name ) == 0 )
  222. return image;
  223. }
  224. return NULL;
  225. }
  226. /**
  227. * Execute image
  228. *
  229. * @v image Executable image
  230. * @ret rc Return status code
  231. *
  232. * The image must already be registered. Note that executing an image
  233. * may cause it to unregister itself. The caller must therefore
  234. * assume that the image pointer becomes invalid.
  235. */
  236. int image_exec ( struct image *image ) {
  237. struct image *saved_current_image;
  238. struct image *replacement = NULL;
  239. struct uri *old_cwuri;
  240. int rc;
  241. /* Sanity check */
  242. assert ( image->flags & IMAGE_REGISTERED );
  243. /* Switch current working directory to be that of the image itself */
  244. old_cwuri = uri_get ( cwuri );
  245. churi ( image->uri );
  246. /* Preserve record of any currently-running image */
  247. saved_current_image = current_image;
  248. /* Take out a temporary reference to the image. This allows
  249. * the image to unregister itself if necessary, without
  250. * automatically freeing itself.
  251. */
  252. current_image = image_get ( image );
  253. /* Check that this image can be executed */
  254. if ( ! ( image->type && image->type->exec ) ) {
  255. rc = -ENOEXEC;
  256. goto err;
  257. }
  258. /* Check that image is trusted (if applicable) */
  259. if ( require_trusted_images && ! ( image->flags & IMAGE_TRUSTED ) ) {
  260. DBGC ( image, "IMAGE %s is not trusted\n", image->name );
  261. rc = -EACCES_UNTRUSTED;
  262. goto err;
  263. }
  264. /* Record boot attempt */
  265. syslog ( LOG_NOTICE, "Executing \"%s\"\n", image->name );
  266. /* Try executing the image */
  267. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  268. DBGC ( image, "IMAGE %s could not execute: %s\n",
  269. image->name, strerror ( rc ) );
  270. /* Do not return yet; we still have clean-up to do */
  271. }
  272. /* Record result of boot attempt */
  273. if ( rc == 0 ) {
  274. syslog ( LOG_NOTICE, "Execution of \"%s\" completed\n",
  275. image->name );
  276. } else {
  277. syslog ( LOG_ERR, "Execution of \"%s\" failed: %s\n",
  278. image->name, strerror ( rc ) );
  279. }
  280. /* Pick up replacement image before we drop the original
  281. * image's temporary reference. The replacement image must
  282. * already be registered, so we don't need to hold a temporary
  283. * reference (which would complicate the tail-recursion).
  284. */
  285. replacement = image->replacement;
  286. if ( replacement )
  287. assert ( replacement->flags & IMAGE_REGISTERED );
  288. err:
  289. /* Unregister image if applicable */
  290. if ( image->flags & IMAGE_AUTO_UNREGISTER )
  291. unregister_image ( image );
  292. /* Debug message for tail-recursion. Placed here because the
  293. * image_put() may end up freeing the image.
  294. */
  295. if ( replacement ) {
  296. DBGC ( image, "IMAGE %s replacing self with IMAGE %s\n",
  297. image->name, replacement->name );
  298. }
  299. /* Drop temporary reference to the original image */
  300. image_put ( image );
  301. /* Restore previous currently-running image */
  302. current_image = saved_current_image;
  303. /* Reset current working directory */
  304. churi ( old_cwuri );
  305. uri_put ( old_cwuri );
  306. /* Tail-recurse into replacement image, if one exists */
  307. if ( replacement )
  308. return image_exec ( replacement );
  309. return rc;
  310. }
  311. /**
  312. * Set replacement image
  313. *
  314. * @v replacement Replacement image
  315. * @ret rc Return status code
  316. *
  317. * The replacement image must already be registered, and must remain
  318. * registered until the currently-executing image returns.
  319. */
  320. int image_replace ( struct image *replacement ) {
  321. struct image *image = current_image;
  322. int rc;
  323. /* Sanity check */
  324. assert ( replacement->flags & IMAGE_REGISTERED );
  325. /* Fail unless there is a currently-executing image */
  326. if ( ! image ) {
  327. rc = -ENOTTY;
  328. DBGC ( replacement, "IMAGE %s cannot replace non-existent "
  329. "image: %s\n", replacement->name, strerror ( rc ) );
  330. return rc;
  331. }
  332. /* Check that the replacement image can be executed */
  333. if ( ! ( replacement->type && replacement->type->exec ) )
  334. return -ENOEXEC;
  335. /* Clear any existing replacement */
  336. image_put ( image->replacement );
  337. /* Set replacement */
  338. image->replacement = image_get ( replacement );
  339. DBGC ( image, "IMAGE %s will replace self with IMAGE %s\n",
  340. image->name, replacement->name );
  341. return 0;
  342. }
  343. /**
  344. * Select image for execution
  345. *
  346. * @v image Executable image
  347. * @ret rc Return status code
  348. */
  349. int image_select ( struct image *image ) {
  350. struct image *tmp;
  351. /* Unselect all other images */
  352. for_each_image ( tmp )
  353. tmp->flags &= ~IMAGE_SELECTED;
  354. /* Check that this image can be executed */
  355. if ( ! ( image->type && image->type->exec ) )
  356. return -ENOEXEC;
  357. /* Mark image as selected */
  358. image->flags |= IMAGE_SELECTED;
  359. return 0;
  360. }
  361. /**
  362. * Find selected image
  363. *
  364. * @ret image Executable image, or NULL
  365. */
  366. struct image * image_find_selected ( void ) {
  367. struct image *image;
  368. for_each_image ( image ) {
  369. if ( image->flags & IMAGE_SELECTED )
  370. return image;
  371. }
  372. return NULL;
  373. }
  374. /**
  375. * Change image trust requirement
  376. *
  377. * @v require_trusted Require trusted images
  378. * @v permanent Make trust requirement permanent
  379. * @ret rc Return status code
  380. */
  381. int image_set_trust ( int require_trusted, int permanent ) {
  382. /* Update trust requirement, if permitted to do so */
  383. if ( ! require_trusted_images_permanent ) {
  384. require_trusted_images = require_trusted;
  385. require_trusted_images_permanent = permanent;
  386. }
  387. /* Fail if we attempted to change the trust requirement but
  388. * were not permitted to do so.
  389. */
  390. if ( require_trusted_images != require_trusted )
  391. return -EACCES_PERMANENT;
  392. return 0;
  393. }
  394. /**
  395. * Create pixel buffer from image
  396. *
  397. * @v image Image
  398. * @v pixbuf Pixel buffer to fill in
  399. * @ret rc Return status code
  400. */
  401. int image_pixbuf ( struct image *image, struct pixel_buffer **pixbuf ) {
  402. int rc;
  403. /* Check that this image can be used to create a pixel buffer */
  404. if ( ! ( image->type && image->type->pixbuf ) )
  405. return -ENOTSUP;
  406. /* Try creating pixel buffer */
  407. if ( ( rc = image->type->pixbuf ( image, pixbuf ) ) != 0 ) {
  408. DBGC ( image, "IMAGE %s could not create pixel buffer: %s\n",
  409. image->name, strerror ( rc ) );
  410. return rc;
  411. }
  412. return 0;
  413. }