Subversion Repositories NedoOS

Rev

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