1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/* dvdisaster: Additional error correction for optical media.
* Copyright (C) 2004-2015 Carsten Gnoerlich.
*
* Email: carsten@dvdisaster.org -or- cgnoerlich@fsfe.org
* Project homepage: http://www.dvdisaster.org
*
* This file is part of dvdisaster.
*
* dvdisaster is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dvdisaster is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dvdisaster. If not, see <http://www.gnu.org/licenses/>.
*/
#include "dvdisaster.h"
/*
* Write the raw dump header
*/
static void init_defective_sector_file(char *path, RawBuffer *rb, LargeFile **file, DefectiveSectorHeader *dsh)
{ int n;
*file = LargeOpen(path, O_RDWR | O_CREAT, IMG_PERMS);
if(!*file)
Stop(_("Could not open %s: %s"), path, strerror(errno));
memset(dsh, 0, sizeof(DefectiveSectorHeader));
dsh->lba = rb->lba;
dsh->sectorSize = CD_RAW_DUMP_SIZE;
if(rb->xaMode)
dsh->properties |= DSH_XA_MODE;
if(rb->validFP)
{ memcpy(dsh->mediumFP, rb->mediumFP, 16);
dsh->properties |= DSH_HAS_FINGERPRINT;
}
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
n = LargeWrite(*file, dsh, sizeof(DefectiveSectorHeader));
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
if(n != sizeof(DefectiveSectorHeader))
Stop(_("Failed writing to defective sector file: %s"), strerror(errno));
}
/*
* Open raw dump, read the header
*/
static void open_defective_sector_file(RawBuffer *rb, char *path, LargeFile **file,
DefectiveSectorHeader *dsh)
{ guint64 length;
int n;
*file = LargeOpen(path, O_RDWR, IMG_PERMS);
if(!*file) return;
LargeStat(path, &length);
n = LargeRead(*file, dsh, sizeof(DefectiveSectorHeader));
if(n != sizeof(DefectiveSectorHeader))
Stop(_("Failed reading from defective sector file: %s"), strerror(errno));
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
dsh->nSectors = (length-sizeof(DefectiveSectorHeader))/dsh->sectorSize;
if(dsh->nSectors*dsh->sectorSize+sizeof(DefectiveSectorHeader) != length)
Stop(_("Defective sector file is truncated"));
/* Expand the old non-C2 raw dumps to new size */
if(dsh->sectorSize == 2352) /* old non C2-style raw dump? */
{ unsigned char *buf,*ptr;
unsigned char zero[296];
int i,n;
PrintCLI(" * Expanding raw dump for sector %lld from 2352 to %d bytes *\n",
(long long)dsh->lba, MAX_RAW_TRANSFER_SIZE);
buf = g_malloc(dsh->sectorSize*dsh->nSectors);
for(i=0, ptr=buf; i<dsh->nSectors; i++, ptr+=2352)
{ int n=LargeRead(*file, ptr, dsh->sectorSize);
if(n != dsh->sectorSize)
Stop(_("Failed reading from defective sector file: %s"), strerror(errno));
}
memset(zero, 0, 296);
dsh->sectorSize = MAX_RAW_TRANSFER_SIZE;
if(!LargeSeek(*file, 0))
Stop(_("Failed seeking in defective sector file: %s"), strerror(errno));
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
n = LargeWrite(*file, dsh, sizeof(DefectiveSectorHeader));
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
if(n != sizeof(DefectiveSectorHeader))
Stop(_("Failed writing to defective sector file: %s"), strerror(errno));
for(i=0, ptr=buf; i<dsh->nSectors; i++, ptr+=2352)
{ n=LargeWrite(*file, ptr, 2352);
if(n != 2352)
Stop(_("Failed writing to defective sector file: %s"), strerror(errno));
n=LargeWrite(*file, zero, 296);
if(n != 296)
Stop(_("Failed writing to defective sector file: %s"), strerror(errno));
}
if(!LargeSeek(*file, sizeof(DefectiveSectorHeader)))
Stop(_("Failed seeking in defective sector file: %s"), strerror(errno));
g_free(buf);
}
/* If the cache file has no fingerprint, add it now */
if(!(dsh->properties & DSH_HAS_FINGERPRINT) && rb->validFP)
{ memcpy(dsh->mediumFP, rb->mediumFP, 16);
dsh->properties |= DSH_HAS_FINGERPRINT;
if(!LargeSeek(*file, 0))
Stop(_("Failed seeking in defective sector file: %s"), strerror(errno));
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
n = LargeWrite(*file, dsh, sizeof(DefectiveSectorHeader));
#ifdef HAVE_BIG_ENDIAN
SwapDefectiveHeaderBytes(dsh);
#endif
if(n != sizeof(DefectiveSectorHeader))
Stop(_("Failed writing to defective sector file: %s"), strerror(errno));
}
/* Verify cache and medium fingerprint */
if((dsh->properties & DSH_HAS_FINGERPRINT) && rb->validFP)
{ if(memcmp(dsh->mediumFP, rb->mediumFP, 16))
Stop(_("Fingerprints of medium and defective sector cache do not match!"));
}
}
/*
* Append RawBuffer contents to defective sector dump
*/
int SaveDefectiveSector(RawBuffer *rb, int can_c2_scan)
{ LargeFile *file;
DefectiveSectorHeader *dsh = alloca(sizeof(DefectiveSectorHeader));
unsigned char *cache_sectors = NULL;
char *filename;
guint64 length,offset;
int count=0;
int i,j,idx;
if(!rb->samplesRead)
return 0; /* Nothing to be done */
/* Open cache file */
filename = g_strdup_printf("%s/%s%lld.raw",
Closure->dDumpDir, Closure->dDumpPrefix,
(long long)rb->lba);
if(!LargeStat(filename, &length))
{ PrintCLIorLabel(Closure->status,_(" [Creating new cache file %s]\n"), filename);
init_defective_sector_file(filename, rb, &file, dsh);
}
else
{ open_defective_sector_file(rb, filename, &file, dsh);
if(!file)
Stop(_("Could not open %s: %s"), filename, strerror(errno));
}
/* Read already cached sectors */
if(dsh->nSectors > 0)
{ if(!LargeSeek(file, sizeof(DefectiveSectorHeader)))
Stop(_("Failed seeking in defective sector file: %s"), strerror(errno));
cache_sectors = g_malloc(dsh->sectorSize*dsh->nSectors);
for(i=0, idx=0; i<dsh->nSectors; i++, idx+=dsh->sectorSize)
{ int n=LargeRead(file, cache_sectors+idx, dsh->sectorSize);
if(n != dsh->sectorSize)
Stop(_("Failed reading from defective sector file: %s"), strerror(errno));
}
}
/* Store sectors which are not already cached */
offset = sizeof(DefectiveSectorHeader) + dsh->sectorSize*dsh->nSectors;
if(!LargeSeek(file, offset))
Stop(_("Failed seeking in defective sector file: %s"), strerror(errno));
for(i=0; i<rb->samplesRead; i++)
{ int new_sector = TRUE;
/* See comment below on C2 mask field to understand rb->sampleSize-1 */
if(cache_sectors) /* Sector already in cache? */
{
for(j=0, idx=0; j<dsh->nSectors; j++, idx+=dsh->sectorSize)
{ if(!memcmp(rb->rawBuf[i], cache_sectors+idx, rb->sampleSize-1))
{ new_sector = FALSE;
break;
}
}
}
for(j=0; j<i; j++) /* Some drives return cached data after first read */
{ if(!memcmp(rb->rawBuf[i], rb->rawBuf[j], rb->sampleSize-1))
{ new_sector = FALSE;
break;
}
}
if(new_sector) /* same sector already in cache */
{ int n;
/* The C2 mask field is not used; so we put a flag into it
to mark raw sectors containing C2 error information. */
if(can_c2_scan)
rb->rawBuf[i][CD_RAW_DUMP_SIZE-1] = 1;
n=LargeWrite(file, rb->rawBuf[i], dsh->sectorSize);
if(n != dsh->sectorSize)
Stop(_("Failed writing to defective sector file: %s"), strerror(errno));
count++;
}
}
LargeClose(file);
PrintCLIorLabel(Closure->status,
_(" [Appended %d/%d sectors to cache file %s; LBA=%lld, ssize=%d, %d sectors]\n"),
count, rb->samplesRead, filename, dsh->lba, dsh->sectorSize, dsh->nSectors);
g_free(filename);
if(cache_sectors)
g_free(cache_sectors);
return count;
}
/*
* Read sectors from the defective sector dump,
* feed them into the raw buffer one by one
* and retry recovery.
*/
int TryDefectiveSectorCache(RawBuffer *rb, unsigned char *outbuf)
{ DefectiveSectorHeader dsh;
LargeFile *file;
char *path;
int status;
int last_sector;
int i;
path = g_strdup_printf("%s/%s%lld.raw",
Closure->dDumpDir, Closure->dDumpPrefix,
(long long)rb->lba);
open_defective_sector_file(rb, path, &file, &dsh);
g_free(path);
if(!file) /* No cache file */
return -1;
/* skip sectors added in current pass */
last_sector = dsh.nSectors - rb->samplesRead;
ReallocRawBuffer(rb, dsh.nSectors);
for(i=0; i<last_sector; i++)
{ int n;
n = LargeRead(file, rb->workBuf->buf, dsh.sectorSize);
if(n != dsh.sectorSize)
Stop(_("Failed reading from defective sector file: %s"), strerror(errno));
status = TryCDFrameRecovery(rb, outbuf);
if(!status)
{ PrintCLIorLabel(Closure->status,
" [Success after processing cached sector %d]\n", i+1);
return status;
}
}
LargeClose(file);
return -1;
}
/*
* Read sectors from the defective sector dump
*/
void ReadDefectiveSectorFile(DefectiveSectorHeader *dsh, RawBuffer *rb, char *path)
{ LargeFile *file;
open_defective_sector_file(rb, path, &file, dsh);
if(!file)
{ Stop(_("Could not open %s: %s"), path, strerror(errno));
return;
}
rb->lba = dsh->lba;
if(dsh->properties & DSH_XA_MODE)
rb->dataOffset = 24;
else rb->dataOffset = 16;
ReallocRawBuffer(rb, dsh->nSectors);
for(rb->samplesRead=0; rb->samplesRead<dsh->nSectors; )
{ int n=LargeRead(file, rb->rawBuf[rb->samplesRead], dsh->sectorSize);
if(n != dsh->sectorSize)
{ Stop(_("Failed reading from defective sector file: %s"), strerror(errno));
return;
}
rb->samplesRead++;
UpdateFrameStats(rb);
CollectGoodVectors(rb);
}
LargeClose(file);
}
|