int set_bootloader_message(const struct bootloader_message *in) { size_t write_size; const MtdPartition *part = get_root_mtd_partition(MISC_NAME); if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { LOGE("Can't find %s\n", MISC_NAME); return -1; } MtdReadContext *read = mtd_read_partition(part); if (read == NULL) { LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); return -1; } ssize_t size = write_size * MISC_PAGES; char data[size]; ssize_t r = mtd_read_data(read, data, size); if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno)); mtd_read_close(read); if (r != size) return -1; memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); #ifdef LOG_VERBOSE printf("\n--- set_bootloader_message ---\n"); dump_data(data, size); printf("\n"); #endif MtdWriteContext *write = mtd_write_partition(part); if (write == NULL) { LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); return -1; } if (mtd_write_data(write, data, size) != size) { LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno)); mtd_write_close(write); return -1; } if (mtd_write_close(write)) { LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno)); return -1; } LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); return 0; }
int reset_fota_cookie_mtd(void) { size_t write_size; mtd_scan_partitions(); const MtdPartition *part = mtd_find_partition_by_name("FOTA"); if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { LOGE("Can't find FOTA\n"); return -1; } MtdReadContext *read = mtd_read_partition(part); if (read == NULL) { LOGE("Can't open FOTA\n(%s)\n", strerror(errno)); return -1; } ssize_t size = write_size; //writing 1 page is enough char data[size]; ssize_t r = mtd_read_data(read, data, size); if (r != size) LOGE("Can't read FOTA\n(%s)\n", strerror(errno)); mtd_read_close(read); if (r != size) return -1; //Resetting FOTA cookie value memset(data, 0x0, sizeof(data)); MtdWriteContext *write = mtd_write_partition(part); if (write == NULL) { LOGE("Can't open FOTA\n(%s)\n", strerror(errno)); return -1; } if (mtd_write_data(write, data, size) != size) { LOGE("Can't write FOTA\n(%s)\n", strerror(errno)); mtd_write_close(write); return -1; } if (mtd_write_close(write)) { LOGE("Can't finish FOTA\n(%s)\n", strerror(errno)); return -1; } LOGI("Reset FOTA cookie done.\n"); return 0; }
static int set_bootloader_message_mtd(const bootloader_message* in, const Volume* v) { size_t write_size; mtd_scan_partitions(); const MtdPartition* part = mtd_find_partition_by_name(v->blk_device); if (part == nullptr || mtd_partition_info(part, nullptr, nullptr, &write_size)) { LOGE("failed to find \"%s\"\n", v->blk_device); return -1; } MtdReadContext* read = mtd_read_partition(part); if (read == nullptr) { LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); return -1; } ssize_t size = write_size * MISC_PAGES; char data[size]; ssize_t r = mtd_read_data(read, data, size); if (r != size) LOGE("failed to read \"%s\": %s\n", v->blk_device, strerror(errno)); mtd_read_close(read); if (r != size) return -1; memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); MtdWriteContext* write = mtd_write_partition(part); if (write == nullptr) { LOGE("failed to open \"%s\": %s\n", v->blk_device, strerror(errno)); return -1; } if (mtd_write_data(write, data, size) != size) { LOGE("failed to write \"%s\": %s\n", v->blk_device, strerror(errno)); mtd_write_close(write); return -1; } if (mtd_write_close(write)) { LOGE("failed to finish \"%s\": %s\n", v->blk_device, strerror(errno)); return -1; } LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); return 0; }
int set_bootloader_message_mtd_name(const struct bootloader_message *in, const char* mtd_name) { size_t write_size; mtd_scan_partitions(); const MtdPartition *part = mtd_find_partition_by_name(mtd_name); if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { printf("Can't find %s\n", mtd_name); return -1; } MtdReadContext *read = mtd_read_partition(part); if (read == NULL) { printf("Can't open %s\n(%s)\n", mtd_name, strerror(errno)); return -1; } ssize_t size = write_size * MISC_PAGES; char data[size]; ssize_t r = mtd_read_data(read, data, size); if (r != size) printf("Can't read %s\n(%s)\n", mtd_name, strerror(errno)); mtd_read_close(read); if (r != size) return -1; memcpy(&data[write_size * MISC_COMMAND_PAGE], in, sizeof(*in)); MtdWriteContext *write = mtd_write_partition(part); if (write == NULL) { printf("Can't open %s\n(%s)\n", mtd_name, strerror(errno)); return -1; } if (mtd_write_data(write, data, size) != size) { printf("Can't write %s\n(%s)\n", mtd_name, strerror(errno)); mtd_write_close(write); return -1; } if (mtd_write_close(write)) { printf("Can't finish %s\n(%s)\n", mtd_name, strerror(errno)); return -1; } printf("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); return 0; }
static unsigned short* CreateEmptyBlockMapping(const MtdPartition* pSrcPart) { size_t srcTotal, srcErase, srcWrite; if (mtd_partition_info(pSrcPart, &srcTotal, &srcErase, &srcWrite) != 0) { fprintf(stderr, "Failed to access partition.\n"); return NULL; } int numSrcBlocks = srcTotal/srcErase; unsigned short* pMapping = malloc(numSrcBlocks * sizeof(unsigned short)); if (pMapping == NULL) { fprintf(stderr, "Failed to allocate block mapping memory.\n"); return NULL; } memset(pMapping, 0xFF, numSrcBlocks * sizeof(unsigned short)); return pMapping; }
int cmd_mtd_backup_raw_partition(const char *partition_name, const char *filename) { MtdReadContext *in; const MtdPartition *partition; char buf[BLOCK_SIZE + SPARE_SIZE]; size_t partition_size; size_t read_size; size_t total; int fd; int wrote; int len; if (mtd_scan_partitions() <= 0) { printf("error scanning partitions"); return -1; } partition = mtd_find_partition_by_name(partition_name); if (partition == NULL) { printf("can't find %s partition", partition_name); return -1; } if (mtd_partition_info(partition, &partition_size, NULL, NULL)) { printf("can't get info of partition %s", partition_name); return -1; } if (!strcmp(filename, "-")) { fd = fileno(stdout); } else { fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC, 0666); } if (fd < 0) { printf("error opening %s", filename); return -1; } in = mtd_read_partition(partition); if (in == NULL) { close(fd); unlink(filename); printf("error opening %s: %s\n", partition_name, strerror(errno)); return -1; } total = 0; while ((len = mtd_read_data(in, buf, BLOCK_SIZE)) > 0) { wrote = write(fd, buf, len); if (wrote != len) { close(fd); unlink(filename); printf("error writing %s", filename); return -1; } total += BLOCK_SIZE; } mtd_read_close(in); if (close(fd)) { unlink(filename); printf("error closing %s", filename); return -1; } return 0; }
int cmd_mtd_restore_raw_partition (const char *partition_name, const char *filename) { const MtdPartition *ptn; MtdWriteContext *write; void *data; unsigned sz; if (mtd_scan_partitions () <= 0) { printf ("error scanning partitions"); return -1; } const MtdPartition *partition = mtd_find_partition_by_name (partition_name); if (partition == NULL) { printf ("can't find %s partition", partition_name); return -1; } // If the first part of the file matches the partition, skip writing int fd = open (filename, O_RDONLY); if (fd < 0) { printf ("error opening %s", filename); return -1; } char header[HEADER_SIZE]; int headerlen = read (fd, header, sizeof (header)); if (headerlen <= 0) { printf ("error reading %s header", filename); return -1; } MtdReadContext *in = mtd_read_partition (partition); if (in == NULL) { printf ("error opening %s: %s\n", partition, strerror (errno)); // just assume it needs re-writing } else { char check[HEADER_SIZE]; int checklen = mtd_read_data (in, check, sizeof (check)); if (checklen <= 0) { printf ("error reading %s: %s\n", partition_name, strerror (errno)); // just assume it needs re-writing } else if (checklen == headerlen && !memcmp (header, check, headerlen)) { printf ("header is the same, not flashing %s\n", partition_name); return 0; } mtd_read_close (in); } // Skip the header (we'll come back to it), write everything else printf ("flashing %s from %s\n", partition_name, filename); MtdWriteContext *out = mtd_write_partition (partition); if (out == NULL) { printf ("error writing %s", partition_name); return -1; } char buf[HEADER_SIZE]; memset (buf, 0, headerlen); int wrote = mtd_write_data (out, buf, headerlen); if (wrote != headerlen) { printf ("error writing %s", partition_name); return -1; } int len; while ((len = read (fd, buf, sizeof (buf))) > 0) { wrote = mtd_write_data (out, buf, len); if (wrote != len) { printf ("error writing %s", partition_name); return -1; } } if (len < 0) { printf ("error reading %s", filename); return -1; } if (mtd_write_close (out)) { printf ("error closing %s", partition_name); return -1; } // Now come back and write the header last out = mtd_write_partition (partition); if (out == NULL) { printf ("error re-opening %s", partition_name); return -1; } wrote = mtd_write_data (out, header, headerlen); if (wrote != headerlen) { printf ("error re-writing %s", partition_name); return -1; } // Need to write a complete block, so write the rest of the first block size_t block_size; if (mtd_partition_info (partition, NULL, &block_size, NULL)) { printf ("error getting %s block size", partition_name); return -1; } if (lseek (fd, headerlen, SEEK_SET) != headerlen) { printf ("error rewinding %s", filename); return -1; } int left = block_size - headerlen; while (left < 0) left += block_size; while (left > 0) { len = read (fd, buf, left > (int) sizeof (buf) ? (int) sizeof (buf) : left); if (len <= 0) { printf ("error reading %s", filename); return -1; } if (mtd_write_data (out, buf, len) != len) { printf ("error writing %s", partition_name); return -1; } left -= len; } if (mtd_write_close (out)) { printf ("error closing %s", partition_name); return -1; } return 0; }
static const unsigned short* CreateBlockMapping(const MtdPartition* pSrcPart, int srcPartStartBlock, const MtdPartition *pReservoirPart, int reservoirPartStartBlock) { size_t srcTotal, srcErase, srcWrite; if (mtd_partition_info(pSrcPart, &srcTotal, &srcErase, &srcWrite) != 0) { fprintf(stderr, "Failed to access partition.\n"); return NULL; } int numSrcBlocks = srcTotal/srcErase; unsigned short* pMapping = malloc(numSrcBlocks * sizeof(unsigned short)); if (pMapping == NULL) { fprintf(stderr, "Failed to allocate block mapping memory.\n"); return NULL; } memset(pMapping, 0xFF, numSrcBlocks * sizeof(unsigned short)); size_t total, erase, write; if (mtd_partition_info(pReservoirPart, &total, &erase, &write) != 0) { fprintf(stderr, "Failed to access reservoir partition.\n"); free(pMapping); return NULL; } if (erase != srcErase || write != srcWrite) { fprintf(stderr, "Source partition and reservoir partition differ in size properties.\n"); free(pMapping); return NULL; } printf("Partition info: Total %d, Erase %d, write %d\n", total, erase, write); BmlOverMtdReadContext *readctx = bml_over_mtd_read_partition(pReservoirPart); if (readctx == NULL) { fprintf(stderr, "Failed to open reservoir partition for reading.\n"); free(pMapping); return NULL; } if (total < erase || total > INT_MAX) { fprintf(stderr, "Unsuitable reservoir partition properties.\n"); free(pMapping); bml_over_mtd_read_close(readctx); return NULL; } int foundMappingTable = 0; int currOffset = total; //Offset *behind* the last byte while (currOffset > 0) { currOffset -= erase; loff_t pos = lseek64(readctx->fd, currOffset, SEEK_SET); int mgbb = ioctl(readctx->fd, MEMGETBADBLOCK, &pos); if (mgbb != 0) { printf("Bad block %d in reservoir area, skipping.\n", currOffset/erase); continue; } ssize_t readBytes = read(readctx->fd, readctx->buffer, erase); if (readBytes != (ssize_t)erase) { fprintf(stderr, "Failed to read good block in reservoir area (%s).\n", strerror(errno)); free(pMapping); bml_over_mtd_read_close(readctx); return NULL; } if (readBytes >= 0x2000) { char* buf = readctx->buffer; if (buf[0]=='U' && buf[1]=='P' && buf[2]=='C' && buf[3]=='H') { printf ("Found mapping block mark at 0x%x (block %d).\n", currOffset, currOffset/erase); unsigned short* mappings = (unsigned short*) &buf[0x1000]; if (mappings[0]==0 && mappings[1]==0xffff) { printf("Found start of mapping table.\n"); foundMappingTable = 1; //Skip first entry (dummy) unsigned short* mappingEntry = mappings + 2; while (mappingEntry - mappings < 100 && mappingEntry[0] != 0xffff) { unsigned short rawSrcBlk = mappingEntry[0]; unsigned short rawDstBlk = mappingEntry[1]; printf("Found raw block mapping %d -> %d\n", rawSrcBlk, rawDstBlk); unsigned int srcAbsoluteStartAddress = srcPartStartBlock * erase; unsigned int resAbsoluteStartAddress = reservoirPartStartBlock * erase; int reservoirLastBlock = reservoirPartStartBlock + numSrcBlocks - 1; if (rawDstBlk < reservoirPartStartBlock || rawDstBlk*erase >= resAbsoluteStartAddress+currOffset) { fprintf(stderr, "Mapped block not within reasonable reservoir area.\n"); foundMappingTable = 0; break; } int srcLastBlock = srcPartStartBlock + numSrcBlocks - 1; if (rawSrcBlk >= srcPartStartBlock && rawSrcBlk <= srcLastBlock) { unsigned short relSrcBlk = rawSrcBlk - srcPartStartBlock; unsigned short relDstBlk = rawDstBlk - reservoirPartStartBlock; printf("Partition relative block mapping %d -> %d\n",relSrcBlk, relDstBlk); printf("Absolute mapped start addresses 0x%x -> 0x%x\n", srcAbsoluteStartAddress+relSrcBlk*erase, resAbsoluteStartAddress+relDstBlk*erase); printf("Partition relative mapped start addresses 0x%x -> 0x%x\n", relSrcBlk*erase, relDstBlk*erase); //Set mapping entry. For duplicate entries, later entries replace former ones. //*Assumption*: Bad blocks in reservoir area will not be mapped themselves in //the mapping table. User partition blocks will not be mapped to bad blocks //(only) in the reservoir area. This has to be confirmed on a wider range of //devices. pMapping[relSrcBlk] = relDstBlk; } mappingEntry+=2; } break; //We found the mapping table, no need to search further } } } } bml_over_mtd_read_close(readctx); if (foundMappingTable == 0) { fprintf(stderr, "Cannot find mapping table in reservoir partition.\n"); free(pMapping); return NULL; } //Consistency and validity check int mappingValid = 1; readctx = bml_over_mtd_read_partition(pSrcPart); if (readctx == NULL) { fprintf(stderr, "Cannot open source partition for reading.\n"); free(pMapping); return NULL; } int currBlock = 0; for (;currBlock < numSrcBlocks; ++currBlock) { loff_t pos = lseek64(readctx->fd, currBlock*erase, SEEK_SET); int mgbb = ioctl(readctx->fd, MEMGETBADBLOCK, &pos); if (mgbb == 0) { if (pMapping[currBlock]!=0xffff) { fprintf(stderr, "Consistency error: Good block has mapping entry %d -> %d\n", currBlock, pMapping[currBlock]); mappingValid = 0; } } else { //Bad block! if (pMapping[currBlock]==0xffff) { fprintf(stderr, "Consistency error: Bad block has no mapping entry \n"); mappingValid = 0; } else { BmlOverMtdReadContext* reservoirReadCtx = bml_over_mtd_read_partition(pReservoirPart); if (reservoirReadCtx == 0) { fprintf(stderr, "Reservoir partition cannot be opened for reading in consistency check.\n"); mappingValid = 0; } else { pos = lseek64(reservoirReadCtx->fd, pMapping[currBlock]*erase, SEEK_SET); mgbb = ioctl(reservoirReadCtx->fd, MEMGETBADBLOCK, &pos); if (mgbb == 0) { printf("Bad block has properly mapped reservoir block %d -> %d\n",currBlock, pMapping[currBlock]); } else { fprintf(stderr, "Consistency error: Mapped block is bad, too. (%d -> %d)\n",currBlock, pMapping[currBlock]); mappingValid = 0; } } bml_over_mtd_read_close(reservoirReadCtx); } } } bml_over_mtd_read_close(readctx); if (!mappingValid) { free(pMapping); return NULL; } return pMapping; }
int set_bootloader_message(const struct bootloader_message *in) { // INFO("Enter set_bootloader_message\n"); if(in->command[0] != 0) { LOGI("command:\n%s\n", in->command); LOGI("status:\n%s\n", in->status); LOGI("recovery:\n%s\n", in->recovery); } else LOGI("bootloader_message is empty\n"); size_t write_size; const MtdPartition *part = get_root_mtd_partition(MISC_NAME); if (part == NULL || mtd_partition_info(part, NULL, NULL, &write_size)) { LOGE("Can't find %s\n", MISC_NAME); return -1; } MtdReadContext *read = mtd_read_partition(part); if (read == NULL) { LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); return -1; } //cmy: 以sector为单位 ssize_t size = write_size * MISC_PAGES; char data[size<<9];//cmy ssize_t r = mtd_read_data(read, data, size); if (r != size) LOGE("Can't read %s\n(%s)\n", MISC_NAME, strerror(errno)); mtd_read_close(read); if (r != size) return -1; memcpy(&data[(write_size<<9) * MISC_COMMAND_PAGE], in, sizeof(*in)); #ifdef LOG_VERBOSE printf("\n--- set_bootloader_message ---\n"); dump_data(data, size<<9); printf("\n"); #endif // INFO("\n--- set_bootloader_message ---\n"); // dump_data(data, size); // INFO("\n"); // CMY:由于底层写数据时会自动执行擦除操作,因些我们可以直接写数据而不考虑擦除动作 #if 1 MtdWriteContext *write = mtd_write_partition(part); if (write == NULL) { LOGE("Can't open %s\n(%s)\n", MISC_NAME, strerror(errno)); return -1; } LOGI("Write bootloader message\n"); if (mtd_write_data(write, data, size) != size) { LOGE("Can't write %s\n(%s)\n", MISC_NAME, strerror(errno)); mtd_write_close(write); return -1; } if (mtd_write_close(write)) { LOGE("Can't finish %s\n(%s)\n", MISC_NAME, strerror(errno)); return -1; } #endif LOGI("Set boot command \"%s\"\n", in->command[0] != 255 ? in->command : ""); return 0; }
/* Read an image file and write it to a flash partition. */ int main(int argc, char **argv) { const MtdPartition *ptn; MtdWriteContext *write; void *data; unsigned sz; int i; char *partitionName = NULL, *imageFile = NULL; int deleteImage = 0; if (argc < 3) { printUsage(argv[0]); return 2; } partitionName = argv[1]; imageFile = argv[2]; if (partitionName == NULL || imageFile == NULL) { printUsage(argv[0]); return 2; } if (mtd_scan_partitions() <= 0) die("error scanning partitions"); const MtdPartition *partition = mtd_find_partition_by_name(partitionName); if (partition == NULL) die("can't find %s partition", partitionName); // If the first part of the file matches the partition, skip writing int fd = open(imageFile, O_RDONLY); if (fd < 0) die("error opening %s", imageFile); char header[HEADER_SIZE]; int headerlen = read(fd, header, sizeof(header)); if (headerlen <= 0) die("error reading %s header", imageFile); MtdReadContext *in = mtd_read_partition(partition); if (in == NULL) { LOGW("error opening %s: %s\n", partitionName, strerror(errno)); // just assume it needs re-writing } else { char check[HEADER_SIZE]; int checklen = mtd_read_data(in, check, sizeof(check)); if (checklen <= 0) { LOGW("error reading %s: %s\n", partitionName, strerror(errno)); // just assume it needs re-writing } else if (checklen == headerlen && !memcmp(header, check, headerlen)) { LOGI("header is the same, not flashing %s\n", argv[1]); if (deleteImage) unlink(imageFile); return 0; } mtd_read_close(in); } // Skip the header (we'll come back to it), write everything else LOGI("flashing %s from %s\n", partitionName, imageFile); MtdWriteContext *out = mtd_write_partition(partition); if (out == NULL) die("error writing %s", partitionName); char buf[HEADER_SIZE]; memset(buf, 0, headerlen); int wrote = mtd_write_data(out, buf, headerlen); if (wrote != headerlen) die("error writing %s", partitionName); int len; while ((len = read(fd, buf, sizeof(buf))) > 0) { wrote = mtd_write_data(out, buf, len); if (wrote != len) die("error writing %s", partitionName); } if (len < 0) die("error reading %s", imageFile); if (mtd_write_close(out)) die("error closing %s", partitionName); // Now come back and write the header last out = mtd_write_partition(partition); if (out == NULL) die("error re-opening %s", partitionName); wrote = mtd_write_data(out, header, headerlen); if (wrote != headerlen) die("error re-writing %s", partitionName); // Need to write a complete block, so write the rest of the first block size_t block_size; if (mtd_partition_info(partition, NULL, &block_size, NULL)) die("error getting %s block size", partitionName); if (lseek(fd, headerlen, SEEK_SET) != headerlen) die("error rewinding %s", imageFile); int left = block_size - headerlen; while (left < 0) left += block_size; while (left > 0) { len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left); if (len <= 0) die("error reading %s", imageFile); if (mtd_write_data(out, buf, len) != len) die("error writing %s", partitionName); left -= len; } if (mtd_write_close(out)) die("error closing %s", partitionName); if (deleteImage) unlink(imageFile); return 0; }
int write_update_for_bootloader( const char *update, int update_length, const char *log_filename) { const MtdPartition *part = mtd_find_partition_by_name(CACHE_NAME); if (part == NULL) { LOGE("Can't find %s\n", CACHE_NAME); return -1; } MtdWriteContext *write = mtd_write_partition(part); if (write == NULL) { LOGE("Can't open %s\n(%s)\n", CACHE_NAME, strerror(errno)); return -1; } /* Write an invalid (zero) header first, to disable any previous * update and any other structured contents (like a filesystem), * and as a placeholder for the amount of space required. */ struct update_header header; memset(&header, 0, sizeof(header)); const ssize_t header_size = sizeof(header); if (mtd_write_data(write, (char*) &header, header_size) != header_size) { LOGE("Can't write header to %s\n(%s)\n", CACHE_NAME, strerror(errno)); mtd_write_close(write); return -1; } /* Write each section individually block-aligned, so we can write * each block independently without complicated buffering. */ memcpy(&header.MAGIC, UPDATE_MAGIC, UPDATE_MAGIC_SIZE); header.version = UPDATE_VERSION; header.size = header_size; if (log_filename != NULL) { // Write 1 byte into the following block, then fill to the end // in order to reserve that block. We'll use the block to // send a copy of the log through to the next invocation of // recovery. We write the log as late as possible in order to // capture any messages emitted by this function. mtd_erase_blocks(write, 0); if (mtd_write_data(write, (char*) &header, 1) != 1) { LOGE("Can't write log block to %s\n(%s)\n", CACHE_NAME, strerror(errno)); mtd_write_close(write); return -1; } } off_t image_start_pos = mtd_erase_blocks(write, 0); header.image_length = update_length; if ((int) header.image_offset == -1 || mtd_write_data(write, update, update_length) != update_length) { LOGE("Can't write update to %s\n(%s)\n", CACHE_NAME, strerror(errno)); mtd_write_close(write); return -1; } mtd_erase_blocks(write, 0); /* Sending image offset as it is.Apps bootloader will take care of bad blocks */ header.image_offset = 0x80000; /* Write the header last, after all the blocks it refers to, so that * when the magic number is installed everything is valid. */ if (mtd_write_close(write)) { LOGE("Can't finish writing %s\n(%s)\n", CACHE_NAME, strerror(errno)); return -1; } write = mtd_write_partition(part); if (write == NULL) { LOGE("Can't reopen %s\n(%s)\n", CACHE_NAME, strerror(errno)); return -1; } if (mtd_write_data(write, (char*) &header, header_size) != header_size) { LOGE("Can't rewrite header to %s\n(%s)\n", CACHE_NAME, strerror(errno)); mtd_write_close(write); return -1; } if (log_filename != NULL) { LOGE("writing log\n"); size_t erase_size; if (mtd_partition_info(part, NULL, &erase_size, NULL) != 0) { LOGE("Error reading block size\n(%s)\n", strerror(errno)); mtd_write_close(write); return -1; } mtd_erase_blocks(write, 0); if (erase_size > 0) { char* log = malloc(erase_size); FILE* f = fopen(log_filename, "rb"); // The fseek() may fail if it tries to go before the // beginning of the log, but that's okay because we want // to be positioned at the start anyway. fseek(f, -(erase_size-sizeof(size_t)-LOG_MAGIC_SIZE), SEEK_END); memcpy(log, LOG_MAGIC, LOG_MAGIC_SIZE); size_t read = fread(log+sizeof(size_t)+LOG_MAGIC_SIZE, 1, erase_size-sizeof(size_t)-LOG_MAGIC_SIZE, f); LOGI("read %d bytes from log\n", (int)read); *(size_t *)(log + LOG_MAGIC_SIZE) = read; fclose(f); if (mtd_write_data(write, log, erase_size) != erase_size) { LOGE("failed to store log in cache partition\n(%s)\n", strerror(errno)); mtd_write_close(write); } free(log); } } if (mtd_erase_blocks(write, 0) != image_start_pos) { LOGE("Misalignment rewriting %s\n(%s)\n", CACHE_NAME, strerror(errno)); mtd_write_close(write); return -1; } LOGE("closing partition\n"); if (mtd_write_close(write)) { LOGE("Can't finish header of %s\n(%s)\n", CACHE_NAME, strerror(errno)); return -1; } return 0; }
int main(int argc, char **argv) { const MtdPartition *ptn; MtdWriteContext *write; void *data; unsigned sz; LOGI("enter\n"); if (argc != 3) { LOGI("exit1\n"); fprintf(stderr, "usage: %s partition file.img\n", argv[0]); return 2; } int fd = open(argv[2], O_RDONLY); if (fd < 0) die("error opening %s", argv[2]); close(fd); if (mtd_scan_partitions() <= 0) die("error scanning partitions"); const MtdPartition *partition = mtd_find_partition_by_name(argv[1]); if (partition == NULL) die("can't find %s partition", argv[1]); LOGI("flashing %s from %s\n", argv[1], argv[2]); if( 0 == write_recovery(argv[2], argv[1]) ) { if( remove(argv[2]) == 0 ) LOGI("flash success!\n"); else die("error remove file: %s", argv[2]); } else die("error closing %s", argv[1]); #if 0 const MtdPartition *partition = mtd_find_partition_by_name(argv[1]); if (partition == NULL) die("can't find %s partition", argv[1]); // If the first part of the file matches the partition, skip writing int fd = open(argv[2], O_RDONLY); if (fd < 0) die("error opening %s", argv[2]); LOGI("read header!\n"); char header[HEADER_SIZE]; int headerlen = read(fd, header, sizeof(header)); if (headerlen <= 0) die("error reading %s header", argv[2]); MtdReadContext *in = mtd_read_partition(partition); if (in == NULL) { LOGW("error opening %s: %s\n", argv[1], strerror(errno)); // just assume it needs re-writing } else { LOGI("mtd_read_data\n"); char check[HEADER_SIZE]; int checklen = mtd_read_data(in, check, sizeof(check)); if (checklen <= 0) { LOGW("error reading %s: %s\n", argv[1], strerror(errno)); // just assume it needs re-writing } else if (checklen == headerlen && !memcmp(header, check, headerlen)) { LOGI("header is the same, not flashing %s\n", argv[1]); return 0; } mtd_read_close(in); } // Skip the header (we'll come back to it), write everything else LOGI("flashing %s from %s\n", argv[1], argv[2]); MtdWriteContext *out = mtd_write_partition(partition); if (out == NULL) die("error writing %s", argv[1]); char buf[HEADER_SIZE]; memset(buf, 0, headerlen); int wrote = mtd_write_data(out, buf, headerlen); if (wrote != headerlen) die("error writing %s", argv[1]); int len; while ((len = read(fd, buf, sizeof(buf))) > 0) { wrote = mtd_write_data(out, buf, len); if (wrote != len) die("error writing %s", argv[1]); } if (len < 0) die("error reading %s", argv[2]); if (mtd_write_close(out)) die("error closing %s", argv[1]); // Now come back and write the header last out = mtd_write_partition(partition); if (out == NULL) die("error re-opening %s", argv[1]); wrote = mtd_write_data(out, header, headerlen); if (wrote != headerlen) die("error re-writing %s", argv[1]); // Need to write a complete block, so write the rest of the first block size_t block_size; if (mtd_partition_info(partition, NULL, &block_size, NULL)) die("error getting %s block size", argv[1]); if (lseek(fd, headerlen, SEEK_SET) != headerlen) die("error rewinding %s", argv[2]); int left = block_size - headerlen; while (left < 0) left += block_size; while (left > 0) { len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left); if (len <= 0) die("error reading %s", argv[2]); if (mtd_write_data(out, buf, len) != len) die("error writing %s", argv[1]); left -= len; } if (mtd_write_close(out)) die("error closing %s", argv[1]); #endif return 0; }
int main(int argc, char **argv) { const MtdPartition *ptn; MtdWriteContext *write; void *data; unsigned sz; if (argc != 4) { fprintf(stderr, "usage: %s type [partition|device] [image_file_path]\n", argv[0]); return 2; } if (0 == strcmp("MTD", argv[1])) { if (mtd_scan_partitions() <= 0) die("error scanning partitions"); const MtdPartition *partition = mtd_find_partition_by_name(argv[1]); if (partition == NULL) die("can't find %s partition", argv[1]); // If the first part of the file matches the partition, skip writing int fd = open(argv[2], O_RDONLY); if (fd < 0) die("error opening %s", argv[2]); char header[HEADER_SIZE]; int headerlen = read(fd, header, sizeof(header)); if (headerlen <= 0) die("error reading %s header", argv[2]); MtdReadContext *in = mtd_read_partition(partition); if (in == NULL) { LOGW("error opening %s: %s\n", argv[1], strerror(errno)); // just assume it needs re-writing } else { char check[HEADER_SIZE]; int checklen = mtd_read_data(in, check, sizeof(check)); if (checklen <= 0) { LOGW("error reading %s: %s\n", argv[1], strerror(errno)); // just assume it needs re-writing } else if (checklen == headerlen && !memcmp(header, check, headerlen)) { LOGI("header is the same, not flashing %s\n", argv[1]); return 0; } mtd_read_close(in); } // Skip the header (we'll come back to it), write everything else LOGI("flashing %s from %s\n", argv[1], argv[2]); MtdWriteContext *out = mtd_write_partition(partition); if (out == NULL) die("error writing %s", argv[1]); char buf[HEADER_SIZE]; memset(buf, 0, headerlen); int wrote = mtd_write_data(out, buf, headerlen); if (wrote != headerlen) die("error writing %s", argv[1]); int len; while ((len = read(fd, buf, sizeof(buf))) > 0) { wrote = mtd_write_data(out, buf, len); if (wrote != len) die("error writing %s", argv[1]); } if (len < 0) die("error reading %s", argv[2]); if (mtd_write_close(out)) die("error closing %s", argv[1]); // Now come back and write the header last out = mtd_write_partition(partition); if (out == NULL) die("error re-opening %s", argv[1]); wrote = mtd_write_data(out, header, headerlen); if (wrote != headerlen) die("error re-writing %s", argv[1]); // Need to write a complete block, so write the rest of the first block size_t block_size; if (mtd_partition_info(partition, NULL, &block_size, NULL)) die("error getting %s block size", argv[1]); if (lseek(fd, headerlen, SEEK_SET) != headerlen) die("error rewinding %s", argv[2]); int left = block_size - headerlen; while (left < 0) left += block_size; while (left > 0) { len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left); if (len == 0) break; if (len < 0) die("error reading %s", argv[2]); if (mtd_write_data(out, buf, len) != len) die("error writing %s", argv[1]); left -= len; } // If there is more to write, input data was less than block_size, so pad // with nulls. memset(buf, 0, sizeof(buf)); while (left > 0) { int pad_len = left > (int)sizeof(buf) ? (int)sizeof(buf) : left; if (mtd_write_data(out, buf, pad_len) != pad_len) die("error writing %s", argv[1]); left -= pad_len; } if (mtd_write_close(out)) die("error closing %s", argv[1]); return 0; } else if (0 == strcmp("EMMC", argv[1]) || 0 == strcmp("INAND",argv[1])) { int fd = open(argv[3], O_RDONLY); if (fd < 0) die("error opening %s", argv[3]); char header[HEADER_SIZE]; int headerlen = read(fd, header, sizeof(header)); if (headerlen != sizeof(header)) die("error reading %s header", argv[3]); FILE* f = fopen(argv[2], "rb"); if (f == NULL ) die("error opening %s", argv[2]); char check[HEADER_SIZE]; int checklen = fread(&check, 1, sizeof(check), f); if (checklen != sizeof(check)) die("error reading %s header", argv[3]); fclose(f); if(!memcmp(header, check, headerlen)) { printf("header is the same, not flashing %s\n", argv[2]); close(fd); return 0; } f = fopen(argv[2], "wb"); char buf[HEADER_SIZE]; int wrote = fwrite(header, sizeof(header), 1, f); if (wrote != 1) { close(fd); fclose(f); die("error writing %s", argv[2]); } int len; while ((len = read(fd, buf, sizeof(buf))) > 0) { wrote = fwrite(buf, sizeof(buf), 1, f); if (wrote != 1) { close(fd); fclose(f); die("error writing %s", argv[2]); } } if (len < 0) { close(fd); fclose(f); die("error reading %s", argv[3]); } close(fd); fclose(f); } else { die("wrong type %s, it should be MTD or EMMC", argv[1]); } printf("flash image %s to %s successfully\n", argv[3], argv[2] ); return 0; }
int main(int argc, char **argv) { const MtdPartition *ptn; MtdWriteContext *write; void *data; unsigned sz; int rc = 0; if (argc != 3) { fprintf(stderr, "usage: %s partition file.img\n", argv[0]); return -EINVAL; } rc = mtd_scan_partitions(); if (rc < 0) { fprintf(stderr, "error scanning partitions\n"); return rc; } else if (rc == 0) { fprintf(stderr, "no partitions found\n"); return -ENODEV; } const MtdPartition *partition = mtd_find_partition_by_name(argv[1]); if (partition == NULL) { fprintf(stderr, "can't find %s partition\n", argv[1]); return -ENODEV; } // If the first part of the file matches the partition, skip writing int fd = open(argv[2], O_RDONLY); if (fd < 0) { fprintf(stderr, "error opening %s\n", argv[2]); return fd; } char header[HEADER_SIZE]; int headerlen = TEMP_FAILURE_RETRY(read(fd, header, sizeof(header))); if (headerlen <= 0) { fprintf(stderr, "error reading %s header\n", argv[2]); rc = -EIO; goto exit; } MtdReadContext *in = mtd_read_partition(partition); if (in == NULL) { fprintf(stderr, "error opening %s: %s\n", argv[1], strerror(errno)); rc = -ENXIO; goto exit; // just assume it needs re-writing } else { char check[HEADER_SIZE]; int checklen = mtd_read_data(in, check, sizeof(check)); if (checklen <= 0) { fprintf(stderr, "error reading %s: %s\n", argv[1], strerror(errno)); rc = -EIO; goto exit; // just assume it needs re-writing } else if (checklen == headerlen && !memcmp(header, check, headerlen)) { fprintf(stderr, "header is the same, not flashing %s\n", argv[1]); rc = -EINVAL; goto exit; } mtd_read_close(in); } // Skip the header (we'll come back to it), write everything else printf("flashing %s from %s\n", argv[1], argv[2]); MtdWriteContext *out = mtd_write_partition(partition); if (out == NULL) { fprintf(stderr, "error writing %s\n", argv[1]); rc = -EIO; goto exit; } char buf[HEADER_SIZE]; memset(buf, 0, headerlen); int wrote = mtd_write_data(out, buf, headerlen); if (wrote != headerlen) { fprintf(stderr, "error writing %s\n", argv[1]); rc = -EIO; goto exit; } int len; while ((len = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf)))) > 0) { wrote = mtd_write_data(out, buf, len); if (wrote != len) { fprintf(stderr, "error writing %s\n", argv[1]); rc = -EIO; goto exit; } } if (len < 0) { fprintf(stderr, "error reading %s\n", argv[2]); rc = -EIO; goto exit; } rc = mtd_write_close(out); if (rc < 0) { fprintf(stderr, "error closing %s\n", argv[1]); goto exit; } // Now come back and write the header last out = mtd_write_partition(partition); if (out == NULL) { fprintf(stderr, "error re-opening %s\n", argv[1]); rc = -EIO; goto exit; } wrote = mtd_write_data(out, header, headerlen); if (wrote != headerlen) { fprintf(stderr, "error re-writing %s\n", argv[1]); rc = -EIO; goto exit; } // Need to write a complete block, so write the rest of the first block size_t block_size; rc = mtd_partition_info(partition, NULL, &block_size, NULL); if (rc < 0) { fprintf(stderr, "error getting %s block size\n", argv[1]); goto exit; } if (TEMP_FAILURE_RETRY(lseek(fd, headerlen, SEEK_SET)) != headerlen) { fprintf(stderr, "error rewinding %s\n", argv[2]); rc = -ESPIPE; goto exit; } int left = block_size - headerlen; while (left < 0) left += block_size; while (left > 0) { len = TEMP_FAILURE_RETRY(read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left)); if (len <= 0) { fprintf(stderr, "error reading %s\n", argv[2]); rc = -EIO; goto exit; } if (mtd_write_data(out, buf, len) != len) { fprintf(stderr, "error writing %s\n", argv[1]); rc = -EIO; goto exit; } left -= len; } rc = mtd_write_close(out); if (rc < 0) { fprintf(stderr, "error closing %s\n", argv[1]); goto exit; } rc = 0; exit: close(fd); return rc; }