|
@@ -0,0 +1,113 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
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.
|
|
8
|
+ *
|
|
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.
|
|
13
|
+ *
|
|
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.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+/**
|
|
20
|
+ * @file
|
|
21
|
+ *
|
|
22
|
+ * gPXE scripts
|
|
23
|
+ *
|
|
24
|
+ */
|
|
25
|
+
|
|
26
|
+#include <string.h>
|
|
27
|
+#include <stdlib.h>
|
|
28
|
+#include <errno.h>
|
|
29
|
+#include <gpxe/image.h>
|
|
30
|
+
|
|
31
|
+struct image_type script_image_type __image_type ( PROBE_NORMAL );
|
|
32
|
+
|
|
33
|
+/**
|
|
34
|
+ * Execute script
|
|
35
|
+ *
|
|
36
|
+ * @v image Script
|
|
37
|
+ * @ret rc Return status code
|
|
38
|
+ */
|
|
39
|
+static int script_exec ( struct image *image ) {
|
|
40
|
+ char cmdbuf[256];
|
|
41
|
+ size_t offset = 0;
|
|
42
|
+ size_t remaining;
|
|
43
|
+ size_t len;
|
|
44
|
+ char *eol;
|
|
45
|
+ int rc;
|
|
46
|
+
|
|
47
|
+ while ( offset < image->len ) {
|
|
48
|
+
|
|
49
|
+ /* Read up to cmdbuf bytes from script into buffer */
|
|
50
|
+ remaining = ( image->len - offset );
|
|
51
|
+ len = sizeof ( cmdbuf );
|
|
52
|
+ if ( len > remaining )
|
|
53
|
+ len = remaining;
|
|
54
|
+ copy_from_user ( cmdbuf, image->data, offset, len );
|
|
55
|
+
|
|
56
|
+ /* Find end of line */
|
|
57
|
+ eol = memchr ( cmdbuf, '\n', sizeof ( cmdbuf ) );
|
|
58
|
+ if ( ! eol )
|
|
59
|
+ eol = memchr ( cmdbuf, '\0', sizeof ( cmdbuf ) );
|
|
60
|
+ if ( ! eol ) {
|
|
61
|
+ DBG ( "Script line too long (max %d bytes)\n",
|
|
62
|
+ sizeof ( cmdbuf ) );
|
|
63
|
+ return -ENOEXEC;
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ /* Mark end of line and execute command */
|
|
67
|
+ *eol = '\0';
|
|
68
|
+ if ( ( rc = system ( cmdbuf ) ) != 0 ) {
|
|
69
|
+ DBG ( "Command \"%s\" exited with status %d\n",
|
|
70
|
+ cmdbuf, rc );
|
|
71
|
+ return rc;
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ /* Move to next line */
|
|
75
|
+ offset += ( ( eol - cmdbuf ) + 1 );
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ return 0;
|
|
79
|
+}
|
|
80
|
+
|
|
81
|
+/**
|
|
82
|
+ * Load script into memory
|
|
83
|
+ *
|
|
84
|
+ * @v image Script
|
|
85
|
+ * @ret rc Return status code
|
|
86
|
+ */
|
|
87
|
+static int script_load ( struct image *image ) {
|
|
88
|
+ static const char magic[] = "#!gpxe\n";
|
|
89
|
+ char test[ sizeof ( magic ) - 1 ];
|
|
90
|
+
|
|
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" );
|
|
95
|
+ return -ENOEXEC;
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ /* This is a script */
|
|
99
|
+ image->type = &script_image_type;
|
|
100
|
+
|
|
101
|
+ /* We don't actually load it anywhere; we will pick the lines
|
|
102
|
+ * out of the image as we need them.
|
|
103
|
+ */
|
|
104
|
+
|
|
105
|
+ return 0;
|
|
106
|
+}
|
|
107
|
+
|
|
108
|
+/** Script image type */
|
|
109
|
+struct image_type script_image_type __image_type ( PROBE_NORMAL ) = {
|
|
110
|
+ .name = "script",
|
|
111
|
+ .load = script_load,
|
|
112
|
+ .exec = script_exec,
|
|
113
|
+};
|