1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
//=========================================================================//
// //
// PonyProg - Serial Device Programmer //
// //
// Copyright (C) 1997-2021 Claudio Lanconelli //
// //
// http://ponyprog.sourceforge.net //
// //
//-------------------------------------------------------------------------//
// //
// This program is free software; you can redistribute it and/or //
// modify it under the terms of the GNU General Public License //
// as published by the Free Software Foundation; either version2 of //
// the License, or (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU //
// General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program (see LICENSE); if not, write to the //
// Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
//=========================================================================//
// #include <stdio.h>
#include <QString>
#include <QDataStream>
#include "e2pfbuf.h" // Header file
#include "crc.h"
#include "errcode.h"
#include "e2phead.h"
#include "eeptypes.h"
static char const *id_string = "E2P!Lanc";
e2pFileBuf::e2pFileBuf(e2AppWinInfo *wininfo)
: FileBuf(wininfo)
{
file_type = E2P;
Q_ASSERT_X(sizeof(struct e2pHeader) == 152, "e2pFileBuf", "Bad size for e2pHeader");
}
e2pFileBuf::~e2pFileBuf()
{
}
/***
void e2pFileBuf::check_offsets()
{
printf("fileID: %lu\n", offsetof(e2pHeader, fileID));
printf("e2pFuseBits: %lu\n", offsetof(e2pHeader, e2pFuseBits));
printf("e2pLockBits: %lu\n", offsetof(e2pHeader, e2pLockBits));
printf("e2pType: %lu\n", offsetof(e2pHeader, e2pType));
printf("e2pSize: %lu\n", offsetof(e2pHeader, e2pSize));
printf("flags: %lu\n", offsetof(e2pHeader, flags));
printf("e2pExtFuseBits: %lu\n", offsetof(e2pHeader, e2pExtFuseBits));
printf("e2pExtLockBits: %lu\n", offsetof(e2pHeader, e2pExtLockBits));
printf("fversion: %lu\n", offsetof(e2pHeader, fversion));
printf("split_size_Low: %lu\n", offsetof(e2pHeader, split_size_Low));
printf("e2pStringID: %lu\n", offsetof(e2pHeader, e2pStringID));
printf("e2pProgBits: %lu\n", offsetof(e2pHeader, e2pProgBits));
printf("e2pComment: %lu\n", offsetof(e2pHeader, e2pComment));
printf("split_size_High: %lu\n", offsetof(e2pHeader, split_size_High));
printf("pad: %lu\n", offsetof(e2pHeader, pad));
printf("e2pCrc: %lu\n", offsetof(e2pHeader, e2pCrc));
printf("headCrc: %lu\n", offsetof(e2pHeader, headCrc));
if (sizeof(struct e2pHeader) != 152)
printf("Bad E2P Header size: %lu\n", sizeof(struct e2pHeader));
else
printf("E2P Header size OK (%lu)\n", sizeof(struct e2pHeader));
}
***/
int e2pFileBuf::Load(int loadtype, long relocation_offset)
{
extern int GetE2PSubType(unsigned long x);
extern int GetE2PPriType(unsigned long x);
QFile fh(FileBuf::GetFileName());
e2pHeader hdr;
int rval;
if (!fh.open(QIODevice::ReadOnly))
{
return FILENOTFOUND;
}
QDataStream datastream(&fh);
// Controlla il tipo di file
if (datastream.readRawData((char *)&hdr, sizeof(e2pHeader)) &&
strncmp(hdr.fileID, id_string, E2P_ID_SIZE) == 0)
{
unsigned char *localbuf = new unsigned char[hdr.e2pSize];
//Controlla il CRC dell'Header
if (mcalc_crc(&hdr, sizeof(hdr) - sizeof(hdr.headCrc)) == hdr.headCrc &&
//Check for CRC in memory
fcalc_crc(fh, sizeof(e2pHeader), 0) == hdr.e2pCrc &&
//read buffer
datastream.readRawData((char *)localbuf, hdr.e2pSize))
// fread(FileBuf::GetBufPtr(), hdr.e2pSize, 1, fh) )
{
SetEEpromType(hdr.e2pType); //set eeprom device type (and block size too)
//FileBuf::SetNoOfBlock( hdr.e2pSize / FileBuf::GetBlockSize() );
if (hdr.fversion > 0)
{
SetLockBits(((uint32_t)hdr.e2pExtLockBits << 8) | hdr.e2pLockBits);
SetFuseBits(((uint32_t)hdr.e2pExtFuseBits << 8) | hdr.e2pFuseBits);
}
else
{
//Old file version
if (GetE2PPriType(hdr.e2pType) == PIC16XX ||
GetE2PPriType(hdr.e2pType) == PIC168XX ||
GetE2PPriType(hdr.e2pType) == PIC125XX)
{
SetLockBits(((uint32_t)hdr.e2pLockBits << 8) | hdr.e2pFuseBits);
}
else
{
SetLockBits(hdr.e2pLockBits);
SetFuseBits(hdr.e2pFuseBits);
}
}
if (hdr.fversion > 1)
{
SetSplitted(((uint32_t)hdr.split_size_High << 16) | hdr.split_size_Low);
}
else
{
SetSplitted(hdr.split_size_Low);
}
SetStringID(hdr.e2pStringID);
SetComment(hdr.e2pComment);
SetRollOver(hdr.flags & 7);
SetCRC(hdr.e2pCrc);
//Copy the content into the buffer
if (loadtype == ALL_TYPE)
{
if (hdr.e2pSize <= GetBufSize())
{
memcpy(FileBuf::GetBufPtr(), localbuf, hdr.e2pSize);
}
}
else if (loadtype == PROG_TYPE)
{
long s = GetSplitted();
if (s <= 0)
{
s = hdr.e2pSize;
}
//if splittedInfo == 0 then copy ALL
if (s <= hdr.e2pSize && s <= GetBufSize())
{
memcpy(FileBuf::GetBufPtr(), localbuf, s);
}
}
else if (loadtype == DATA_TYPE)
{
long s = GetSplitted();
if (s >= 0 &&
s < hdr.e2pSize &&
hdr.e2pSize <= GetBufSize())
{
memcpy(FileBuf::GetBufPtr() + s, localbuf + s, hdr.e2pSize - s);
}
}
rval = GetNoOfBlock();
}
else
{
rval = READERROR;
}
delete[] localbuf;
}
else
{
rval = BADFILETYPE;
}
fh.close();
return rval;
}
int e2pFileBuf::Save(int savetype, long relocation_offset)
{
e2pHeader hdr;
int rval;
if (FileBuf::GetNoOfBlock() <= 0)
{
return NOTHINGTOSAVE;
}
QFile fh(FileBuf::GetFileName());
//Header settings
memset(&hdr, 0, sizeof(hdr)); //Clear all to zero first
strncpy(hdr.fileID, id_string, E2P_ID_SIZE); //Id
hdr.e2pSize = FileBuf::GetNoOfBlock() * FileBuf::GetBlockSize();
unsigned char *localbuf = new unsigned char[hdr.e2pSize];
long s = GetSplitted();
memset(localbuf, 0xff, hdr.e2pSize);
if (fh.exists())
{
if (!fh.open(QIODevice::ReadOnly))
{
delete[] localbuf;
return CREATEERROR;
}
QDataStream datastream(&fh);
//Initialize local buffer
// if the file already exist read the current content
// otherwise set the localbuffer to 0xFF
if (fh.seek(sizeof(hdr)))
{
datastream.readRawData((char *)localbuf, hdr.e2pSize);
}
fh.close();
}
if (savetype == ALL_TYPE)
{
memcpy(localbuf, FileBuf::GetBufPtr(), hdr.e2pSize);
}
else if (savetype == DATA_TYPE)
{
if (hdr.e2pSize > s)
{
memcpy(localbuf + s, FileBuf::GetBufPtr() + s, hdr.e2pSize - s);
}
}
else if (savetype == PROG_TYPE)
{
if (s > 0 && s <= hdr.e2pSize)
{
memcpy(localbuf, FileBuf::GetBufPtr(), s);
}
}
hdr.fversion = E2P_FVERSION;
hdr.e2pLockBits = (uint8_t)(GetLockBits() & 0xFF);
hdr.e2pExtLockBits = (uint16_t)(GetLockBits() >> 8);
hdr.e2pFuseBits = (uint8_t)(GetFuseBits() & 0xFF);
hdr.e2pExtFuseBits = (uint16_t)(GetFuseBits() >> 8);
hdr.e2pType = GetEEpromType();
strncpy(hdr.e2pStringID, GetStringID().toLatin1().constData(), 28);
strncpy(hdr.e2pComment, GetComment().toLatin1().constData(), 85);
hdr.flags = GetRollOver() & 7;
hdr.split_size_Low = (uint16_t)GetSplitted();
hdr.split_size_High = (uint16_t)(GetSplitted() >> 16);
hdr.e2pCrc = mcalc_crc(localbuf, hdr.e2pSize);
hdr.headCrc = mcalc_crc(&hdr, sizeof(hdr) - sizeof(hdr.headCrc));
if (fh.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
QDataStream datastream(&fh);
//Write to file
if (datastream.writeRawData((char *)&hdr, sizeof(hdr)) && //Write the header
datastream.writeRawData((char *)localbuf, hdr.e2pSize)) //Write the buffer
{
rval = GetNoOfBlock();
}
else
{
rval = WRITEERROR;
}
fh.close();
}
else
{
rval = CREATEERROR;
}
delete[] localbuf;
return rval;
}
|