]> FriiDump Source - friidump.git/blob - libfriidump/dumper.c
FriiDump 0.5.3.10: add Redump evidence and optimize Xbox flow
[friidump.git] / libfriidump / dumper.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Arep                                            *
3  *   Support is provided through the forums at                             *
4  *   http://www.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 "misc.h"
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <multihash.h>
28 #include "constants.h"
29 #include "disc.h"
30 #include "dumper.h"
31 #include "xbox_ref_bridge.h"
32 #include "xbox_ref/xbox_ref_log.h"
33
34 #ifndef WIN32
35 #include <unistd.h>
36 #include <sys/types.h>
37 #endif
38
39 struct dumper_s {
40         disc *dsk;
41         char *outfile_raw;
42         u_int32_t start_sector_raw;
43         FILE *fp_raw;
44         char *outfile_iso;
45         u_int32_t start_sector_iso;
46         FILE *fp_iso;
47         char *outfile_xiso;
48         u_int32_t start_sector_xiso;
49         FILE *fp_xiso;
50         u_int32_t start_sector;
51         bool hashing;
52         bool flushing;
53
54         multihash hash_raw;
55         multihash hash_iso;
56         multihash hash_xiso;
57         xbox_ref_dump_result xbox_ref_result;
58         
59         progress_func progress;
60         void *progress_data;
61 };
62
63 #define DUMPER_READ_BLOCK_RETRIES 3
64
65 static bool dumper_read_sector_with_retry (dumper *dmp, u_int32_t sector_no, u_int8_t **isobuf, u_int8_t **rawbuf) {
66         int attempt;
67         bool need_iso, need_raw;
68
69         if (!dmp || !dmp -> dsk)
70                 return false;
71
72         need_iso = dmp -> fp_iso != NULL;
73         need_raw = dmp -> fp_raw != NULL;
74
75         for (attempt = 0; attempt < DUMPER_READ_BLOCK_RETRIES; attempt++) {
76                 if (isobuf) *isobuf = NULL;
77                 if (rawbuf) *rawbuf = NULL;
78
79                 disc_read_sector (dmp -> dsk, sector_no, isobuf, rawbuf);
80                 if ((!need_iso || (isobuf && *isobuf)) && (!need_raw || (rawbuf && *rawbuf))) {
81                         if (attempt > 0)
82                                 warning ("Recovered sectors %u..%u after dump-level retry %d", sector_no, sector_no + SECTORS_PER_BLOCK - 1, attempt);
83                         return true;
84                 }
85
86                 if (attempt + 1 < DUMPER_READ_BLOCK_RETRIES)
87                         warning ("Dump-level read retry %d/%d for sectors %u..%u", attempt + 1, DUMPER_READ_BLOCK_RETRIES - 1, sector_no, sector_no + SECTORS_PER_BLOCK - 1);
88         }
89
90         return false;
91 }
92
93 #define XBOX_XISO_LEADIN_SECTORS 32
94 #define XBOX_XISO_STANDARD_GAME_LBA 32
95 #define XBOX_XISO_START_LBA_MAGIC 306112
96 #define XBOX_XISO_DUAL_LAYER_END_LBA 1913920
97
98 /* DiscImageCreator-compatible Original Xbox/XGD1 2048-byte-sector layout
99  * constants. DIC uses XBOX_SIZE=3820880, XBOX_LAYER_BREAK=1913776,
100  * DVD start PSN 0x30000, and Xbox start PSN 0x60600. The virtual redump
101  * layout below follows those constants: video L0, middle/padding, game
102  * partition, middle/padding, and video L1 tail.
103  */
104 #define XBOX_XGD1_DIC_DVD_START_PSN 0x30000
105 #define XBOX_XGD1_DIC_XBOX_START_PSN 0x60600
106 #define XBOX_XGD1_DIC_XBOX_SIZE 3820880
107 #define XBOX_XGD1_DIC_LAYER_BREAK 1913776
108 #define XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS 3431264
109 #define XBOX_XGD1_FULL_REDUMP_SECTORS XBOX_XGD1_DIC_XBOX_SIZE
110 #define XBOX_XGD1_GAME_OUTPUT_START_LBA (XBOX_XGD1_DIC_XBOX_START_PSN - XBOX_XGD1_DIC_DVD_START_PSN)
111 #define XBOX_XGD1_VIDEO_L0_SECTORS 6832
112 #define XBOX_XGD1_VIDEO_L1_SECTORS 160
113 #define XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA (XBOX_XGD1_FULL_REDUMP_SECTORS - XBOX_XGD1_VIDEO_L1_SECTORS)
114 #define XBOX_XGD1_GAME_SOURCE_SECTORS (XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS - XBOX_XISO_LEADIN_SECTORS)
115 #define XBOX_XGD1_LAYER_THRESHOLD 2000000
116 #define XBOX_METADATA_PATH_MAX 4096
117
118 static u_int32_t read_le32 (const u_int8_t *p) {
119         return ((u_int32_t) p[0]) | ((u_int32_t) p[1] << 8) | ((u_int32_t) p[2] << 16) | ((u_int32_t) p[3] << 24);
120 }
121
122 static bool xbox_probe_xdfs_volume (disc *d, u_int32_t lba, u_int32_t *root_lba, u_int32_t *volume_size_raw) {
123         u_int8_t sector[SECTOR_SIZE];
124
125         memset (sector, 0, sizeof (sector));
126         if (disc_xbox_read_10 (d, lba, 1, sector, sizeof (sector)) < 0)
127                 return false;
128
129         if (memcmp (sector, "MICROSOFT", 9) != 0)
130                 return false;
131
132         if (root_lba)
133                 *root_lba = read_le32 (sector + 0x14);
134         if (volume_size_raw)
135                 *volume_size_raw = read_le32 (sector + 0x1C);
136
137         return true;
138 }
139
140 static bool xbox_redump_layout_constants_match_dic (void) {
141         return XBOX_XGD1_FULL_REDUMP_SECTORS == XBOX_XGD1_DIC_XBOX_SIZE &&
142                XBOX_XGD1_DIC_LAYER_BREAK == 1913776 &&
143                XBOX_XGD1_GAME_OUTPUT_START_LBA == 0x30600 &&
144                XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA == 3820720 &&
145                XBOX_XGD1_VIDEO_L0_SECTORS +
146                (XBOX_XGD1_GAME_OUTPUT_START_LBA - XBOX_XGD1_VIDEO_L0_SECTORS) +
147                XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS +
148                (XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA -
149                 (XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS)) +
150                XBOX_XGD1_VIDEO_L1_SECTORS == XBOX_XGD1_FULL_REDUMP_SECTORS;
151 }
152
153
154 typedef struct {
155         bool has_pfi;
156         bool has_dmi;
157         bool pfi_written;
158         bool dmi_written;
159         u_int8_t pfi[2048];
160         u_int8_t dmi[2048];
161         u_int32_t locked_visible_sectors;
162         u_int32_t unlocked_visible_sectors;
163         u_int32_t output_sectors;
164         u_int32_t game_source_lba;
165         u_int32_t game_source_sectors;
166         u_int32_t game_leadin_read_sectors;
167         u_int32_t game_leadin_zero_sectors;
168         char pfi_path[XBOX_METADATA_PATH_MAX];
169         char dmi_path[XBOX_METADATA_PATH_MAX];
170         char json_path[XBOX_METADATA_PATH_MAX];
171 } xbox_redump_metadata;
172
173 static void xbox_make_related_path (const char *filename, const char *suffix, char *out, size_t out_size) {
174         const char *slash1, *slash2, *slash, *dot;
175         size_t suffix_len, base_len;
176
177         if (!out || out_size == 0)
178                 return;
179         out[0] = '\0';
180         if (!filename || !suffix)
181                 return;
182
183         suffix_len = strlen (suffix);
184         if (out_size <= suffix_len + 1)
185                 return;
186
187         slash1 = strrchr (filename, '/');
188         slash2 = strrchr (filename, '\\');
189         if (slash1 && slash2)
190                 slash = slash1 > slash2 ? slash1 : slash2;
191         else
192                 slash = slash1 ? slash1 : slash2;
193
194         dot = strrchr (filename, '.');
195         if (!dot || (slash && dot < slash))
196                 dot = filename + strlen (filename);
197
198         base_len = (size_t) (dot - filename);
199         if (base_len + suffix_len + 1 > out_size)
200                 base_len = out_size - suffix_len - 1;
201         memcpy (out, filename, base_len);
202         out[base_len] = '\0';
203         strncat (out, suffix, out_size - strlen (out) - 1);
204 }
205
206 static const char *xbox_path_leaf (const char *path) {
207         const char *slash1, *slash2;
208         if (!path)
209                 return "";
210         slash1 = strrchr (path, '/');
211         slash2 = strrchr (path, '\\');
212         if (slash1 && slash2)
213                 return (slash1 > slash2 ? slash1 : slash2) + 1;
214         if (slash1)
215                 return slash1 + 1;
216         if (slash2)
217                 return slash2 + 1;
218         return path;
219 }
220
221 static void xbox_json_string (FILE *f, const char *s) {
222         fputc ('"', f);
223         if (s) {
224                 while (*s) {
225                         unsigned char c = (unsigned char) *s++;
226                         if (c == '"' || c == '\\') {
227                                 fputc ('\\', f);
228                                 fputc (c, f);
229                         } else if (c == '\n') {
230                                 fputs ("\\n", f);
231                         } else if (c == '\r') {
232                                 fputs ("\\r", f);
233                         } else if (c == '\t') {
234                                 fputs ("\\t", f);
235                         } else if (c < 0x20) {
236                                 fprintf (f, "\\u%04x", c);
237                         } else {
238                                 fputc (c, f);
239                         }
240                 }
241         }
242         fputc ('"', f);
243 }
244
245 static bool xbox_write_binary_file (const char *path, const u_int8_t *data, size_t len) {
246         FILE *f;
247         if (!path || !data || len == 0)
248                 return false;
249         f = fopen (path, "wb");
250         if (!f)
251                 return false;
252         if (fwrite (data, 1, len, f) != len) {
253                 fclose (f);
254                 return false;
255         }
256         return fclose (f) == 0;
257 }
258
259 static void xbox_capture_pfi_dmi (disc *d, xbox_redump_metadata *meta) {
260         if (!d || !meta)
261                 return;
262         if (!meta -> has_pfi)
263                 meta -> has_pfi = disc_xbox_read_dvd_structure (d, 0x00, 0x00, meta -> pfi, sizeof (meta -> pfi)) >= 0;
264         if (!meta -> has_dmi)
265                 meta -> has_dmi = disc_xbox_read_dvd_structure (d, 0x04, 0x00, meta -> dmi, sizeof (meta -> dmi)) >= 0;
266 }
267
268 static void xbox_write_redump_metadata (dumper *dmp, xbox_redump_metadata *meta) {
269         FILE *json;
270         const char *pfi_leaf;
271         const char *dmi_leaf;
272
273         if (!dmp || !meta || !dmp -> outfile_iso)
274                 return;
275
276         xbox_make_related_path (dmp -> outfile_iso, ".pfi.bin", meta -> pfi_path, sizeof (meta -> pfi_path));
277         xbox_make_related_path (dmp -> outfile_iso, ".dmi.bin", meta -> dmi_path, sizeof (meta -> dmi_path));
278         xbox_make_related_path (dmp -> outfile_iso, ".redump.json", meta -> json_path, sizeof (meta -> json_path));
279
280         if (meta -> has_pfi)
281                 meta -> pfi_written = xbox_write_binary_file (meta -> pfi_path, meta -> pfi, sizeof (meta -> pfi));
282         if (meta -> has_dmi)
283                 meta -> dmi_written = xbox_write_binary_file (meta -> dmi_path, meta -> dmi, sizeof (meta -> dmi));
284
285         json = fopen (meta -> json_path, "wb");
286         if (!json) {
287                 warning ("Could not write Xbox redump metadata JSON");
288                 return;
289         }
290
291         pfi_leaf = meta -> pfi_written ? xbox_path_leaf (meta -> pfi_path) : NULL;
292         dmi_leaf = meta -> dmi_written ? xbox_path_leaf (meta -> dmi_path) : NULL;
293
294         fprintf (json, "{\n");
295         fprintf (json, "  \"format\": \"friidump-xbox-redump\",\n");
296         fprintf (json, "  \"version\": 1,\n");
297         fprintf (json, "  \"image\": {\n");
298         fprintf (json, "    \"file\": "); xbox_json_string (json, xbox_path_leaf (dmp -> outfile_iso)); fprintf (json, ",\n");
299         fprintf (json, "    \"sector_size\": %u,\n", SECTOR_SIZE);
300         fprintf (json, "    \"sector_count\": %u,\n", meta -> output_sectors);
301         fprintf (json, "    \"byte_count\": %llu,\n", (unsigned long long) meta -> output_sectors * SECTOR_SIZE);
302         fprintf (json, "    \"hashes\": {\n");
303         fprintf (json, "      \"crc32\": "); xbox_json_string (json, dumper_get_iso_crc32 (dmp)); fprintf (json, ",\n");
304         fprintf (json, "      \"md5\": "); xbox_json_string (json, dumper_get_iso_md5 (dmp)); fprintf (json, ",\n");
305         fprintf (json, "      \"sha1\": "); xbox_json_string (json, dumper_get_iso_sha1 (dmp)); fprintf (json, ",\n");
306         fprintf (json, "      \"sha256\": "); xbox_json_string (json, dumper_get_iso_sha2 (dmp)); fprintf (json, "\n");
307         fprintf (json, "    }\n");
308         fprintf (json, "  },\n");
309         fprintf (json, "  \"drive\": {\n");
310         fprintf (json, "    \"model\": "); xbox_json_string (json, disc_get_drive_model_string (dmp -> dsk)); fprintf (json, ",\n");
311         fprintf (json, "    \"command\": %u,\n", disc_get_command (dmp -> dsk));
312         fprintf (json, "    \"method\": %u\n", disc_get_method (dmp -> dsk));
313         fprintf (json, "  },\n");
314         fprintf (json, "  \"layout\": {\n");
315         fprintf (json, "    \"layout_type\": \"original_xbox_xgd1_redump_style_2048\",\n");
316         fprintf (json, "    \"locked_visible_sector_count\": %u,\n", meta -> locked_visible_sectors);
317         fprintf (json, "    \"unlocked_visible_sector_count\": %u,\n", meta -> unlocked_visible_sectors);
318         fprintf (json, "    \"video_l0_start_lba\": 0,\n");
319         fprintf (json, "    \"video_l0_sector_count\": %u,\n", XBOX_XGD1_VIDEO_L0_SECTORS);
320         fprintf (json, "    \"pregame_padding_start_lba\": %u,\n", XBOX_XGD1_VIDEO_L0_SECTORS);
321         fprintf (json, "    \"pregame_padding_sector_count\": %u,\n", XBOX_XGD1_GAME_OUTPUT_START_LBA - XBOX_XGD1_VIDEO_L0_SECTORS);
322         fprintf (json, "    \"game_output_start_lba\": %u,\n", XBOX_XGD1_GAME_OUTPUT_START_LBA);
323         fprintf (json, "    \"game_leadin_sector_count\": %u,\n", XBOX_XISO_LEADIN_SECTORS);
324         fprintf (json, "    \"game_unlocked_source_start_lba\": %u,\n", meta -> game_source_lba);
325         fprintf (json, "    \"game_unlocked_source_sector_count\": %u,\n", meta -> game_source_sectors);
326         fprintf (json, "    \"postgame_padding_start_lba\": %u,\n", XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS);
327         fprintf (json, "    \"postgame_padding_sector_count\": %u,\n", XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA - (XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS));
328         fprintf (json, "    \"video_l1_start_lba\": %u,\n", XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA);
329         fprintf (json, "    \"video_l1_sector_count\": %u,\n", XBOX_XGD1_VIDEO_L1_SECTORS);
330         fprintf (json, "    \"layer_break_lba\": %u\n", XBOX_XGD1_DIC_LAYER_BREAK);
331         fprintf (json, "  },\n");
332         fprintf (json, "  \"discimagecreator_reference\": {\n");
333         fprintf (json, "    \"xbox_size_sectors\": %u,\n", XBOX_XGD1_DIC_XBOX_SIZE);
334         fprintf (json, "    \"xbox_layer_break_lba\": %u,\n", XBOX_XGD1_DIC_LAYER_BREAK);
335         fprintf (json, "    \"dvd_start_psn\": %u,\n", XBOX_XGD1_DIC_DVD_START_PSN);
336         fprintf (json, "    \"xbox_start_psn\": %u,\n", XBOX_XGD1_DIC_XBOX_START_PSN);
337         fprintf (json, "    \"game_output_start_lba\": %u,\n", XBOX_XGD1_GAME_OUTPUT_START_LBA);
338         fprintf (json, "    \"layout_constants_match\": %s\n", xbox_redump_layout_constants_match_dic () ? "true" : "false");
339         fprintf (json, "  },\n");
340         fprintf (json, "  \"lead_in_capture\": {\n");
341         fprintf (json, "    \"game_leadin_attempted\": true,\n");
342         fprintf (json, "    \"readable_sectors\": %u,\n", meta -> game_leadin_read_sectors);
343         fprintf (json, "    \"zero_filled_sectors\": %u\n", meta -> game_leadin_zero_sectors);
344         fprintf (json, "  },\n");
345         fprintf (json, "  \"dvd_structures\": {\n");
346         fprintf (json, "    \"pfi\": "); if (pfi_leaf) xbox_json_string (json, pfi_leaf); else fprintf (json, "null"); fprintf (json, ",\n");
347         fprintf (json, "    \"dmi\": "); if (dmi_leaf) xbox_json_string (json, dmi_leaf); else fprintf (json, "null"); fprintf (json, "\n");
348         fprintf (json, "  },\n");
349         fprintf (json, "  \"notes\": [\n");
350         fprintf (json, "    \"PFI and DMI are stored as separate binary READ DVD STRUCTURE captures because they are not READ(10) user-data sectors.\",\n");
351         fprintf (json, "    \"Pregame and postgame padding are zero-filled unless a future workflow proves readable source sectors for those ranges.\"\n");
352         fprintf (json, "  ]\n");
353         fprintf (json, "}\n");
354         fclose (json);
355
356         if (meta -> pfi_written) {
357                 debug ("Wrote Xbox PFI: %s", meta -> pfi_path);
358         } else {
359                 warning ("Xbox PFI was not captured or could not be written");
360         }
361         if (meta -> dmi_written) {
362                 debug ("Wrote Xbox DMI: %s", meta -> dmi_path);
363         } else {
364                 warning ("Xbox DMI was not captured or could not be written");
365         }
366         debug ("Wrote Xbox redump metadata: %s", meta -> json_path);
367 }
368
369 static bool xbox_write_iso_sectors (dumper *dmp, const u_int8_t *buf, u_int32_t sectors, u_int32_t output_lba, u_int32_t total_sectors) {
370         if (!dmp || !dmp -> fp_iso || !buf)
371                 return false;
372         clearerr (dmp -> fp_iso);
373         if (fwrite (buf, SECTOR_SIZE, sectors, dmp -> fp_iso) != sectors || ferror (dmp -> fp_iso)) {
374                 error ("fwrite() to Xbox redump ISO failed at output LBA %u", output_lba);
375                 return false;
376         }
377         if (dmp -> hashing)
378                 multihash_update (&(dmp -> hash_iso), (u_int8_t *) buf, (size_t) sectors * SECTOR_SIZE);
379         if (dmp -> flushing)
380                 fflush (dmp -> fp_iso);
381         if (dmp -> progress && ((output_lba % 320) == 0 || output_lba + sectors >= total_sectors))
382                 dmp -> progress (false, output_lba + sectors, total_sectors, dmp -> progress_data);
383         return true;
384 }
385
386 static bool xbox_write_zero_iso_range (dumper *dmp, u_int32_t sectors, u_int32_t output_lba, u_int32_t total_sectors, const char *label) {
387         u_int8_t zero[BLOCK_SIZE];
388         u_int32_t done = 0;
389         memset (zero, 0, sizeof (zero));
390         debug ("Writing Xbox %s zero-fill: output LBA %u, sectors %u", label ? label : "range", output_lba, sectors);
391         while (done < sectors) {
392                 u_int32_t todo = sectors - done;
393                 if (todo > SECTORS_PER_BLOCK)
394                         todo = SECTORS_PER_BLOCK;
395                 if (!xbox_write_iso_sectors (dmp, zero, todo, output_lba + done, total_sectors))
396                         return false;
397                 done += todo;
398         }
399         return true;
400 }
401
402 static bool xbox_dump_read10_iso_range (dumper *dmp, u_int32_t source_lba, u_int32_t sectors, u_int32_t output_lba, u_int32_t total_sectors, const char *label, bool auth_recovery, u_int32_t *current_sector) {
403         u_int8_t buf[BLOCK_SIZE];
404         u_int8_t one[SECTOR_SIZE];
405         u_int32_t done = 0;
406         debug ("Reading Xbox %s: source LBA %u, output LBA %u, sectors %u", label ? label : "range", source_lba, output_lba, sectors);
407         disc_xbox_recovery_kick (dmp -> dsk, auth_recovery);
408         while (done < sectors) {
409                 u_int32_t todo = sectors - done;
410                 int retry;
411                 bool ok = false;
412                 if (todo > SECTORS_PER_BLOCK)
413                         todo = SECTORS_PER_BLOCK;
414                 if (current_sector)
415                         *current_sector = output_lba + done;
416                 for (retry = 0; retry < 3 && !ok; retry++) {
417                         if (retry > 0)
418                                 disc_xbox_recovery_kick (dmp -> dsk, auth_recovery);
419                         memset (buf, 0, sizeof (buf));
420                         if (disc_xbox_read_10 (dmp -> dsk, source_lba + done, todo, buf, sizeof (buf)) >= 0)
421                                 ok = true;
422                 }
423                 if (!ok && todo > 1) {
424                         /* Some Xbox-capable drives are more reliable at visible DVD-video edges
425                          * and layer/layout boundaries with single-sector READ(10) commands.
426                          * Fall back before declaring the whole 16-sector batch bad. */
427                         u_int32_t i;
428                         bool singles_ok = true;
429                         for (i = 0; i < todo; i++) {
430                                 bool one_ok = false;
431                                 for (retry = 0; retry < 3 && !one_ok; retry++) {
432                                         if (retry > 0)
433                                                 disc_xbox_recovery_kick (dmp -> dsk, auth_recovery);
434                                         memset (one, 0, sizeof (one));
435                                         if (disc_xbox_read_10 (dmp -> dsk, source_lba + done + i, 1, one, sizeof (one)) >= 0)
436                                                 one_ok = true;
437                                 }
438                                 if (!one_ok) {
439                                         if (current_sector)
440                                                 *current_sector = output_lba + done + i;
441                                         xbox_ref_log_fprintf (stderr, "\n[XBOX] READ(10) failed for %s at source LBA %u / output LBA %u\n", label ? label : "range", source_lba + done + i, output_lba + done + i);
442                                         error ("Xbox READ(10) failed for %s at source LBA %u", label ? label : "range", source_lba + done + i);
443                                         singles_ok = false;
444                                         break;
445                                 }
446                                 memcpy (buf + ((size_t) i * SECTOR_SIZE), one, SECTOR_SIZE);
447                         }
448                         ok = singles_ok;
449                 }
450                 if (!ok) {
451                         if (current_sector)
452                                 *current_sector = output_lba + done;
453                         xbox_ref_log_fprintf (stderr, "\n[XBOX] READ(10) failed for %s at source LBA %u / output LBA %u\n", label ? label : "range", source_lba + done, output_lba + done);
454                         error ("Xbox READ(10) failed for %s at source LBA %u", label ? label : "range", source_lba + done);
455                         return false;
456                 }
457                 if (!xbox_write_iso_sectors (dmp, buf, todo, output_lba + done, total_sectors))
458                         return false;
459                 done += todo;
460         }
461         return true;
462 }
463
464 static bool xbox_read_range_to_memory (disc *d, u_int32_t source_lba, u_int32_t sectors, u_int8_t *out, size_t out_size, const char *label, bool auth_recovery) {
465         u_int8_t one[SECTOR_SIZE];
466         u_int32_t done = 0;
467         if (!d || !out || out_size < (size_t) sectors * SECTOR_SIZE)
468                 return false;
469         debug ("Capturing Xbox %s to memory: source LBA %u, sectors %u", label ? label : "range", source_lba, sectors);
470         disc_xbox_recovery_kick (d, auth_recovery);
471         while (done < sectors) {
472                 u_int32_t todo = sectors - done;
473                 int retry;
474                 bool ok = false;
475                 if (todo > SECTORS_PER_BLOCK)
476                         todo = SECTORS_PER_BLOCK;
477                 for (retry = 0; retry < 3 && !ok; retry++) {
478                         if (retry > 0)
479                                 disc_xbox_recovery_kick (d, auth_recovery);
480                         if (disc_xbox_read_10 (d, source_lba + done, todo, out + ((size_t) done * SECTOR_SIZE), out_size - ((size_t) done * SECTOR_SIZE)) >= 0)
481                                 ok = true;
482                 }
483                 if (!ok && todo > 1) {
484                         u_int32_t i;
485                         bool singles_ok = true;
486                         for (i = 0; i < todo; i++) {
487                                 bool one_ok = false;
488                                 for (retry = 0; retry < 3 && !one_ok; retry++) {
489                                         if (retry > 0)
490                                                 disc_xbox_recovery_kick (d, auth_recovery);
491                                         memset (one, 0, sizeof (one));
492                                         if (disc_xbox_read_10 (d, source_lba + done + i, 1, one, sizeof (one)) >= 0)
493                                                 one_ok = true;
494                                 }
495                                 if (!one_ok) {
496                                         xbox_ref_log_fprintf (stderr, "\n[XBOX] READ(10) failed while capturing %s at source LBA %u\n", label ? label : "range", source_lba + done + i);
497                                         error ("Xbox READ(10) failed while capturing %s at source LBA %u", label ? label : "range", source_lba + done + i);
498                                         singles_ok = false;
499                                         break;
500                                 }
501                                 memcpy (out + ((size_t) (done + i) * SECTOR_SIZE), one, SECTOR_SIZE);
502                         }
503                         ok = singles_ok;
504                 }
505                 if (!ok) {
506                         xbox_ref_log_fprintf (stderr, "\n[XBOX] READ(10) failed while capturing %s at source LBA %u\n", label ? label : "range", source_lba + done);
507                         error ("Xbox READ(10) failed while capturing %s at source LBA %u", label ? label : "range", source_lba + done);
508                         return false;
509                 }
510                 done += todo;
511         }
512         return true;
513 }
514
515 static bool xbox_write_leadin_iso_range (dumper *dmp, u_int32_t source_lba, u_int32_t output_lba, u_int32_t total_sectors, xbox_redump_metadata *meta, const char *label, bool auth_recovery, u_int32_t *current_sector) {
516         u_int8_t buf[SECTOR_SIZE];
517         u_int8_t zero[SECTOR_SIZE];
518         u_int32_t i;
519         bool warned = false;
520         memset (zero, 0, sizeof (zero));
521         debug ("Attempting Xbox %s lead-in from source LBA %u..%u", label ? label : "game", source_lba, source_lba + XBOX_XISO_LEADIN_SECTORS - 1);
522         disc_xbox_recovery_kick (dmp -> dsk, auth_recovery);
523         for (i = 0; i < XBOX_XISO_LEADIN_SECTORS; i++) {
524                 const u_int8_t *src = buf;
525                 if (current_sector)
526                         *current_sector = output_lba + i;
527                 memset (buf, 0, sizeof (buf));
528                 if (disc_xbox_read_10 (dmp -> dsk, source_lba + i, 1, buf, sizeof (buf)) < 0) {
529                         if (!warned) {
530                                 warning ("Drive could not read one or more Xbox lead-in sectors; zero-filling only unreadable lead-in sectors");
531                                 warned = true;
532                         }
533                         src = zero;
534                         if (meta) meta -> game_leadin_zero_sectors++;
535                 } else {
536                         if (meta) meta -> game_leadin_read_sectors++;
537                 }
538                 if (!xbox_write_iso_sectors (dmp, src, 1, output_lba + i, total_sectors))
539                         return false;
540         }
541         return true;
542 }
543
544 static bool xbox_get_xiso_range (disc *d, u_int32_t *source_lba, u_int32_t *source_sectors, u_int32_t *target_sectors) {
545         u_int32_t total_sectors, root_lba, volume_size_raw, volume_sectors;
546
547         if (!d || !source_lba || !source_sectors || !target_sectors)
548                 return false;
549
550         if (disc_xbox_unlock (d) < 0) {
551                 error ("Could not switch Xbox drive into the game/XDVDFS view");
552                 return false;
553         }
554
555         total_sectors = disc_get_sectors_no (d);
556         root_lba = 0;
557         volume_size_raw = 0;
558
559         /* Try the historical physical/game-partition XDVDFS location first,
560          * then the unlocked/XISO-visible LBA 0x20. GDR-8050L after the
561          * native Xbox handshake normally resolves at 0x20.
562          */
563         if (xbox_probe_xdfs_volume (d, XBOX_XISO_START_LBA_MAGIC, &root_lba, &volume_size_raw)) {
564                 *source_lba = XBOX_XISO_START_LBA_MAGIC;
565         } else if (xbox_probe_xdfs_volume (d, XBOX_XISO_STANDARD_GAME_LBA, &root_lba, &volume_size_raw)) {
566                 *source_lba = XBOX_XISO_STANDARD_GAME_LBA;
567         } else {
568                 error ("No Xbox XDVDFS volume found at LBA %u or %u", XBOX_XISO_START_LBA_MAGIC, XBOX_XISO_STANDARD_GAME_LBA);
569                 return false;
570         }
571
572         if (total_sectors > 3300000) {
573                 /* Retail dual-layer XISO copies from the detected game/XDVDFS
574                  * start LBA through logical LBA 1913920, then prepends 32
575                  * output lead-in sectors.
576                  */
577                 if (*source_lba >= XBOX_XISO_DUAL_LAYER_END_LBA) {
578                         error ("Xbox XISO source LBA %u is beyond dual-layer XISO end LBA %u", *source_lba, XBOX_XISO_DUAL_LAYER_END_LBA);
579                         return false;
580                 }
581                 *source_sectors = XBOX_XISO_DUAL_LAYER_END_LBA - *source_lba;
582         } else {
583                 /* For single-layer/homebrew-style media, XDVDFS VolumeSize is a
584                  * byte count, so convert it to 2048-byte sectors.
585                  */
586                 if (volume_size_raw == 0) {
587                         error ("Xbox XDVDFS header reported a zero volume size");
588                         return false;
589                 }
590
591                 volume_sectors = volume_size_raw / SECTOR_SIZE;
592                 if (volume_sectors == 0) {
593                         error ("Xbox XDVDFS volume size %u is smaller than one sector", volume_size_raw);
594                         return false;
595                 }
596                 if (*source_lba + volume_sectors > total_sectors) {
597                         warning ("Xbox XISO volume size extends beyond visible capacity; clipping to remaining visible sectors");
598                         if (total_sectors > *source_lba)
599                                 volume_sectors = total_sectors - *source_lba;
600                         else
601                                 return false;
602                 }
603                 *source_sectors = volume_sectors;
604         }
605
606         *target_sectors = *source_sectors + XBOX_XISO_LEADIN_SECTORS;
607         debug ("Xbox XISO range: source LBA %u, descriptor RootLBA %u, source sectors %u, output sectors %u", *source_lba, root_lba, *source_sectors, *target_sectors);
608         return true;
609 }
610
611
612 /**
613  * Tries to open the output file for writing and to find out if it contains valid data so that the dump can continue.
614  * @param dvd 
615  * @param outfile 
616  * @param[out] fp The file pointer to write to.
617  * @param[out] start_sector The sector to start reading from.
618  * @return true if the dumping can start/continue, false otherwise (for instance if outfile cannot be written to).
619  */
620 bool dumper_set_raw_output_file (dumper *dmp, char *outfile_raw, bool resume) {
621         bool out;
622         my_off_t filesize;
623         FILE *fp;
624
625         if (!outfile_raw) {
626                 /* Raw output disabled */
627                 out = true;
628                 dmp -> start_sector_raw = -1;
629                 my_free (dmp -> outfile_raw);
630                 dmp -> outfile_raw = NULL;
631         } else if (dmp -> outfile_raw) {
632                 error ("Raw output file already defined");
633                 out = false;
634         } else if (!(fp = fopen (outfile_raw, "rb"))) {         /** @todo Maybe we could not open file for permission problems */
635                 /* Raw output file does not exist, start from scratch */
636                 out = true;
637                 dmp -> start_sector_raw = 0;
638                 my_strdup (dmp -> outfile_raw, outfile_raw);
639         } else if (resume) {
640                 /* Raw output file exists and resume was requested, so see how many dumped sectors it contains */
641                 my_fseek (fp, 0, SEEK_END);
642                 filesize = my_ftell (fp);
643                 fclose (fp);
644                 out = true;
645                 dmp -> start_sector_raw = (u_int32_t) (filesize / RAW_SECTOR_SIZE / SECTORS_PER_BLOCK) * SECTORS_PER_BLOCK;
646                 debug ("Raw output can restart from sector %u", dmp -> start_sector_raw);
647                 my_strdup (dmp -> outfile_raw, outfile_raw);
648         } else {
649                 /* Raw output file exists but resume was not requested, error */
650                 fclose (fp);
651                 error ("Raw output file exists, but resume was not requested.");
652                 out = false;
653                 dmp -> start_sector_raw = -1;
654                 my_free (dmp -> outfile_raw);
655                 dmp -> outfile_raw = NULL;
656         }
657
658         return (out);
659 }
660
661
662 bool dumper_set_iso_output_file (dumper *dmp, char *outfile_iso, bool resume) {
663         bool out;
664         my_off_t filesize;
665         FILE *fp;
666
667         if (!outfile_iso) {
668                 /* ISO output disabled */
669                 out = true;
670                 dmp -> start_sector_iso = -1;
671                 my_free (dmp -> outfile_iso);
672                 dmp -> outfile_iso = NULL;
673         } else if (outfile_iso[0] == '\0') {
674                 /* Xbox/GDR-8050L auto-name placeholder; the copied reference path opens the final file. */
675                 out = true;
676                 dmp -> start_sector_iso = 0;
677                 my_strdup (dmp -> outfile_iso, outfile_iso);
678         } else if (dmp -> outfile_iso) {
679                 error ("ISO output file already defined");
680                 out = false;
681         } else if (!(fp = fopen (outfile_iso, "rb"))) {         /** @todo Maybe we could not open file for permission problems */
682                 /* Raw output file does not exist, start from scratch */
683                 out = true;
684                 dmp -> start_sector_iso = 0;
685                 my_strdup (dmp -> outfile_iso, outfile_iso);
686         } else if (resume) {
687                 /* Raw output file exists and resume was requested, so see how many dumped sectors it contains */
688                 my_fseek (fp, 0, SEEK_END);
689                 filesize = my_ftell (fp);
690                 fclose (fp);
691                 out = true;
692                 dmp -> start_sector_iso = (u_int32_t) (filesize / SECTOR_SIZE / SECTORS_PER_BLOCK) * SECTORS_PER_BLOCK;
693                 debug ("ISO output can restart from sector %u", dmp -> start_sector_iso);
694                 my_strdup (dmp -> outfile_iso, outfile_iso);
695         } else {
696                 /* Raw output file exists but resume was not requested, error */
697                 fclose (fp);
698                 error ("ISO output file exists, but resume was not requested.");
699                 out = false;
700                 dmp -> start_sector_iso = -1;
701                 my_free (dmp -> outfile_iso);
702                 dmp -> outfile_iso = NULL;
703         }
704
705         return (out);
706 }
707
708
709 bool dumper_set_xiso_output_file (dumper *dmp, char *outfile_xiso, bool resume) {
710         bool out;
711         my_off_t filesize;
712         FILE *fp;
713
714         if (!outfile_xiso) {
715                 out = true;
716                 dmp -> start_sector_xiso = -1;
717                 my_free (dmp -> outfile_xiso);
718                 dmp -> outfile_xiso = NULL;
719         } else if (outfile_xiso[0] == '\0') {
720                 /* Xbox/GDR-8050L auto-name placeholder; the copied reference path opens the final file. */
721                 out = true;
722                 dmp -> start_sector_xiso = 0;
723                 my_strdup (dmp -> outfile_xiso, outfile_xiso);
724         } else if (dmp -> outfile_xiso) {
725                 error ("XISO output file already defined");
726                 out = false;
727         } else if (!(fp = fopen (outfile_xiso, "rb"))) {
728                 out = true;
729                 dmp -> start_sector_xiso = 0;
730                 my_strdup (dmp -> outfile_xiso, outfile_xiso);
731         } else if (resume) {
732                 my_fseek (fp, 0, SEEK_END);
733                 filesize = my_ftell (fp);
734                 fclose (fp);
735                 out = true;
736                 dmp -> start_sector_xiso = (u_int32_t) (filesize / SECTOR_SIZE / SECTORS_PER_BLOCK) * SECTORS_PER_BLOCK;
737                 debug ("XISO output can restart from output sector %u", dmp -> start_sector_xiso);
738                 my_strdup (dmp -> outfile_xiso, outfile_xiso);
739         } else {
740                 fclose (fp);
741                 error ("XISO output file exists, but resume was not requested.");
742                 out = false;
743                 dmp -> start_sector_xiso = -1;
744                 my_free (dmp -> outfile_xiso);
745                 dmp -> outfile_xiso = NULL;
746         }
747
748         return (out);
749 }
750
751
752 bool dumper_prepare_xiso (dumper *dmp) {
753         bool out;
754         u_int8_t buf[SECTOR_SIZE];
755         size_t r;
756         u_int32_t i;
757
758         if (!dmp -> outfile_xiso) {
759                 error ("No XISO output file defined");
760                 return false;
761         }
762
763         dmp -> start_sector = dmp -> start_sector_xiso;
764
765 #ifdef WIN32
766         if (dmp -> outfile_xiso[0] == '\0' && disc_is_xbox_challenge_drive (dmp -> dsk)) {
767                 if (dmp -> hashing)
768                         multihash_init (&(dmp -> hash_xiso));
769                 dmp -> fp_xiso = NULL;
770                 return true;
771         }
772 #endif
773
774         if (dmp -> hashing)
775                 multihash_init (&(dmp -> hash_xiso));
776
777         dmp -> fp_xiso = fopen (dmp -> outfile_xiso, "a+b");
778         if (!dmp -> fp_xiso)
779                 return false;
780
781         if (dmp -> hashing) {
782                 debug ("Calculating hashes for pre-existing XISO dump data");
783                 if (dmp -> start_sector > 0) {
784                         for (i = 0; i < dmp -> start_sector && (r = fread (buf, SECTOR_SIZE, 1, dmp -> fp_xiso)) > 0; i++)
785                                 multihash_update (&(dmp -> hash_xiso), buf, SECTOR_SIZE);
786                         MY_ASSERT (r > 0);
787                 }
788         }
789
790         if (my_fseek (dmp -> fp_xiso, dmp -> start_sector * SECTOR_SIZE, SEEK_SET) == 0 &&
791             ftruncate (fileno (dmp -> fp_xiso), (int64_t) dmp -> start_sector * SECTOR_SIZE) == 0) {
792                 out = true;
793                 debug ("Writing to file \"%s\" in Xbox XISO format (fseeked() to %lld)", dmp -> outfile_xiso, my_ftell (dmp -> fp_xiso));
794         } else {
795                 out = false;
796                 fclose (dmp -> fp_xiso);
797                 dmp -> fp_xiso = NULL;
798         }
799
800         return (out);
801 }
802
803
804 bool dumper_prepare (dumper *dmp) {
805         bool out;
806         u_int8_t buf[RAW_SECTOR_SIZE];
807         size_t r;
808         u_int32_t i;
809
810         /* Outputting to both files, resume must start from the file with the least sectors. Hopefully they will have the same number of sectors, anyway... */
811         if (dmp -> outfile_raw && dmp -> outfile_iso && dmp -> start_sector_raw != dmp -> start_sector_iso) {
812                 if (dmp -> start_sector_raw < dmp -> start_sector_iso)
813                         dmp -> start_sector = dmp -> start_sector_raw;
814                 else
815                         dmp -> start_sector = dmp -> start_sector_iso;
816         } else if (dmp -> outfile_raw) {
817                 dmp -> start_sector = dmp -> start_sector_raw;
818         } else if (dmp -> outfile_iso) {
819                 dmp -> start_sector = dmp -> start_sector_iso;
820         } else {
821                 MY_ASSERT (0);
822         }
823
824 #ifdef WIN32
825         if (dmp -> outfile_iso && dmp -> outfile_iso[0] == '\0' && disc_is_xbox_challenge_drive (dmp -> dsk)) {
826                 if (dmp -> hashing) {
827                         multihash_init (&(dmp -> hash_raw));
828                         multihash_init (&(dmp -> hash_iso));
829                 }
830                 dmp -> fp_raw = NULL;
831                 dmp -> fp_iso = NULL;
832                 return true;
833         }
834 #endif
835
836         /* Prepare hashes */
837         if (dmp -> hashing) {
838                 multihash_init (&(dmp -> hash_raw));
839                 multihash_init (&(dmp -> hash_iso));
840         }
841
842         /* Setup raw output file */
843         if (dmp -> outfile_raw) {
844                 dmp -> fp_raw = fopen (dmp -> outfile_raw, "a+b");
845
846                 if (dmp -> hashing) {
847                         debug ("Calculating hashes for pre-existing raw dump data");
848                         if (dmp -> start_sector > 0) {
849                                 for (i = 0; i < dmp -> start_sector && (r = fread (buf, RAW_SECTOR_SIZE, 1, dmp -> fp_raw)) > 0; i++)
850                                         multihash_update (&(dmp -> hash_raw), buf, RAW_SECTOR_SIZE);
851                                 MY_ASSERT (r > 0);
852                         }
853                 }
854
855                 /* Now call fseek as file will only be written, from now on */
856                 if (my_fseek (dmp -> fp_raw, dmp -> start_sector * RAW_SECTOR_SIZE, SEEK_SET) == 0 &&
857                     ftruncate (fileno (dmp -> fp_raw), (int64_t) dmp -> start_sector * RAW_SECTOR_SIZE) == 0) {
858                         out = true;
859                         debug ("Writing to file \"%s\" in raw format (fseeked() to %lld)", dmp -> outfile_raw, my_ftell (dmp -> fp_raw));
860                 } else {
861                         out = false;
862                         fclose (dmp -> fp_raw);
863                         dmp -> fp_raw = NULL;
864                 }
865         } else {
866                 dmp -> fp_raw = NULL;
867         }
868
869         /* Setup ISO output file */
870         if (dmp -> outfile_iso) {
871                 dmp -> fp_iso = fopen (dmp -> outfile_iso, "a+b");
872
873                 if (dmp -> hashing) {
874                         debug ("Calculating hashes for pre-existing ISO dump data");
875                         if (dmp -> start_sector > 0) {
876                                 for (i = 0; i < dmp -> start_sector && (r = fread (buf, SECTOR_SIZE, 1, dmp -> fp_iso)) > 0; i++)
877                                         multihash_update (&(dmp -> hash_iso), buf, SECTOR_SIZE);
878                                 MY_ASSERT (r > 0);
879                         }
880                 }
881
882                 if (my_fseek (dmp -> fp_iso, dmp -> start_sector * SECTOR_SIZE, SEEK_SET) == 0 &&
883                     ftruncate (fileno (dmp -> fp_iso), (int64_t) dmp -> start_sector * SECTOR_SIZE) == 0) {
884                         out = true;
885                         debug ("Writing to file \"%s\" in ISO format (fseeked() to %lld)", dmp -> outfile_iso, my_ftell (dmp -> fp_iso));
886                 } else {
887                         out = false;
888                         fclose (dmp -> fp_iso);
889                         dmp -> fp_iso = NULL;
890                 }
891         } else {
892                 dmp -> fp_iso = NULL;
893         }
894
895         return (out);
896 }
897
898
899
900 static int dumper_dump_xbox_redump_iso (dumper *dmp, u_int32_t *current_sector) {
901         xbox_redump_metadata meta;
902         u_int8_t *video_l1 = NULL;
903         u_int32_t locked_sectors = 0, unlocked_sectors = 0, sector_size = 0;
904         u_int32_t game_source_lba = 0xFFFFFFFFU;
905         u_int32_t root_lba = 0, volume_size_raw = 0;
906         u_int32_t pregame_padding = XBOX_XGD1_GAME_OUTPUT_START_LBA - XBOX_XGD1_VIDEO_L0_SECTORS;
907         u_int32_t postgame_padding = XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA - (XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS);
908         bool out = false;
909         bool locked_view_is_xdfs;
910         disc_type type_id;
911         char *type = NULL;
912
913         if (!dmp)
914                 return false;
915
916         disc_get_type (dmp -> dsk, &type_id, &type);
917         if (type_id != DISC_TYPE_XBOX)
918                 return false;
919
920         if (dmp -> fp_raw) {
921                 error ("Xbox redump-style ISO output cannot be combined with -r/raw output in one run");
922                 return false;
923         }
924
925         if (dmp -> start_sector != 0) {
926                 error ("Resume is not supported for Xbox redump-style ISO reconstruction because the drive view changes mid-dump");
927                 return false;
928         }
929
930 #ifdef WIN32
931         if (disc_is_xbox_challenge_drive (dmp -> dsk)) {
932                 int ref_status;
933                 if (dmp -> fp_iso) {
934                         fclose (dmp -> fp_iso);
935                         dmp -> fp_iso = NULL;
936                 }
937                 if (current_sector)
938                         *current_sector = 0;
939                 xbox_ref_log_fprintf (stderr, "[XBOX] Dispatching GDR-8050L -i directly to copied original dumper code using FriiDump's existing drive HANDLE.\n");
940                 ref_status = xbox_ref_gdr8050l_dump_with_handle (disc_get_native_handle (dmp -> dsk), disc_get_device (dmp -> dsk), dmp -> outfile_iso, '1', &(dmp -> xbox_ref_result));
941                 return ref_status == 0;
942         }
943 #endif
944
945         if (!dmp -> fp_iso)
946                 return false;
947
948         memset (&meta, 0, sizeof (meta));
949
950         /* Original GDR-8050L dumper order for redump-style output:
951          *   1. unlock/authenticate into the game view;
952          *   2. read XDVDFS/game metadata while that view is available;
953          *   3. perform a real media transition to return to the visible DVD-video view;
954          *   4. dump video/front/tail, unlock again, then dump game data.
955          *
956          * The video partition is not encrypted/locked; "lock" here only means the
957          * drive-visible DVD-video state before the Xbox game-view unlock. */
958         if (disc_is_xbox_challenge_drive (dmp -> dsk)) {
959                 xbox_ref_log_fprintf (stderr, "[XBOX] GDR-8050L flow follows original main.c/DumpXboxGameDisc option 1.\n");
960                 xbox_ref_log_fprintf (stderr, "[XBOX] Stage 1/6: primary UnlockDrive() handshake.\n");
961                 disc_xbox_wait_ready (dmp -> dsk, 30000);
962                 if (disc_xbox_unlock (dmp -> dsk) < 0)
963                         xbox_ref_log_fprintf (stderr, "[XBOX][WARN] Stage 1 UnlockDrive() returned failure; continuing like the reference dumper and letting later reads prove state.\n");
964                 xbox_ref_log_fprintf (stderr, "[XBOX] Stage 2/6: media cycle after primary handshake.\n");
965                 if (disc_xbox_media_cycle (dmp -> dsk) < 0) {
966                         xbox_ref_log_fprintf (stderr, "[XBOX][FATAL] Stage 2 failed: required media-cycle failed.\n");
967                         return false;
968                 }
969                 xbox_ref_log_fprintf (stderr, "[XBOX] Stage 3/6: re-apply UnlockDrive() after media change.\n");
970                 if (disc_xbox_unlock (dmp -> dsk) < 0)
971                         xbox_ref_log_fprintf (stderr, "[XBOX][WARN] Stage 3 UnlockDrive() returned failure; continuing like the reference dumper and letting GetXboxGameInfo/XDVDFS probe prove state.\n");
972
973                 xbox_ref_log_fprintf (stderr, "[XBOX] Stage 4/6: RefreshVolume, settle, EnsureDriveReady, SetDriveSpeedMax, and volume lock before metadata probe.\n");
974                 disc_xbox_refresh_volume (dmp -> dsk);
975                 disc_xbox_wait_ready (dmp -> dsk, 30000);
976                 disc_set_speed (dmp -> dsk, 0xFFFF);
977                 if (disc_xbox_lock_volume (dmp -> dsk) < 0)
978                         xbox_ref_log_fprintf (stderr, "[XBOX][WARN] FSCTL_LOCK_VOLUME equivalent failed; continuing like reference dumper warning path.\n");
979                 disc_xbox_read_capacity_10 (dmp -> dsk, &unlocked_sectors, &sector_size);
980                 meta.unlocked_visible_sectors = unlocked_sectors;
981                 xbox_capture_pfi_dmi (dmp -> dsk, &meta);
982                 if (xbox_probe_xdfs_volume (dmp -> dsk, XBOX_XISO_STANDARD_GAME_LBA, &root_lba, &volume_size_raw)) {
983                         meta.game_source_lba = XBOX_XISO_STANDARD_GAME_LBA;
984                         meta.game_source_sectors = XBOX_XGD1_GAME_SOURCE_SECTORS;
985                 } else if (xbox_probe_xdfs_volume (dmp -> dsk, XBOX_XISO_START_LBA_MAGIC, &root_lba, &volume_size_raw)) {
986                         meta.game_source_lba = XBOX_XISO_START_LBA_MAGIC;
987                         meta.game_source_sectors = XBOX_XGD1_GAME_SOURCE_SECTORS;
988                 }
989
990                 xbox_ref_log_fprintf (stderr, "[XBOX] Stage 5/6: media cycle back to visible DVD-video view before sector 0 capture.\n");
991                 if (disc_xbox_media_cycle (dmp -> dsk) < 0) {
992                         xbox_ref_log_fprintf (stderr, "[XBOX][FATAL] Stage 5 failed: could not restore visible DVD-video view.\n");
993                         return false;
994                 }
995                 disc_xbox_wait_ready (dmp -> dsk, 45000);
996         } else if (disc_is_xbox_vendor_unlock_drive (dmp -> dsk)) {
997                 /* GDR-3120L/Kreon-style FF 08 01 profiles can explicitly select
998                  * state 0 for the visible DVD-video view. */
999                 if (disc_xbox_lock (dmp -> dsk) < 0)
1000                         warning ("Could not set Xbox vendor lock state 0; continuing with the current visible view");
1001         }
1002
1003         disc_xbox_read_capacity_10 (dmp -> dsk, &locked_sectors, &sector_size);
1004         meta.locked_visible_sectors = locked_sectors;
1005         xbox_capture_pfi_dmi (dmp -> dsk, &meta);
1006
1007         locked_view_is_xdfs = xbox_probe_xdfs_volume (dmp -> dsk, XBOX_XISO_STANDARD_GAME_LBA, NULL, NULL);
1008         if (locked_view_is_xdfs && disc_is_xbox_challenge_drive (dmp -> dsk)) {
1009                 error ("Current GDR-8050L view still exposes XDVDFS at LBA 32 after media cycle; cannot capture visible DVD-video view for redump-style ISO");
1010                 return false;
1011         }
1012
1013         if (!xbox_redump_layout_constants_match_dic ()) {
1014                 error ("Xbox redump layout constants do not match the DiscImageCreator-compatible XGD1 layout");
1015                 return false;
1016         }
1017
1018         meta.output_sectors = XBOX_XGD1_FULL_REDUMP_SECTORS;
1019         if (dmp -> progress)
1020                 dmp -> progress (true, 0, meta.output_sectors, dmp -> progress_data);
1021
1022         debug ("Xbox redump-style layout: video L0=%u, pregame padding=%u, game view=%u, postgame padding=%u, video L1=%u",
1023                 XBOX_XGD1_VIDEO_L0_SECTORS, pregame_padding, XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS, postgame_padding, XBOX_XGD1_VIDEO_L1_SECTORS);
1024
1025         video_l1 = (u_int8_t *) malloc ((size_t) XBOX_XGD1_VIDEO_L1_SECTORS * SECTOR_SIZE);
1026         if (!video_l1) {
1027                 error ("Could not allocate Xbox VIDEO-L1 capture buffer");
1028                 return false;
1029         }
1030
1031         /* Capture the tail of the visible DVD-video view before switching to the game view. */
1032         if (!xbox_read_range_to_memory (dmp -> dsk, XBOX_XGD1_VIDEO_L0_SECTORS, XBOX_XGD1_VIDEO_L1_SECTORS,
1033                         video_l1, (size_t) XBOX_XGD1_VIDEO_L1_SECTORS * SECTOR_SIZE, "VIDEO-L1", false))
1034                 goto cleanup;
1035
1036         if (!xbox_dump_read10_iso_range (dmp, 0, XBOX_XGD1_VIDEO_L0_SECTORS, 0, meta.output_sectors, "VIDEO-L0", false, current_sector))
1037                 goto cleanup;
1038
1039         if (current_sector) *current_sector = XBOX_XGD1_VIDEO_L0_SECTORS;
1040         if (!xbox_write_zero_iso_range (dmp, pregame_padding, XBOX_XGD1_VIDEO_L0_SECTORS, meta.output_sectors, "PREGAME-PADDING"))
1041                 goto cleanup;
1042
1043         xbox_ref_log_fprintf (stderr, "[XBOX] Stage 6/6: re-apply UnlockDrive() for unlocked game/XDVDFS data.\n");
1044         if (disc_xbox_unlock (dmp -> dsk) < 0)
1045                 xbox_ref_log_fprintf (stderr, "[XBOX][WARN] Stage 6 UnlockDrive() returned failure; continuing until the XDVDFS probe/read proves failure.\n");
1046         disc_xbox_refresh_volume (dmp -> dsk);
1047         disc_xbox_wait_ready (dmp -> dsk, 30000);
1048         disc_set_speed (dmp -> dsk, 0xFFFF);
1049         disc_xbox_read_capacity_10 (dmp -> dsk, &unlocked_sectors, &sector_size);
1050         meta.unlocked_visible_sectors = unlocked_sectors;
1051         xbox_capture_pfi_dmi (dmp -> dsk, &meta);
1052
1053         if (xbox_probe_xdfs_volume (dmp -> dsk, XBOX_XISO_STANDARD_GAME_LBA, &root_lba, &volume_size_raw))
1054                 game_source_lba = XBOX_XISO_STANDARD_GAME_LBA;
1055         else if (xbox_probe_xdfs_volume (dmp -> dsk, XBOX_XISO_START_LBA_MAGIC, &root_lba, &volume_size_raw))
1056                 game_source_lba = XBOX_XISO_START_LBA_MAGIC;
1057         else {
1058                 error ("Could not locate Xbox XDVDFS header after unlock at LBA %u or %u", XBOX_XISO_STANDARD_GAME_LBA, XBOX_XISO_START_LBA_MAGIC);
1059                 goto cleanup;
1060         }
1061
1062         meta.game_source_lba = game_source_lba;
1063         meta.game_source_sectors = XBOX_XGD1_GAME_SOURCE_SECTORS;
1064         if (game_source_lba != XBOX_XISO_STANDARD_GAME_LBA && disc_is_xbox_challenge_drive (dmp -> dsk))
1065                 warning ("Xbox XDVDFS was detected at LBA %u, not the expected GDR-8050L game-view LBA 32", game_source_lba);
1066
1067         if (disc_is_xbox_challenge_drive (dmp -> dsk)) {
1068                 /* Match the original GDR-8050L dumper's option-1 redump path: the
1069                  * 32-sector XISO/game lead-in area is synthetic zero-fill, and the real
1070                  * XDVDFS data begins at unlocked source LBA 32. */
1071                 xbox_ref_log_fprintf (stderr, "[XBOX] Writing 32-sector game lead-in as zero-fill for GDR-8050L redump flow.\n");
1072                 meta.game_leadin_zero_sectors += XBOX_XISO_LEADIN_SECTORS;
1073                 if (current_sector) *current_sector = XBOX_XGD1_GAME_OUTPUT_START_LBA;
1074                 if (!xbox_write_zero_iso_range (dmp, XBOX_XISO_LEADIN_SECTORS, XBOX_XGD1_GAME_OUTPUT_START_LBA, meta.output_sectors, "GAME-XISO-LEADIN"))
1075                         goto cleanup;
1076         } else {
1077                 if (!xbox_write_leadin_iso_range (dmp,
1078                                 (game_source_lba >= XBOX_XISO_LEADIN_SECTORS) ? game_source_lba - XBOX_XISO_LEADIN_SECTORS : 0,
1079                                 XBOX_XGD1_GAME_OUTPUT_START_LBA, meta.output_sectors, &meta, "redump game", true, current_sector))
1080                         goto cleanup;
1081         }
1082
1083         if (!xbox_dump_read10_iso_range (dmp, game_source_lba, XBOX_XGD1_GAME_SOURCE_SECTORS,
1084                         XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XISO_LEADIN_SECTORS, meta.output_sectors, "GAME-XDVDFS", true, current_sector))
1085                 goto cleanup;
1086
1087         if (current_sector) *current_sector = XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS;
1088         if (!xbox_write_zero_iso_range (dmp, postgame_padding, XBOX_XGD1_GAME_OUTPUT_START_LBA + XBOX_XGD1_UNLOCKED_GAME_VIEW_SECTORS,
1089                         meta.output_sectors, "POSTGAME-PADDING"))
1090                 goto cleanup;
1091
1092         if (!xbox_write_iso_sectors (dmp, video_l1, XBOX_XGD1_VIDEO_L1_SECTORS, XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA, meta.output_sectors))
1093                 goto cleanup;
1094
1095         if (dmp -> hashing)
1096                 multihash_finish (&(dmp -> hash_iso));
1097         if (dmp -> fp_iso) {
1098                 fclose (dmp -> fp_iso);
1099                 dmp -> fp_iso = NULL;
1100         }
1101
1102         xbox_write_redump_metadata (dmp, &meta);
1103         out = true;
1104
1105 cleanup:
1106         if (video_l1)
1107                 free (video_l1);
1108         /* Keep current_sector at the last phase-specific LBA on failure so the
1109          * CLI can report a useful range instead of collapsing every setup failure
1110          * back to 0..15. */
1111         return out;
1112 }
1113
1114 int dumper_dump (dumper *dmp, u_int32_t *current_sector) {
1115         bool out;
1116         u_int8_t *rawbuf, *isobuf;
1117         u_int32_t i, sectors_no, last_sector;
1118         disc_type type_id;
1119         char *type_string = NULL;
1120 #ifdef DEBUGaa
1121         bool no_unscrambling;
1122 #endif
1123
1124 #ifdef DEBUGaa
1125         no_unscrambling = dd -> no_unscrambling;
1126         if (fp_iso && no_unscrambling) {
1127                 warning ("Output to ISO format requested, ignoring no_unscrambling!");
1128                 no_unscrambling = false;
1129         }
1130 #endif
1131
1132         disc_get_type (dmp -> dsk, &type_id, &type_string);
1133         if (type_id == DISC_TYPE_XBOX && dmp -> fp_iso)
1134                 return dumper_dump_xbox_redump_iso (dmp, current_sector);
1135
1136         sectors_no = disc_get_sectors_no (dmp -> dsk);
1137 #if 0
1138         if (dd -> start_sector != -1) {
1139                 if (dd -> start_sector >= sectors_no) {
1140                         error ("Cannot start dumping from sector %u as the inserted disc only has %u sectors\n", dd -> start_sector, sectors_no);
1141                         out = false;
1142                 } else {
1143                         warning ("Start sector forced to %u\n", dd -> start_sector);
1144                         ss = dd -> start_sector;
1145
1146                         if (fp_iso)
1147                                 fseek (fp_iso, ss * 2048, SEEK_SET);
1148                         if (fp_raw)
1149                                 fseek (fp_raw, ss * 2064, SEEK_SET);
1150                 }
1151         }
1152 #endif
1153         
1154         if (true) {
1155                 debug ("Starting dump process from sector %u...\n", dmp -> start_sector);
1156
1157                 /* First call to progress function */
1158                 if (dmp -> progress)
1159                         dmp -> progress (true, dmp -> start_sector, sectors_no, dmp -> progress_data);
1160
1161                 last_sector=sectors_no-1;
1162                 
1163                 for (i = dmp -> start_sector, out = true; i < sectors_no && out; i++) {
1164                         if (!dumper_read_sector_with_retry (dmp, i, &isobuf, &rawbuf)) {
1165                                 error ("Read failed after dump-level retries at sectors %u..%u", i, i + SECTORS_PER_BLOCK - 1);
1166                                 out = false;
1167                                 *(current_sector) = i;
1168                         }
1169
1170                         if (out && dmp -> fp_raw) {
1171                                 clearerr (dmp -> fp_raw);
1172
1173                                 if (!rawbuf) {
1174                                         error ("NULL buffer");
1175                                         out = false;
1176                                         *(current_sector) = i;
1177                                 }
1178                                 else fwrite (rawbuf, RAW_SECTOR_SIZE, 1, dmp -> fp_raw);
1179                                 if (ferror (dmp -> fp_raw)) {
1180                                         error ("fwrite() to raw output file failed");
1181                                         out = false;
1182                                         *(current_sector) = i;
1183                                 }
1184
1185                                 if (dmp -> flushing)
1186                                         fflush (dmp -> fp_raw);
1187
1188                                 if (dmp -> hashing && out)
1189                                         multihash_update (&(dmp -> hash_raw), rawbuf, RAW_SECTOR_SIZE);
1190                         }
1191
1192                         if (out && dmp -> fp_iso) {
1193                                 clearerr (dmp -> fp_iso);
1194
1195                                 if (!isobuf) {
1196                                         error ("NULL buffer");
1197                                         out = false;
1198                                         *(current_sector) = i;
1199                                 }
1200                                 else fwrite (isobuf, SECTOR_SIZE, 1, dmp -> fp_iso);
1201
1202                                 if (ferror (dmp -> fp_iso)) {
1203                                         error ("fwrite() to ISO output file failed");
1204                                         out = false;
1205                                         *(current_sector) = i;
1206                                 }
1207
1208                                 if (dmp -> flushing)
1209                                         fflush (dmp -> fp_iso);
1210
1211                                 if (dmp -> hashing && out)
1212                                         multihash_update (&(dmp -> hash_iso), isobuf, SECTOR_SIZE);
1213                         }
1214
1215                         if ((i % 320 == 0) || (i == last_sector)) { //speedhack
1216                                 if (dmp -> progress)
1217                                         dmp -> progress (false, i + 1, sectors_no, dmp -> progress_data);               /* i + 1 'cause sectors range from 0 to N */
1218                         }
1219                 }
1220
1221                 if (dmp -> hashing) {
1222                         multihash_finish (&(dmp -> hash_raw));
1223                         multihash_finish (&(dmp -> hash_iso));
1224                 }
1225
1226                 if (dmp -> fp_raw)
1227                         fclose (dmp -> fp_raw);
1228                 if (dmp -> fp_iso)
1229                         fclose (dmp -> fp_iso);
1230                 if (out) {
1231
1232
1233                 }
1234         }
1235
1236         return (out);
1237 }
1238
1239
1240 int dumper_dump_xiso (dumper *dmp, u_int32_t *current_sector) {
1241         bool out;
1242         u_int8_t *rawbuf, *isobuf;
1243         u_int8_t sector_buf[SECTOR_SIZE];
1244         u_int32_t source_lba, source_sectors, target_sectors, i, source_sector, last_sector, leadin_source_lba;
1245         bool leadin_fallback_warned;
1246
1247         if (!dmp) {
1248                 error ("XISO dumper is not prepared");
1249                 return false;
1250         }
1251
1252 #ifdef WIN32
1253         if (disc_is_xbox_challenge_drive (dmp -> dsk)) {
1254                 int ref_status;
1255                 if (dmp -> fp_xiso) {
1256                         fclose (dmp -> fp_xiso);
1257                         dmp -> fp_xiso = NULL;
1258                 }
1259                 if (current_sector)
1260                         *current_sector = 0;
1261                 xbox_ref_log_fprintf (stderr, "[XBOX] Dispatching GDR-8050L -X directly to copied original dumper code using FriiDump's existing drive HANDLE.\n");
1262                 ref_status = xbox_ref_gdr8050l_dump_with_handle (disc_get_native_handle (dmp -> dsk), disc_get_device (dmp -> dsk), dmp -> outfile_xiso, '2', &(dmp -> xbox_ref_result));
1263                 return ref_status == 0;
1264         }
1265 #endif
1266
1267         if (!dmp -> fp_xiso) {
1268                 error ("XISO dumper is not prepared");
1269                 return false;
1270         }
1271
1272         if (!xbox_get_xiso_range (dmp -> dsk, &source_lba, &source_sectors, &target_sectors)) {
1273                 if (current_sector)
1274                         *current_sector = dmp -> start_sector;
1275                 return false;
1276         }
1277
1278         if (dmp -> start_sector >= target_sectors) {
1279                 error ("Cannot resume Xbox XISO at output sector %u; target has only %u sectors", dmp -> start_sector, target_sectors);
1280                 if (current_sector)
1281                         *current_sector = dmp -> start_sector;
1282                 return false;
1283         }
1284
1285         memset (sector_buf, 0, sizeof (sector_buf));
1286         leadin_source_lba = (source_lba >= XBOX_XISO_LEADIN_SECTORS) ? (source_lba - XBOX_XISO_LEADIN_SECTORS) : 0;
1287         leadin_fallback_warned = false;
1288         out = true;
1289         last_sector = target_sectors - 1;
1290
1291         debug ("Starting Xbox XISO dump from output sector %u...", dmp -> start_sector);
1292         debug ("Xbox XISO lead-in source LBA %u..%u will be attempted before zero-fill fallback", leadin_source_lba, leadin_source_lba + XBOX_XISO_LEADIN_SECTORS - 1);
1293         if (dmp -> progress)
1294                 dmp -> progress (true, dmp -> start_sector, target_sectors, dmp -> progress_data);
1295
1296         for (i = dmp -> start_sector; i < target_sectors && out; i++) {
1297                 if (i < XBOX_XISO_LEADIN_SECTORS) {
1298                         isobuf = NULL;
1299                         rawbuf = NULL;
1300                         if (disc_xbox_read_10 (dmp -> dsk, leadin_source_lba + i, 1, sector_buf, sizeof (sector_buf)) >= 0) {
1301                                 isobuf = sector_buf;
1302                         } else {
1303                                 if (!leadin_fallback_warned) {
1304                                         warning ("Drive could not read one or more Xbox XISO lead-in sectors; using zero-fill fallback for unreadable lead-in sectors");
1305                                         leadin_fallback_warned = true;
1306                                 }
1307                                 memset (sector_buf, 0, sizeof (sector_buf));
1308                                 isobuf = sector_buf;
1309                         }
1310
1311                         clearerr (dmp -> fp_xiso);
1312                         fwrite (isobuf, SECTOR_SIZE, 1, dmp -> fp_xiso);
1313                         if (ferror (dmp -> fp_xiso)) {
1314                                 error ("fwrite() to XISO output file failed");
1315                                 out = false;
1316                                 if (current_sector)
1317                                         *current_sector = i;
1318                         }
1319
1320                         if (dmp -> hashing && out)
1321                                 multihash_update (&(dmp -> hash_xiso), isobuf, SECTOR_SIZE);
1322                 } else {
1323                         source_sector = source_lba + (i - XBOX_XISO_LEADIN_SECTORS);
1324                         isobuf = sector_buf;
1325                         rawbuf = NULL;
1326                         if (disc_xbox_read_10 (dmp -> dsk, source_sector, 1, sector_buf, sizeof (sector_buf)) < 0) {
1327                                 error ("Xbox XISO READ(10) failed at source LBA %u", source_sector);
1328                                 out = false;
1329                                 if (current_sector)
1330                                         *current_sector = i;
1331                         } else {
1332                                 clearerr (dmp -> fp_xiso);
1333                                 fwrite (isobuf, SECTOR_SIZE, 1, dmp -> fp_xiso);
1334                                 if (ferror (dmp -> fp_xiso)) {
1335                                         error ("fwrite() to XISO output file failed");
1336                                         out = false;
1337                                         if (current_sector)
1338                                                 *current_sector = i;
1339                                 }
1340
1341                                 if (dmp -> hashing && out)
1342                                         multihash_update (&(dmp -> hash_xiso), isobuf, SECTOR_SIZE);
1343                         }
1344                 }
1345
1346                 if (dmp -> flushing)
1347                         fflush (dmp -> fp_xiso);
1348
1349                 if ((i % 320 == 0) || (i == last_sector)) {
1350                         if (dmp -> progress)
1351                                 dmp -> progress (false, i + 1, target_sectors, dmp -> progress_data);
1352                 }
1353         }
1354
1355         if (dmp -> hashing)
1356                 multihash_finish (&(dmp -> hash_xiso));
1357
1358         if (dmp -> fp_xiso) {
1359                 fclose (dmp -> fp_xiso);
1360                 dmp -> fp_xiso = NULL;
1361         }
1362
1363         return (out);
1364 }
1365
1366
1367 dumper *dumper_new (disc *d) {
1368         dumper *dmp;
1369
1370         dmp = (dumper *) malloc (sizeof (dumper));
1371         memset (dmp, 0, sizeof (dumper));
1372         dmp -> dsk = d;
1373         xbox_ref_dump_result_init (&(dmp -> xbox_ref_result));
1374         dumper_set_hashing (dmp, true);
1375         dumper_set_flushing (dmp, true);
1376
1377         return (dmp);
1378 }
1379
1380
1381 void dumper_set_progress_callback (dumper *dmp, progress_func progress, void *progress_data) {
1382         dmp -> progress = progress;
1383         dmp -> progress_data = progress_data;
1384
1385         return;
1386 }
1387
1388
1389 void dumper_set_hashing (dumper *dmp, bool h) {
1390         dmp -> hashing = h;
1391         debug ("Hashing %s", h ? "enabled" : "disabled");
1392
1393         return;
1394 }
1395
1396
1397 void dumper_set_flushing (dumper *dmp, bool f) {
1398         dmp -> flushing = f;
1399         debug ("Flushing %s", f ? "enabled" : "disabled");
1400
1401         return;
1402 }
1403
1404 void *dumper_destroy (dumper *dmp) {
1405         my_free (dmp -> outfile_raw);
1406         my_free (dmp -> outfile_iso);
1407         my_free (dmp -> outfile_xiso);
1408         my_free (dmp);
1409
1410         return (NULL);
1411 }
1412
1413
1414 char *dumper_get_iso_crc32 (dumper *dmp) {
1415         return ((dmp -> hash_iso).crc32_s);
1416 }
1417
1418
1419 char *dumper_get_raw_crc32 (dumper *dmp) {
1420         return ((dmp -> hash_raw).crc32_s);
1421 }
1422
1423 char *dumper_get_xiso_crc32 (dumper *dmp) {
1424         return ((dmp -> hash_xiso).crc32_s);
1425 }
1426
1427
1428 char *dumper_get_iso_md4 (dumper *dmp) {
1429         return ((dmp -> hash_iso).md4_s);
1430 }
1431
1432
1433 char *dumper_get_raw_md4 (dumper *dmp) {
1434         return ((dmp -> hash_raw).md4_s);
1435 }
1436
1437 char *dumper_get_xiso_md4 (dumper *dmp) {
1438         return ((dmp -> hash_xiso).md4_s);
1439 }
1440
1441
1442 char *dumper_get_iso_md5 (dumper *dmp) {
1443         return ((dmp -> hash_iso).md5_s);
1444 }
1445
1446
1447 char *dumper_get_raw_md5 (dumper *dmp) {
1448         return ((dmp -> hash_raw).md5_s);
1449 }
1450
1451 char *dumper_get_xiso_md5 (dumper *dmp) {
1452         return ((dmp -> hash_xiso).md5_s);
1453 }
1454
1455
1456 char *dumper_get_iso_ed2k (dumper *dmp) {
1457         return ((dmp -> hash_iso).ed2k_s);
1458 }
1459
1460
1461 char *dumper_get_raw_ed2k (dumper *dmp) {
1462         return ((dmp -> hash_raw).ed2k_s);
1463 }
1464
1465
1466 char *dumper_get_iso_sha1 (dumper *dmp) {
1467         return ((dmp -> hash_iso).sha1_s);
1468 }
1469
1470
1471 char *dumper_get_raw_sha1 (dumper *dmp) {
1472         return ((dmp -> hash_raw).sha1_s);
1473 }
1474
1475 char *dumper_get_xiso_sha1 (dumper *dmp) {
1476         return ((dmp -> hash_xiso).sha1_s);
1477 }
1478
1479
1480 char *dumper_get_iso_sha2 (dumper *dmp) {
1481         return ((dmp -> hash_iso).sha2_s);
1482 }
1483
1484
1485 char *dumper_get_raw_sha2 (dumper *dmp) {
1486         return ((dmp -> hash_raw).sha2_s);
1487 }
1488
1489 char *dumper_get_xiso_sha2 (dumper *dmp) {
1490         return ((dmp -> hash_xiso).sha2_s);
1491 }
1492
1493
1494 bool dumper_get_xbox_reference_result (dumper *dmp, xbox_ref_dump_result *result) {
1495         if (!dmp || !result || !(dmp -> xbox_ref_result).attempted)
1496                 return false;
1497         *result = dmp -> xbox_ref_result;
1498         return true;
1499 }