2011-12-06 02:23:28 +00:00
|
|
|
/*
|
|
|
|
* badblocks.c - Bad blocks checker
|
|
|
|
*
|
|
|
|
* Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
|
|
|
|
* Laboratoire MASI, Institut Blaise Pascal
|
|
|
|
* Universite Pierre et Marie Curie (Paris VI)
|
|
|
|
*
|
|
|
|
* Copyright 1995, 1996, 1997, 1998, 1999 by Theodore Ts'o
|
|
|
|
* Copyright 1999 by David Beattie
|
2016-02-20 22:52:32 +00:00
|
|
|
* Copyright 2011-2016 by Pete Batard
|
2011-12-06 02:23:28 +00:00
|
|
|
*
|
|
|
|
* This file is based on the minix file system programs fsck and mkfs
|
|
|
|
* written and copyrighted by Linus Torvalds <Linus.Torvalds@cs.helsinki.fi>
|
|
|
|
*
|
|
|
|
* %Begin-Header%
|
|
|
|
* This file may be redistributed under the terms of the GNU Public
|
|
|
|
* License.
|
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* History:
|
|
|
|
* 93/05/26 - Creation from e2fsck
|
|
|
|
* 94/02/27 - Made a separate bad blocks checker
|
|
|
|
* 99/06/30...99/07/26 - Added non-destructive write-testing,
|
|
|
|
* configurable blocks-at-once parameter,
|
|
|
|
* loading of badblocks list to avoid testing
|
|
|
|
* blocks known to be bad, multiple passes to
|
|
|
|
* make sure that no new blocks are added to the
|
|
|
|
* list. (Work done by David Beattie)
|
|
|
|
* 11/12/04 - Windows/Rufus integration (Pete Batard)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <windows.h>
|
2012-03-03 22:59:58 +00:00
|
|
|
#include <stdint.h>
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
#include "rufus.h"
|
2013-10-15 21:58:27 +00:00
|
|
|
#include "resource.h"
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "msapi_utf8.h"
|
2013-10-15 21:58:27 +00:00
|
|
|
#include "localization.h"
|
2011-12-30 21:23:13 +00:00
|
|
|
|
2016-02-20 22:52:32 +00:00
|
|
|
#include "badblocks.h"
|
|
|
|
#include "file.h"
|
|
|
|
|
2011-12-30 21:23:13 +00:00
|
|
|
FILE* log_fd = NULL;
|
|
|
|
static const char* abort_msg = "Too many bad blocks, aborting test\n";
|
2012-03-03 22:59:58 +00:00
|
|
|
static const char* bb_prefix = "Bad Blocks: ";
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
/*
|
2011-12-06 14:05:53 +00:00
|
|
|
*From e2fsprogs/lib/ext2fs/badblocks.c
|
2011-12-06 02:23:28 +00:00
|
|
|
*/
|
2011-12-06 18:08:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Badblocks list
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
struct bb_struct_u64_list {
|
2011-12-06 18:08:16 +00:00
|
|
|
int magic;
|
|
|
|
int num;
|
|
|
|
int size;
|
2012-03-03 22:59:58 +00:00
|
|
|
uint64_t *list;
|
2011-12-06 18:08:16 +00:00
|
|
|
int badblocks_flags;
|
|
|
|
};
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
struct bb_struct_u64_iterate {
|
|
|
|
int magic;
|
|
|
|
bb_u64_list bb;
|
|
|
|
int ptr;
|
2011-12-06 18:08:16 +00:00
|
|
|
};
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
static errcode_t make_u64_list(int size, int num, uint64_t *list, bb_u64_list *ret)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
bb_u64_list bb;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
bb = calloc(1, sizeof(struct bb_struct_u64_list));
|
2011-12-06 02:23:28 +00:00
|
|
|
if (bb == NULL)
|
2012-03-03 22:59:58 +00:00
|
|
|
return BB_ET_NO_MEMORY;
|
|
|
|
bb->magic = BB_ET_MAGIC_BADBLOCKS_LIST;
|
2011-12-06 02:23:28 +00:00
|
|
|
bb->size = size ? size : 10;
|
|
|
|
bb->num = num;
|
|
|
|
bb->list = malloc(sizeof(blk_t) * bb->size);
|
|
|
|
if (bb->list == NULL) {
|
|
|
|
free(bb);
|
|
|
|
bb = NULL;
|
2012-03-03 22:59:58 +00:00
|
|
|
return BB_ET_NO_MEMORY;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
if (list)
|
|
|
|
memcpy(bb->list, list, bb->size * sizeof(blk_t));
|
|
|
|
else
|
|
|
|
memset(bb->list, 0, bb->size * sizeof(blk_t));
|
|
|
|
*ret = bb;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This procedure creates an empty badblocks list.
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
static errcode_t bb_badblocks_list_create(bb_badblocks_list *ret, int size)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
return make_u64_list(size, 0, 0, (bb_badblocks_list *) ret);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This procedure adds a block to a badblocks list.
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
static errcode_t bb_u64_list_add(bb_u64_list bb, uint64_t blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
|
|
|
int i, j;
|
2012-03-03 22:59:58 +00:00
|
|
|
uint64_t* old_bb_list = bb->list;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
BB_CHECK_MAGIC(bb, BB_ET_MAGIC_BADBLOCKS_LIST);
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
if (bb->num >= bb->size) {
|
|
|
|
bb->size += 100;
|
2012-03-03 22:59:58 +00:00
|
|
|
bb->list = realloc(bb->list, bb->size * sizeof(uint64_t));
|
2011-12-06 02:23:28 +00:00
|
|
|
if (bb->list == NULL) {
|
|
|
|
bb->list = old_bb_list;
|
|
|
|
bb->size -= 100;
|
2012-03-03 22:59:58 +00:00
|
|
|
return BB_ET_NO_MEMORY;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
2013-01-09 21:54:28 +00:00
|
|
|
memset(&bb->list[bb->size-100], 0, 100 * sizeof(uint64_t));
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add special case code for appending to the end of the list
|
|
|
|
*/
|
|
|
|
i = bb->num-1;
|
|
|
|
if ((bb->num != 0) && (bb->list[i] == blk))
|
|
|
|
return 0;
|
|
|
|
if ((bb->num == 0) || (bb->list[i] < blk)) {
|
|
|
|
bb->list[bb->num++] = blk;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
j = bb->num;
|
|
|
|
for (i=0; i < bb->num; i++) {
|
|
|
|
if (bb->list[i] == blk)
|
|
|
|
return 0;
|
|
|
|
if (bb->list[i] > blk) {
|
|
|
|
j = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i=bb->num; i > j; i--)
|
|
|
|
bb->list[i] = bb->list[i-1];
|
|
|
|
bb->list[j] = blk;
|
|
|
|
bb->num++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
static errcode_t bb_badblocks_list_add(bb_badblocks_list bb, blk_t blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
return bb_u64_list_add((bb_u64_list) bb, blk);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This procedure finds a particular block is on a badblocks
|
|
|
|
* list.
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
static int bb_u64_list_find(bb_u64_list bb, uint64_t blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
|
|
|
int low, high, mid;
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if (bb->magic != BB_ET_MAGIC_BADBLOCKS_LIST)
|
2011-12-06 02:23:28 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (bb->num == 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
low = 0;
|
|
|
|
high = bb->num-1;
|
|
|
|
if (blk == bb->list[low])
|
|
|
|
return low;
|
|
|
|
if (blk == bb->list[high])
|
|
|
|
return high;
|
|
|
|
|
|
|
|
while (low < high) {
|
|
|
|
mid = ((unsigned)low + (unsigned)high)/2;
|
|
|
|
if (mid == low || mid == high)
|
|
|
|
break;
|
|
|
|
if (blk == bb->list[mid])
|
|
|
|
return mid;
|
|
|
|
if (blk < bb->list[mid])
|
|
|
|
high = mid;
|
|
|
|
else
|
|
|
|
low = mid;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This procedure tests to see if a particular block is on a badblocks
|
|
|
|
* list.
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
static int bb_u64_list_test(bb_u64_list bb, uint64_t blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
if (bb_u64_list_find(bb, blk) < 0)
|
2011-12-06 02:23:28 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
static int bb_badblocks_list_test(bb_badblocks_list bb, blk_t blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
return bb_u64_list_test((bb_u64_list) bb, blk);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
static int bb_u64_list_iterate(bb_u64_iterate iter, uint64_t *blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
bb_u64_list bb;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if (iter->magic != BB_ET_MAGIC_BADBLOCKS_ITERATE)
|
2011-12-06 02:23:28 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
bb = iter->bb;
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if (bb->magic != BB_ET_MAGIC_BADBLOCKS_LIST)
|
2011-12-06 02:23:28 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (iter->ptr < bb->num) {
|
|
|
|
*blk = bb->list[iter->ptr++];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
*blk = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
static int bb_badblocks_list_iterate(bb_badblocks_iterate iter, blk_t *blk)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
return bb_u64_list_iterate((bb_u64_iterate) iter, blk);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2011-12-06 14:05:53 +00:00
|
|
|
* from e2fsprogs/misc/badblocks.c
|
2011-12-06 02:23:28 +00:00
|
|
|
*/
|
2011-12-06 23:35:55 +00:00
|
|
|
static int v_flag = 1; /* verbose */
|
2011-12-06 18:08:16 +00:00
|
|
|
static int s_flag = 1; /* show progress of test */
|
2011-12-06 23:35:55 +00:00
|
|
|
static int cancel_ops = 0; /* abort current operation */
|
2011-12-07 00:28:27 +00:00
|
|
|
static int cur_pattern, nr_pattern;
|
|
|
|
static int cur_op;
|
2011-12-06 18:08:16 +00:00
|
|
|
/* Abort test if more than this number of bad blocks has been encountered */
|
2012-03-03 22:59:58 +00:00
|
|
|
static unsigned int max_bb = BB_BAD_BLOCKS_THRESHOLD;
|
2011-12-06 02:23:28 +00:00
|
|
|
static blk_t currently_testing = 0;
|
|
|
|
static blk_t num_blocks = 0;
|
2012-03-03 22:59:58 +00:00
|
|
|
static uint32_t num_read_errors = 0;
|
|
|
|
static uint32_t num_write_errors = 0;
|
|
|
|
static uint32_t num_corruption_errors = 0;
|
|
|
|
static bb_badblocks_list bb_list = NULL;
|
2011-12-06 02:23:28 +00:00
|
|
|
static blk_t next_bad = 0;
|
2012-03-03 22:59:58 +00:00
|
|
|
static bb_badblocks_iterate bb_iter = NULL;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
static __inline void *allocate_buffer(size_t size) {
|
2011-12-06 14:05:53 +00:00
|
|
|
#ifdef __MINGW32__
|
2012-03-03 22:59:58 +00:00
|
|
|
return __mingw_aligned_malloc(size, BB_SYS_PAGE_SIZE);
|
2016-02-20 22:52:32 +00:00
|
|
|
#else
|
2012-03-03 22:59:58 +00:00
|
|
|
return _aligned_malloc(size, BB_SYS_PAGE_SIZE);
|
2011-12-06 14:05:53 +00:00
|
|
|
#endif
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static __inline void free_buffer(void* p) {
|
2011-12-06 14:05:53 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
__mingw_aligned_free(p);
|
|
|
|
#else
|
2011-12-06 02:23:28 +00:00
|
|
|
_aligned_free(p);
|
2011-12-06 14:05:53 +00:00
|
|
|
#endif
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine reports a new bad block. If the bad block has already
|
|
|
|
* been seen before, then it returns 0; otherwise it returns 1.
|
|
|
|
*/
|
|
|
|
static int bb_output (blk_t bad, enum error_types error_type)
|
|
|
|
{
|
2011-12-06 14:05:53 +00:00
|
|
|
errcode_t error_code;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
if (bb_badblocks_list_test(bb_list, bad))
|
2011-12-06 02:23:28 +00:00
|
|
|
return 0;
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%s%lu\n", bb_prefix, (unsigned long)bad);
|
2011-12-30 21:23:13 +00:00
|
|
|
fprintf(log_fd, "Block %lu: %s error\n", (unsigned long)bad, (error_type==READ_ERROR)?"read":
|
|
|
|
((error_type == WRITE_ERROR)?"write":"corruption"));
|
|
|
|
fflush(log_fd);
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
error_code = bb_badblocks_list_add(bb_list, bad);
|
2011-12-06 14:05:53 +00:00
|
|
|
if (error_code) {
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sError %d adding to in-memory bad block list", bb_prefix, error_code);
|
2011-12-06 18:08:16 +00:00
|
|
|
return 0;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* kludge:
|
|
|
|
increment the iteration through the bb_list if
|
|
|
|
an element was just added before the current iteration
|
|
|
|
position. This should not cause next_bad to change. */
|
|
|
|
if (bb_iter && bad < next_bad)
|
2012-03-03 22:59:58 +00:00
|
|
|
bb_badblocks_list_iterate (bb_iter, &next_bad);
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
if (error_type == READ_ERROR) {
|
|
|
|
num_read_errors++;
|
|
|
|
} else if (error_type == WRITE_ERROR) {
|
|
|
|
num_write_errors++;
|
|
|
|
} else if (error_type == CORRUPTION_ERROR) {
|
|
|
|
num_corruption_errors++;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static float calc_percent(unsigned long current, unsigned long total) {
|
|
|
|
float percent = 0.0;
|
|
|
|
if (total <= 0)
|
|
|
|
return percent;
|
|
|
|
if (current >= total) {
|
|
|
|
percent = 100.0f;
|
|
|
|
} else {
|
|
|
|
percent=(100.0f*(float)current/(float)total);
|
|
|
|
}
|
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_status(void)
|
|
|
|
{
|
2011-12-06 23:35:55 +00:00
|
|
|
float percent;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2011-12-06 23:35:55 +00:00
|
|
|
percent = calc_percent((unsigned long) currently_testing,
|
|
|
|
(unsigned long) num_blocks);
|
2015-01-01 23:39:28 +00:00
|
|
|
PrintInfo(0, MSG_235, lmprintf(MSG_191 + ((cur_op==OP_WRITE)?0:1)),
|
2011-12-08 00:22:13 +00:00
|
|
|
cur_pattern, nr_pattern,
|
2013-10-15 21:58:27 +00:00
|
|
|
percent,
|
2011-12-06 23:35:55 +00:00
|
|
|
num_read_errors,
|
|
|
|
num_write_errors,
|
2013-12-19 23:56:40 +00:00
|
|
|
num_corruption_errors);
|
2015-01-01 23:39:28 +00:00
|
|
|
percent = (percent/2.0f) + ((cur_op==OP_READ)? 50.0f : 0.0f);
|
2011-12-09 01:39:13 +00:00
|
|
|
UpdateProgress(OP_BADBLOCKS, (((cur_pattern-1)*100.0f) + percent) / nr_pattern);
|
2011-12-06 23:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CALLBACK alarm_intr(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
|
|
|
|
{
|
|
|
|
if (!num_blocks)
|
|
|
|
return;
|
|
|
|
if (FormatStatus) {
|
2015-01-28 23:22:11 +00:00
|
|
|
uprintf("%sInterrupting at block %" PRIu64 "\n", bb_prefix,
|
2011-12-06 23:35:55 +00:00
|
|
|
(unsigned long long) currently_testing);
|
|
|
|
cancel_ops = -1;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
2011-12-06 23:35:55 +00:00
|
|
|
print_status();
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pattern_fill(unsigned char *buffer, unsigned int pattern,
|
|
|
|
size_t n)
|
|
|
|
{
|
|
|
|
unsigned int i, nb;
|
|
|
|
unsigned char bpattern[sizeof(pattern)], *ptr;
|
|
|
|
|
|
|
|
if (pattern == (unsigned int) ~0) {
|
|
|
|
for (ptr = buffer; ptr < buffer + n; ptr++) {
|
|
|
|
(*ptr) = rand() % (1 << (8 * sizeof(char)));
|
|
|
|
}
|
2015-01-01 23:39:28 +00:00
|
|
|
PrintInfo(3500, MSG_236);
|
2011-12-06 02:23:28 +00:00
|
|
|
} else {
|
|
|
|
bpattern[0] = 0;
|
|
|
|
for (i = 0; i < sizeof(bpattern); i++) {
|
|
|
|
if (pattern == 0)
|
|
|
|
break;
|
|
|
|
bpattern[i] = pattern & 0xFF;
|
|
|
|
pattern = pattern >> 8;
|
|
|
|
}
|
|
|
|
nb = i ? (i-1) : 0;
|
|
|
|
for (ptr = buffer, i = nb; ptr < buffer + n; ptr++) {
|
|
|
|
*ptr = bpattern[i];
|
|
|
|
if (i == 0)
|
|
|
|
i = nb;
|
|
|
|
else
|
|
|
|
i--;
|
|
|
|
}
|
2015-01-01 23:39:28 +00:00
|
|
|
PrintInfo(3500, MSG_237, bpattern[i]);
|
2011-12-07 00:28:27 +00:00
|
|
|
cur_pattern++;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a read of a sequence of blocks; return the number of blocks
|
|
|
|
* successfully sequentially read.
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
static int64_t do_read (HANDLE hDrive, unsigned char * buffer, uint64_t tryout, uint64_t block_size,
|
2011-12-06 02:23:28 +00:00
|
|
|
blk_t current_block)
|
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
int64_t got;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
if (v_flag > 1)
|
|
|
|
print_status();
|
|
|
|
|
|
|
|
/* Try the read */
|
|
|
|
got = read_sectors(hDrive, block_size, current_block, tryout, buffer);
|
|
|
|
if (got < 0)
|
|
|
|
got = 0;
|
|
|
|
if (got & 511)
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sWeird value (%ld) in do_read\n", bb_prefix, got);
|
2011-12-06 02:23:28 +00:00
|
|
|
got /= block_size;
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform a write of a sequence of blocks; return the number of blocks
|
|
|
|
* successfully sequentially written.
|
|
|
|
*/
|
2012-03-03 22:59:58 +00:00
|
|
|
static int64_t do_write(HANDLE hDrive, unsigned char * buffer, uint64_t tryout, uint64_t block_size,
|
|
|
|
blk_t current_block)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-03-03 22:59:58 +00:00
|
|
|
int64_t got;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
|
|
|
if (v_flag > 1)
|
|
|
|
print_status();
|
|
|
|
|
|
|
|
/* Try the write */
|
|
|
|
got = write_sectors(hDrive, block_size, current_block, tryout, buffer);
|
|
|
|
if (got < 0)
|
|
|
|
got = 0;
|
|
|
|
if (got & 511)
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sWeird value (%ld) in do_write\n", bb_prefix, got);
|
2011-12-06 02:23:28 +00:00
|
|
|
got /= block_size;
|
|
|
|
return got;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
static unsigned int test_rw(HANDLE hDrive, blk_t last_block, size_t block_size, blk_t first_block,
|
|
|
|
size_t blocks_at_once, int nb_passes)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2012-01-08 23:41:20 +00:00
|
|
|
unsigned char *buffer = NULL, *read_buffer;
|
2013-10-15 21:58:27 +00:00
|
|
|
const unsigned int pattern[] = BADBLOCK_PATTERNS;
|
2012-03-03 22:59:58 +00:00
|
|
|
int i, pat_idx;
|
2011-12-06 02:23:28 +00:00
|
|
|
unsigned int bb_count = 0;
|
2012-03-03 22:59:58 +00:00
|
|
|
blk_t got, tryout, recover_block = ~0, *blk_id;
|
2012-02-24 19:44:24 +00:00
|
|
|
size_t id_offset;
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-01-08 23:41:20 +00:00
|
|
|
if ((nb_passes < 1) || (nb_passes > 4)) {
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sInvalid number of passes\n", bb_prefix);
|
2011-12-06 23:35:55 +00:00
|
|
|
cancel_ops = -1;
|
2011-12-06 02:23:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer = allocate_buffer(2 * blocks_at_once * block_size);
|
|
|
|
read_buffer = buffer + blocks_at_once * block_size;
|
|
|
|
|
|
|
|
if (!buffer) {
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sError while allocating buffers\n", bb_prefix);
|
2011-12-06 23:35:55 +00:00
|
|
|
cancel_ops = -1;
|
2011-12-06 02:23:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sChecking from block %lu to %lu\n", bb_prefix,
|
|
|
|
(unsigned long) first_block, (unsigned long) last_block - 1);
|
2012-01-08 23:41:20 +00:00
|
|
|
nr_pattern = nb_passes;
|
2011-12-07 00:28:27 +00:00
|
|
|
cur_pattern = 0;
|
2011-12-06 23:35:55 +00:00
|
|
|
|
2012-01-08 23:41:20 +00:00
|
|
|
for (pat_idx = 0; pat_idx < nb_passes; pat_idx++) {
|
2011-12-06 23:35:55 +00:00
|
|
|
if (cancel_ops) goto out;
|
2015-02-03 23:42:27 +00:00
|
|
|
id_offset = rand() * (block_size-sizeof(blk_t)) / RAND_MAX;
|
2011-12-06 02:23:28 +00:00
|
|
|
pattern_fill(buffer, pattern[pat_idx], blocks_at_once * block_size);
|
2012-11-03 17:22:28 +00:00
|
|
|
uprintf("%sUsing offset %d for fake device check\n", bb_prefix, id_offset);
|
2011-12-06 02:23:28 +00:00
|
|
|
num_blocks = last_block - 1;
|
|
|
|
currently_testing = first_block;
|
|
|
|
if (s_flag | v_flag)
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sWriting test pattern 0x%02X\n", bb_prefix, pattern[pat_idx]);
|
2011-12-07 00:28:27 +00:00
|
|
|
cur_op = OP_WRITE;
|
2011-12-06 02:23:28 +00:00
|
|
|
tryout = blocks_at_once;
|
|
|
|
while (currently_testing < last_block) {
|
2012-03-03 22:59:58 +00:00
|
|
|
if (cancel_ops) goto out;
|
2011-12-06 02:23:28 +00:00
|
|
|
if (max_bb && bb_count >= max_bb) {
|
|
|
|
if (s_flag || v_flag) {
|
2011-12-30 21:23:13 +00:00
|
|
|
uprintf(abort_msg);
|
|
|
|
fprintf(log_fd, abort_msg);
|
|
|
|
fflush(log_fd);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
2011-12-06 23:35:55 +00:00
|
|
|
cancel_ops = -1;
|
2012-03-03 22:59:58 +00:00
|
|
|
goto out;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
if (currently_testing + tryout > last_block)
|
|
|
|
tryout = last_block - currently_testing;
|
2012-05-16 22:38:39 +00:00
|
|
|
if (detect_fakes) {
|
|
|
|
/* Add the block number at a fixed (random) offset during each pass to
|
|
|
|
allow for the detection of 'fake' media (eg. 2GB USB masquerading as 16GB) */
|
|
|
|
for (i=0; i<(int)blocks_at_once; i++) {
|
|
|
|
blk_id = (blk_t*)(intptr_t)(buffer + id_offset+ i*block_size);
|
|
|
|
*blk_id = (blk_t)(currently_testing + i);
|
|
|
|
}
|
2012-02-24 19:44:24 +00:00
|
|
|
}
|
2011-12-06 02:23:28 +00:00
|
|
|
got = do_write(hDrive, buffer, tryout, block_size, currently_testing);
|
|
|
|
if (v_flag > 1)
|
|
|
|
print_status();
|
|
|
|
|
|
|
|
if (got == 0 && tryout == 1)
|
|
|
|
bb_count += bb_output(currently_testing++, WRITE_ERROR);
|
|
|
|
currently_testing += got;
|
|
|
|
if (got != tryout) {
|
|
|
|
tryout = 1;
|
|
|
|
if (recover_block == ~0)
|
|
|
|
recover_block = currently_testing -
|
|
|
|
got + blocks_at_once;
|
|
|
|
continue;
|
|
|
|
} else if (currently_testing == recover_block) {
|
|
|
|
tryout = blocks_at_once;
|
|
|
|
recover_block = ~0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
num_blocks = 0;
|
|
|
|
if (s_flag | v_flag)
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sReading and comparing\n", bb_prefix);
|
2011-12-07 00:28:27 +00:00
|
|
|
cur_op = OP_READ;
|
2011-12-06 02:23:28 +00:00
|
|
|
num_blocks = last_block;
|
|
|
|
currently_testing = first_block;
|
|
|
|
|
|
|
|
tryout = blocks_at_once;
|
|
|
|
while (currently_testing < last_block) {
|
2011-12-06 23:35:55 +00:00
|
|
|
if (cancel_ops) goto out;
|
2011-12-06 02:23:28 +00:00
|
|
|
if (max_bb && bb_count >= max_bb) {
|
|
|
|
if (s_flag || v_flag) {
|
2011-12-30 21:23:13 +00:00
|
|
|
uprintf(abort_msg);
|
|
|
|
fprintf(log_fd, abort_msg);
|
|
|
|
fflush(log_fd);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
2012-03-03 22:59:58 +00:00
|
|
|
cancel_ops = -1;
|
|
|
|
goto out;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
if (currently_testing + tryout > last_block)
|
|
|
|
tryout = last_block - currently_testing;
|
2012-05-16 22:38:39 +00:00
|
|
|
if (detect_fakes) {
|
|
|
|
for (i=0; i<(int)blocks_at_once; i++) {
|
|
|
|
blk_id = (blk_t*)(intptr_t)(buffer + id_offset+ i*block_size);
|
|
|
|
*blk_id = (blk_t)(currently_testing + i);
|
|
|
|
}
|
2012-02-24 19:44:24 +00:00
|
|
|
}
|
2011-12-06 02:23:28 +00:00
|
|
|
got = do_read(hDrive, read_buffer, tryout, block_size,
|
|
|
|
currently_testing);
|
|
|
|
if (got == 0 && tryout == 1)
|
|
|
|
bb_count += bb_output(currently_testing++, READ_ERROR);
|
|
|
|
currently_testing += got;
|
|
|
|
if (got != tryout) {
|
|
|
|
tryout = 1;
|
|
|
|
if (recover_block == ~0)
|
|
|
|
recover_block = currently_testing -
|
|
|
|
got + blocks_at_once;
|
|
|
|
continue;
|
|
|
|
} else if (currently_testing == recover_block) {
|
|
|
|
tryout = blocks_at_once;
|
|
|
|
recover_block = ~0;
|
|
|
|
}
|
|
|
|
for (i=0; i < got; i++) {
|
|
|
|
if (memcmp(read_buffer + i * block_size,
|
|
|
|
buffer + i * block_size,
|
|
|
|
block_size))
|
2012-03-03 22:59:58 +00:00
|
|
|
bb_count += bb_output(currently_testing+i-got, CORRUPTION_ERROR);
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
if (v_flag > 1)
|
|
|
|
print_status();
|
|
|
|
}
|
|
|
|
|
|
|
|
num_blocks = 0;
|
|
|
|
}
|
2011-12-06 23:35:55 +00:00
|
|
|
out:
|
2011-12-06 02:23:28 +00:00
|
|
|
free_buffer(buffer);
|
|
|
|
return bb_count;
|
|
|
|
}
|
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
BOOL BadBlocks(HANDLE hPhysicalDrive, ULONGLONG disk_size, size_t block_size,
|
2012-01-08 23:41:20 +00:00
|
|
|
int nb_passes, badblocks_report *report, FILE* fd)
|
2011-12-06 02:23:28 +00:00
|
|
|
{
|
2011-12-06 14:05:53 +00:00
|
|
|
errcode_t error_code;
|
2012-03-03 22:59:58 +00:00
|
|
|
blk_t first_block = 0, last_block = disk_size/block_size;
|
2011-12-06 23:35:55 +00:00
|
|
|
|
|
|
|
if (report == NULL) return FALSE;
|
2012-03-29 19:27:53 +00:00
|
|
|
num_read_errors = 0;
|
|
|
|
num_write_errors = 0;
|
|
|
|
num_corruption_errors = 0;
|
2011-12-06 23:35:55 +00:00
|
|
|
report->bb_count = 0;
|
2011-12-30 21:23:13 +00:00
|
|
|
if (fd != NULL) {
|
|
|
|
log_fd = fd;
|
|
|
|
} else {
|
|
|
|
log_fd = freopen(NULL, "w", stderr);
|
|
|
|
}
|
2011-12-06 02:23:28 +00:00
|
|
|
|
2012-03-03 22:59:58 +00:00
|
|
|
error_code = bb_badblocks_list_create(&bb_list, 0);
|
2011-12-06 14:05:53 +00:00
|
|
|
if (error_code) {
|
2012-03-03 22:59:58 +00:00
|
|
|
uprintf("%sError %d while creating in-memory bad blocks list", bb_prefix, error_code);
|
2011-12-06 23:35:55 +00:00
|
|
|
return FALSE;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|
|
|
|
|
2011-12-06 23:35:55 +00:00
|
|
|
cancel_ops = 0;
|
|
|
|
/* use a timer to update status every second */
|
2011-12-09 01:39:13 +00:00
|
|
|
SetTimer(hMainDialog, TID_BADBLOCKS_UPDATE, 1000, alarm_intr);
|
2012-03-03 22:59:58 +00:00
|
|
|
report->bb_count = test_rw(hPhysicalDrive, last_block, block_size, first_block, BB_BLOCKS_AT_ONCE, nb_passes);
|
2011-12-09 01:39:13 +00:00
|
|
|
KillTimer(hMainDialog, TID_BADBLOCKS_UPDATE);
|
2011-12-06 02:23:28 +00:00
|
|
|
free(bb_list->list);
|
|
|
|
free(bb_list);
|
2011-12-06 23:35:55 +00:00
|
|
|
report->num_read_errors = num_read_errors;
|
|
|
|
report->num_write_errors = num_write_errors;
|
|
|
|
report->num_corruption_errors = num_corruption_errors;
|
|
|
|
|
2011-12-08 12:14:21 +00:00
|
|
|
if ((cancel_ops) && (!report->bb_count))
|
2011-12-06 23:35:55 +00:00
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
2011-12-06 02:23:28 +00:00
|
|
|
}
|