From f33c735e3fda9e8127ec3b3b5f7296a438dc9a2c Mon Sep 17 00:00:00 2001 From: satelliteseeker Date: Sat, 20 Oct 2018 18:22:05 -0700 Subject: [PATCH] Add padding to NCA files over USB transmission --- include/install/usb_nsp.hpp | 3 +++ include/util/usb_util.hpp | 2 +- source/install/usb_nsp.cpp | 15 ++++++++++----- source/util/usb_util.cpp | 4 ++-- tools/usb_install_pc.py | 19 ++++++++++++++++--- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/install/usb_nsp.hpp b/include/install/usb_nsp.hpp index 76de5bab..c5cb5ba1 100644 --- a/include/install/usb_nsp.hpp +++ b/include/install/usb_nsp.hpp @@ -5,6 +5,9 @@ namespace tin::install::nsp { + static const size_t PADDING_SIZE = 0x1000; + static const u32 CMD_ID_FILE_RANGE = 1; + static const u32 CMD_ID_FILE_RANGE_PADDED = 2; class USBNSP : public RemoteNSP { private: diff --git a/include/util/usb_util.hpp b/include/util/usb_util.hpp index 302b59a8..1b0e6377 100644 --- a/include/util/usb_util.hpp +++ b/include/util/usb_util.hpp @@ -30,7 +30,7 @@ namespace tin::util static void SendCmdHeader(u32 cmdId, size_t dataSize); static void SendExitCmd(); - static USBCmdHeader SendFileRangeCmd(std::string nspName, u64 offset, u64 size); + static USBCmdHeader SendFileRangeCmd(u32 cmdId, std::string nspName, u64 offset, u64 size); }; size_t USBRead(void* out, size_t len); diff --git a/source/install/usb_nsp.cpp b/source/install/usb_nsp.cpp index c4f3613f..82820ce3 100644 --- a/source/install/usb_nsp.cpp +++ b/source/install/usb_nsp.cpp @@ -36,9 +36,9 @@ namespace tin::install::nsp int USBThreadFunc(void* in) { USBFuncArgs* args = reinterpret_cast(in); - tin::util::USBCmdHeader header = tin::util::USBCmdManager::SendFileRangeCmd(args->nspName, args->pfs0Offset, args->ncaSize); + tin::util::USBCmdHeader header = tin::util::USBCmdManager::SendFileRangeCmd(CMD_ID_FILE_RANGE_PADDED, args->nspName, args->pfs0Offset, args->ncaSize); - u8* buf = (u8*)memalign(0x1000, 0x100000); + u8* buf = (u8*)memalign(0x1000, tin::data::BUFFER_SEGMENT_DATA_SIZE); u64 sizeRemaining = header.dataSize; size_t tmpSizeRead = 0; @@ -46,7 +46,12 @@ namespace tin::install::nsp { while (sizeRemaining) { - tmpSizeRead = usbCommsRead(buf, std::min(sizeRemaining, (u64)0x100000)); + size_t padding = 0; + if (header.cmdId == CMD_ID_FILE_RANGE_PADDED) { + padding = PADDING_SIZE; + } + + tmpSizeRead = usbCommsRead(buf, std::min(sizeRemaining + padding, tin::data::BUFFER_SEGMENT_DATA_SIZE)) - padding; //LOG_DEBUG("Read bytes\n") //printBytes(nxlinkout, buf, tmpSizeRead, true); sizeRemaining -= tmpSizeRead; @@ -57,7 +62,7 @@ namespace tin::install::nsp break; } - args->bufferedPlaceholderWriter->AppendData(buf, tmpSizeRead); + args->bufferedPlaceholderWriter->AppendData(buf + padding, tmpSizeRead); } } catch (std::exception& e) @@ -151,7 +156,7 @@ namespace tin::install::nsp void USBNSP::BufferData(void* buf, off_t offset, size_t size) { - tin::util::USBCmdHeader header = tin::util::USBCmdManager::SendFileRangeCmd(m_nspName, offset, size); + tin::util::USBCmdHeader header = tin::util::USBCmdManager::SendFileRangeCmd(CMD_ID_FILE_RANGE, m_nspName, offset, size); tin::util::USBRead(buf, header.dataSize); } } \ No newline at end of file diff --git a/source/util/usb_util.cpp b/source/util/usb_util.cpp index 062d4010..861cb765 100644 --- a/source/util/usb_util.cpp +++ b/source/util/usb_util.cpp @@ -22,7 +22,7 @@ namespace tin::util USBCmdManager::SendCmdHeader(0, 0); } - USBCmdHeader USBCmdManager::SendFileRangeCmd(std::string nspName, u64 offset, u64 size) + USBCmdHeader USBCmdManager::SendFileRangeCmd(u32 cmdId, std::string nspName, u64 offset, u64 size) { struct FileRangeCmdHeader { @@ -37,7 +37,7 @@ namespace tin::util fRangeHeader.nspNameLen = nspName.size(); fRangeHeader.padding = 0; - USBCmdManager::SendCmdHeader(1, sizeof(FileRangeCmdHeader) + fRangeHeader.nspNameLen); + USBCmdManager::SendCmdHeader(cmdId, sizeof(FileRangeCmdHeader) + fRangeHeader.nspNameLen); USBWrite(&fRangeHeader, sizeof(FileRangeCmdHeader)); USBWrite(nspName.c_str(), fRangeHeader.nspNameLen); diff --git a/tools/usb_install_pc.py b/tools/usb_install_pc.py index 3123640c..3d6e4932 100644 --- a/tools/usb_install_pc.py +++ b/tools/usb_install_pc.py @@ -33,9 +33,13 @@ CMD_ID_EXIT = 0 CMD_ID_FILE_RANGE = 1 +CMD_ID_FILE_RANGE_PADDED = 2 CMD_TYPE_RESPONSE = 1 +BUFFER_SEGMENT_DATA_SIZE = 0x100000 +PADDING_SIZE = 0x1000 + def send_response_header(out_ep, cmd_id, data_size): out_ep.write(b'TUC0') # Tinfoil USB Command 0 out_ep.write(struct.pack('= end_off: read_size = end_off - curr_off buf = f.read(read_size) + if (padding): + buf = b'\x00' * PADDING_SIZE + buf out_ep.write(data=buf, timeout=0) curr_off += read_size @@ -91,6 +102,8 @@ def poll_commands(nsp_dir, in_ep, out_ep): break elif cmd_id == CMD_ID_FILE_RANGE: file_range_cmd(nsp_dir, in_ep, out_ep, data_size) + elif cmd_id == CMD_ID_FILE_RANGE_PADDED: + file_range_cmd(nsp_dir, in_ep, out_ep, data_size, padding=True) def send_nsp_list(nsp_dir, out_ep): nsp_path_list = list()