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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #ifndef _IPXE_IMAGE_H
  2. #define _IPXE_IMAGE_H
  3. /**
  4. * @file
  5. *
  6. * Executable images
  7. *
  8. */
  9. FILE_LICENCE ( GPL2_OR_LATER );
  10. #include <ipxe/tables.h>
  11. #include <ipxe/list.h>
  12. #include <ipxe/uaccess.h>
  13. #include <ipxe/refcnt.h>
  14. struct uri;
  15. struct image_type;
  16. /** An executable 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. /** Replacement image
  37. *
  38. * An image wishing to replace itself with another image (in a
  39. * style similar to a Unix exec() call) should return from its
  40. * exec() method with the replacement image set to point to
  41. * the new image.
  42. *
  43. * If an image unregisters itself as a result of being
  44. * executed, it must make sure that its replacement image (if
  45. * any) is registered, otherwise the replacement is likely to
  46. * be freed before it can be executed.
  47. */
  48. struct image *replacement;
  49. };
  50. /** Image is selected for execution */
  51. #define IMAGE_SELECTED 0x0001
  52. /** An executable image type */
  53. struct image_type {
  54. /** Name of this image type */
  55. char *name;
  56. /** Probe image
  57. *
  58. * @v image Executable image
  59. * @ret rc Return status code
  60. *
  61. * Return success if the image is of this image type.
  62. */
  63. int ( * probe ) ( struct image *image );
  64. /**
  65. * Execute image
  66. *
  67. * @v image Executable image
  68. * @ret rc Return status code
  69. */
  70. int ( * exec ) ( struct image *image );
  71. };
  72. /**
  73. * Multiboot image probe priority
  74. *
  75. * Multiboot images are also valid executables in another format
  76. * (e.g. ELF), so we must perform the multiboot probe first.
  77. */
  78. #define PROBE_MULTIBOOT 01
  79. /**
  80. * Normal image probe priority
  81. */
  82. #define PROBE_NORMAL 02
  83. /**
  84. * PXE image probe priority
  85. *
  86. * PXE images have no signature checks, so will claim all image files.
  87. * They must therefore be tried last in the probe order list.
  88. */
  89. #define PROBE_PXE 03
  90. /** Executable image type table */
  91. #define IMAGE_TYPES __table ( struct image_type, "image_types" )
  92. /** An executable image type */
  93. #define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order )
  94. extern struct list_head images;
  95. extern struct image *current_image;
  96. /** Iterate over all registered images */
  97. #define for_each_image( image ) \
  98. list_for_each_entry ( (image), &images, list )
  99. /**
  100. * Test for existence of images
  101. *
  102. * @ret existence Some images exist
  103. */
  104. static inline int have_images ( void ) {
  105. return ( ! list_empty ( &images ) );
  106. }
  107. /**
  108. * Retrieve first image
  109. *
  110. * @ret image Image, or NULL
  111. */
  112. static inline struct image * first_image ( void ) {
  113. return list_first_entry ( &images, struct image, list );
  114. }
  115. extern struct image * alloc_image ( void );
  116. extern void image_set_uri ( struct image *image, struct uri *uri );
  117. extern int image_set_cmdline ( struct image *image, const char *cmdline );
  118. extern int register_image ( struct image *image );
  119. extern void unregister_image ( struct image *image );
  120. struct image * find_image ( const char *name );
  121. extern int image_probe ( struct image *image );
  122. extern int image_exec ( struct image *image );
  123. extern int image_select ( struct image *image );
  124. extern struct image * image_find_selected ( void );
  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 /* _IPXE_IMAGE_H */