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.
29 #include <gpxe/image.h>
31 struct image_type script_image_type __image_type ( PROBE_NORMAL );
37 * @ret rc Return status code
39 static int script_exec ( struct image *image ) {
47 /* Temporarily de-register image, so that a "boot" command
48 * doesn't throw us into an execution loop. Hold a reference
49 * to avoid the image's being freed.
52 unregister_image ( image );
54 while ( offset < image->len ) {
56 /* Read up to cmdbuf bytes from script into buffer */
57 remaining = ( image->len - offset );
58 len = sizeof ( cmdbuf );
59 if ( len > remaining )
61 copy_from_user ( cmdbuf, image->data, offset, len );
63 /* Find end of line */
64 eol = memchr ( cmdbuf, '\n', sizeof ( cmdbuf ) );
66 eol = memchr ( cmdbuf, '\0', sizeof ( cmdbuf ) );
68 DBG ( "Script line too long (max %d bytes)\n",
74 /* Mark end of line and execute command */
76 DBG ( "$ %s\n", cmdbuf );
77 if ( ( rc = system ( cmdbuf ) ) != 0 ) {
78 DBG ( "Command \"%s\" exited with status %d\n",
83 /* Move to next line */
84 offset += ( ( eol - cmdbuf ) + 1 );
89 /* Re-register image and return */
90 register_image ( image );
96 * Load script into memory
99 * @ret rc Return status code
101 static int script_load ( struct image *image ) {
102 static const char magic[] = "#!gpxe\n";
103 char test[ sizeof ( magic ) - 1 ];
105 /* Check for magic signature */
106 copy_from_user ( test, image->data, 0, sizeof ( test ) );
107 if ( memcmp ( test, magic, sizeof ( test ) ) != 0 ) {
108 DBG ( "Invalid magic signature\n" );
112 /* This is a script */
113 image->type = &script_image_type;
115 /* We don't actually load it anywhere; we will pick the lines
116 * out of the image as we need them.
122 /** Script image type */
123 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {