+static int handlepbp(const char *isofile) {
+ struct {
+ unsigned int sig;
+ unsigned int dontcare[8];
+ unsigned int psar_offs;
+ } pbp_hdr;
+ struct {
+ unsigned char type;
+ unsigned char pad0;
+ unsigned char track;
+ char index0[3];
+ char pad1;
+ char index1[3];
+ } toc_entry;
+ struct {
+ unsigned int offset;
+ unsigned int size;
+ unsigned int dontcare[6];
+ } index_entry;
+ char psar_sig[11];
+ unsigned int t, cd_length, cdimg_base;
+ unsigned int offsettab[8], psisoimg_offs;
+ const char *ext = NULL;
+ int i, ret;
+
+ if (strlen(isofile) >= 4)
+ ext = isofile + strlen(isofile) - 4;
+ if (ext == NULL || (strcmp(ext, ".pbp") != 0 && strcmp(ext, ".PBP") != 0))
+ return -1;
+
+ fseek(cdHandle, 0, SEEK_SET);
+
+ numtracks = 0;
+
+ ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), cdHandle);
+ if (ret != sizeof(pbp_hdr)) {
+ SysPrintf("failed to read pbp\n");
+ goto fail_io;
+ }
+
+ ret = fseek(cdHandle, pbp_hdr.psar_offs, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs);
+ goto fail_io;
+ }
+
+ psisoimg_offs = pbp_hdr.psar_offs;
+ fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
+ psar_sig[10] = 0;
+ if (strcmp(psar_sig, "PSTITLEIMG") == 0) {
+ // multidisk image?
+ ret = fseek(cdHandle, pbp_hdr.psar_offs + 0x200, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to %x\n", pbp_hdr.psar_offs + 0x200);
+ goto fail_io;
+ }
+
+ if (fread(&offsettab, 1, sizeof(offsettab), cdHandle) != sizeof(offsettab)) {
+ SysPrintf("failed to read offsettab\n");
+ goto fail_io;
+ }
+
+ for (i = 0; i < sizeof(offsettab) / sizeof(offsettab[0]); i++) {
+ if (offsettab[i] == 0)
+ break;
+ }
+ cdrIsoMultidiskCount = i;
+ if (cdrIsoMultidiskCount == 0) {
+ SysPrintf("multidisk eboot has 0 images?\n");
+ goto fail_io;
+ }
+
+ if (cdrIsoMultidiskSelect >= cdrIsoMultidiskCount)
+ cdrIsoMultidiskSelect = 0;
+
+ psisoimg_offs += offsettab[cdrIsoMultidiskSelect];
+
+ ret = fseek(cdHandle, psisoimg_offs, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to %x\n", psisoimg_offs);
+ goto fail_io;
+ }
+
+ fread(psar_sig, 1, sizeof(psar_sig), cdHandle);
+ psar_sig[10] = 0;
+ }
+
+ if (strcmp(psar_sig, "PSISOIMG00") != 0) {
+ SysPrintf("bad psar_sig: %s\n", psar_sig);
+ goto fail_io;
+ }
+
+ // seek to TOC
+ ret = fseek(cdHandle, psisoimg_offs + 0x800, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to %x\n", psisoimg_offs + 0x800);
+ goto fail_io;
+ }
+
+ // first 3 entries are special
+ fseek(cdHandle, sizeof(toc_entry), SEEK_CUR);
+ fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
+ numtracks = btoi(toc_entry.index1[0]);
+
+ fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
+ cd_length = btoi(toc_entry.index1[0]) * 60 * 75 +
+ btoi(toc_entry.index1[1]) * 75 + btoi(toc_entry.index1[2]);
+
+ for (i = 1; i <= numtracks; i++) {
+ fread(&toc_entry, 1, sizeof(toc_entry), cdHandle);
+
+ ti[i].type = (toc_entry.type == 1) ? CDDA : DATA;
+
+ ti[i].start_offset = btoi(toc_entry.index0[0]) * 60 * 75 +
+ btoi(toc_entry.index0[1]) * 75 + btoi(toc_entry.index0[2]);
+ ti[i].start_offset *= 2352;
+ ti[i].start[0] = btoi(toc_entry.index1[0]);
+ ti[i].start[1] = btoi(toc_entry.index1[1]);
+ ti[i].start[2] = btoi(toc_entry.index1[2]);
+
+ if (i > 1) {
+ t = msf2sec(ti[i].start) - msf2sec(ti[i - 1].start);
+ sec2msf(t, ti[i - 1].length);
+ }
+ }
+ t = cd_length - ti[numtracks].start_offset / 2352;
+ sec2msf(t, ti[numtracks].length);
+
+ // seek to ISO index
+ ret = fseek(cdHandle, psisoimg_offs + 0x4000, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to ISO index\n");
+ goto fail_io;
+ }
+
+ compr_img = calloc(1, sizeof(*compr_img));
+ if (compr_img == NULL)
+ goto fail_io;
+
+ compr_img->block_shift = 4;
+ compr_img->current_block = (unsigned int)-1;
+
+ compr_img->index_len = (0x100000 - 0x4000) / sizeof(index_entry);
+ compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
+ if (compr_img->index_table == NULL)
+ goto fail_io;
+
+ cdimg_base = psisoimg_offs + 0x100000;
+ for (i = 0; i < compr_img->index_len; i++) {
+ ret = fread(&index_entry, 1, sizeof(index_entry), cdHandle);
+ if (ret != sizeof(index_entry)) {
+ SysPrintf("failed to read index_entry #%d\n", i);
+ goto fail_index;
+ }
+
+ if (index_entry.size == 0)
+ break;
+
+ compr_img->index_table[i] = cdimg_base + index_entry.offset;
+ }
+ compr_img->index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
+
+ return 0;
+
+fail_index:
+ free(compr_img->index_table);
+ compr_img->index_table = NULL;
+fail_io:
+ if (compr_img != NULL) {
+ free(compr_img);
+ compr_img = NULL;
+ }
+ return -1;
+}
+
+static int handlecbin(const char *isofile) {
+ struct
+ {
+ char magic[4];
+ unsigned int header_size;
+ unsigned long long total_bytes;
+ unsigned int block_size;
+ unsigned char ver; // 1
+ unsigned char align;
+ unsigned char rsv_06[2];
+ } ciso_hdr;
+ const char *ext = NULL;
+ unsigned int index = 0, plain;
+ int i, ret;
+
+ if (strlen(isofile) >= 5)
+ ext = isofile + strlen(isofile) - 5;
+ if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
+ return -1;
+
+ fseek(cdHandle, 0, SEEK_SET);
+
+ ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
+ if (ret != sizeof(ciso_hdr)) {
+ SysPrintf("failed to read ciso header\n");
+ return -1;
+ }
+
+ if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
+ SysPrintf("bad ciso header\n");
+ return -1;
+ }
+ if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
+ ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
+ if (ret != 0) {
+ SysPrintf("failed to seek to %x\n", ciso_hdr.header_size);
+ return -1;
+ }
+ }
+
+ compr_img = calloc(1, sizeof(*compr_img));
+ if (compr_img == NULL)
+ goto fail_io;
+
+ compr_img->block_shift = 0;
+ compr_img->current_block = (unsigned int)-1;
+
+ compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
+ compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
+ if (compr_img->index_table == NULL)
+ goto fail_io;
+
+ ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
+ if (ret != compr_img->index_len) {
+ SysPrintf("failed to read index table\n");
+ goto fail_index;
+ }
+
+ for (i = 0; i < compr_img->index_len + 1; i++) {
+ index = compr_img->index_table[i];
+ plain = index & 0x80000000;
+ index &= 0x7fffffff;
+ compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
+ }
+ if ((long long)index << ciso_hdr.align >= 0x80000000ll)
+ SysPrintf("warning: ciso img too large, expect problems\n");
+
+ return 0;
+
+fail_index:
+ free(compr_img->index_table);
+ compr_img->index_table = NULL;
+fail_io:
+ if (compr_img != NULL) {
+ free(compr_img);
+ compr_img = NULL;
+ }
+ return -1;
+}
+