From 8e6187ab0f59d0fcc993ae3b80dc4857183df616 Mon Sep 17 00:00:00 2001 From: Maschell Date: Mon, 24 Oct 2016 00:49:25 +0200 Subject: [PATCH] Refactored the existing functions and added new functions Added: repeatwrite dump_syslog --- src/de/mas/wupclient/Starter.java | 6 +- src/de/mas/wupclient/client/WUPClient.java | 123 +++------------- .../operations/DownloadUploadOperations.java | 136 ++++++++++++++++++ .../client/operations/FSAOperations.java | 30 ++-- .../client/operations/MCPOperations.java | 31 ++++ ...lOperations.java => SystemOperations.java} | 100 ++++++++++--- .../client/operations/UtilOperations.java | 123 ++++------------ .../client/utils/MetaInformation.java | 5 - src/de/mas/wupclient/client/utils/Utils.java | 46 +++++- 9 files changed, 360 insertions(+), 240 deletions(-) create mode 100644 src/de/mas/wupclient/client/operations/DownloadUploadOperations.java create mode 100644 src/de/mas/wupclient/client/operations/MCPOperations.java rename src/de/mas/wupclient/client/operations/{IoctlOperations.java => SystemOperations.java} (52%) diff --git a/src/de/mas/wupclient/Starter.java b/src/de/mas/wupclient/Starter.java index 5fcc4fa..fbd48b6 100644 --- a/src/de/mas/wupclient/Starter.java +++ b/src/de/mas/wupclient/Starter.java @@ -2,6 +2,7 @@ package de.mas.wupclient; import java.io.IOException; import de.mas.wupclient.client.WUPClient; +import de.mas.wupclient.client.operations.DownloadUploadOperations; import de.mas.wupclient.client.operations.UtilOperations; import de.mas.wupclient.client.utils.Logger; @@ -14,10 +15,13 @@ public class Starter { WUPClient w = new WUPClient(ip); try { UtilOperations util = UtilOperations.UtilOperationsFactory(w); + DownloadUploadOperations dlul = DownloadUploadOperations.DownloadUploadOperationsFactory(w); + util.dump_syslog(); + Logger.logCmd("Lets into the " + w.getCwd() + "/sys/title/00050010/10040200/" + " folder!"); util.lsRecursive(w.getCwd() + "/sys/title/00050010/10040200/"); Logger.logCmd("And download the /code/app.xml to /test/app.xml"); - util.downloadFile(w.getCwd() + "/sys/title/00050010/10040200/code", "app.xml", "test", null); + dlul.downloadFile(w.getCwd() + "/sys/title/00050010/10040200/code", "app.xml", "test", null); Logger.logCmd("done!"); } catch (IOException e) { e.printStackTrace(); diff --git a/src/de/mas/wupclient/client/WUPClient.java b/src/de/mas/wupclient/client/WUPClient.java index 74cab4f..e637a6c 100644 --- a/src/de/mas/wupclient/client/WUPClient.java +++ b/src/de/mas/wupclient/client/WUPClient.java @@ -5,11 +5,12 @@ import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.nio.ByteBuffer; -import java.nio.ByteOrder; import java.util.Arrays; +import de.mas.wupclient.client.operations.SystemOperations; import de.mas.wupclient.client.utils.Logger; import de.mas.wupclient.client.utils.Result; +import de.mas.wupclient.client.utils.Utils; public class WUPClient { private String IP; @@ -20,24 +21,19 @@ public class WUPClient { public WUPClient(String ip){ setIP(ip); createSocket(ip); - setFsaHandle(-1); + setFSAHandle(-1); setCwd("/vol/storage_mlc01"); } public Result send(int command, byte[] data) throws IOException{ DataOutputStream outToServer = new DataOutputStream(getSocket().getOutputStream()); - ByteBuffer destByteBuffer = ByteBuffer.allocate(data.length + 4); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.putInt(command); - destByteBuffer.put(data); try { - outToServer.write(destByteBuffer.array()); + outToServer.write(Utils.m_packBE(command,data)); } catch (IOException e) { Logger.logErr("send failed"); e.printStackTrace(); } - destByteBuffer.clear(); - destByteBuffer = ByteBuffer.allocate(0x600); + ByteBuffer destByteBuffer = ByteBuffer.allocate(0x600); byte[] result = new byte[0x0600]; int size = getSocket().getInputStream().read(result); destByteBuffer.put(result, 0, size); @@ -46,11 +42,7 @@ public class WUPClient { } public byte[] read(int addr, int len) throws IOException{ - ByteBuffer destByteBuffer = ByteBuffer.allocate(8); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.putInt(addr); - destByteBuffer.putInt(len); - Result result = send(1,destByteBuffer.array()); + Result result = send(1, Utils.m_packBE(addr,len)); if(result.getResultValue() == 0){ return result.getData(); }else{ @@ -59,13 +51,8 @@ public class WUPClient { } } - public int write(int addr, byte[] data ) throws IOException{ - ByteBuffer destByteBuffer = ByteBuffer.allocate(4 + data.length); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.putInt(addr); - destByteBuffer.put(data); - - Result result = send(0,destByteBuffer.array()); + public int write(int addr, byte[] data ) throws IOException{ + Result result = send(0,Utils.m_packBE(addr,data)); if(result.getResultValue() == 0){ return result.getResultValue(); }else{ @@ -75,19 +62,9 @@ public class WUPClient { } public int svc(int svc_id, int[] arguments) throws IOException{ - ByteBuffer destByteBuffer = ByteBuffer.allocate(4 + (arguments.length * 0x04)); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.putInt(svc_id); - for(int i = 0;i result = send(2,destByteBuffer.array()); + Result result = send(2,Utils.m_packBE(svc_id,arguments)); if(result.getResultValue() == 0){ - destByteBuffer = ByteBuffer.allocate(0x04); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.put(Arrays.copyOfRange(result.getData(), 0, 0x04)); - return destByteBuffer.getInt(0); + return Utils.bigEndianByteArrayToInt(Arrays.copyOfRange(result.getData(), 0, 0x04)); }else{ Logger.logErr("svc error: " + result.getResultValue()); return -1; @@ -100,12 +77,7 @@ public class WUPClient { } public int memcpy(int dest, int source, int len) throws IOException{ - ByteBuffer destByteBuffer = ByteBuffer.allocate(12); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.putInt(dest); - destByteBuffer.putInt(source); - destByteBuffer.putInt(len); - Result result = send(4,destByteBuffer.array()); + Result result = send(4, Utils.m_packBE(dest,source,len)); if(result.getResultValue() == 0){ return result.getResultValue(); }else{ @@ -113,75 +85,29 @@ public class WUPClient { return -1; } } - public int alloc(int size) throws IOException{ - return alloc(size,0x00); - } - public int alloc(int size, int align) throws IOException{ - if(size == 0){ - return 0; - } - if(align == 0){ - int result = svc(0x27, new int[] {0xCAFF,size}); - return result; + + public int repeatwrite(int dest, int val, int n) throws IOException{ + Result result = send(5, Utils.m_packBE(dest,val,n)); + if(result.getResultValue() == 0){ + return result.getResultValue(); }else{ - int result = svc(0x28, new int[] {0xCAFF,size,align}); - return result; + Logger.logErr("repeatwrite error: " + result.getResultValue()); + return -1; } } - public int free(int address) throws IOException{ - if(address == 0){ - return 0; - } - return svc(0x29, new int[] {0xCAFF,address}); - } - public int load_buffer(byte[] data) throws IOException{ - return load_buffer(data,0x00); - } - public int load_buffer(byte[] data,int align) throws IOException{ - if(data.length == 0) - return 0; - - int address = alloc(data.length, align); - write(address, data); - return address; - - } - - public int load_string(String b) throws IOException{ - return load_string(b,0x00); - } - public int load_string(String b,int align) throws IOException{ - byte[] buffer = b.getBytes(); - byte[] newBuffer = new byte[buffer.length+1]; - System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); - newBuffer[buffer.length] = 0; - - int result = load_buffer(newBuffer, align); - return result; - } - - public int open(String device, int mode) throws IOException{ - int address = load_string(device); - int handle = svc(0x33, new int[] {address,mode}); - free(address); - return handle; - } - public int close(int handle) throws IOException{ - return svc(0x34,new int[]{handle}); - } public int get_fsa_handle() throws IOException{ - if(getFsaHandle()== -1){ - setFsaHandle(open("/dev/fsa", 0)); + if(fsaHandle == -1){ + setFSAHandle(SystemOperations.SystemOperationsFactory(this).open("/dev/fsa", 0)); } - return getFsaHandle(); + return fsaHandle; } public int FSA_Close(int handle) throws IOException{ int result = svc(0x34, new int[]{handle}); if(result == 0){ - setFsaHandle(-1); + setFSAHandle(-1); }else{ Logger.logErr("FSA_Close: failed"); } @@ -219,10 +145,7 @@ public class WUPClient { public void setIP(String iP) { IP = iP; } - private int getFsaHandle() { - return fsaHandle; - } - public void setFsaHandle(int fsaHandle) { + public void setFSAHandle(int fsaHandle) { this.fsaHandle = fsaHandle; } public String getCwd() { diff --git a/src/de/mas/wupclient/client/operations/DownloadUploadOperations.java b/src/de/mas/wupclient/client/operations/DownloadUploadOperations.java new file mode 100644 index 0000000..3bcb5d9 --- /dev/null +++ b/src/de/mas/wupclient/client/operations/DownloadUploadOperations.java @@ -0,0 +1,136 @@ +package de.mas.wupclient.client.operations; + +import java.io.ByteArrayOutputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import de.mas.wupclient.client.WUPClient; +import de.mas.wupclient.client.utils.FEntry; +import de.mas.wupclient.client.utils.Logger; +import de.mas.wupclient.client.utils.Result; +import de.mas.wupclient.client.utils.Utils; + +public class DownloadUploadOperations extends Operations { + private static Map instances = new HashMap<>(); + public static DownloadUploadOperations DownloadUploadOperationsFactory(WUPClient client){ + if(!instances.containsKey(client)){ + instances.put(client, new DownloadUploadOperations(client)); + } + return instances.get(client); + } + + private UtilOperations util = null; + private FSAOperations fsa = null; + + public DownloadUploadOperations(WUPClient client) { + super(client); + setUtil(UtilOperations.UtilOperationsFactory(client)); + setFSA(FSAOperations.FSAOperationsFactory(client)); + } + + public boolean downloadFolder(String path) throws IOException{ + return downloadFolder(path,null,false); + } + + public boolean downloadFolder(String sourcePath,String targetPath) throws IOException{ + return downloadFolder(sourcePath,targetPath,false); + } + + public boolean downloadFolder(String sourcePath, String targetPath,boolean useRelativPath) throws IOException { + List files = util.ls(sourcePath,true); + if(targetPath == null || targetPath.isEmpty()){ + targetPath = sourcePath; + } + for(FEntry f: files){ + if(f.isFile()){ + downloadFile(sourcePath, f.getFilename(),targetPath); + }else{ + downloadFolder(sourcePath + "/" + f.getFilename(), targetPath,useRelativPath); + } + } + return false; + } + + public byte[] downloadFileToByteArray(String path) throws IOException{ + Logger.logCmd("Downloading " + path); + int fsa_handle = getClient().get_fsa_handle(); + Result res = fsa.FSA_OpenFile(fsa_handle, path, "r"); + boolean success = false; + byte[] result = null; + if(res.getResultValue() == 0){ + int block_size = 0x400; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + success = true; + while(true){ + Result read_result= fsa.FSA_ReadFile(fsa_handle, res.getData(), 0x1, block_size); + + if(read_result.getResultValue() <0){ + Logger.logErr("FSA_ReadFile returned " + read_result.getResultValue()); + success = false; + break; + } + + out.write(Arrays.copyOf(read_result.getData(), read_result.getResultValue())); + + if(read_result.getResultValue() <= 0) + break; + } + fsa.FSA_CloseFile(fsa_handle, res.getData()); + if(success){ + result = out.toByteArray(); + } + } + return result; + } + + public boolean downloadFile(String sourcePath, String filename, String targetPath) throws IOException { + return downloadFile(sourcePath, filename, targetPath,null); + } + + public boolean downloadFile(String sourcePath,String sourceFilename,String targetPath,String targetFileName) throws IOException { + byte[] data = downloadFileToByteArray(sourcePath + "/" + sourceFilename); + if(data == null){ + System.out.println("failed"); + return false; + } + String subdir = ""; + + if(targetFileName == null){ + subdir += targetPath + "/" + sourceFilename; + }else{ + subdir += targetPath + "/" + targetFileName; + } + if(subdir.startsWith("/")){ + subdir = subdir.substring(1); + } + + Utils.createSubfolder(subdir); + FileOutputStream stream = new FileOutputStream(subdir); + try { + stream.write(data); + } finally { + stream.close(); + } + return true; + } + + public UtilOperations getUtil() { + return util; + } + + public void setUtil(UtilOperations util) { + this.util = util; + } + + public FSAOperations getFSA() { + return fsa; + } + + public void setFSA(FSAOperations fsa) { + this.fsa = fsa; + } +} diff --git a/src/de/mas/wupclient/client/operations/FSAOperations.java b/src/de/mas/wupclient/client/operations/FSAOperations.java index 8ac3ef8..25bfca9 100644 --- a/src/de/mas/wupclient/client/operations/FSAOperations.java +++ b/src/de/mas/wupclient/client/operations/FSAOperations.java @@ -20,26 +20,26 @@ public class FSAOperations extends Operations { return instances.get(client); } - private IoctlOperations ioctl = null; + private SystemOperations system = null; private FSAOperations(WUPClient client) { super(client); - setIoctlOperations(IoctlOperations.IoctlOperationsFactory(client)); + setIoctlOperations(SystemOperations.SystemOperationsFactory(client)); } - public IoctlOperations getIoctlOperations() { - return ioctl; + public SystemOperations getIoctlOperations() { + return system; } - public void setIoctlOperations(IoctlOperations ioctl) { - this.ioctl = ioctl; + public void setIoctlOperations(SystemOperations ioctl) { + this.system = ioctl; } public Result FSA_OpenDir(int handle, String path) throws IOException{ byte[] inbuffer = new byte[0x520]; Utils.writeNullTerminatedStringToByteArray(inbuffer, path, 0x04); - Result res = ioctl.ioctl(handle, 0x0A, inbuffer, 0x293); + Result res = system.ioctl(handle, 0x0A, inbuffer, 0x293); return new Result(res.getResultValue(),Utils.bigEndianByteArrayToInt(Arrays.copyOfRange(res.getData(), 0x04, 0x08))); } @@ -47,7 +47,7 @@ public class FSAOperations extends Operations { public int FSA_CloseDir(int handle, int dirhandle) throws IOException{ byte[] inbuffer = new byte[0x520]; Utils.writeIntToByteArray(inbuffer, dirhandle, 0x04); - Result res = ioctl.ioctl(handle, 0x0D, inbuffer, 0x293); + Result res = system.ioctl(handle, 0x0D, inbuffer, 0x293); return res.getResultValue(); } @@ -55,7 +55,7 @@ public class FSAOperations extends Operations { byte[] inbuffer = new byte[0x520]; Utils.writeIntToByteArray(inbuffer,dirhandle,4); - Result res = ioctl.ioctl(fsa_handle, 0x0B, inbuffer, 0x293); + Result res = system.ioctl(fsa_handle, 0x0B, inbuffer, 0x293); byte[] unknowndata = Arrays.copyOfRange(res.getData(), 0x04, 0x68); String filename = Utils.getStringFromByteArray(Arrays.copyOfRange(res.getData(), 0x68, res.getData().length)); @@ -73,7 +73,7 @@ public class FSAOperations extends Operations { Utils.writeNullTerminatedStringToByteArray(inbuffer, volume_path, 0x0284); Utils.writeIntToByteArray(inbuffer,flags,0x0504); - Result result = ioctl.ioctlv(handle, 0x01, new byte[][] {inbuffer,new byte[0]}, new int[]{0x293}); + Result result = system.ioctlv(handle, 0x01, new byte[][] {inbuffer,new byte[0]}, new int[]{0x293}); return result.getResultValue(); } @@ -82,7 +82,7 @@ public class FSAOperations extends Operations { byte[] inbuffer = new byte[0x520]; Utils.writeNullTerminatedStringToByteArray(inbuffer, volume_path, 0x04); Utils.writeIntToByteArray(inbuffer,flags,0x284); - Result result = ioctl.ioctl(handle,0x02,inbuffer,0x293); + Result result = system.ioctl(handle,0x02,inbuffer,0x293); return result.getResultValue(); } @@ -90,7 +90,7 @@ public class FSAOperations extends Operations { byte[] inbuffer = new byte[0x520]; Utils.writeNullTerminatedStringToByteArray(inbuffer, path, 0x04); Utils.writeIntToByteArray(inbuffer,flags,0x284); - Result result = ioctl.ioctl(handle, 0x07, inbuffer, 0x293); + Result result = system.ioctl(handle, 0x07, inbuffer, 0x293); return result.getResultValue(); } @@ -98,7 +98,7 @@ public class FSAOperations extends Operations { byte[] inbuffer = new byte[0x520]; Utils.writeNullTerminatedStringToByteArray(inbuffer, path, 0x04); Utils.writeNullTerminatedStringToByteArray(inbuffer, mode, 0x284); - Result result = ioctl.ioctl(handle, 0x0E, inbuffer, 0x293); + Result result = system.ioctl(handle, 0x0E, inbuffer, 0x293); return new Result(result.getResultValue(),Utils.bigEndianByteArrayToInt(Arrays.copyOfRange(result.getData(), 0x04, 0x08))); } @@ -106,7 +106,7 @@ public class FSAOperations extends Operations { public int FSA_CloseFile(int handle, int file_handle) throws IOException{ byte[] inbuffer = new byte[0x520]; Utils.writeIntToByteArray(inbuffer, file_handle, 0x04); - Result res = ioctl.ioctl(handle, 0x15, inbuffer, 0x293); + Result res = system.ioctl(handle, 0x15, inbuffer, 0x293); return res.getResultValue(); } @@ -115,7 +115,7 @@ public class FSAOperations extends Operations { Utils.writeIntToByteArray(inbuffer, size, 0x08); Utils.writeIntToByteArray(inbuffer, cnt, 0x0C); Utils.writeIntToByteArray(inbuffer, file_handle, 0x14); - Result result = ioctl.ioctlv(handle, 0x0F, new byte[][] {inbuffer}, new int[]{size * cnt,0x293}); + Result result = system.ioctlv(handle, 0x0F, new byte[][] {inbuffer}, new int[]{size * cnt,0x293}); return new Result(result.getResultValue(),result.getData()[0]); } diff --git a/src/de/mas/wupclient/client/operations/MCPOperations.java b/src/de/mas/wupclient/client/operations/MCPOperations.java new file mode 100644 index 0000000..9ca7309 --- /dev/null +++ b/src/de/mas/wupclient/client/operations/MCPOperations.java @@ -0,0 +1,31 @@ +package de.mas.wupclient.client.operations; + +import java.util.HashMap; +import java.util.Map; + +import de.mas.wupclient.client.WUPClient; + +public class MCPOperations extends Operations { + private static Map instances = new HashMap<>(); + public static DownloadUploadOperations DownloadUploadOperationsFactory(WUPClient client){ + if(!instances.containsKey(client)){ + instances.put(client, new DownloadUploadOperations(client)); + } + return instances.get(client); + } + + SystemOperations system = null; + + public MCPOperations(WUPClient client) { + super(client); + setSystem(SystemOperations.SystemOperationsFactory(client)); + } + + public SystemOperations getSystem() { + return system; + } + + public void setSystem(SystemOperations system) { + this.system = system; + } +} diff --git a/src/de/mas/wupclient/client/operations/IoctlOperations.java b/src/de/mas/wupclient/client/operations/SystemOperations.java similarity index 52% rename from src/de/mas/wupclient/client/operations/IoctlOperations.java rename to src/de/mas/wupclient/client/operations/SystemOperations.java index d6ed09c..fec9d52 100644 --- a/src/de/mas/wupclient/client/operations/IoctlOperations.java +++ b/src/de/mas/wupclient/client/operations/SystemOperations.java @@ -10,33 +10,33 @@ import de.mas.wupclient.client.WUPClient; import de.mas.wupclient.client.utils.Result; import de.mas.wupclient.client.utils.Utils; -public class IoctlOperations extends Operations { - private static Map instances = new HashMap<>(); - public static IoctlOperations IoctlOperationsFactory(WUPClient client){ +public class SystemOperations extends Operations { + private static Map instances = new HashMap<>(); + public static SystemOperations SystemOperationsFactory(WUPClient client){ if(!instances.containsKey(client)){ - instances.put(client, new IoctlOperations(client)); + instances.put(client, new SystemOperations(client)); } return instances.get(client); } - private IoctlOperations(WUPClient client) { + private SystemOperations(WUPClient client) { super(client); } public Result ioctl(int handle,int cmd, byte[] inbuf,int outbuf_size) throws IOException{ - int in_address = getClient().load_buffer(inbuf); + int in_address = load_buffer(inbuf); byte[] out_data = new byte[0]; int ret; if(outbuf_size > 0){ - int out_address = getClient().alloc(outbuf_size); + int out_address = alloc(outbuf_size); ret = getClient().svc(0x38, new int[] {handle,cmd,in_address,inbuf.length,out_address,outbuf_size}); out_data = getClient().read(out_address, outbuf_size); - getClient().free(out_address); + free(out_address); }else{ ret = getClient().svc(0x38, new int[] {handle,cmd,in_address,inbuf.length,0,0}); } - getClient().free(in_address); + free(in_address); return new Result(ret,out_data); } @@ -48,24 +48,25 @@ public class IoctlOperations extends Operations { destByteBuffer.putInt(data[1]); destByteBuffer.putInt(0); } - return getClient().load_buffer(destByteBuffer.array()); + return load_buffer(destByteBuffer.array()); + } + + public Result ioctlv(int handle, int cmd, byte[][] inbufs, int[] outbuf_sizes) throws IOException { + return ioctlv(handle,cmd,inbufs,outbuf_sizes,new int[0][],new int[0][]); } - /** - * UNTESTED! - */ public Result ioctlv(int handle, int cmd,byte[][] inbufs,int[] outbuf_sizes,int[][] ínbufs_ptr,int[][] outbufs_ptr) throws IOException{ int new_inbufs[][] = new int[inbufs.length][2]; int i = 0; for(byte[] data : inbufs){ - new_inbufs[i][0] = getClient().load_buffer(data, 0x40); + new_inbufs[i][0] = load_buffer(data, 0x40); new_inbufs[i][1] = data.length; i++; } int new_outbufs[][] = new int[outbuf_sizes.length][2]; i = 0; for(int cur_size : outbuf_sizes){ - new_outbufs[i][0] = getClient().alloc(cur_size, 0x40); + new_outbufs[i][0] = alloc(cur_size, 0x40); new_outbufs[i][1] = cur_size; i++; } @@ -83,14 +84,75 @@ public class IoctlOperations extends Operations { i=0; int[][] free_buffer = Utils.concatAll(new_inbufs,new_outbufs); for (int[] foo : free_buffer){ - getClient().free(foo[0]); + free(foo[0]); i++; } - getClient().free(iovecs); + free(iovecs); return new Result(ret,out_data); } - public Result ioctlv(int handle, int cmd, byte[][] inbufs, int[] outbuf_sizes) throws IOException { - return ioctlv(handle,cmd,inbufs,outbuf_sizes,new int[0][],new int[0][]); + + public int alloc(int size) throws IOException{ + return alloc(size,0x00); } + + public int alloc(int size, int align) throws IOException{ + if(size == 0){ + return 0; + } + if(align == 0){ + int result = getClient().svc(0x27, new int[] {0xCAFF,size}); + return result; + }else{ + int result = getClient().svc(0x28, new int[] {0xCAFF,size,align}); + return result; + } + } + + public int free(int address) throws IOException{ + if(address == 0){ + return 0; + } + return getClient().svc(0x29, new int[] {0xCAFF,address}); + } + + public int load_buffer(byte[] data) throws IOException{ + return load_buffer(data,0x00); + } + + public int load_buffer(byte[] data,int align) throws IOException{ + if(data.length == 0) + return 0; + + int address = alloc(data.length, align); + getClient().write(address, data); + return address; + + } + + public int load_string(String b) throws IOException{ + return load_string(b,0x00); + } + + public int load_string(String b,int align) throws IOException{ + byte[] buffer = b.getBytes(); + byte[] newBuffer = new byte[buffer.length+1]; + System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); + newBuffer[buffer.length] = 0; + + int result = load_buffer(newBuffer, align); + return result; + } + + public int open(String device, int mode) throws IOException{ + int address = load_string(device); + int handle = getClient().svc(0x33, new int[] {address,mode}); + free(address); + return handle; + } + + public int close(int handle) throws IOException{ + return getClient().svc(0x34,new int[]{handle}); + } + } diff --git a/src/de/mas/wupclient/client/operations/UtilOperations.java b/src/de/mas/wupclient/client/operations/UtilOperations.java index 0427501..e1dc694 100644 --- a/src/de/mas/wupclient/client/operations/UtilOperations.java +++ b/src/de/mas/wupclient/client/operations/UtilOperations.java @@ -1,10 +1,7 @@ package de.mas.wupclient.client.operations; -import java.io.ByteArrayOutputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -25,10 +22,12 @@ public class UtilOperations extends Operations { } private FSAOperations fsa = null; - + private SystemOperations system = null; + private UtilOperations(WUPClient client) { super(client); setFSAOperations(FSAOperations.FSAOperationsFactory(client)); + setSystem(SystemOperations.SystemOperationsFactory(client)); } public List ls() throws IOException{ return ls(null,false); @@ -102,6 +101,24 @@ public class UtilOperations extends Operations { } } } + public void dump_syslog() throws IOException { + int syslog_address = Utils.bigEndianByteArrayToInt(getClient().read(0x05095ECC, 4)) + 0x10; + int block_size = 0x400; + for(int i = 0;i<0x40000;i += block_size){ + byte[] data = getClient().read(syslog_address + i, block_size); + String log = Utils.getStringFromByteArray(data); + if(!log.isEmpty()){ + System.out.println(log); + }else{ + break; + } + } + } + + public int mkdir(String path, int flags) throws IOException{ + int fsa_handle = getClient().get_fsa_handle(); + return fsa.FSA_MakeDir(fsa_handle, path, 2); + } public int mount(String device_path, String volume_path, int flags) throws IOException{ int fsa_handle = getClient().get_fsa_handle(); @@ -136,101 +153,17 @@ public class UtilOperations extends Operations { return unmount("/vol/storage_odd_ticket", 2); } - public int mkdir(String path, int flags) throws IOException{ - int fsa_handle = getClient().get_fsa_handle(); - return fsa.FSA_MakeDir(fsa_handle, path, 2); - } - - public boolean downloadFolder(String path) throws IOException{ - return downloadFolder(path,null,false); - } - - public boolean downloadFolder(String sourcePath,String targetPath) throws IOException{ - return downloadFolder(sourcePath,targetPath,false); - } - - public boolean downloadFolder(String sourcePath, String targetPath,boolean useRelativPath) throws IOException { - List files = ls(sourcePath,true); - if(targetPath == null || targetPath.isEmpty()){ - targetPath = sourcePath; - } - for(FEntry f: files){ - if(f.isFile()){ - downloadFile(sourcePath, f.getFilename(),targetPath); - }else{ - downloadFolder(sourcePath + "/" + f.getFilename(), targetPath,useRelativPath); - } - } - return false; - } - - public byte[] downloadFileToByteArray(String path) throws IOException{ - Logger.logCmd("Downloading " + path); - int fsa_handle = getClient().get_fsa_handle(); - Result res = fsa.FSA_OpenFile(fsa_handle, path, "r"); - boolean success = false; - byte[] result = null; - if(res.getResultValue() == 0){ - int block_size = 0x400; - ByteArrayOutputStream out = new ByteArrayOutputStream(); - success = true; - while(true){ - Result read_result= fsa.FSA_ReadFile(fsa_handle, res.getData(), 0x1, block_size); - - if(read_result.getResultValue() <0){ - Logger.logErr("FSA_ReadFile returned " + read_result.getResultValue()); - success = false; - break; - } - - out.write(Arrays.copyOf(read_result.getData(), read_result.getResultValue())); - - if(read_result.getResultValue() <= 0) - break; - } - fsa.FSA_CloseFile(fsa_handle, res.getData()); - if(success){ - result = out.toByteArray(); - } - } - return result; - } - - public boolean downloadFile(String sourcePath, String filename, String targetPath) throws IOException { - return downloadFile(sourcePath, filename, targetPath,null); - } - - public boolean downloadFile(String sourcePath,String sourceFilename,String targetPath,String targetFileName) throws IOException { - byte[] data = downloadFileToByteArray(sourcePath + "/" + sourceFilename); - if(data == null){ - System.out.println("failed"); - return false; - } - String subdir = ""; - - if(targetFileName == null){ - subdir += targetPath + "/" + sourceFilename; - }else{ - subdir += targetPath + "/" + targetFileName; - } - if(subdir.startsWith("/")){ - subdir = subdir.substring(1); - } - //System.out.println(subdir); - Utils.createSubfolder(subdir); - FileOutputStream stream = new FileOutputStream(subdir); - try { - stream.write(data); - } finally { - stream.close(); - } - return true; - } - public FSAOperations getFSAOperations() { return fsa; } public void setFSAOperations(FSAOperations fsa) { this.fsa = fsa; } + + public SystemOperations getSystem() { + return system; + } + public void setSystem(SystemOperations system) { + this.system = system; + } } diff --git a/src/de/mas/wupclient/client/utils/MetaInformation.java b/src/de/mas/wupclient/client/utils/MetaInformation.java index e009828..a5278f6 100644 --- a/src/de/mas/wupclient/client/utils/MetaInformation.java +++ b/src/de/mas/wupclient/client/utils/MetaInformation.java @@ -1,8 +1,6 @@ package de.mas.wupclient.client.utils; import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; public class MetaInformation implements Comparable, Serializable{ private static final long serialVersionUID = 1L; @@ -109,9 +107,6 @@ public class MetaInformation implements Comparable, Serializabl @Override public String toString(){ String result = getTitleIDAsString() + ";" + region +";" + getContent_platform() + ";" + getCompany_code() + ";"+ getProduct_code()+ ";" + getID6() + ";" + getLongnameEN(); - for(Integer i :versions){ - result += ";" + i; - } return result; } diff --git a/src/de/mas/wupclient/client/utils/Utils.java b/src/de/mas/wupclient/client/utils/Utils.java index f08d706..758ed91 100644 --- a/src/de/mas/wupclient/client/utils/Utils.java +++ b/src/de/mas/wupclient/client/utils/Utils.java @@ -65,10 +65,7 @@ public class Utils { } public static byte[] intToBigEndianByteArray(int value){ - ByteBuffer destByteBuffer = ByteBuffer.allocate(4); - destByteBuffer.order(ByteOrder.BIG_ENDIAN); - destByteBuffer.putInt(value); - return destByteBuffer.array(); + return m_packBE(value); } public static int bigEndianByteArrayToInt(byte[] data){ @@ -139,7 +136,7 @@ public class Utils { String company_code = document.getElementsByTagName("company_code").item(0).getTextContent().toString(); String content_platform = document.getElementsByTagName("content_platform").item(0).getTextContent().toString(); String region = document.getElementsByTagName("region").item(0).getTextContent().toString(); - MetaInformation nusinfo = new MetaInformation(Utils.StringToLong(title_id),longname,ID6,proc,content_platform,company_code,(int) StringToLong(region),new String[1]); + MetaInformation nusinfo = new MetaInformation(Utils.StringToLong(title_id),longname,ID6,proc,content_platform,company_code,(int) StringToLong(region)); return nusinfo; } catch (ParserConfigurationException | SAXException | IOException e) { @@ -170,4 +167,43 @@ public class Utils { } } + public static byte[] m_packBE(Object... args){ + return m_pack(ByteOrder.BIG_ENDIAN,args); + } + + public static byte[] m_pack(ByteOrder order,Object... args){ + int totalLength = 0; + for(Object o :args){ + if(o instanceof String){ + totalLength += ((String)o).getBytes().length; + }else if(o instanceof Integer){ + totalLength += 0x04; + }else if(o instanceof int[]){ + totalLength += 0x04 * ((int[])o).length; + }else if(o instanceof byte[]){ + totalLength += ((byte[])o).length; + }else{ + Logger.logErr("Can't use this datatype"); + } + } + ByteBuffer destByteBuffer = ByteBuffer.allocate(totalLength); + destByteBuffer.order(order); + for(Object o :args){ + if(o instanceof String){ + destByteBuffer.put(((String)o).getBytes()); + }else if(o instanceof Integer){ + destByteBuffer.putInt((Integer)o); + }else if(o instanceof int[]){ + for(Integer i : ((int[])o)){ + destByteBuffer.putInt(i); + } + }else if(o instanceof byte[]){ + destByteBuffer.put((byte[])o); + }else{ + Logger.logErr("Can't use this datatype"); + } + } + return destByteBuffer.array(); + } + }