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.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef _GPXE_IMAGE_H
  2. #define _GPXE_IMAGE_H
  3. /**
  4. * @file
  5. *
  6. * Executable/loadable images
  7. *
  8. */
  9. #include <gpxe/tables.h>
  10. #include <gpxe/list.h>
  11. #include <gpxe/uaccess.h>
  12. #include <gpxe/refcnt.h>
  13. struct uri;
  14. struct image_type;
  15. /** An executable or loadable image */
  16. struct image {
  17. /** Reference count */
  18. struct refcnt refcnt;
  19. /** List of registered images */
  20. struct list_head list;
  21. /** URI of image */
  22. struct uri *uri;
  23. /** Name */
  24. char name[16];
  25. /** Flags */
  26. unsigned int flags;
  27. /** Command line to pass to image */
  28. char *cmdline;
  29. /** Raw file image */
  30. userptr_t data;
  31. /** Length of raw file image */
  32. size_t len;
  33. /** Image type, if known */
  34. struct image_type *type;
  35. /** Image type private data */
  36. union {
  37. physaddr_t phys;
  38. userptr_t user;
  39. unsigned long ul;
  40. } priv;
  41. /** Replacement image
  42. *
  43. * An image wishing to replace itself with another image (in a
  44. * style similar to a Unix exec() call) should return from its
  45. * exec() method with the replacement image set to point to
  46. * the new image. The new image must already be in a suitable
  47. * state for execution (i.e. loaded).
  48. *
  49. * If an image unregisters itself as a result of being
  50. * executed, it must make sure that its replacement image (if
  51. * any) is registered, otherwise the replacement is likely to
  52. * be freed before it can be executed.
  53. */
  54. struct image *replacement;
  55. };
  56. /** Image is loaded */
  57. #define IMAGE_LOADED 0x0001
  58. /** An executable or loadable image type */
  59. struct image_type {
  60. /** Name of this image type */
  61. char *name;
  62. /**
  63. * Load image into memory
  64. *
  65. * @v image Executable/loadable image
  66. * @ret rc Return status code
  67. *
  68. * Load the image into memory at the correct location as
  69. * determined by the file format.
  70. *
  71. * If the file image is in the correct format, the method must
  72. * update @c image->type to point to its own type (unless @c
  73. * image->type is already set). This allows the autoloading
  74. * code to disambiguate between "this is not my image format"
  75. * and "there is something wrong with this image". In
  76. * particular, setting @c image->type and then returning an
  77. * error will cause image_autoload() to abort and return an
  78. * error, rather than continuing to the next image type.
  79. */
  80. int ( * load ) ( struct image *image );
  81. /**
  82. * Execute loaded image
  83. *
  84. * @v image Loaded image
  85. * @ret rc Return status code
  86. *
  87. * Note that the image may be invalidated by the act of
  88. * execution, i.e. an image is allowed to choose to unregister
  89. * (and so potentially free) itself.
  90. */
  91. int ( * exec ) ( struct image *image );
  92. };
  93. /**
  94. * Multiboot image probe priority
  95. *
  96. * Multiboot images are also valid executables in another format
  97. * (e.g. ELF), so we must perform the multiboot probe first.
  98. */
  99. #define PROBE_MULTIBOOT 01
  100. /**
  101. * Normal image probe priority
  102. */
  103. #define PROBE_NORMAL 02
  104. /**
  105. * PXE image probe priority
  106. *
  107. * PXE images have no signature checks, so will claim all image files.
  108. * They must therefore be tried last in the probe order list.
  109. */
  110. #define PROBE_PXE 03
  111. /** An executable or loadable image type */
  112. #define __image_type( probe_order ) \
  113. __table ( struct image_type, image_types, probe_order )
  114. extern struct list_head images;
  115. /** Iterate over all registered images */
  116. #define for_each_image( image ) \
  117. list_for_each_entry ( (image), &images, list )
  118. /**
  119. * Test for existence of images
  120. *
  121. * @ret existence Some images exist
  122. */
  123. static inline int have_images ( void ) {
  124. return ( ! list_empty ( &images ) );
  125. }
  126. extern struct image * alloc_image ( void );
  127. extern int image_set_uri ( struct image *image, struct uri *uri );
  128. extern int image_set_cmdline ( struct image *image, const char *cmdline );
  129. extern int register_image ( struct image *image );
  130. extern void unregister_image ( struct image *image );
  131. extern void promote_image ( struct image *image );
  132. struct image * find_image ( const char *name );
  133. extern int image_load ( struct image *image );
  134. extern int image_autoload ( struct image *image );
  135. extern int image_exec ( struct image *image );
  136. extern int register_and_autoload_image ( struct image *image );
  137. extern int register_and_autoexec_image ( struct image *image );
  138. /**
  139. * Increment reference count on an image
  140. *
  141. * @v image Image
  142. * @ret image Image
  143. */
  144. static inline struct image * image_get ( struct image *image ) {
  145. ref_get ( &image->refcnt );
  146. return image;
  147. }
  148. /**
  149. * Decrement reference count on an image
  150. *
  151. * @v image Image
  152. */
  153. static inline void image_put ( struct image *image ) {
  154. ref_put ( &image->refcnt );
  155. }
  156. /**
  157. * Set image name
  158. *
  159. * @v image Image
  160. * @v name New image name
  161. * @ret rc Return status code
  162. */
  163. static inline int image_set_name ( struct image *image, const char *name ) {
  164. strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
  165. return 0;
  166. }
  167. #endif /* _GPXE_IMAGE_H */