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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.
  48. */
  49. struct image *replacement;
  50. };
  51. /** Image is loaded */
  52. #define IMAGE_LOADED 0x0001
  53. /** An executable or loadable image type */
  54. struct image_type {
  55. /** Name of this image type */
  56. char *name;
  57. /**
  58. * Load image into memory
  59. *
  60. * @v image Executable/loadable image
  61. * @ret rc Return status code
  62. *
  63. * Load the image into memory at the correct location as
  64. * determined by the file format.
  65. *
  66. * If the file image is in the correct format, the method must
  67. * update @c image->type to point to its own type (unless @c
  68. * image->type is already set). This allows the autoloading
  69. * code to disambiguate between "this is not my image format"
  70. * and "there is something wrong with this image". In
  71. * particular, setting @c image->type and then returning an
  72. * error will cause image_autoload() to abort and return an
  73. * error, rather than continuing to the next image type.
  74. */
  75. int ( * load ) ( struct image *image );
  76. /**
  77. * Execute loaded image
  78. *
  79. * @v image Loaded image
  80. * @ret rc Return status code
  81. *
  82. * Note that the image may be invalidated by the act of
  83. * execution, i.e. an image is allowed to choose to unregister
  84. * (and so potentially free) itself.
  85. */
  86. int ( * exec ) ( struct image *image );
  87. };
  88. /**
  89. * Multiboot image probe priority
  90. *
  91. * Multiboot images are also valid executables in another format
  92. * (e.g. ELF), so we must perform the multiboot probe first.
  93. */
  94. #define PROBE_MULTIBOOT 01
  95. /**
  96. * Normal image probe priority
  97. */
  98. #define PROBE_NORMAL 02
  99. /**
  100. * PXE image probe priority
  101. *
  102. * PXE images have no signature checks, so will claim all image files.
  103. * They must therefore be tried last in the probe order list.
  104. */
  105. #define PROBE_PXE 03
  106. /** An executable or loadable image type */
  107. #define __image_type( probe_order ) \
  108. __table ( struct image_type, image_types, probe_order )
  109. extern struct list_head images;
  110. /** Iterate over all registered images */
  111. #define for_each_image( image ) \
  112. list_for_each_entry ( (image), &images, list )
  113. extern struct image * alloc_image ( void );
  114. extern int image_set_uri ( struct image *image, struct uri *uri );
  115. extern int image_set_cmdline ( struct image *image, const char *cmdline );
  116. extern int register_image ( struct image *image );
  117. extern void unregister_image ( struct image *image );
  118. extern void promote_image ( struct image *image );
  119. struct image * find_image ( const char *name );
  120. extern int image_load ( struct image *image );
  121. extern int image_autoload ( struct image *image );
  122. extern int image_exec ( struct image *image );
  123. extern int register_and_autoload_image ( struct image *image );
  124. extern int register_and_autoexec_image ( struct image *image );
  125. /**
  126. * Increment reference count on an image
  127. *
  128. * @v image Image
  129. * @ret image Image
  130. */
  131. static inline struct image * image_get ( struct image *image ) {
  132. ref_get ( &image->refcnt );
  133. return image;
  134. }
  135. /**
  136. * Decrement reference count on an image
  137. *
  138. * @v image Image
  139. */
  140. static inline void image_put ( struct image *image ) {
  141. ref_put ( &image->refcnt );
  142. }
  143. /**
  144. * Set image name
  145. *
  146. * @v image Image
  147. * @v name New image name
  148. * @ret rc Return status code
  149. */
  150. static inline int image_set_name ( struct image *image, const char *name ) {
  151. strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
  152. return 0;
  153. }
  154. #endif /* _GPXE_IMAGE_H */