[uri] Make URI parsing handle IPv6 addresses
[gpxe.git] / src / image / script.c
blob0835ecb5932795a6702fc8d68acf5d99548305e5
1 /*
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 );
21 /**
22 * @file
24 * gPXE scripts
28 #include <string.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <gpxe/image.h>
34 struct image_type script_image_type __image_type ( PROBE_NORMAL );
36 /**
37 * Execute script
39 * @v image Script
40 * @ret rc Return status code
42 static int script_exec ( struct image *image ) {
43 size_t offset = 0;
44 off_t eol;
45 size_t len;
46 int rc;
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 ) );
58 if ( eol < 0 )
59 eol = image->len;
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 );
67 cmdbuf[len] = '\0';
68 DBG ( "$ %s\n", cmdbuf );
69 if ( ( rc = system ( cmdbuf ) ) != 0 ) {
70 DBG ( "Command \"%s\" failed: %s\n",
71 cmdbuf, strerror ( rc ) );
72 goto done;
76 /* Move to next line */
77 offset += ( len + 1 );
80 rc = 0;
81 done:
82 /* Re-register image and return */
83 register_image ( image );
84 return rc;
87 /**
88 * Load script into memory
90 * @v image Script
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 */];
97 /* Sanity check */
98 if ( image->len < sizeof ( test ) ) {
99 DBG ( "Too short to be a script\n" );
100 return -ENOEXEC;
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" );
108 return -ENOEXEC;
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.
118 return 0;
121 /** Script image type */
122 struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
123 .name = "script",
124 .load = script_load,
125 .exec = script_exec,