flashrom: Don't exit() after successful erase
Makes -Er erased.bin, -Ev erased.bin and -Ew some.bin work, though -Ew is
redundant because most if not all flash drivers erase in the write functions.
With this patch, flashrom also verifies that the erase actually succeeded,
by reading back the flash contents and checking that all bytes are 0xff.
Previously, flashrom would exit 0 even when erase failed.
Signed-off-by: Peter Stuge <peter@stuge.se>
|
|
|
|
| 247 | 247 | { |
| 248 | 248 | uint8_t *buf; |
| 249 | 249 | unsigned long size; |
| | 250 | size_t erasedbytes; |
| 250 | 251 | FILE *image; |
| 251 | 252 | /* Probe for up to three flash chips. */ |
| 252 | 253 | struct flashchip *flash, *flashes[3]; |
| … |
… |
|
| 517 | 518 | } |
| 518 | 519 | |
| 519 | 520 | size = flash->total_size * 1024; |
| 520 | | buf = (uint8_t *) calloc(size, sizeof(char)); |
| | 521 | buf = (uint8_t *) calloc(size + 1, sizeof(char)); |
| 521 | 522 | |
| 522 | 523 | if (erase_it) { |
| 523 | | printf("Erasing flash chip.\n"); |
| | 524 | printf("Erasing flash chip... "); |
| 524 | 525 | if (!flash->erase) { |
| 525 | | fprintf(stderr, "Error: flashrom has no erase function for this flash chip.\n"); |
| | 526 | printf("FAILED!\n"); |
| | 527 | fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n"); |
| 526 | 528 | return 1; |
| 527 | 529 | } |
| 528 | 530 | flash->erase(flash); |
| 529 | | exit(0); |
| 530 | | } else if (read_it) { |
| | 531 | if (flash->read == NULL) |
| | 532 | memcpy(buf, (const char *)flash->virtual_memory, size); |
| | 533 | else |
| | 534 | flash->read(flash, buf); |
| | 535 | buf[size] = 0; |
| | 536 | erasedbytes = strspn((char *)buf, "\xff"); |
| | 537 | if (erasedbytes != size) { |
| | 538 | printf("FAILED!\n"); |
| | 539 | fprintf(stderr, "ERROR at 0x%08x: Expected=0xff, Read=0x%02x\n", |
| | 540 | erasedbytes, buf[erasedbytes]); |
| | 541 | return 1; |
| | 542 | } |
| | 543 | printf("OK.\n"); |
| | 544 | ret = 0; |
| | 545 | } |
| | 546 | |
| | 547 | if (read_it) { |
| 531 | 548 | if ((image = fopen(filename, "w")) == NULL) { |
| 532 | 549 | perror(filename); |
| 533 | 550 | exit(1); |
| … |
… |
|
| 545 | 562 | fwrite(buf, sizeof(char), size, image); |
| 546 | 563 | fclose(image); |
| 547 | 564 | printf("done\n"); |
| 548 | | } else { |
| | 565 | } |
| | 566 | |
| | 567 | if (write_it || verify_it) { |
| 549 | 568 | struct stat image_stat; |
| 550 | 569 | |
| 551 | 570 | if ((image = fopen(filename, "r")) == NULL) { |