| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-21 01:47:53 +01:00
										 |  |  | #include <algorithm>
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							|  |  |  | #include <cstring>
 | 
					
						
							|  |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 20:06:58 -05:00
										 |  |  | #include "Common/CommonTypes.h"
 | 
					
						
							| 
									
										
										
										
											2014-02-17 05:18:15 -05:00
										 |  |  | #include "Common/FileUtil.h"
 | 
					
						
							|  |  |  | #include "DiscIO/WbfsBlob.h"
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace DiscIO | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | static const u64 WII_SECTOR_SIZE = 0x8000; | 
					
						
							|  |  |  | static const u64 WII_SECTOR_COUNT = 143432 * 2; | 
					
						
							|  |  |  | static const u64 WII_DISC_HEADER_SIZE = 256; | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | static inline u64 align(u64 value, u64 bounds) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return (value + (bounds - 1)) & (~(bounds - 1)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | WbfsFileReader::WbfsFileReader(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 	: m_total_files(0), m_size(0), m_wlba_table(nullptr), m_good(true) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | 	if (filename.length() < 4 || !OpenFiles(filename) || !ReadHeader()) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		m_good = false; | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Grab disc info (assume slot 0, checked in ReadHeader())
 | 
					
						
							|  |  |  | 	m_wlba_table = new u16[m_blocks_per_disc]; | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_files[0]->file.Seek(m_hd_sector_size + WII_DISC_HEADER_SIZE /*+ i * m_disc_info_size*/, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	m_files[0]->file.ReadBytes(m_wlba_table, m_blocks_per_disc * sizeof(u16)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | WbfsFileReader::~WbfsFileReader() | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	for (u32 i = 0; i != m_files.size(); ++ i) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		delete m_files[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	delete[] m_wlba_table; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool WbfsFileReader::OpenFiles(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | { | 
					
						
							|  |  |  | 	m_total_files = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	while (true) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		file_entry* new_entry = new file_entry; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Replace last character with index (e.g. wbfs = wbf1)
 | 
					
						
							|  |  |  | 		std::string path = filename; | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 		if (0 != m_total_files) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			path[path.length() - 1] = '0' + m_total_files; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 		if (!new_entry->file.Open(path, "rb")) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			delete new_entry; | 
					
						
							|  |  |  | 			return 0 != m_total_files; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 		new_entry->base_address = m_size; | 
					
						
							|  |  |  | 		new_entry->size = new_entry->file.GetSize(); | 
					
						
							|  |  |  | 		m_size += new_entry->size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		m_total_files ++; | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 		m_files.push_back(new_entry); | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool WbfsFileReader::ReadHeader() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	m_files[0]->file.Seek(4, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	// Read hd size info
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_files[0]->file.ReadBytes(&m_hd_sector_count, 4); | 
					
						
							|  |  |  | 	m_hd_sector_count = Common::swap32(m_hd_sector_count); | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_files[0]->file.ReadBytes(&m_hd_sector_shift, 1); | 
					
						
							|  |  |  | 	m_hd_sector_size = 1ull << m_hd_sector_shift; | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	if (m_size != (m_hd_sector_count * m_hd_sector_size)) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		//printf("File size doesn't match expected size\n");
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	// Read wbfs cluster info
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_files[0]->file.ReadBytes(&m_wbfs_sector_shift, 1); | 
					
						
							|  |  |  | 	m_wbfs_sector_size = 1ull << m_wbfs_sector_shift; | 
					
						
							|  |  |  | 	m_wbfs_sector_count = m_size / m_wbfs_sector_size; | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	if (m_wbfs_sector_size < WII_SECTOR_SIZE) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		//Setting this too low would case a very large memory allocation
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_blocks_per_disc = (WII_SECTOR_COUNT * WII_SECTOR_SIZE) / m_wbfs_sector_size; | 
					
						
							|  |  |  | 	m_disc_info_size = align(WII_DISC_HEADER_SIZE + m_blocks_per_disc * 2, m_hd_sector_size); | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	// Read disc table
 | 
					
						
							|  |  |  | 	m_files[0]->file.Seek(2, SEEK_CUR); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	m_files[0]->file.ReadBytes(m_disc_table, 500); | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	if (0 == m_disc_table[0]) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		//printf("Game must be in 'slot 0'\n");
 | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool WbfsFileReader::Read(u64 offset, u64 nbytes, u8* out_ptr) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	while (nbytes) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-04-09 18:57:39 -05:00
										 |  |  | 		u64 read_size = 0; | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 		File::IOFile& data_file = SeekToCluster(offset, &read_size); | 
					
						
							|  |  |  | 		read_size = (read_size > nbytes) ? nbytes : read_size; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		data_file.ReadBytes(out_ptr, read_size); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		out_ptr += read_size; | 
					
						
							|  |  |  | 		nbytes -= read_size; | 
					
						
							|  |  |  | 		offset += read_size; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | File::IOFile& WbfsFileReader::SeekToCluster(u64 offset, u64* available) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 	u64 base_cluster = (offset >> m_wbfs_sector_shift); | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	if (base_cluster < m_blocks_per_disc) | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 		u64 cluster_address = m_wbfs_sector_size * Common::swap16(m_wlba_table[base_cluster]); | 
					
						
							|  |  |  | 		u64 cluster_offset = offset & (m_wbfs_sector_size - 1); | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 		u64 final_address = cluster_address + cluster_offset; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 		for (u32 i = 0; i != m_total_files; i ++) | 
					
						
							| 
									
										
										
										
											2013-10-29 01:23:17 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 			if (final_address < (m_files[i]->base_address + m_files[i]->size)) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 			{ | 
					
						
							|  |  |  | 				m_files[i]->file.Seek(final_address - m_files[i]->base_address, SEEK_SET); | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 				if (available) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 				{ | 
					
						
							|  |  |  | 					u64 till_end_of_file = m_files[i]->size - (final_address - m_files[i]->base_address); | 
					
						
							| 
									
										
										
										
											2014-09-01 15:48:02 -04:00
										 |  |  | 					u64 till_end_of_sector = m_wbfs_sector_size - cluster_offset; | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 					*available = std::min(till_end_of_file, till_end_of_sector); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				return m_files[i]->file; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	PanicAlert("Read beyond end of disc"); | 
					
						
							|  |  |  | 	m_files[0]->file.Seek(0, SEEK_SET); | 
					
						
							|  |  |  | 	return m_files[0]->file; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | WbfsFileReader* WbfsFileReader::Create(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	WbfsFileReader* reader = new WbfsFileReader(filename); | 
					
						
							| 
									
										
										
										
											2013-03-19 09:59:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-11 00:30:55 +13:00
										 |  |  | 	if (reader->IsGood()) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		return reader; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		delete reader; | 
					
						
							| 
									
										
										
										
											2014-03-09 21:14:26 +01:00
										 |  |  | 		return nullptr; | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-12 15:33:41 -04:00
										 |  |  | bool IsWbfsBlob(const std::string& filename) | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | { | 
					
						
							|  |  |  | 	File::IOFile f(filename, "rb"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	u8 magic[4] = {0, 0, 0, 0}; | 
					
						
							|  |  |  | 	f.ReadBytes(&magic, 4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-16 15:30:18 -05:00
										 |  |  | 	return (magic[0] == 'W') && | 
					
						
							|  |  |  | 	       (magic[1] == 'B') && | 
					
						
							|  |  |  | 	       (magic[2] == 'F') && | 
					
						
							|  |  |  | 	       (magic[3] == 'S'); | 
					
						
							| 
									
										
										
										
											2012-05-04 00:09:01 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace
 |