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 while ( offset < image->len ) {
49 /* Read up to cmdbuf bytes from script into buffer */
50 remaining = ( image->len - offset );
51 len = sizeof ( cmdbuf );
52 if ( len > remaining )
54 copy_from_user ( cmdbuf, image->data, offset, len );
56 /* Find end of line */
57 eol = memchr ( cmdbuf, '\n', sizeof ( cmdbuf ) );
59 eol = memchr ( cmdbuf, '\0', sizeof ( cmdbuf ) );
61 DBG ( "Script line too long (max %d bytes)\n",
66 /* Mark end of line and execute command */
68 if ( ( rc = system ( cmdbuf ) ) != 0 ) {
69 DBG ( "Command \"%s\" exited with status %d\n",
74 /* Move to next line */
75 offset += ( ( eol - cmdbuf ) + 1 );
82 * Load script into memory
85 * @ret rc Return status code
87 static int script_load ( struct image *image ) {
88 static const char magic[] = "#!gpxe\n";
89 char test[ sizeof ( magic ) - 1 ];
91 /* Check for magic signature */
92 copy_from_user ( test, image->data, 0, sizeof ( test ) );
93 if ( memcmp ( test, magic, sizeof ( test ) ) != 0 ) {
94 DBG ( "Invalid magic signature\n" );
98 /* This is a script */
99 image->type = &script_image_type;
101 /* We don't actually load it anywhere; we will pick the lines
102 * out of the image as we need them.
108 /** Script image type */
109 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {