⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hdl_dump.cpp

📁 PS2游戏硬盘直灌(HDL)的Windows下VC的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
 * hdl_dump.c
 * $Id: hdl_dump.c,v 1.14 2005/02/17 17:50:25 b081 Exp $
 *
 * Copyright 2004 Bobi B., w1zard0f07@yahoo.com
 *
 * This file is part of hdl_dump.
 *
 * hdl_dump is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * hdl_dump is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with hdl_dump; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#if defined (_BUILD_WIN32)
#  include <windows.h>
#  include <stdlib.h>
#  include <stdio.h>
#  include <conio.h>
#endif
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "byteseq.h"
#include "retcodes.h"
#include "osal.h"
#include "apa.h"
#include "common.h"
#include "progress.h"
#include "hdl.h"
#include "isofs.h"
#include "iin.h"
#include "aligned.h"
#include "hio_probe.h"
#include "net_io.h"
#if defined (_WITH_ASPI)
#  include "aspi_hlio.h"
#endif

#define INCLUDE_DUMP_CMD
#define INCLUDE_COMPARE_CMD
#define INCLUDE_COMPARE_IIN_CMD
#define INCLUDE_MAP_CMD
#define INCLUDE_ZERO_CMD
#define INCLUDE_CUTOUT_CMD
#define INCLUDE_INFO_CMD
#define INCLUDE_READ_TEST_CMD

/* command names */
#define CMD_QUERY "query"
#if defined (INCLUDE_DUMP_CMD)
#  define CMD_DUMP "dump"
#endif
#if defined (INCLUDE_COMPARE_CMD)
#  define CMD_COMPARE "compare"
#endif
#if defined (INCLUDE_COMPARE_IIN_CMD)
#  define CMD_COMPARE_IIN "compare_iin"
#endif
#define CMD_TOC "toc"
#define CMD_HDL_TOC "hdl_toc"
#if defined (INCLUDE_MAP_CMD)
#  define CMD_MAP "map"
#endif
#define CMD_DELETE "delete"
#if defined (INCLUDE_ZERO_CMD)
#  define CMD_ZERO "zero"
#endif
#if defined (INCLUDE_CUTOUT_CMD)
#  define CMD_CUTOUT "cutout"
#endif
#if defined (INCLUDE_INFO_CMD)
#  define CMD_HDL_INFO "info"
#endif
#define CMD_HDL_EXTRACT "extract"
#define CMD_HDL_INJECT_CD "inject_cd"
#define CMD_HDL_INJECT_DVD "inject_dvd"
#define CMD_CDVD_INFO "cdvd_info"
#if defined (INCLUDE_READ_TEST_CMD)
#  define CMD_READ_TEST "read_test"
#endif
#define CMD_POWER_OFF "poweroff"
#if defined (INCLUDE_CHECK_CMD)
#  define CMD_CHECK "check"
#endif


/**************************************************************/
static void
show_apa_toc (const apa_partition_table_t *table)
{
  u_int32_t i;

  for (i=0; i<table->part_count; ++i)
    {
      const ps2_partition_header_t *part = &table->parts [i].header;
	  
      fprintf (stdout, "%06lx00%c%c %5luMB ",
	       (unsigned long) (get_u32 (&part->start) >> 8),
	       table->parts [i].existing ? '.' : '*',
	       table->parts [i].modified ? '*' : ':',
	       (unsigned long) (get_u32 (&part->length) / 2048));
      if (get_u32 (&part->main) == 0)
	fprintf (stdout, "%4x [%-*s]\n",
		 get_u16 (&part->type), PS2_PART_IDMAX, part->id);
      else
	fprintf (stdout, "      part # %2lu in %06lx00\n",
		 (unsigned long) (get_u32 (&part->number)),
		 (unsigned long) (get_u32 (&part->main) >> 8));
    }

  fprintf (stdout, "Total device size: %uMB, used: %uMB, available: %uMB\n",
	   (unsigned int) table->device_size_in_mb,
	   (unsigned int) (table->allocated_chunks * 128),
	   (unsigned int) (table->free_chunks * 128));
}


/**************************************************************/
#if defined (INCLUDE_MAP_CMD)
static void
show_apa_map (const apa_partition_table_t *table)
{
  /* show device map */
  const char *map = table->chunks_map;
  const u_int32_t GIGS_PER_ROW = 8;
  u_int32_t i, count = 0;

  for (i=0; i<table->total_chunks; ++i)
    {
      if (count == 0)
	fprintf (stdout, "%3uGB: ",
		 (unsigned int) ((i / ((GIGS_PER_ROW * 1024) / 128)) *
				 GIGS_PER_ROW));

      fputc (map [i], stdout);
      if ((count & 0x07) == 0x07)
	fputc (' ', stdout);

      if (++count == ((GIGS_PER_ROW * 1024) / 128)) /* 8G on each row */
	{
	  fputc ('\n', stdout);
	  count = 0;
	}
    }

  fprintf (stdout, "\nTotal device size: %uMB, used: %uMB, available: %uMB\n",
	   (unsigned int) table->device_size_in_mb,
	   (unsigned int) (table->allocated_chunks * 128),
	   (unsigned int) (table->free_chunks * 128));
}
#endif /* INCLUDE_MAP_CMD defined? */


/**************************************************************/
static int
show_toc (const char *device_name)
{
  apa_partition_table_t *table;
  int result = apa_ptable_read (device_name, &table);
  if (result == RET_OK)
    {
      show_apa_toc (table);
      apa_ptable_free (table);
    }
  return (result);
}


/**************************************************************/
static int
show_hdl_toc (const char *device_name)
{
  hio_t *hio;
  int result = hio_probe (device_name, &hio);
  if (result == RET_OK)
    {
      hdl_games_list_t *glist;
      result = hdl_glist_read (hio, &glist);
      if (result == RET_OK)
	{
	  u_int32_t i;
	  printf ("%-4s%9s %-5s %-12s %s\n",
		  "type", "size", "flags", "startup", "name");
	  for (i=0; i<glist->count; ++i)
	    {
	      const hdl_game_info_t *game = glist->games + i;
	      char compat_flags [6 + 1] = { "\0\0\0\0\0\0" };
	      if (game->compat_flags & 0x01)
		strcat (compat_flags, "+1");
	      if (game->compat_flags & 0x02)
		strcat (compat_flags, "+2");
	      if (game->compat_flags & 0x04)
		strcat (compat_flags, "+3");
	      printf ("%3s %7luKB %5s %-12s %s\n",
		      game->is_dvd ? "DVD" : "CD ",
		      game->total_size_in_kb,
		      compat_flags + 1, /* trim leading + */
		      game->startup,
		      game->name);
	    }
	  printf ("total %uMB, used %uMB, available %uMB\n",
		  (unsigned int) (glist->total_chunks * 128),
		  (unsigned int) ((glist->total_chunks -
				   glist->free_chunks) * 128),
		  (unsigned int) (glist->free_chunks * 128));
	  hdl_glist_free (glist);
	}
      hio->close (hio);
    }
  return (result);
}

/**************************************************************/
#if defined (INCLUDE_MAP_CMD)
static int
show_map (const char *device_name)
{
  apa_partition_table_t *table;
  int result = apa_ptable_read (device_name, &table);
  if (result == RET_OK)
    {
      show_apa_map (table);
      apa_ptable_free (table);
    }
  return (result);
}
#endif /* INCLUDE_MAP_CMD defined? */


/**************************************************************/
#if defined (INCLUDE_INFO_CMD)
static int
show_hdl_game_info (const char *device_name,
		    const char *game_name)
{
  hio_t *hio;
  int result = hio_probe (device_name, &hio);
  if (result == RET_OK)
    {
      apa_partition_table_t *table;
      result = apa_ptable_read_ex (hio, &table);
      if (result == RET_OK)
	{
	  u_int32_t partition_index;
	  result = apa_find_partition (table, game_name, &partition_index);
	  if (result == RET_NOT_FOUND)
	    { /* use heuristics - look among the HD Loader partitions */
	      char tmp [100];
	      strcpy (tmp, "PP.HDL.");
	      strcat (tmp, game_name);
	      result = apa_find_partition (table, tmp, &partition_index);
	    }

	  if (result == RET_OK)
	    { /* partition found */
	      const u_int32_t PART_SYSDATA_SIZE = 4 * 1024 * 1024;
	      unsigned char *buffer = (unsigned char *)osal_alloc (PART_SYSDATA_SIZE);
	      if (buffer != NULL)
		{
		  u_int32_t len;
		  result = hio->read (hio,
				      get_u32 (&table->parts [partition_index].header.start),
				      (4 _MB) / HDD_SECTOR_SIZE, buffer, &len);
		  if (result == OSAL_OK)
		    {
		      const char *signature = (char*) buffer + 0x001010ac;
		      const char *hdl_name = (char*) buffer + 0x00101008;
		      u_int32_t type = buffer [0x001010ec];
		      u_int32_t num_parts = buffer [0x001010f0];
		      const u_int32_t *data = (u_int32_t*) (buffer + 0x001010f5);
		      u_int32_t i;

		      if (buffer [0x00101000] == 0xed &&
			  buffer [0x00101001] == 0xfe &&
			  buffer [0x00101002] == 0xad &&
			  buffer [0x00101003] == 0xde)
			{ /* 0xdeadfeed magic found */
			  u_int64_t total_size = 0;

#if 0
			  /* save main partition incomplete header or debug purposes */
			  write_file (signature, buffer, 0x00101200);
#endif

			  fprintf (stdout, "%s: [%s], %s\n",
				   signature, hdl_name, (type == 0x14 ? "DVD" : "CD"));
			  if (buffer [0x1010a8] != 0)
			    {
			      char compat_flags [6 + 1] = { "" };
			      if (buffer [0x1010a8] & 0x01)
				strcat (compat_flags, "+1");
			      if (buffer [0x1010a8] & 0x02)
				strcat (compat_flags, "+2");
			      if (buffer [0x1010a8] & 0x04)
				strcat (compat_flags, "+3");
			      fprintf (stdout, "compatibility flags: %s\n",
				       compat_flags + 1);
			    }
			  for (i=0; i<num_parts; ++i)
			    {
			      unsigned long start = get_u32 (data + (i * 3 + 1));
			      unsigned long length = get_u32 (data + (i * 3 + 2));
			      total_size += ((u_int64_t) length) << 8;
			      fprintf (stdout,
				       "\tpart %2u is from sector 0x%06lx00, "
				       "%7luKB long\n",
				       (unsigned int) (i + 1),
				       start, length / 4);
			    }
			  fprintf (stdout,
				   "Total size: %luKB (%luMB approx.)\n",
				   (unsigned long) (total_size / 1024),
				   (unsigned long) (total_size / (1024 * 1024)));
			}
		      else
			result = RET_NOT_HDL_PART;
		    }
		  osal_free (buffer);
		}
	      else
		result = RET_NO_MEM;
	    }
	  apa_ptable_free (table);
	}
      hio->close (hio);
    }
  return (result);
}
#endif /* INCLUDE_INFO_CMD defined? */


/**************************************************************/
#if defined (INCLUDE_CUTOUT_CMD)
static int
show_apa_cut_out_for_inject (const char *device_name,
			     u_int32_t size_in_mb)
{
  apa_partition_table_t *table;
  int result = apa_ptable_read (device_name, &table);
  if (result == RET_OK)
    {
      u_int32_t new_partition_index;
      result = apa_allocate_space (table,
				   "<new partition>",
				   size_in_mb,
				   &new_partition_index,
				   0);
      if (result == RET_OK)
	{
	  show_apa_toc (table);
	  show_apa_map (table);
	}

      apa_ptable_free (table);
    }
  return (result);
}
#endif /* INCLUDE_CUTOUT_CMD defined? */


/**************************************************************/
static int
delete_partition (const char *device_name,
		  const char *name)
{
  apa_partition_table_t *table;
  int result = apa_ptable_read (device_name, &table);
  if (result == RET_OK)
    {
      result = apa_delete_partition (table, name);

      if (result == RET_OK)
	result = apa_commit (device_name, table);

      apa_ptable_free (table);
    }
  return (result);
}


/**************************************************************/
#if defined (INCLUDE_COMPARE_CMD)
static int
compare (const char *file_name_1,
	 int file_name_1_is_device,
	 const char *file_name_2,
	 int file_name_2_is_device)
{
  osal_handle_t file1 = OSAL_HANDLE_INIT, file2 = OSAL_HANDLE_INIT;
  int different = 0;
  int result = osal_open (file_name_1, &file1, 1);
  if (result == OSAL_OK)
    {
      result = osal_open (file_name_2, &file2, 1);
      if (result == OSAL_OK)
	{
	  const u_int32_t BUFF_SIZE = 4 * 1024 * 1024;
	  char *buffer = (char *)osal_alloc (BUFF_SIZE);
	  if (buffer != NULL)
	    {
	      u_int32_t read1 = 0, read2 = 0;
	      do
		{
		  result = osal_read (file1, buffer + 0 * BUFF_SIZE / 2,
				      BUFF_SIZE / 2, &read1);
		  if (result == OSAL_OK)
		    {
		      result = osal_read (file2, buffer + 1 * BUFF_SIZE / 2,
					  BUFF_SIZE / 2, &read2);
		      if (result == OSAL_OK)
			{
			  different = read1 != read2;
			  if (!different)
			    {
			      u_int32_t i;
			      const char *p1 = buffer + 0 * BUFF_SIZE / 2;
			      const char *p2 = buffer + 1 * BUFF_SIZE / 2;
			      for (i=0; !different && i<read1; ++i)
				different = *p1++ != *p2++;
			    }
			}
		    }
		}
	      while (result == OSAL_OK && read1 > 0 && read2 > 0 && !different);
	    }
	  else
	    result = RET_NO_MEM;
	  osal_close (file2);
	}
      osal_close (file1);
    }
  return (result == RET_OK ? (!different ? RET_OK : RET_DIFFERENT) : result);
}
#endif /* INCLUDE_COMPARE_CMD defined? */


/**************************************************************/
#if defined (INCLUDE_ZERO_CMD)
static int
zero_device (const char *device_name)
{
  osal_handle_t device;
  int result = osal_open_device_for_writing (device_name, &device);
  if (result == OSAL_OK)
    {
      void *buffer = osal_alloc (1 _MB);
      if (buffer != NULL)
	{
	  u_int32_t bytes;
	  memset (buffer, 0, 1 _MB);
	  do
	    {
	      result = osal_write (device, buffer, 1 _MB, &bytes);
	    }
	  while (result == OSAL_OK && bytes > 0);
	}
      else
	result = RET_NO_MEM;
      osal_close (device);
    }
  return (result);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -