/*## (c) SECURITY EXPLORATIONS    2018 poland                                #*/
/*##     http://www.security-explorations.com                                #*/

/* THIS SOFTWARE IS PROTECTED BY DOMESTIC AND INTERNATIONAL COPYRIGHT LAWS    */
/* UNAUTHORISED COPYING OF THIS SOFTWARE IN EITHER SOURCE OR BINARY FORM IS   */
/* EXPRESSLY FORBIDDEN. ANY USE, INCLUDING THE REPRODUCTION, MODIFICATION,    */
/* DISTRIBUTION, TRANSMISSION, RE-PUBLICATION, STORAGE OR DISPLAY OF ANY      */
/* PART OF THE SOFTWARE, FOR COMMERCIAL OR ANY OTHER PURPOSES REQUIRES A      */
/* VALID LICENSE FROM THE COPYRIGHT HOLDER.                                   */

/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS    */
/* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,*/
/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    */
/* SECURITY EXPLORATIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, */
/* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  */
/* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE     */
/* SOFTWARE.                                                                  */

import java.lang.*;
import java.util.*;
import java.io.*;

public class Config {
 public static final int NBOX_HDTV         = 1;
 public static final int NBOX_RECORDER     = 2;
 public static final int NBOX_2850ST       = 3;
 public static final int NBOX_2849ST       = 4;
 public static final int NBOX_2851S        = 5;

 public static int BOXTYPE;

 static {
  switch(SysInfo.hwid) {
   case SysInfoDefs.ITI5800S:
    BOXTYPE=NBOX_HDTV;
    break;

   case SysInfoDefs.ITI5800SX:
    BOXTYPE=NBOX_RECORDER;
    break;

   case SysInfoDefs.ITI2850ST:
    if (SysInfo.serial_num().startsWith("DFKB")) {
     BOXTYPE=NBOX_2849ST;
    } else {
     BOXTYPE=NBOX_2850ST;
    }
    break;

   case SysInfoDefs.ITI2851S:
    BOXTYPE=NBOX_2851S;
    break;
  }

  try {
   new File(ConfigDefs.DATA_DIR).mkdirs();
  } catch(Throwable t) {}
 }

 public static boolean isCARBO(int hwid) {
  return ((hwid==SysInfoDefs.ITI2850ST)||(hwid==SysInfoDefs.ITI2851S));
 }

 public static boolean isHERMES(int hwid) {
  return ((hwid==SysInfoDefs.ITI5800S)||(hwid==SysInfoDefs.ITI5800SX));
 }

 public static boolean isCARBO() {
  return ((BOXTYPE==NBOX_2850ST)||(BOXTYPE==NBOX_2849ST)||(BOXTYPE==NBOX_2851S));
 }

 public static boolean isHERMES() {
  return ((BOXTYPE==NBOX_HDTV)||(BOXTYPE==NBOX_RECORDER));
 }

 public static int syscall_table_addr() {
  int addr=-1;

  switch(Config.BOXTYPE) {
   case Config.NBOX_HDTV:
    addr=0x84599DA4;
    break;   
   case Config.NBOX_RECORDER:
    addr=0x84599DB0;
    break;   
   case Config.NBOX_2849ST:
   case Config.NBOX_2850ST:
    addr=0x80999704;
    break;
  }

  return addr;
 }

 public static int rootpipe_readfd_addr() {
  int addr=-1;

  switch(Config.BOXTYPE) {
   case Config.NBOX_HDTV:
    addr=0x014EF238;
    break;   
   case Config.NBOX_RECORDER:
    addr=0x0151D244;
    break;   
  }

  return addr;
 }

 public static int rootpipe_writefd_addr() {
  int addr=-1;

  switch(Config.BOXTYPE) {
   case Config.NBOX_HDTV:
    addr=0x014EF23C;
    break;   
   case Config.NBOX_RECORDER:
    addr=0x0151D248;
    break;   
  }

  return addr;
 }

 public static int smartcard_fd_addr() {
  int addr=-1;

  switch(Config.BOXTYPE) {
   case Config.NBOX_HDTV:
    addr=0x015223ec;
    break;   
   case Config.NBOX_RECORDER:
    addr=0x01578ac4;
    break;   
  }

  addr=Memory.read_dword(addr)+4;

  return addr;
 }
}
