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
00017
00018
00019
00020
00021
00022
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
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
00109
00110
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
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
00161 test_enable_CF0();
00162 $stage_xml->stage = "5";
00163 stage_save();
00164 _reboot();
00165 }
00166
00167 if($stage_xml->stage == "5") {
00168
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
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
00199 function test_board_presence() {
00200 global $stage_fn;
00201
00202 $_xml = file_get_contents("http://127.0.0.1/i2c.php?cmd=fromEEPROM");
00203 $xml = simplexml_load_string($_xml);
00204
00205 if($xml->getName() === "board") {
00206
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
00217 res_update("board", "FAIL", "can't found board");
00218 return FALSE;
00219 } else {
00220
00221 res_update("board", "OK", "board present, but ID is not written");
00222 }
00223 }
00224 }
00225
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
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
00249 if($r == "")
00250
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
00270 $r = exec("df | grep '${dev}${n}'");
00271 if($r != "")
00272 break;
00273 }
00274
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
00286 function what_is_storage($disk) {
00287
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
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
00361 $reg = 0x7B;
00362 $reg_new = sprintf("0x%02X", $reg);
00363
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
00379 $reg = 0x57;
00380 $reg_new = sprintf("0x%02X", $reg);
00381
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
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 ?>