1 <?php // -*- Mode: PHP; -*-
4 * Copyright (C) 2009 Marty Connor <mdc@etherboot.org>.
5 * Copyright (C) 2009 Entity Cyber, Inc.
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.
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.
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.
22 // Get utility functions and set globals
23 require_once "utils.php";
25 // Make sure at least $A (action) was supplied
26 if ( ! isset ( $_POST['A'] ) ) {
28 // Present user with form to customize build options
29 require_once "customize-flags.php";
33 // If user chose "Customize" option on form
34 } else if ( $_POST['A'] == "Customize" ) {
36 // Present user with form to customize build options
37 require_once "customize-flags.php";
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" ) {
45 // Note that this method of redirections discards all the
46 // configuration flags, which is intentional in this case.
49 header ( "Location: $dest" );
51 // This next "echo" should normally not be seen, because
52 // the "header" statement above should cause immediate
53 // redirection but just in case...
55 echo "Try this link: <a href=\"$dest\">$dest</a>";
60 // OK, we're going to try to use whatever options have been set
63 // Make sure at least $nic was supplied
64 if ( ! isset ( $_POST['nic'] ) ) {
65 die ( "No NIC supplied!" );
67 if ( isset ( $nics[$_POST['nic']] ) ) {
68 $nic = $nics[$_POST['nic']];
70 die ( "Invalid NIC \"${_POST['nic']}\" supplied!" );
74 $flags = get_flags ();
76 // Get requested format
77 $ofmt = isset ( $_POST['ofmt'] ) ? $_POST['ofmt'] : "";
78 $fmt_extension = isset ( $ofmts[$ofmt] ) ? $ofmts[$ofmt] : 'dsk';
80 // Handle some special cases
82 $pci_vendor_code = "";
83 $pci_device_code = "";
85 if ( $nic == 'undionly' && $fmt_extension == "pxe" ) {
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.
92 $fmt_extension = "kpxe";
94 } else if ( $fmt_extension == "rom" ) {
96 if ( ! isset ( $_POST['pci_vendor_code'] )
97 || ! isset ( $_POST['pci_device_code'] ) ) {
98 die ( "rom output format selected but PCI code(s) missing!" );
101 $pci_vendor_code = $_POST['pci_vendor_code'];
102 $pci_device_code = $_POST['pci_device_code'];
104 if ( $pci_vendor_code == ""
105 || $pci_device_code == "" ) {
106 die ( "rom output format selected but PCI code(s) missing!" );
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 );
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 );
119 // concatenate the pci codes to get the $nic part of the
121 $pci_codes = strtolower ( $pci_vendor_code . $pci_device_code );
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']}" );
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"
137 * remove temporary build directory
139 * @return bool true if removal is successful, false otherwise
141 function rm_build_dir ()
144 global $keep_build_dir;
146 if ( $keep_build_dir !== true ) {
147 rm_file_or_dir ( $build_dir );
151 // Arrange for the build directory to always be removed on exit.
153 $keep_build_dir = false;
154 register_shutdown_function ( 'rm_build_dir' );
156 // Make temporary copy of src directory
157 $build_dir = mktempcopy ( "$src_dir", "/tmp", "MDCROM" );
158 $config_dir = $build_dir . "/config";
160 // Write config files with supplied flags
161 write_ipxe_config_files ( $config_dir, $flags );
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";
169 if ( substr ( $embedded_script, 0, 5 ) != "#!ipxe" ) {
170 $embedded_script = "#!ipxe\n" . $embedded_script;
173 // iPXE 0.9.7 doesn't like '\r\n" in the shebang...
174 $embedded_script = str_replace ( "\r\n", "\n", $embedded_script );
176 write_file_from_string ( $emb_script_path, $embedded_script );
177 $emb_script_cmd = "EMBEDDED_IMAGE=${emb_script_path}";
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');
184 $gitversion = "GITVERSION=$gitversion";
187 $make_cmd = "make -C '$build_dir' '$make_target' $gitversion $emb_script_cmd 2>&1";
189 exec ( $make_cmd, $maketxt, $status );
191 // Uncomment the following section for debugging
195 echo "<h2>build.php:</h2>";
196 echo "<h3>Begin debugging output</h3>";
198 //echo "<h3>\$_POST variables</h3>";
199 //echo "<pre>"; var_dump ( $_POST ); echo "</pre>";
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>";
208 echo "<h3>Flags:</h3>";
209 show_flags ( $flags );
211 if ( $embedded_script != "" ) {
212 echo "<h3>Embedded script:</h3>";
213 echo "<blockquote>"."<pre>";
214 echo $embedded_script;
215 echo "</pre>"."</blockquote>";
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>" );
229 **/ // End debugging section
231 // Send ROM to browser (with extreme prejudice)
233 if ( $status == 0 ) {
235 $fp = fopen("${build_dir}/${make_target}", "rb" );
238 $len = filesize ( "${build_dir}/${make_target}" );
241 $buf = fread ( $fp, $len );
244 // Delete build directory as soon as it is not needed
247 $output_filename = preg_replace('/[^a-z0-9\+\.\-]/i', '', "ipxe-${version}-${nic}.${fmt_extension}");
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");
266 * If we reach this point, the build has failed, and we provide
267 * debugging information for a potential bug report
271 // Remove build directory
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>";
284 echo "<h3>Flags:</h3>";
285 show_flags ( $flags );
287 if ( $embedded_script != "" ) {
288 echo "<h3>Embedded script:</h3>";
289 echo "<blockquote>"."<pre>";
290 echo $embedded_script;
291 echo "</pre>"."</blockquote>";
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>";
300 echo "Please let us know that this happened, and paste the above output into your email message.<br>";
302 include_once $bottom_inc;