Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

image.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 <errno.h>
  22. #include <assert.h>
  23. #include <vsprintf.h>
  24. #include <gpxe/list.h>
  25. #include <gpxe/image.h>
  26. /** @file
  27. *
  28. * Executable/loadable images
  29. *
  30. */
  31. /** List of registered images */
  32. struct list_head images = LIST_HEAD_INIT ( images );
  33. /** List of image types */
  34. static struct image_type image_types[0]
  35. __table_start ( struct image_type, image_types );
  36. static struct image_type image_types_end[0]
  37. __table_end ( struct image_type, image_types );
  38. /**
  39. * Register executable/loadable image
  40. *
  41. * @v image Executable/loadable image
  42. * @ret rc Return status code
  43. */
  44. int register_image ( struct image *image ) {
  45. static unsigned int imgindex = 0;
  46. /* Create image name */
  47. snprintf ( image->name, sizeof ( image->name ), "img%d",
  48. imgindex++ );
  49. /* Add to image list */
  50. list_add_tail ( &image->list, &images );
  51. DBGC ( image, "IMAGE %p registered as %s\n", image, image->name );
  52. return 0;
  53. }
  54. /**
  55. * Unregister executable/loadable image
  56. *
  57. * @v image Executable/loadable image
  58. */
  59. void unregister_image ( struct image *image ) {
  60. list_del ( &image->list );
  61. DBGC ( image, "IMAGE %p unregistered\n", image );
  62. }
  63. /**
  64. * Load executable/loadable image into memory
  65. *
  66. * @v image Executable/loadable image
  67. * @ret rc Return status code
  68. */
  69. int image_load ( struct image *image ) {
  70. int rc;
  71. assert ( image->type != NULL );
  72. if ( ( rc = image->type->load ( image ) ) != 0 ) {
  73. DBGC ( image, "IMAGE %p could not load: %s\n",
  74. image, strerror ( rc ) );
  75. return rc;
  76. }
  77. return 0;
  78. }
  79. /**
  80. * Autodetect image type and load executable/loadable image into memory
  81. *
  82. * @v image Executable/loadable image
  83. * @ret rc Return status code
  84. */
  85. int image_autoload ( struct image *image ) {
  86. struct image_type *type;
  87. int rc;
  88. for ( type = image_types ; type < image_types_end ; type++ ) {
  89. rc = type->load ( image );
  90. if ( image->type == NULL )
  91. continue;
  92. if ( rc != 0 ) {
  93. DBGC ( image, "IMAGE %p (%s) could not load: %s\n",
  94. image, image->type->name, strerror ( rc ) );
  95. return rc;
  96. }
  97. return 0;
  98. }
  99. DBGC ( image, "IMAGE %p format not recognised\n", image );
  100. return -ENOEXEC;
  101. }
  102. /**
  103. * Execute loaded image
  104. *
  105. * @v image Loaded image
  106. * @ret rc Return status code
  107. */
  108. int image_exec ( struct image *image ) {
  109. int rc;
  110. assert ( image->type != NULL );
  111. if ( ( rc = image->type->exec ( image ) ) != 0 ) {
  112. DBGC ( image, "IMAGE %p could not execute: %s\n",
  113. image, strerror ( rc ) );
  114. return rc;
  115. }
  116. /* Well, some formats might return... */
  117. return 0;
  118. }