packages/web/353/test/test_inner_10369.php

Go to the documentation of this file.
00001 #!/usr/local/sbin/php -q
00002 <?php
00003 
00004 #
00005 # TODO: work with RAW storage
00006 #
00007 #
00008 #
00009 
00010 require 'i2c.inc';
00011 
00012 $stage_fn = "/etc/test/board_10369.stage";
00013 $res_fn = "/etc/test/board_10369.res";
00014 $launch_fn = "/etc/launch.conf";
00015 $self_fn="test_inner_10369.php";
00016 //$stage = "0";
00017 
00018 // stage:
00019 // 0 - basic check: board presence; temperature sensor; prepare for drivers tests - identification CF0/CF1 and reboot;
00020 
00021 //------------------------------------------------------------------------------
00022 // general flow functions
00023 
00024 $stage_xml = new SimpleXMLElement("<?xml version='1.0' standalone='yes'?><board_10369_stage/>");
00025 $stage_xml->stage = "finish";
00026 
00027 function stage_load() {
00028         global $stage_xml;
00029         global $stage_fn;
00030 
00031         if(is_file($stage_fn))
00032                 $stage_xml = simplexml_load_file($stage_fn);
00033 }
00034 
00035 function stage_save() {
00036         global $stage_xml;
00037         global $stage_fn;
00038 
00039         $file = fopen($stage_fn, "w+");
00040         if($file) {
00041                 $rslt = $stage_xml->asXML();
00042                 fwrite($file, $rslt);
00043                 fclose($file);
00044         }
00045 }
00046 
00047 function pr($text) {
00048         echo "<pre>$text</pre>\n";
00049         flush();
00050 }
00051 
00052 function _reboot() {
00053         exec("sync; reboot");
00054 }
00055 
00056 # offline result - stored in a file
00057 function res_update($name, $result, $description) {
00058         global $res_fn;
00059 
00060         $file = fopen($res_fn, "a+");
00061         if($result != "")
00062                 fwrite($file, "<${name} result=\"${result}\">".$description."</${name}>\n");
00063         else
00064                 fwrite($file, "<${name}>".$description."</${name}>\n");
00065         fclose($file);
00066 
00067         pr("<${name} result=\"${result}\">".$description."</${name}>");
00068 }
00069 
00070 function _finish() {
00071         global $stage_xml;
00072         global $launch_fn;
00073 
00074         res_update("finish", "OK", "");
00075         $stage_xml->stage = "finish";
00076         stage_save();
00077         if(is_file($launch_fn))
00078                 unlink($launch_fn);
00079         exit(0);
00080 }
00081 
00082 //------------------------------------------------------------------------------
00083 // check the stage
00084 $flag = FALSE;
00085 stage_load();
00086 
00087 $file = fopen($launch_fn, 'w+');
00088 if($file) {
00089         $pwd = exec("pwd");
00090         fwrite($file, $pwd."/".$self_fn."\n");;
00091         fclose($file);
00092 }
00093 
00094 if($stage_xml->stage == "finish") {
00095         $stage_xml->stage = "0";
00096 }
00097 
00098 if($stage_xml->stage == "0") {
00099         $flag = TRUE;
00100         if(is_file($res_fn))
00101                 unlink($res_fn);
00102         if(test_board_presence() == FALSE)
00103                 _finish();
00104         $stage_xml->stage = "1";
00105 }
00106 
00107 if($stage_xml->stage == "1") {
00108         // check USB storage presence and prepare CF1 & SATA to test
00109         // if so - write test file and reboot (stage 2)
00110         // else - go to "stage 3" for next storage check
00111         $flag = TRUE;
00112         test_enable_CF1_SATA();
00113         if(test_storage("sda", "USB") == TRUE) {
00114                 $stage_xml->stage = "2";
00115                 stage_save();
00116                 _reboot();
00117         } else {
00118                 $stage_xml->stage = "3";
00119                 stage_save();
00120                 _reboot();
00121         }
00122 }
00123 
00124 if($stage_xml->stage == "2") {
00125         $flag = TRUE;
00126         $stage_xml->stage = "3";
00127         test_storage_md5("sda", "USB");
00128 }
00129 
00130 if($stage_xml->stage == "3") {
00131         // CF1 Slave, SATA Master branch - check hda &&
00132         $flag = TRUE;
00133         $CHECK = FALSE;
00134         if(test_storage("hda", "SATA") === TRUE) {
00135                 $stage_xml->sata = "ok";
00136                 $CHECK = TRUE;
00137         }
00138         if(test_storage("hdb", "CF1") === TRUE) {
00139                 $stage_xml->cf1 = "ok";
00140                 $CHECK = TRUE;
00141         }
00142         if($CHECK) {
00143                 $stage_xml->stage = "4";
00144                 stage_save();
00145                 _reboot();
00146         } else {
00147                 test_enable_CF0();
00148                 $stage_xml->stage = "5";
00149                 stage_save();
00150                 _reboot();
00151         }
00152 }
00153 
00154 if($stage_xml->stage == "4") {
00155         $flag = TRUE;
00156         if($stage_xml->sata == "ok")
00157                 test_storage_md5("hda", "SATA");
00158         if($stage_xml->cf1 == "ok")
00159                 test_storage_md5("hdb", "CF1");
00160 //      test_enable_CF1_SATA();
00161         test_enable_CF0();
00162         $stage_xml->stage = "5";
00163         stage_save();
00164         _reboot();
00165 }
00166 
00167 if($stage_xml->stage == "5") {
00168         // set CF0 as Master and SATA as Slave, reboot and check it
00169         $flag = TRUE;
00170         $CHECK = test_storage("hda", "CF0");
00171         if($CHECK) {
00172                 $stage_xml->stage = "6";
00173                 stage_save();
00174                 _reboot();
00175         } else {
00176                 $stage_xml->stage = "7";
00177         }
00178 //      _finish();
00179 }
00180 
00181 if($stage_xml->stage == "6") {
00182         $flag = TRUE;
00183         $stage_xml->stage = "7";
00184         test_storage_md5("hda", "CF0");
00185 }
00186 
00187 if($stage_xml->stage == "7") {
00188         $flag = TRUE;
00189         _finish();
00190 }
00191 
00192 if(!$flag) {
00193         res_update("10369", "FAIL", "wrong test sequence");
00194         _finish();
00195 }
00196 
00197 //------------------------------------------------------------------------------
00198 // tests
00199 function test_board_presence() {
00200         global $stage_fn;
00201         // check the board ID
00202         $_xml = file_get_contents("http://127.0.0.1/i2c.php?cmd=fromEEPROM");
00203         $xml = simplexml_load_string($_xml);
00204         // do it in a last time - it's must be merged to two parts...
00205         if($xml->getName() === "board") {
00206                 // board is found, and ID is written
00207                 res_update("board", "OK", "board present and ID is written");
00208                 res_update("brand", "", $xml->brand);
00209                 res_update("model", "", $xml->model);
00210                 res_update("rev", "", $xml->rev);
00211                 res_update("serial", "", $xml->serial);
00212                 res_update("time", "", $xml->time);
00213         } else {
00214                 if($xml->getName() === "i2c") {
00215                         if($xml->error) {
00216                                 // board not found
00217                                 res_update("board", "FAIL", "can't found board");
00218                                 return FALSE;
00219                         } else {
00220                                 // board is found, and ID is not written
00221                                 res_update("board", "OK", "board present, but ID is not written");
00222                         }
00223                 }
00224         }
00225         // read temperature sensor
00226         $temp = 0.0;
00227         $_xml = file_get_contents("http://127.0.0.1/i2c.php?width=8&bus=1&adr=0x4800");
00228         $xml = simplexml_load_string($_xml);
00229         if($xml->rdata)
00230                 $temp = (float)$xml->rdata;
00231         $_xml = file_get_contents("http://127.0.0.1/i2c.php?width=8&bus=1&adr=0x4822");
00232         $xml = simplexml_load_string($_xml);
00233         if($xml->rdata)
00234                 $temp += ((float)$xml->rdata) / 256.0;
00235         $temp=sprintf("%.2f", $temp);
00236         res_update("temperature", "OK", $temp);
00237         return TRUE;
00238 }
00239 
00240 //------------------------------------------------------------------------------
00241 // storage specific
00242 
00243 class storage {}
00244 
00245 function st_presence($disk) {
00246         $res = FALSE;
00247         $r = exec("dd if=/dev/${disk} of=/dev/null bs=16 count=1 2>&1 | grep 'No such device'");
00248 //      res_update("st_presence", "$disk", "${r}");
00249         if($r == "")
00250 //      if(exec("dd if=/dev/${disk} of=/dev/null bs=16 count=1 2>&1 | grep 'No such device'") == "")
00251                 $res = TRUE;
00252         return $res;
00253 }
00254 
00255 function st_partition($dev, $part) {
00256         $res = FALSE;
00257         $str = exec("fdisk -l /dev/${dev} | grep ${part}");
00258         if($str != "")
00259                 $res = TRUE;
00260         return $res;
00261 }
00262 
00263 function st_mount($dev, $point) {
00264         if(!is_dir($point))
00265                 exec("mkdir $point");
00266         $n = 1;
00267         for($n = 1; $n < 5; $n++) {
00268                 exec("mount ${dev}1 $point");
00269 //              $r = exec("df | grep '${dev}${n} '");
00270                 $r = exec("df | grep '${dev}${n}'");
00271                 if($r != "")
00272                         break;
00273         }
00274 //      $r = exec("df | grep '${dev}${n} '");
00275         $r = exec("df | grep '${dev}${n}'");
00276         if($r != "")
00277                 return TRUE;
00278         return FALSE;
00279 }
00280 
00281 function st_umount($point) {
00282         exec("umount $point");
00283 }
00284 
00285 // check the storage device
00286 function what_is_storage($disk) {
00287 //      object $usb;
00288         $st = new storage;
00289         $st->presence = FALSE;
00290         $st->device = "$disk";
00291         $st->raw = TRUE;
00292         if(!st_presence("$disk")) {
00293                 return $st;
00294         } else {
00295                 $st->presence = TRUE;
00296                 $parts = st_partition("$disk", "${disk}1") || st_partition("$disk", "${disk}2") || st_partition("$disk", "${disk}3") || st_partition("$disk", "${disk}4");
00297                 if($parts)
00298                         $st->raw = FALSE;
00299         }
00300         return $st;
00301 }
00302 
00303 # use `nanddump` instead of `cat /dev/mtdblock3` for correct BB skipping
00304 
00305 function test_storage($disk, $name) {
00306         $st = what_is_storage("$disk");
00307         if($st->presence == FALSE) {
00308                 res_update("${name}", "UNKNOWN", "${name} storage device /dev/${disk} not found");
00309                 return FALSE;
00310         } else {
00311                 if($st->raw == FALSE) {
00312                         $m = st_mount("/dev/${disk}", "/tmp/z");
00313                         if($m == TRUE) {
00314                                 $MAC=exec("bootblocktool -x SERNO");
00315 #                               exec("cat /dev/mtdblock3 > /tmp/z/test_353.${MAC}");
00316                                 exec("nanddump -b /dev/mtd3 -f /tmp/z/test_353.${MAC}");
00317                                 st_umount("/tmp/z");
00318                         } else {
00319                                 res_update("${name}", "FAIL", "${name} storage with partitions, can't mount any partition");
00320                                 return FALSE;
00321                         }
00322                 } else {
00323                         res_update("${name}", "UNKNOWN", "${name} storage w/o partitions");
00324                         return FALSE;
00325                 }
00326         }
00327 //      res_update("${name}", "OK", "all looks ok");
00328         return TRUE;
00329 }
00330 
00331 function test_storage_md5($disk, $name) {
00332         $st = what_is_storage("${disk}");
00333         if($st->presence == FALSE) {
00334                 res_update("${name}", "FAIL", "${name} storage device /dev/${disk} is not the same");
00335                 return;
00336         } else {
00337                 if($st->raw == FALSE) {
00338                         $m = st_mount("/dev/${disk}", "/tmp/z");
00339                         if($m == TRUE) {
00340                                 $MAC=exec("bootblocktool -x SERNO");
00341 #                               $md5_1 = exec("cat /dev/mtdblock3 | md5sum");
00342                                 $md5_1 = exec("nanddump -b /dev/mtd3 2>/dev/null | md5sum");
00343                                 $md5_2 = exec("cat /tmp/z/test_353.${MAC} | md5sum");
00344                                 unlink("/tmp/z/test_353.${MAC}");
00345                                 st_umount("/tmp/z");
00346                                 if($md5_1 === $md5_2) {
00347                                         res_update("${name}", "OK", "MD5SUM is correct");
00348                                         return;
00349                                 }
00350                         }
00351                         res_update("${name}", "FAIL", "MD5SUM is different");
00352                 } else {
00353                         res_update("${name}", "FAIL", "{name} storage is not the same");
00354                 }
00355         }
00356         res_update("${name}", "OK", "all is ok");
00357 }
00358 
00359 function test_enable_CF1_SATA() {
00360         // hda - SATA; hdb - CF1
00361         $reg = 0x7B; // 01111011 - disable CF0, CF1 Slave, SATA Master
00362         $reg_new = sprintf("0x%02X", $reg);
00363 //echo "new REG == ".$reg_new."\n";
00364         $_xml = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000");
00365         $xml = simplexml_load_string($_xml);
00366 $file = fopen("php://stderr", "w");
00367 fprintf($file, "\nbefore: %s\n", (string)$xml->hex_data);
00368 fprintf($file, "enable CF1, SATA (0x7B)\n");
00369         $r1 = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000&data=${reg_new}");
00370         $_xml = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000");
00371         $xml = simplexml_load_string($_xml);
00372         $r2 = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000&data=${reg_new}");
00373 fprintf($file, "after : %s\n\n", (string)$xml->hex_data);
00374 fclose($file);
00375 }
00376 
00377 function test_enable_CF0() {
00378         // hda - CF0; hdb - SATA
00379         $reg = 0x57; // 01010111 - enable CF0 Master, disable CF1, SATA Slave
00380         $reg_new = sprintf("0x%02X", $reg);
00381 //echo "new REG == ".$reg_new."\n";
00382         $_xml = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000");
00383         $xml = simplexml_load_string($_xml);
00384 $file = fopen("php://stderr", "w");
00385 fprintf($file, "\nbefore: %s\n", (string)$xml->hex_data);
00386 fprintf($file, "enable CF0 (0x57)\n");
00387         $r1 = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000&data=${reg_new}");
00388         $_xml = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000");
00389         $xml = simplexml_load_string($_xml);
00390         $r2 = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000&data=${reg_new}");
00391 fprintf($file, "after : %s\n\n", (string)$xml->hex_data);
00392 fclose($file);
00393 }
00394 
00395 /*
00396 /*
00397 function stage_2() {
00398         $_xml = file_get_contents("http://127.0.0.1/i2c.php?bus=1&raw=0x2000");
00399         $xml = simplexml_load_string($_xml);
00400 
00401         $SATA="";
00402         $CF0="";
00403         $CF1="";
00404 
00405         // try to mount /dev/hda & /dev/hdb
00406         $dev_hda_is=0;
00407         $dev_hdb_is=0;
00408         if(exec("dd if=/dev/hda of=/dev/null bs=16 count=1 2>&1 | grep 'No such device'") == "")
00409                 $dev_hda_is=1;
00410         if(exec("dd if=/dev/hdb of=/dev/null bs=16 count=1 2>&1 | grep 'No such device'") == "")
00411                 $dev_hdb_is=1;
00412 
00413         // check the CF0 and CF1 bits; if the CF0 is present:
00414         //      1. set: CF1 dis., ATA slave, CF0 en.  - reboot; test CF0 as master and ATA as slave;
00415         //      2. set: CF0 dis., ATA master, CF1 en. - reboot; test CF1 as slave and ATA as master;
00416 
00417         if($xml->rdata) {
00418                 pr("rdata == ".$xml->rdata);
00419                 $reg = (int)$xml->rdata;
00420                 // check the SATA & CF devices
00421                 // SATA is disabled ?
00422                 $reg & 0x10;
00423                 if($reg & 0x10) {
00424                         // is master?
00425                         if($reg & 0x20)
00426                                 $SATA="a";
00427                         else
00428                                 $SATA="b";
00429                 }
00430         }
00431 }
00432 */
00433 
00434 /*
00435 // check what we have - the drives must be umounted; or check 'df -h' output for this
00436 if(!is_dir("/tmp/z"))
00437         mkdir("/tmp/z");
00438 
00439 $rez = exec("mount /dev/hda1 /tmp/z; df -h | grep /dev/hda");
00440 if($rez != "") {
00441         $mac=exec("bootblocktool -x SERNO");
00442         exec("cat /dev/mtdblock3 > /tmp/z/test_10369.".$mac);
00443         // check MD5SUM here !!!
00444         exec("umount /tmp/z");
00445 } else {
00446         // can be just w/o partitions - check it !!!
00447         pr("ATA drive /dev/hda not found");
00448 }
00449 
00450 $rez = exec("mount /dev/hdb1 /tmp/z; df -h | grep /dev/hdb");
00451 if($rez != "") {
00452         $mac=exec("bootblocktool -x SERNO");
00453         exec("cat /dev/mtdblock3 > /tmp/z/test_10369.".$mac);
00454         // check MD5SUM here !!!
00455         exec("umount /tmp/z");
00456 } else {
00457         // can be just w/o partitions - check it !!!
00458         pr("ATA drive /dev/hdb not found");
00459 }
00460 */
00461 
00462 // 
00463 
00464 /*
00465 pr($xml->result);
00466 pr($xml->brand);
00467 pr($xml->model);
00468 pr($xml->rev);
00469 pr($xml->serial);
00470 pr($xml->time);
00471 */
00472 
00473 ?>

Generated on Thu Aug 7 16:19:01 2008 for elphel by  doxygen 1.5.1