Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / contrib / rom-o-matic / build.php
1 <?php // -*- Mode: PHP; -*-
2
3 /**
4  * Copyright (C) 2009 Marty Connor <mdc@etherboot.org>.
5  * Copyright (C) 2009 Entity Cyber, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 // Get utility functions and set globals
23 require_once "utils.php";
24
25 // Make sure at least $A (action)  was supplied
26 if ( ! isset ( $_POST['A'] ) ) {
27
28     // Present user with form to customize build options
29     require_once "customize-flags.php";
30
31     exit ();
32
33 // If user chose "Customize" option on form
34 } else if ( $_POST['A'] == "Customize" ) {
35
36     // Present user with form to customize build options
37     require_once "customize-flags.php";
38
39     exit ();
40
41 // The following conditional includes all other cases except "Get Image"
42 // particularly the explicit ($A == "Start Over") case
43 } else if ( $_POST['A'] != "Get Image" ) {
44
45     // Note that this method of redirections discards all the
46     // configuration flags, which is intentional in this case.
47
48     $dest = curDirURL ();
49     header ( "Location: $dest" );
50
51     // This next "echo" should normally not be seen, because
52     // the "header" statement above should cause immediate
53     // redirection but just in case...
54
55     echo "Try this link: <a href=\"$dest\">$dest</a>";
56
57     exit ();
58 }
59
60 // OK, we're going to try to use whatever options have been set
61 // to build an image.
62
63 // Make sure at least $nic was supplied
64 if ( ! isset ( $_POST['nic'] ) ) {
65     die ( "No NIC supplied!" );
66 }
67 if ( isset ( $nics[$_POST['nic']] ) ) {
68     $nic = $nics[$_POST['nic']];
69 } else {
70     die ( "Invalid NIC \"${_POST['nic']}\" supplied!" );
71 }
72
73 // Fetch flags
74 $flags = get_flags ();
75
76 // Get requested format
77 $ofmt = isset ( $_POST['ofmt'] ) ? $_POST['ofmt'] : "";
78 $fmt_extension = isset ( $ofmts[$ofmt] ) ? $ofmts[$ofmt] : 'dsk';
79
80 // Handle some special cases
81
82 $pci_vendor_code = "";
83 $pci_device_code = "";
84
85 if ( $nic == 'undionly' && $fmt_extension == "pxe" ) {
86
87     // undionly.pxe can't work because it unloads the PXE stack
88     // that it needs to communicate with, so we set the extension
89     // to .kpxe, which has a chance of working. The extension
90     // .kkpxe is another option.
91
92     $fmt_extension = "kpxe";
93
94 } else if ( $fmt_extension == "rom" ) {
95
96     if ( ! isset ( $_POST['pci_vendor_code'] )
97                  || ! isset ( $_POST['pci_device_code'] ) ) {
98                 die ( "rom output format selected but PCI code(s) missing!" );
99         }
100
101         $pci_vendor_code = $_POST['pci_vendor_code'];
102         $pci_device_code = $_POST['pci_device_code'];
103
104     if ( $pci_vendor_code == ""
105                  || $pci_device_code == "" ) {
106                 die ( "rom output format selected but PCI code(s) missing!" );
107         }
108
109         // Try to be forgiving of 0xAAAA format
110         if ( strtolower ( substr ( $pci_vendor_code, 0, 2 ) ) == "0x"
111                  && strlen ( $pci_vendor_code ) == 6 ) {
112                 $pci_vendor_code = substr ( $pci_vendor_code, 2, 4 );
113         }
114         if ( strtolower ( substr ( $pci_device_code, 0, 2 ) ) == "0x"
115                  && strlen ( $pci_device_code ) == 6 ) {
116                 $pci_device_code = substr ( $pci_device_code, 2, 4 );
117         }
118
119     // concatenate the pci codes to get the $nic part of the
120     // Make target
121     $pci_codes = strtolower (  $pci_vendor_code . $pci_device_code );
122
123     $nic = $pci_codes;
124     if ( ! isset ( $roms[$pci_codes] ) ) {
125         die (   "Sorry, no network driver supports PCI codes<br>"
126               . "${_POST['pci_vendor_code']}:"
127               . "${_POST['pci_device_code']}" );
128     }
129 } else if ( $fmt_extension != "rom"
130             && ( $pci_vendor_code != "" || $pci_device_code != "" ) ) {
131     die (   "'$fmt_extension' format was selected but PCI IDs were"
132           . " also entered.<br>Did you mean to select 'rom' output format"
133           . " instead?" );
134 }
135
136 /**
137  * remove temporary build directory
138  *
139  * @return bool true if removal is successful, false otherwise
140  */
141 function rm_build_dir ()
142 {
143     global $build_dir;
144     global $keep_build_dir;
145
146     if ( $keep_build_dir !== true ) {
147         rm_file_or_dir ( $build_dir );
148     }
149 }
150
151 // Arrange for the build directory to always be removed on exit.
152 $build_dir = "";
153 $keep_build_dir = false;
154 register_shutdown_function ( 'rm_build_dir' );
155
156 // Make temporary copy of src directory
157 $build_dir = mktempcopy ( "$src_dir", "/tmp", "MDCROM" );
158 $config_dir = $build_dir . "/config";
159
160 // Write config files with supplied flags
161 write_ipxe_config_files ( $config_dir, $flags );
162
163 // Handle a possible embedded script
164 $emb_script_cmd = "";
165 $embedded_script = isset ( $_POST['embedded_script'] ) ? $_POST['embedded_script'] : "";
166 if ( $embedded_script != "" ) {
167     $emb_script_path = "$build_dir" . "/script0.ipxe";
168
169         if ( substr ( $embedded_script, 0, 5 ) != "#!ipxe" ) {
170                 $embedded_script = "#!ipxe\n" . $embedded_script;
171         }
172
173     // iPXE 0.9.7 doesn't like '\r\n" in the shebang...
174     $embedded_script = str_replace ( "\r\n", "\n", $embedded_script );
175
176     write_file_from_string ( $emb_script_path, $embedded_script );
177     $emb_script_cmd = "EMBEDDED_IMAGE=${emb_script_path}";
178 }
179
180 // Make the requested image.  $status is set to 0 on success
181 $make_target = "bin/${nic}.${fmt_extension}";
182 $gitversion = exec('git describe --always --abbrev=1 --match "" 2>/dev/null');
183 if ($gitversion) {
184         $gitversion = "GITVERSION=$gitversion";
185 }
186
187 $make_cmd = "make -C '$build_dir' '$make_target' $gitversion $emb_script_cmd 2>&1";
188
189 exec ( $make_cmd, $maketxt, $status );
190
191 // Uncomment the following section for debugging
192
193 /**
194
195 echo "<h2>build.php:</h2>";
196 echo "<h3>Begin debugging output</h3>";
197
198 //echo "<h3>\$_POST variables</h3>";
199 //echo "<pre>"; var_dump ( $_POST ); echo "</pre>";
200
201 echo "<h3>Build options:</h3>";
202 echo "<strong>Build directory is:</strong> $build_dir" . "<br><br>";
203 echo "\$_POST['ofmt'] = " . "\"${_POST['ofmt']}\"" . "<br>";
204 echo "\$_POST['nic'] = " . "\"${_POST['nic']}\"" .  "<br>";
205 echo "\$_POST['pci_vendor_code'] = " . "\"${_POST['pci_vendor_code']}\"" . "<br>";
206 echo "\$_POST['pci_device_code'] = " . "\"${_POST['pci_device_code']}\"" . "<br>";
207
208 echo "<h3>Flags:</h3>";
209 show_flags ( $flags );
210
211 if ( $embedded_script != "" ) {
212     echo "<h3>Embedded script:</h3>";
213     echo "<blockquote>"."<pre>";
214     echo $embedded_script;
215     echo "</pre>"."</blockquote>";
216 }
217
218 echo "<h3>Make output:</h3>";
219 echo "Make command: " . $make_cmd . "<br>";
220 echo "Build status = <? echo $status ?>" . "<br>";
221 echo "<blockquote>"."<pre>";
222 echo htmlentities ( implode ("\n", $maketxt ) );
223 echo "</pre>"."</blockquote>";
224 // Uncomment the next line if you want to keep the
225 // build directory around for inspection after building.
226 $keep_build_dir = true;
227 die ( "<h3>End debugging output</h3>" );
228
229 **/ //   End debugging section
230
231 // Send ROM to browser (with extreme prejudice)
232
233 if ( $status == 0 ) {
234
235     $fp = fopen("${build_dir}/${make_target}", "rb" );
236     if ( $fp > 0 ) {
237
238         $len = filesize ( "${build_dir}/${make_target}" );
239         if ( $len > 0 ) {
240
241             $buf = fread ( $fp, $len );
242             fclose ( $fp );
243
244             // Delete build directory as soon as it is not needed
245             rm_build_dir ();
246
247             $output_filename = preg_replace('/[^a-z0-9\+\.\-]/i', '', "ipxe-${version}-${nic}.${fmt_extension}");
248
249             // Try to force IE to handle downloading right.
250             Header ( "Cache-control: private");
251             Header ( "Content-Type: application/x-octet-stream; " .
252                      "name=$output_filename");
253             Header ( "Content-Disposition: attachment; " .
254                      "Filename=$output_filename");
255             Header ( "Content-Location: $output_filename");
256             Header ( "Content-Length: $len");
257
258             echo $buf;
259
260             exit ();
261         }
262     }
263 }
264
265 /*
266  * If we reach this point, the build has failed, and we provide
267  * debugging information for a potential bug report
268  *
269  */
270
271 // Remove build directory
272 rm_build_dir ();
273
274 // Announce failure if $status from make was non-zero
275 echo "<h2>Build failed.  Status = " . $status . "</h2>";
276 echo "<h2>build.php:</h2>";
277 echo "<h3>Build options:</h3>";
278 echo "<strong>Build directory is:</strong> $build_dir" . "<br><br>";
279 echo "\$_POST['ofmt'] = " . "\"${_POST['ofmt']}\"" . "<br>";
280 echo "\$_POST['nic'] = " . "\"${_POST['nic']}\"" .  "<br>";
281 echo "\$_POST['pci_vendor_code'] = " . "\"${_POST['pci_vendor_code']}\"" . "<br>";
282 echo "\$_POST['pci_device_code'] = " . "\"${_POST['pci_device_code']}\"" . "<br>";
283
284 echo "<h3>Flags:</h3>";
285 show_flags ( $flags );
286
287 if ( $embedded_script != "" ) {
288     echo "<h3>Embedded script:</h3>";
289     echo "<blockquote>"."<pre>";
290     echo $embedded_script;
291     echo "</pre>"."</blockquote>";
292 }
293
294 echo "<h3>Make output:</h3>";
295 echo "Make command: " . $make_cmd . "<br>";
296 echo "<blockquote>"."<pre>";
297 echo htmlentities ( implode ("\n", $maketxt ) );
298 echo "</pre>"."</blockquote>";
299
300 echo "Please let us know that this happened, and paste the above output into your email message.<br>";
301
302 include_once $bottom_inc;
303
304 // For emacs:
305 //  Local variables:
306 //  c-basic-offset: 4
307 //  c-indent-level: 4
308 //  tab-width: 4
309 //  End:
310
311 ?>