Subversion Repositories NedoOS

Rev

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