mtd: check for Seama magic early when fixing MD5
This avoid long (and unneeded) process of reading all data in case of running on MTD not containig Seama entity. Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
This commit is contained in:
parent
320641585b
commit
8632d89fa0
1 changed files with 21 additions and 7 deletions
|
@ -53,7 +53,7 @@ ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
|
||||||
int
|
int
|
||||||
seama_fix_md5(char *buf, size_t len)
|
seama_fix_md5(char *buf, size_t len)
|
||||||
{
|
{
|
||||||
struct seama_hdr *shdr;
|
struct seama_hdr *shdr = (struct seama_hdr *) buf;
|
||||||
char *data;
|
char *data;
|
||||||
size_t msize;
|
size_t msize;
|
||||||
size_t isize;
|
size_t isize;
|
||||||
|
@ -64,12 +64,6 @@ seama_fix_md5(char *buf, size_t len)
|
||||||
if (len < sizeof(struct seama_hdr))
|
if (len < sizeof(struct seama_hdr))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
shdr = (struct seama_hdr *) buf;
|
|
||||||
if (shdr->magic != htonl(SEAMA_MAGIC)) {
|
|
||||||
fprintf(stderr, "no SEAMA header found\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
isize = ntohl(shdr->size);
|
isize = ntohl(shdr->size);
|
||||||
msize = ntohs(shdr->metasize);
|
msize = ntohs(shdr->metasize);
|
||||||
if (isize == 0) {
|
if (isize == 0) {
|
||||||
|
@ -115,9 +109,11 @@ int
|
||||||
mtd_fixseama(const char *mtd, size_t offset)
|
mtd_fixseama(const char *mtd, size_t offset)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
char *first_block;
|
||||||
char *buf;
|
char *buf;
|
||||||
ssize_t res;
|
ssize_t res;
|
||||||
size_t block_offset;
|
size_t block_offset;
|
||||||
|
struct seama_hdr *shdr;
|
||||||
|
|
||||||
if (quiet < 2)
|
if (quiet < 2)
|
||||||
fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
|
fprintf(stderr, "Trying to fix SEAMA header in %s at 0x%x...\n",
|
||||||
|
@ -138,6 +134,24 @@ mtd_fixseama(const char *mtd, size_t offset)
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
first_block = malloc(erasesize);
|
||||||
|
if (!first_block) {
|
||||||
|
perror("malloc");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
res = pread(fd, first_block, erasesize, block_offset);
|
||||||
|
if (res != erasesize) {
|
||||||
|
perror("pread");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
shdr = (struct seama_hdr *)first_block;
|
||||||
|
if (shdr->magic != htonl(SEAMA_MAGIC)) {
|
||||||
|
fprintf(stderr, "No SEAMA header found\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
buf = malloc(mtdsize);
|
buf = malloc(mtdsize);
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
perror("malloc");
|
perror("malloc");
|
||||||
|
|
Loading…
Reference in a new issue