|
| 1 | +#include <malloc.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#define NEWLIB_PORT_AWARE |
| 6 | +#include <io_common.h> |
| 7 | + |
| 8 | +#include "cnf_lite.h" |
| 9 | + |
| 10 | +// Function prototypes. |
| 11 | +static int get_CNF_string(char **CNF_p_p, char **name_p_p, char **value_p_p); |
| 12 | + |
| 13 | +static int file_exists(const char *file) |
| 14 | +{ |
| 15 | + int fd, result; |
| 16 | + if ((fd = open(file, O_RDONLY)) >= 0) { |
| 17 | + result = 1; |
| 18 | + close(fd); |
| 19 | + } else |
| 20 | + result = 0; |
| 21 | + |
| 22 | + return result; |
| 23 | +} |
| 24 | + |
| 25 | +//________________ From uLaunchELF ______________________ |
| 26 | +//--------------------------------------------------------------------------- |
| 27 | +// get_CNF_string is the main CNF parser called for each CNF variable in a |
| 28 | +// CNF file. Input and output data is handled via its pointer parameters. |
| 29 | +// The return value flags 'false' when no variable is found. (normal at EOF) |
| 30 | +//--------------------------------------------------------------------------- |
| 31 | +static int get_CNF_string(char **CNF_p_p, char **name_p_p, char **value_p_p) |
| 32 | +{ |
| 33 | + char *np, *vp, *tp = *CNF_p_p; |
| 34 | + |
| 35 | +start_line: |
| 36 | + while ((*tp <= ' ') && (*tp > '\0')) |
| 37 | + tp += 1; // Skip leading whitespace, if any |
| 38 | + if (*tp == '\0') |
| 39 | + return 0; // but exit at EOF |
| 40 | + np = tp; // Current pos is potential name |
| 41 | + if (*tp < 'A') // but may be a comment line |
| 42 | + { // We must skip a comment line |
| 43 | + while ((*tp != '\r') && (*tp != '\n') && (*tp > '\0')) |
| 44 | + tp += 1; // Seek line end |
| 45 | + goto start_line; // Go back to try next line |
| 46 | + } |
| 47 | + |
| 48 | + while ((*tp >= 'A') || ((*tp >= '0') && (*tp <= '9'))) |
| 49 | + tp += 1; // Seek name end |
| 50 | + if (*tp == '\0') |
| 51 | + return 0; // but exit at EOF |
| 52 | + |
| 53 | + while ((*tp <= ' ') && (*tp > '\0')) |
| 54 | + *tp++ = '\0'; // zero&skip post-name whitespace |
| 55 | + if (*tp != '=') |
| 56 | + return 0; // exit (syntax error) if '=' missing |
| 57 | + *tp++ = '\0'; // zero '=' (possibly terminating name) |
| 58 | + |
| 59 | + while ((*tp <= ' ') && (*tp > '\0') // Skip pre-value whitespace, if any |
| 60 | + && (*tp != '\r') && (*tp != '\n') // but do not pass the end of the line |
| 61 | + && (*tp != '\7') // allow ctrl-G (BEL) in value |
| 62 | + ) |
| 63 | + tp += 1; |
| 64 | + if (*tp == '\0') |
| 65 | + return 0; // but exit at EOF |
| 66 | + vp = tp; // Current pos is potential value |
| 67 | + |
| 68 | + while ((*tp != '\r') && (*tp != '\n') && (*tp != '\0')) |
| 69 | + tp += 1; // Seek line end |
| 70 | + if (*tp != '\0') |
| 71 | + *tp++ = '\0'; // terminate value (passing if not EOF) |
| 72 | + while ((*tp <= ' ') && (*tp > '\0')) |
| 73 | + tp += 1; // Skip following whitespace, if any |
| 74 | + |
| 75 | + *CNF_p_p = tp; // return new CNF file position |
| 76 | + *name_p_p = np; // return found variable name |
| 77 | + *value_p_p = vp; // return found variable value |
| 78 | + return 1; // return control to caller |
| 79 | +} // Ends get_CNF_string |
| 80 | + |
| 81 | +//---------------------------------------------------------------- |
| 82 | +int Read_SYSTEM_CNF(char *boot_path, char *ver) |
| 83 | +{ |
| 84 | + // Returns disc type : 0 = failed; 1 = PS1; 2 = PS2; |
| 85 | + size_t CNF_size; |
| 86 | + char *RAM_p, *CNF_p, *name, *value; |
| 87 | + int fd = -1; |
| 88 | + int Disc_Type = -1; // -1 = Internal : Not Tested; |
| 89 | + |
| 90 | + // place 3 question mark in ver string |
| 91 | + strcpy(ver, "???"); |
| 92 | + |
| 93 | + fd = open("cdrom0:\\SYSTEM.CNF;1", O_RDONLY); |
| 94 | + if (fd < 0) { |
| 95 | + failed_load: |
| 96 | + if (Disc_Type == -1) { |
| 97 | + // Test PS1 special cases |
| 98 | + if (file_exists("cdrom0:\\PSXMYST\\MYST.CCS;1")) { |
| 99 | + strcpy(boot_path, "SLPS_000.24"); |
| 100 | + Disc_Type = 1; |
| 101 | + } else if (file_exists("cdrom0:\\CDROM\\LASTPHOT\\ALL_C.NBN;1")) { |
| 102 | + strcpy(boot_path, "SLPS_000.65"); |
| 103 | + Disc_Type = 1; |
| 104 | + } else if (file_exists("cdrom0:\\PSX.EXE;1")) { |
| 105 | + // place 3 question mark in pathname |
| 106 | + strcpy(boot_path, "???"); |
| 107 | + Disc_Type = 1; |
| 108 | + } |
| 109 | + } |
| 110 | + if (Disc_Type == -1) |
| 111 | + Disc_Type = 0; |
| 112 | + return Disc_Type; |
| 113 | + } // This point is only reached after succefully opening CNF |
| 114 | + |
| 115 | + Disc_Type = 0; |
| 116 | + |
| 117 | + CNF_size = lseek(fd, 0, SEEK_END); |
| 118 | + lseek(fd, 0, SEEK_SET); |
| 119 | + RAM_p = (char *)malloc(CNF_size); |
| 120 | + CNF_p = RAM_p; |
| 121 | + if (CNF_p == NULL) { |
| 122 | + close(fd); |
| 123 | + goto failed_load; |
| 124 | + } |
| 125 | + read(fd, CNF_p, CNF_size); // Read CNF as one long string |
| 126 | + close(fd); |
| 127 | + CNF_p[CNF_size] = '\0'; // Terminate the CNF string |
| 128 | + |
| 129 | + strcpy(boot_path, "???"); // place 3 question mark in boot path |
| 130 | + |
| 131 | + while (get_CNF_string(&CNF_p, &name, &value)) { |
| 132 | + // A variable was found, now we dispose of its value. |
| 133 | + if (!strcmp(name, "BOOT2")) { // check for BOOT2 entry |
| 134 | + strcpy(boot_path, value); |
| 135 | + Disc_Type = 2; // If found, PS2 disc type |
| 136 | + continue; |
| 137 | + } |
| 138 | + if (!strcmp(name, "BOOT")) { // check for BOOT entry |
| 139 | + strcpy(boot_path, value); |
| 140 | + Disc_Type = 1; // If found, PS1 disc type |
| 141 | + continue; |
| 142 | + } |
| 143 | + if (!strcmp(name, "VER")) { // check for VER entry |
| 144 | + strcpy(ver, value); |
| 145 | + continue; |
| 146 | + } |
| 147 | + } // ends for |
| 148 | + free(RAM_p); |
| 149 | + return Disc_Type; |
| 150 | +} |
0 commit comments