Ejemplo n.º 1
0
Archivo: VFS.cpp Proyecto: ss23/rpcs3
u64 VFS::GetDirSize(const std::string& ps3_path) const
{
	u64 result = 0;

	for (const auto entry : vfsDir(ps3_path))
	{
		if (entry->name == "." || entry->name == "..")
		{
			continue;
		}

		if (entry->flags & DirEntry_TypeFile)
		{
			result += entry->size;
		}

		if (entry->flags & DirEntry_TypeDir)
		{
			result += GetDirSize(ps3_path + "/" + entry->name);
		}
	}

	return result;
}
Ejemplo n.º 2
0
s32 npDrmIsAvailable(u32 k_licensee_addr, vm::cptr<char> drm_path)
{
	if (!Emu.GetVFS().ExistsFile(drm_path.get_ptr()))
	{
		sceNp.Warning("npDrmIsAvailable(): '%s' not found", drm_path.get_ptr());
		return CELL_ENOENT;
	}

	std::string k_licensee_str = "0";
	u8 k_licensee[0x10];

	if (k_licensee_addr)
	{
		for (s32 i = 0; i < 0x10; i++)
		{
			k_licensee[i] = vm::read8(k_licensee_addr + i);
			k_licensee_str += fmt::format("%02x", k_licensee[i]);
		}
	}

	sceNp.Warning("npDrmIsAvailable(): Found DRM license file at %s", drm_path.get_ptr());
	sceNp.Warning("npDrmIsAvailable(): Using k_licensee 0x%s", k_licensee_str.c_str());

	// Set the necessary file paths.
	std::string drm_file_name = fmt::AfterLast(drm_path.get_ptr(), '/');

	// TODO: Make more explicit what this actually does (currently it copies "XXXXXXXX" from drm_path (== "/dev_hdd0/game/XXXXXXXXX/*" assumed)
	std::string titleID(&drm_path[15], 9);

	// TODO: These shouldn't use current dir
	std::string enc_drm_path = drm_path.get_ptr();
	std::string dec_drm_path = "/dev_hdd1/cache/" + drm_file_name;
	std::string pf_str("00000001");  // TODO: Allow multiple profiles. Use default for now.
	std::string rap_path("/dev_hdd0/home/" + pf_str + "/exdata/");

	// Search dev_usb000 for a compatible RAP file. 
	for (const auto entry : vfsDir(rap_path))
	{
		if (entry->name.find(titleID) != std::string::npos)
		{
			rap_path += entry->name;
			break;
		}
	}

	if (rap_path.back() == '/')
	{
		sceNp.Warning("npDrmIsAvailable(): Can't find RAP file for '%s' (titleID='%s')", drm_path.get_ptr(), titleID);
	}

	// Decrypt this EDAT using the supplied k_licensee and matching RAP file.
	std::string enc_drm_path_local, dec_drm_path_local, rap_path_local;
	Emu.GetVFS().GetDevice(enc_drm_path, enc_drm_path_local);
	Emu.GetVFS().GetDevice(dec_drm_path, dec_drm_path_local);
	Emu.GetVFS().GetDevice(rap_path, rap_path_local);

	if (DecryptEDAT(enc_drm_path_local, dec_drm_path_local, 8, rap_path_local, k_licensee, false) >= 0)
	{
		// If decryption succeeds, replace the encrypted file with it.
		fs::remove_file(enc_drm_path_local);
		fs::rename(dec_drm_path_local, enc_drm_path_local);
	}

	return CELL_OK;
}
Ejemplo n.º 3
0
never_inline s32 savedata_op(PPUThread& ppu, u32 operation, u32 version, vm::cptr<char> dirName, u32 errDialog, PSetList setList, PSetBuf setBuf, PFuncList funcList, PFuncFixed funcFixed, PFuncStat funcStat, PFuncFile funcFile, u32 container, u32 unknown, vm::ptr<void> userdata, u32 userId, PFuncDone funcDone)
{
	// TODO: check arguments

	// try to lock the mutex (not sure how it originally works; std::try_to_lock makes it non-blocking)
	std::unique_lock<std::mutex> lock(g_savedata_dialog->mutex, std::try_to_lock);

	if (!lock)
	{
		return CELL_SAVEDATA_ERROR_BUSY;
	}

	struct _stack_t
	{
		CellSaveDataCBResult  result;
		CellSaveDataListGet   listGet;
		CellSaveDataListSet   listSet;
		CellSaveDataFixedSet  fixedSet;
		CellSaveDataStatGet   statGet;
		CellSaveDataStatSet   statSet;
		CellSaveDataFileGet   fileGet;
		CellSaveDataFileSet   fileSet;
	};

	const vm::var<_stack_t> stack(ppu);

	const auto result   = stack.ptr(&_stack_t::result);
	const auto listGet  = stack.ptr(&_stack_t::listGet);
	const auto listSet  = stack.ptr(&_stack_t::listSet);
	const auto fixedSet = stack.ptr(&_stack_t::fixedSet);
	const auto statGet  = stack.ptr(&_stack_t::statGet);
	const auto statSet  = stack.ptr(&_stack_t::statSet);
	const auto fileGet  = stack.ptr(&_stack_t::fileGet);
	const auto fileSet  = stack.ptr(&_stack_t::fileSet);

	// path of the specified user (00000001 by default)
	const std::string base_dir = fmt::format("/dev_hdd0/home/%08u/savedata/", userId ? userId : 1u);

	result->userdata = userdata; // probably should be assigned only once (allows the callback to change it)

	SaveDataEntry save_entry;

	if (setList)
	{
		std::vector<SaveDataEntry> save_entries;

		listGet->dirNum = 0;
		listGet->dirListNum = 0;
		listGet->dirList.set(setBuf->buf.addr());
		memset(listGet->reserved, 0, sizeof(listGet->reserved));

		const auto prefix_list = fmt::split(setList->dirNamePrefix.get_ptr(), { "|" });

		for (const auto entry : vfsDir(base_dir))
		{
			if (entry->flags & DirEntry_TypeFile)
			{
				continue;
			}

			for (const auto& prefix : prefix_list)
			{
				if (entry->name.substr(0, prefix.size()) == prefix)
				{
					// Count the amount of matches and the amount of listed directories
					if (listGet->dirListNum++ < setBuf->dirListMax)
					{
						listGet->dirNum++;

						// PSF parameters
						vfsFile f(base_dir + entry->name + "/PARAM.SFO");
						const PSFLoader psf(f);

						if (!psf)
						{
							break;
						}

						SaveDataEntry save_entry2;
						save_entry2.dirName = psf.GetString("SAVEDATA_DIRECTORY");
						save_entry2.listParam = psf.GetString("SAVEDATA_LIST_PARAM");
						save_entry2.title = psf.GetString("TITLE");
						save_entry2.subtitle = psf.GetString("SUB_TITLE");
						save_entry2.details = psf.GetString("DETAIL");

						save_entry2.size = 0;

						for (const auto entry2 : vfsDir(base_dir + entry->name))
						{
							save_entry2.size += entry2->size;
						}

						save_entry2.atime = entry->access_time;
						save_entry2.mtime = entry->modify_time;
						save_entry2.ctime = entry->create_time;
						//save_entry2.iconBuf = NULL; // TODO: Here should be the PNG buffer
						//save_entry2.iconBufSize = 0; // TODO: Size of the PNG file
						save_entry2.isNew = false;

						save_entries.push_back(save_entry2);
					}

					break;
				}
			}
		}

		// Sort the entries
		{
			const u32 order = setList->sortOrder;
			const u32 type = setList->sortType;

			if (order > CELL_SAVEDATA_SORTORDER_ASCENT || type > CELL_SAVEDATA_SORTTYPE_SUBTITLE)
			{
				// error
			}

			std::sort(save_entries.begin(), save_entries.end(), [=](const SaveDataEntry& entry1, const SaveDataEntry& entry2)
			{
				if (order == CELL_SAVEDATA_SORTORDER_DESCENT && type == CELL_SAVEDATA_SORTTYPE_MODIFIEDTIME)
				{
					return entry1.mtime >= entry2.mtime;
				}
				if (order == CELL_SAVEDATA_SORTORDER_DESCENT && type == CELL_SAVEDATA_SORTTYPE_SUBTITLE)
				{
					return entry1.subtitle >= entry2.subtitle;
				}
				if (order == CELL_SAVEDATA_SORTORDER_ASCENT && type == CELL_SAVEDATA_SORTTYPE_MODIFIEDTIME)
				{
					return entry1.mtime < entry2.mtime;
				}
				if (order == CELL_SAVEDATA_SORTORDER_ASCENT && type == CELL_SAVEDATA_SORTTYPE_SUBTITLE)
				{
					return entry1.subtitle < entry2.subtitle;
				}

				return true;
			});
		}

		// Fill the listGet->dirList array
		auto dir_list = listGet->dirList.get_ptr();

		for (const auto& entry : save_entries)
		{
			auto& dir = *dir_list++;
			strcpy_trunc(dir.dirName, entry.dirName);
			strcpy_trunc(dir.listParam, entry.listParam);
			memset(dir.reserved, 0, sizeof(dir.reserved));
		}

		s32 selected = -1;

		if (funcList)
		{
			// List Callback
			funcList(ppu, result, listGet, listSet);

			if (result->result < 0)
			{
				cellSysutil.Warning("savedata_op(): funcList returned < 0.");
				return CELL_SAVEDATA_ERROR_CBRESULT;
			}

			// Clean save data list
			save_entries.erase(std::remove_if(save_entries.begin(), save_entries.end(), [&listSet](const SaveDataEntry& entry) -> bool
			{
				for (u32 i = 0; i < listSet->fixedListNum; i++)
				{
					if (entry.dirName == listSet->fixedList[i].dirName)
					{
						return false;
					}
				}

				return true;
			}), save_entries.end());

			// Focus save data
			s32 focused = -1;

			switch (const u32 pos_type = listSet->focusPosition)
			{
			case CELL_SAVEDATA_FOCUSPOS_DIRNAME:
			{
				for (s32 i = 0; i < save_entries.size(); i++)
				{
					if (save_entries[i].dirName == listSet->focusDirName.get_ptr())
					{
						focused = i;
						break;
					}
				}

				break;
			}
			case CELL_SAVEDATA_FOCUSPOS_LISTHEAD:
			{
				focused = save_entries.empty() ? -1 : 0;
				break;
			}
			case CELL_SAVEDATA_FOCUSPOS_LISTTAIL:
			{
				focused = save_entries.size() - 1;
				break;
			}
			case CELL_SAVEDATA_FOCUSPOS_LATEST:
			{
				s64 max = INT64_MIN;

				for (s32 i = 0; i < save_entries.size(); i++)
				{
					if (save_entries[i].mtime > max)
					{
						focused = i;
						max = save_entries[i].mtime;
					}
				}

				break;
			}
			case CELL_SAVEDATA_FOCUSPOS_OLDEST:
			{
				s64 min = INT64_MAX;

				for (s32 i = 0; i < save_entries.size(); i++)
				{
					if (save_entries[i].mtime < min)
					{
						focused = i;
						min = save_entries[i].mtime;
					}
				}

				break;
			}
			case CELL_SAVEDATA_FOCUSPOS_NEWDATA:
			{
				break;
			}
			default:
			{
				cellSysutil.Error("savedata_op(): unknown listSet->focusPosition (0x%x)", pos_type);
				return CELL_SAVEDATA_ERROR_PARAM;
			}
			}

			// Display Save Data List
			selected = g_savedata_dialog->ShowSaveDataList(save_entries, focused, listSet);

			if (selected == -1)
			{
				if (listSet->newData)
				{
					save_entry.dirName = listSet->newData->dirName.get_ptr();
				}
				else
				{
					return CELL_OK; // ???
				}
			}
		}

		if (funcFixed)
		{
			// Fixed Callback
			funcFixed(ppu, result, listGet, fixedSet);

			if (result->result < 0)
			{
				cellSysutil.Warning("savedata_op(): funcFixed returned < 0.");
				return CELL_SAVEDATA_ERROR_CBRESULT;
			}

			for (s32 i = 0; i < save_entries.size(); i++)
			{
				if (save_entries[i].dirName == fixedSet->dirName.get_ptr())
				{
					selected = i;
					break;
				}
			}

			if (selected == -1)
			{
				save_entry.dirName = fixedSet->dirName.get_ptr();
			}
		}

		if (selected >= 0)
		{
			if (selected < save_entries.size())
			{
				save_entry.dirName = std::move(save_entries[selected].dirName);
			}
			else
			{
				throw EXCEPTION("Invalid savedata selected");
			}
		}
	}

	if (dirName)
	{
		save_entry.dirName = dirName.get_ptr();
	}

	std::string dir_path = base_dir + save_entry.dirName + "/";
	std::string sfo_path = dir_path + "PARAM.SFO";

	PSFLoader psf;

	// Load PARAM.SFO
	{
		vfsFile f(sfo_path);
		psf.Load(f);
	}

	// Get save stats
	{
		std::string dir_local_path;

		Emu.GetVFS().GetDevice(dir_path, dir_local_path);

		fs::stat_t dir_info;
		if (!fs::stat(dir_local_path, dir_info))
		{
			// error
		}

		statGet->hddFreeSizeKB = 40 * 1024 * 1024; // 40 GB
		statGet->isNewData = save_entry.isNew = !psf;

		statGet->dir.atime = save_entry.atime = dir_info.atime;
		statGet->dir.mtime = save_entry.mtime = dir_info.mtime;
		statGet->dir.ctime = save_entry.ctime = dir_info.ctime;
		strcpy_trunc(statGet->dir.dirName, save_entry.dirName);

		statGet->getParam.attribute = psf.GetInteger("ATTRIBUTE"); // ???
		strcpy_trunc(statGet->getParam.title, save_entry.title = psf.GetString("TITLE"));
		strcpy_trunc(statGet->getParam.subTitle, save_entry.subtitle = psf.GetString("SUB_TITLE"));
		strcpy_trunc(statGet->getParam.detail, save_entry.details = psf.GetString("DETAIL"));
		strcpy_trunc(statGet->getParam.listParam, save_entry.listParam = psf.GetString("SAVEDATA_LIST_PARAM"));

		statGet->bind = 0;
		statGet->sizeKB = save_entry.size / 1024;
		statGet->sysSizeKB = 0; // This is the size of system files, but PARAM.SFO is very small and PARAM.PDF is not used

		statGet->fileNum = 0;
		statGet->fileList.set(setBuf->buf.addr());
		statGet->fileListNum = 0;
		memset(statGet->reserved, 0, sizeof(statGet->reserved));

		auto file_list = statGet->fileList.get_ptr();

		for (const auto entry : vfsDir(dir_path))
		{
			// only files, system files ignored, fileNum is limited by setBuf->fileListMax
			if (entry->flags & DirEntry_TypeFile && entry->name != "PARAM.SFO" && statGet->fileListNum++ < setBuf->fileListMax)
			{
				statGet->fileNum++;

				auto& file = *file_list++;

				if (entry->name == "ICON0.PNG")
				{
					file.fileType = CELL_SAVEDATA_FILETYPE_CONTENT_ICON0;
				}
				else if (entry->name == "ICON1.PAM")
				{
					file.fileType = CELL_SAVEDATA_FILETYPE_CONTENT_ICON1;
				}
				else if (entry->name == "PIC1.PNG")
				{
					file.fileType = CELL_SAVEDATA_FILETYPE_CONTENT_PIC1;
				}
				else if (entry->name == "SND0.AT3")
				{
					file.fileType = CELL_SAVEDATA_FILETYPE_CONTENT_SND0;
				}
				else if (psf.GetInteger("*" + entry->name)) // let's put the list of protected files in PARAM.SFO (int param = 1 if protected)
				{
					file.fileType = CELL_SAVEDATA_FILETYPE_SECUREFILE;
				}
				else
				{
					file.fileType = CELL_SAVEDATA_FILETYPE_NORMALFILE;
				}

				file.size = entry->size;
				file.atime = entry->access_time;
				file.mtime = entry->modify_time;
				file.ctime = entry->create_time;
				strcpy_trunc(file.fileName, entry->name);
			}
		}

		// Stat Callback
		funcStat(ppu, result, statGet, statSet);

		if (result->result < 0)
		{
			cellSysutil.Warning("savedata_op(): funcStat returned < 0.");
			return CELL_SAVEDATA_ERROR_CBRESULT;
		}

		if (statSet->setParam)
		{
			psf.Clear();

			// Update PARAM.SFO
			psf.SetString("ACCOUNT_ID", ""); // ???
			psf.SetInteger("ATTRIBUTE", statSet->setParam->attribute);
			psf.SetString("CATEGORY", "SD"); // ???
			psf.SetString("PARAMS", ""); // ???
			psf.SetString("PARAMS2", ""); // ???
			psf.SetInteger("PARENTAL_LEVEL", 0); // ???
			psf.SetString("DETAIL", statSet->setParam->detail);
			psf.SetString("SAVEDATA_DIRECTORY", save_entry.dirName);
			psf.SetString("SAVEDATA_LIST_PARAM", statSet->setParam->listParam);
			psf.SetString("SUB_TITLE", statSet->setParam->subTitle);
			psf.SetString("TITLE", statSet->setParam->title);
		}
		else if (!psf)
		{
			// setParam is NULL for new savedata: abort operation

			return CELL_OK;
		}

		switch (const u32 mode = statSet->reCreateMode & 0xffff)
		{
		case CELL_SAVEDATA_RECREATE_NO:
		{
			cellSaveData.Error("Savedata %s considered broken", save_entry.dirName);
			// fallthrough
		}

		case CELL_SAVEDATA_RECREATE_NO_NOBROKEN:
		{
			break;
		}

		case CELL_SAVEDATA_RECREATE_YES:
		case CELL_SAVEDATA_RECREATE_YES_RESET_OWNER:
		{
			// kill it with fire
			for (const auto entry : vfsDir(dir_path))
			{
				if (entry->flags & DirEntry_TypeFile)
				{
					Emu.GetVFS().RemoveFile(dir_path + entry->name);
				}
			}

			if (!statSet->setParam)
			{
				// Savedata deleted and setParam is NULL: delete directory and abort operation
				if (Emu.GetVFS().RemoveDir(dir_path)) cellSysutil.Error("savedata_op(): savedata directory %s deleted", save_entry.dirName);

				return CELL_OK;
			}

			break;
		}

		default:
		{
			cellSysutil.Error("savedata_op(): unknown statSet->reCreateMode (0x%x)", statSet->reCreateMode);
			return CELL_SAVEDATA_ERROR_PARAM;
		}
		}
	}

	// Create save directory if necessary
	if (psf && save_entry.isNew && !Emu.GetVFS().CreateDir(dir_path))
	{
		// Let's ignore this error for now
	}

	// Enter the loop where the save files are read/created/deleted

	fileGet->excSize = 0;
	memset(fileGet->reserved, 0, sizeof(fileGet->reserved));

	while (funcFile)
	{
		funcFile(ppu, result, fileGet, fileSet);

		if (result->result < 0)
		{
			cellSysutil.Warning("savedata_op(): funcFile returned < 0.");
			return CELL_SAVEDATA_ERROR_CBRESULT;
		}

		if (result->result == CELL_SAVEDATA_CBRESULT_OK_LAST || result->result == CELL_SAVEDATA_CBRESULT_OK_LAST_NOCONFIRM)
		{
			break;
		}

		std::string file_path;

		switch (const u32 type = fileSet->fileType)
		{
		case CELL_SAVEDATA_FILETYPE_SECUREFILE:
		case CELL_SAVEDATA_FILETYPE_NORMALFILE:
		{
			file_path = fileSet->fileName.get_ptr();
			break;
		}

		case CELL_SAVEDATA_FILETYPE_CONTENT_ICON0:
		{
			file_path = "ICON0.PNG";
			break;
		}

		case CELL_SAVEDATA_FILETYPE_CONTENT_ICON1:
		{
			file_path = "ICON1.PAM";
			break;
		}

		case CELL_SAVEDATA_FILETYPE_CONTENT_PIC1:
		{
			file_path = "PIC1.PNG";
			break;
		}

		case CELL_SAVEDATA_FILETYPE_CONTENT_SND0:
		{
			file_path = "SND0.AT3";
			break;
		}

		default:
		{
			cellSysutil.Error("savedata_op(): unknown fileSet->fileType (0x%x)", type);
			return CELL_SAVEDATA_ERROR_PARAM;
		}
		}

		psf.SetInteger("*" + file_path, fileSet->fileType == CELL_SAVEDATA_FILETYPE_SECUREFILE);

		std::string local_path;

		Emu.GetVFS().GetDevice(dir_path + file_path, local_path);

		switch (const u32 op = fileSet->fileOperation)
		{
		case CELL_SAVEDATA_FILEOP_READ:
		{
			fs::file file(local_path, fom::read);
			file.seek(fileSet->fileOffset);
			fileGet->excSize = static_cast<u32>(file.read(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize)));
			break;
		}

		case CELL_SAVEDATA_FILEOP_WRITE:
		{
			fs::file file(local_path, fom::write | fom::create);
			file.seek(fileSet->fileOffset);
			fileGet->excSize = static_cast<u32>(file.write(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize)));
			file.trunc(file.seek(0, fsm::cur)); // truncate
			break;
		}

		case CELL_SAVEDATA_FILEOP_DELETE:
		{
			fs::remove_file(local_path);
			fileGet->excSize = 0;
			break;
		}

		case CELL_SAVEDATA_FILEOP_WRITE_NOTRUNC:
		{
			fs::file file(local_path, fom::write | fom::create);
			file.seek(fileSet->fileOffset);
			fileGet->excSize = static_cast<u32>(file.write(fileSet->fileBuf.get_ptr(), std::min<u32>(fileSet->fileSize, fileSet->fileBufSize)));
			break;
		}

		default:
		{
			cellSysutil.Error("savedata_op(): unknown fileSet->fileOperation (0x%x)", op);
			return CELL_SAVEDATA_ERROR_PARAM;
		}
		}
	}

	// Write PARAM.SFO
	if (psf)
	{
		vfsFile f(sfo_path, fom::write | fom::create | fom::trunc);
		psf.Save(f);
	}

	return CELL_OK;
}