選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

build.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. $make_cmd = "make -C '$build_dir' '$make_target' $emb_script_cmd 2>&1";
  143. exec ( $make_cmd, $maketxt, $status );
  144. // Uncomment the following section for debugging
  145. /**
  146. echo "<h2>build.php:</h2>";
  147. echo "<h3>Begin debugging output</h3>";
  148. //echo "<h3>\$_POST variables</h3>";
  149. //echo "<pre>"; var_dump ( $_POST ); echo "</pre>";
  150. echo "<h3>Build options:</h3>";
  151. echo "<strong>Build directory is:</strong> $build_dir" . "<br><br>";
  152. echo "\$_POST['ofmt'] = " . "\"${_POST['ofmt']}\"" . "<br>";
  153. echo "\$_POST['nic'] = " . "\"${_POST['nic']}\"" . "<br>";
  154. echo "\$_POST['pci_vendor_code'] = " . "\"${_POST['pci_vendor_code']}\"" . "<br>";
  155. echo "\$_POST['pci_device_code'] = " . "\"${_POST['pci_device_code']}\"" . "<br>";
  156. echo "<h3>Flags:</h3>";
  157. show_flags ( $flags );
  158. if ( $embedded_script != "" ) {
  159. echo "<h3>Embedded script:</h3>";
  160. echo "<blockquote>"."<pre>";
  161. echo $embedded_script;
  162. echo "</pre>"."</blockquote>";
  163. }
  164. echo "<h3>Make output:</h3>";
  165. echo "Make command: " . $make_cmd . "<br>";
  166. echo "Build status = <? echo $status ?>" . "<br>";
  167. echo "<blockquote>"."<pre>";
  168. echo htmlentities ( implode ("\n", $maketxt ) );
  169. echo "</pre>"."</blockquote>";
  170. // Uncomment the next line if you want to keep the
  171. // build directory around for inspection after building.
  172. $keep_build_dir = true;
  173. die ( "<h3>End debugging output</h3>" );
  174. **/ // End debugging section
  175. // Send ROM to browser (with extreme prejudice)
  176. if ( $status == 0 ) {
  177. $fp = fopen("${build_dir}/${make_target}", "rb" );
  178. if ( $fp > 0 ) {
  179. $len = filesize ( "${build_dir}/${make_target}" );
  180. if ( $len > 0 ) {
  181. $buf = fread ( $fp, $len );
  182. fclose ( $fp );
  183. // Delete build directory as soon as it is not needed
  184. rm_build_dir ();
  185. $output_filename = "ipxe-${version}-${nic}.${fmt_extension}";
  186. // Try to force IE to handle downloading right.
  187. Header ( "Cache-control: private");
  188. Header ( "Content-Type: application/x-octet-stream; " .
  189. "name=$output_filename");
  190. Header ( "Content-Disposition: attachment; " .
  191. "Filename=$output_filename");
  192. Header ( "Content-Location: $output_filename");
  193. Header ( "Content-Length: $len");
  194. echo $buf;
  195. exit ();
  196. }
  197. }
  198. }
  199. /*
  200. * If we reach this point, the build has failed, and we provide
  201. * debugging information for a potential bug report
  202. *
  203. */
  204. // Remove build directory
  205. rm_build_dir ();
  206. // Announce failure if $status from make was non-zero
  207. echo "<h2>Build failed. Status = " . $status . "</h2>";
  208. echo "<h2>build.php:</h2>";
  209. echo "<h3>Build options:</h3>";
  210. echo "<strong>Build directory is:</strong> $build_dir" . "<br><br>";
  211. echo "\$_POST['ofmt'] = " . "\"${_POST['ofmt']}\"" . "<br>";
  212. echo "\$_POST['nic'] = " . "\"${_POST['nic']}\"" . "<br>";
  213. echo "\$_POST['pci_vendor_code'] = " . "\"${_POST['pci_vendor_code']}\"" . "<br>";
  214. echo "\$_POST['pci_device_code'] = " . "\"${_POST['pci_device_code']}\"" . "<br>";
  215. echo "<h3>Flags:</h3>";
  216. show_flags ( $flags );
  217. if ( $embedded_script != "" ) {
  218. echo "<h3>Embedded script:</h3>";
  219. echo "<blockquote>"."<pre>";
  220. echo $embedded_script;
  221. echo "</pre>"."</blockquote>";
  222. }
  223. echo "<h3>Make output:</h3>";
  224. echo "Make command: " . $make_cmd . "<br>";
  225. echo "<blockquote>"."<pre>";
  226. echo htmlentities ( implode ("\n", $maketxt ) );
  227. echo "</pre>"."</blockquote>";
  228. echo "Please let us know that this happened, and paste the above output into your email message.<br>";
  229. include_once $bottom_inc;
  230. // For emacs:
  231. // Local variables:
  232. // c-basic-offset: 4
  233. // c-indent-level: 4
  234. // tab-width: 4
  235. // End:
  236. ?>