Home | History | Annotate | Download | only in mtdutils

Lines Matching full:ctx

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