Subversion Repositories NedoOS

Rev

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

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