-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically save a OTP/SEEPROM dump to the sd card
- Loading branch information
Showing
12 changed files
with
543 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#pragma once | ||
#include "version.h" | ||
|
||
#define PLUGIN_VERSION "v0.1.4" | ||
#define PLUGIN_VERSION "v0.1.5" | ||
#define PLUGIN_VERSION_FULL PLUGIN_VERSION PLUGIN_VERSION_EXTRA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
|
||
#include "CFile.hpp" | ||
#include <stdarg.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <strings.h> | ||
|
||
CFile::CFile() { | ||
iFd = -1; | ||
mem_file = nullptr; | ||
filesize = 0; | ||
pos = 0; | ||
} | ||
|
||
CFile::CFile(const std::string &filepath, eOpenTypes mode) { | ||
iFd = -1; | ||
this->open(filepath, mode); | ||
} | ||
|
||
CFile::CFile(const uint8_t *mem, int32_t size) { | ||
iFd = -1; | ||
this->open(mem, size); | ||
} | ||
|
||
CFile::~CFile() { | ||
this->close(); | ||
} | ||
|
||
int32_t CFile::open(const std::string &filepath, eOpenTypes mode) { | ||
this->close(); | ||
int32_t openMode = 0; | ||
|
||
// This depend on the devoptab implementation. | ||
// see https://github.com/devkitPro/wut/blob/master/libraries/wutdevoptab/devoptab_fs_open.c#L21 fpr reference | ||
|
||
switch (mode) { | ||
default: | ||
case ReadOnly: // file must exist | ||
openMode = O_RDONLY; | ||
break; | ||
case WriteOnly: // file will be created / zerod | ||
openMode = O_TRUNC | O_CREAT | O_WRONLY; | ||
break; | ||
case ReadWrite: // file must exist | ||
openMode = O_RDWR; | ||
break; | ||
case Append: // append to file, file will be created if missing. write only | ||
openMode = O_CREAT | O_APPEND | O_WRONLY; | ||
break; | ||
} | ||
|
||
//! Using fopen works only on the first launch as expected | ||
//! on the second launch it causes issues because we don't overwrite | ||
//! the .data sections which is needed for a normal application to re-init | ||
//! this will be added with launching as RPX | ||
iFd = ::open(filepath.c_str(), openMode); | ||
if (iFd < 0) | ||
return iFd; | ||
|
||
|
||
filesize = ::lseek(iFd, 0, SEEK_END); | ||
::lseek(iFd, 0, SEEK_SET); | ||
|
||
return 0; | ||
} | ||
|
||
int32_t CFile::open(const uint8_t *mem, int32_t size) { | ||
this->close(); | ||
|
||
mem_file = mem; | ||
filesize = size; | ||
|
||
return 0; | ||
} | ||
|
||
void CFile::close() { | ||
if (iFd >= 0) | ||
::close(iFd); | ||
|
||
iFd = -1; | ||
mem_file = NULL; | ||
filesize = 0; | ||
pos = 0; | ||
} | ||
|
||
int32_t CFile::read(uint8_t *ptr, size_t size) { | ||
if (iFd >= 0) { | ||
int32_t ret = ::read(iFd, ptr, size); | ||
if (ret > 0) | ||
pos += ret; | ||
return ret; | ||
} | ||
|
||
int32_t readsize = size; | ||
|
||
if (readsize > (int64_t) (filesize - pos)) | ||
readsize = filesize - pos; | ||
|
||
if (readsize <= 0) | ||
return readsize; | ||
|
||
if (mem_file != NULL) { | ||
memcpy(ptr, mem_file + pos, readsize); | ||
pos += readsize; | ||
return readsize; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int32_t CFile::write(const uint8_t *ptr, size_t size) { | ||
if (iFd >= 0) { | ||
size_t done = 0; | ||
while (done < size) { | ||
int32_t ret = ::write(iFd, ptr, size - done); | ||
if (ret <= 0) | ||
return ret; | ||
|
||
ptr += ret; | ||
done += ret; | ||
pos += ret; | ||
} | ||
return done; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
int32_t CFile::seek(long int offset, int32_t origin) { | ||
int32_t ret = 0; | ||
int64_t newPos = pos; | ||
|
||
if (origin == SEEK_SET) { | ||
newPos = offset; | ||
} else if (origin == SEEK_CUR) { | ||
newPos += offset; | ||
} else if (origin == SEEK_END) { | ||
newPos = filesize + offset; | ||
} | ||
|
||
if (newPos < 0) { | ||
pos = 0; | ||
} else { | ||
pos = newPos; | ||
} | ||
|
||
if (iFd >= 0) | ||
ret = ::lseek(iFd, pos, SEEK_SET); | ||
|
||
if (mem_file != NULL) { | ||
if (pos > filesize) { | ||
pos = filesize; | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int32_t CFile::fwrite(const char *format, ...) { | ||
char tmp[512]; | ||
tmp[0] = 0; | ||
int32_t result = -1; | ||
|
||
va_list va; | ||
va_start(va, format); | ||
if ((vsprintf(tmp, format, va) >= 0)) { | ||
result = this->write((uint8_t *) tmp, strlen(tmp)); | ||
} | ||
va_end(va); | ||
|
||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#ifndef CFILE_HPP_ | ||
#define CFILE_HPP_ | ||
|
||
#include <fcntl.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <string> | ||
#include <unistd.h> | ||
#include <wut_types.h> | ||
|
||
class CFile { | ||
public: | ||
enum eOpenTypes { | ||
ReadOnly, | ||
WriteOnly, | ||
ReadWrite, | ||
Append | ||
}; | ||
|
||
CFile(); | ||
|
||
CFile(const std::string &filepath, eOpenTypes mode); | ||
|
||
CFile(const uint8_t *memory, int32_t memsize); | ||
|
||
virtual ~CFile(); | ||
|
||
int32_t open(const std::string &filepath, eOpenTypes mode); | ||
|
||
int32_t open(const uint8_t *memory, int32_t memsize); | ||
|
||
BOOL isOpen() const { | ||
if (iFd >= 0) | ||
return true; | ||
|
||
if (mem_file) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
void close(); | ||
|
||
int32_t read(uint8_t *ptr, size_t size); | ||
|
||
int32_t write(const uint8_t *ptr, size_t size); | ||
|
||
int32_t fwrite(const char *format, ...); | ||
|
||
int32_t seek(long int offset, int32_t origin); | ||
|
||
uint64_t tell() { | ||
return pos; | ||
}; | ||
|
||
uint64_t size() { | ||
return filesize; | ||
}; | ||
|
||
void rewind() { | ||
this->seek(0, SEEK_SET); | ||
}; | ||
|
||
protected: | ||
int32_t iFd; | ||
const uint8_t *mem_file{}; | ||
uint64_t filesize{}; | ||
uint64_t pos{}; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.