Subversion Repositories NedoOS

Rev

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