]> FriiDump Source - friidump.git/blob - libfriidump/hitachi.c
PFES-REPO-README-001: Refresh canonical documentation
[friidump.git] / libfriidump / hitachi.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Arep                                            *
3  *   Support is provided through the forums at                             *
4  *   http://wii.console-tribe.com                                          *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20  ***************************************************************************/
21
22 /*! \file
23  * \brief Memory dump functions specific to drives based on the Hitachi MN103 chip.
24  *
25  * Drives which are supported by this set of functions are, for instance, the LG GDR-8161B, GDR-8162B, GDR-8163B and GDR-8164B. All testing has been performed
26  * with the latter model, so I'm not really sure about the others, but they should work ;). Please report any issues and more compatible drives!
27  *
28  * This file contains code derived from the work of Kevin East (SeventhSon), kev@kev.nu, http://www.kev.nu/360/ , which, in turn, derives from work by
29  * a lot of other people. See his page for full details.
30  */
31
32 #include <stdio.h>
33 #include <sys/types.h>
34 #include "misc.h"
35 #include "dvd_drive.h"
36
37 /*! \brief Memory offset at which the drive cache memory is mapped.
38  *
39  * During READ, sector data is cached beginning at this address.
40  */
41
42 #define HITACHI_MEM_BASE 0x80000000
43
44
45 /**
46  * Dumps a single block (with arbitrary size) of the address space of the MN103 microcontroller within the Hitachi-LG Xbox 360 DVD drive and similar drives.
47  * This function is derived from the work of Kevin East (SeventhSon), kev@kev.nu, http://www.kev.nu/360/.
48  * @param dvd The DVD drive the command should be exectued on.
49  * @param offset The absolute memory offset to start dumping.
50  * @param block_size The block size for the dump.
51  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
52  * @return < 0 if an error occurred, 0 otherwise.
53  */
54 int hitachi_dvd_dump_memblock (dvd_drive *dvd, u_int32_t offset, u_int32_t block_size, u_int8_t *buf) {
55         mmc_command mmc;
56         int out;
57
58         if (!buf) {
59                 error ("NULL buffer");
60                 out = -1;
61         } else if (!block_size || block_size > 65535) {
62                 error ("invalid block_size (valid: 1 - 65535)");
63                 out = -2;
64         } else {
65                 dvd_init_command (&mmc, buf, block_size, NULL);
66                 mmc.cmd[0] = 0xE7; // vendor specific command (discovered by DaveX)
67                 mmc.cmd[1] = 0x48; // H
68                 mmc.cmd[2] = 0x49; // I
69                 mmc.cmd[3] = 0x54; // T
70                 mmc.cmd[4] = 0x01; // read MCU memory sub-command
71                 mmc.cmd[6] = (unsigned char) ((offset & 0xFF000000) >> 24);     // address MSB
72                 mmc.cmd[7] = (unsigned char) ((offset & 0x00FF0000) >> 16);     // address
73                 mmc.cmd[8] = (unsigned char) ((offset & 0x0000FF00) >> 8);      // address
74                 mmc.cmd[9] = (unsigned char) (offset & 0x000000FF);             // address LSB
75                 mmc.cmd[10] = (unsigned char) ((block_size & 0xFF00) >> 8);     // length MSB
76                 mmc.cmd[11] = (unsigned char) (block_size & 0x00FF);            // length LSB
77
78                 out = dvd_execute_cmd (dvd, &mmc, false);
79         }
80
81         return (out);
82 }
83
84
85 /**
86  * Dumps a given portion of the address space of the MN103 microcontroller within the Hitachi-LG Xbox 360 DVD drive.
87  * WARNING: it can take a while to dump a lot of data.
88  * @param dvd The DVD drive the command should be exectued on.
89  * @param offset The absolute memory offset to start dumping.
90  * @param block_len The number of blocks to dump.
91  * @param block_size The block size for the dump.
92  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
93  * @return < 0 if an error occurred, 0 otherwise.
94  */
95 int hitachi_dvd_dump_mem_generic (dvd_drive *dvd, u_int32_t offset, u_int32_t block_len, u_int32_t block_size, u_int8_t *buf) {
96         u_int32_t i;
97         int r, out;
98
99         if (!buf) {
100                 error ("NULL buffer");
101                 out = -1;
102         } else {
103                 r = -10;
104                 for (i = 0; i < block_len; i++) {
105                         if ((r = hitachi_dvd_dump_memblock (dvd, offset + i * block_size, block_size, buf + i * block_size)) < 0) {
106                                 error ("hitachi_dvd_read_block() failed with %d", r);
107                                 break;
108                         }
109                 }
110                 out = r;
111         }
112
113         return (out);
114 }
115
116
117 /**
118  * Dumps a given portion of the address space of the MN103 microcontroller within the Hitachi-LG Xbox 360 DVD drive, starting at the offset at which
119  * sector data are cached.
120  * WARNING: it can take a while to dump a lot of data.
121  * @param dvd The DVD drive the command should be exectued on.
122  * @param offset The memory offset to start dumping, relative to the cache start offset.
123  * @param block_len The number of blocks to dump.
124  * @param block_size The block size for the dump.
125  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
126  * @return < 0 if an error occurred, 0 otherwise.
127  */
128 int hitachi_dvd_dump_mem (dvd_drive *dvd, u_int32_t offset, u_int32_t block_len, u_int32_t block_size, u_int8_t *buf) {
129         u_int32_t i;
130         u_int32_t base;
131         int r, out;
132
133         if (!buf) {
134                 error ("NULL buffer");
135                 out = -1;
136         } else {
137                 base = dvd_get_hlds_e7_cache_base (dvd);
138                 if (base == 0)
139                         base = HITACHI_MEM_BASE;
140                 r = -10;
141                 for (i = 0; i < block_len; i++) {
142                         if ((r = hitachi_dvd_dump_memblock (dvd, base + offset + i * block_size, block_size, buf + i * block_size)) < 0) {
143                                 error ("hitachi_dvd_read_block() failed with %d", r);
144                                 break;
145                         }
146                 }
147                 out = r;
148         }
149
150         return (out);
151 }