RCM (Fusée Gelée) support, numerous UI updates and a lot of things for version 2.

This commit is contained in:
Dmitry Isaenko 2020-02-10 02:19:39 +03:00
parent 3d3fb56f9e
commit 010c33c593
36 changed files with 1572 additions and 92 deletions

View file

@ -123,7 +123,7 @@ public class AppPreferences {
public double getSceneWidth(){ return preferences.getDouble("WIND_WIDTH", 850.0); }
public void setSceneWidth(double value){ preferences.putDouble("WIND_WIDTH", value); }
public double getSceneHeight(){ return preferences.getDouble("WIND_HEIGHT", 475.0); }
public double getSceneHeight(){ return preferences.getDouble("WIND_HEIGHT", 525.0); }
public void setSceneHeight(double value){ preferences.putDouble("WIND_HEIGHT", value); }
// Split and Merge //
public int getSplitMergeType(){ return preferences.getInt("SM_TYPE", 0); }
@ -131,4 +131,7 @@ public class AppPreferences {
public String getSplitMergeRecent(){ return preferences.get("SM_RECENT", System.getProperty("user.home")); }
public void setSplitMergeRecent(String value){ preferences.put("SM_RECENT", value); }
// RCM //
public String getRecentRcm(int num){ return preferences.get(String.format("RCM_%02d", num), ""); }
public void setRecentRcm(int num, String value){ preferences.put(String.format("RCM_%02d", num), value); }
}

View file

@ -42,14 +42,14 @@ public class UsbCommunications extends Task<Void> {
protected Void call() {
logPrinter.print("\tStart chain", EMsgType.INFO);
UsbConnect usbConnect = new UsbConnect(logPrinter);
UsbConnect usbConnect = new UsbConnect(logPrinter, false);
if (! usbConnect.isConnected()){
close(EFileStatus.FAILED);
return null;
}
DeviceHandle handler = usbConnect.getHandlerNS();
DeviceHandle handler = usbConnect.getNsHandler();
TransferModule module;

View file

@ -4,20 +4,41 @@ import nsusbloader.ModelControllers.LogPrinter;
import nsusbloader.NSLDataTypes.EMsgType;
import org.usb4java.*;
class UsbConnect {
private final int DEFAULT_INTERFACE = 0;
public class UsbConnect {
private int DEFAULT_INTERFACE;
private Context contextNS;
private DeviceHandle handlerNS;
private Device deviceNS;
private LogPrinter logPrinter;
private boolean connected;
UsbConnect(LogPrinter logPrinter){
private boolean connected; // TODO: replace to 'connectionFailure' and invert requests everywhere
public UsbConnect(LogPrinter logPrinter, boolean initForRCM){
this.logPrinter = logPrinter;
this.connected = false;
short VENDOR_ID;
short PRODUCT_ID;
if (initForRCM){
// CORRECT NV:
DEFAULT_INTERFACE = 1;
VENDOR_ID = 0x0955;
PRODUCT_ID = 0x7321;
/* // QA:
DEFAULT_INTERFACE = 0;
VENDOR_ID = 0x1a86;
PRODUCT_ID = 0x7523;
*/
}
else {
DEFAULT_INTERFACE = 0;
VENDOR_ID = 0x057E;
PRODUCT_ID = 0x3000;
}
int result;
// Creating Context required by libusb. Optional. TODO: Consider removing.
@ -28,8 +49,7 @@ class UsbConnect {
close();
return;
}
else
logPrinter.print("libusb initialization", EMsgType.PASS);
logPrinter.print("libusb initialization", EMsgType.PASS);
// Searching for NS in devices: obtain list of all devices
DeviceList deviceList = new DeviceList();
@ -39,11 +59,10 @@ class UsbConnect {
close();
return;
}
else
logPrinter.print("Get device list", EMsgType.PASS);
logPrinter.print("Get device list", EMsgType.PASS);
// Searching for NS in devices: looking for NS
DeviceDescriptor descriptor;
Device deviceNS = null;
deviceNS = null;
for (Device device: deviceList){
descriptor = new DeviceDescriptor(); // mmm.. leave it as is.
result = LibUsb.getDeviceDescriptor(device, descriptor);
@ -53,21 +72,20 @@ class UsbConnect {
close();
return;
}
if ((descriptor.idVendor() == 0x057E) && descriptor.idProduct() == 0x3000){
if ((descriptor.idVendor() == VENDOR_ID) && descriptor.idProduct() == PRODUCT_ID){
deviceNS = device;
logPrinter.print("Read file descriptors for USB devices", EMsgType.PASS);
break;
}
}
// Free device list.
if (deviceNS != null){
logPrinter.print("NS in connected USB devices found", EMsgType.PASS);
}
else {
if (deviceNS == null){
logPrinter.print("NS in connected USB devices not found", EMsgType.FAIL);
close();
return;
}
logPrinter.print("NS in connected USB devices found", EMsgType.PASS);
// Handle NS device
handlerNS = new DeviceHandle();
result = LibUsb.open(deviceNS, handlerNS);
@ -75,10 +93,11 @@ class UsbConnect {
logPrinter.print("Open NS USB device\n Returned: "+UsbErrorCodes.getErrCode(result), EMsgType.FAIL);
if (result == LibUsb.ERROR_ACCESS)
logPrinter.print("Double check that you have administrator privileges (you're 'root') or check 'udev' rules set for this user (linux only)!\n\n" +
"Steps to set 'udev' rules:\n" +
"root # vim /etc/udev/rules.d/99-NS.rules\n" +
"SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"057e\", ATTRS{idProduct}==\"3000\", GROUP=\"plugdev\"\n" +
"root # udevadm control --reload-rules && udevadm trigger\n", EMsgType.INFO);
String.format("Steps to set 'udev' rules:\n" +
"root # vim /etc/udev/rules.d/99-NS.rules\n" +
"SUBSYSTEM==\"usb\", ATTRS{idVendor}==\"%04x\", ATTRS{idProduct}==\"%04x\", GROUP=\"plugdev\"\n" +
"root # udevadm control --reload-rules && udevadm trigger\n", VENDOR_ID, PRODUCT_ID)
, EMsgType.INFO);
// Let's make a bit dirty workaround since such shit happened
logPrinter.print("Requested context close", EMsgType.INFO);
LibUsb.exit(contextNS);
@ -107,25 +126,24 @@ class UsbConnect {
return;
}
*/
// Set configuration (soft reset if needed)
result = LibUsb.setConfiguration(handlerNS, 1); // 1 - configuration all we need
if (result != LibUsb.SUCCESS){
logPrinter.print("Set active configuration to device\n Returned: "+UsbErrorCodes.getErrCode(result), EMsgType.FAIL);
close();
return;
}
else
if ( ! initForRCM){
// Set configuration (soft reset if needed)
result = LibUsb.setConfiguration(handlerNS, 1); // 1 - configuration all we need
if (result != LibUsb.SUCCESS){
logPrinter.print("Set active configuration to device\n Returned: "+UsbErrorCodes.getErrCode(result), EMsgType.FAIL);
close();
return;
}
logPrinter.print("Set active configuration to device.", EMsgType.PASS);
}
// Claim interface
result = LibUsb.claimInterface(handlerNS, DEFAULT_INTERFACE);
if (result != LibUsb.SUCCESS) {
logPrinter.print("Claim interface\n Returned: "+UsbErrorCodes.getErrCode(result), EMsgType.FAIL);
logPrinter.print("Claim interface\n Returned: "+UsbErrorCodes.getErrCode(result), EMsgType.FAIL);
close();
return;
}
else
logPrinter.print("Claim interface", EMsgType.PASS);
logPrinter.print("Claim interface", EMsgType.PASS);
this.connected = true;
}
@ -134,23 +152,36 @@ class UsbConnect {
* Get USB status
* @return status of connection
*/
boolean isConnected() { return connected; }
public boolean isConnected() { return connected; }
/**
* Getter for handler
* @return DeviceHandle of NS
*/
DeviceHandle getHandlerNS(){ return handlerNS; }
public DeviceHandle getNsHandler(){ return handlerNS; }
/**
* Getter for 'Bus ID' where NS located found
*/
public int getNsBus(){
return LibUsb.getBusNumber(deviceNS);
}
/**
* Getter for 'Device address' where NS located at
*/
public int getNsAddress(){
return LibUsb.getDeviceAddress(deviceNS);
}
/**
* Correct exit
* */
void close(){
public void close(){
// Close handler in the end
if (handlerNS != null) {
// Try to release interface
int result = LibUsb.releaseInterface(handlerNS, DEFAULT_INTERFACE);
if (result != LibUsb.SUCCESS)
logPrinter.print("Release interface\n Returned: "+result+" (sometimes it's not an issue)", EMsgType.WARNING);
logPrinter.print("Release interface" +
"\n Returned: "+result+" (sometimes it's not an issue)", EMsgType.WARNING);
else
logPrinter.print("Release interface", EMsgType.PASS);

View file

@ -2,8 +2,8 @@ package nsusbloader.COM.USB;
import org.usb4java.LibUsb;
class UsbErrorCodes {
static String getErrCode(int value){
public class UsbErrorCodes {
public static String getErrCode(int value){
switch (value){
case LibUsb.ERROR_ACCESS:
return "ERROR_ACCESS";

View file

@ -7,7 +7,6 @@ import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
@ -25,8 +24,6 @@ import java.util.List;
import java.util.ResourceBundle;
public class FrontController implements Initializable {
@FXML
private Pane specialPane;
@FXML
private AnchorPane usbNetPane;
@ -52,7 +49,6 @@ public class FrontController implements Initializable {
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.resourceBundle = resourceBundle;
specialPane.getStyleClass().add("special-pane-as-border"); // UI hacks
ObservableList<String> choiceProtocolList = FXCollections.observableArrayList("TinFoil", "GoldLeaf");
choiceProtocol.setItems(choiceProtocolList);
@ -195,7 +191,7 @@ public class FrontController implements Initializable {
else
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("NSP ROM", "*.nsp"));
filesList = fileChooser.showOpenMultipleDialog(specialPane.getScene().getWindow());
filesList = fileChooser.showOpenMultipleDialog(usbNetPane.getScene().getWindow());
if (filesList != null && !filesList.isEmpty()) {
tableFilesListController.setFiles(filesList);
uploadStopBtn.setDisable(false);
@ -216,7 +212,7 @@ public class FrontController implements Initializable {
else
dirChooser.setInitialDirectory(new File(System.getProperty("user.home")));
splitFile = dirChooser.showDialog(specialPane.getScene().getWindow());
splitFile = dirChooser.showDialog(usbNetPane.getScene().getWindow());
if (splitFile != null && splitFile.getName().toLowerCase().endsWith(".nsp")) {
tableFilesListController.setFile(splitFile);

View file

@ -31,6 +31,8 @@ public class NSLMainController implements Initializable {
private SettingsController SettingsTabController;
@FXML
private SplitMergeController SplitMergeTabController;
@FXML
private RcmController RcmTabController;
@Override
public void initialize(URL url, ResourceBundle rb) {
@ -123,6 +125,8 @@ public class NSLMainController implements Initializable {
public SplitMergeController getSmCtrlr(){
return SplitMergeTabController;
}
public RcmController getRcmCtrlr(){ return RcmTabController; }
/**
* Save preferences before exit
* */
@ -147,5 +151,6 @@ public class NSLMainController implements Initializable {
);
SplitMergeTabController.updatePreferencesOnExit(); // NOTE: This shit above should be re-written to similar pattern
RcmTabController.updatePreferencesOnExit();
}
}

View file

@ -0,0 +1,257 @@
package nsusbloader.Controllers;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import nsusbloader.AppPreferences;
import nsusbloader.MediatorControl;
import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.ServiceWindow;
import nsusbloader.Utilities.RcmTask;
import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
public class RcmController implements Initializable {
@FXML
private ToggleGroup rcmToggleGrp;
@FXML
private VBox rcmToolPane;
@FXML
private RadioButton pldrRadio1,
pldrRadio2,
pldrRadio3,
pldrRadio4,
pldrRadio5;
@FXML
private Button injectPldBtn;
@FXML
private Label payloadFNameLbl1, payloadFPathLbl1,
payloadFNameLbl2, payloadFPathLbl2,
payloadFNameLbl3, payloadFPathLbl3,
payloadFNameLbl4, payloadFPathLbl4,
payloadFNameLbl5, payloadFPathLbl5;
@FXML
private Label statusLbl;
private ResourceBundle rb;
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
this.rb = resourceBundle;
rcmToggleGrp.selectToggle(pldrRadio1);
pldrRadio1.setOnAction(e -> statusLbl.setText(""));
pldrRadio2.setOnAction(e -> statusLbl.setText(""));
pldrRadio3.setOnAction(e -> statusLbl.setText(""));
pldrRadio4.setOnAction(e -> statusLbl.setText(""));
pldrRadio5.setOnAction(e -> statusLbl.setText(""));
String recentRcm1 = AppPreferences.getInstance().getRecentRcm(1);
String recentRcm2 = AppPreferences.getInstance().getRecentRcm(2);
String recentRcm3 = AppPreferences.getInstance().getRecentRcm(3);
String recentRcm4 = AppPreferences.getInstance().getRecentRcm(4);
String recentRcm5 = AppPreferences.getInstance().getRecentRcm(5);
String myRegexp;
if (File.separator.equals("/"))
myRegexp = "^.+/";
else
myRegexp = "^.+\\\\";
if (! recentRcm1.isEmpty()) {
payloadFNameLbl1.setText(recentRcm1.replaceAll(myRegexp, ""));
payloadFPathLbl1.setText(recentRcm1);
}
if (! recentRcm2.isEmpty()) {
payloadFNameLbl2.setText(recentRcm2.replaceAll(myRegexp, ""));
payloadFPathLbl2.setText(recentRcm2);
}
if (! recentRcm3.isEmpty()) {
payloadFNameLbl3.setText(recentRcm3.replaceAll(myRegexp, ""));
payloadFPathLbl3.setText(recentRcm3);
}
if (! recentRcm4.isEmpty()) {
payloadFNameLbl4.setText(recentRcm4.replaceAll(myRegexp, ""));
payloadFPathLbl4.setText(recentRcm4);
}
if (! recentRcm5.isEmpty()) {
payloadFNameLbl5.setText(recentRcm5.replaceAll(myRegexp, ""));
payloadFPathLbl5.setText(recentRcm5);
}
injectPldBtn.setDisable(false); // TODO: write logic ?? Like in case PAYLOADER exist, button active. If not: not active?
injectPldBtn.setOnAction(actionEvent -> smash());
}
private void smash(){
statusLbl.setText("");
if (MediatorControl.getInstance().getTransferActive()) {
ServiceWindow.getErrorNotification(rb.getString("windowTitleError"), rb.getString("windowBodyPleaseFinishTransfersFirst"));
return;
}
Task<Boolean> RcmTask;
RadioButton selectedRadio = (RadioButton)rcmToggleGrp.getSelectedToggle();
switch (selectedRadio.getId()){
case "pldrRadio1":
RcmTask = new RcmTask(payloadFPathLbl1.getText());
break;
case "pldrRadio2":
RcmTask = new RcmTask(payloadFPathLbl2.getText());
break;
case "pldrRadio3":
RcmTask = new RcmTask(payloadFPathLbl3.getText());
break;
case "pldrRadio4":
RcmTask = new RcmTask(payloadFPathLbl4.getText());
break;
case "pldrRadio5":
RcmTask = new RcmTask(payloadFPathLbl5.getText());
break;
default:
return;
}
RcmTask.setOnSucceeded(event -> {
if (RcmTask.getValue())
statusLbl.setText(rb.getString("done_txt"));
else
statusLbl.setText(rb.getString("failure_txt"));
});
Thread RcmThread = new Thread(RcmTask);
RcmThread.setDaemon(true);
RcmThread.start();
}
@FXML
private void bntSelectPayloader(ActionEvent event){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(rb.getString("btn_Select"));
File validator = new File(payloadFPathLbl1.getText()).getParentFile();
if (validator != null && validator.exists())
fileChooser.setInitialDirectory(validator);
else
fileChooser.setInitialDirectory(new File(System.getProperty("user.home")));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("bin", "*.bin"));
File payloadFile = fileChooser.showOpenDialog(payloadFPathLbl1.getScene().getWindow());
if (payloadFile != null) {
final Node btn = (Node)event.getSource();
switch (btn.getId()){
case "selPldBtn1":
payloadFNameLbl1.setText(payloadFile.getName());
payloadFPathLbl1.setText(payloadFile.getAbsolutePath());
rcmToggleGrp.selectToggle(pldrRadio1);
break;
case "selPldBtn2":
payloadFNameLbl2.setText(payloadFile.getName());
payloadFPathLbl2.setText(payloadFile.getAbsolutePath());
rcmToggleGrp.selectToggle(pldrRadio2);
break;
case "selPldBtn3":
payloadFNameLbl3.setText(payloadFile.getName());
payloadFPathLbl3.setText(payloadFile.getAbsolutePath());
rcmToggleGrp.selectToggle(pldrRadio3);
break;
case "selPldBtn4":
payloadFNameLbl4.setText(payloadFile.getName());
payloadFPathLbl4.setText(payloadFile.getAbsolutePath());
rcmToggleGrp.selectToggle(pldrRadio4);
break;
case "selPldBtn5":
payloadFNameLbl5.setText(payloadFile.getName());
payloadFPathLbl5.setText(payloadFile.getAbsolutePath());
rcmToggleGrp.selectToggle(pldrRadio5);
}
}
}
@FXML
private void bntResetPayloader(ActionEvent event){
final Node btn = (Node)event.getSource();
switch (btn.getId()){
case "resPldBtn1":
payloadFNameLbl1.setText("");
payloadFPathLbl1.setText("");
statusLbl.setText("");
break;
case "resPldBtn2":
payloadFNameLbl2.setText("");
payloadFPathLbl2.setText("");
statusLbl.setText("");
break;
case "resPldBtn3":
payloadFNameLbl3.setText("");
payloadFPathLbl3.setText("");
statusLbl.setText("");
break;
case "resPldBtn4":
payloadFNameLbl4.setText("");
payloadFPathLbl4.setText("");
statusLbl.setText("");
break;
case "resPldBtn5":
payloadFNameLbl5.setText("");
payloadFPathLbl5.setText("");
statusLbl.setText("");
}
}
@FXML
public void selectPldrPane(MouseEvent mouseEvent) {
final Node selectedPane = (Node)mouseEvent.getSource();
switch (selectedPane.getId()){
case "pldPane1":
pldrRadio1.fire();
break;
case "pldPane2":
pldrRadio2.fire();
break;
case "pldPane3":
pldrRadio3.fire();
break;
case "pldPane4":
pldrRadio4.fire();
break;
case "pldPane5":
pldrRadio5.fire();
break;
}
}
public void notifySmThreadStarted(boolean isStart, EModule type){
rcmToolPane.setDisable(isStart);
if (type.equals(EModule.RCM) && isStart){
MediatorControl.getInstance().getContoller().logArea.clear();
}
}
/**
* Save application settings on exit
* */
public void updatePreferencesOnExit(){
AppPreferences.getInstance().setRecentRcm(1, payloadFPathLbl1.getText());
AppPreferences.getInstance().setRecentRcm(2, payloadFPathLbl2.getText());
AppPreferences.getInstance().setRecentRcm(3, payloadFPathLbl3.getText());
AppPreferences.getInstance().setRecentRcm(4, payloadFPathLbl4.getText());
AppPreferences.getInstance().setRecentRcm(5, payloadFPathLbl5.getText());
}
}

View file

@ -88,10 +88,15 @@ public class SplitMergeController implements Initializable {
if (splitRad.isSelected()) {
FileChooser fc = new FileChooser();
fc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFile"));
if (fileFolderActualPathLbl.getText().isEmpty())
fc.setInitialDirectory(new File(System.getProperty("user.home")));
if (! fileFolderActualPathLbl.getText().isEmpty()){
File temporaryFile = new File(fileFolderActualPathLbl.getText()).getParentFile();
if (temporaryFile != null && temporaryFile.exists())
fc.setInitialDirectory(temporaryFile);
else
fc.setInitialDirectory(new File(System.getProperty("user.home")));
}
else
fc.setInitialDirectory(new File(fileFolderActualPathLbl.getText()).getParentFile());
fc.setInitialDirectory(new File(System.getProperty("user.home")));
File fileFile = fc.showOpenDialog(changeSaveToBtn.getScene().getWindow());
if (fileFile == null)
return;
@ -101,10 +106,16 @@ public class SplitMergeController implements Initializable {
else{
DirectoryChooser dc = new DirectoryChooser();
dc.setTitle(resourceBundle.getString("tabSplMrg_Btn_SelectFolder"));
if (fileFolderActualPathLbl.getText().isEmpty())
dc.setInitialDirectory(new File(System.getProperty("user.home")));
if (! fileFolderActualPathLbl.getText().isEmpty()){
File temporaryFile = new File(fileFolderActualPathLbl.getText());
if (temporaryFile.exists())
dc.setInitialDirectory(temporaryFile);
else
dc.setInitialDirectory(new File(System.getProperty("user.home")));
}
else
dc.setInitialDirectory(new File(fileFolderActualPathLbl.getText()));
dc.setInitialDirectory(new File(System.getProperty("user.home")));
File folderFile = dc.showDialog(changeSaveToBtn.getScene().getWindow());
if (folderFile == null)
return;
@ -132,6 +143,8 @@ public class SplitMergeController implements Initializable {
convertBtn.setText(resourceBundle.getString("btn_Stop"));
convertRegion.getStyleClass().clear();
convertRegion.getStyleClass().add("regionStop");
convertBtn.getStyleClass().remove("buttonUp");
convertBtn.getStyleClass().add("buttonStop");
return;
}
splitRad.setDisable(false);
@ -142,6 +155,8 @@ public class SplitMergeController implements Initializable {
convertBtn.setOnAction(e -> setConvertBtnAction());
convertBtn.setText(resourceBundle.getString("tabSplMrg_Btn_Convert"));
convertRegion.getStyleClass().clear();
convertBtn.getStyleClass().remove("buttonStop");
convertBtn.getStyleClass().add("buttonUp");
if (splitRad.isSelected())
convertRegion.getStyleClass().add("regionSplitToOne");
else
@ -159,6 +174,7 @@ public class SplitMergeController implements Initializable {
* It's button listener when convert-process NOT in progress
* */
private void setConvertBtnAction(){
statusLbl.setText("");
if (MediatorControl.getInstance().getTransferActive()) {
ServiceWindow.getErrorNotification(resourceBundle.getString("windowTitleError"), resourceBundle.getString("windowBodyPleaseFinishTransfersFirst"));
return;

View file

@ -25,6 +25,7 @@ public class MediatorControl {
isTransferActive.set(isActive);
mainCtrler.getFrontCtrlr().notifyTransmThreadStarted(isActive, appModuleType);
mainCtrler.getSmCtrlr().notifySmThreadStarted(isActive, appModuleType);
mainCtrler.getRcmCtrlr().notifySmThreadStarted(isActive, appModuleType);
}
public synchronized boolean getTransferActive() { return this.isTransferActive.get(); }
}

View file

@ -13,7 +13,7 @@ import java.util.ResourceBundle;
public class NSLMain extends Application {
public static final String appVersion = "v1.1";
public static final String appVersion = "v2.0";
@Override
public void start(Stage primaryStage) throws Exception{

View file

@ -0,0 +1,105 @@
package nsusbloader.Utilities;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
public class JNIRcmLoader {
private JNIRcmLoader(){}
public static boolean load(){
String osName = System.getProperty("os.name").toLowerCase().replace(" ", "");
String osArch = System.getProperty("os.arch").toLowerCase().replace(" ", "");
String libPostfix;
if (osName.equals("linux")){
switch (osArch){
case "i386":
case "i586":
case "i686":
osArch = "x86";
break;
case "x86_64":
case "amd64":
osArch = "amd64";
break;
default:
return false;
}
libPostfix = "so";
}
else if (osName.contains("windows")){
osName = "windows";
libPostfix = "dll";
switch (osArch){
case "x86":
case "i386":
case "i586":
case "i686":
osArch = "x86";
break;
case "x86_64":
case "amd64":
osArch = "amd64";
break;
default:
return false;
}
}
else
return false;
final URL url_ = RcmSmash.class.getResource("/native/"+osName+"/"+osArch+"/smashlib."+libPostfix);
if (url_ == null)
return false;
String proto = url_.getProtocol();
File libraryFile;
if (proto.equals("file")){
// We can pick file from disk as is.
try {
libraryFile = new File(url_.toURI());
}
catch (URISyntaxException e){
e.printStackTrace();
return false;
}
}
else if (proto.equals("jar")){
// We have to export file to temp dir.
InputStream inStream = RcmSmash.class.getResourceAsStream("/native/"+osName+"/"+osArch+"/smashlib."+libPostfix);
if (inStream == null)
return false;
// Create temp folder
try{
File tmpDirFile = File.createTempFile("jni", null);
if (! tmpDirFile.delete())
return false;
if (! tmpDirFile.mkdirs())
return false;
libraryFile = new File(tmpDirFile, "smashlib."+libPostfix);
byte[] ioBuffer = new byte[8192];
FileOutputStream foStream = new FileOutputStream(libraryFile);
while (inStream.read(ioBuffer) != -1)
foStream.write(ioBuffer);
foStream.close();
inStream.close();
libraryFile.deleteOnExit();
tmpDirFile.deleteOnExit();
}
catch (IOException ioe){
ioe.printStackTrace();
return false;
}
}
else
return false;
//System.out.println("LIB LOCATION: "+libraryFile);
System.load(libraryFile.getAbsolutePath());
//System.out.println("LIB LOADED");
return true;
}
}

View file

@ -0,0 +1,17 @@
package nsusbloader.Utilities;
public class RcmSmash {
private static final boolean supported;
static {
supported = JNIRcmLoader.load();
}
private RcmSmash(){}
public static native int smashLinux(final int bus_id, final int device_addr);
public static native int smashWindows();
public static boolean isSupported() { return supported; }
}

View file

@ -0,0 +1,332 @@
package nsusbloader.Utilities;
/*
* Implementation of the 'Fusée Gelée' RCM payload that is inspired by 'fusee-launcher' application by ktemkin.
* Definitely uses ideas and even some code.
* Check original project: https://github.com/reswitched/fusee-launcher
*
* This code is not political. It could be used by anyone.
* Find details in LICENSE file in the root directory of this project.
**/
import javafx.concurrent.Task;
import nsusbloader.COM.USB.UsbConnect;
import nsusbloader.COM.USB.UsbErrorCodes;
import nsusbloader.ModelControllers.LogPrinter;
import nsusbloader.NSLDataTypes.EModule;
import nsusbloader.NSLDataTypes.EMsgType;
import org.usb4java.*;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Arrays;
public class RcmTask extends Task<Boolean> {
private enum ECurrentOS {
win, lin, mac, unsupported
}
private LogPrinter logPrinter;
private String filePath;
private DeviceHandle handler;
private byte[] fullPayload;
private static final byte[] initSeq = { (byte) 0x98, (byte) 0x02, (byte) 0x03 };
private static final byte[] mezzo = {
(byte) 0x5c, (byte) 0x00, (byte) 0x9f, (byte) 0xe5, (byte) 0x5c, (byte) 0x10, (byte) 0x9f, (byte) 0xe5, (byte) 0x5c, (byte) 0x20, (byte) 0x9f, (byte) 0xe5, (byte) 0x01, (byte) 0x20, (byte) 0x42, (byte) 0xe0,
(byte) 0x0e, (byte) 0x00, (byte) 0x00, (byte) 0xeb, (byte) 0x48, (byte) 0x00, (byte) 0x9f, (byte) 0xe5, (byte) 0x10, (byte) 0xff, (byte) 0x2f, (byte) 0xe1, (byte) 0x00, (byte) 0x00, (byte) 0xa0, (byte) 0xe1,
(byte) 0x48, (byte) 0x00, (byte) 0x9f, (byte) 0xe5, (byte) 0x48, (byte) 0x10, (byte) 0x9f, (byte) 0xe5, (byte) 0x01, (byte) 0x29, (byte) 0xa0, (byte) 0xe3, (byte) 0x07, (byte) 0x00, (byte) 0x00, (byte) 0xeb,
(byte) 0x38, (byte) 0x00, (byte) 0x9f, (byte) 0xe5, (byte) 0x01, (byte) 0x19, (byte) 0xa0, (byte) 0xe3, (byte) 0x01, (byte) 0x00, (byte) 0x80, (byte) 0xe0, (byte) 0x34, (byte) 0x10, (byte) 0x9f, (byte) 0xe5,
(byte) 0x03, (byte) 0x28, (byte) 0xa0, (byte) 0xe3, (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0xeb, (byte) 0x20, (byte) 0x00, (byte) 0x9f, (byte) 0xe5, (byte) 0x10, (byte) 0xff, (byte) 0x2f, (byte) 0xe1,
(byte) 0x04, (byte) 0x30, (byte) 0x91, (byte) 0xe4, (byte) 0x04, (byte) 0x30, (byte) 0x80, (byte) 0xe4, (byte) 0x04, (byte) 0x20, (byte) 0x52, (byte) 0xe2, (byte) 0xfb, (byte) 0xff, (byte) 0xff, (byte) 0x1a,
(byte) 0x1e, (byte) 0xff, (byte) 0x2f, (byte) 0xe1, (byte) 0x00, (byte) 0xf0, (byte) 0x00, (byte) 0x40, (byte) 0x20, (byte) 0x00, (byte) 0x01, (byte) 0x40, (byte) 0x7c, (byte) 0x00, (byte) 0x01, (byte) 0x40,
(byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x40, (byte) 0x40, (byte) 0x0e, (byte) 0x01, (byte) 0x40, (byte) 0x00, (byte) 0x70, (byte) 0x01, (byte) 0x40
}; // 124 bytes
private static final byte[] sprayPttrn = { 0x00, 0x00, 0x01, 0x40};
public RcmTask(String filePath){
this.logPrinter = new LogPrinter(EModule.RCM);
this.filePath = filePath;
}
@Override
protected Boolean call() {
logPrinter.print("Selected: "+filePath, EMsgType.INFO);
logPrinter.print("=============== RCM ===============", EMsgType.INFO);
ECurrentOS ecurrentOS;
String realOsName = System.getProperty("os.name").toLowerCase().replace(" ", "");
if (realOsName.equals("macos") || realOsName.equals("macosx") || realOsName.equals("freebsd"))
ecurrentOS = ECurrentOS.mac;
else if (realOsName.contains("windows"))
ecurrentOS = ECurrentOS.win;
else if (realOsName.equals("linux"))
ecurrentOS = ECurrentOS.lin;
else
ecurrentOS = ECurrentOS.unsupported;
logPrinter.print("Found your OS: "+System.getProperty("os.name"), EMsgType.PASS);
if (! ecurrentOS.equals(ECurrentOS.mac)){
if (! RcmSmash.isSupported()){
logPrinter.print("Unfortunately your platform '"+System.getProperty("os.name")+
"' of '"+System.getProperty("os.arch")+"' is not supported :("+
"\n But you could file a bug with request."+
"\n\n Nothing has been sent to NS. Execution stopped.", EMsgType.FAIL);
logPrinter.close();
return false;
}
}
if (preparePayload()){
logPrinter.close();
return false;
}
// === TEST THIS ===
// writeTestFile();
// =================
// Bring up USB connection
UsbConnect usbConnect = new UsbConnect(logPrinter, true);
if (! usbConnect.isConnected()){
logPrinter.close();
return false;
}
this.handler = usbConnect.getNsHandler();
// Get device ID and show it.
if (readUsbDeviceID()){
usbConnect.close();
logPrinter.close();
return false;
}
// Send payload
for (int i=0; i < fullPayload.length / 4096 ; i++){
if (writeUsb(Arrays.copyOfRange(fullPayload, i*4096, (i+1)*4096))){
logPrinter.print("Failed to sent payload ["+i+"]"+
"\n\n Execution stopped.", EMsgType.FAIL);
usbConnect.close();
logPrinter.close();
return false;
}
}
logPrinter.print("Information sent to NS.", EMsgType.PASS);
if (ecurrentOS.equals(ECurrentOS.mac)){
if (smashMacOS()){
usbConnect.close();
logPrinter.close();
return false;
}
}
else {
// JNI MAGIC HERE
int retval;
if (ecurrentOS.equals(ECurrentOS.lin))
retval = RcmSmash.smashLinux(usbConnect.getNsBus(), usbConnect.getNsAddress());
else if (ecurrentOS.equals(ECurrentOS.win))
retval = RcmSmash.smashWindows();
else {
// ( ?_?)
logPrinter.print("Failed to smash the stack since your OS is not supported. Please report this issue."+
"\n\n Execution stopped and failed. And it's strange.", EMsgType.FAIL);
usbConnect.close();
logPrinter.close();
return false;
}
if (retval != 0){
logPrinter.print("Failed to smash the stack ("+retval+")"+
"\n\n Execution stopped and failed.", EMsgType.FAIL);
usbConnect.close();
logPrinter.close();
return false;
}
logPrinter.print(".:: Payload complete ::.", EMsgType.PASS);
}
usbConnect.close();
logPrinter.close();
return true;
}
/**
* Prepare the 'big' or full-size byte-buffer that is actually is a payload that we're about to use.
* @return false for issues
* true for good result
* */
private boolean preparePayload(){
File pldrFile = new File(filePath);
// 126296 b <- biggest size per CTCaer; 16384 selected randomly as minimum threshold. It's probably wrong.
if (pldrFile.length() > 126296 || pldrFile.length() < 16384) {
logPrinter.print("File size of this payload looks wired. It's "+pldrFile.length()+" bytes."+
"\n 1. Double-check that you're using the right payload." +
"\n 2. Please report this issue in case you're sure that you're doing everything right." +
"\n\n Nothing has been sent to NS. Execution stopped.", EMsgType.FAIL);
return true;
}
// Get payload file size
int pldFileSize = (int) pldrFile.length();
// Get full payload array size
int totalSize = 4328 + pldFileSize + 8640;
totalSize += 4096 - (totalSize % 4096);
if ((totalSize / 4096 % 2) == 0) // Flip buffer story to get 0x40009000 (hi) buf to always smash with 0x7000 (dec: 28672)
totalSize += 4096;
// Double-check
if (totalSize > 0x30298){
logPrinter.print("File size of the payload is too bit. Comparing to maximum size, it's greater to "+(totalSize - 0x30298)+" bytes!"+
"\n 1. Double-check that you're using the right payload." +
"\n 2. Please report this issue in case you're sure that you're doing everything right." +
"\n\n Nothing has been sent to NS. Execution stopped.", EMsgType.FAIL); // Occurs: never. I'm too lazy to check.
return true;
}
// Define holder of 'everything payload'
fullPayload = new byte[totalSize];
// Prepare array to store file payload.
byte[] dataPldFile = new byte[pldFileSize];
try{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pldrFile));
int readSize;
if ((readSize = bis.read(dataPldFile)) != pldFileSize){
logPrinter.print("Failed to retrieve data from payload file." +
"\n Got only "+readSize+" bytes while "+pldFileSize+" expected." +
"\n\n Nothing has been sent to NS. Execution stopped.", EMsgType.FAIL);
bis.close();
return true;
}
bis.close();
}
catch (Exception e){
logPrinter.print("Failed to retrieve data from payload file: " +e.getMessage()+
"\n\n Nothing has been sent to NS. Execution stopped.", EMsgType.FAIL);
return true;
}
// Trust me
System.arraycopy(initSeq, 0, fullPayload, 0, 3);
System.arraycopy(mezzo, 0, fullPayload, 680, 124);
System.arraycopy(dataPldFile, 0, fullPayload, 4328, 16384);
for (int i = 0; i < 2160; i++)
System.arraycopy(sprayPttrn, 0, fullPayload, 20712+i*4, 4);
System.arraycopy(dataPldFile, 16384, fullPayload, 29352, pldFileSize-16384);
return false;
}
/**
* Read device ID in the early beginning
* @return false if NO issues
* true if issues
* */
private boolean readUsbDeviceID(){
ByteBuffer readBuffer = ByteBuffer.allocateDirect(16);
IntBuffer readBufTransferred = IntBuffer.allocate(1);
int result = LibUsb.bulkTransfer(handler, (byte) 0x81, readBuffer, readBufTransferred, 1000);
if (result != LibUsb.SUCCESS) {
logPrinter.print("Unable to get device ID" +
"\n\n Nothing has been sent to NS. Execution stopped.", EMsgType.FAIL);
return true;
}
int trans = readBufTransferred.get();
byte[] receivedBytes = new byte[trans];
readBuffer.get(receivedBytes);
StringBuilder idStrBld = new StringBuilder("Found device with ID: ");
for (byte b: receivedBytes)
idStrBld.append(String.format("%02x ", b));
logPrinter.print(idStrBld.toString(), EMsgType.PASS);
return false;
}
/**
* Sending byte array to USB device
* @return 'false' if no issues
* 'true' if errors happened
* */
private boolean writeUsb(byte[] message){
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(4096);
writeBuffer.put(message);
IntBuffer writeBufTransferred = IntBuffer.allocate(1);
int result = LibUsb.bulkTransfer(handler, (byte) 0x01, writeBuffer, writeBufTransferred, 5050);
if (result == LibUsb.SUCCESS) {
if (writeBufTransferred.get() == 4096)
return false;
logPrinter.print("RCM Data transfer issue [write]" +
"\n Requested: " + message.length +
"\n Transferred: " + writeBufTransferred.get()+
"\n\n Execution stopped.", EMsgType.FAIL);
return true;
}
logPrinter.print("RCM Data transfer issue [write]" +
"\n Returned: " + UsbErrorCodes.getErrCode(result) +
"\n\n Execution stopped.", EMsgType.FAIL);
return true;
}
/**
* MacOS version of RcmSmash class
* */
boolean smashMacOS(){
// Release interface
int result = LibUsb.releaseInterface(handler, 1);
if (result != LibUsb.SUCCESS) {
logPrinter.print("Release interface failed" +
"\n Returned: " + result, EMsgType.FAIL);
return true;
}
logPrinter.print("Release interface 1.", EMsgType.PASS);
// Claim interface
result = LibUsb.claimInterface(handler, 0);
if (result != LibUsb.SUCCESS) {
logPrinter.print("Claim interface 0." +
"\n Returned: "+UsbErrorCodes.getErrCode(result), EMsgType.FAIL);
return true;
}
logPrinter.print("Claim interface", EMsgType.PASS);
ByteBuffer writeBuffer = ByteBuffer.allocateDirect(28672); //writeBuffer.order() equals BIG_ENDIAN; 28672
result = LibUsb.controlTransfer(handler, (byte) 0x82, LibUsb.REQUEST_GET_STATUS, (short) 0, (short) 0, writeBuffer, 1000);
if (result < 0){
logPrinter.print("Failed to smash the stack ("+UsbErrorCodes.getErrCode(result)+")"+
"\n\n Execution stopped and failed.", EMsgType.FAIL);
return true;
}
logPrinter.print("Payload complete!", EMsgType.PASS);
return false;
}
//*****************************************************************************************************************/
/*
private void writeTestFile(){
try {
File testFile = new File("/tmp/dmTests.bin");
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(testFile)
);
bos.write(fullPayload);
bos.close();
}
catch (Exception e){ e.printStackTrace(); }
// ------------------ TEST THIS p.2 ----------------------
writeUsbTest(fullPayload);
}
private boolean writeUsbTest(byte[] message){
for (int i=0; i < message.length / 0x1000 ;i++){
try {
File testFile = new File(String.format("/tmp/cnk_%02d.bin", i));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(testFile)
);
bos.write(Arrays.copyOfRange(message, i*4096, (i+1)*4096));
bos.close();
}
catch (Exception e){ e.printStackTrace(); }
}
return false;
}
*/
}