/* returns the # of failures, 0 on success */ static unsigned ProgramImageFile(char* filename, UINT8* pos, tImageHeader* pImageHeader, int start, int size) { int i; int fd; int read; /* how many for this sector */ unsigned failures = 0; fd = rb->open(filename, O_RDONLY); if (fd < 0) return false; /* no error checking necessary here, we checked for minimum size already */ rb->lseek(fd, start, SEEK_SET); /* go to start position */ *(tImageHeader*)sector = *pImageHeader; /* copy header into sector buffer */ read = rb->read(fd, sector + sizeof(tImageHeader), SECTORSIZE - sizeof(tImageHeader)); /* payload behind */ size -= read; read += sizeof(tImageHeader); /* to be programmed, but not part of the file */ do { if (!EraseSector(pos)) { /* nothing we can do, let the programming count the errors */ } for (i=0; i<read; i++) { if (!ProgramByte(pos + i, sector[i])) { failures++; } } pos += SECTORSIZE; read = rb->read(fd, sector, (size > SECTORSIZE) ? SECTORSIZE : size); /* payload for next sector */ size -= read; } while (read > 0); rb->close(fd); return failures; }
/* returns the # of failures, 0 on success */ unsigned ProgramFirmwareFile(char* filename, int chipsize) { int i, j; int fd; int read = SEC_SIZE; /* how many for this sector */ UINT16 keep = *(UINT16*)(FB + KEEP); /* we must keep this! */ unsigned failures = 0; fd = rb->open(filename, O_RDONLY); if (fd < 0) return false; for (i=0; i<chipsize; i+=SEC_SIZE) { if (!EraseSector(FB + i)) { /* nothing we can do, let the programming count the errors */ } if (read == SEC_SIZE) /* not EOF yet */ { read = rb->read(fd, sector, SEC_SIZE); if (i==0) { /* put original value back in */ *(UINT16*)(sector + KEEP) = keep; } for (j=0; j<read; j++) { if (!ProgramByte(FB + i + j, sector[j])) { failures++; } } } } rb->close(fd); return failures; }