1 /***************************************************************************
2 * Copyright (C) 2007 by Arep *
3 * Support is provided through the forums at *
4 * http://www.console-tribe.com *
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. *
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. *
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 ***************************************************************************/
27 #include <multihash.h>
28 #include "constants.h"
31 #include "xbox_ref_bridge.h"
32 #include "xbox_ref/xbox_ref_log.h"
36 #include <sys/types.h>
42 u_int32_t start_sector_raw;
45 u_int32_t start_sector_iso;
48 u_int32_t start_sector_xiso;
50 u_int32_t start_sector;
57 xbox_ref_dump_result xbox_ref_result;
59 progress_func progress;
63 #define DUMPER_READ_BLOCK_RETRIES 3
65 static bool dumper_read_sector_with_retry (dumper *dmp, u_int32_t sector_no, u_int8_t **isobuf, u_int8_t **rawbuf) {
67 bool need_iso, need_raw;
69 if (!dmp || !dmp -> dsk)
72 need_iso = dmp -> fp_iso != NULL;
73 need_raw = dmp -> fp_raw != NULL;
75 for (attempt = 0; attempt < DUMPER_READ_BLOCK_RETRIES; attempt++) {
76 if (isobuf) *isobuf = NULL;
77 if (rawbuf) *rawbuf = NULL;
79 disc_read_sector (dmp -> dsk, sector_no, isobuf, rawbuf);
80 if ((!need_iso || (isobuf && *isobuf)) && (!need_raw || (rawbuf && *rawbuf))) {
82 warning ("Recovered sectors %u..%u after dump-level retry %d", sector_no, sector_no + SECTORS_PER_BLOCK - 1, attempt);
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);
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
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.
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
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);
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];
125 memset (sector, 0, sizeof (sector));
126 if (disc_xbox_read_10 (d, lba, 1, sector, sizeof (sector)) < 0)
129 if (memcmp (sector, "MICROSOFT", 9) != 0)
133 *root_lba = read_le32 (sector + 0x14);
135 *volume_size_raw = read_le32 (sector + 0x1C);
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;
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;
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;
177 if (!out || out_size == 0)
180 if (!filename || !suffix)
183 suffix_len = strlen (suffix);
184 if (out_size <= suffix_len + 1)
187 slash1 = strrchr (filename, '/');
188 slash2 = strrchr (filename, '\\');
189 if (slash1 && slash2)
190 slash = slash1 > slash2 ? slash1 : slash2;
192 slash = slash1 ? slash1 : slash2;
194 dot = strrchr (filename, '.');
195 if (!dot || (slash && dot < slash))
196 dot = filename + strlen (filename);
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);
206 static const char *xbox_path_leaf (const char *path) {
207 const char *slash1, *slash2;
210 slash1 = strrchr (path, '/');
211 slash2 = strrchr (path, '\\');
212 if (slash1 && slash2)
213 return (slash1 > slash2 ? slash1 : slash2) + 1;
221 static void xbox_json_string (FILE *f, const char *s) {
225 unsigned char c = (unsigned char) *s++;
226 if (c == '"' || c == '\\') {
229 } else if (c == '\n') {
231 } else if (c == '\r') {
233 } else if (c == '\t') {
235 } else if (c < 0x20) {
236 fprintf (f, "\\u%04x", c);
245 static bool xbox_write_binary_file (const char *path, const u_int8_t *data, size_t len) {
247 if (!path || !data || len == 0)
249 f = fopen (path, "wb");
252 if (fwrite (data, 1, len, f) != len) {
256 return fclose (f) == 0;
259 static void xbox_capture_pfi_dmi (disc *d, xbox_redump_metadata *meta) {
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;
268 static void xbox_write_redump_metadata (dumper *dmp, xbox_redump_metadata *meta) {
270 const char *pfi_leaf;
271 const char *dmi_leaf;
273 if (!dmp || !meta || !dmp -> outfile_iso)
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));
281 meta -> pfi_written = xbox_write_binary_file (meta -> pfi_path, meta -> pfi, sizeof (meta -> pfi));
283 meta -> dmi_written = xbox_write_binary_file (meta -> dmi_path, meta -> dmi, sizeof (meta -> dmi));
285 json = fopen (meta -> json_path, "wb");
287 warning ("Could not write Xbox redump metadata JSON");
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;
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");
356 if (meta -> pfi_written) {
357 debug ("Wrote Xbox PFI: %s", meta -> pfi_path);
359 warning ("Xbox PFI was not captured or could not be written");
361 if (meta -> dmi_written) {
362 debug ("Wrote Xbox DMI: %s", meta -> dmi_path);
364 warning ("Xbox DMI was not captured or could not be written");
366 debug ("Wrote Xbox redump metadata: %s", meta -> json_path);
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)
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);
378 multihash_update (&(dmp -> hash_iso), (u_int8_t *) buf, (size_t) sectors * SECTOR_SIZE);
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);
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];
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))
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];
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;
412 if (todo > SECTORS_PER_BLOCK)
413 todo = SECTORS_PER_BLOCK;
415 *current_sector = output_lba + done;
416 for (retry = 0; retry < 3 && !ok; retry++) {
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)
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. */
428 bool singles_ok = true;
429 for (i = 0; i < todo; i++) {
431 for (retry = 0; retry < 3 && !one_ok; retry++) {
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)
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);
446 memcpy (buf + ((size_t) i * SECTOR_SIZE), one, SECTOR_SIZE);
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);
457 if (!xbox_write_iso_sectors (dmp, buf, todo, output_lba + done, total_sectors))
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];
467 if (!d || !out || out_size < (size_t) sectors * SECTOR_SIZE)
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;
475 if (todo > SECTORS_PER_BLOCK)
476 todo = SECTORS_PER_BLOCK;
477 for (retry = 0; retry < 3 && !ok; retry++) {
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)
483 if (!ok && todo > 1) {
485 bool singles_ok = true;
486 for (i = 0; i < todo; i++) {
488 for (retry = 0; retry < 3 && !one_ok; retry++) {
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)
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);
501 memcpy (out + ((size_t) (done + i) * SECTOR_SIZE), one, SECTOR_SIZE);
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);
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];
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;
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) {
530 warning ("Drive could not read one or more Xbox lead-in sectors; zero-filling only unreadable lead-in sectors");
534 if (meta) meta -> game_leadin_zero_sectors++;
536 if (meta) meta -> game_leadin_read_sectors++;
538 if (!xbox_write_iso_sectors (dmp, src, 1, output_lba + i, total_sectors))
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;
547 if (!d || !source_lba || !source_sectors || !target_sectors)
550 if (disc_xbox_unlock (d) < 0) {
551 error ("Could not switch Xbox drive into the game/XDVDFS view");
555 total_sectors = disc_get_sectors_no (d);
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.
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;
568 error ("No Xbox XDVDFS volume found at LBA %u or %u", XBOX_XISO_START_LBA_MAGIC, XBOX_XISO_STANDARD_GAME_LBA);
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.
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);
581 *source_sectors = XBOX_XISO_DUAL_LAYER_END_LBA - *source_lba;
583 /* For single-layer/homebrew-style media, XDVDFS VolumeSize is a
584 * byte count, so convert it to 2048-byte sectors.
586 if (volume_size_raw == 0) {
587 error ("Xbox XDVDFS header reported a zero volume size");
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);
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;
603 *source_sectors = volume_sectors;
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);
613 * Tries to open the output file for writing and to find out if it contains valid data so that the dump can continue.
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).
620 bool dumper_set_raw_output_file (dumper *dmp, char *outfile_raw, bool resume) {
626 /* Raw output disabled */
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");
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 */
637 dmp -> start_sector_raw = 0;
638 my_strdup (dmp -> outfile_raw, outfile_raw);
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);
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);
649 /* Raw output file exists but resume was not requested, error */
651 error ("Raw output file exists, but resume was not requested.");
653 dmp -> start_sector_raw = -1;
654 my_free (dmp -> outfile_raw);
655 dmp -> outfile_raw = NULL;
662 bool dumper_set_iso_output_file (dumper *dmp, char *outfile_iso, bool resume) {
668 /* ISO output disabled */
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. */
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");
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 */
684 dmp -> start_sector_iso = 0;
685 my_strdup (dmp -> outfile_iso, outfile_iso);
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);
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);
696 /* Raw output file exists but resume was not requested, error */
698 error ("ISO output file exists, but resume was not requested.");
700 dmp -> start_sector_iso = -1;
701 my_free (dmp -> outfile_iso);
702 dmp -> outfile_iso = NULL;
709 bool dumper_set_xiso_output_file (dumper *dmp, char *outfile_xiso, bool resume) {
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. */
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");
727 } else if (!(fp = fopen (outfile_xiso, "rb"))) {
729 dmp -> start_sector_xiso = 0;
730 my_strdup (dmp -> outfile_xiso, outfile_xiso);
732 my_fseek (fp, 0, SEEK_END);
733 filesize = my_ftell (fp);
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);
741 error ("XISO output file exists, but resume was not requested.");
743 dmp -> start_sector_xiso = -1;
744 my_free (dmp -> outfile_xiso);
745 dmp -> outfile_xiso = NULL;
752 bool dumper_prepare_xiso (dumper *dmp) {
754 u_int8_t buf[SECTOR_SIZE];
758 if (!dmp -> outfile_xiso) {
759 error ("No XISO output file defined");
763 dmp -> start_sector = dmp -> start_sector_xiso;
766 if (dmp -> outfile_xiso[0] == '\0' && disc_is_xbox_challenge_drive (dmp -> dsk)) {
768 multihash_init (&(dmp -> hash_xiso));
769 dmp -> fp_xiso = NULL;
775 multihash_init (&(dmp -> hash_xiso));
777 dmp -> fp_xiso = fopen (dmp -> outfile_xiso, "a+b");
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);
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) {
793 debug ("Writing to file \"%s\" in Xbox XISO format (fseeked() to %lld)", dmp -> outfile_xiso, my_ftell (dmp -> fp_xiso));
796 fclose (dmp -> fp_xiso);
797 dmp -> fp_xiso = NULL;
804 bool dumper_prepare (dumper *dmp) {
806 u_int8_t buf[RAW_SECTOR_SIZE];
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;
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;
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));
830 dmp -> fp_raw = NULL;
831 dmp -> fp_iso = NULL;
837 if (dmp -> hashing) {
838 multihash_init (&(dmp -> hash_raw));
839 multihash_init (&(dmp -> hash_iso));
842 /* Setup raw output file */
843 if (dmp -> outfile_raw) {
844 dmp -> fp_raw = fopen (dmp -> outfile_raw, "a+b");
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);
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) {
859 debug ("Writing to file \"%s\" in raw format (fseeked() to %lld)", dmp -> outfile_raw, my_ftell (dmp -> fp_raw));
862 fclose (dmp -> fp_raw);
863 dmp -> fp_raw = NULL;
866 dmp -> fp_raw = NULL;
869 /* Setup ISO output file */
870 if (dmp -> outfile_iso) {
871 dmp -> fp_iso = fopen (dmp -> outfile_iso, "a+b");
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);
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) {
885 debug ("Writing to file \"%s\" in ISO format (fseeked() to %lld)", dmp -> outfile_iso, my_ftell (dmp -> fp_iso));
888 fclose (dmp -> fp_iso);
889 dmp -> fp_iso = NULL;
892 dmp -> fp_iso = NULL;
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);
909 bool locked_view_is_xdfs;
916 disc_get_type (dmp -> dsk, &type_id, &type);
917 if (type_id != DISC_TYPE_XBOX)
921 error ("Xbox redump-style ISO output cannot be combined with -r/raw output in one run");
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");
931 if (disc_is_xbox_challenge_drive (dmp -> dsk)) {
934 fclose (dmp -> fp_iso);
935 dmp -> fp_iso = NULL;
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;
948 memset (&meta, 0, sizeof (meta));
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.
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");
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");
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, §or_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;
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");
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");
1003 disc_xbox_read_capacity_10 (dmp -> dsk, &locked_sectors, §or_size);
1004 meta.locked_visible_sectors = locked_sectors;
1005 xbox_capture_pfi_dmi (dmp -> dsk, &meta);
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");
1013 if (!xbox_redump_layout_constants_match_dic ()) {
1014 error ("Xbox redump layout constants do not match the DiscImageCreator-compatible XGD1 layout");
1018 meta.output_sectors = XBOX_XGD1_FULL_REDUMP_SECTORS;
1019 if (dmp -> progress)
1020 dmp -> progress (true, 0, meta.output_sectors, dmp -> progress_data);
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);
1025 video_l1 = (u_int8_t *) malloc ((size_t) XBOX_XGD1_VIDEO_L1_SECTORS * SECTOR_SIZE);
1027 error ("Could not allocate Xbox VIDEO-L1 capture buffer");
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))
1036 if (!xbox_dump_read10_iso_range (dmp, 0, XBOX_XGD1_VIDEO_L0_SECTORS, 0, meta.output_sectors, "VIDEO-L0", false, current_sector))
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"))
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, §or_size);
1050 meta.unlocked_visible_sectors = unlocked_sectors;
1051 xbox_capture_pfi_dmi (dmp -> dsk, &meta);
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;
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);
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);
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"))
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))
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))
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"))
1092 if (!xbox_write_iso_sectors (dmp, video_l1, XBOX_XGD1_VIDEO_L1_SECTORS, XBOX_XGD1_VIDEO_L1_OUTPUT_START_LBA, meta.output_sectors))
1096 multihash_finish (&(dmp -> hash_iso));
1097 if (dmp -> fp_iso) {
1098 fclose (dmp -> fp_iso);
1099 dmp -> fp_iso = NULL;
1102 xbox_write_redump_metadata (dmp, &meta);
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
1114 int dumper_dump (dumper *dmp, u_int32_t *current_sector) {
1116 u_int8_t *rawbuf, *isobuf;
1117 u_int32_t i, sectors_no, last_sector;
1119 char *type_string = NULL;
1121 bool no_unscrambling;
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;
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);
1136 sectors_no = disc_get_sectors_no (dmp -> dsk);
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);
1143 warning ("Start sector forced to %u\n", dd -> start_sector);
1144 ss = dd -> start_sector;
1147 fseek (fp_iso, ss * 2048, SEEK_SET);
1149 fseek (fp_raw, ss * 2064, SEEK_SET);
1155 debug ("Starting dump process from sector %u...\n", dmp -> start_sector);
1157 /* First call to progress function */
1158 if (dmp -> progress)
1159 dmp -> progress (true, dmp -> start_sector, sectors_no, dmp -> progress_data);
1161 last_sector=sectors_no-1;
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);
1167 *(current_sector) = i;
1170 if (out && dmp -> fp_raw) {
1171 clearerr (dmp -> fp_raw);
1174 error ("NULL buffer");
1176 *(current_sector) = i;
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");
1182 *(current_sector) = i;
1185 if (dmp -> flushing)
1186 fflush (dmp -> fp_raw);
1188 if (dmp -> hashing && out)
1189 multihash_update (&(dmp -> hash_raw), rawbuf, RAW_SECTOR_SIZE);
1192 if (out && dmp -> fp_iso) {
1193 clearerr (dmp -> fp_iso);
1196 error ("NULL buffer");
1198 *(current_sector) = i;
1200 else fwrite (isobuf, SECTOR_SIZE, 1, dmp -> fp_iso);
1202 if (ferror (dmp -> fp_iso)) {
1203 error ("fwrite() to ISO output file failed");
1205 *(current_sector) = i;
1208 if (dmp -> flushing)
1209 fflush (dmp -> fp_iso);
1211 if (dmp -> hashing && out)
1212 multihash_update (&(dmp -> hash_iso), isobuf, SECTOR_SIZE);
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 */
1221 if (dmp -> hashing) {
1222 multihash_finish (&(dmp -> hash_raw));
1223 multihash_finish (&(dmp -> hash_iso));
1227 fclose (dmp -> fp_raw);
1229 fclose (dmp -> fp_iso);
1240 int dumper_dump_xiso (dumper *dmp, u_int32_t *current_sector) {
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;
1248 error ("XISO dumper is not prepared");
1253 if (disc_is_xbox_challenge_drive (dmp -> dsk)) {
1255 if (dmp -> fp_xiso) {
1256 fclose (dmp -> fp_xiso);
1257 dmp -> fp_xiso = NULL;
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;
1267 if (!dmp -> fp_xiso) {
1268 error ("XISO dumper is not prepared");
1272 if (!xbox_get_xiso_range (dmp -> dsk, &source_lba, &source_sectors, &target_sectors)) {
1274 *current_sector = dmp -> start_sector;
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);
1281 *current_sector = dmp -> start_sector;
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;
1289 last_sector = target_sectors - 1;
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);
1296 for (i = dmp -> start_sector; i < target_sectors && out; i++) {
1297 if (i < XBOX_XISO_LEADIN_SECTORS) {
1300 if (disc_xbox_read_10 (dmp -> dsk, leadin_source_lba + i, 1, sector_buf, sizeof (sector_buf)) >= 0) {
1301 isobuf = sector_buf;
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;
1307 memset (sector_buf, 0, sizeof (sector_buf));
1308 isobuf = sector_buf;
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");
1317 *current_sector = i;
1320 if (dmp -> hashing && out)
1321 multihash_update (&(dmp -> hash_xiso), isobuf, SECTOR_SIZE);
1323 source_sector = source_lba + (i - XBOX_XISO_LEADIN_SECTORS);
1324 isobuf = sector_buf;
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);
1330 *current_sector = i;
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");
1338 *current_sector = i;
1341 if (dmp -> hashing && out)
1342 multihash_update (&(dmp -> hash_xiso), isobuf, SECTOR_SIZE);
1346 if (dmp -> flushing)
1347 fflush (dmp -> fp_xiso);
1349 if ((i % 320 == 0) || (i == last_sector)) {
1350 if (dmp -> progress)
1351 dmp -> progress (false, i + 1, target_sectors, dmp -> progress_data);
1356 multihash_finish (&(dmp -> hash_xiso));
1358 if (dmp -> fp_xiso) {
1359 fclose (dmp -> fp_xiso);
1360 dmp -> fp_xiso = NULL;
1367 dumper *dumper_new (disc *d) {
1370 dmp = (dumper *) malloc (sizeof (dumper));
1371 memset (dmp, 0, sizeof (dumper));
1373 xbox_ref_dump_result_init (&(dmp -> xbox_ref_result));
1374 dumper_set_hashing (dmp, true);
1375 dumper_set_flushing (dmp, true);
1381 void dumper_set_progress_callback (dumper *dmp, progress_func progress, void *progress_data) {
1382 dmp -> progress = progress;
1383 dmp -> progress_data = progress_data;
1389 void dumper_set_hashing (dumper *dmp, bool h) {
1391 debug ("Hashing %s", h ? "enabled" : "disabled");
1397 void dumper_set_flushing (dumper *dmp, bool f) {
1398 dmp -> flushing = f;
1399 debug ("Flushing %s", f ? "enabled" : "disabled");
1404 void *dumper_destroy (dumper *dmp) {
1405 my_free (dmp -> outfile_raw);
1406 my_free (dmp -> outfile_iso);
1407 my_free (dmp -> outfile_xiso);
1414 char *dumper_get_iso_crc32 (dumper *dmp) {
1415 return ((dmp -> hash_iso).crc32_s);
1419 char *dumper_get_raw_crc32 (dumper *dmp) {
1420 return ((dmp -> hash_raw).crc32_s);
1423 char *dumper_get_xiso_crc32 (dumper *dmp) {
1424 return ((dmp -> hash_xiso).crc32_s);
1428 char *dumper_get_iso_md4 (dumper *dmp) {
1429 return ((dmp -> hash_iso).md4_s);
1433 char *dumper_get_raw_md4 (dumper *dmp) {
1434 return ((dmp -> hash_raw).md4_s);
1437 char *dumper_get_xiso_md4 (dumper *dmp) {
1438 return ((dmp -> hash_xiso).md4_s);
1442 char *dumper_get_iso_md5 (dumper *dmp) {
1443 return ((dmp -> hash_iso).md5_s);
1447 char *dumper_get_raw_md5 (dumper *dmp) {
1448 return ((dmp -> hash_raw).md5_s);
1451 char *dumper_get_xiso_md5 (dumper *dmp) {
1452 return ((dmp -> hash_xiso).md5_s);
1456 char *dumper_get_iso_ed2k (dumper *dmp) {
1457 return ((dmp -> hash_iso).ed2k_s);
1461 char *dumper_get_raw_ed2k (dumper *dmp) {
1462 return ((dmp -> hash_raw).ed2k_s);
1466 char *dumper_get_iso_sha1 (dumper *dmp) {
1467 return ((dmp -> hash_iso).sha1_s);
1471 char *dumper_get_raw_sha1 (dumper *dmp) {
1472 return ((dmp -> hash_raw).sha1_s);
1475 char *dumper_get_xiso_sha1 (dumper *dmp) {
1476 return ((dmp -> hash_xiso).sha1_s);
1480 char *dumper_get_iso_sha2 (dumper *dmp) {
1481 return ((dmp -> hash_iso).sha2_s);
1485 char *dumper_get_raw_sha2 (dumper *dmp) {
1486 return ((dmp -> hash_raw).sha2_s);
1489 char *dumper_get_xiso_sha2 (dumper *dmp) {
1490 return ((dmp -> hash_xiso).sha2_s);
1494 bool dumper_get_xbox_reference_result (dumper *dmp, xbox_ref_dump_result *result) {
1495 if (!dmp || !result || !(dmp -> xbox_ref_result).attempted)
1497 *result = dmp -> xbox_ref_result;