diff --git a/src/main/java/nsusbloader/RainbowHexDump.java b/src/main/java/nsusbloader/RainbowHexDump.java index 1086840..d3131ad 100644 --- a/src/main/java/nsusbloader/RainbowHexDump.java +++ b/src/main/java/nsusbloader/RainbowHexDump.java @@ -47,6 +47,18 @@ public class RainbowHexDump { + "\n"); } + public static void hexDumpUTF8ForWin(byte[] byteArray){ + for (int i=0; i < byteArray.length; i++) + System.out.print(String.format("%02d-", i%100)); + System.out.println(">"+byteArray.length); + for (byte b: byteArray) + System.out.print(String.format("%02x ", b)); + System.out.println(); + System.out.print("\t\t\t" + + new String(byteArray, StandardCharsets.UTF_8) + + "\n"); + } + public static void hexDumpUTF16LE(byte[] byteArray){ System.out.print(ANSI_BLUE); for (int i=0; i < byteArray.length; i++) diff --git a/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java b/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java index 682c94e..5b1f18f 100644 --- a/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java +++ b/src/main/java/nsusbloader/Utilities/NxdtUsbAbi1.java @@ -104,8 +104,8 @@ class NxdtUsbAbi1 { while (true){ directive = readUsbDirective(); - RainbowHexDump.hexDumpUTF8(directive); - + RainbowHexDump.hexDumpUTF8ForWin(directive); + if (isInvalidDirective(directive)) continue; @@ -123,6 +123,7 @@ class NxdtUsbAbi1 { return; default: writeUsb(USBSTATUS_UNSUPPORTED_CMD); + logPrinter.print("Unsupported command: " + command, EMsgType.FAIL); } } } @@ -142,7 +143,7 @@ class NxdtUsbAbi1 { logPrinter.print("Invalid 'MAGIC'", EMsgType.INFO); return true; } - if (message.length != NXDT_COMMAND_SIZE){ + if (message.length < 0x10){ writeUsb(USBSTATUS_MALFORMED_REQUEST); logPrinter.print("Invalid directive size. Expected size is 4096 while received is "+message.length, EMsgType.INFO); return true; @@ -160,10 +161,9 @@ class NxdtUsbAbi1 { if (ABI_VERSION != versionABI){ writeUsb(USBSTATUS_UNSUPPORTED_ABI); - throw new Exception("ABI v"+message[11]+" is not supported in current version."); + throw new Exception("ABI v"+versionABI+" is not supported in current version."); } writeUsb(USBSTATUS_SUCCESS); - // consider refactoring: create sub-classes for various ABI versions and check if it's supported or not } private void handleSendFileProperties(byte[] message) throws Exception{ @@ -171,25 +171,15 @@ class NxdtUsbAbi1 { final int fileNameLen = getLEint(message, 0x18); String filename = new String(message, 0x20, fileNameLen, StandardCharsets.UTF_8); - // TODO: In here should be also a field with NSP Header size that would be transmitted after the end of main data transfer; NOTE: Handle with RandomAccessFile. - logPrinter.print("Write request for: '"+filename+"' ("+fileSize+" bytes)", EMsgType.INFO); // If RomFs related - if (filename.startsWith("/")) { + if (isRomFs(filename)) { if (isWindows) filename = saveToPath + filename.replaceAll("/", "\\\\"); else filename = saveToPath + filename; - try { - createPath(filename); - } - catch (Exception e){ - writeUsb(USBSTATUS_HOSTIOERROR); - logPrinter.print("Unable to create dir(s) for file in "+filename+ - "\n Returned: "+e.getMessage(), EMsgType.FAIL); - return; - } + createPath(filename); } else filename = saveToPath + filename; @@ -197,9 +187,9 @@ class NxdtUsbAbi1 { File fileToDump = new File(filename); // Check if enough space - if (fileToDump.getFreeSpace() <= fileSize){ + if (fileToDump.getParentFile().getFreeSpace() <= fileSize){ writeUsb(USBSTATUS_HOSTIOERROR); - logPrinter.print("Not enough space on selected volume. Need: "+fileSize+" while available: "+fileToDump.getFreeSpace(), EMsgType.FAIL); + logPrinter.print("Not enough space on selected volume. Need: "+fileSize+" while available: "+fileToDump.getParentFile().getFreeSpace(), EMsgType.FAIL); return; } // Check if FS is NOT read-only @@ -209,42 +199,14 @@ class NxdtUsbAbi1 { return; } + writeUsb(USBSTATUS_SUCCESS); + dumpFile(fileToDump, fileSize); writeUsb(USBSTATUS_SUCCESS); - // TODO: check if NSP_SIZE != 0 then go dump header } - private void createPath(String path) throws Exception{ - // QA NOTE: Clarify if exception is even possible on RO FS. - - File resultingFile = new File(path); - File folderForTheFile = resultingFile.getParentFile(); - folderForTheFile.mkdirs(); - } - - private void dumpFile(File file, long size) throws Exception{ - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file, false)); - - byte[] readBuffer; - long received = 0; - int bufferSize; - - while (received < size){ - readBuffer = readUsbFile(); - bos.write(readBuffer); - bufferSize = readBuffer.length; - received += bufferSize; - - logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0); - } - - logPrinter.updateProgress(1.0); - bos.close(); - } - - private int getLEint(byte[] bytes, int fromOffset){ return ByteBuffer.wrap(bytes, fromOffset, 0x4).order(ByteOrder.LITTLE_ENDIAN).getInt(); } @@ -253,6 +215,45 @@ class NxdtUsbAbi1 { return ByteBuffer.wrap(bytes, fromOffset, 0x8).order(ByteOrder.LITTLE_ENDIAN).getLong(); } + private boolean isRomFs(String filename){ + return filename.startsWith("/"); + } + + private void createPath(String path) throws Exception{ + File resultingFile = new File(path); + File folderForTheFile = resultingFile.getParentFile(); + + if (folderForTheFile.exists()) + return; + + if (folderForTheFile.mkdirs()) + return; + + writeUsb(USBSTATUS_HOSTIOERROR); + throw new Exception("Unable to create dir(s) for file in "+folderForTheFile); + } + + private void dumpFile(File file, long size) throws Exception{ + RandomAccessFile raf = new RandomAccessFile(file, "rw"); + + byte[] readBuffer; + long received = 0; + int bufferSize; + + while (received < size){ + readBuffer = readUsbFile(); + raf.write(readBuffer); + bufferSize = readBuffer.length; + received += bufferSize; + + logPrinter.updateProgress((received + bufferSize) / (size / 100.0) / 100.0); + } + + logPrinter.updateProgress(1.0); + raf.close(); + } + + /** * Sending any byte array to USB device * @return 'false' if no issues diff --git a/src/main/resources/res/nxdt_icon.jpg b/src/main/resources/res/nxdt_icon.jpg deleted file mode 100644 index f4d2627..0000000 Binary files a/src/main/resources/res/nxdt_icon.jpg and /dev/null differ