You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

build.php 9.3KB

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