defs.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. <?php // php pages made with phpMyBuilder <http://kyber.dk/phpMyBuilder> ?>
  2. <?php
  3. // (C) Copyright 2001
  4. // Murray Jensen <Murray.Jensen@cmst.csiro.au>
  5. // CSIRO Manufacturing Science and Technology, Preston Lab
  6. // contains mysql user id and password - keep secret
  7. require("config.php");
  8. if (isset($logout)) {
  9. Header("status: 401 Unauthorized");
  10. Header("HTTP/1.0 401 Unauthorized");
  11. Header("WWW-authenticate: basic realm=\"$bddb_label\"");
  12. echo "<html><head><title>" .
  13. "Access to '$bddb_label' Denied" .
  14. "</title></head>\n";
  15. echo "<body bgcolor=#ffffff><br></br><br></br><center><h1>" .
  16. "You must be an Authorised User " .
  17. "to access the '$bddb_label'" .
  18. "</h1>\n</center></body></html>\n";
  19. exit;
  20. }
  21. // contents of the various enumerated types - if first item is
  22. // empty ('') then the enum is allowed to be null (ie "not null"
  23. // is not set on the column)
  24. // all column names in the database table
  25. $columns = array(
  26. 'serno','ethaddr','date','batch',
  27. 'type','rev','location','comments',
  28. 'sdram0','sdram1','sdram2','sdram3',
  29. 'flash0','flash1','flash2','flash3',
  30. 'zbt0','zbt1','zbt2','zbt3','zbt4','zbt5','zbt6','zbt7',
  31. 'zbt8','zbt9','zbta','zbtb','zbtc','zbtd','zbte','zbtf',
  32. 'xlxtyp0','xlxtyp1','xlxtyp2','xlxtyp3',
  33. 'xlxspd0','xlxspd1','xlxspd2','xlxspd3',
  34. 'xlxtmp0','xlxtmp1','xlxtmp2','xlxtmp3',
  35. 'xlxgrd0','xlxgrd1','xlxgrd2','xlxgrd3',
  36. 'cputyp','cpuspd','cpmspd','busspd',
  37. 'hstype','hschin','hschout'
  38. );
  39. // board type
  40. $type_vals = array('IO','CLP','DSP','INPUT','ALT-INPUT','DISPLAY');
  41. // sdram sizes (nbits array is for write into eeprom config file)
  42. $sdram_vals = array('','32M','64M','128M','256M');
  43. $sdram_nbits = array(0,25,26,27,28);
  44. // flash sizes (nbits array is for write into eeprom config file)
  45. $flash_vals = array('','4M','8M','16M','32M','64M');
  46. $flash_nbits = array(0,22,23,24,25,26);
  47. // zbt ram sizes (nbits array is for write into eeprom config file)
  48. $zbt_vals = array('','512K','1M','2M','4M');
  49. $zbt_nbits = array(0,19,20,21,22);
  50. // Xilinx attributes
  51. $xlxtyp_vals = array('','XCV300E','XCV400E','XCV600E','XC2V2000','XC2V3000','XC2V4000','XC2V6000');
  52. $xlxspd_vals = array('','6','7','8','4','5');
  53. $xlxtmp_vals = array('','COM','IND');
  54. $xlxgrd_vals = array('','NORMAL','ENGSAMP');
  55. // processor attributes
  56. $cputyp_vals = array('','MPC8260');
  57. $clk_vals = array('','33MHZ','66MHZ','100MHZ','133MHZ','166MHZ','200MHZ');
  58. // high-speed serial attributes
  59. $hstype_vals = array('','AMCC-S2064A');
  60. $hschin_vals = array('0','1','2','3','4');
  61. $hschout_vals = array('0','1','2','3','4');
  62. // value filters - used when outputting html
  63. function rev_filter($num) {
  64. if ($num == 0)
  65. return "001";
  66. else
  67. return sprintf("%03d", $num);
  68. }
  69. function text_filter($str) {
  70. return urldecode($str);
  71. }
  72. mt_srand(time() | getmypid());
  73. // set up MySQL connection
  74. mysql_connect("", $mysql_user, $mysql_pw) || die("cannot connect");
  75. mysql_select_db($mysql_db) || die("cannot select db");
  76. // page header
  77. function pg_head($title)
  78. {
  79. echo "<html>\n<head>\n";
  80. echo "<link rel=stylesheet href=\"bddb.css\" type=\"text/css\" title=\"style sheet\"></link>\n";
  81. echo "<title>$title</title>\n";
  82. echo "</head>\n";
  83. echo "<body>\n";
  84. echo "<center><h1>$title</h1></center>\n";
  85. echo "<hr></hr>\n";
  86. }
  87. // page footer
  88. function pg_foot()
  89. {
  90. echo "<hr></hr>\n";
  91. echo "<table width=\"100%\"><tr><td align=left>\n<address>" .
  92. "If you have any problems, email " .
  93. "<a href=\"mailto:Murray.Jensen@cmst.csiro.au\">" .
  94. "Murray Jensen" .
  95. "</a></address>\n" .
  96. "</td><td align=right>\n" .
  97. "<a href=\"index.php?logout=true\">logout</a>\n" .
  98. "</td></tr></table>\n";
  99. echo "<p><small><i>Made with " .
  100. "<a href=\"http://kyber.dk/phpMyBuilder/\">" .
  101. "Kyber phpMyBuilder</a></i></small></p>\n";
  102. echo "</body>\n";
  103. echo "</html>\n";
  104. }
  105. // some support functions
  106. if (!function_exists('array_search')) {
  107. function array_search($needle, $haystack, $strict = false) {
  108. if (is_array($haystack) && count($haystack)) {
  109. $ntype = gettype($needle);
  110. foreach ($haystack as $key => $value) {
  111. if ($value == $needle && (!$strict ||
  112. gettype($value) == $ntype))
  113. return $key;
  114. }
  115. }
  116. return false;
  117. }
  118. }
  119. if (!function_exists('in_array')) {
  120. function in_array($needle, $haystack, $strict = false) {
  121. if (is_array($haystack) && count($haystack)) {
  122. $ntype = gettype($needle);
  123. foreach ($haystack as $key => $value) {
  124. if ($value == $needle && (!$strict ||
  125. gettype($value) == $ntype))
  126. return true;
  127. }
  128. }
  129. return false;
  130. }
  131. }
  132. function key_in_array($key, $array) {
  133. return in_array($key, array_keys($array), true);
  134. }
  135. function enum_to_index($name, $vals) {
  136. $index = array_search($GLOBALS[$name], $vals);
  137. if ($vals[0] != '')
  138. $index++;
  139. return $index;
  140. }
  141. // fetch a value from an array - return empty string is not present
  142. function get_key_value($key, $array) {
  143. if (key_in_array($key, $array))
  144. return $array[$key];
  145. else
  146. return '';
  147. }
  148. function fprintf() {
  149. $n = func_num_args();
  150. if ($n < 2)
  151. return FALSE;
  152. $a = func_get_args();
  153. $fp = array_shift($a);
  154. $x = "\$s = sprintf";
  155. $sep = '(';
  156. foreach ($a as $z) {
  157. $x .= "$sep'$z'";
  158. $sep = ',';
  159. }
  160. $x .= ');';
  161. eval($x);
  162. $l = strlen($s);
  163. $r = fwrite($fp, $s, $l);
  164. if ($r != $l)
  165. return FALSE;
  166. else
  167. return TRUE;
  168. }
  169. // functions to display (print) a database table and its columns
  170. function begin_table($ncols) {
  171. global $table_ncols;
  172. $table_ncols = $ncols;
  173. echo "<table align=center width=\"100%\""
  174. . " border=1 cellpadding=4 cols=$table_ncols>\n";
  175. }
  176. function begin_field($name, $span = 0) {
  177. global $table_ncols;
  178. echo "<tr valign=top>\n";
  179. echo "\t<th align=center>$name</th>\n";
  180. if ($span <= 0)
  181. $span = $table_ncols - 1;
  182. if ($span > 1)
  183. echo "\t<td colspan=$span>\n";
  184. else
  185. echo "\t<td>\n";
  186. }
  187. function cont_field($span = 1) {
  188. echo "\t</td>\n";
  189. if ($span > 1)
  190. echo "\t<td colspan=$span>\n";
  191. else
  192. echo "\t<td>\n";
  193. }
  194. function end_field() {
  195. echo "\t</td>\n";
  196. echo "</tr>\n";
  197. }
  198. function end_table() {
  199. echo "</table>\n";
  200. }
  201. function print_field($name, $array, $size = 0, $filt='') {
  202. begin_field($name);
  203. if (key_in_array($name, $array))
  204. $value = $array[$name];
  205. else
  206. $value = '';
  207. if ($filt != '')
  208. $value = $filt($value);
  209. echo "\t\t<input name=$name value=\"$value\"";
  210. if ($size > 0)
  211. echo " size=$size maxlength=$size";
  212. echo "></input>\n";
  213. end_field();
  214. }
  215. function print_field_multiline($name, $array, $cols, $rows, $filt='') {
  216. begin_field($name);
  217. if (key_in_array($name, $array))
  218. $value = $array[$name];
  219. else
  220. $value = '';
  221. if ($filt != '')
  222. $value = $filt($value);
  223. echo "\t\t<textarea name=$name " .
  224. "cols=$cols rows=$rows wrap=off>\n";
  225. echo "$value";
  226. echo "</textarea>\n";
  227. end_field();
  228. }
  229. // print a mysql ENUM as an html RADIO INPUT
  230. function print_enum($name, $array, $vals, $def = -1) {
  231. begin_field($name);
  232. if (key_in_array($name, $array))
  233. $chk = array_search($array[$name], $vals, FALSE);
  234. else
  235. $chk = $def;
  236. $nval = count($vals);
  237. for ($i = 0; $i < $nval; $i++) {
  238. $val = $vals[$i];
  239. if ($val == '')
  240. $pval = "none";
  241. else
  242. $pval = "$val";
  243. printf("\t\t<input type=radio name=$name"
  244. . " value=\"$val\"%s>$pval</input>\n",
  245. $i == $chk ? " checked" : "");
  246. }
  247. end_field();
  248. }
  249. // print a group of mysql ENUMs (e.g. name0,name1,...) as an html SELECT
  250. function print_enum_multi($base, $array, $vals, $cnt, $defs, $grp = 0) {
  251. global $table_ncols;
  252. if ($grp <= 0)
  253. $grp = $cnt;
  254. $ncell = $cnt / $grp;
  255. $span = ($table_ncols - 1) / $ncell;
  256. begin_field($base, $span);
  257. $nval = count($vals);
  258. for ($i = 0; $i < $cnt; $i++) {
  259. if ($i > 0 && ($i % $grp) == 0)
  260. cont_field($span);
  261. $name = sprintf("%s%x", $base, $i);
  262. echo "\t\t<select name=$name>\n";
  263. if (key_in_array($name, $array))
  264. $ai = array_search($array[$name], $vals, FALSE);
  265. else {
  266. if (key_in_array($i, $defs))
  267. $ai = $defs[$i];
  268. else
  269. $ai = 0;
  270. }
  271. for ($j = 0; $j < $nval; $j++) {
  272. $val = $vals[$j];
  273. if ($val == '')
  274. $pval = "&nbsp;";
  275. else
  276. $pval = "$val";
  277. printf("\t\t\t<option " .
  278. "value=\"%s\"%s>%s</option>\n",
  279. $val,
  280. $j == $ai ? " selected" : "",
  281. $pval);
  282. }
  283. echo "\t\t</select>\n";
  284. }
  285. end_field();
  286. }
  287. // functions to handle the form input
  288. // fetch all the parts of an "enum_multi" into a string suitable
  289. // for a MySQL query
  290. function gather_enum_multi_query($base, $cnt) {
  291. $retval = '';
  292. for ($i = 0; $i < $cnt; $i++) {
  293. $name = sprintf("%s%x", $base, $i);
  294. if (isset($GLOBALS[$name])) {
  295. $retval .= sprintf(", %s='%s'",
  296. $name, $GLOBALS[$name]);
  297. }
  298. }
  299. return $retval;
  300. }
  301. // fetch all the parts of an "enum_multi" into a string suitable
  302. // for a display e.g. in an html table cell
  303. function gather_enum_multi_print($base, $cnt, $array) {
  304. $retval = '';
  305. for ($i = 0; $i < $cnt; $i++) {
  306. $name = sprintf("%s%x", $base, $i);
  307. if ($array[$name] != '') {
  308. if ($retval != '')
  309. $retval .= ',';
  310. $retval .= $array[$name];
  311. }
  312. }
  313. return $retval;
  314. }
  315. // fetch all the parts of an "enum_multi" into a string suitable
  316. // for writing to the eeprom data file
  317. function gather_enum_multi_write($base, $cnt, $vals, $xfrm = array()) {
  318. $retval = '';
  319. for ($i = 0; $i < $cnt; $i++) {
  320. $name = sprintf("%s%x", $base, $i);
  321. if ($GLOBALS[$name] != '') {
  322. if ($retval != '')
  323. $retval .= ',';
  324. $index = enum_to_index($name, $vals);
  325. if ($xfrm != array())
  326. $retval .= $xfrm[$index];
  327. else
  328. $retval .= $index;
  329. }
  330. }
  331. return $retval;
  332. }
  333. // count how many parts of an "enum_multi" are actually set
  334. function count_enum_multi($base, $cnt) {
  335. $retval = 0;
  336. for ($i = 0; $i < $cnt; $i++) {
  337. $name = sprintf("%s%x", $base, $i);
  338. if (isset($GLOBALS[$name]))
  339. $retval++;
  340. }
  341. return $retval;
  342. }
  343. // ethernet address functions
  344. // generate a (possibly not unique) random vendor ethernet address
  345. // (setting bit 6 in the ethernet address - motorola wise i.e. bit 0
  346. // is the most significant bit - means it is not an assigned ethernet
  347. // address - it is a "locally administered" address). Also, make sure
  348. // it is NOT a multicast ethernet address (by setting bit 7 to 0).
  349. // e.g. the first byte of all ethernet addresses generated here will
  350. // have 2 in the bottom two bits (incidentally, these are the first
  351. // two bits transmitted on the wire, since the octets in ethernet
  352. // addresses are transmitted LSB first).
  353. function gen_eth_addr($serno) {
  354. $ethaddr_high = (mt_rand(0, 65535) & 0xfeff) | 0x0200;
  355. $ethaddr_low = mt_rand(0, 4294967295);
  356. return sprintf("%02lx:%02lx:%02lx:%02lx:%02lx:%02lx",
  357. $ethaddr_high >> 8, $ethaddr_high & 0xff,
  358. $ethaddr_low >> 24, ($ethaddr_low >> 16) & 0xff,
  359. ($ethaddr_low >> 8) & 0xff, $ethaddr_low & 0xff);
  360. }
  361. // check that an ethernet address is valid
  362. function eth_addr_is_valid($ethaddr) {
  363. $ethbytes = split(':', $ethaddr);
  364. if (count($ethbytes) != 6)
  365. return FALSE;
  366. for ($i = 0; $i < 6; $i++) {
  367. $ethbyte = $ethbytes[$i];
  368. if (!ereg('^[0-9a-f][0-9a-f]$', $ethbyte))
  369. return FALSE;
  370. }
  371. return TRUE;
  372. }
  373. // write a simple eeprom configuration file
  374. function write_eeprom_cfg_file() {
  375. global $sernos, $nsernos, $bddb_cfgdir, $numerrs, $cfgerrs;
  376. global $date, $batch, $type_vals, $rev;
  377. global $sdram_vals, $sdram_nbits;
  378. global $flash_vals, $flash_nbits;
  379. global $zbt_vals, $zbt_nbits;
  380. global $xlxtyp_vals, $xlxspd_vals, $xlxtmp_vals, $xlxgrd_vals;
  381. global $cputyp, $cputyp_vals, $clk_vals;
  382. global $hstype, $hstype_vals, $hschin, $hschout;
  383. $numerrs = 0;
  384. $cfgerrs = array();
  385. for ($i = 0; $i < $nsernos; $i++) {
  386. $serno = sprintf("%010d", $sernos[$i]);
  387. $wfp = @fopen($bddb_cfgdir . "/$serno.cfg", "w");
  388. if (!$wfp) {
  389. $cfgerrs[$i] = 'file create fail';
  390. $numerrs++;
  391. continue;
  392. }
  393. set_file_buffer($wfp, 0);
  394. if (!fprintf($wfp, "serno=%d\n", $sernos[$i])) {
  395. $cfgerrs[$i] = 'cfg wr fail (serno)';
  396. fclose($wfp);
  397. $numerrs++;
  398. continue;
  399. }
  400. if (!fprintf($wfp, "date=%s\n", $date)) {
  401. $cfgerrs[$i] = 'cfg wr fail (date)';
  402. fclose($wfp);
  403. $numerrs++;
  404. continue;
  405. }
  406. if ($batch != '') {
  407. if (!fprintf($wfp, "batch=%s\n", $batch)) {
  408. $cfgerrs[$i] = 'cfg wr fail (batch)';
  409. fclose($wfp);
  410. $numerrs++;
  411. continue;
  412. }
  413. }
  414. $typei = enum_to_index("type", $type_vals);
  415. if (!fprintf($wfp, "type=%d\n", $typei)) {
  416. $cfgerrs[$i] = 'cfg wr fail (type)';
  417. fclose($wfp);
  418. $numerrs++;
  419. continue;
  420. }
  421. if (!fprintf($wfp, "rev=%d\n", $rev)) {
  422. $cfgerrs[$i] = 'cfg wr fail (rev)';
  423. fclose($wfp);
  424. $numerrs++;
  425. continue;
  426. }
  427. $s = gather_enum_multi_write("sdram", 4,
  428. $sdram_vals, $sdram_nbits);
  429. if ($s != '') {
  430. $b = fprintf($wfp, "sdram=%s\n", $s);
  431. if (!$b) {
  432. $cfgerrs[$i] = 'cfg wr fail (sdram)';
  433. fclose($wfp);
  434. $numerrs++;
  435. continue;
  436. }
  437. }
  438. $s = gather_enum_multi_write("flash", 4,
  439. $flash_vals, $flash_nbits);
  440. if ($s != '') {
  441. $b = fprintf($wfp, "flash=%s\n", $s);
  442. if (!$b) {
  443. $cfgerrs[$i] = 'cfg wr fail (flash)';
  444. fclose($wfp);
  445. $numerrs++;
  446. continue;
  447. }
  448. }
  449. $s = gather_enum_multi_write("zbt", 16,
  450. $zbt_vals, $zbt_nbits);
  451. if ($s != '') {
  452. $b = fprintf($wfp, "zbt=%s\n", $s);
  453. if (!$b) {
  454. $cfgerrs[$i] = 'cfg wr fail (zbt)';
  455. fclose($wfp);
  456. $numerrs++;
  457. continue;
  458. }
  459. }
  460. $s = gather_enum_multi_write("xlxtyp", 4, $xlxtyp_vals);
  461. if ($s != '') {
  462. $b = fprintf($wfp, "xlxtyp=%s\n", $s);
  463. if (!$b) {
  464. $cfgerrs[$i] = 'cfg wr fail (xlxtyp)';
  465. fclose($wfp);
  466. $numerrs++;
  467. continue;
  468. }
  469. }
  470. $s = gather_enum_multi_write("xlxspd", 4, $xlxspd_vals);
  471. if ($s != '') {
  472. $b = fprintf($wfp, "xlxspd=%s\n", $s);
  473. if (!$b) {
  474. $cfgerrs[$i] = 'cfg wr fail (xlxspd)';
  475. fclose($wfp);
  476. $numerrs++;
  477. continue;
  478. }
  479. }
  480. $s = gather_enum_multi_write("xlxtmp", 4, $xlxtmp_vals);
  481. if ($s != '') {
  482. $b = fprintf($wfp, "xlxtmp=%s\n", $s);
  483. if (!$b) {
  484. $cfgerrs[$i] = 'cfg wr fail (xlxtmp)';
  485. fclose($wfp);
  486. $numerrs++;
  487. continue;
  488. }
  489. }
  490. $s = gather_enum_multi_write("xlxgrd", 4, $xlxgrd_vals);
  491. if ($s != '') {
  492. $b = fprintf($wfp, "xlxgrd=%s\n", $s);
  493. if (!$b) {
  494. $cfgerrs[$i] = 'cfg wr fail (xlxgrd)';
  495. fclose($wfp);
  496. $numerrs++;
  497. continue;
  498. }
  499. }
  500. if ($cputyp != '') {
  501. $cputypi = enum_to_index("cputyp",$cputyp_vals);
  502. $cpuspdi = enum_to_index("cpuspd", $clk_vals);
  503. $busspdi = enum_to_index("busspd", $clk_vals);
  504. $cpmspdi = enum_to_index("cpmspd", $clk_vals);
  505. $b = fprintf($wfp, "cputyp=%d\ncpuspd=%d\n" .
  506. "busspd=%d\ncpmspd=%d\n",
  507. $cputypi, $cpuspdi, $busspdi, $cpmspdi);
  508. if (!$b) {
  509. $cfgerrs[$i] = 'cfg wr fail (cputyp)';
  510. fclose($wfp);
  511. $numerrs++;
  512. continue;
  513. }
  514. }
  515. if ($hstype != '') {
  516. $hstypei = enum_to_index("hstype",$hstype_vals);
  517. $b = fprintf($wfp, "hstype=%d\n" .
  518. "hschin=%s\nhschout=%s\n",
  519. $hstypei, $hschin, $hschout);
  520. if (!$b) {
  521. $cfgerrs[$i] = 'cfg wr fail (hstype)';
  522. fclose($wfp);
  523. $numerrs++;
  524. continue;
  525. }
  526. }
  527. if (!fclose($wfp)) {
  528. $cfgerrs[$i] = 'file cls fail';
  529. $numerrs++;
  530. }
  531. }
  532. return $numerrs;
  533. }
  534. ?>