2007-03-27 17:47:10 +00:00
|
|
|
/*
|
|
|
|
* CiderPress
|
|
|
|
* Copyright (C) 2007 by faddenSoft, LLC. All Rights Reserved.
|
|
|
|
* See the file LICENSE for distribution terms.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Support for 2MG/2IMG wrapper files.
|
|
|
|
*
|
|
|
|
* This needs to be directly accessible from external applications, so try not
|
|
|
|
* to hook into private DiskImg state.
|
|
|
|
*/
|
|
|
|
#include "StdAfx.h"
|
|
|
|
#include "TwoImg.h"
|
|
|
|
#include "DiskImgPriv.h"
|
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
///*static*/ const char* TwoImgHeader::kMagic = "2IMG"; // file magic #
|
|
|
|
///*static*/ const char* TwoImgHeader::kCreator = "CdrP"; // our "creator" ID
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize a header to default values, using the supplied image length
|
|
|
|
* where appropriate.
|
|
|
|
*
|
|
|
|
* Sets up a header for a 2MG file with the disk data and nothing else.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 if one of the arguments was bad.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int TwoImgHeader::InitHeader(int imageFormat, uint32_t imageSize,
|
|
|
|
uint32_t imageBlockCount)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
if (imageSize == 0)
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1;
|
2014-12-12 00:04:35 +00:00
|
|
|
if (imageFormat < kImageFormatDOS || imageFormat > (int) kImageFormatNibble)
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
if (imageFormat != kImageFormatNibble &&
|
|
|
|
imageSize != imageBlockCount * 512)
|
|
|
|
{
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGW("2MG InitHeader: bad sizes %d %u %u", imageFormat,
|
2007-03-27 17:47:10 +00:00
|
|
|
imageSize, imageBlockCount);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(fComment == NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
//memcpy(fMagic, kMagic, 4);
|
|
|
|
//memcpy(fCreator, kCreator, 4);
|
|
|
|
fMagic = kMagic;
|
|
|
|
fCreator = kCreatorCiderPress;
|
|
|
|
fHeaderLen = kOurHeaderLen;
|
|
|
|
fVersion = kOurVersion;
|
|
|
|
fImageFormat = imageFormat;
|
|
|
|
fFlags = 0;
|
|
|
|
fNumBlocks = imageBlockCount;
|
|
|
|
fDataOffset = kOurHeaderLen;
|
|
|
|
fDataLen = imageSize;
|
|
|
|
fCmtOffset = 0;
|
|
|
|
fCmtLen = 0;
|
|
|
|
fCreatorOffset = 0;
|
|
|
|
fCreatorLen = 0;
|
|
|
|
fSpare[0] = fSpare[1] = fSpare[2] = fSpare[3] = 0;
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the DOS volume number.
|
|
|
|
*
|
|
|
|
* If not set, we currently return the initial value (-1), rather than the
|
|
|
|
* default volume number. For the way we currently make use of this, this
|
|
|
|
* makes the most sense.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int16_t TwoImgHeader::GetDOSVolumeNum(void) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(fFlags & kDOSVolumeSet);
|
|
|
|
return fDOSVolumeNum;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the DOS volume number.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
void TwoImgHeader::SetDOSVolumeNum(short dosVolumeNum)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
|
|
|
assert(dosVolumeNum >= 0 && dosVolumeNum < 256);
|
|
|
|
fFlags |= dosVolumeNum;
|
|
|
|
fFlags |= kDOSVolumeSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the comment.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
void TwoImgHeader::SetComment(const char* comment)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
delete[] fComment;
|
2014-11-18 05:13:13 +00:00
|
|
|
if (comment == NULL) {
|
|
|
|
fComment = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
} else {
|
|
|
|
fComment = new char[strlen(comment)+1];
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fComment != NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
strcpy(fComment, comment);
|
|
|
|
// else throw alloc failure
|
|
|
|
}
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fComment == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
fCmtLen = 0;
|
|
|
|
fCmtOffset = 0;
|
|
|
|
if (fCreatorOffset > 0)
|
|
|
|
fCreatorOffset = fDataOffset + fDataLen;
|
|
|
|
} else {
|
|
|
|
fCmtLen = strlen(fComment);
|
|
|
|
fCmtOffset = fDataOffset + fDataLen;
|
|
|
|
if (fCreatorOffset > 0)
|
|
|
|
fCreatorOffset = fCmtOffset + fCmtLen;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the creator chunk.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
void TwoImgHeader::SetCreatorChunk(const void* chunk, long len)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
assert(len >= 0);
|
|
|
|
|
|
|
|
delete[] fCreatorChunk;
|
2014-11-18 05:13:13 +00:00
|
|
|
if (chunk == NULL || len == 0) {
|
|
|
|
fCreatorChunk = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
} else {
|
|
|
|
fCreatorChunk = new char[len];
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fCreatorChunk != NULL)
|
2014-11-04 00:26:53 +00:00
|
|
|
memcpy(fCreatorChunk, chunk, len);
|
|
|
|
// else throw alloc failure
|
|
|
|
}
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
if (fCreatorChunk == NULL) {
|
2014-11-04 00:26:53 +00:00
|
|
|
fCreatorLen = 0;
|
|
|
|
fCreatorOffset = 0;
|
|
|
|
} else {
|
|
|
|
fCreatorLen = len;
|
|
|
|
if (fCmtOffset > 0)
|
|
|
|
fCreatorOffset = fCmtOffset + fCmtLen;
|
|
|
|
else
|
|
|
|
fCreatorOffset = fDataOffset + fDataLen;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the header from a 2IMG file. Pass in "totalLength" as a sanity check.
|
|
|
|
*
|
|
|
|
* THOUGHT: provide a simple GenericFD conversion for FILE*, and then just
|
|
|
|
* call the GenericFD version of ReadHeader.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, nonzero on error or invalid header.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int TwoImgHeader::ReadHeader(FILE* fp, uint32_t totalLength)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf[kOurHeaderLen];
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
fread(buf, kOurHeaderLen, 1, fp);
|
|
|
|
if (ferror(fp))
|
|
|
|
return errno ? errno : -1;
|
|
|
|
|
|
|
|
if (UnpackHeader(buf, totalLength) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the comment, if any.
|
|
|
|
*/
|
|
|
|
if (fCmtOffset > 0 && fCmtLen > 0) {
|
|
|
|
if (GetChunk(fp, fCmtOffset - kOurHeaderLen, fCmtLen,
|
|
|
|
(void**) &fComment) != 0)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Throwing comment away");
|
2014-11-04 00:26:53 +00:00
|
|
|
fCmtLen = 0;
|
|
|
|
fCmtOffset = 0;
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Got comment: '%s'", fComment);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the creator chunk, if any.
|
|
|
|
*/
|
|
|
|
if (fCreatorOffset > 0 && fCreatorLen > 0) {
|
|
|
|
if (GetChunk(fp, fCreatorOffset - kOurHeaderLen, fCreatorLen,
|
|
|
|
(void**) &fCreatorChunk) != 0)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Throwing creator chunk away");
|
2014-11-04 00:26:53 +00:00
|
|
|
fCreatorLen = 0;
|
|
|
|
fCreatorOffset = 0;
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
//LOGI("Got creator chunk: '%s'", fCreatorChunk);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the header from a 2IMG file. Pass in "totalLength" as a sanity check.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, nonzero on error or invalid header.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int TwoImgHeader::ReadHeader(GenericFD* pGFD, uint32_t totalLength)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf[kOurHeaderLen];
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
dierr = pGFD->Read(buf, kOurHeaderLen);
|
|
|
|
if (dierr != kDIErrNone)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (UnpackHeader(buf, totalLength) != 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the comment, if any.
|
|
|
|
*/
|
|
|
|
if (fCmtOffset > 0 && fCmtLen > 0) {
|
|
|
|
if (GetChunk(pGFD, fCmtOffset - kOurHeaderLen, fCmtLen,
|
|
|
|
(void**) &fComment) != 0)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Throwing comment away");
|
2014-11-04 00:26:53 +00:00
|
|
|
fCmtLen = 0;
|
|
|
|
fCmtOffset = 0;
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Got comment: '%s'", fComment);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract the creator chunk, if any.
|
|
|
|
*/
|
|
|
|
if (fCreatorOffset > 0 && fCreatorLen > 0) {
|
|
|
|
if (GetChunk(pGFD, fCreatorOffset - kOurHeaderLen, fCreatorLen,
|
|
|
|
(void**) &fCreatorChunk) != 0)
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Throwing creator chunk away");
|
2014-11-04 00:26:53 +00:00
|
|
|
fCreatorLen = 0;
|
|
|
|
fCreatorOffset = 0;
|
|
|
|
} else {
|
2014-11-18 21:05:15 +00:00
|
|
|
//LOGI("Got creator chunk: '%s'", fCreatorChunk);
|
2014-11-04 00:26:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grab a chunk of data from a relative offset.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int TwoImgHeader::GetChunk(GenericFD* pGFD, di_off_t relOffset, long len,
|
2014-11-04 00:26:53 +00:00
|
|
|
void** pBuf)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
DIError dierr;
|
|
|
|
di_off_t curPos;
|
|
|
|
|
|
|
|
/* remember current offset */
|
|
|
|
curPos = pGFD->Tell();
|
|
|
|
|
|
|
|
/* seek out to chunk and grab it */
|
|
|
|
dierr = pGFD->Seek(relOffset, kSeekCur);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("2MG seek to chunk failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(*pBuf == NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
*pBuf = new char[len+1]; // one extra, for null termination
|
|
|
|
|
|
|
|
dierr = pGFD->Read(*pBuf, len);
|
|
|
|
if (dierr != kDIErrNone) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("2MG chunk read failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
delete[] (char*) (*pBuf);
|
2014-11-18 05:13:13 +00:00
|
|
|
*pBuf = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
(void) pGFD->Seek(curPos, kSeekSet);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* null-terminate, in case this was a string */
|
|
|
|
((char*) *pBuf)[len] = '\0';
|
|
|
|
|
|
|
|
/* seek back to where we were */
|
|
|
|
(void) pGFD->Seek(curPos, kSeekSet);
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Grab a chunk of data from a relative offset.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int TwoImgHeader::GetChunk(FILE* fp, di_off_t relOffset, long len,
|
2014-11-04 00:26:53 +00:00
|
|
|
void** pBuf)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
long curPos;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
/* remember current offset */
|
|
|
|
curPos = ftell(fp);
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Current offset=%ld", curPos);
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
/* seek out to chunk and grab it */
|
|
|
|
if (fseek(fp, (long) relOffset, SEEK_CUR) == -1) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("2MG seek to chunk failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
return errno ? errno : -1;;
|
|
|
|
}
|
|
|
|
|
2014-11-18 05:13:13 +00:00
|
|
|
assert(*pBuf == NULL);
|
2014-11-04 00:26:53 +00:00
|
|
|
*pBuf = new char[len+1]; // one extra, for null termination
|
|
|
|
|
|
|
|
count = fread(*pBuf, len, 1, fp);
|
|
|
|
if (!count || ferror(fp) || feof(fp)) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("2MG chunk read failed");
|
2014-11-04 00:26:53 +00:00
|
|
|
delete[] (char*) (*pBuf);
|
2014-11-18 05:13:13 +00:00
|
|
|
*pBuf = NULL;
|
2014-11-04 00:26:53 +00:00
|
|
|
(void) fseek(fp, curPos, SEEK_SET);
|
|
|
|
clearerr(fp);
|
|
|
|
return errno ? errno : -1;;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* null-terminate, in case this was a string */
|
|
|
|
((char*) *pBuf)[len] = '\0';
|
|
|
|
|
|
|
|
/* seek back to where we were */
|
|
|
|
(void) fseek(fp, curPos, SEEK_SET);
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unpack the 64-byte 2MG header.
|
|
|
|
*
|
|
|
|
* Performs some sanity checks. Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
2014-11-24 20:56:19 +00:00
|
|
|
int TwoImgHeader::UnpackHeader(const uint8_t* buf, uint32_t totalLength)
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
fMagic = GetLongBE(&buf[0x00]);
|
|
|
|
fCreator = GetLongBE(&buf[0x04]);
|
|
|
|
fHeaderLen = GetShortLE(&buf[0x08]);
|
|
|
|
fVersion = GetShortLE(&buf[0x0a]);
|
|
|
|
fImageFormat = GetLongLE(&buf[0x0c]);
|
|
|
|
fFlags = GetLongLE(&buf[0x10]);
|
|
|
|
fNumBlocks = GetLongLE(&buf[0x14]);
|
|
|
|
fDataOffset = GetLongLE(&buf[0x18]);
|
|
|
|
fDataLen = GetLongLE(&buf[0x1c]);
|
|
|
|
fCmtOffset = GetLongLE(&buf[0x20]);
|
|
|
|
fCmtLen = GetLongLE(&buf[0x24]);
|
|
|
|
fCreatorOffset = GetLongLE(&buf[0x28]);
|
|
|
|
fCreatorLen = GetLongLE(&buf[0x2c]);
|
|
|
|
fSpare[0] = GetLongLE(&buf[0x30]);
|
|
|
|
fSpare[1] = GetLongLE(&buf[0x34]);
|
|
|
|
fSpare[2] = GetLongLE(&buf[0x38]);
|
|
|
|
fSpare[3] = GetLongLE(&buf[0x3c]);
|
|
|
|
|
|
|
|
fMagicStr[0] = (char) (fMagic >> 24);
|
|
|
|
fMagicStr[1] = (char) (fMagic >> 16);
|
|
|
|
fMagicStr[2] = (char) (fMagic >> 8);
|
|
|
|
fMagicStr[3] = (char) fMagic;
|
|
|
|
fMagicStr[4] = '\0';
|
|
|
|
fCreatorStr[0] = (char) (fCreator >> 24);
|
|
|
|
fCreatorStr[1] = (char) (fCreator >> 16);
|
|
|
|
fCreatorStr[2] = (char) (fCreator >> 8);
|
|
|
|
fCreatorStr[3] = (char) fCreator;
|
|
|
|
fCreatorStr[4] = '\0';
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
if (fMagic != kMagic) {
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Magic number does not match 2IMG");
|
2007-03-27 17:47:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fVersion > 1) {
|
2014-11-24 20:56:19 +00:00
|
|
|
LOGW("ERROR: unsupported version=%d", fVersion);
|
2014-11-04 00:26:53 +00:00
|
|
|
return -1; // bad header until I hear otherwise
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fFlags & kDOSVolumeSet)
|
|
|
|
fDOSVolumeNum = fFlags & kDOSVolumeMask;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
DumpHeader();
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
/* fix broken 'WOOF' images from Sweet-16 */
|
|
|
|
if (fCreator == kCreatorSweet16 && fDataLen == 0 &&
|
|
|
|
fImageFormat != kImageFormatNibble)
|
|
|
|
{
|
|
|
|
fDataLen = fNumBlocks * kBlockSize;
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGI("NOTE: fixing zero dataLen in 'WOOF' image (set to %u)",
|
2014-11-04 00:26:53 +00:00
|
|
|
fDataLen);
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
/*
|
|
|
|
* Perform some sanity checks.
|
|
|
|
*/
|
|
|
|
if (fImageFormat != kImageFormatNibble &&
|
2007-03-27 17:47:10 +00:00
|
|
|
fNumBlocks * kBlockSize != fDataLen)
|
|
|
|
{
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGW("numBlocks/dataLen mismatch (%u vs %u)",
|
2014-11-04 00:26:53 +00:00
|
|
|
fNumBlocks * kBlockSize, fDataLen);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (fDataLen + fDataOffset > totalLength) {
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGW("Invalid dataLen/offset/fileLength (dl=%u, off=%u, tlen=%u)",
|
2014-11-04 00:26:53 +00:00
|
|
|
fDataLen, fDataOffset, totalLength);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
if (fImageFormat < kImageFormatDOS || fImageFormat > kImageFormatNibble) {
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGW("Invalid image format %u", fImageFormat);
|
2007-03-27 17:47:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fCmtOffset > 0 && fCmtOffset < fDataOffset + fDataLen) {
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGW("2MG comment is inside the data section (off=%u, data end=%u)",
|
2014-11-04 00:26:53 +00:00
|
|
|
fCmtOffset, fDataOffset+fDataLen);
|
|
|
|
DebugBreak();
|
|
|
|
// ignore the comment
|
|
|
|
fCmtOffset = 0;
|
|
|
|
fCmtLen = 0;
|
|
|
|
}
|
|
|
|
if (fCreatorOffset > 0 && fCreatorLen > 0) {
|
2014-11-24 20:56:19 +00:00
|
|
|
uint32_t prevEnd = fDataOffset + fDataLen + fCmtLen;
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
if (fCreatorOffset < prevEnd) {
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGW("2MG creator chunk is inside prev data (off=%u, data end=%u)",
|
2014-11-04 00:26:53 +00:00
|
|
|
fCreatorOffset, prevEnd);
|
|
|
|
DebugBreak();
|
|
|
|
// ignore the creator chunk
|
|
|
|
fCreatorOffset = 0;
|
|
|
|
fCreatorLen = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-03-27 17:47:10 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the header to a 2IMG file.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or an errno value on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TwoImgHeader::WriteHeader(FILE* fp) const
|
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf[kOurHeaderLen];
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
PackHeader(buf);
|
|
|
|
if (fwrite(buf, kOurHeaderLen, 1, fp) != 1)
|
|
|
|
return errno ? errno : -1;
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the header to a 2IMG file.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, or an errno value on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TwoImgHeader::WriteHeader(GenericFD* pGFD) const
|
|
|
|
{
|
2014-11-24 20:56:19 +00:00
|
|
|
uint8_t buf[kOurHeaderLen];
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
PackHeader(buf);
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
if (pGFD->Write(buf, kOurHeaderLen) != kDIErrNone)
|
|
|
|
return -1;
|
2007-03-27 17:47:10 +00:00
|
|
|
|
2014-11-04 00:26:53 +00:00
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the footer. File must be seeked to end of data chunk.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TwoImgHeader::WriteFooter(FILE* fp) const
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Writing footer at offset=%ld", (long) ftell(fp));
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
if (fCmtLen) {
|
|
|
|
fwrite(fComment, fCmtLen, 1, fp);
|
|
|
|
}
|
|
|
|
if (fCreatorLen) {
|
|
|
|
fwrite(fCreatorChunk, fCreatorLen, 1, fp);
|
|
|
|
}
|
|
|
|
if (ferror(fp))
|
|
|
|
return errno ? errno : -1;
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write the footer. File must be seeked to end of data chunk.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
TwoImgHeader::WriteFooter(GenericFD* pGFD) const
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("Writing footer at offset=%ld", (long) pGFD->Tell());
|
2014-11-04 00:26:53 +00:00
|
|
|
|
|
|
|
if (fCmtLen) {
|
|
|
|
if (pGFD->Write(fComment, fCmtLen) != kDIErrNone)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (fCreatorLen) {
|
|
|
|
if (pGFD->Write(fCreatorChunk, fCreatorLen) != kDIErrNone)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Pack the header values into a 64-byte buffer.
|
|
|
|
*/
|
|
|
|
void
|
2014-11-24 20:56:19 +00:00
|
|
|
TwoImgHeader::PackHeader(uint8_t* buf) const
|
2007-03-27 17:47:10 +00:00
|
|
|
{
|
2014-11-04 00:26:53 +00:00
|
|
|
if (fCmtLen > 0 && fCmtOffset == 0) {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
if (fCreatorLen > 0 && fCreatorOffset == 0) {
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
PutLongBE(&buf[0x00], fMagic);
|
|
|
|
PutLongBE(&buf[0x04], fCreator);
|
|
|
|
PutShortLE(&buf[0x08], fHeaderLen);
|
|
|
|
PutShortLE(&buf[0x0a], fVersion);
|
|
|
|
PutLongLE(&buf[0x0c], fImageFormat);
|
|
|
|
PutLongLE(&buf[0x10], fFlags);
|
|
|
|
PutLongLE(&buf[0x14], fNumBlocks);
|
|
|
|
PutLongLE(&buf[0x18], fDataOffset);
|
|
|
|
PutLongLE(&buf[0x1c], fDataLen);
|
|
|
|
PutLongLE(&buf[0x20], fCmtOffset);
|
|
|
|
PutLongLE(&buf[0x24], fCmtLen);
|
|
|
|
PutLongLE(&buf[0x28], fCreatorOffset);
|
|
|
|
PutLongLE(&buf[0x2c], fCreatorLen);
|
|
|
|
PutLongLE(&buf[0x30], fSpare[0]);
|
|
|
|
PutLongLE(&buf[0x34], fSpare[1]);
|
|
|
|
PutLongLE(&buf[0x38], fSpare[2]);
|
|
|
|
PutLongLE(&buf[0x3c], fSpare[3]);
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dump the contents of an ImgHeader.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TwoImgHeader::DumpHeader(void) const
|
|
|
|
{
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("--- header contents:");
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGI("\tmagic = '%s' (0x%08x)", fMagicStr, fMagic);
|
|
|
|
LOGI("\tcreator = '%s' (0x%08x)", fCreatorStr, fCreator);
|
|
|
|
LOGI("\theaderLen = %u", fHeaderLen);
|
|
|
|
LOGI("\tversion = %u", fVersion);
|
|
|
|
LOGI("\timageFormat = %u", fImageFormat);
|
|
|
|
LOGI("\tflags = 0x%08x", fFlags);
|
2014-11-18 21:05:15 +00:00
|
|
|
LOGI("\t locked = %s",
|
2014-11-04 00:26:53 +00:00
|
|
|
(fFlags & kFlagLocked) ? "true" : "false");
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGI("\t DOS volume = %s (%d)",
|
2014-11-04 00:26:53 +00:00
|
|
|
(fFlags & kDOSVolumeSet) ? "true" : "false",
|
|
|
|
fFlags & kDOSVolumeMask);
|
2014-12-12 00:04:35 +00:00
|
|
|
LOGI("\tnumBlocks = %u", fNumBlocks);
|
|
|
|
LOGI("\tdataOffset = %u", fDataOffset);
|
|
|
|
LOGI("\tdataLen = %u", fDataLen);
|
|
|
|
LOGI("\tcmtOffset = %u", fCmtOffset);
|
|
|
|
LOGI("\tcmtLen = %u", fCmtLen);
|
|
|
|
LOGI("\tcreatorOffset = %u", fCreatorOffset);
|
|
|
|
LOGI("\tcreatorLen = %u", fCreatorLen);
|
|
|
|
LOGI("---");
|
2007-03-27 17:47:10 +00:00
|
|
|
}
|