#include <stdio.h>
#include <string.h>
#include <intrz80.h>
#include <stdlib.h>
#include <oscalls.h>
#include <terminal.c>
#include <tcp.h>
#include <osfs.h>
#include <intrz80.h>
#include <ctype.h>
#include <math.h>
unsigned char is_atm;
unsigned int errn;
unsigned long contLen;
unsigned char saveFlag, saveBak;
unsigned char crlf[2] = {13, 10};
unsigned long bytecount, downloaded;
unsigned char status, key, curFormat;
struct sockaddr_in targetadr;
struct readstructure readStruct;
FILE *fp2;
struct window
{
unsigned char x;
unsigned char y;
unsigned char w;
unsigned char h;
unsigned char text;
unsigned char back;
unsigned char tittle[80];
} cw;
unsigned char kernelName[32];
unsigned char machineName[32];
unsigned char kernelLink[256];
unsigned int bufSize = 2000;
unsigned char netbuf[2048];
unsigned char netbuf2[2048];
void clearStatus(void)
{
AT(1, 24);
}
void printTable(void)
{
unsigned int cycle;
for (cycle = 32; cycle < 256; cycle++)
{
if (cycle % 10 == 0)
{
}
}
}
void delay(unsigned long counter)
{
unsigned long start, finish;
counter = counter / 20;
if (counter < 1)
{
counter = 1;
}
finish = start + counter;
while (start < finish)
{
}
}
void drawWindow(struct window w)
{
unsigned char wcount, tempx, tittleStart;
AT(w.x, w.y - 1);
tittleStart
= w.
x + (w.
w / 2) - (strlen(w.
tittle) / 2) + 1;
BOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
AT(w.x, w.y);
ATRIB(w.text);
for (wcount = 0; wcount < w.w; wcount++)
{
}
AT(w.x, w.y + w.h);
for (wcount = 0; wcount < w.w; wcount++)
{
}
tempx = w.x + w.w + 1;
for (wcount = 1; wcount < w.h; wcount++)
{
AT(w.x, w.y + wcount);
AT(tempx, w.y + wcount);
}
AT(w.x, w.y + 2);
for (wcount = 0; wcount < w.w; wcount++)
{
}
AT(tittleStart, w.y + 1);
}
void fatalError(unsigned char *message)
{
strcpy(cw.
tittle, "FATAL ERROR!");
{
}
else
cw.x = 80 / 2 - cw.w / 2;
cw.y = 11;
cw.h = 4;
cw.text = 97;
cw.back = 41;
drawWindow(cw);
AT(cw.x + 2, cw.y + 3);
AT(1, 1);
}
void infoBox(unsigned char *message)
{
strcpy(cw.
tittle, "nedoOS system updater 0.1");
{
}
else
cw.x = 80 / 2 - cw.w / 2;
cw.y = 15;
cw.h = 4;
cw.text = 97;
cw.back = 42;
drawWindow(cw);
AT(cw.x + 2, cw.y + 3);
AT(1, 1);
}
unsigned char OS_SHELL(unsigned char *command)
{
unsigned char fileName[] = "cmd.com";
unsigned char appCmd[128] = "cmd.com ";
unsigned char diskBuf[1024];
unsigned int shellSize, loaded, loop;
unsigned char pgbak;
union APP_PAGES shell_pg;
union APP_PAGES main_pg;
unsigned char curPath[256];
main_pg.l = OS_GETMAINPAGES();
pgbak = main_pg.pgs.window_0;
OS_GETPATH((unsigned int)&curPath);
OS_SETSYSDRV();
fp2 = OS_OPENHANDLE(fileName, 0x80);
if (((int)fp2) & 0xff)
{
clearStatus();
AT(1, 24);
}
shellSize = OS_GETFILESIZE(fp2);
OS_CHDIR(curPath);
OS_NEWAPP((unsigned int)&shell_pg);
SETPG32KHIGH(shell_pg.pgs.window_3);
memcpy((char *)(0xC080), &appCmd
, sizeof(appCmd
));
for (loop = 0; loop < shellSize; loop = loop + loaded)
{
loaded = OS_READHANDLE(diskBuf, fp2, sizeof(diskBuf));
memcpy((char *)(0xC100 + loop
), &diskBuf
, loaded
);
}
OS_CLOSEHANDLE(fp2);
SETPG32KHIGH(pgbak);
clearStatus();
AT(1, 24);
printf("Running shell [pId:%u][%s][%s]", shell_pg.
pgs.
pId, curPath
, appCmd
);
YIELD();
delay(250);
OS_RUNAPP(shell_pg.pgs.pId);
OS_WAITPID(shell_pg.pgs.pId);
return shell_pg.pgs.pId;
}
// #include <network.c>
void errorPrint(unsigned int error)
{
clearStatus();
AT(1, 24);
switch (error)
{
case 2:
break;
case 4:
break;
case 23:
break;
case 35:
break;
case 37:
break;
case 38:
break;
case 40:
break;
case 41:
break;
case 47:
break;
case 53:
printf("53 ERR_ECONNABORTED");
break;
case 54:
break;
case 57:
break;
case 65:
break;
default:
printf("[%u] UNKNOWN ERROR", error
);
break;
}
YIELD();
do
{
key = _low_level_get();
} while (key == 0);
}
unsigned char OpenSock(unsigned char family, unsigned char protocol)
{
unsigned char socket;
unsigned int todo;
todo = OS_NETSOCKET((family << 8) + protocol);
if (todo > 32767)
{
clearStatus();
AT(1, 24);
errorPrint(todo & 255);
}
else
{
socket = ((todo & 65280) >> 8);
}
return socket;
}
unsigned char netConnect(unsigned char socket)
{
unsigned int todo;
targetadr.family = AF_INET;
targetadr.porth = 00;
targetadr.portl = 80;
targetadr.b1 = 31;
targetadr.b2 = 31;
targetadr.b3 = 65;
targetadr.b4 = 35;
todo = OS_NETCONNECT(socket, &targetadr);
if (todo > 32767)
{
clearStatus();
AT(1, 24);
errorPrint(todo & 255);
}
return 0;
}
unsigned int tcpRead(unsigned char socket)
{
unsigned char retry = 250;
unsigned int err, todo;
readStruct.socket = socket;
readStruct.BufAdr = (unsigned int)&netbuf;
readStruct.bufsize = bufSize;
readStruct.protocol = SOCK_STREAM;
wizread:
todo = OS_WIZNETREAD(&readStruct);
if (todo > 32767)
{
if (retry == 0)
{
err = todo & 255;
clearStatus();
AT(1, 24);
errorPrint(err);
if (err == 35)
{
return 0;
}
fatalError("ERROR CONNECTION TO SERVER");
}
retry--;
YIELD();
YIELD();
delay(300);
YIELD();
YIELD();
goto wizread;
}
return todo;
}
unsigned int netShutDown(unsigned char socket)
{
unsigned int todo;
todo = OS_NETSHUTDOWN(socket);
if (todo > 32767)
{
errorPrint(todo & 255);
return 255;
}
return 0;
}
unsigned int cutHeader(unsigned int todo)
{
unsigned int q, headlng;
unsigned char *count;
count
= strstr(netbuf
, "Content-Length:");
if (count == NULL)
{
clearStatus();
AT(1, 24);
printf("Content-Length: not found.");
contLen = 0;
}
else
{
contLen
= atol(count
+ 15);
bytecount = contLen;
// AT (1,24);
// printf("=> Dlinna soderzhimogo = %lu \n\r", bytecount);
}
count
= strstr(netbuf
, "\r\n\r\n");
headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
q = todo - headlng;
memcpy(&netbuf
, count
+ 4, q
);
return q;
}
unsigned char saveBuf(unsigned char *fileNamePtr, unsigned char operation, unsigned int sizeOfBuf)
{
unsigned char fileName[255];
if (operation == 00)
{
strcpy(fileName
, fileNamePtr
);
fp2 = OS_CREATEHANDLE(fileName, 0x80);
if (((int)fp2) & 0xff)
{
clearStatus();
AT(1, 24);
}
OS_CLOSEHANDLE(fp2);
fp2 = OS_OPENHANDLE(fileName, 0x80);
if (((int)fp2) & 0xff)
{
clearStatus();
AT(1, 24);
}
AT(1, 24);
return 0;
}
if (operation == 01)
{
OS_WRITEHANDLE(netbuf, fp2, sizeOfBuf);
downloaded = downloaded + sizeOfBuf;
return 0;
}
if (operation == 02)
{
OS_CLOSEHANDLE(fp2);
return 0;
}
return 0;
}
unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
{
unsigned char retry = 20;
unsigned int todo;
readStruct.socket = socket;
readStruct.BufAdr = messageadr;
readStruct.bufsize = size;
readStruct.protocol = SOCK_STREAM;
wizwrite:
todo = OS_WIZNETWRITE(&readStruct);
if (todo > 32767)
{
clearStatus();
AT(1, 24);
errorPrint(todo & 255);
if (retry == 0)
{
}
retry--;
YIELD();
delay(250);
goto wizwrite;
}
return todo;
}
unsigned char getFile(unsigned char *fileLink, unsigned char *fileNamePtr)
{
unsigned int todo;
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";
unsigned char socket;
unsigned int bytes2read, headskip;
clearStatus();
socket = OpenSock(AF_INET, SOCK_STREAM);
todo = netConnect(socket);
todo
= tcpSend
(socket
, (unsigned int)&netbuf
, strlen(netbuf
));
headskip = 0;
bytecount = 255;
downloaded = 0;
saveBuf(fileNamePtr, 00, 0);
AT(1, 24);
while (bytecount != 0)
{
todo = tcpRead(socket);
if (todo == 0)
{
break;
}
bytes2read = todo;
if (headskip == 0)
{
headskip = 1;
bytes2read = cutHeader(todo);
}
AT(34, 24);
printf("%lu of %lu kb", downloaded
/ 1024, contLen
/ 1024);
saveBuf(fileNamePtr, 01, bytes2read);
bytecount = bytecount - bytes2read;
key = _low_level_get();
if (key == 27)
{
saveBuf(fileNamePtr, 02, 00);
fatalError("File download aborted!");
}
}
saveBuf(fileNamePtr, 02, 00);
netShutDown(socket);
if (downloaded != contLen)
{
fatalError("File download error!");
}
return 0;
}
////////////////////////////////////////////////////
unsigned char getConfig(void)
{
is_atm = (unsigned char)OS_GETCONFIG();
// H=system drive, L= 1-Evo 2-ATM2 3-ATM3 6-p2.666 ;E=pgsys(system page) D= TR-DOS page
switch ((is_atm))
{
case 1:
strcpy(machineName
, "ZX-Evolution");
strcpy(kernelName
, "sd_boot.$C");
strcpy(kernelLink
, "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fsd_boot.%24C");
break;
case 2:
strcpy(machineName
, "TURBO 2+");
strcpy(kernelName
, "osatm2hd.$C");
strcpy(kernelLink
, "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fosatm2hd.%24C");
break;
case 3: // SD HDD versions
strcpy(machineName
, "TURBO 3 [SD]");
strcpy(kernelName
, "osatm3hd.$C");
strcpy(kernelLink
, "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fosatm3hd.%24C");
break;
case 6: // SD HDD versions
strcpy(machineName
, "P2.666 [SD]");
strcpy(kernelName
, "osp26sd.$C");
strcpy(kernelLink
, "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fosp26sd.%24C");
break;
default:
strcpy(machineName
, "NOT DETECED (ZX-Evo)");
strcpy(kernelName
, "sd_boot.$C");
strcpy(kernelLink
, "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fsd_boot.%24C");
break;
}
return is_atm;
}
void getTools(void)
{
unsigned char pkunzipLink[] = "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fbin%2Fpkunzip.com";
unsigned char tarLink[] = "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fbin%2Ftar.com";
unsigned char cmdLink[] = "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fbin%2Fcmd.com";
unsigned char termLink[] = "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fbin%2Fterm.com";
unsigned char updLink[] = "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fbin%2Fupdater.com";
errn = OS_MKDIR("bin");
ATRIB(cw.text);
ATRIB(cw.back);
errn = getFile(pkunzipLink, "bin/pkunzip.com");
errn = getFile(tarLink, "bin/tar.com");
errn = getFile(cmdLink, "bin/cmd.com");
errn = getFile(termLink, "bin/term.com");
errn = getFile(updLink, "bin/updater.com");
}
void deleteWorkFiles(void)
{
OS_DELETE("bin.zip");
OS_DELETE("bin.tar");
OS_DELETE("bin.old"); // deleting not empty folders not supported
OS_DELETE("bin.r17");
OS_DELETE("bin.r18");
OS_DELETE("bin.r19");
OS_DELETE("bin.r20");
}
void deleteTempBin(void)
{
OS_DELETE("bin/pkunzip.com");
OS_DELETE("bin/tar.com");
OS_DELETE("bin/cmd.com");
OS_DELETE("bin/term.com");
OS_DELETE("bin/updater.com");
OS_DELETE("bin");
}
void fullUpdate(void)
{
unsigned char relLink[] = "http://nedoos.ru/images/release.zip";
BOX(1, 1, 80, 25, 40, 176);
cw.x = 20;
cw.y = 5;
cw.w = 40;
cw.h = 10;
cw.text = 97;
cw.back = 45;
strcpy(cw.
tittle, "nedoOS FULL updater 0.1");
getConfig();
OS_SETSYSDRV();
errn = OS_CHDIR("..");
strcat(cw.
tittle, machineName
);
drawWindow(cw);
OS_DELETE("release.zip");
OS_DELETE("bin.old");
OS_DELETE("doc.old");
OS_DELETE("nedodemo.old");
OS_DELETE("nedogame.old");
clearStatus();
AT(cw.x + 2, cw.y + 3);
printf("Downloading file: release.zip");
errn = getFile(relLink, "release.zip"); // Downloading the file
clearStatus();
AT(cw.x + 2, cw.y + 4);
printf("Backuping old system.\r\n");
errn = OS_RENAME("bin", "bin.old");
errn = OS_RENAME("doc", "doc.old");
errn = OS_RENAME("nedodemo", "nedodemo.old");
errn = OS_RENAME("nedogame", "nedogame.old");
clearStatus();
AT(cw.x + 2, cw.y + 5);
printf("Downloading tools.\r\n");
getTools();
BOX(1, 1, 80, 25, 40, 32);
AT(1, 1);
printf("Depacking release. Its take about 10 hours. Please wait.\r\n");
YIELD();
OS_SHELL("pkunzip.com release.zip");
// BOX(1, 1, 80, 25, 40, 176);
// drawWindow(cw);
infoBox("System Updated successfully.");
ATRIB(40);
ATRIB(32);
}
void binUpdate(void)
{
unsigned char binLink[] = "/svn/dl.php?repname=NedoOS&path=%2Frelease%2Fbin%2F&isdir=1";
BOX(1, 1, 80, 25, 40, 176);
cw.x = 20;
cw.y = 5;
cw.w = 40;
cw.h = 10;
cw.text = 97;
cw.back = 44;
strcpy(cw.
tittle, "nedoOS BIN updater 0.1");
getConfig();
strcat(cw.
tittle, machineName
);
drawWindow(cw);
OS_SETSYSDRV();
errn = OS_CHDIR("..");
deleteWorkFiles();
clearStatus();
AT(cw.x + 2, cw.y + 3);
printf("Downloading bin.zip");
errn = getFile(binLink, "bin.zip"); // Downloading the file
clearStatus();
AT(cw.x + 2, cw.y + 4);
printf("Downloading tools...");
getTools();
BOX(1, 1, 80, 25, 40, 32);
AT(1, 1);
printf("Depacking release. Its take about 10 minutes. Please wait.\r\n");
YIELD();
OS_SHELL("pkunzip.com bin.zip");
BOX(1, 1, 80, 25, 40, 176);
drawWindow(cw);
AT(cw.x + 2, cw.y + 3);
ATRIB(cw.text);
ATRIB(cw.back);
printf("Renaming bin.r?? to bin.tar");
errn = OS_RENAME("bin.r17", "bin.tar"); // Masks not supported. Just some bad hardcode.
errn = OS_RENAME("bin.r18", "bin.tar");
errn = OS_RENAME("bin.r19", "bin.tar");
errn = OS_RENAME("bin.r20", "bin.tar");
AT(cw.x + 2, cw.y + 4);
ATRIB(cw.text);
ATRIB(cw.back);
printf("Untaring bin.tar, please wait");
OS_SHELL("tar.com bin.tar");
AT(cw.x + 2, cw.y + 5);
ATRIB(cw.text);
ATRIB(cw.back);
printf("Backuping old bin to bin.old");
errn = OS_RENAME("bin", "bin.old");
AT(cw.x + 2, cw.y + 6);
ATRIB(cw.text);
ATRIB(cw.back);
deleteTempBin();
errn = OS_RENAME("bin.r17", "bin"); // Masks not supported. Just some bad hardcode.
errn = OS_RENAME("bin.r18", "bin");
errn = OS_RENAME("bin.r19", "bin");
errn = OS_RENAME("bin.r20", "bin");
AT(cw.x + 2, cw.y + 7);
ATRIB(cw.text);
ATRIB(cw.back);
printf("Deleting zip & tar.");
deleteWorkFiles();
AT(cw.x + 2, cw.y + 8);
ATRIB(cw.text);
ATRIB(cw.back);
printf("Downloading kernel: %s", machineName
);
errn = getFile(kernelLink, kernelName); // Downloading the file
// clearStatus();
// ATRIB(cw.text);
// ATRIB(cw.back);
// AT(cw.x + 2, cw.y + 9);
// printf("Updating kernel [%s]", kernelName);
// OS_DELETE(kernelName);
// errn = OS_RENAME("kernel.tmp", kernelName);
// strcpy(kernelLink, "ren kernel.tmp ");
// strcat(kernelLink, kernelName);
// OS_SHELL(kernelLink);
clearStatus();
infoBox("System Updated successfully");
ATRIB(40);
ATRIB(32);
}
C_task main(int argc, char *argv[])
{
os_initstdio();
if (argc > 1)
{
if (argv[1] == "F")
{
fullUpdate();
}
else
{
fatalError("Use 'F' key to FULL update");
}
}
else
{
binUpdate();
}
}
/*
[BIN]
[DOC]
[NEDODEMO]
[NEDOGAME]
[kernel]
full release http://nedoos.ru/images/release.zip
Clean install
- Переименовать выбранные папки
- Создать новые папки
- Скачать архив выбранной папки
- Распаковать все содержимое.
Clean install config restore
- восстановление из old конфигов
*/