Skip to content

Commit

Permalink
Rename NewlibStat to CommonStat
Browse files Browse the repository at this point in the history
This is more descriptive of the structure's true purpose to move
stat fields which are common between the host Posix *stat() APIs and
newlib's.
  • Loading branch information
adamgreen committed May 24, 2014
1 parent 1e2eda9 commit ef108fa
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
5 changes: 3 additions & 2 deletions include/NewlibSemihost.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@

#include <stdint.h>

typedef struct NewlibStat
/* The more common struct stat fields that will be transferred from host to target. */
typedef struct CommonStat
{
uint32_t mode;
uint32_t size;
uint32_t blksize;
uint32_t blocks;
} NewlibStat;
} CommonStat;

#endif /* !__ASSEMBLER__ */
#endif /* _NEWLIB_SEMIHOST_H_ */
12 changes: 6 additions & 6 deletions libpinkysim/src/NewlibSemihost.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void writeToFile(PlatformSemihostParameters* pSemihostParameters);
static int isGdbConnected(void);
static int isConsoleInput(uint32_t fileDescriptor);
static void readFromFile(PlatformSemihostParameters* pSemihostParameters);
static void copyHostStatToTargetStat(NewlibStat* pTarget, const struct stat* pHost);
static void copyHostStatToCommonStat(CommonStat* pTarget, const struct stat* pHost);


int handleNewlibSemihostWriteRequest(PlatformSemihostParameters* pSemihostParameters)
Expand Down Expand Up @@ -191,11 +191,11 @@ int handleNewlibSemihostFStatRequest(PlatformSemihostParameters* pSemihostParame
__try
{
struct stat hostStat;
NewlibStat* pTargetStat = MemorySim_MapSimulatedAddressToHostAddressForWrite(mri4simGetContext()->pMemory,
CommonStat* pTargetStat = MemorySim_MapSimulatedAddressToHostAddressForWrite(mri4simGetContext()->pMemory,
fileStatAddress,
sizeof(*pTargetStat));
int fstatResult = fstat(file, &hostStat);
copyHostStatToTargetStat(pTargetStat, &hostStat);
copyHostStatToCommonStat(pTargetStat, &hostStat);
SetSemihostReturnValues(fstatResult, errno);
}
__catch
Expand All @@ -217,11 +217,11 @@ int handleNewlibSemihostStatRequest(PlatformSemihostParameters* pSemihostParamet
struct stat hostStat;
const void* pFilename = MemorySim_MapSimulatedAddressToHostAddressForRead(mri4simGetContext()->pMemory,
filenameAddress, filenameLength);
NewlibStat* pTargetStat = MemorySim_MapSimulatedAddressToHostAddressForWrite(mri4simGetContext()->pMemory,
CommonStat* pTargetStat = MemorySim_MapSimulatedAddressToHostAddressForWrite(mri4simGetContext()->pMemory,
fileStatAddress,
sizeof(*pTargetStat));
int statResult = hook_stat(pFilename, &hostStat);
copyHostStatToTargetStat(pTargetStat, &hostStat);
copyHostStatToCommonStat(pTargetStat, &hostStat);
SetSemihostReturnValues(statResult, errno);
}
__catch
Expand All @@ -232,7 +232,7 @@ int handleNewlibSemihostStatRequest(PlatformSemihostParameters* pSemihostParamet
return 1;
}

static void copyHostStatToTargetStat(NewlibStat* pTarget, const struct stat* pHost)
static void copyHostStatToCommonStat(CommonStat* pTarget, const struct stat* pHost)
{
pTarget->mode = pHost->st_mode;
pTarget->size = pHost->st_size;
Expand Down
28 changes: 14 additions & 14 deletions libpinkysim/tests/semihostTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ TEST(semihostTests, UnlinkCall_AttemptToUseInvalidFilenamePointer_ShouldFail)

TEST(semihostTests, StatCall_VerifyReturnValueAndOutputStructure)
{
NewlibStat newlibStat;
CommonStat commonStat;
struct stat hostStat;

const char testFilename[] = "foo.bar";
m_pContext->R[0] = INITIAL_SP - sizeof(testFilename);
m_pContext->R[1] = INITIAL_SP - sizeof(testFilename) - sizeof(newlibStat);
m_pContext->R[1] = INITIAL_SP - sizeof(testFilename) - sizeof(commonStat);
m_pContext->R[2] = sizeof(testFilename);
copyBufferToSimulator(m_pContext->R[0], testFilename, m_pContext->R[2]);
hostStat.st_mode = 0x1234;
Expand All @@ -431,16 +431,16 @@ TEST(semihostTests, StatCall_VerifyReturnValueAndOutputStructure)
mri4simRun(mockIComm_Get(), FALSE);
STRCMP_EQUAL("", mockIComm_GetTransmittedData());
CHECK_EQUAL(0, m_pContext->R[0]);
NewlibStat expected = {0x1234, 0xbaadf00d, 0x87654321, 0xfeedfeed};
validateBytesInSimulator(INITIAL_SP - sizeof(testFilename) - sizeof(newlibStat), &expected, sizeof(expected));
CommonStat expected = {0x1234, 0xbaadf00d, 0x87654321, 0xfeedfeed};
validateBytesInSimulator(INITIAL_SP - sizeof(testFilename) - sizeof(commonStat), &expected, sizeof(expected));
}

TEST(semihostTests, StatCall_VerifyErrorReturnsInR0andR1)
{
NewlibStat newlibStat;
CommonStat commonStat;
const char testFilename[] = "foo.bar";
m_pContext->R[0] = INITIAL_SP - sizeof(testFilename);
m_pContext->R[1] = INITIAL_SP - sizeof(testFilename) - sizeof(newlibStat);
m_pContext->R[1] = INITIAL_SP - sizeof(testFilename) - sizeof(commonStat);
m_pContext->R[2] = sizeof(testFilename);
copyBufferToSimulator(m_pContext->R[0], testFilename, m_pContext->R[2]);
mockFileIo_SetStatCallResults(-1, EIO, NULL);
Expand All @@ -457,9 +457,9 @@ TEST(semihostTests, StatCall_VerifyErrorReturnsInR0andR1)

TEST(semihostTests, StatCall_WithInvalidFilenamePointer_ShouldFail)
{
NewlibStat newlibStat;
CommonStat commonStat;
m_pContext->R[0] = 0xFFFFFFF0;
m_pContext->R[1] = INITIAL_SP - sizeof(newlibStat);
m_pContext->R[1] = INITIAL_SP - sizeof(commonStat);
m_pContext->R[2] = 4;
mockFileIo_SetStatCallResults(0, 0, NULL);

Expand Down Expand Up @@ -494,11 +494,11 @@ TEST(semihostTests, StatCall_WithInvalidBufferPointer_ShouldFail)

TEST(semihostTests, FStatCall_VerifyReturnValueAndOutputStructure)
{
NewlibStat newlibStat;
CommonStat commonStat;
struct stat hostStat;

m_pContext->R[0] = 4;
m_pContext->R[1] = INITIAL_SP - sizeof(newlibStat);
m_pContext->R[1] = INITIAL_SP - sizeof(commonStat);
hostStat.st_mode = 0x1234;
hostStat.st_size = 0xbaadf00d;
hostStat.st_blksize = 0x87654321;
Expand All @@ -512,15 +512,15 @@ TEST(semihostTests, FStatCall_VerifyReturnValueAndOutputStructure)
mri4simRun(mockIComm_Get(), FALSE);
STRCMP_EQUAL("", mockIComm_GetTransmittedData());
CHECK_EQUAL(0, m_pContext->R[0]);
NewlibStat expected = {0x1234, 0xbaadf00d, 0x87654321, 0xfeedfeed};
validateBytesInSimulator(INITIAL_SP - sizeof(newlibStat), &expected, sizeof(expected));
CommonStat expected = {0x1234, 0xbaadf00d, 0x87654321, 0xfeedfeed};
validateBytesInSimulator(INITIAL_SP - sizeof(commonStat), &expected, sizeof(expected));
}

TEST(semihostTests, FStatCall_VerifyErrorReturnsInR0andR1)
{
NewlibStat newlibStat;
CommonStat commonStat;
m_pContext->R[0] = 4;
m_pContext->R[1] = INITIAL_SP - sizeof(newlibStat);
m_pContext->R[1] = INITIAL_SP - sizeof(commonStat);
mockFileIo_SetFStatCallResults(-1, EIO, NULL);

emitBKPT(NEWLIB_FSTAT);
Expand Down
16 changes: 8 additions & 8 deletions samples/libstartup/NewlibRetarget.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern int errno;
extern uint32_t __end__[1];


static void copyStat(struct stat* pStat, const NewlibStat* pNewlibStat);
static void copyCommonStatToNewlibStat(struct stat* pStat, const CommonStat* pNewlibStat);


/* File system related syscalls. */
Expand All @@ -45,14 +45,14 @@ int _unlink(const char *pFilename)

int _stat(const char *pFilename, struct stat *pStat)
{
NewlibStat newlibStat;
int result = semihostStat(pFilename, &newlibStat);
CommonStat commonStat;
int result = semihostStat(pFilename, &commonStat);
if (result == 0)
copyStat(pStat, &newlibStat);
copyCommonStatToNewlibStat(pStat, &commonStat);
return result;
}

static void copyStat(struct stat* pStat, const NewlibStat* pNewlibStat)
static void copyCommonStatToNewlibStat(struct stat* pStat, const CommonStat* pNewlibStat)
{
pStat->st_mode = pNewlibStat->mode;
pStat->st_size = pNewlibStat->size;
Expand Down Expand Up @@ -91,10 +91,10 @@ int _close(int file)

int _fstat(int file, struct stat *st)
{
NewlibStat newlibStat;
int result = semihostFStat(file, &newlibStat);
CommonStat commonStat;
int result = semihostFStat(file, &commonStat);
if (result == 0)
copyStat(st, &newlibStat);
copyCommonStatToNewlibStat(st, &commonStat);
return result;
}

Expand Down
4 changes: 2 additions & 2 deletions samples/libstartup/SemihostThunks.S
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ semihostUnlink:

.global semihostStat
.type semihostStat, function
/* extern "C" int semihostStat(const char *pFilename, struct stat *pStat);
/* extern "C" int semihostStat(const char *pFilename, CommonStat *pStat);
Issues stat call to PC via GDB.
*/
semihostStat:
Expand Down Expand Up @@ -140,7 +140,7 @@ semihostClose:

.global semihostFStat
.type semihostFStat, function
/* extern "C" int semihostFStat(int file, struct stat *pStat);
/* extern "C" int semihostFStat(int file, CommonStat *pStat);
Issues stat call to PC via GDB.
*/
semihostFStat:
Expand Down
4 changes: 2 additions & 2 deletions samples/libstartup/SemihostThunks.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ int semihostRead(int file, char *ptr, int len);
int semihostOpen(const char *pFilename, int flags, int mode);
int semihostRename(const char *pOldFilename, const char *pNewFilename);
int semihostUnlink(const char *pFilename);
int semihostStat(const char *pFilename, NewlibStat *pStat);
int semihostStat(const char *pFilename, CommonStat *pStat);
int semihostLSeek(int file, int offset, int whence);
int semihostClose(int file);
int semihostFStat(int file, NewlibStat *pStat);
int semihostFStat(int file, CommonStat *pStat);
void semihostExit(int code);


Expand Down

0 comments on commit ef108fa

Please sign in to comment.