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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /** Iterate over all registered images */
  96. #define for_each_image( image ) \
  97. list_for_each_entry ( (image), &images, list )
  98. /**
  99. * Test for existence of images
  100. *
  101. * @ret existence Some images exist
  102. */
  103. static inline int have_images ( void ) {
  104. return ( ! list_empty ( &images ) );
  105. }
  106. extern struct image * alloc_image ( void );
  107. extern void image_set_uri ( struct image *image, struct uri *uri );
  108. extern int image_set_cmdline ( struct image *image, const char *cmdline );
  109. extern int register_image ( struct image *image );
  110. extern void unregister_image ( struct image *image );
  111. struct image * find_image ( const char *name );
  112. extern int image_probe ( struct image *image );
  113. extern int image_exec ( struct image *image );
  114. extern int image_select ( struct image *image );
  115. extern struct image * image_find_selected ( void );
  116. extern int register_and_select_image ( struct image *image );
  117. extern int register_and_boot_image ( struct image *image );
  118. /**
  119. * Increment reference count on an image
  120. *
  121. * @v image Image
  122. * @ret image Image
  123. */
  124. static inline struct image * image_get ( struct image *image ) {
  125. ref_get ( &image->refcnt );
  126. return image;
  127. }
  128. /**
  129. * Decrement reference count on an image
  130. *
  131. * @v image Image
  132. */
  133. static inline void image_put ( struct image *image ) {
  134. ref_put ( &image->refcnt );
  135. }
  136. /**
  137. * Set image name
  138. *
  139. * @v image Image
  140. * @v name New image name
  141. * @ret rc Return status code
  142. */
  143. static inline int image_set_name ( struct image *image, const char *name ) {
  144. strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) );
  145. return 0;
  146. }
  147. #endif /* _IPXE_IMAGE_H */