// libcdvd (libraries.md section 8): the disc image's sectors, the stream, the drive's time. What TMB calls: sceCdInit, // sceCdMmode, sceCdDiskReady, sceFsReset, sceCdReadClock, sceCdRead, sceCdSync, sceCdGetError, sceCdStInit, // sceCdStStart, sceCdStRead, sceCdStStop, sceCdStPause, sceCdStResume. #include "bozo/libs/libraries.h" #include "bozo/ee/dispatcher.h" #include #include #include #include #include namespace bozo::libs { // ---- DiscImage ----------------------------------------------------------------------------------------------- namespace { bool seek(std::FILE *f, uint64_t at) { #ifdef _WIN32 return _fseeki64(f, static_cast(at), SEEK_SET) == 0; #else return fseeko(f, static_cast(at), SEEK_SET) == 0; #endif } } DiscImage::~DiscImage() { if (f_) std::fclose(f_); } bool DiscImage::open(const std::string &path, std::string &error) { if (f_) std::fclose(f_); f_ = std::fopen(path.c_str(), "rb"); if (!f_) { error = "cannot open the disc image " + path; return false; } path_ = path; uint64_t size = 0; #ifdef _WIN32 _fseeki64(f_, 0, SEEK_END); size = static_cast(_ftelli64(f_)); #else fseeko(f_, 0, SEEK_END); size = static_cast(ftello(f_)); #endif sectors_ = static_cast(size / kSectorBytes); // An ISO 9660 image has its primary volume descriptor at sector 16 ("\1CD001"). uint8_t pvd[kSectorBytes]; if (!read(16, 1, pvd) || pvd[0] != 1 || std::string(reinterpret_cast(pvd + 1), 5) != "CD001") { error = path + " is not an ISO 9660 image (no primary volume descriptor at sector 16)"; std::fclose(f_); f_ = nullptr; return false; } return true; } bool DiscImage::read(uint32_t lbn, uint32_t count, uint8_t *out) { if (!f_) return false; std::memset(out, 0, static_cast(count) * kSectorBytes); if (lbn >= sectors_) return true; const uint32_t n = std::min(count, sectors_ - lbn); if (!seek(f_, static_cast(lbn) * kSectorBytes)) return false; return std::fread(out, kSectorBytes, n, f_) == n; } bool DiscImage::layer1Start(uint32_t &start) { uint8_t pvd[kSectorBytes]; if (!read(16, 1, pvd)) return false; const uint32_t layer0 = uint32_t(pvd[80]) | uint32_t(pvd[81]) << 8 | uint32_t(pvd[82]) << 16 | uint32_t(pvd[83]) << 24; if (layer0 <= 16 || layer0 >= sectors_ || !read(layer0, 1, pvd) || pvd[0] != 1 || std::string(reinterpret_cast(pvd + 1), 5) != "CD001") return false; start = layer0 - 16; return true; } bool DiscImage::findFile(const std::string &path, uint32_t &lbn, uint32_t &bytes, uint32_t layer) { const auto word = [](const uint8_t *p) { return uint32_t(p[0]) | uint32_t(p[1]) << 8 | uint32_t(p[2]) << 16 | uint32_t(p[3]) << 24; }; const auto normal = [](std::string value) { const size_t version = value.find(';'); if (version != std::string::npos) value.resize(version); for (char &c : value) c = static_cast(std::toupper(static_cast(c))); return value; }; std::string clean = path; for (char &c : clean) if (c == '\\') c = '/'; if (const auto colon = clean.find(':'); colon != std::string::npos) clean.erase(0, colon + 1); std::vector parts; for (size_t at = 0; at < clean.size();) { const size_t end = clean.find('/', at); const auto part = clean.substr(at, end == std::string::npos ? end : end - at); if (!part.empty() && part != ".") parts.push_back(normal(part)); if (end == std::string::npos) break; at = end + 1; } if (parts.empty()) return false; uint32_t base = 0; if (layer > 1 || (layer == 1 && !layer1Start(base))) return false; uint8_t pvd[kSectorBytes]; if (!read(base + 16, 1, pvd) || pvd[156] < 34) return false; uint32_t sector = base + word(pvd + 158), size = word(pvd + 166); for (size_t part = 0; part < parts.size(); ++part) { if (!size || size > 8u * 1024 * 1024) return false; const uint32_t count = (size + kSectorBytes - 1) / kSectorBytes; if (sector >= sectors_ || count > sectors_ - sector) return false; std::vector directory(size_t(count) * kSectorBytes); if (!read(sector, count, directory.data())) return false; bool found = false; for (size_t at = 0; at < size;) { const uint32_t length = directory[at]; if (!length) { at = (at / kSectorBytes + 1) * kSectorBytes; continue; } if (length < 34 || at + length > size) return false; const uint8_t *record = directory.data() + at; if (33u + record[32] > length) return false; const std::string name(reinterpret_cast(record + 33), record[32]); if (normal(name) == parts[part]) { if (part + 1 < parts.size() && !(record[25] & 2)) return false; sector = base + word(record + 2); size = word(record + 10); found = true; break; } at += length; } if (!found) return false; } lbn = sector; bytes = size; return true; } bool discRead(Libraries &l, uint32_t lbn, uint32_t count, uint8_t *out) { CdState &cd = l.cd; if (!cd.disc.isOpen()) { std::string err; if (cd.imagePath.empty() || !cd.disc.open(cd.imagePath, err)) return false; } return cd.disc.read(lbn, count, out); } // ---- The library --------------------------------------------------------------------------------------------- namespace { // Copies `count` sectors from `lbn` to guest memory at `to`. False when the disc cannot be read. bool sectors(Libraries &l, uint32_t lbn, uint32_t count, std::vector &data) { CdState &cd = l.cd; if (!cd.disc.isOpen()) { std::string err; if (cd.imagePath.empty() || !cd.disc.open(cd.imagePath, err)) return false; } data.resize(static_cast(count) * kSectorBytes); return cd.disc.read(lbn, count, data.data()); } bool sectorsTo(Libraries &l, Guest &g, uint32_t lbn, uint32_t count, uint32_t to) { std::vector data; if (!sectors(l, lbn, count, data)) return false; g.write(to, data.data(), static_cast(data.size())); return true; } uint8_t bcd(int v) { return static_cast((v / 10) * 16 + v % 10); } // sceCdInit(mode), sceCdMmode(media): 1. sceCdDiskReady(mode): 2 (ready). sceFsReset(): 0. void init(Libraries &l, Guest &g) { l.cd.initialized=g.arg(0)!=2;l.cd.error=0;g.ret(1); } void status(Libraries &l, Guest &g) { // Public SDK status values from libcdvd-common.h. These describe // the native file job, not an addressable console device bank. const auto &cd=l.cd; g.ret(!cd.initialized?0:cd.error?0x20:(cd.streaming || l.now(g)r[28].uw[0]; g.ret(static_cast(old)); } void initCallback(Libraries &l, Guest &g) { if(l.cd.callbackConfigured) { g.ret(0); return; } // sceCdInitEeCB(priority, stack, stack_bytes): native completion uses // the handler queue. Keep its caller-supplied stack, without an EE // SDK worker waiting on private semaphore globals. const uint32_t stack=g.arg(1),bytes=g.arg(2); if(g.arg(0)>=ee::kPriorities || !bytes || stack>0xffffffffu-bytes || !g.runtime().memory.hostPointer(stack,bytes)) { g.ret(-1); return; } l.cd.callbackStack=stack+bytes;l.cd.callbackConfigured=true;g.ret(1); } // sceCdReadClock(sceCdCLOCK *): stat, second, minute, hour, pad, day, month, year (BCD); 1. void readClock(Libraries &l, Guest &g) { int sec = 0, min = 0, hour = 12, day = 18, month = 6, year = 1; // the reference's deterministic clock if (l.cd.hostClock) { const std::time_t t = std::time(nullptr); if (const std::tm *tm = std::localtime(&t)) { sec = tm->tm_sec; min = tm->tm_min; hour = tm->tm_hour; day = tm->tm_mday; month = tm->tm_mon + 1; year = tm->tm_year % 100; } } const uint8_t c[8] = {0, bcd(sec), bcd(min), bcd(hour), 0, bcd(day), bcd(month), bcd(year)}; g.write(g.arg(0), c, 8); g.ret(1); } void readEnds(Libraries &l, Guest &g, bool ok, uint32_t n) { CdState &cd = l.cd; cd.error = ok ? 0 : -1; cd.readBytes=ok?n*kSectorBytes:0; const uint64_t drive = static_cast(n) * kSectorCycles; g.cycles = l.callCycles(cd.timedCompletion ? 0 : drive); cd.readEnd = l.now(g) + (cd.timedCompletion ? g.cycles + drive : g.cycles); if(ok && cd.callback) { if(!l.dispatcher()) throw std::runtime_error("CD callback requires native dispatcher"); ee::QueuedHandler h;h.kind=ee::kInvokeLibraryCall;h.pc=cd.callback; h.gp=cd.callbackGp;h.sp=cd.callbackStack;h.a[0]=1; // SCECdFuncRead if(cd.timedCompletion) l.dispatcher()->kernel().scheduleDeadline(kReadDoneKey, cd.readEnd, h); else // The read returns after charging its full duration; this callback cannot execute inside that // call. l.queueHandler(h, Resume{}); } g.ret(ok ? 1 : 0); } // sceCdRead(lbn, sectors, buffer, mode): the sectors are in the buffer when it returns, after a drive's time; // 1 (0 when the disc cannot be read). void read(Libraries &l, Guest &g) { const uint32_t lbn = g.arg(0), n = g.arg(1), to = g.arg(2); readEnds(l, g, sectorsTo(l, g, lbn, n, to), n); } // sceCdReadIOPm(lbn, sectors, IOP buffer, mode): as sceCdRead, into the memory of the port's compiled modules // (SifState::transfer); 0 without such memory. void readIop(Libraries &l, Guest &g) { const uint32_t lbn = g.arg(0), n = g.arg(1), to = g.arg(2); std::vector data; const bool ok = l.sif.transfer && sectors(l, lbn, n, data); if (ok) l.sif.transfer(to, data.data(), static_cast(data.size())); readEnds(l, g, ok, n); } // sceCdSync(mode): mode 0 waits for the read to finish (a vblank wait, its v0 0), then 0; mode 1: 1 while a // read is in progress, else 0. void sync(Libraries &l, Guest &g) { CdState &cd = l.cd; const uint64_t now = l.now(g); const bool busy = now < cd.readEnd; if (g.arg(0) != 0) { g.ret(busy ? 1 : 0); return; } uint64_t token = 0; g.cycles = l.callCycles(0); if (l.waitPoint(busy, l.vblankTokenFor(cd.readEnd), token)) { l.waitVblank(token, 0); return; } g.ret(0); } // sceCdGetError(): the last read's error (0: none). void getError(Libraries &l, Guest &g) { g.ret(l.cd.error); } // sceCdStInit(ring sectors, banks, IOP buffer): 1. void stInit(Libraries &l, Guest &g) { CdState &cd = l.cd; cd.ringSectors = g.arg(0); cd.banks = g.arg(1); cd.iopBuffer = g.arg(2); g.ret(1); } // sceCdStStart(lbn, mode *): the stream starts at lbn; 1. void stStart(Libraries &l, Guest &g) { CdState &cd = l.cd; cd.streaming = true; cd.paused = false; cd.streamLbn = g.arg(0); cd.delivered = 0; cd.produced = 0; cd.producedAt = l.now(g); g.ret(1); } // sceCdStStop(): 1. void stStop(Libraries &l, Guest &g) { l.cd.streaming = false; l.cd.paused = false; g.ret(1); } void stPause(Libraries &l, Guest &g) { CdState &cd = l.cd; cd.advance(l.now(g)); cd.paused = true; g.ret(1); } void stResume(Libraries &l, Guest &g) { CdState &cd = l.cd; cd.advance(l.now(g)); cd.paused = false; g.ret(1); } // The sectors of a stream read go to the game: the next `n` of the stream. void deliver(Libraries &l, Guest &g, uint32_t n, uint32_t to, uint32_t errorAt) { CdState &cd = l.cd; const bool ok = sectorsTo(l, g, cd.streamLbn + cd.delivered, n, to); cd.delivered += n; if (errorAt) g.write32(errorAt, ok ? 0 : 1); g.ret(static_cast(n)); } // sceCdStRead(sectors, buffer, mode, error *): the next sectors of the stream. Those that have arrived are // copied at once; mode 1 (TMB's) waits for the rest: a vblank wait (its result the vblank's parity) finished // as the thread resumes, by their copy and v0 = sectors. Returns the sectors copied (mode 0: those there). void stRead(Libraries &l, Guest &g) { CdState &cd = l.cd; const uint32_t n = g.arg(0), to = g.arg(1), mode = g.arg(2), errorAt = g.arg(3); cd.advance(l.now(g)); uint64_t available = std::min(n, cd.produced - cd.delivered); if (l.pin.active) { // The ring's state when the reference made the call: what it copied at once. const int64_t base = GuestMemory::flatOffset(to, 1); available = 0; if (base >= 0 && l.pin.writtenEnd > base) available = std::min(n, (l.pin.writtenEnd - static_cast(base) + kSectorBytes - 1) / kSectorBytes); } g.cycles = l.callCycles(0); uint64_t token = 0; if (l.waitPoint(mode == 1 && available < n, l.vblankTokenFor(cd.readyAt(n)), token)) { const uint32_t now = static_cast(available); if (now) sectorsTo(l, g, cd.streamLbn + cd.delivered, now, to); cd.delivered += now; cd.pendingSectors = n - now; cd.pendingBuffer = to + now * kSectorBytes; cd.pendingError = errorAt; l.waitVblank(token, -1, Resume{kResumeCdStreamRead, {n}}); return; } if (mode == 0 && cd.wholeStreamReads && available < n) available = 0; deliver(l, g, mode == 1 ? n : static_cast(available), to, errorAt); } } namespace { void searchLayer(Libraries &l, Guest &g, uint32_t layer) { // sceCdlFILE: sector, byte length, name[16], date[8]. uint8_t sector[kSectorBytes]; if (!discRead(l, 16, 1, sector)) { g.ret(0); return; } const std::string name = g.string(g.arg(1)); uint32_t lbn = 0, bytes = 0; if (!l.cd.disc.findFile(name, lbn, bytes, layer)) { g.ret(0); return; } uint8_t result[32] = {}; std::memcpy(result, &lbn, 4); std::memcpy(result + 4, &bytes, 4); const auto slash = name.find_last_of("/\\"); const auto leaf = name.substr(slash == std::string::npos ? 0 : slash + 1); std::memcpy(result + 8, leaf.data(), std::min(16, leaf.size())); g.write(g.arg(0), result, sizeof(result)); g.ret(1); } void searchFile(Libraries &l, Guest &g) { searchLayer(l, g, 0); } // sceCdLayerSearchFile(file, name, layer). void layerSearchFile(Libraries &l, Guest &g) { searchLayer(l, g, g.arg(2)); } // sceCdReadDvdDualInfo(int *onDual, u_int *layer1Start). void readDvdDualInfo(Libraries &l, Guest &g) { uint8_t sector[kSectorBytes]; uint32_t start = 0; const int32_t dual = discRead(l, 16, 1, sector) && l.cd.disc.layer1Start(start) ? 1 : 0; if (!dual) start = 0; g.write(g.arg(0), &dual, sizeof(dual)); g.write(g.arg(1), &start, sizeof(start)); g.ret(1); } } namespace { void fsInit(Libraries &, Guest &g) { g.ret(0); } void fsOpen(Libraries &l, Guest &g) { uint8_t sector[kSectorBytes]; if ((g.arg(1) & 3u) != 1u || !discRead(l, 16, 1, sector)) { g.ret(-1); return; } uint32_t lbn=0, bytes=0; if (!l.cd.disc.findFile(g.string(g.arg(0)),lbn,bytes)) { g.ret(-1); return; } const int32_t id=l.cd.nextFile++; l.cd.files.emplace(id,CdState::File{lbn,bytes,0}); g.ret(id); } void fsClose(Libraries &l, Guest &g) { g.ret(l.cd.files.erase(g.sarg(0)) ? 0 : -1); } // sceIoctl(fd, request, arg): request 1 is the status a file opened with O_NOWAIT (0x8001) is polled with: 1 // while an operation runs. The native sceOpen / sceRead have finished as they return: *arg = 0, done (Amplitude // polls it after each, 0x1af6a0 and 0x28f528). Any other request: -1. void fsIoctl(Libraries &, Guest &g) { const uint32_t request = g.arg(1), arg = g.arg(2); if (request == 1u && arg) { g.write32(arg, 0); g.ret(0); return; } g.ret(-1); } // sceGetstat(name, struct sce_stat *): a disc file's size, as a read-only regular file (iomanX's sce_stat: // mode +0, attr +4, size +8, ctime/atime/mtime +12..+35, hisize +36, private +40..+63); -1 when it is not on // the disc. The image is opened first, as sceOpen does. Shinobido checks its files with it before reading // them (0x2464e0). void fsGetstat(Libraries &l, Guest &g) { uint32_t lbn=0, bytes=0; uint8_t sector[kSectorBytes]; if (!discRead(l, 16, 1, sector) || !l.cd.disc.findFile(g.string(g.arg(0)),lbn,bytes)) { g.ret(-1); return; } if (const uint32_t stat=g.arg(1)) { for (uint32_t i=0; i<64; i+=4) g.write32(stat+i, 0); g.write32(stat+0x00, 0x2000u | 0x0124u); // FIO_S_IFREG, read permission g.write32(stat+0x08, bytes); g.write32(stat+0x28, lbn); // private[0]: the file's first sector, as cdvdman fills it } g.ret(0); } void fsSeek(Libraries &l, Guest &g) { auto it=l.cd.files.find(g.sarg(0)); if (it==l.cd.files.end() || g.arg(2)>2) { g.ret(-1); return; } auto &f=it->second; const int64_t base=g.arg(2)==0 ? 0 : g.arg(2)==1 ? f.position : f.bytes; const int64_t pos=base+g.sarg(1); if (pos<0 || pos>INT32_MAX) { g.ret(-1); return; } f.position=static_cast(pos); g.ret(static_cast(pos)); } void fsRead(Libraries &l, Guest &g) { auto it=l.cd.files.find(g.sarg(0)); if (it==l.cd.files.end() || g.sarg(2)<0) { g.ret(-1); return; } auto &f=it->second; const uint32_t bytes=std::min(g.arg(2),f.position data(size_t(count)*kSectorBytes); if (!discRead(l,f.sector+f.position/kSectorBytes,count,data.data())) { g.ret(-1); return; } g.write(g.arg(1),data.data()+skip,bytes); f.position+=bytes; g.ret(static_cast(bytes)); } void fsWrite(Libraries &, Guest &g) { if (g.arg(0)!=1 && g.arg(0)!=2) { g.ret(-1); return; } const auto *data=g.in(g.arg(1),g.arg(2)); g.ret(static_cast(std::fwrite(data,1,g.arg(2),g.arg(0)==1 ? stdout : stderr))); } } // A blocking stream read's end, as its thread resumes: the rest of the sectors, then the count asked for. void cdResume(Libraries &l, Guest &g, const Resume &r) { CdState &c = l.cd; c.advance(l.now(g)); deliver(l, g, c.pendingSectors, c.pendingBuffer, c.pendingError); c.pendingSectors = 0; g.ret(static_cast(r.a[0])); } void addCd(std::vector &out) { out.push_back({"sceFsInit", kCd, fsInit}); out.push_back({"sceOpen", kCd, fsOpen}); out.push_back({"sceClose", kCd, fsClose}); out.push_back({"sceRead", kCd, fsRead}); out.push_back({"sceWrite", kCd, fsWrite}); out.push_back({"sceLseek", kCd, fsSeek}); out.push_back({"sceGetstat", kCd, fsGetstat}); out.push_back({"sceIoctl", kCd, fsIoctl}); out.push_back({"sceCdSearchFile", kCd, searchFile}); out.push_back({"sceCdLayerSearchFile", kCd, layerSearchFile}); out.push_back({"sceCdReadDvdDualInfo", kCd, readDvdDualInfo}); out.push_back({"sceCdInit", kCd, init}); out.push_back({"sceCdStatus", kCd, status}); out.push_back({"sceCdGetReadPos", kCd, readPosition}); out.push_back({"sceCdCallback", kCd, callback}); out.push_back({"sceCdInitEeCB", kCd, initCallback}); out.push_back({"sceCdMmode", kCd, mmode}); out.push_back({"sceCdDiskReady", kCd, diskReady}); out.push_back({"sceCdNcmdDiskReady", kCd, ncmdDiskReady}); out.push_back({"sceCdSyncS", kCd, syncS}); out.push_back({"sceCdGetDiskType", kCd, diskType}); out.push_back({"sceCdBreak", kCd, breakCommand}); out.push_back({"sceCdSeek", kCd, seek, false, true}); out.push_back({"sceFsReset", kCd, fsReset}); out.push_back({"sceCdReadClock", kCd, readClock}); out.push_back({"sceCdRead", kCd, read, false, true}); out.push_back({"sceCdReadIOPm", kCd, readIop, false, true}); out.push_back({"sceCdSync", kCd, sync, false, true}); out.push_back({"sceCdGetError", kCd, getError}); out.push_back({"sceCdStInit", kCd, stInit}); out.push_back({"sceCdStStart", kCd, stStart}); out.push_back({"sceCdStRead", kCd, stRead, false, true}); out.push_back({"sceCdStStop", kCd, stStop}); out.push_back({"sceCdStPause", kCd, stPause}); out.push_back({"sceCdStResume", kCd, stResume}); } }