From 05ff3da0f88c5f6b03c43371e88fb1b22434a51e Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Wed, 6 Jan 2010 18:39:36 +0000 Subject: major patch for openbsd --- src/buffer.c | 72 +++++++++++++++++++++++++------------------------- src/dos_cpt.h | 28 +++++++++++++------- src/init.c | 84 +++++++++++++++++++++++++++++------------------------------ src/makefile | 58 ----------------------------------------- src/sp12.c | 41 ++++++++++++++++++----------- src/sp12.h | 6 ++++- 6 files changed, 128 insertions(+), 161 deletions(-) delete mode 100644 src/makefile diff --git a/src/buffer.c b/src/buffer.c index 5d37e14..4a3a424 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -394,26 +394,26 @@ int file_hex_ascii(FILE *filePtr, unsigned int buffer[], addressPerLine = 0x08; loByte = (unsigned char) buffer[address]; hiByte = (unsigned char) (buffer[address] >> 8); - sprintf(lineBuf, "%02x %02x ", hiByte, loByte); - strcat(dataStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "%02x %02x ", hiByte, loByte); + strlcat(dataStr, lineBuf, sizeof(dataStr)); if ((hiByte > 31) && (hiByte < 127)) - sprintf(lineBuf, "%c", hiByte); + snprintf(lineBuf, sizeof(lineBuf), "%c", hiByte); else - sprintf(lineBuf, "."); - strcat(asciiStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "."); + strlcat(asciiStr, lineBuf, sizeof(asciiStr)); if ((loByte > 31) && (loByte < 127)) - sprintf(lineBuf, "%c", loByte); + snprintf(lineBuf, sizeof(lineBuf), "%c", loByte); else - sprintf(lineBuf, "."); - strcat(asciiStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "."); + strlcat(asciiStr, lineBuf, sizeof(asciiStr)); } else { - sprintf(lineBuf, "%02x ", buffer[address]); - strcat(dataStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "%02x ", buffer[address]); + strlcat(dataStr, lineBuf, sizeof(dataStr)); if ((buffer[address] > 31) && (buffer[address] < 127)) - sprintf(lineBuf, "%c", buffer[address]); + snprintf(lineBuf, sizeof(lineBuf), "%c", buffer[address]); else - sprintf(lineBuf, "."); - strcat(asciiStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "."); + strlcat(asciiStr, lineBuf, sizeof(asciiStr)); } } if ((fprintf(filePtr, "%06lx %s %s\n", @@ -509,26 +509,26 @@ void printBuffer(unsigned int buffer[], long bufLimit, int twoByteFlag) { addressPerLine = 0x08; loByte = (unsigned char) buffer[address]; hiByte = (unsigned char) (buffer[address] >> 8); - sprintf(lineBuf, "%02x %02x ", hiByte, loByte); - strcat(dataStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "%02x %02x ", hiByte, loByte); + strlcat(dataStr, lineBuf, sizeof(dataStr)); if ((hiByte > 31) && (hiByte < 127)) - sprintf(lineBuf, "%c", hiByte); + snprintf(lineBuf, sizeof(lineBuf), "%c", hiByte); else - sprintf(lineBuf, "."); - strcat(asciiStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "."); + strlcat(asciiStr, lineBuf, sizeof(asciiStr)); if ((loByte > 31) && (loByte < 127)) - sprintf(lineBuf, "%c", loByte); + snprintf(lineBuf, sizeof(lineBuf), "%c", loByte); else - sprintf(lineBuf, "."); - strcat(asciiStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "."); + strlcat(asciiStr, lineBuf, sizeof(asciiStr)); } else { - sprintf(lineBuf, "%02x ", buffer[address]); - strcat(dataStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "%02x ", buffer[address]); + strlcat(dataStr, lineBuf, sizeof(dataStr)); if ((buffer[address] > 31) && (buffer[address] < 127)) - sprintf(lineBuf, "%c", buffer[address]); + snprintf(lineBuf, sizeof(lineBuf), "%c", buffer[address]); else - sprintf(lineBuf, "."); - strcat(asciiStr, lineBuf); + snprintf(lineBuf, sizeof(lineBuf), "."); + strlcat(asciiStr, lineBuf, sizeof(asciiStr)); } } printf("%06lx %s %s\n", address - addressPerLine, dataStr, asciiStr); @@ -578,13 +578,13 @@ int logWrites(char *commandStr, unsigned int address, int data, char fileName[MAXLEN]; char *fileNamePtr; - if (log.logging != 1) + if (logging.logging != 1) return(0); formatStr(commandStr, 6, MAXLEN); - strcpy(deviceName, device.name); + strlcpy(deviceName, device.name, sizeof(deviceName)); if (strcmp(deviceName, "AT90(L)S2343 or Tiny22(L)") == 0) - strcpy(deviceName, "2343/Tiny22"); + strlcpy(deviceName, "2343/Tiny22", sizeof(deviceName)); formatStr(deviceName, 12, MAXLEN); queryBuf = malloc(FLASHBUF_UPPERLIMIT * sizeof (unsigned int)); if (queryBuf == NULL) { @@ -595,20 +595,20 @@ int logWrites(char *commandStr, unsigned int address, int data, curTime = time(NULL); locTime = localtime(&curTime); - if ((logPtr = fopen(log.logPath, "a")) == NULL) { + if ((logPtr = fopen(logging.logPath, "a")) == NULL) { free(queryBuf); return(1); } fprintf(logPtr, "%s %s ", deviceName, commandStr); if (strchr(commandStr, 'f') != NULL) { - strcpy(fileName, path); + strlcpy(fileName, path, sizeof(fileName)); fileNamePtr = fileName; idx = strlen(fileName); while (idx-- > 0 && fileName[idx] != '\\' && fileName[idx] != '/') fileNamePtr = &fileName[idx]; formatStr(fileNamePtr, 14, MAXLEN - idx); fprintf(logPtr, "%s ", fileNamePtr); - if (log.query) { + if (logging.query) { if (strchr(commandStr, 'p') != NULL) { readFlashArea(queryBuf, device.flashLimit); areaChksum = checksum(queryBuf, device.flashLimit); @@ -667,17 +667,17 @@ int logLocks(unsigned int buffer[], char *lockBits) { char deviceName[MAXLEN]; char lBits[MAXLEN] = ""; - if (log.logging != 1) + if (logging.logging != 1) return(0); - sprintf(lBits, "%02lx", strtol(lockBits, NULL, 2)); + snprintf(lBits, sizeof(lBits), "%02lx", strtol(lockBits, NULL, 2)); - strcpy(deviceName, device.name); + strlcpy(deviceName, device.name, sizeof(deviceName)); formatStr(deviceName, 12, MAXLEN); curTime = time(NULL); locTime = localtime(&curTime); - if ((logPtr = fopen(log.logPath, "a")) == NULL) + if ((logPtr = fopen(logging.logPath, "a")) == NULL) return(1); fprintf(logPtr, "%s -L%s ", deviceName, lBits); readFlashArea(buffer, device.flashLimit); diff --git a/src/dos_cpt.h b/src/dos_cpt.h index 8cbfb80..a8adfbc 100644 --- a/src/dos_cpt.h +++ b/src/dos_cpt.h @@ -20,7 +20,10 @@ /* Kevin Towers can be reached by email: ktowers@omnexcontrols.com */ /* Ken Huntington can be reached by email: kenh@compmore.net */ -#ifdef LINUX +#ifndef _DOS_CPT_H +#define _DOS_CPT_H + +#if defined (__linux__) #include #include @@ -30,9 +33,20 @@ #define delay(a) usleep(a*1000) -#else +#define strlcpy(d, s, l) (strncpy(d, s, l), (d)[(l) - 1] = '\0') +#define strlcat(d, s, l) strncat(d, s, (l) - strlen(d) - 1) + +#elif defined(__OpenBSD__) + +#include +#include + +#define inportb(port) inb(port) +#define outportb(port, data) outb(port, data) -#ifdef WIN32 +#define delay(t) usleep((t) * 1000) + +#elif defined(__WIN32__) // these functions are in winnt.c void win_giveio(void); @@ -44,10 +58,6 @@ inline void outportb (unsigned short port, const unsigned char val); #include -#endif // WIN32 -#endif // LINUX - - - - +#endif +#endif /* not _DOS_CPT_H */ diff --git a/src/init.c b/src/init.c index a9798a1..402f357 100644 --- a/src/init.c +++ b/src/init.c @@ -140,7 +140,7 @@ unsigned char PORTACTIVE; struct timeConsts timeConst; struct device_info device; -struct logging log; +struct logging logging; int portAddress; int portStatus; @@ -307,11 +307,11 @@ void initTimeConst(FILE *rcPtr) { fprintf(rcPtr, "KANDA=%d\n", KandaMode); fprintf(rcPtr, "# Set the log path/file to log writes and locks.\n"); fprintf(rcPtr, "# (less than 200 characters)\n"); - fprintf(rcPtr, "LOGPATH=%s\n", log.logPath); + fprintf(rcPtr, "LOGPATH=%s\n", logging.logPath); fprintf(rcPtr, "# Set logging=1 to activate logging:\n"); - fprintf(rcPtr, "LOGGING=%d\n", log.logging); + fprintf(rcPtr, "LOGGING=%d\n", logging.logging); fprintf(rcPtr, "# Set both logging and query=1 to always query writes:\n"); - fprintf(rcPtr, "QUERY=%d\n", log.query); + fprintf(rcPtr, "QUERY=%d\n", logging.query); fprintf(rcPtr, "# Chip erase delay, 120ms ATMega, 20ms others:\n"); fprintf(rcPtr, "CHIPERASE=%d\n", timeConst.chipErase); fprintf(rcPtr, "# Time constant for 200ms will be:\n"); @@ -371,25 +371,25 @@ void initSp12(char *howWeAreCalled) { writeRetries = 0; timeConst.byteWriteAdjusted = 0; timeConst.pageWriteAdjusted = 0; - log.logging = 0; - log.query = 0; - strcpy(log.logPath, "sp12log.txt"); + logging.logging = 0; + logging.query = 0; + strlcpy(logging.logPath, "sp12log.txt", sizeof(logging.logPath)); KandaMode = 3; /* if it remains 3 after reading _sp12rc, */ /* check how we were called */ if ((sp12rc = getenv("SP12")) != NULL) { - strcpy(rcPath, sp12rc); -#ifdef LINUX - if (rcPath[strlen(rcPath)-1] != '/') - strcat (rcPath, "/"); -#else + strlcpy(rcPath, sp12rc, sizeof(rcPath)); +#if defined(__WIN32__) if (rcPath[strlen(rcPath)-1] != '\\') - strcat (rcPath, "\\"); + strcat(rcPath, "\\", ); +#else + if (rcPath[strlen(rcPath)-1] != '/') + strlcat(rcPath, "/", sizeof(rcPath)); #endif printf("Path to _sp12rc and _sp12dev: %s\n", rcPath); - strcat(rcPath, "_sp12rc"); + strlcat(rcPath, "_sp12rc", sizeof(rcPath)); } else { - strcpy(rcPath, "_sp12rc"); + strlcpy(rcPath, "_sp12rc", sizeof(rcPath)); printf("Path to _sp12rc and _sp12dev: Local directory\n"); } if ((rcPtr = fopen(rcPath, "r")) != NULL) { @@ -399,9 +399,9 @@ void initSp12(char *howWeAreCalled) { while (fgets(rcLine, MAXLEN, rcPtr) != NULL) { sscanf(rcLine, "PORT=%x", &portAddress); sscanf(rcLine, "KANDA=%d", &KandaMode); - sscanf(rcLine, "LOGPATH=%s", log.logPath); - sscanf(rcLine, "LOGGING=%d", &log.logging); - sscanf(rcLine, "QUERY=%d", &log.query); + sscanf(rcLine, "LOGPATH=%s", logging.logPath); + sscanf(rcLine, "LOGGING=%d", &logging.logging); + sscanf(rcLine, "QUERY=%d", &logging.query); sscanf(rcLine, "RESETPULS=%ld", &timeConst.resetPuls); sscanf(rcLine, "CHIPERASE=%d", &timeConst.chipErase); sscanf(rcLine, "POWERON=%d", &timeConst.powerOn); @@ -446,7 +446,7 @@ void initSp12(char *howWeAreCalled) { || timeConst.programEnable == 0 || timeConst.chipErase == 0 || timeConst.byteWrite == 0 || timeConst.powerOn == 0 || timeConst.pageWrite == 0) { - fprintf(stderr, "Initialisation has failed (parallel port not found,\n"); + fprintf(stderr, "Initialisation has failed (parallel port not found, "); fprintf(stderr, "or %s corrupt).\n", rcPath); exit(1); } @@ -457,7 +457,7 @@ void initSp12(char *howWeAreCalled) { */ outportb(portAddress, 0x80); if (inportb(portAddress) != 0x80) { - fprintf(stderr, "Initialisation has failed (parallel port not\n"); + fprintf(stderr, "Initialisation has failed (parallel port not "); fprintf(stderr, "responding, or %s corrupt).\n", rcPath); exit(1); } else { @@ -606,17 +606,17 @@ int setDevicePars(int initFlag, char *identifier) { if ((sp12dev = getenv("SP12")) != NULL) { - strcpy(rcPath, sp12dev); -#ifdef LINUX - if (rcPath[strlen(rcPath)-1] != '/') - strcat (rcPath, "/"); -#else + strlcpy(rcPath, sp12dev, sizeof(rcPath)); +#if defined(__WIN32__) if (rcPath[strlen(rcPath)-1] != '\\') - strcat (rcPath, "\\"); + strcat(rcPath, "\\"); +#else + if (rcPath[strlen(rcPath)-1] != '/') + strlcat(rcPath, "/", sizeof(rcPath)); #endif - strcat(rcPath, "_sp12dev"); + strlcat(rcPath, "_sp12dev", sizeof(rcPath)); } else { - strcpy(rcPath, "_sp12dev"); + strlcpy(rcPath, "_sp12dev", sizeof(rcPath)); } if ((rcPtr = fopen(rcPath, "r")) == NULL) { @@ -631,17 +631,17 @@ int setDevicePars(int initFlag, char *identifier) { deviceCode = deviceCode | device.sigByte_1; switch (device.sigByte_0) { case 0x1E: - strcpy(device.madeBy, "Atmel"); + strlcpy(device.madeBy, "Atmel", sizeof(device.madeBy)); break; default: - strcpy(device.madeBy, "an unknown manufacturer"); + strlcpy(device.madeBy, "an unknown manufacturer", sizeof(device.madeBy)); } begin = 0; iFlag = 0; found = 0; - strcpy(LOCK_MESSAGE, ""); - strcpy(FUSES_MESSAGE, ""); - strcpy(device.name, "unknown device, or no device"); + strlcpy(LOCK_MESSAGE, "", sizeof(LOCK_MESSAGE)); + strlcpy(FUSES_MESSAGE, "", sizeof(FUSES_MESSAGE)); + strlcpy(device.name, "unknown device, or no device", sizeof(device.name)); device.flashLimit = 0; device.eepromLimit = 0; device.pageMode = 0; @@ -686,7 +686,7 @@ int setDevicePars(int initFlag, char *identifier) { } if (begin && sscanf(rcLine, "DEVICENAME = %[A-Z ()0-9a-z]", rawLine) == 1) { - strcpy(device.name, rawLine); + strlcpy(device.name, rawLine, sizeof(device.name)); } if (begin && sscanf(rcLine, "FLASHSIZE = %i", &iNum) == 1) { device.flashLimit = iNum; @@ -813,17 +813,17 @@ int supported(void) { int found = 0; if ((sp12dev = getenv("SP12")) != NULL) { - strcpy(rcPath, sp12dev); -#ifdef LINUX - if (rcPath[strlen(rcPath)-1] != '/') - strcat (rcPath, "/"); -#else + strlcpy(rcPath, sp12dev, sizeof(rcPath)); +#if defined(__WIN32__) if (rcPath[strlen(rcPath)-1] != '\\') - strcat (rcPath, "\\"); + strcat(rcPath, "\\"); +#else + if (rcPath[strlen(rcPath)-1] != '/') + strlcat(rcPath, "/", sizeof(rcPath)); #endif - strcat(rcPath, "_sp12dev"); + strlcat(rcPath, "_sp12dev", sizeof(rcPath)); } else { - strcpy(rcPath, "_sp12dev"); + strlcpy(rcPath, "_sp12dev", sizeof(rcPath)); } if ((rcPtr = fopen(rcPath, "r")) == NULL) { diff --git a/src/makefile b/src/makefile deleted file mode 100644 index cc01f38..0000000 --- a/src/makefile +++ /dev/null @@ -1,58 +0,0 @@ -# -# Makefile for the Free Software Foundations gcc compiler -# Optimize flag = 1 to achieve correct timing -# -CC=gcc -LINK=gcc -# -# LINUX FLAGS -CFLAGS=-c -O1 -Wall -DLINUX -LFLAGS=-s -o -TARGET=sp12 -ERASE=rm -f -OBJECTS=sp12.o init.o flash.o device.o buffer.o eeprom.o - -# DOS FLAGS -#CFLAGS=-c -O1 -Wall -#LFLAGS=-lemu -s -o -#TARGET=sp12.exe -#ERASE=del -#OBJECTS=sp12.o init.o flash.o device.o buffer.o eeprom.o - -# Win FLAGS - MingW -#CFLAGS=-c -O1 -Wall -mwindows -mconsole -#LFLAGS=-s -o -#TARGET=sp12.exe -#ERASE=del -#OBJECTS=sp12.o init.o flash.o device.o buffer.o eeprom.o winnt.o - -all: $(TARGET) - -sp12.o: sp12.c - $(CC) $(CFLAGS) sp12.c - -init.o: init.c - $(CC) $(CFLAGS) init.c - -flash.o: flash.c - $(CC) $(CFLAGS) flash.c - -device.o: device.c - $(CC) $(CFLAGS) device.c - -buffer.o: buffer.c - $(CC) $(CFLAGS) buffer.c - -eeprom.o: eeprom.c - $(CC) $(CFLAGS) eeprom.c - -winnt.o: winnt.c - $(CC) $(CFLAGS) winnt.c - -$(TARGET): makefile $(OBJECTS) - $(LINK) $(LFLAGS) $(TARGET) $(OBJECTS) - -clean: - $(ERASE) $(TARGET) - $(ERASE) *.o - diff --git a/src/sp12.c b/src/sp12.c index 738f404..c0c9139 100644 --- a/src/sp12.c +++ b/src/sp12.c @@ -22,6 +22,11 @@ /* Ken Huntington can be reached by email: kenh@compmore.net */ /* Artur Pundsack can be reached by email: ap@pa-tec.de */ +#if defined(__OpenBSD__) +#include +#include +#endif + #include #include #include @@ -222,7 +227,7 @@ void processCommand(void) { * on the third byte of PROGRAM_ENABLE. * Initiating contact at low speed */ - strcpy(device.name, "AT90S1200(A)"); + strlcpy(device.name, "AT90S1200(A)", sizeof(device.name)); setSckTiming(0.1); enableSPI(1200); deviceCode = setDevicePars(initFlag, identifier); @@ -811,22 +816,28 @@ int main(int argc, char *argv[]) checksumPath[0] = '\0'; sp12Start = clock(); -#ifdef LINUX +#if defined(__WIN32__) + win_giveio (); // get access to I/O ports in Windows NT/2000/XP +#else /* * Take control of LPT I/O ports */ +#if defined(__linux__) if (ioperm(0x278,3,1)) {perror("ioperm error at 0x278");exit(1);} if (ioperm(0x378,3,1)) {perror("ioperm error at 0x378");exit(1);} if (ioperm(0x3BC,3,1)) {perror("ioperm error at 0x3BC");exit(1);} +#elif defined(__OpenBSD__) + if (i386_iopl(1) == -1) { + perror("i386_iopl error"); + exit(1); + } +#endif +#endif /* * Revoke setuid/setguid-root status */ setgid(getgid()); setuid(getuid()); -#endif -#ifdef WIN32 - win_giveio (); // get access to I/O ports in Windows NT/2000/XP -#endif /* * Check if something is on the command line, print usage message @@ -866,15 +877,15 @@ int main(int argc, char *argv[]) exitSp12(2); } if (checksumFlag && !readFlag && !writeFlag) { - strcpy(checksumPath, argv[idx]); + strlcpy(checksumPath, argv[idx], sizeof(checksumPath)); pathAvailable = 1; processCommand(); } else if (flashFlag) { - strcpy(flashPath, argv[idx]); + strlcpy(flashPath, argv[idx], sizeof(flashPath)); pathAvailable = 1; processCommand(); } else if (eepromFlag) { - strcpy(eepromPath, argv[idx]); + strlcpy(eepromPath, argv[idx], sizeof(eepromPath)); pathAvailable = 1; processCommand(); } else { @@ -911,13 +922,13 @@ int main(int argc, char *argv[]) } if (*argv[idx] == '-') { if (strchr(argv[idx], 'w') != NULL) - strcpy(commandStr, argv[idx]); + strlcpy(commandStr, argv[idx], sizeof(commandStr)); while ((ascii = *++argv[idx]) != '\0') { switch(ascii) { case 'i': if (strlen(argv[idx]) > 1) { initFlag = 1; - strcpy(identifier, ++argv[idx]); + strlcpy(identifier, ++argv[idx], sizeof(identifier)); argv[idx][1] = '\0'; } if (idx > 1) { @@ -972,7 +983,7 @@ int main(int argc, char *argv[]) case 'L': if (strlen(argv[idx]) > 1 \ && (*(argv[idx] + 1) == '0' || *(argv[idx] + 1) == '1')) { - strcpy(lockBits, ++argv[idx]); + strlcpy(lockBits, ++argv[idx], sizeof(lockBits)); argv[idx][1] = '\0'; } lockFlag = 1; @@ -1019,7 +1030,7 @@ int main(int argc, char *argv[]) case 'F': if (strlen(argv[idx]) > 1 \ && (*(argv[idx] + 1) == '0' || *(argv[idx] + 1) == '1')) { - strcpy(fusesBits, ++argv[idx]); + strlcpy(fusesBits, ++argv[idx], sizeof(fusesBits)); argv[idx][1] = '\0'; } fusesFlag = 1; @@ -1027,7 +1038,7 @@ int main(int argc, char *argv[]) case 'H': if (strlen(argv[idx]) > 1 \ && (*(argv[idx] + 1) == '0' || *(argv[idx] + 1) == '1')) { - strcpy(fusesBits, ++argv[idx]); + strlcpy(fusesBits, ++argv[idx], sizeof(fusesBits)); argv[idx][1] = '\0'; } highFFlag = 1; @@ -1035,7 +1046,7 @@ int main(int argc, char *argv[]) case 'X': if (strlen(argv[idx]) > 1 \ && (*(argv[idx] + 1) == '0' || *(argv[idx] + 1) == '1')) { - strcpy(fusesBits, ++argv[idx]); + strlcpy(fusesBits, ++argv[idx], sizeof(fusesBits)); argv[idx][1] = '\0'; } extdFFlag = 1; diff --git a/src/sp12.h b/src/sp12.h index a75ef18..e3d116c 100644 --- a/src/sp12.h +++ b/src/sp12.h @@ -22,6 +22,9 @@ /* Ken Huntington can be reached by email: kenh@compmore.net */ /* Artur Pundsack can be reached by email: ap@pa-tec.de */ +#ifndef _SP12_H +#define _SP12_H + #define WORDLEN 16 #define DATALEN 5 #define FLASH_DATAPTR 6 @@ -184,7 +187,7 @@ struct logging { extern struct timeConsts timeConst; extern struct device_info device; -extern struct logging log; +extern struct logging logging; extern int portAddress; extern int portStatus; @@ -518,3 +521,4 @@ int writeEepromArea(unsigned int eepromBuf[], long bufLimit, void readEepromArea(unsigned int EepromBuf[], long bufLimit); +#endif /* not _SP12_H */ -- cgit v1.2.3