PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
main.c
1/*
2 main.c - Main program file.
3*/
4
5#include <errno.h>
6#include <stdio.h>
7#include <string.h>
8#include <sys/stat.h>
9#include <unistd.h>
10#include <ctype.h>
11
12#include "romimg.h"
13
14static void DisplayROMImgDetails(const ROMIMG *ROMImg)
15{
16 unsigned int i, TotalSize;
17 struct FileEntry *file;
18 char filename[11];
19 if (ROMImg->date != 0)
20 printf("ROM datestamp:\t%04x/%02x/%02x\n", ((unsigned short int *)&ROMImg->date)[1], ((unsigned char *)&ROMImg->date)[1], ((unsigned char *)&ROMImg->date)[0]);
21 if (ROMImg->comment != NULL)
22 printf("ROM comment:\t%s\n", ROMImg->comment);
23
24 printf("File list:\n"
25 GREEN"Name"DEFCOL" \tSize\n"
26 "-----------------------------\n");
27 for (i = 0, file = ROMImg->files, TotalSize = 0; i < ROMImg->NumFiles; TotalSize += file->RomDir.size, i++, file++) {
28 strncpy(filename, file->RomDir.name, sizeof(filename) - 1);
29 filename[sizeof(filename) - 1] = '\0';
30 printf(GREEN"%-10s"DEFCOL"\t%u\n", filename, file->RomDir.size);
31 }
32
33 printf("\nTotal size: %u bytes.\n", TotalSize);
34}
35
36static void DisplaySyntaxHelp(void)
37{
38 printf("Syntax:\n"
39 "ROMIMG -c <ROM image> <files>\n\tCreate ROM image *\n"
40 "ROMIMG -l <ROM image>\n\tList files in ROM image\n"
41 "ROMIMG -a <ROM image> <file(s)>\n\tAdd file(s) to ROM image *\n"
42 "ROMIMG -d <ROM image> <file(s)>\n\tDelete file(s) from ROM image\n"
43 "ROMIMG -x <ROM image>\n\tExtract all files from ROM image\n"
44 "ROMIMG -x <ROM image> <file>\n\tExtract file from ROM image\n"
45 "\n note*: write the switch in uppercase to perform filename transformation (eg: 'ioman.irx' > 'IOMAN')\n"
46 );
47}
48
49static void DisplayAddDeleteOperationResult(int result, const char *InvolvedFile)
50{
51 switch (result) {
52 case 0: // No error.
53 printf(GRNBOLD"done!"DEFCOL"\n");
54 break;
55 case ENOENT:
56 printf(YELBOLD"file not found."DEFCOL"\n");
57 break;
58 case EIO:
59 printf(REDBOLD"Error writing to file: %s"DEFCOL"\n", InvolvedFile);
60 break;
61 case EEXIST:
62 printf(YELBOLD"File already exists."DEFCOL"\n");
63 break;
64 default:
65 printf(REDBOLD"failed! code: %d"DEFCOL"\n", result);
66 }
67}
68
69int main(int argc, char **argv)
70{
71 int result;
72 FILE *OutputFile;
73 ROMIMG ROMImg;
74 unsigned int i, FilesAffected;
75 char filename[11];
76 struct FileEntry *file;
77
78 printf("PlayStation 2 ROM image generator v1.12\n"
79 "Compiled on " __DATE__ " - " __TIME__ "\n"
80 "---------------------------------------\n\n");
81 for (result = 0; result < argc; result++)
82 DPRINTF("\t -- argv[%d] = %s\n", result, argv[result]);
83
84 if (argc < 2) {
85 DisplaySyntaxHelp();
86 return EINVAL;
87 }
88
89 if (argc >= 4 && strcasecmp(argv[1], "-c") == 0) {
90 if ((result = CreateBlankROMImg(argv[2], &ROMImg)) == 0) {
91 for (FilesAffected = 0, i = 0; i < argc - 3; i++) {
92 printf("Adding file '%s'", argv[3 + i]);
93 if ((result = AddFile(&ROMImg, argv[3 + i], isupper(argv[1][1]))) == 0)
94 FilesAffected++;
95 printf(result == 0 ? GRNBOLD" done!"DEFCOL"\n" : REDBOLD" failed!"DEFCOL"\n");
96 }
97
98 if (FilesAffected > 0) {
99 printf("Writing image... ");
100 printf("%s", (result = WriteROMImg(argv[2], &ROMImg)) == 0 ? GRNBOLD"done!"DEFCOL"\n" : REDBOLD"failed!"DEFCOL"\n");
101 }
102 UnloadROMImg(&ROMImg);
103 } else
104 ERROR("(Internal fault) Can't create blank image file: %d (%s). Please report.\n", result, strerror(result));
105 } else if (argc >= 4 && strcasecmp(argv[1], "-a") == 0) {
106 if ((result = LoadROMImg(&ROMImg, argv[2])) == 0) {
107 for (i = 0, FilesAffected = 0; i < argc - 3; i++) {
108 printf("Adding file '%s'", argv[3 + i]);
109 if ((result = AddFile(&ROMImg, argv[3 + i], isupper(argv[1][1]))) == 0)
110 FilesAffected++;
111 DisplayAddDeleteOperationResult(result, argv[3 + i]);
112 }
113
114 if (FilesAffected > 0) {
115 printf("Writing image... ");
116 printf("%s", (result = WriteROMImg(argv[2], &ROMImg)) == 0 ? GRNBOLD"done!"DEFCOL"\n" : REDBOLD"failed!"DEFCOL"\n");
117 }
118 UnloadROMImg(&ROMImg);
119 } else
120 ERROR("Can't load image file: %s\n", argv[2]);
121 } else if (argc >= 4 && strcmp(argv[1], "-d") == 0) {
122 if ((result = LoadROMImg(&ROMImg, argv[2])) == 0) {
123 for (i = 0, FilesAffected = 0; i < argc - 3; i++) {
124 printf("Removing file %s...", argv[3 + i]);
125 if ((result = DeleteFile(&ROMImg, argv[3 + i])) == 0)
126 FilesAffected++;
127 DisplayAddDeleteOperationResult(result, argv[3 + i]);
128 }
129
130 if (FilesAffected > 0) {
131 printf("Writing image...");
132 printf("%s", (result = WriteROMImg(argv[2], &ROMImg)) == 0 ? GRNBOLD"done!"DEFCOL"\n" : REDBOLD"failed!"DEFCOL"\n");
133 }
134 UnloadROMImg(&ROMImg);
135 } else
136 ERROR("Can't load image file: %s\n", argv[2]);
137 } else if (argc == 3 && strcmp(argv[1], "-l") == 0) {
138 if ((result = LoadROMImg(&ROMImg, argv[2])) == 0) {
139 DisplayROMImgDetails(&ROMImg);
140 UnloadROMImg(&ROMImg);
141 } else
142 ERROR("Can't load image file: %s\n", argv[2]);
143 } else if ((argc == 3 || argc == 4) && strcmp(argv[1], "-x") == 0) {
144 if ((result = LoadROMImg(&ROMImg, argv[2])) == 0) {
145 if (argc == 3) {
146 char FOLDER[256] = "ext_";
147 strcat(FOLDER, argv[2]);
148#if defined(_WIN32) || defined(WIN32)
149 mkdir(FOLDER);
150#else
151 mkdir(FOLDER, 0755);
152#endif
153 if (chdir(FOLDER)) {
154 ERROR("Can't change directory to %s\n", FOLDER);
155 UnloadROMImg(&ROMImg);
156 return EINVAL;
157 }
158
159 printf("File list:\n"
160 GREEN"Name"DEFCOL" \tSize\n"
161 "-----------------------------\n");
162 for (i = 0, file = ROMImg.files; i < ROMImg.NumFiles; i++, file++) {
163 strncpy(filename, file->RomDir.name, sizeof(filename) - 1);
164 filename[sizeof(filename) - 1] = '\0';
165 printf(GREEN"%-10s"DEFCOL"\t%u\n", filename, file->RomDir.size);
166
167 if (file->RomDir.size > 0) {
168 if ((OutputFile = fopen(filename, "wb")) != NULL) {
169 if (fwrite(file->FileData, 1, file->RomDir.size, OutputFile) != file->RomDir.size) {
170 ERROR("Error writing to file %s\n", filename);
171 }
172 fclose(OutputFile);
173 } else {
174 ERROR("Can't create file: %s\n", filename);
175 }
176 }
177 }
178 } else {
179 printf("Extracting file %s...", argv[3]);
180 DisplayAddDeleteOperationResult(result = ExtractFile(&ROMImg, argv[3], argv[3]), argv[3]);
181 }
182
183 UnloadROMImg(&ROMImg);
184 } else
185 ERROR("Can't load image file: %s\n", argv[2]);
186 } else {
187 WARNING("Unrecognized command or incorrect syntax: %s\n", argv[1]);
188 DisplaySyntaxHelp();
189 result = EINVAL;
190 }
191
192 return result;
193}
#define ENOENT
Definition errno.h:23
#define EEXIST
Definition errno.h:53
#define EINVAL
Definition errno.h:63
#define EIO
Definition errno.h:29
Definition romimg.h:52