Browse Source

Use systematic names for XXX_IMAGE.

Add scripts as an image format (since it's trivial to do).
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
67aedf34fa
3 changed files with 124 additions and 7 deletions
  1. 3
    2
      src/config.h
  2. 8
    5
      src/core/config.c
  3. 113
    0
      src/image/script.c

+ 3
- 2
src/config.h View File

@@ -102,11 +102,12 @@
102 102
 #undef	ELF64_IMAGE		/* ELF64 image support */
103 103
 #undef	ELF_IMAGE		/* ELF image support */
104 104
 #undef	COFF_IMAGE		/* COFF image support */
105
-#undef	IMAGE_FREEBSD		/* FreeBSD kernel image support */
106
-#undef	IMAGE_MULTIBOOT		/* MultiBoot image support */
105
+#undef	FREEBSD_IMAGE		/* FreeBSD kernel image support */
106
+#define	MULTIBOOT_IMAGE		/* MultiBoot image support */
107 107
 #undef	AOUT_IMAGE		/* a.out image support */
108 108
 #undef	WINCE_IMAGE		/* WinCE image support */
109 109
 #undef	PXE_IMAGE		/* PXE image support */
110
+#define SCRIPT_IMAGE		/* gPXE script image support */
110 111
 
111 112
 /* @END general.h */ 
112 113
 

+ 8
- 5
src/core/config.c View File

@@ -122,21 +122,24 @@ REQUIRE_OBJECT ( elf );
122 122
 #ifdef COFF_IMAGE
123 123
 REQUIRE_OBJECT ( coff );
124 124
 #endif
125
-#ifdef IMAGE_FREEBSD
125
+#ifdef FREEBSD_IMAGE
126 126
 REQUIRE_OBJECT ( freebsd );
127 127
 #endif
128
-#ifdef	IMAGE_MULTIBOOT
128
+#ifdef MULTIBOOT_IMAGE
129 129
 REQUIRE_OBJECT ( multiboot );
130 130
 #endif
131
-#ifdef	AOUT_IMAGE
131
+#ifdef AOUT_IMAGE
132 132
 REQUIRE_OBJECT ( aout );
133 133
 #endif
134
-#ifdef	WINCE_IMAGE
134
+#ifdef WINCE_IMAGE
135 135
 REQUIRE_OBJECT ( wince );
136 136
 #endif
137
-#ifdef	PXE_IMAGE
137
+#ifdef PXE_IMAGE
138 138
 REQUIRE_OBJECT ( pxe );
139 139
 #endif
140
+#ifdef SCRIPT_IMAGE
141
+REQUIRE_OBJECT ( script );
142
+#endif
140 143
 
141 144
 /*
142 145
  * Drag in all requested commands

+ 113
- 0
src/image/script.c View File

@@ -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
+};

Loading…
Cancel
Save