script.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <string.h>
00029 #include <stdlib.h>
00030 #include <ctype.h>
00031 #include <errno.h>
00032 #include <gpxe/image.h>
00033
00034 struct image_type script_image_type __image_type ( PROBE_NORMAL );
00035
00036
00037
00038
00039
00040
00041
00042 static int script_exec ( struct image *image ) {
00043 size_t offset = 0;
00044 off_t eol;
00045 size_t len;
00046 int rc;
00047
00048
00049
00050
00051 unregister_image ( image );
00052
00053 while ( offset < image->len ) {
00054
00055
00056 eol = memchr_user ( image->data, offset, '\n',
00057 ( image->len - offset ) );
00058 if ( eol < 0 )
00059 eol = image->len;
00060 len = ( eol - offset );
00061
00062
00063 {
00064 char cmdbuf[ len + 1 ];
00065
00066 copy_from_user ( cmdbuf, image->data, offset, len );
00067 cmdbuf[len] = '\0';
00068 DBG ( "$ %s\n", cmdbuf );
00069 if ( ( rc = system ( cmdbuf ) ) != 0 ) {
00070 DBG ( "Command \"%s\" failed: %s\n",
00071 cmdbuf, strerror ( rc ) );
00072 goto done;
00073 }
00074 }
00075
00076
00077 offset += ( len + 1 );
00078 }
00079
00080 rc = 0;
00081 done:
00082
00083 register_image ( image );
00084 return rc;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 static int script_load ( struct image *image ) {
00094 static const char magic[] = "#!gpxe";
00095 char test[ sizeof ( magic ) - 1 + 1 ];
00096
00097
00098 if ( image->len < sizeof ( test ) ) {
00099 DBG ( "Too short to be a script\n" );
00100 return -ENOEXEC;
00101 }
00102
00103
00104 copy_from_user ( test, image->data, 0, sizeof ( test ) );
00105 if ( ( memcmp ( test, magic, ( sizeof ( test ) - 1 ) ) != 0 ) ||
00106 ! isspace ( test[ sizeof ( test ) - 1 ] ) ) {
00107 DBG ( "Invalid magic signature\n" );
00108 return -ENOEXEC;
00109 }
00110
00111
00112 image->type = &script_image_type;
00113
00114
00115
00116
00117
00118 return 0;
00119 }
00120
00121
00122 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
00123 .name = "script",
00124 .load = script_load,
00125 .exec = script_exec,
00126 };