]> FriiDump Source - friidump.git/blob - libfriidump/renesas.c
PFES-REPO-008: Import FriiDump 0.5.3.5 Project Frankenstein baseline
[friidump.git] / libfriidump / renesas.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 #include "rs.h"
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include "misc.h"
26 #include "dvd_drive.h"
27
28 /**
29  * Command found in Lite-On LH-18A1H
30  * Expected to work with: DH*, DW*, LH*, SH* series
31  * Possibly would work with other Mediatek based drives:
32  * Samsung SE*, SH* series
33  * Some Dell drives
34  * Some Sony drives
35  * Asus DRW-1814* (DRW* series?)
36  */
37
38
39 /**
40  * @param dvd The DVD drive the command should be exectued on.
41  * @param offset The absolute memory offset to start dumping.
42  * @param block_size The block size for the dump.
43  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
44  * @return < 0 if an error occurred, 0 otherwise.
45  */
46 int renesas_dvd_dump_memblock (dvd_drive *dvd, u_int32_t offset, u_int32_t block_size, u_int8_t *buf) {
47         mmc_command mmc;
48         int out;
49
50         u_int32_t src_offset;
51         u_int32_t sec_nr;
52         u_int32_t sec_cnt;
53         u_int32_t first_sec_nr;
54
55         if (!buf) {
56                 error ("NULL buffer");
57                 out = -1;
58         } else if (!block_size || block_size > 65535) {
59                 error ("invalid block_size (valid: 1 - 65535)");
60                 out = -2;
61         } else {
62                 dvd_init_command (&mmc, buf, block_size, NULL);
63                 mmc.cmd[0] = 0x3C;
64                 mmc.cmd[1] = 0x05;
65                 mmc.cmd[2]  = (unsigned char) ((offset & 0xFF000000) >> 24);    // address MSB
66                 mmc.cmd[3]  = (unsigned char) ((offset & 0x00FF0000) >> 16);    // address MSB
67                 mmc.cmd[4]  = (unsigned char) ((offset & 0x0000FF00) >> 8);     // address
68                 mmc.cmd[5]  = (unsigned char) ( offset & 0x000000FF);           // address LSB
69 //              mmc.cmd[6]  = (unsigned char) ((block_size & 0x00FF0000) >> 16);// length  MSB
70                 mmc.cmd[6]  = 0;
71                 mmc.cmd[7]  = (unsigned char) ((block_size & 0x0000FF00) >> 8); // length
72                 mmc.cmd[8]  = (unsigned char) ( block_size & 0x000000FF);       // length  LSB
73                 mmc.cmd[9]  = 0x44;
74
75                 out = dvd_execute_cmd (dvd, &mmc, false);
76
77                 src_offset   = 0;
78                 first_sec_nr = 0x55555555;
79                 sec_cnt      = 0;
80                 while (src_offset < block_size) {
81                         sec_nr=(*(buf+src_offset+1)<<16)+(*(buf+src_offset+2)<<8)+(*(buf+src_offset+3));
82                         if (first_sec_nr==0x55555555) {
83                                 first_sec_nr=sec_nr;
84                         }
85                         else
86                                 if (sec_nr!=first_sec_nr+sec_cnt) {
87                                         error ("sector sequence broken");
88                                         out = -3;
89                                         *(buf+src_offset+0)=0xff;
90                                         *(buf+src_offset+1)=0xff;
91                                         *(buf+src_offset+2)=0xff;
92                                         *(buf+src_offset+3)=0xff;
93                                         break;
94                                 }
95                         src_offset += 2064;
96                         sec_cnt += 1;
97                 }
98         }
99         return (out);
100 }
101
102
103
104 /**
105  * WARNING: it can take a while to dump a lot of data.
106  * @param dvd The DVD drive the command should be exectued on.
107  * @param offset The memory offset to start dumping, relative to the cache start offset.
108  * @param block_len The number of blocks to dump.
109  * @param block_size The block size for the dump.
110  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
111  * @return < 0 if an error occurred, 0 otherwise.
112  */
113 int renesas_dvd_dump_mem (dvd_drive *dvd, u_int32_t offset, u_int32_t block_len, u_int32_t block_size, u_int8_t *buf) {
114         u_int32_t i;
115         int r, out;
116
117         if (!buf) {
118                 error ("NULL buffer");
119                 out = -1;
120         } else {
121                 r = -10;
122                 for (i = 0; i < block_len; i++) {
123                         if ((r = renesas_dvd_dump_memblock (dvd, offset + i * block_size, block_size, buf + i * block_size)) < 0) {
124                                 error ("renesas_dvd_dump_memblock() failed with %d", r);
125                                 break;
126                         }
127                 }
128                 out = r;
129         }
130
131         return (out);
132 }