Subversion Repositories NedoOS

Rev

Rev 2412 | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <intrz80.h>
  4. #include <stdlib.h>
  5. #include <oscalls.h>
  6. #include <../common/terminal.c>
  7. #include <tcp.h>
  8. #include <osfs.h>
  9. #include <intrz80.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. //////////////////
  13. #define true 1
  14. #define false 0
  15. FILE *fp2; // must be global if savebuf may not to close file.
  16.  
  17. unsigned int RBR_THR = 0xf8ef;
  18. unsigned int IER = 0xf9ef;
  19. unsigned int IIR_FCR = 0xfaef;
  20. unsigned int LCR = 0xfbef;
  21. unsigned int MCR = 0xfcef;
  22. unsigned int LSR = 0xfdef;
  23. unsigned int MSR = 0xfeef;
  24. unsigned int SR = 0xffef;
  25. unsigned int divider = 1;
  26. unsigned int comType = 0;
  27. unsigned int espType = 32;
  28. unsigned int netDriver = 0;
  29. unsigned int espRetry = 5;
  30. unsigned long factor, timerok, count = 0;
  31. unsigned int magic = 15;
  32.  
  33. unsigned char uVer[] = "2.0";
  34. unsigned char curPath[128];
  35. unsigned char curLetter;
  36. unsigned char oldBinExt;
  37. unsigned int errn, headlng;
  38. unsigned long contLen;
  39. unsigned char saveFlag, saveBak;
  40. unsigned char crlf[2] = {13, 10};
  41.  
  42. unsigned char status, curFormat;
  43.  
  44. struct sockaddr_in targetadr;
  45. struct readstructure readStruct;
  46. struct sockaddr_in dnsaddress;
  47.  
  48. struct window
  49. {
  50.         unsigned char x;
  51.         unsigned char y;
  52.         unsigned char w;
  53.         unsigned char h;
  54.         unsigned char text;
  55.         unsigned char back;
  56.         unsigned char tittle[80];
  57.  
  58. } cw;
  59.  
  60. struct configuration
  61. {
  62.         unsigned char kernelName[32];
  63.         unsigned char machineName[32];
  64.         unsigned char kernelLink[256];
  65.         unsigned char is_atm;
  66. } config;
  67.  
  68. unsigned char netbuf[4096];
  69.  
  70. unsigned char userAgent[] = " HTTP/1.1\r\nHost: nedoos.ru\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0";
  71. unsigned char binLink[] = "http://nedoos.ru/images/sysbin.zip";
  72. unsigned char pkunzipLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/pkunzip.com";
  73. unsigned char tarLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/tar.com";
  74. unsigned char cmdLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/cmd.com";
  75. unsigned char termLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/term.com";
  76. unsigned char updLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/updater.com";
  77. unsigned char newsLink[] = "/svn/dl.php?repname=NedoOS&path=/release/doc/updater.new";
  78. unsigned char wizNetLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/wizcfg.com";
  79. unsigned char netIniLink[] = "/svn/dl.php?repname=NedoOS&path=/release/bin/net.ini";
  80. unsigned char relLink[] = "http://nedoos.ru/images/release.zip";
  81. unsigned char nameBuf1[512];
  82. unsigned char *nameBuf = nameBuf1;
  83. // const unsigned char sendOk[] = "SEND OK";
  84. const unsigned char gotWiFi[] = "WIFI GOT IP";
  85. unsigned char cmd[512];
  86. unsigned char link[512];
  87.  
  88. void clearNetBuf(unsigned int const size)
  89. {
  90.         unsigned int counter;
  91.         for (counter = 0; counter < size; counter++)
  92.         {
  93.                 netbuf[counter] = 0;
  94.         }
  95. }
  96.  
  97. void quit(void)
  98. {
  99.         ATRIB(40);
  100.         ATRIB(32);
  101.         AT(1, 25);
  102.         exit(0);
  103. }
  104.  
  105. void spaces(unsigned char number)
  106. {
  107.         while (number > 0)
  108.         {
  109.                 putchar(' ');
  110.                 number--;
  111.         }
  112. }
  113.  
  114. void clearStatus(void)
  115. {
  116.         AT(1, 24);
  117.         spaces(80);
  118.         putchar('\r');
  119. }
  120.  
  121. void printTable(void)
  122. {
  123.         unsigned int cycle;
  124.  
  125.         for (cycle = 32; cycle < 256; cycle++)
  126.         {
  127.                 printf("%03u:", cycle);
  128.                 putchar(cycle);
  129.                 printf(" ");
  130.                 if (cycle % 10 == 0)
  131.                 {
  132.                         printf("\r\n");
  133.                 }
  134.         }
  135. }
  136.  
  137. void delay(unsigned long counter)
  138. {
  139.         unsigned long start, finish;
  140.         counter = counter / 20;
  141.         if (counter < 1)
  142.         {
  143.                 counter = 1;
  144.         }
  145.         start = time();
  146.         finish = start + counter;
  147.  
  148.         while (start < finish)
  149.         {
  150.                 start = time();
  151.         }
  152. }
  153.  
  154. unsigned char delayLongKey(unsigned long counter)
  155. {
  156.         unsigned long start, finish, key;
  157.         counter = counter / 20;
  158.         if (counter < 1)
  159.         {
  160.                 counter = 1;
  161.         }
  162.         start = time();
  163.         finish = start + counter;
  164.  
  165.         while (start < finish)
  166.         {
  167.                 start = time();
  168.                 key = _low_level_get();
  169.                 if (key != 0)
  170.                 {
  171.  
  172.                         if (key == 27)
  173.                         {
  174.                                 quit();
  175.                         }
  176.                         return key;
  177.                 }
  178.                 YIELD();
  179.         }
  180.         return 0;
  181. }
  182.  
  183. void waitKey(void)
  184. {
  185.         do
  186.         {
  187.                 YIELD();
  188.         } while (_low_level_get() == 0);
  189. }
  190.  
  191. void printNews(void) // max 20 lines in total and 59 col.
  192. {
  193.         FILE *fpNews;
  194.         unsigned char str[1];
  195.         unsigned char curLine, nbyte;
  196.  
  197.         fpNews = OS_OPENHANDLE("updater.new", 0x80);
  198.         if (((int)fpNews) & 0xff)
  199.         {
  200. #include <printnews.c>
  201.                 return;
  202.         }
  203.         curLine = 0;
  204.         while (curLine < 21)
  205.         {
  206.                 AT(20, 2 + curLine);
  207.                 while (1)
  208.                 {
  209.                         OS_READHANDLE(str, fpNews, sizeof(str));
  210.  
  211.                         if (errno != 0)
  212.                         {
  213.                                 OS_CLOSEHANDLE(fpNews);
  214.                                 return;
  215.                         }
  216.  
  217.                         nbyte = str[0];
  218.  
  219.                         if (nbyte != 13)
  220.                         {
  221.                                 putchar(nbyte);
  222.                         }
  223.                         else
  224.                         {
  225.                                 break;
  226.                         }
  227.                 }
  228.                 OS_READHANDLE(str, fpNews, sizeof(str));
  229.                 curLine++;
  230.         }
  231.         OS_CLOSEHANDLE(fpNews);
  232. }
  233.  
  234. void drawWindow(struct window w)
  235. {
  236.         unsigned char wcount, tempx, tittleStart;
  237.  
  238.         AT(w.x, w.y - 1);
  239.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2) + 1;
  240.         BOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  241.         AT(w.x, w.y);
  242.         ATRIB(w.text);
  243.         putchar(201);
  244.         for (wcount = 0; wcount < w.w; wcount++)
  245.         {
  246.                 putchar(205);
  247.         }
  248.         putchar(187);
  249.  
  250.         AT(w.x, w.y + w.h);
  251.         putchar(200);
  252.         for (wcount = 0; wcount < w.w; wcount++)
  253.         {
  254.                 putchar(205);
  255.         }
  256.         putchar(188);
  257.  
  258.         tempx = w.x + w.w + 1;
  259.         for (wcount = 1; wcount < w.h; wcount++)
  260.         {
  261.                 AT(w.x, w.y + wcount);
  262.                 putchar(186);
  263.                 AT(tempx, w.y + wcount);
  264.                 putchar(186);
  265.         }
  266.  
  267.         AT(w.x, w.y + 2);
  268.         putchar(199);
  269.         for (wcount = 0; wcount < w.w; wcount++)
  270.         {
  271.                 putchar(196);
  272.         }
  273.         putchar(182);
  274.  
  275.         AT(tittleStart, w.y + 1);
  276.         printf("%s", w.tittle);
  277. }
  278.  
  279. void fatalError(const unsigned char *message)
  280. {
  281.         strcpy(cw.tittle, "FATAL ERROR!");
  282.  
  283.         if (strlen(message) > strlen(cw.tittle))
  284.         {
  285.                 cw.w = strlen(message) + 2;
  286.         }
  287.         else
  288.                 cw.w = strlen(cw.tittle) + 2;
  289.         cw.x = 80 / 2 - cw.w / 2;
  290.         cw.y = 11;
  291.         cw.h = 4;
  292.         cw.text = 97;
  293.         cw.back = 41;
  294.         drawWindow(cw);
  295.         AT(cw.x + 2, cw.y + 3);
  296.         printf("%s", message);
  297.         AT(1, 1);
  298.         getchar();
  299.         exit(0);
  300. }
  301.  
  302. void infoBox(const unsigned char *message)
  303. {
  304.         strcpy(cw.tittle, "nedoOS system updater ");
  305.         strcat(cw.tittle, uVer);
  306.  
  307.         if (strlen(message) > strlen(cw.tittle))
  308.         {
  309.                 cw.w = strlen(message) + 2;
  310.         }
  311.         else
  312.                 cw.w = strlen(cw.tittle) + 2;
  313.         cw.x = 80 / 2 - cw.w / 2;
  314.         cw.y = 15;
  315.         cw.h = 4;
  316.         cw.text = 97;
  317.         cw.back = 42;
  318.  
  319.         drawWindow(cw);
  320.         AT(cw.x + 2, cw.y + 3);
  321.         printf("%s", message);
  322.         AT(1, 1);
  323. }
  324.  
  325. unsigned char OS_SHELL(const unsigned char *command)
  326. {
  327.         unsigned char fileName[] = "bin/cmd.com";
  328.         unsigned char appCmd[128] = "cmd.com ";
  329.         unsigned int shellSize, loaded, loop, adr;
  330.         unsigned char pgbak;
  331.         union APP_PAGES shell_pg;
  332.         union APP_PAGES main_pg;
  333.         FILE *fp3;
  334.         main_pg.l = OS_GETMAINPAGES();
  335.         pgbak = main_pg.pgs.window_3;
  336.         OS_GETPATH((unsigned int)&curPath);
  337.         OS_CHDIR("/");
  338.  
  339.         strcat(appCmd, command);
  340.         fp3 = OS_OPENHANDLE(fileName, 0x80);
  341.         if (((int)fp3) & 0xff)
  342.         {
  343.                 clearStatus();
  344.                 AT(1, 24);
  345.                 printf("%s", fileName);
  346.                 printf(" not found.");
  347.                 getchar();
  348.                 exit(0);
  349.         }
  350.  
  351.         shellSize = OS_GETFILESIZE(fp3);
  352.  
  353.         OS_CHDIR(curPath);
  354.  
  355.         OS_NEWAPP((unsigned int)&shell_pg);
  356.         shell_pg.l = OS_GETAPPMAINPAGES(shell_pg.pgs.pId);
  357.         SETPG32KHIGH(shell_pg.pgs.window_0);
  358.         memcpy((unsigned char *)(0xC080), (unsigned char *)(&appCmd), strlen(appCmd) + 1);
  359.  
  360.         loop = 0;
  361.         while (loop < shellSize)
  362.         {
  363.                 loaded = OS_READHANDLE(netbuf, fp3, sizeof(netbuf));
  364.                 adr = 0xC100 + loop;
  365.                 memcpy((unsigned char *)(adr), &netbuf, loaded);
  366.                 loop = loop + loaded;
  367.         }
  368.         OS_CLOSEHANDLE(fp3);
  369.         SETPG32KHIGH(pgbak);
  370.         clearStatus();
  371.         AT(1, 24);
  372.         printf("Shell [pId:%u][%s][%s]", shell_pg.pgs.pId, curPath, appCmd);
  373.         AT(1, 24);
  374.         delay(300);
  375.         OS_RUNAPP(shell_pg.pgs.pId);
  376.         AT(1, 4);
  377.         OS_WAITPID(shell_pg.pgs.pId);
  378.         return shell_pg.pgs.pId;
  379. }
  380.  
  381. ///////////////////////////
  382. #include <../common/esp-com.c>
  383. #include <../common/network.c>
  384. //////////////////////////
  385.  
  386. unsigned char saveBuf(unsigned char *fileNamePtr, unsigned char operation, unsigned int sizeOfBuf)
  387. {
  388.         switch (operation)
  389.         {
  390.         case 00:
  391.                 fp2 = OS_CREATEHANDLE(fileNamePtr, 0x80);
  392.                 if (((int)fp2) & 0xff)
  393.                 {
  394.                         clearStatus();
  395.                         AT(1, 24);
  396.                         printf("%s", fileNamePtr);
  397.                         printf(" creating error.");
  398.                         exit(0);
  399.                 }
  400.                 OS_CLOSEHANDLE(fp2);
  401.  
  402.                 fp2 = OS_OPENHANDLE(fileNamePtr, 0x80);
  403.                 if (((int)fp2) & 0xff)
  404.                 {
  405.                         clearStatus();
  406.                         AT(1, 24);
  407.                         printf("%s", fileNamePtr);
  408.                         printf(" opening error. ");
  409.  
  410.                         exit(0);
  411.                 }
  412.                 AT(1, 24);
  413.                 break;
  414.  
  415.         case 01:
  416.                 OS_WRITEHANDLE(netbuf + headlng, fp2, sizeOfBuf);
  417.                 break;
  418.  
  419.         default:
  420.                 OS_CLOSEHANDLE(fp2);
  421.         }
  422.  
  423.         return 0;
  424. }
  425.  
  426. unsigned int cutHeader(void)
  427. {
  428.         unsigned int err;
  429.         unsigned char *count1;
  430.  
  431.         err = httpError();
  432.         if (err != 200)
  433.         {
  434.                 BOX(1, 1, 80, 25, 40, 32);
  435.                 AT(1, 1);
  436.                 printf("HTTP ERROR %u", err);
  437.                 puts("^^^^^^^^^^^^^^^^^^^^^");
  438.                 puts(netbuf);
  439.                 getchar();
  440.                 fatalError("Server response error!");
  441.         }
  442.         count1 = strstr(netbuf, "Content-Length:");
  443.         if (count1 == NULL)
  444.         {
  445.                 printf("contLen  not found \r\n");
  446.                 contLen = 0;
  447.         }
  448.         else
  449.         {
  450.                 contLen = atol(count1 + 15);
  451.                 // printf("Content-Length: %lu \n\r", contLen);
  452.         }
  453.  
  454.         count1 = strstr(netbuf, "\r\n\r\n");
  455.         if (count1 == NULL)
  456.         {
  457.                 printf("header not found\r\n");
  458.         }
  459.         else
  460.         {
  461.                 // printf("header %u bytes\r\n", headlng);
  462.         }
  463.         return ((unsigned int)count1 - (unsigned int)netbuf + 4);
  464. }
  465.  
  466. char testOperation3(const char *process, int socket)
  467. {
  468.         if (socket < 0)
  469.         {
  470.                 clearStatus();
  471.                 getErrorText(-socket, cmd);
  472.                 printf("%s: [ERROR:%s]          ", process, cmd);
  473.                 YIELD();
  474.                 return false;
  475.         }
  476.         return true;
  477. }
  478.  
  479. unsigned char getFileNet(const unsigned char *fileLink, unsigned char *fileNamePtr)
  480. {
  481.         int todo;
  482.         char socket, firstPacket;
  483.         unsigned int fileSize1;
  484.         unsigned long downloaded = 0;
  485.         unsigned int down;
  486.         sprintf(netbuf, "GET %s%s", fileLink, userAgent);
  487.         socket = OpenSock(AF_INET, SOCK_STREAM);
  488.         if (!testOperation3("OS_NETSOCKET", socket))
  489.         {
  490.                 return false;
  491.         }
  492.  
  493.         todo = netConnect(socket, 1);
  494.         if (!testOperation3("OS_NETCONNECT", todo))
  495.         {
  496.                 return false;
  497.         }
  498.  
  499.         todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), 1);
  500.         if (!testOperation3("OS_WIZNETWRITE", todo))
  501.         {
  502.                 return false;
  503.         }
  504.  
  505.         firstPacket = true;
  506.  
  507.         do
  508.         {
  509.                 headlng = 0;
  510.                 todo = tcpRead(socket, 1);
  511.                 if (!testOperation3("OS_WIZNETREAD", todo))
  512.                 {
  513.                         return false;
  514.                 }
  515.  
  516.                 if (firstPacket)
  517.                 {
  518.                         firstPacket = false;
  519.                         headlng = cutHeader();
  520.                         todo = todo - headlng;
  521.                         fileSize1 = contLen / 1024;
  522.                         saveBuf(fileNamePtr, 00, 0);
  523.                 }
  524.  
  525.                 downloaded = downloaded + todo;
  526.                 down = downloaded / 1024;
  527.                 printf("\r %5u of %5u kb     ", down, fileSize1);
  528.                 saveBuf(fileNamePtr, 01, todo);
  529.                 if (_low_level_get() == 27)
  530.                 {
  531.                         saveBuf(fileNamePtr, 02, 00);
  532.                         fatalError("Updating aborted!");
  533.                 }
  534.         } while (downloaded < contLen);
  535.         netShutDown(socket, 0);
  536.         saveBuf(fileNamePtr, 02, 00);
  537.  
  538.         if (downloaded != contLen)
  539.         {
  540.                 return false;
  541.         }
  542.         return true;
  543. }
  544.  
  545. unsigned char getFileEsp(const unsigned char *fileLink, unsigned char *fileNamePtr)
  546. {
  547.         int todo;
  548.         unsigned char firstPacket = true;
  549.         unsigned int fileSize1;
  550.         unsigned long downloaded = 0;
  551.         unsigned int down, byte;
  552.         unsigned int sizeLink;
  553.         const unsigned char *count1;
  554.  
  555.         do
  556.         {
  557.                 sendcommand("AT+CIPSTART=\"TCP\",\"nedoos.ru\",80");
  558.  
  559.                 if (!getAnswer3()) // "CONNECT"
  560.                 {
  561.                         writeLog("Timeout 'AT+CIPSTART' return false", "getFileEsp     ");
  562.                         return false;
  563.                 }
  564.                 count1 = strstr(netbuf, "CONNECT");
  565.                 if (count1 == NULL)
  566.                 {
  567.                         writeLog("Error in AT+CIPSTART. Not 'CONNECT'.", "getFileEsp     ");
  568.                         clearStatus();
  569.                         espReBoot();
  570.                         delayLongKey(1000);
  571.                 }
  572.                 else
  573.                 {
  574.                         break;
  575.                 }
  576.         } while (42);
  577.  
  578.         sprintf(link, "GET %s%s", fileLink, userAgent);
  579.         sizeLink = strlen(link);
  580.  
  581.         sprintf(cmd, "AT+CIPSEND=%u", sizeLink + 2); // second CRLF in send command
  582.         sendcommand(cmd);
  583.         do
  584.         {
  585.                 byte = uartReadBlock();
  586.                 if (byte > 255)
  587.                 {
  588.                         writeLog("Timeout when waiting '>' ", "getFileEsp     ");
  589.                         return false;
  590.                 }
  591.                 // putchar(byte);
  592.         } while (byte != '>');
  593.  
  594.         sendcommand(link);
  595.  
  596.         do
  597.         {
  598.                 clearNetBuf(255);
  599.                 headlng = 0;
  600.                 todo = recvHead();
  601.  
  602.                 if (todo == 0)
  603.                 {
  604.                         writeLog("Error parsing packet size, todo = 0", "getFileEsp     ");
  605.                         writeLog(netbuf, "getFileEsp     ");
  606.                         return false;
  607.                 }
  608.  
  609.                 if (!getdataEsp(todo))
  610.                 {
  611.                         saveBuf(fileNamePtr, 01, todo);
  612.                         saveBuf(fileNamePtr, 02, 00);
  613.                         writeLog("Downloading timeout. Exit!", "getFileEsp     ");
  614.                         fatalError("[getdataEsp]Downloading timeout. Exit!");
  615.                 }
  616.  
  617.                 if (firstPacket)
  618.                 {
  619.                         firstPacket = false;
  620.                         headlng = cutHeader();
  621.                         todo = todo - headlng;
  622.                         fileSize1 = contLen / 1024;
  623.                         saveBuf(fileNamePtr, 00, 0);
  624.                 }
  625.                 downloaded = downloaded + todo;
  626.                 down = downloaded / 1024;
  627.                 printf("\r %5u of %5u kb     ", down, fileSize1);
  628.                 saveBuf(fileNamePtr, 01, todo);
  629.                 if (_low_level_get() == 27)
  630.                 {
  631.                         saveBuf(fileNamePtr, 02, 00);
  632.                         fatalError("Updating aborted! Exit.");
  633.                 }
  634.  
  635.         } while (downloaded < contLen);
  636.         saveBuf(fileNamePtr, 02, 00);
  637.         sendcommand("AT+CIPCLOSE");
  638.  
  639.         if (!getAnswer3()) // CLOSED or ERROR
  640.         {
  641.                 writeLog("Timeout  waiting CLOSED or ERROR continue", "fillPictureEsp ");
  642.         }
  643.  
  644.         count1 = strstr(netbuf, "CLOSED");
  645.         if (count1 != NULL)
  646.         {
  647.                 if (!getAnswer3()) // OK
  648.                 {
  649.                         writeLog("Timeout  waiting OK after CLOSED continue", "fillPictureEsp ");
  650.                 }
  651.         }
  652.  
  653.         return true;
  654. }
  655.  
  656. unsigned char getFile(const unsigned char *fileLink, unsigned char *fileNamePtr)
  657. {
  658.         char result;
  659.         clearStatus();
  660.         AT(26, 24);
  661.         printf("%s", fileNamePtr);
  662.  
  663.         switch (netDriver)
  664.         {
  665.         case 0:
  666.                 result = getFileNet(fileLink, fileNamePtr);
  667.                 break;
  668.         case 1:
  669.                 result = getFileEsp(fileLink, fileNamePtr);
  670.                 break;
  671.         }
  672.  
  673.         if (!result)
  674.         {
  675.                 sprintf(cmd, "%s download error!", fileNamePtr);
  676.                 fatalError(cmd);
  677.         }
  678.  
  679.         return false;
  680. }
  681.  
  682. unsigned char getConfig(void)
  683. {
  684.         config.is_atm = (unsigned char)OS_GETCONFIG();
  685.         // H=system drive, L= 1-Evo 2-ATM2 3-ATM3 6-p2.666 ;E=pgsys(system page) D= TR-DOS page
  686.         switch ((config.is_atm))
  687.         {
  688.         case 1:
  689.                 strcpy(config.machineName, "ZX-Evolution");
  690.                 strcpy(config.kernelName, "sd_boot.$C");
  691.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/sd_boot.%24C");
  692.                 break;
  693.         case 2:
  694.                 strcpy(config.machineName, "TURBO 2+");
  695.                 strcpy(config.kernelName, "osatm2hd.$C");
  696.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/osatm2hd.%24C");
  697.  
  698.                 break;
  699.  
  700.         case 3: // SD HDD versions
  701.                 strcpy(config.machineName, "TURBO 3 [SD]");
  702.                 strcpy(config.kernelName, "osatm3hd.$C");
  703.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/osatm3hd.%24C");
  704.                 break;
  705.         case 6: // SD HDD versions
  706.                 strcpy(config.machineName, "P2.666 [SD]");
  707.                 strcpy(config.kernelName, "osp26sd.$C");
  708.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/osp26sd.%24C");
  709.                 break;
  710.  
  711.         default:
  712.                 strcpy(config.machineName, "NOT DETECED (ZX-Evo)");
  713.                 strcpy(config.kernelName, "sd_boot.$C");
  714.                 strcpy(config.kernelLink, "/svn/dl.php?repname=NedoOS&path=/release/sd_boot.s%24C");
  715.                 break;
  716.         }
  717.         return config.is_atm;
  718. }
  719.  
  720. // Downloading minimal tools for updating/boot
  721. void getTools(void)
  722. {
  723.         ATRIB(cw.text);
  724.         ATRIB(cw.back);
  725.         getFile(wizNetLink, "bin.new/wizcfg.com");
  726.         getFile(newsLink, "updater.new");
  727.         getFile(pkunzipLink, "bin.new/pkunzip.com");
  728.         getFile(tarLink, "bin.new/tar.com");
  729.         getFile(cmdLink, "bin.new/cmd.com");
  730.         getFile(termLink, "bin.new/term.com");
  731.         getFile(updLink, "bin.new/updater.com");
  732.         getFile(netIniLink, "bin.new/net.ini");
  733. }
  734.  
  735. unsigned char ren2old(unsigned char *name)
  736. {
  737.         unsigned char counter = 255; // For OLD must be 255
  738.         OS_MKDIR((void *)name);
  739.         sprintf(nameBuf, "%s.old", name);
  740.         while (OS_RENAME((void *)name, (void *)nameBuf) != 0)
  741.         {
  742.                 counter++;
  743.                 sprintf(nameBuf, "%s.%u", name, counter);
  744.                 if (counter == 255)
  745.                 {
  746.                         fatalError("Unable to rename old folder");
  747.                 }
  748.         }
  749.         return counter;
  750. }
  751.  
  752. /*
  753. void ren2tar(void)
  754. {
  755.         unsigned int counter = 2400;
  756.         do
  757.         {
  758.                 sprintf(nameBuf, "bin.r%u", counter);
  759.  
  760.                 errn = OS_RENAME((void *)nameBuf, "bin.tar");
  761.                 counter--;
  762.                 if (counter < 2000)
  763.                 {
  764.                         fatalError("Unable to rename TAR file");
  765.                 }
  766.         } while (errn != 0);
  767. }
  768. */
  769.  
  770. /*
  771. void ren2bin(void)
  772. {
  773.         unsigned char counter = 20;
  774.         sprintf(nameBuf, "bin.r%u", counter);
  775.         do
  776.         {
  777.                 errn = OS_RENAME((void *)nameBuf, "bin");
  778.                 counter++;
  779.                 sprintf(nameBuf, "bin.r%u", counter);
  780.                 if (counter > 99)
  781.                 {
  782.                         fatalError("Unable to rename BIN folder");
  783.                 }
  784.         } while (errn != 0);
  785. }
  786. */
  787.  
  788. void restoreConfig(unsigned char oldBinExt)
  789. {
  790.         unsigned char count;
  791.         errn = OS_CHDIR("/");
  792.         errn = OS_RENAME("bin/autoexec.bat", "bin/autoexec.bat.new");
  793.         errn = OS_RENAME("bin/net.ini", "bin/net.ini.new");
  794.         errn = OS_RENAME("bin/nv.ext", "bin/nv.ext.new");
  795.         errn = OS_RENAME("bin/gp/gp.ini", "bin/gp/gp.ini.new");
  796.         errn = OS_RENAME("/bin/browser/index.gph", "/bin/browser/index.gph.new");
  797.         if (oldBinExt == 255)
  798.         {
  799.                 errn = OS_SHELL("copy bin.old/autoexec.bat bin/autoexec.bat");
  800.                 errn = OS_SHELL("copy bin.old/net.ini bin/net.ini");
  801.                 errn = OS_SHELL("copy bin.old/nv.ext bin/nv.ext");
  802.                 errn = OS_SHELL("copy bin.old/gp/gp.ini bin/gp/gp.ini");
  803.                 errn = OS_SHELL("copy bin.old/browser/index.gph bin/browser/index.gph");
  804.         }
  805.         else
  806.         {
  807.                 sprintf(nameBuf, "copy bin.%u/autoexec.bat bin/autoexec.bat", oldBinExt);
  808.                 OS_SHELL((void *)nameBuf);
  809.                 sprintf(nameBuf, "copy bin.%u/net.ini bin/net.ini", oldBinExt);
  810.                 OS_SHELL((void *)nameBuf);
  811.                 sprintf(nameBuf, "copy bin.%u/nv.ext bin/nv.ext", oldBinExt);
  812.                 OS_SHELL((void *)nameBuf);
  813.                 sprintf(nameBuf, "copy bin.%u/gp/gp.ini bin/gp/gp.ini", oldBinExt);
  814.                 OS_SHELL((void *)nameBuf);
  815.                 sprintf(nameBuf, "copy bin.%u/browser/index.gph bin/browser/index.gph", oldBinExt);
  816.                 OS_SHELL((void *)nameBuf);
  817.         }
  818.         AT(1, 4);
  819.         ATRIB(40);
  820.         for (count = 0; count < 15; count++)
  821.         {
  822.                 putchar(176);
  823.         }
  824.         errn = OS_RENAME("bin/autoexec.bat.new", "bin/autoexec.bat"); // If file already exist we dont rename
  825.         errn = OS_RENAME("bin/net.ini.new", "bin/net.ini");
  826.         errn = OS_RENAME("bin/nv.ext.new", "bin/nv.ext");
  827.         errn = OS_RENAME("bin/gp/gp.ini.new", "bin/gp/gp.ini");
  828.         errn = OS_RENAME("bin/browser/index.gph.new", "bin/browser/index.gph");
  829. }
  830.  
  831. // Download, backup, unpack release.zip
  832. void fullUpdate(void)
  833. {
  834.         BOX(1, 1, 80, 25, 40, 176);
  835.         cw.x = 20;
  836.         cw.y = 5;
  837.         cw.w = 40;
  838.         cw.h = 10;
  839.         cw.text = 97;
  840.         cw.back = 45;
  841.  
  842.         AT(1, 1);
  843.         ATRIB(cw.text);
  844.         ATRIB(cw.back);
  845.         printf("[%u:%u]              [FULL UPDATE - UPDATING ALL SYSTEM FILES]    [Build:%s]", netDriver, comType, __TIME__);
  846.         strcpy(cw.tittle, "nedoOS FULL updater ");
  847.         strcat(cw.tittle, uVer);
  848.         getConfig();
  849.  
  850.         strcat(cw.tittle, " (");
  851.         strcat(cw.tittle, config.machineName);
  852.         strcat(cw.tittle, ")");
  853.         drawWindow(cw);
  854.  
  855.         OS_CHDIR("/");
  856.         OS_DELETE("release.zip");
  857.         OS_SHELL("time2 >updlog.txt");
  858.  
  859.         clearStatus();
  860.         AT(cw.x + 2, cw.y + 3);
  861.         printf("1. Downloading release.zip.");
  862.         YIELD();
  863.         errn = getFile(relLink, "release.zip"); //  Downloading the file
  864.         OS_SHELL("time2 >>updlog.txt");
  865.         AT(cw.x + 2, cw.y + 4);
  866.         printf("2. Downloading pkunzip.com.");
  867.         YIELD();
  868.         getFile(pkunzipLink, "pkunzip.com");
  869.         getFile(cmdLink, "cmd.com");
  870.         getFile(newsLink, "updater.new");
  871.         clearStatus();
  872.         AT(cw.x + 2, cw.y + 5);
  873.         printf("3. Backuping old system.");
  874.         YIELD();
  875.         oldBinExt = ren2old("bin");
  876.         ren2old("doc");
  877.         ren2old("ini");
  878.         ren2old("nedodemo");
  879.         ren2old("nedogame");
  880.         BOX(1, 1, 80, 25, 40, 32);
  881.         AT(1, 1);
  882.         printf("4. Unpacking release.zip. Its take about 5 hours. Please wait.\r\n");
  883.         printNews();
  884.         YIELD();
  885.         OS_MKDIR("bin");
  886.         OS_RENAME("cmd.com", "bin/cmd.com");
  887.         OS_SHELL("pkunzip.com release.zip");
  888.  
  889.         BOX(1, 1, 80, 25, 40, 176);
  890.         drawWindow(cw);
  891.         printf("[%u:%u]              [FULL UPDATE - UPDATING ALL SYSTEM FILES]    [Build:%s]", netDriver, comType, __TIME__);
  892.         clearStatus();
  893.         AT(cw.x + 2, cw.y + 3);
  894.         printf("1. Downloading release.zip.");
  895.         AT(cw.x + 2, cw.y + 4);
  896.         printf("2. Downloading pkunzip.com.");
  897.         AT(cw.x + 2, cw.y + 5);
  898.         printf("3. Backuping old system.");
  899.         AT(cw.x + 2, cw.y + 6);
  900.         printf("4. Unpacking release.zip.");
  901.         AT(cw.x + 2, cw.y + 7);
  902.         printf("5. Restoring configs.");
  903.         YIELD();
  904. }
  905.  
  906. // Updating only BIN folders, where is OS lives.
  907. void binUpdate(void)
  908. {
  909.         BOX(1, 1, 80, 25, 40, 176);
  910.         cw.x = 20;
  911.         cw.y = 5;
  912.         cw.w = 40;
  913.         cw.h = 13;
  914.         cw.text = 97;
  915.         cw.back = 44;
  916.  
  917.         AT(1, 1);
  918.         ATRIB(cw.text);
  919.         ATRIB(cw.back);
  920.         printf("[%u:%u]            [STANDART UPDATE - UPDATING ONLY BIN FOLDER]   [Build:%s]", netDriver, comType, __TIME__);
  921.         strcpy(cw.tittle, "nedoOS BIN updater ");
  922.         strcat(cw.tittle, uVer);
  923.         getConfig();
  924.         strcat(cw.tittle, " (");
  925.         strcat(cw.tittle, config.machineName);
  926.         strcat(cw.tittle, ")");
  927.         drawWindow(cw);
  928.  
  929.         clearStatus();
  930.         AT(cw.x + 2, cw.y + 10);
  931.         printf(">To full update start 'updater.com F'<");
  932.         YIELD();
  933.  
  934.         OS_CHDIR("/");
  935.         OS_DELETE("bin.new");
  936.         if (OS_MKDIR("bin.new") != 0)
  937.         {
  938.                 fatalError("Please delete 'bin.new' and try again");
  939.         }
  940.  
  941.         OS_SHELL("time2 >updlog.txt");
  942.  
  943.         AT(cw.x + 2, cw.y + 3);
  944.         printf("1. Downloading bin.zip.");
  945.         YIELD();
  946.         getFile(binLink, "bin.new/bin.zip"); //  Downloading the file
  947.         OS_SHELL("time2 >>updlog.txt");
  948.         clearStatus();
  949.         AT(cw.x + 2, cw.y + 4);
  950.         printf("2. Downloading tools.");
  951.         getTools();
  952.  
  953.         BOX(1, 1, 80, 25, 40, 32);
  954.         AT(1, 1);
  955.         printf("3. Unpacking bin.zip. Its take about 10 minutes. Please wait.\r\n");
  956.         printNews();
  957.         YIELD();
  958.  
  959.         OS_CHDIR("bin.new");
  960.         OS_SHELL("pkunzip.com bin.zip");
  961.  
  962.         BOX(1, 1, 80, 25, 40, 176);
  963.         AT(1, 1);
  964.         ATRIB(cw.text);
  965.         ATRIB(cw.back);
  966.         printf("[%u:%u]            [STANDART UPDATE - UPDATING ONLY BIN FOLDER]   [Build:%s]", netDriver, comType, __TIME__);
  967.         drawWindow(cw);
  968.  
  969.         ATRIB(cw.text);
  970.         ATRIB(cw.back);
  971.         AT(cw.x + 2, cw.y + 3);
  972.         puts("1. Downloading bin.zip.");
  973.         AT(cw.x + 2, cw.y + 4);
  974.         puts("2. Downloading tools.");
  975.         AT(cw.x + 2, cw.y + 5);
  976.         puts("3. Unpacking bin.zip.");
  977.         AT(cw.x + 2, cw.y + 6);
  978.         puts("4. Backuping bin to bin.old.");
  979.         YIELD();
  980.         clearStatus();
  981.         OS_CHDIR("/");
  982.         oldBinExt = ren2old("bin");
  983.         AT(cw.x + 2, cw.y + 7);
  984.         puts("5. Renaming bin.new to bin.");
  985.         YIELD();
  986.         clearStatus();
  987.         OS_RENAME("bin.new", "bin");
  988.         YIELD();
  989.         clearStatus();
  990.         AT(cw.x + 2, cw.y + 8);
  991.         printf("6. Downloading kernel [%s].", config.machineName);
  992.         errn = OS_CHDIR("/");
  993.         errn = getFile(config.kernelLink, config.kernelName); //  Downloading the file
  994.         AT(cw.x + 2, cw.y + 9);
  995.         ATRIB(cw.text);
  996.         ATRIB(cw.back);
  997.         printf("7. Restoring configs."); // in main loop
  998. }
  999.  
  1000. char readParamFromIni(void)
  1001. {
  1002.         FILE *fpini;
  1003.         unsigned char *count1;
  1004.         const char currentNetwork[] = "currentNetwork";
  1005.         unsigned char curNet = 0;
  1006.  
  1007.         OS_GETPATH((unsigned int)&curPath);
  1008.         OS_SETSYSDRV();
  1009.         OS_CHDIR("/");
  1010.         OS_CHDIR("ini");
  1011.  
  1012.         fpini = OS_OPENHANDLE("network.ini", 0x80);
  1013.         if (((int)fpini) & 0xff)
  1014.         {
  1015.                 clearStatus();
  1016.                 printf("network.ini not found.\r\n");
  1017.                 getchar();
  1018.                 return false;
  1019.         }
  1020.  
  1021.         OS_READHANDLE(netbuf, fpini, sizeof(netbuf) - 1);
  1022.         OS_CLOSEHANDLE(fpini);
  1023.  
  1024.         count1 = strstr(netbuf, currentNetwork);
  1025.         if (count1 != NULL)
  1026.         {
  1027.                 sscanf(count1 + strlen(currentNetwork) + 1, "%u", &curNet);
  1028.         }
  1029.  
  1030.         OS_CHDIR(curPath);
  1031.         return curNet;
  1032. }
  1033.  
  1034. C_task main(int argc, const char *argv[])
  1035. {
  1036.         unsigned char test;
  1037.         os_initstdio();
  1038.  
  1039.         targetadr.family = AF_INET;
  1040.         targetadr.porth = 00;
  1041.         targetadr.portl = 80;
  1042.         targetadr.b1 = 31;
  1043.         targetadr.b2 = 31;
  1044.         targetadr.b3 = 65;
  1045.         targetadr.b4 = 35;
  1046.  
  1047.         netDriver = readParamFromIni();
  1048.         clearStatus();
  1049.         // puts("network.ini loaded.");
  1050.         // YIELD();
  1051.         OS_GETPATH((unsigned int)&curPath);
  1052.         curLetter = curPath[0];
  1053.  
  1054.         if (argc > 1)
  1055.         {
  1056.                 if (argv[1][0] == 'F')
  1057.                 {
  1058.                         netDriver = 0;
  1059.                         get_dns();
  1060.                         test = dnsResolve("nedoos.ru");
  1061.                         if (test)
  1062.                         {
  1063.                                 fullUpdate();
  1064.                         }
  1065.                 }
  1066.                 else if (argv[1][0] == 'e')
  1067.                 {
  1068.                         netDriver = 1;
  1069.                         clearStatus();
  1070.                         printf("    ESP-COM mode enabled...");
  1071.                         loadEspConfig();
  1072.                         uart_init(divider);
  1073.                         espReBoot();
  1074.                         binUpdate();
  1075.                 }
  1076.                 else if (argv[1][0] == 'E')
  1077.                 {
  1078.                         netDriver = 1;
  1079.                         clearStatus();
  1080.                         printf("    ESP-COM mode enabled...");
  1081.                         loadEspConfig();
  1082.                         uart_init(divider);
  1083.                         espReBoot();
  1084.                         fullUpdate();
  1085.                 }
  1086.                 else
  1087.                 {
  1088.                         AT(1, 1);
  1089.                         // printTable();
  1090.                         //  printNews();
  1091.                         // getchar();
  1092.                         fatalError("Use 'F' key to FULL update");
  1093.                 }
  1094.         }
  1095.         else
  1096.         {
  1097.  
  1098.                 if (netDriver == 0)
  1099.                 {
  1100.                         get_dns();
  1101.                         if (dnsResolve("nedoos.ru"))
  1102.                         {
  1103.                                 binUpdate();
  1104.                         }
  1105.                         else
  1106.                         {
  1107.                                 fatalError("Check connection to the nedoos.ru server!");
  1108.                         }
  1109.                 }
  1110.                 else if (netDriver == 1)
  1111.                 {
  1112.                         loadEspConfig();
  1113.                         uart_init(divider);
  1114.                         espReBoot();
  1115.                         binUpdate();
  1116.                 }
  1117.         }
  1118.         restoreConfig(oldBinExt);
  1119.         OS_DELETE("bin/bin.zip");
  1120.         OS_DELETE("pkunzip.com");
  1121.         clearStatus();
  1122.         infoBox("System Updated successfully!");
  1123.         OS_SHELL("time2 >>updlog.txt");
  1124.         delayLongKey(5000);
  1125.         // getchar();
  1126.         //   OS_DELETE("release.zip");
  1127.         quit();
  1128. }
  1129.