Ticket #103: fr.103.erasenoexit.patch

File fr.103.erasenoexit.patch, 2.0 KB (added by stuge, 5 months ago)
  • flashrom.c

    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>
    
     
    247247{ 
    248248        uint8_t *buf; 
    249249        unsigned long size; 
     250        size_t erasedbytes; 
    250251        FILE *image; 
    251252        /* Probe for up to three flash chips. */ 
    252253        struct flashchip *flash, *flashes[3]; 
     
    517518        } 
    518519 
    519520        size = flash->total_size * 1024; 
    520         buf = (uint8_t *) calloc(size, sizeof(char)); 
     521        buf = (uint8_t *) calloc(size + 1, sizeof(char)); 
    521522 
    522523        if (erase_it) { 
    523                 printf("Erasing flash chip.\n"); 
     524                printf("Erasing flash chip... "); 
    524525                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"); 
    526528                        return 1; 
    527529                } 
    528530                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) { 
    531548                if ((image = fopen(filename, "w")) == NULL) { 
    532549                        perror(filename); 
    533550                        exit(1); 
     
    545562                fwrite(buf, sizeof(char), size, image); 
    546563                fclose(image); 
    547564                printf("done\n"); 
    548         } else { 
     565        } 
     566 
     567        if (write_it || verify_it) { 
    549568                struct stat image_stat; 
    550569 
    551570                if ((image = fopen(filename, "r")) == NULL) {