Subversion Repositories NedoOS

Rev

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