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 memset ( cmdbuf, 0, sizeof ( cmdbuf ) );
62 copy_from_user ( cmdbuf, image->data, offset, len );
64 /* Find end of line */
65 eol = memchr ( cmdbuf, '\n', sizeof ( cmdbuf ) );
67 eol = memchr ( cmdbuf, '\0', sizeof ( cmdbuf ) );
69 DBG ( "Script line too long (max %d bytes)\n",
75 /* Mark end of line and execute command */
77 DBG ( "$ %s\n", cmdbuf );
78 if ( ( rc = system ( cmdbuf ) ) != 0 ) {
79 DBG ( "Command \"%s\" failed: %s\n",
80 cmdbuf, strerror ( rc ) );
84 /* Move to next line */
85 offset += ( ( eol - cmdbuf ) + 1 );
90 /* Re-register image and return */
91 register_image ( image );
97 * Load script into memory
100 * @ret rc Return status code
102 static int script_load ( struct image *image ) {
103 static const char magic[] = "#!gpxe\n";
104 char test[ sizeof ( magic ) - 1 ];
106 /* Check for magic signature */
107 copy_from_user ( test, image->data, 0, sizeof ( test ) );
108 if ( memcmp ( test, magic, sizeof ( test ) ) != 0 ) {
109 DBG ( "Invalid magic signature\n" );
113 /* This is a script */
114 image->type = &script_image_type;
116 /* We don't actually load it anywhere; we will pick the lines
117 * out of the image as we need them.
123 /** Script image type */
124 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {