Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mtd resetbc #1

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions package/system/mtd/src/linksys_bootcount.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,10 @@ struct bootcounter {
uint32_t checksum;
};

static char page[2048];

int mtd_resetbc(const char *mtd)
{
struct mtd_info_user mtd_info;
struct bootcounter *curr = (struct bootcounter *)page;
struct bootcounter *curr = NULL;
unsigned int i;
unsigned int bc_offset_increment;
int last_count = 0;
Expand Down Expand Up @@ -108,24 +106,23 @@ int mtd_resetbc(const char *mtd)
}

num_bc = mtd_info.size / bc_offset_increment;
curr = malloc(bc_offset_increment);

for (i = 0; i < num_bc; i++) {
pread(fd, curr, sizeof(*curr), i * bc_offset_increment);
pread(fd, curr, sizeof(struct bootcounter), i * bc_offset_increment);

/* Existing code assumes erase is to 0xff; left as-is (2019) */
if (curr->magic == 0xffffffff)
break;

if (curr->magic != BOOTCOUNT_MAGIC &&
curr->magic != 0xffffffff) {
DLOG_ERR("Unexpected magic %08x at offset %08x; aborting.",
curr->magic, i * bc_offset_increment);
if (curr->magic != BOOTCOUNT_MAGIC || curr->checksum != curr->magic + curr->count) {
DLOG_ERR("Unexpected boot-count log at offset %08x: magic %08x boot count %08x checksum %08x; aborting.",
i * bc_offset_increment, curr->magic, curr->count, curr->checksum);

retval = -2;
goto out;
}

if (curr->magic == 0xffffffff)
break;

last_count = curr->count;
}

Expand Down Expand Up @@ -182,6 +179,7 @@ int mtd_resetbc(const char *mtd)
}

out:
if (curr != NULL) free(curr);
close(fd);
return retval;
}
Loading