00001 #ifndef _GPXE_IMAGE_H 00002 #define _GPXE_IMAGE_H 00003 00004 /** 00005 * @file 00006 * 00007 * Executable/loadable images 00008 * 00009 */ 00010 00011 FILE_LICENCE ( GPL2_OR_LATER ); 00012 00013 #include <gpxe/tables.h> 00014 #include <gpxe/list.h> 00015 #include <gpxe/uaccess.h> 00016 #include <gpxe/refcnt.h> 00017 00018 struct uri; 00019 struct image_type; 00020 00021 /** An executable or loadable image */ 00022 struct image { 00023 /** Reference count */ 00024 struct refcnt refcnt; 00025 00026 /** List of registered images */ 00027 struct list_head list; 00028 00029 /** URI of image */ 00030 struct uri *uri; 00031 /** Name */ 00032 char name[16]; 00033 /** Flags */ 00034 unsigned int flags; 00035 00036 /** Command line to pass to image */ 00037 char *cmdline; 00038 /** Raw file image */ 00039 userptr_t data; 00040 /** Length of raw file image */ 00041 size_t len; 00042 00043 /** Image type, if known */ 00044 struct image_type *type; 00045 /** Image type private data */ 00046 union { 00047 physaddr_t phys; 00048 userptr_t user; 00049 unsigned long ul; 00050 } priv; 00051 00052 /** Replacement image 00053 * 00054 * An image wishing to replace itself with another image (in a 00055 * style similar to a Unix exec() call) should return from its 00056 * exec() method with the replacement image set to point to 00057 * the new image. The new image must already be in a suitable 00058 * state for execution (i.e. loaded). 00059 * 00060 * If an image unregisters itself as a result of being 00061 * executed, it must make sure that its replacement image (if 00062 * any) is registered, otherwise the replacement is likely to 00063 * be freed before it can be executed. 00064 */ 00065 struct image *replacement; 00066 }; 00067 00068 /** Image is loaded */ 00069 #define IMAGE_LOADED 0x0001 00070 00071 /** An executable or loadable image type */ 00072 struct image_type { 00073 /** Name of this image type */ 00074 char *name; 00075 /** 00076 * Load image into memory 00077 * 00078 * @v image Executable/loadable image 00079 * @ret rc Return status code 00080 * 00081 * Load the image into memory at the correct location as 00082 * determined by the file format. 00083 * 00084 * If the file image is in the correct format, the method must 00085 * update @c image->type to point to its own type (unless @c 00086 * image->type is already set). This allows the autoloading 00087 * code to disambiguate between "this is not my image format" 00088 * and "there is something wrong with this image". In 00089 * particular, setting @c image->type and then returning an 00090 * error will cause image_autoload() to abort and return an 00091 * error, rather than continuing to the next image type. 00092 */ 00093 int ( * load ) ( struct image *image ); 00094 /** 00095 * Execute loaded image 00096 * 00097 * @v image Loaded image 00098 * @ret rc Return status code 00099 * 00100 * Note that the image may be invalidated by the act of 00101 * execution, i.e. an image is allowed to choose to unregister 00102 * (and so potentially free) itself. 00103 */ 00104 int ( * exec ) ( struct image *image ); 00105 }; 00106 00107 /** 00108 * Multiboot image probe priority 00109 * 00110 * Multiboot images are also valid executables in another format 00111 * (e.g. ELF), so we must perform the multiboot probe first. 00112 */ 00113 #define PROBE_MULTIBOOT 01 00114 00115 /** 00116 * Normal image probe priority 00117 */ 00118 #define PROBE_NORMAL 02 00119 00120 /** 00121 * PXE image probe priority 00122 * 00123 * PXE images have no signature checks, so will claim all image files. 00124 * They must therefore be tried last in the probe order list. 00125 */ 00126 #define PROBE_PXE 03 00127 00128 /** Executable or loadable image type table */ 00129 #define IMAGE_TYPES __table ( struct image_type, "image_types" ) 00130 00131 /** An executable or loadable image type */ 00132 #define __image_type( probe_order ) __table_entry ( IMAGE_TYPES, probe_order ) 00133 00134 extern struct list_head images; 00135 00136 /** Iterate over all registered images */ 00137 #define for_each_image( image ) \ 00138 list_for_each_entry ( (image), &images, list ) 00139 00140 /** 00141 * Test for existence of images 00142 * 00143 * @ret existence Some images exist 00144 */ 00145 static inline int have_images ( void ) { 00146 return ( ! list_empty ( &images ) ); 00147 } 00148 00149 extern struct image * alloc_image ( void ); 00150 extern int image_set_uri ( struct image *image, struct uri *uri ); 00151 extern int image_set_cmdline ( struct image *image, const char *cmdline ); 00152 extern int register_image ( struct image *image ); 00153 extern void unregister_image ( struct image *image ); 00154 extern void promote_image ( struct image *image ); 00155 struct image * find_image ( const char *name ); 00156 extern int image_load ( struct image *image ); 00157 extern int image_autoload ( struct image *image ); 00158 extern int image_exec ( struct image *image ); 00159 extern int register_and_autoload_image ( struct image *image ); 00160 extern int register_and_autoexec_image ( struct image *image ); 00161 00162 /** 00163 * Increment reference count on an image 00164 * 00165 * @v image Image 00166 * @ret image Image 00167 */ 00168 static inline struct image * image_get ( struct image *image ) { 00169 ref_get ( &image->refcnt ); 00170 return image; 00171 } 00172 00173 /** 00174 * Decrement reference count on an image 00175 * 00176 * @v image Image 00177 */ 00178 static inline void image_put ( struct image *image ) { 00179 ref_put ( &image->refcnt ); 00180 } 00181 00182 /** 00183 * Set image name 00184 * 00185 * @v image Image 00186 * @v name New image name 00187 * @ret rc Return status code 00188 */ 00189 static inline int image_set_name ( struct image *image, const char *name ) { 00190 strncpy ( image->name, name, ( sizeof ( image->name ) - 1 ) ); 00191 return 0; 00192 } 00193 00194 #endif /* _GPXE_IMAGE_H */
1.5.7.1