Lines Matching refs:ctx
259 MtdReadContext *ctx = (MtdReadContext*) malloc(sizeof(MtdReadContext)); in mtd_read_partition() local
260 if (ctx == NULL) return NULL; in mtd_read_partition()
262 ctx->buffer = malloc(partition->erase_size); in mtd_read_partition()
263 if (ctx->buffer == NULL) { in mtd_read_partition()
264 free(ctx); in mtd_read_partition()
270 ctx->fd = open(mtddevname, O_RDONLY); in mtd_read_partition()
271 if (ctx->fd < 0) { in mtd_read_partition()
272 free(ctx->buffer); in mtd_read_partition()
273 free(ctx); in mtd_read_partition()
277 ctx->partition = partition; in mtd_read_partition()
278 ctx->consumed = partition->erase_size; in mtd_read_partition()
279 return ctx; in mtd_read_partition()
284 void mtd_read_skip_to(const MtdReadContext* ctx, size_t offset) { in mtd_read_skip_to() argument
285 lseek64(ctx->fd, offset, SEEK_SET); in mtd_read_skip_to()
329 ssize_t mtd_read_data(MtdReadContext *ctx, char *data, size_t len) in mtd_read_data() argument
333 if (ctx->consumed < ctx->partition->erase_size) { in mtd_read_data()
334 size_t avail = ctx->partition->erase_size - ctx->consumed; in mtd_read_data()
336 memcpy(data + read, ctx->buffer + ctx->consumed, copy); in mtd_read_data()
337 ctx->consumed += copy; in mtd_read_data()
342 while (ctx->consumed == ctx->partition->erase_size && in mtd_read_data()
343 len - read >= ctx->partition->erase_size) { in mtd_read_data()
344 if (read_block(ctx->partition, ctx->fd, data + read)) return -1; in mtd_read_data()
345 read += ctx->partition->erase_size; in mtd_read_data()
353 if (ctx->consumed == ctx->partition->erase_size && read < len) { in mtd_read_data()
354 if (read_block(ctx->partition, ctx->fd, ctx->buffer)) return -1; in mtd_read_data()
355 ctx->consumed = 0; in mtd_read_data()
362 void mtd_read_close(MtdReadContext *ctx) in mtd_read_close() argument
364 close(ctx->fd); in mtd_read_close()
365 free(ctx->buffer); in mtd_read_close()
366 free(ctx); in mtd_read_close()
371 MtdWriteContext *ctx = (MtdWriteContext*) malloc(sizeof(MtdWriteContext)); in mtd_write_partition() local
372 if (ctx == NULL) return NULL; in mtd_write_partition()
374 ctx->bad_block_offsets = NULL; in mtd_write_partition()
375 ctx->bad_block_alloc = 0; in mtd_write_partition()
376 ctx->bad_block_count = 0; in mtd_write_partition()
378 ctx->buffer = malloc(partition->erase_size); in mtd_write_partition()
379 if (ctx->buffer == NULL) { in mtd_write_partition()
380 free(ctx); in mtd_write_partition()
386 ctx->fd = open(mtddevname, O_RDWR); in mtd_write_partition()
387 if (ctx->fd < 0) { in mtd_write_partition()
388 free(ctx->buffer); in mtd_write_partition()
389 free(ctx); in mtd_write_partition()
393 ctx->partition = partition; in mtd_write_partition()
394 ctx->stored = 0; in mtd_write_partition()
395 return ctx; in mtd_write_partition()
398 static void add_bad_block_offset(MtdWriteContext *ctx, off_t pos) { in add_bad_block_offset() argument
399 if (ctx->bad_block_count + 1 > ctx->bad_block_alloc) { in add_bad_block_offset()
400 ctx->bad_block_alloc = (ctx->bad_block_alloc*2) + 1; in add_bad_block_offset()
401 ctx->bad_block_offsets = realloc(ctx->bad_block_offsets, in add_bad_block_offset()
402 ctx->bad_block_alloc * sizeof(off_t)); in add_bad_block_offset()
404 ctx->bad_block_offsets[ctx->bad_block_count++] = pos; in add_bad_block_offset()
407 static int write_block(MtdWriteContext *ctx, const char *data) in write_block() argument
409 const MtdPartition *partition = ctx->partition; in write_block()
410 int fd = ctx->fd; in write_block()
420 add_bad_block_offset(ctx, pos); in write_block()
465 add_bad_block_offset(ctx, pos); in write_block()
476 ssize_t mtd_write_data(MtdWriteContext *ctx, const char *data, size_t len) in mtd_write_data() argument
481 if (ctx->stored > 0 || len - wrote < ctx->partition->erase_size) { in mtd_write_data()
482 size_t avail = ctx->partition->erase_size - ctx->stored; in mtd_write_data()
484 memcpy(ctx->buffer + ctx->stored, data + wrote, copy); in mtd_write_data()
485 ctx->stored += copy; in mtd_write_data()
490 if (ctx->stored == ctx->partition->erase_size) { in mtd_write_data()
491 if (write_block(ctx, ctx->buffer)) return -1; in mtd_write_data()
492 ctx->stored = 0; in mtd_write_data()
496 while (ctx->stored == 0 && len - wrote >= ctx->partition->erase_size) { in mtd_write_data()
497 if (write_block(ctx, data + wrote)) return -1; in mtd_write_data()
498 wrote += ctx->partition->erase_size; in mtd_write_data()
505 off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks) in mtd_erase_blocks() argument
508 if (ctx->stored > 0) { in mtd_erase_blocks()
509 size_t zero = ctx->partition->erase_size - ctx->stored; in mtd_erase_blocks()
510 memset(ctx->buffer + ctx->stored, 0, zero); in mtd_erase_blocks()
511 if (write_block(ctx, ctx->buffer)) return -1; in mtd_erase_blocks()
512 ctx->stored = 0; in mtd_erase_blocks()
515 off_t pos = lseek(ctx->fd, 0, SEEK_CUR); in mtd_erase_blocks()
518 const int total = (ctx->partition->size - pos) / ctx->partition->erase_size; in mtd_erase_blocks()
528 if (ioctl(ctx->fd, MEMGETBADBLOCK, &bpos) > 0) { in mtd_erase_blocks()
530 pos += ctx->partition->erase_size; in mtd_erase_blocks()
536 erase_info.length = ctx->partition->erase_size; in mtd_erase_blocks()
537 if (ioctl(ctx->fd, MEMERASE, &erase_info) < 0) { in mtd_erase_blocks()
540 pos += ctx->partition->erase_size; in mtd_erase_blocks()
546 int mtd_write_close(MtdWriteContext *ctx) in mtd_write_close() argument
550 if (mtd_erase_blocks(ctx, 0) == (off_t) -1) r = -1; in mtd_write_close()
551 if (close(ctx->fd)) r = -1; in mtd_write_close()
552 free(ctx->bad_block_offsets); in mtd_write_close()
553 free(ctx->buffer); in mtd_write_close()
554 free(ctx); in mtd_write_close()
561 off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos) { in mtd_find_write_start() argument
563 for (i = 0; i < ctx->bad_block_count; ++i) { in mtd_find_write_start()
564 if (ctx->bad_block_offsets[i] == pos) { in mtd_find_write_start()
565 pos += ctx->partition->erase_size; in mtd_find_write_start()
566 } else if (ctx->bad_block_offsets[i] > pos) { in mtd_find_write_start()