Subversion Repositories NedoOS

Rev

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