a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync<?php // -*- Mode: PHP; -*-
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Copyright (C) 2009 Marty Connor <mdc@etherboot.org>.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Copyright (C) 2009 Entity Cyber, Inc.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is free software; you can redistribute it and/or
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * modify it under the terms of the GNU General Public License as
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * published by the Free Software Foundation; either version 2 of the
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * License, or any later version.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * This program is distributed in the hope that it will be useful, but
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * WITHOUT ANY WARRANTY; without even the implied warranty of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * General Public License for more details.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * You should have received a copy of the GNU General Public License
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * along with this program; if not, write to the Free Software
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Include table of user-configurable iPXE options
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncrequire_once "flag-table.php";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Include user-shadowable globals
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncrequire_once "globals.php";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Allow user to shadow globals
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync include_once "local-config.php";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// General utility functions
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Remove undesirable characters from a given string
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Certain characters have the potential to be used for
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * malicious purposes by web-based attackers. This routine
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * filters out such characters.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $s supplied string
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string returned string with unwanted characters
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ( $ltr >= ord ( "A" ) ) && ( $ltr <= ord ( "Z" ) ) ||
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ( $ltr >= ord ( "0" ) ) && ( $ltr <= ord ( "9" ) ) ||
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ( $ltr == ord ( "." ) ) && ( strlen ( $result ) > 0 ) ||
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return URL of the currently running script, minus the filename
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string the URL of the currently running script, minus the filename
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $isHTTPS = ( isset ( $_SERVER["HTTPS"] ) && $_SERVER["HTTPS"] == "on" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ( ( !$isHTTPS && $_SERVER["SERVER_PORT"] != "80" ) ||
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync ( $isHTTPS && $_SERVER["SERVER_PORT"] != "443" ) ) );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $port = ( $port ) ? ':" . $_SERVER["SERVER_PORT"] : '";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Extract NIC families and associated ROM PCI IDs from the src/bin/NIC file.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * $src_dir must contain the path of the iPXE src directory for this build
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return array[0] array $new_nics
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return array[1] array $roms
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ! $fd ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync die ( "Missing src/bin/NIC file. 'make bin/NIC'" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( strpos ( $first_eight_chars, "family" ) === 0 ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // get pathname of NIC driver
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync #list ( $dummy, $nic ) = split( "[ \t]+", $line );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // extract filename name of driver from pathname
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // For each ISA NIC, there can only be one ROM variant
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // If the first 8 digits of the line are hex digits
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // add this rom to the current nic family.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // put most NICs in nice alpha order for menu
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // add special cases to the top
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // skip the undi driver
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// HTML form utility functions
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return html code to create hidden form input fields
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $flag name of form variable to set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value value to give form variable
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string html code for given hidden form input field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return "<input type=\"hidden\" value=\"$value\" name=\"$flag\"></input>";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return html code to create checkbox form input fields
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $flag name of form variable to set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value "on" means box should be checked
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string html code for given hidden form input field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return "<input type=\"checkbox\" value=\"on\" name=\"$flag\"" .
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return html code to create text form input fields
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $flag name of form variable to set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value initial contents of field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $size size in characters of text box
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string html code for given text input field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return "<input type=\"text\" size=\"$size\" value=\"$value\" name=\"$flag\">";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return html code to create textarea form fields
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $flag name of form variable to set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value initial contents of textarea
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $rows height of text area in rows
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $cols width of text area in columns
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string html code for given textarea input field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync return "<textarea name=\"$flag\" rows=\"$rows\" cols=\"$cols\">"
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return html code to create select (menu) form fields
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Use array of strings as menu choices
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $flag name of form variable to set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param array $options array of strings representing choices
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value value of choice to select in menu
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string html code for given select (menu) input field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $s .= "<option" . ( $option == $value ? " selected>" : ">" ) .
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return html code to create select (menu) form fields
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Use indices of array of strings as menu choices rather than
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * the values pointed to by the indicies.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $flag name of form variable to set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param array $options array of strings representing choices
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value value of choice to select in menu
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string html code for given select (menu) input field
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $s .= "<option" . ( $option == $value ? " selected>" : ">" ) .
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Flag (compile option) handling functions
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return default compile options (flags)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Initial compile options are in a global called $flag_table.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Create and return an array containing the ones we want.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return array default compile options (flags)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Fields like headers have no "value" property
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return combination of default and user compile options (flags)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Initial compile options are in a global called $flag_table.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Compile options may have been changed via form input. We return
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * an array with either the default value of each option or a user
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * supplied value from form input.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return array combined default and user supplied compile options (flags)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( strtolower ( substr ( $flags[$flag], 0, 2 ) ) != "0x" ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Unchecked checkboxes don't pass any POST value
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // so we must check for them specially. At this
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // point we know that there is no $_POST value set
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // for this option. If it is a checkbox, this means
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // it is unchecked, so record that in $flags so we
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // can later generate an #undef for this option.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Output given value in appropriate format for iPXE config file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * iPXE config/*.h files use C pre-processor syntax. Output the given
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * compile option in a format appropriate to its type
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $key index into $flag_table for given compile option
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $value value we wish to set compile option to
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string code to set compile option to given value
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Determine type of given compile option (flag)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync } else if ( $type == "on/off" && $value != "on" ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $s = ( "#define $key \"" . cleanstring ( $value ) . "\"" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $s = ( "#define $key \\\"" . cleanstring ( $value ) . "\\\"" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Output html code to display all compile options as a table
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param array $flags array of compile options
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return void
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "<table>\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Hide parameters from users that should not be changed.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $hide_from_user = isset ( $props["hide_from_user"] ) ? $props["hide_from_user"] : "no";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $value = isset ( $flags[$flag] ) ? $flags[$flag] : '";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Hidden flags cannot not be set by the user. We use hidden form
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // fields to keep them at their default values.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Flag (iPXE compile option) should be displayed to user
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "<td>", checkbox ( $flag, $value ), "</td><td><strong>$flag</strong></td>";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync } else { // don't display checkbox for non-on/off flags
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "<td> </td><td><strong>$flag: </strong>";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "</td>";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "</tr>\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "<tr><td> </td>";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "<td>\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "\n</td></tr>\n";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo "</table>";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Return an array of configuration sections used in all compile options
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * $flag_table, the global list of compile options contains a 'cfgsec'
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * property for each flag we are interested in. We return a list of
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * all the unique cfgsec options we find in $flag_table.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return array an array of strings representing all unique cfgsec values
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * found in $flag_table
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// File and directory handling functions
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Create a copy of a given source directory to a given destination
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Since we are going to modify the source directory, we create a copy
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * of the directory with a unique name in the given destination directory.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * We supply a prefix for the tempnam call to prepend to the random filename
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * it generates.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $src source directory
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $dst destination directory
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $prefix string to append to directory created
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return string absolute path to destination directory
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $src = dirname ( $_SERVER['SCRIPT_FILENAME"] ) . "/" . $src;
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Create a file in the given destination directory with a unique name
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // Delete the file just created, since it would interfere with the copy we
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // are about to do. We only care that the dir name we copy to is unique.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync exec ( "/bin/cp -a '$src' '$dir' 2>&1", $cpytxt, $status );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync die ( "src directory copy failed!" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Write iPXE config files based on value of given flags
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * iPXE compile options are stored in src/config/*.h .
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * We write out a config file for each set of options.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $config_dir directory to write .h files to
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param array $flags array of compile options for this build
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return void
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsyncfunction write_ipxe_config_files ( $config_dir, $flags )
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync $ifdef_secname = "CONFIG_" . strtoupper ( $cfgsec ) . "_H";
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync // When the flag matches this section name, write it out
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync fwrite ( $fp, pprint_flag ( $key, $value ) . "\n" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Output a string to a file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Output a given string to a given pathname. The file will be created if
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * necessary, and the string will replace the file's contents in all cases.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $fname pathname of file to output string to
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $ftext text to output to file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return void
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( ! $fp ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Delete a file or recursively delete a directory tree
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param string $file_or_dir_name name of file or directory to delete
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return bool Returns TRUE on success, FALSE on failure
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync if ( is_file ( $file_or_dir_name ) || is_link ( $file_or_dir_name ) ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync while ( ( $dir_entry = $dir->read () ) !== false ) {
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync rm_file_or_dir ( $file_or_dir_name . '/" . $dir_entry );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Debugging functions
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Emit html code to display given array of compile options (flags)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @param array $flags array of compile options for this build
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return void
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo ( "\$flags contains " . count ( $flags ) . " elements:" . "<br>" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo ( "\$flags[" . $key . "]=" . "\"$flag\"" . "<br>" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * Emit HTML code to display default array of compile options (flags)
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * $flag_table contains default compile options and properties. This
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * routine outputs HTML code to display all properties of $flag_table.
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync * @return void
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync echo ( "\$flag_table contains " . count ( $flag_table ) . " elements:" . "<br>" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync print ( " " . $key2 . " = " . $props2 . "<br>" );
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Parse src/bin/NIC file
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// For emacs:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// Local variables:
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// c-basic-offset: 4
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// c-indent-level: 4
a734c64bff58bda2fa48c2795453e092167b0ff7vboxsync// tab-width: 4