]> FriiDump Source - friidump.git/blob - libfriidump/lite-on.c
PFES-REPO-008: Import FriiDump 0.5.3.5 Project Frankenstein baseline
[friidump.git] / libfriidump / lite-on.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; verified with LH-18A1P and LH-20A1H
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 //u_int8_t  tmp[64*1024];
39
40 /**
41  * @param dvd The DVD drive the command should be exectued on.
42  * @param offset The absolute memory offset to start dumping.
43  * @param block_size The block size for the dump.
44  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
45  * @return < 0 if an error occurred, 0 otherwise.
46  */
47 int liteon_dvd_dump_memblock (dvd_drive *dvd, u_int32_t offset, u_int32_t block_size, u_int8_t *buf) {
48         mmc_command mmc;
49         int out;
50         u_int32_t raw_block_size;
51         u_int32_t raw_offset;
52
53         u_int32_t src_offset;
54         u_int32_t dst_offset;
55         u_int32_t row_nr;
56         u_int32_t sec_nr;
57         u_int32_t sec_cnt;
58         u_int32_t first_sec_nr;
59         u_int8_t  tmp[64*1024];
60         //u_int8_t  *tmp;
61
62         raw_block_size = (block_size / 2064) * 0x950;
63         raw_offset = (offset / 2064) * 0x950;
64
65         if (!buf) {
66                 error ("NULL buffer");
67                 out = -1;
68         } else if (!block_size || raw_block_size >= 65535) {
69                 error ("invalid raw_block_size (valid: 1 - 65534)");
70                 error ("raw_block_size = (block_size / 2064) * 2384");
71                 out = -2;
72         } else {
73                 //tmp = malloc(64*1024);
74                 dvd_init_command (&mmc, tmp, raw_block_size, NULL); //64*1024
75                 mmc.cmd[0]  = 0x3C; // READ BUFFER
76                 mmc.cmd[1]  = 0x01; // Vendor specific - sole parameter supported by Lite-On
77                 mmc.cmd[2]  = 0x01; // == 0x02; 0xE2 = EEPROM; 0xF1 = KEYPARA;
78                 mmc.cmd[3]  = (unsigned char) ((raw_offset & 0x00FF0000) >> 16);// address MSB
79                 mmc.cmd[4]  = (unsigned char) ((raw_offset & 0x0000FF00) >> 8); // address
80                 mmc.cmd[5]  = (unsigned char) ( raw_offset & 0x000000FF);       // address LSB
81                 mmc.cmd[6]  = (unsigned char) ((raw_block_size & 0x00FF0000) >> 16);    // length  MSB
82                 mmc.cmd[7]  = (unsigned char) ((raw_block_size & 0x0000FF00) >> 8);     // length
83                 mmc.cmd[8]  = (unsigned char) ( raw_block_size & 0x000000FF);           // length  LSB
84
85                 out = dvd_execute_cmd (dvd, &mmc, false);
86
87                 src_offset   = 0;
88                 dst_offset   = 0;
89                 first_sec_nr = 0x55555555;
90                 sec_cnt      = 0;
91                 while (src_offset < raw_block_size) {
92                         sec_nr=(*(tmp+src_offset+1)<<16)+(*(tmp+src_offset+2)<<8)+(*(tmp+src_offset+3));
93 //fprintf (stdout,"sec_nr: %x\n", sec_nr);
94                         if (first_sec_nr==0x55555555) {
95                                 first_sec_nr=sec_nr;
96                                 rs_decode(tmp+src_offset, 0, 0);
97                         }
98                         else
99                                 if (sec_nr==first_sec_nr+sec_cnt) {
100                                         rs_decode(tmp+src_offset, 0, 0); //sector seq = ok
101                                 }
102                                 else { //sector seq broken -> corrupt
103                                         error ("sector sequence broken");
104                                         out = -3;
105                                         *(tmp+src_offset+0)=0xff;
106                                         *(tmp+src_offset+1)=0xff;
107                                         *(tmp+src_offset+2)=0xff;
108                                         *(tmp+src_offset+3)=0xff;
109                                         break;
110                                 }
111                         for (row_nr=0; row_nr<12; row_nr++) {
112                                 memcpy(buf+dst_offset, tmp+src_offset, 172);
113                                 dst_offset += 172;
114                                 src_offset += 182;
115                         }
116                         src_offset += 200;
117                         sec_cnt += 1;
118                 }
119                 //free(tmp);
120         }
121         return (out);
122 }
123
124
125
126 /**
127  * WARNING: it can take a while to dump a lot of data.
128  * @param dvd The DVD drive the command should be exectued on.
129  * @param offset The memory offset to start dumping, relative to the cache start offset.
130  * @param block_len The number of blocks to dump.
131  * @param block_size The block size for the dump.
132  * @param buf Where to place the dumped data. This must have been setup by the caller to store up to block_size bytes.
133  * @return < 0 if an error occurred, 0 otherwise.
134  */
135 int liteon_dvd_dump_mem (dvd_drive *dvd, u_int32_t offset, u_int32_t block_len, u_int32_t block_size, u_int8_t *buf) {
136         u_int32_t i;
137         int r, out;
138
139         if (!buf) {
140                 error ("NULL buffer");
141                 out = -1;
142         } else {
143                 r = -10;
144                 for (i = 0; i < block_len; i++) {
145                         if ((r = liteon_dvd_dump_memblock (dvd, offset + i * block_size, block_size, buf + i * block_size)) < 0) {
146                                 error ("liteon_dvd_dump_memblock() failed with %d", r);
147                                 break;
148                         }
149                 }
150                 out = r;
151         }
152
153         return (out);
154 }