2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER );
32 #include <gpxe/image.h>
34 struct image_type script_image_type __image_type ( PROBE_NORMAL );
40 * @ret rc Return status code
42 static int script_exec ( struct image *image ) {
48 /* Temporarily de-register image, so that a "boot" command
49 * doesn't throw us into an execution loop.
51 unregister_image ( image );
53 while ( offset < image->len ) {
55 /* Find length of next line, excluding any terminating '\n' */
56 eol = memchr_user ( image->data, offset, '\n',
57 ( image->len - offset ) );
60 len = ( eol - offset );
62 /* Copy line, terminate with NUL, and execute command */
64 char cmdbuf[ len + 1 ];
66 copy_from_user ( cmdbuf, image->data, offset, len );
68 DBG ( "$ %s\n", cmdbuf );
69 if ( ( rc = system ( cmdbuf ) ) != 0 ) {
70 DBG ( "Command \"%s\" failed: %s\n",
71 cmdbuf, strerror ( rc ) );
76 /* Move to next line */
77 offset += ( len + 1 );
82 /* Re-register image and return */
83 register_image ( image );
88 * Load script into memory
91 * @ret rc Return status code
93 static int script_load ( struct image *image ) {
94 static const char magic[] = "#!gpxe";
95 char test[ sizeof ( magic ) - 1 /* NUL */ + 1 /* terminating space */];
98 if ( image->len < sizeof ( test ) ) {
99 DBG ( "Too short to be a script\n" );
103 /* Check for magic signature */
104 copy_from_user ( test, image->data, 0, sizeof ( test ) );
105 if ( ( memcmp ( test, magic, ( sizeof ( test ) - 1 ) ) != 0 ) ||
106 ! isspace ( test[ sizeof ( test ) - 1 ] ) ) {
107 DBG ( "Invalid magic signature\n" );
111 /* This is a script */
112 image->type = &script_image_type;
114 /* We don't actually load it anywhere; we will pick the lines
115 * out of the image as we need them.
121 /** Script image type */
122 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {