Skip to content

Commit

Permalink
[romimg] implement filename transformation
Browse files Browse the repository at this point in the history
  • Loading branch information
israpps committed Nov 6, 2024
1 parent c848b03 commit bbe52ac
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion samples/Makefile.eeglobal_sample
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ ifeq (_$(IOPRP_CONTENTS)_,__)
$(error Cannot generate IOPRP if 'IOPRP_CONTENTS' variable is empty)
else
$(DIR_GUARD)
romimg -c $@ $<
romimg -C $@ $<
endif

22 changes: 12 additions & 10 deletions tools/romimg/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>

#include "romimg.h"

Expand Down Expand Up @@ -34,13 +35,15 @@ static void DisplayROMImgDetails(const ROMIMG *ROMImg)

static void DisplaySyntaxHelp(void)
{
printf(REDBOLD"Syntax error"DEFCOL". Syntax:\n"
"ROMIMG -c <ROM image> <files>\n\tCreate ROM image\n"
printf("Syntax:\n"
"ROMIMG -c <ROM image> <files>\n\tCreate ROM image *\n"
"ROMIMG -l <ROM image>\n\tList files in ROM image\n"
"ROMIMG -a <ROM image> <file(s)>\n\tAdd file(s) to ROM image\n"
"ROMIMG -a <ROM image> <file(s)>\n\tAdd file(s) to ROM image *\n"
"ROMIMG -d <ROM image> <file(s)>\n\tDelete file(s) from ROM image\n"
"ROMIMG -x <ROM image>\n\tExtract all files from ROM image\n"
"ROMIMG -x <ROM image> <file>\n\tExtract file from ROM image\n");
"ROMIMG -x <ROM image> <file>\n\tExtract file from ROM image\n"
"\n note*: write the switch in uppercase to perform filename transformation (eg: 'ioman.irx' > 'IOMAN')\n"
);
}

static void DisplayAddDeleteOperationResult(int result, const char *InvolvedFile)
Expand Down Expand Up @@ -80,31 +83,30 @@ int main(int argc, char **argv)

if (argc < 2) {
DisplaySyntaxHelp();
DPRINTF("ERROR: LESS THAN TWO ARGS PROVIDED\n");
return EINVAL;
}

if (argc >= 4 && strcmp(argv[1], "-c") == 0) {
if (argc >= 4 && strcasecmp(argv[1], "-c") == 0) {
if ((result = CreateBlankROMImg(argv[2], &ROMImg)) == 0) {
for (FilesAffected = 0, i = 0; i < argc - 3; i++) {
printf("Adding file '%s'", argv[3 + i]);
if ((result = AddFile(&ROMImg, argv[3 + i])) == 0)
if ((result = AddFile(&ROMImg, argv[3 + i], isupper(argv[1][1]))) == 0)
FilesAffected++;
printf(result == 0 ? GRNBOLD" done!"DEFCOL"\n" : REDBOLD" failed!"DEFCOL"\n");
}

if (FilesAffected > 0) {
printf("Writing image...");
printf("Writing image... ");
printf("%s", (result = WriteROMImg(argv[2], &ROMImg)) == 0 ? GRNBOLD"done!"DEFCOL"\n" : REDBOLD"failed!"DEFCOL"\n");
}
UnloadROMImg(&ROMImg);
} else
ERROR("(Internal fault) Can't create blank image file: %d (%s). Please report.\n", result, strerror(result));
} else if (argc >= 4 && strcmp(argv[1], "-a") == 0) {
} else if (argc >= 4 && strcasecmp(argv[1], "-a") == 0) {
if ((result = LoadROMImg(&ROMImg, argv[2])) == 0) {
for (i = 0, FilesAffected = 0; i < argc - 3; i++) {
printf("Adding file '%s'", argv[3 + i]);
if ((result = AddFile(&ROMImg, argv[3 + i])) == 0)
if ((result = AddFile(&ROMImg, argv[3 + i], isupper(argv[1][1]))) == 0)
FilesAffected++;
DisplayAddDeleteOperationResult(result, argv[3 + i]);
}
Expand Down
10 changes: 10 additions & 0 deletions tools/romimg/src/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <errno.h>
#include <stdlib.h>
#include <ctype.h>
#include "dprintf.h"
#if defined(_WIN32) || defined(WIN32)
#include <windows.h>
Expand Down Expand Up @@ -114,3 +115,12 @@ int GetCurrentWorkingDirectory(char *buffer, unsigned int BufferSize)
return EIO;
#endif
}

void upperbuff(char *temp)
{
char *s = temp;
while (*s) {
*s = toupper((unsigned char) *s);
s++;
}
}
1 change: 1 addition & 0 deletions tools/romimg/src/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ int GetLocalhostName(char *buffer, unsigned int BufferSize);
unsigned int GetSystemDate(void);
unsigned int GetFileCreationDate(const char *path);
int GetCurrentWorkingDirectory(char *buffer, unsigned int BufferSize);
void upperbuff(char *temp);

#if defined(_WIN32) || defined(WIN32)
#define PATHSEP '\\'
Expand Down
15 changes: 13 additions & 2 deletions tools/romimg/src/romimg.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ int CreateBlankROMImg(const char *filename, ROMIMG *ROMImg)
GetCurrentWorkingDirectory(cwd, sizeof(cwd));
/* Comment format: YYYYMMDD-XXXYYY,conffile,<filename>,<user>@<localhost>/<image path> */
CommentLength = IMAGE_COMMENT_BASESIZE + strlen(filename) + sizeof(LocalhostName) + sizeof(UserName) + MAX_PATH;
ROMImg->comment = (char *)calloc(0, CommentLength+1);
ROMImg->comment = (char *)malloc( CommentLength+1);
if (!ROMImg->comment) return ENOMEM;
snprintf(ROMImg->comment, CommentLength, "%08x,conffile,%s,%s@%s/%s", ROMImg->date, filename, BUFCHK(UserName), BUFCHK(LocalhostName), cwd);

Expand Down Expand Up @@ -453,15 +453,26 @@ static int AddExtInfoStat(struct FileEntry *file, unsigned char type, void *data
return result;
}

int AddFile(ROMIMG *ROMImg, const char *path)
int AddFile(ROMIMG *ROMImg, const char *path, int upperconv)
{
char tbuf[16] = "\0"; // we dont need a large buf, this is for filling in the filename on ROMFS
FILE *InputFile;
int result;
unsigned int FileDateStamp;
unsigned short FileVersion;
if ((InputFile = fopen(path, "rb")) != NULL) {
const char* fname = strrchr(path, PATHSEP);
if (fname == NULL) fname = path; else fname++;
if (upperconv) {
strncpy(tbuf, fname, sizeof(tbuf)-1);
tbuf[10] = '\0';
if (tbuf[0] != '\0') {
upperbuff(tbuf);
fname = tbuf;
char* T = strrchr(fname, '.');
if (T != NULL) *T = '\0'; //null terminate extension
}
}
int size;
fseek(InputFile, 0, SEEK_END);
size = ftell(InputFile);
Expand Down
4 changes: 2 additions & 2 deletions tools/romimg/src/romimg.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ int CreateBlankROMImg(const char *filename, ROMIMG *ROMImg);
int WriteROMImg(const char *file, const ROMIMG *ROMImg);
int LoadROMImg(ROMIMG *ROMImg, const char *path);
void UnloadROMImg(ROMIMG *ROMImg);
int AddFile(ROMIMG *ROMImg, const char *path);
int AddFile(ROMIMG *ROMImg, const char *path, int upperconv);
int DeleteFile(ROMIMG *ROMImg, const char *filename);
int ExtractFile(const ROMIMG *ROMImg, const char *filename, const char *FileToExtract);
int IsFileExists(const ROMIMG *ROMImg, const char *filename);

#endif /* __ROMING_H__ */
#endif /* __ROMING_H__ */

0 comments on commit bbe52ac

Please sign in to comment.