OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2009-2014 Travis Geiselbrecht | 2 * Copyright (c) 2009-2014 Travis Geiselbrecht |
3 * | 3 * |
4 * Permission is hereby granted, free of charge, to any person obtaining | 4 * Permission is hereby granted, free of charge, to any person obtaining |
5 * a copy of this software and associated documentation files | 5 * a copy of this software and associated documentation files |
6 * (the "Software"), to deal in the Software without restriction, | 6 * (the "Software"), to deal in the Software without restriction, |
7 * including without limitation the rights to use, copy, modify, merge, | 7 * including without limitation the rights to use, copy, modify, merge, |
8 * publish, distribute, sublicense, and/or sell copies of the Software, | 8 * publish, distribute, sublicense, and/or sell copies of the Software, |
9 * and to permit persons to whom the Software is furnished to do so, | 9 * and to permit persons to whom the Software is furnished to do so, |
10 * subject to the following conditions: | 10 * subject to the following conditions: |
(...skipping 18 matching lines...) Expand all Loading... |
29 #include <lib/console.h> | 29 #include <lib/console.h> |
30 #include <lib/bio.h> | 30 #include <lib/bio.h> |
31 #include <lib/partition.h> | 31 #include <lib/partition.h> |
32 #include <platform.h> | 32 #include <platform.h> |
33 | 33 |
34 #if WITH_LIB_CKSUM | 34 #if WITH_LIB_CKSUM |
35 #include <lib/cksum.h> | 35 #include <lib/cksum.h> |
36 #endif | 36 #endif |
37 | 37 |
38 #define DMA_ALIGNMENT (CACHE_LINE) | 38 #define DMA_ALIGNMENT (CACHE_LINE) |
| 39 #define THREE_BYTE_ADDR_BOUNDARY (16777216) |
| 40 #define SUB_ERASE_TEST_SAMPLES (32) |
39 | 41 |
40 #if defined(WITH_LIB_CONSOLE) | 42 #if defined(WITH_LIB_CONSOLE) |
41 | 43 |
42 #if LK_DEBUGLEVEL > 0 | 44 #if LK_DEBUGLEVEL > 0 |
43 static int cmd_bio(int argc, const cmd_args *argv); | 45 static int cmd_bio(int argc, const cmd_args *argv); |
44 static int bio_test_device(bdev_t* device); | 46 static int bio_test_device(bdev_t* device); |
45 | 47 |
46 STATIC_COMMAND_START | 48 STATIC_COMMAND_START |
47 STATIC_COMMAND("bio", "block io debug commands", &cmd_bio) | 49 STATIC_COMMAND("bio", "block io debug commands", &cmd_bio) |
48 STATIC_COMMAND_END(bio); | 50 STATIC_COMMAND_END(bio); |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
326 printf("validating erase...\n"); | 328 printf("validating erase...\n"); |
327 size_t num_invalid_blocks = 0; | 329 size_t num_invalid_blocks = 0; |
328 for (bnum_t bnum = 0; bnum < device->block_count; bnum++) { | 330 for (bnum_t bnum = 0; bnum < device->block_count; bnum++) { |
329 if (!is_valid_block(device, bnum, &device->erase_byte, sizeof(device->er
ase_byte))) { | 331 if (!is_valid_block(device, bnum, &device->erase_byte, sizeof(device->er
ase_byte))) { |
330 num_invalid_blocks++; | 332 num_invalid_blocks++; |
331 } | 333 } |
332 } | 334 } |
333 return num_invalid_blocks; | 335 return num_invalid_blocks; |
334 } | 336 } |
335 | 337 |
| 338 static bool test_erase_block(bdev_t* device, uint32_t block_addr) |
| 339 { |
| 340 bool success = false; |
| 341 uint8_t valid_byte[1]; |
| 342 |
| 343 uint8_t *block_contents = memalign(DMA_ALIGNMENT, device->block_size); |
| 344 memset(block_contents, ~(device->erase_byte), device->block_size); |
| 345 |
| 346 ssize_t err = bio_write_block(device, block_contents, block_addr, 1); |
| 347 if (err != (ssize_t)device->block_size) { |
| 348 goto finish; |
| 349 } |
| 350 |
| 351 valid_byte[0] = ~(device->erase_byte); |
| 352 if (!is_valid_block(device, block_addr, valid_byte, 1)) { |
| 353 goto finish; |
| 354 } |
| 355 |
| 356 err = bio_erase(device, block_addr * device->block_size, 1); |
| 357 if (err <= 0) { |
| 358 goto finish; |
| 359 } |
| 360 |
| 361 valid_byte[0] = device->erase_byte; |
| 362 if (is_valid_block(device, block_addr, valid_byte, 1)) { |
| 363 success = true; |
| 364 } |
| 365 |
| 366 finish: |
| 367 free(block_contents); |
| 368 return success; |
| 369 } |
| 370 |
| 371 // Ensure that (sub)sector erase work. |
| 372 static bool sub_erase_test(bdev_t* device, uint32_t n_samples) |
| 373 { |
| 374 printf("Sampling the device %d times.\n", n_samples); |
| 375 for (uint32_t i = 0; i < n_samples; i++) { |
| 376 bnum_t block_addr = rand() % device->block_count; |
| 377 if (!test_erase_block(device, block_addr)) { |
| 378 return false; |
| 379 } |
| 380 } |
| 381 return true; |
| 382 } |
| 383 |
336 static uint8_t get_signature(uint32_t word) | 384 static uint8_t get_signature(uint32_t word) |
337 { | 385 { |
338 uint8_t* sigptr = (uint8_t*)(&word); | 386 uint8_t* sigptr = (uint8_t*)(&word); |
339 return sigptr[0] ^ sigptr[1] ^ sigptr[2] ^ sigptr[3]; | 387 return sigptr[0] ^ sigptr[1] ^ sigptr[2] ^ sigptr[3]; |
340 } | 388 } |
341 | 389 |
342 // returns the number of blocks where the write was not successful. | 390 // returns the number of blocks where the write was not successful. |
343 static ssize_t write_test(bdev_t *device) | 391 static ssize_t write_test(bdev_t *device) |
344 { | 392 { |
345 uint8_t *test_buffer = memalign(DMA_ALIGNMENT, device->block_size); | 393 uint8_t *test_buffer = memalign(DMA_ALIGNMENT, device->block_size); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 printf("not continuing to test writes.\n"); | 428 printf("not continuing to test writes.\n"); |
381 return -1; | 429 return -1; |
382 } | 430 } |
383 | 431 |
384 num_errors = write_test(device); | 432 num_errors = write_test(device); |
385 printf("Discovered %ld error(s) while testing write.\n", num_errors); | 433 printf("Discovered %ld error(s) while testing write.\n", num_errors); |
386 if (num_errors) { | 434 if (num_errors) { |
387 return -1; | 435 return -1; |
388 } | 436 } |
389 | 437 |
| 438 printf ("Testing sub-erase...\n"); |
| 439 bool success = sub_erase_test(device, SUB_ERASE_TEST_SAMPLES); |
| 440 if (!success) { |
| 441 printf("Discovered errors while testing sub-erase.\n"); |
| 442 return -1; |
| 443 } else { |
| 444 printf("No errors while testing sub-erase.\n"); |
| 445 } |
| 446 |
390 return 0; | 447 return 0; |
391 } | 448 } |
OLD | NEW |