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.8KB

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